1 /*
2  * Copyright (c) 2022-2023 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 #include <hisysevent.h>
26 
27 using namespace testing::ext;
28 using namespace OHOS::HiviewDFX;
29 using namespace OHOS::PowerMgr;
30 using namespace OHOS::DisplayPowerMgr;
31 using namespace OHOS;
32 using namespace std;
33 
34 namespace {
35 static constexpr uint32_t BRIGHTNESS_SETTING_VALUE = 50;
36 static constexpr uint32_t BRIGHTNESS_OVERRIDE_VALUE = 100;
37 static constexpr uint32_t BRIGHTNESS_DURATION = 0;
38 static constexpr uint32_t BRIGHTNESS_NONE_VALUE = 0;
39 static constexpr uint32_t CODE_VALUE = 100;
40 static constexpr uint32_t DISPLAY_ID = 0;
41 static constexpr uint32_t MAIN_ID = 0;
42 static constexpr uint32_t REASON = 0;
43 static constexpr int32_t BRIGHTNESS_ADJUST_VALUE = 150;
44 static constexpr int32_t TIMEOUT_MS = 500;
45 static constexpr double DISCOUNT_VALUE = 0.30;
46 } // namespace
47 
OnDisplayStateChanged(uint32_t displayId,DisplayPowerMgr::DisplayState state,uint32_t reason)48 void DisplayMockTest::DisplayPowerMgrTestCallback::OnDisplayStateChanged(
49     uint32_t displayId, DisplayPowerMgr::DisplayState state, uint32_t reason)
50 {
51     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrTestCallback::OnDisplayStateChangedStub");
52 }
53 
54 namespace {
55 /**
56  * @tc.name: DisplayMockTest_001
57  * @tc.desc: test DisplayPowerCallbackStub function
58  * @tc.type: FUNC
59  * @tc.require: issueI5YZQR
60  */
61 HWTEST_F(DisplayMockTest, DisplayMockTest_001, TestSize.Level0)
62 {
63     sptr<DisplayPowerCallbackStub> callbackPtr = new DisplayMockTest::DisplayPowerMgrTestCallback();
64     MessageParcel data;
65     MessageParcel reply;
66     MessageOption option;
67     EXPECT_NE(ERR_OK, callbackPtr->OnRemoteRequest(CODE_VALUE, data, reply, option));
68 }
69 
70 /**
71  * @tc.name: DisplayMockTest_002
72  * @tc.desc: test DisplayPowerCallbackStub function
73  * @tc.type: FUNC
74  * @tc.require: issueI5YZQR
75  */
76 HWTEST_F(DisplayMockTest, DisplayMockTest_002, TestSize.Level0)
77 {
78     sptr<DisplayPowerCallbackStub> callbackPtr = new DisplayMockTest::DisplayPowerMgrTestCallback();
79     MessageParcel data;
80     MessageParcel reply;
81     MessageOption option;
82     uint32_t code = static_cast<int32_t>(PowerMgr::DisplayPowerCallbackInterfaceCode::ON_DISPLAY_STATE_CHANGED);
83     EXPECT_NE(ERR_OK, callbackPtr->OnRemoteRequest(code, data, reply, option));
84 }
85 
86 /**
87  * @tc.name: DisplayMockTest_003
88  * @tc.desc: test DisplayPowerMgrProxy function(IRemoteObject and IDisplayPowerCallback is not null)
89  * @tc.type: FUNC
90  * @tc.require: issueI5YZQR
91  */
92 HWTEST_F(DisplayMockTest, DisplayMockTest_003, TestSize.Level0)
93 {
94     sptr<MockDisplayRemoteObject> sptrRemoteObj = new MockDisplayRemoteObject();
95     auto sptrDisplayProxy = std::make_shared<DisplayPowerMgr::DisplayPowerMgrProxy>(sptrRemoteObj);
96     EXPECT_FALSE(sptrDisplayProxy->SetDisplayState(DISPLAY_ID, DisplayPowerMgr::DisplayState::DISPLAY_ON, REASON));
97     EXPECT_NE(DisplayPowerMgr::DisplayState::DISPLAY_ON, sptrDisplayProxy->GetDisplayState(DISPLAY_ID));
98     std::vector<uint32_t> result;
99     result.push_back(DISPLAY_ID);
100     EXPECT_NE(result, sptrDisplayProxy->GetDisplayIds());
101     EXPECT_EQ(MAIN_ID, sptrDisplayProxy->GetMainDisplayId());
102     EXPECT_FALSE(sptrDisplayProxy->SetBrightness(BRIGHTNESS_SETTING_VALUE, DISPLAY_ID, false));
103     EXPECT_FALSE(sptrDisplayProxy->DiscountBrightness(DISCOUNT_VALUE, DISPLAY_ID));
104     EXPECT_FALSE(sptrDisplayProxy->OverrideBrightness(BRIGHTNESS_OVERRIDE_VALUE, DISPLAY_ID));
105     EXPECT_FALSE(sptrDisplayProxy->RestoreBrightness(DISPLAY_ID));
106     EXPECT_EQ(BRIGHTNESS_NONE_VALUE, sptrDisplayProxy->GetBrightness(DISPLAY_ID));
107     EXPECT_EQ(BRIGHTNESS_NONE_VALUE, sptrDisplayProxy->GetDeviceBrightness(DISPLAY_ID));
108     EXPECT_EQ(BRIGHTNESS_NONE_VALUE, sptrDisplayProxy->GetDefaultBrightness());
109     EXPECT_EQ(BRIGHTNESS_NONE_VALUE, sptrDisplayProxy->GetMaxBrightness());
110     EXPECT_EQ(BRIGHTNESS_NONE_VALUE, sptrDisplayProxy->GetMinBrightness());
111     EXPECT_FALSE(sptrDisplayProxy->AdjustBrightness(DISPLAY_ID, BRIGHTNESS_ADJUST_VALUE, BRIGHTNESS_DURATION));
112     EXPECT_FALSE(sptrDisplayProxy->AutoAdjustBrightness(true));
113     EXPECT_FALSE(sptrDisplayProxy->AutoAdjustBrightness(false));
114     EXPECT_FALSE(sptrDisplayProxy->IsAutoAdjustBrightness());
115     EXPECT_FALSE(sptrDisplayProxy->BoostBrightness(TIMEOUT_MS, DISPLAY_ID));
116     EXPECT_FALSE(sptrDisplayProxy->CancelBoostBrightness(DISPLAY_ID));
117 }
118 } // namespace
119