• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "display_mock_test.h"
17 #include "display_log.h"
18 #include "display_mgr_errors.h"
19 #include "display_power_callback_ipc_interface_code.h"
20 #include "display_power_mgr_client.h"
21 #include "display_power_mgr_proxy.h"
22 #include "ipc_object_stub.h"
23 #include "mock_display_remote_object.h"
24 #include "power_state_machine_info.h"
25 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
26 #include <hisysevent.h>
27 #endif
28 
29 using namespace testing::ext;
30 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
31 using namespace OHOS::HiviewDFX;
32 #endif
33 using namespace OHOS::PowerMgr;
34 using namespace OHOS::DisplayPowerMgr;
35 using namespace OHOS;
36 using namespace std;
37 
38 namespace {
39 static constexpr uint32_t BRIGHTNESS_SETTING_VALUE = 50;
40 static constexpr uint32_t BRIGHTNESS_OVERRIDE_VALUE = 100;
41 static constexpr uint32_t BRIGHTNESS_DURATION = 0;
42 static constexpr uint32_t BRIGHTNESS_NONE_VALUE = 0;
43 static constexpr uint32_t CODE_VALUE = 100;
44 static constexpr uint32_t DISPLAY_ID = 0;
45 static constexpr uint32_t MAIN_ID = 0;
46 static constexpr uint32_t REASON = 0;
47 static constexpr int32_t BRIGHTNESS_ADJUST_VALUE = 150;
48 static constexpr int32_t TIMEOUT_MS = 500;
49 static constexpr double DISCOUNT_VALUE = 0.30;
50 static constexpr uint32_t DEFAULT_DURATION = 500;
51 static constexpr uint32_t ERR_OK = 0;
52 } // namespace
53 
OnDisplayStateChanged(uint32_t displayId,DisplayPowerMgr::DisplayState state,uint32_t reason)54 void DisplayMockTest::DisplayPowerMgrTestCallback::OnDisplayStateChanged(
55     uint32_t displayId, DisplayPowerMgr::DisplayState state, uint32_t reason)
56 {
57     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrTestCallback::OnDisplayStateChangedStub");
58 }
59 
60 namespace {
61 /**
62  * @tc.name: DisplayMockTest_001
63  * @tc.desc: test DisplayPowerCallbackStub function
64  * @tc.type: FUNC
65  * @tc.require: issueI5YZQR
66  */
67 HWTEST_F(DisplayMockTest, DisplayMockTest_001, TestSize.Level0)
68 {
69     DISPLAY_HILOGI(LABEL_TEST, "DisplayMockTest_001 function start!");
70     sptr<DisplayPowerCallbackStub> callbackPtr = new DisplayMockTest::DisplayPowerMgrTestCallback();
71     MessageParcel data;
72     MessageParcel reply;
73     MessageOption option;
74     EXPECT_NE(ERR_OK, callbackPtr->OnRemoteRequest(CODE_VALUE, data, reply, option));
75     DISPLAY_HILOGI(LABEL_TEST, "DisplayMockTest_001 function end!");
76 }
77 
78 /**
79  * @tc.name: DisplayMockTest_002
80  * @tc.desc: test DisplayPowerCallbackStub function
81  * @tc.type: FUNC
82  * @tc.require: issueI5YZQR
83  */
84 HWTEST_F(DisplayMockTest, DisplayMockTest_002, TestSize.Level0)
85 {
86     DISPLAY_HILOGI(LABEL_TEST, "DisplayMockTest_002 function start!");
87     sptr<DisplayPowerCallbackStub> callbackPtr = new DisplayMockTest::DisplayPowerMgrTestCallback();
88     MessageParcel data;
89     MessageParcel reply;
90     MessageOption option;
91     uint32_t code = static_cast<int32_t>(PowerMgr::DisplayPowerCallbackInterfaceCode::ON_DISPLAY_STATE_CHANGED);
92     EXPECT_NE(ERR_OK, callbackPtr->OnRemoteRequest(code, data, reply, option));
93     DISPLAY_HILOGI(LABEL_TEST, "DisplayMockTest_002 function end!");
94 }
95 
96 /**
97  * @tc.name: DisplayMockTest_003
98  * @tc.desc: test DisplayPowerMgrProxy function(IRemoteObject and IDisplayPowerCallback is not null)
99  * @tc.type: FUNC
100  * @tc.require: issueI5YZQR
101  */
102 HWTEST_F(DisplayMockTest, DisplayMockTest_003, TestSize.Level0)
103 {
104     DISPLAY_HILOGI(LABEL_TEST, "DisplayMockTest_003 function start!");
105     sptr<MockDisplayRemoteObject> sptrRemoteObj = new MockDisplayRemoteObject();
106     auto sptrDisplayProxy = std::make_shared<DisplayPowerMgr::DisplayPowerMgrProxy>(sptrRemoteObj);
107     bool bRet = false;
108     EXPECT_NE(sptrDisplayProxy->SetDisplayState(DISPLAY_ID,
109         static_cast<uint32_t>(DisplayPowerMgr::DisplayState::DISPLAY_ON), REASON, bRet), ERR_OK);
110     EXPECT_FALSE(bRet);
111     int32_t state = 0;
112     EXPECT_NE(sptrDisplayProxy->GetDisplayState(DISPLAY_ID, state), ERR_OK);
113     EXPECT_NE(DisplayPowerMgr::DisplayState::DISPLAY_ON, DisplayPowerMgr::DisplayState(state));
114     std::vector<uint32_t> result;
115     result.push_back(DISPLAY_ID);
116     std::vector<uint32_t> ids;
117     EXPECT_NE(sptrDisplayProxy->GetDisplayIds(ids), ERR_OK);
118     EXPECT_NE(result, ids);
119     uint32_t id = 0;
120     EXPECT_NE(sptrDisplayProxy->GetMainDisplayId(id), ERR_OK);
121     EXPECT_EQ(MAIN_ID, id);
122     DISPLAY_HILOGI(LABEL_TEST, "DisplayMockTest_003 function end!");
123 }
124 
125 /**
126  * @tc.name: DisplayMockTest_004
127  * @tc.desc: test DisplayPowerMgrProxy function(IRemoteObject and IDisplayPowerCallback is not null)
128  * @tc.type: FUNC
129  * @tc.require:
130  */
131 HWTEST_F(DisplayMockTest, DisplayMockTest_004, TestSize.Level0)
132 {
133     DISPLAY_HILOGI(LABEL_TEST, "DisplayMockTest_004 function start!");
134     sptr<MockDisplayRemoteObject> sptrRemoteObj = new MockDisplayRemoteObject();
135     auto sptrDisplayProxy = std::make_shared<DisplayPowerMgr::DisplayPowerMgrProxy>(sptrRemoteObj);
136     bool bRet = false;
137     uint32_t brightness = 0;
138     int32_t errCode = 0;
139     EXPECT_NE(sptrDisplayProxy->SetBrightness(BRIGHTNESS_SETTING_VALUE, DISPLAY_ID, false, bRet, errCode), ERR_OK);
140     EXPECT_FALSE(bRet);
141     EXPECT_NE(sptrDisplayProxy->DiscountBrightness(DISCOUNT_VALUE, DISPLAY_ID, bRet), ERR_OK);
142     EXPECT_FALSE(bRet);
143     EXPECT_NE(sptrDisplayProxy->OverrideBrightness(BRIGHTNESS_OVERRIDE_VALUE, DISPLAY_ID, DEFAULT_DURATION, bRet),
144         ERR_OK);
145     EXPECT_FALSE(bRet);
146     EXPECT_NE(sptrDisplayProxy->RestoreBrightness(DISPLAY_ID, DEFAULT_DURATION, bRet), ERR_OK);
147     EXPECT_FALSE(bRet);
148     EXPECT_NE(sptrDisplayProxy->GetBrightness(DISPLAY_ID, brightness), ERR_OK);
149     EXPECT_EQ(BRIGHTNESS_NONE_VALUE, brightness);
150     EXPECT_NE(sptrDisplayProxy->GetDeviceBrightness(DISPLAY_ID, brightness), ERR_OK);
151     EXPECT_EQ(BRIGHTNESS_NONE_VALUE, brightness);
152     EXPECT_NE(sptrDisplayProxy->GetDefaultBrightness(brightness), ERR_OK);
153     EXPECT_EQ(BRIGHTNESS_NONE_VALUE, brightness);
154     EXPECT_NE(sptrDisplayProxy->GetMaxBrightness(brightness), ERR_OK);
155     EXPECT_EQ(BRIGHTNESS_NONE_VALUE, brightness);
156     EXPECT_NE(sptrDisplayProxy->GetMinBrightness(brightness), ERR_OK);
157     EXPECT_EQ(BRIGHTNESS_NONE_VALUE, brightness);
158     EXPECT_NE(sptrDisplayProxy->AdjustBrightness(DISPLAY_ID, BRIGHTNESS_ADJUST_VALUE, BRIGHTNESS_DURATION, bRet),
159         ERR_OK);
160     EXPECT_FALSE(bRet);
161     EXPECT_NE(sptrDisplayProxy->AutoAdjustBrightness(true, bRet), ERR_OK);
162     EXPECT_FALSE(bRet);
163     EXPECT_NE(sptrDisplayProxy->AutoAdjustBrightness(false, bRet), ERR_OK);
164     EXPECT_FALSE(bRet);
165     EXPECT_NE(sptrDisplayProxy->IsAutoAdjustBrightness(bRet), ERR_OK);
166     EXPECT_FALSE(bRet);
167     EXPECT_NE(sptrDisplayProxy->BoostBrightness(TIMEOUT_MS, DISPLAY_ID, bRet), ERR_OK);
168     EXPECT_FALSE(bRet);
169     EXPECT_NE(sptrDisplayProxy->CancelBoostBrightness(DISPLAY_ID, bRet), ERR_OK);
170     EXPECT_FALSE(bRet);
171     DISPLAY_HILOGI(LABEL_TEST, "DisplayMockTest_004 function end!");
172 }
173 } // namespace
174