1 /*
2 * Copyright (c) 2023-2025 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 #include "power_screenon_mock_test.h"
16 #include <fstream>
17 #include <thread>
18 #include <unistd.h>
19
20 #ifdef POWERMGR_GTEST
21 #define private public
22 #define protected public
23 #endif
24
25 #include <datetime_ex.h>
26 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
27 #include <input_manager.h>
28 #endif
29 #include <securec.h>
30
31 #include "permission.h"
32 #include "power_mgr_service.h"
33 #include "power_state_callback_stub.h"
34 #include "power_state_machine.h"
35 #include "setting_helper.h"
36 #include "power_log.h"
37
38 using namespace testing::ext;
39 using namespace OHOS::PowerMgr;
40 using namespace OHOS;
41 using namespace std;
42 static sptr<PowerMgrService> g_service;
43 static constexpr int32_t TEST_MODEL = 2;
44 static constexpr int SLEEP_WAIT_TIME_S = 2;
45
46 namespace {
47 bool g_isSystem = true;
48 bool g_isPermissionGranted = true;
49 } // namespace
50
51 namespace OHOS::PowerMgr {
SetUpTestCase(void)52 void PowerScreenOnMockTest::SetUpTestCase(void)
53 {
54 g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
55 g_service->OnStart();
56 }
57
TearDownTestCase(void)58 void PowerScreenOnMockTest::TearDownTestCase(void)
59 {
60 g_service->OnStop();
61 DelayedSpSingleton<PowerMgrService>::DestroyInstance();
62 }
63
SetUp(void)64 void PowerScreenOnMockTest::SetUp(void)
65 {
66 EXPECT_TRUE(g_isSystem);
67 EXPECT_TRUE(g_isPermissionGranted);
68 }
69
TearDown(void)70 void PowerScreenOnMockTest::TearDown(void)
71 {
72 g_isSystem = true;
73 g_isPermissionGranted = true;
74 }
75
IsScreenOn(bool needPrintLog)76 bool PowerMgrService::IsScreenOn(bool needPrintLog)
77 {
78 static uint32_t count = 0;
79 bool ret = true;
80
81 if ((count % TEST_MODEL)) {
82 ret = !ret;
83 }
84 count++;
85 return ret;
86 }
87
IsSystem()88 bool Permission::IsSystem()
89 {
90 GTEST_LOG_(INFO) << "PowerScreenOnMockTest g_isSystem: " << g_isSystem;
91 return g_isSystem;
92 }
93
IsPermissionGranted(const std::string & perm)94 bool Permission::IsPermissionGranted(const std::string& perm)
95 {
96 GTEST_LOG_(INFO) << "PowerScreenOnMockTest IsPermissionGranted: " << g_isSystem;
97 return g_isPermissionGranted;
98 }
99 } // namespace OHOS::PowerMgr
100
101
102 namespace {
103 /**
104 * @tc.name: PowerScreenOnMockTest001
105 * @tc.desc: test ControlListener(exception)
106 * @tc.type: FUNC
107 * @tc.require: issueI7G6OY
108 */
109 HWTEST_F(PowerScreenOnMockTest, PowerScreenOnMockTest001, TestSize.Level1)
110 {
111 POWER_HILOGI(LABEL_TEST, "PowerScreenOnMockTest001 function start!");
112 GTEST_LOG_(INFO) << "PowerScreenOnMockTest001: start";
113
114 auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
115 if (pmsTest_ == nullptr) {
116 GTEST_LOG_(INFO) << "PowerScreenOnMockTest001: Failed to get PowerMgrService";
117 }
118 pmsTest_->Init();
119 pmsTest_->WakeupControllerInit();
120 pmsTest_->wakeupController_->stateMachine_->stateAction_->SetDisplayState(DisplayState::DISPLAY_OFF);
121 pmsTest_->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
122 EXPECT_TRUE(pmsTest_ != nullptr);
123
124 sleep(SLEEP_WAIT_TIME_S);
125 POWER_HILOGI(LABEL_TEST, "PowerScreenOnMockTest001 function end!");
126 GTEST_LOG_(INFO) << "PowerScreenOnMockTest001: end";
127 }
128
129 /**
130 * @tc.name: PowerScreenOnMockTest002
131 * @tc.desc: test RefreshActivity(exception)
132 * @tc.type: FUNC
133 * @tc.require: issueI7G6OY
134 */
135 HWTEST_F(PowerScreenOnMockTest, PowerScreenOnMockTest002, TestSize.Level1)
136 {
137 POWER_HILOGI(LABEL_TEST, "PowerScreenOnMockTest002 function start!");
138 auto pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
139 if (pmsTest == nullptr) {
140 GTEST_LOG_(INFO) << "PowerScreenOnMockTest002: Failed to get PowerMgrService";
141 }
142 pmsTest->Init();
143 std::string reason = "PowerScreenOnMockTest002";
144 PowerErrors ret =
145 pmsTest->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_APPLICATION, reason);
146 EXPECT_EQ(ret, PowerErrors::ERR_OK);
147 ret = pmsTest->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_APPLICATION, reason);
148 EXPECT_EQ(ret, PowerErrors::ERR_FREQUENT_FUNCTION_CALL);
149 g_isPermissionGranted = false;
150 ret = pmsTest->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_APPLICATION, reason);
151 EXPECT_EQ(ret, PowerErrors::ERR_PERMISSION_DENIED);
152 g_isSystem = false;
153 ret = pmsTest->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_APPLICATION, reason);
154 EXPECT_EQ(ret, PowerErrors::ERR_SYSTEM_API_DENIED);
155 POWER_HILOGI(LABEL_TEST, "PowerScreenOnMockTest002 function end!");
156 }
157 } // namespace