• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <fstream>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 
20 #include "v1_2/ipower_interface.h"
21 #include "v1_2/power_types.h"
22 #include "v1_2/running_lock_types.h"
23 
24 using namespace OHOS::HDI;
25 using namespace OHOS::HDI::Power::V1_2;
26 using namespace testing::ext;
27 
28 namespace {
29 sptr<IPowerInterface> g_powerInterface = nullptr;
30 std::mutex g_mutex;
31 const uint32_t MAX_PATH = 256;
32 const uint32_t WAIT_TIME = 1;
33 const std::string SUSPEND_STATE = "mem";
34 const std::string SUSPEND_STATE_PATH = "/sys/power/state";
35 const std::string LOCK_PATH = "/sys/power/wake_lock";
36 const std::string UNLOCK_PATH = "/sys/power/wake_unlock";
37 class HdfPowerHdiTest : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     std::string ReadFile(const std::string& file);
41 };
42 
SetUpTestCase()43 void HdfPowerHdiTest::SetUpTestCase()
44 {
45     g_powerInterface = IPowerInterface::Get(true);
46 }
47 
ReadFile(const std::string & file)48 std::string HdfPowerHdiTest::ReadFile(const std::string& file)
49 {
50     std::ifstream ifs;
51     ifs.open(file);
52     if (!ifs.is_open()) {
53         return "";
54     }
55     std::string line;
56     std::getline(ifs, line);
57 
58     ifs.close();
59     return line;
60 }
61 }
62 
63 namespace {
64 /**
65   * @tc.name: HdfPowerHdiTest001
66   * @tc.desc: Get a client and check whether the client is empty.
67   * @tc.type: FUNC
68   */
69 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest001, TestSize.Level1)
70 {
71     ASSERT_NE(nullptr, g_powerInterface);
72 }
73 
74 /**
75   * @tc.name: HdfPowerHdiTest002
76   * @tc.desc: check startsuspend
77   * @tc.type: FUNC
78   */
79 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest002, TestSize.Level1)
80 {
81     int32_t ret = g_powerInterface->StartSuspend();
82     EXPECT_EQ(0, ret);
83 
84     char stateBuf[MAX_PATH] = {0};
85     std::string stateValue;
86 
87     ret = snprintf_s(stateBuf, MAX_PATH, sizeof(stateBuf) - 1, SUSPEND_STATE_PATH.c_str());
88     EXPECT_FALSE(ret < EOK);
89     sleep(WAIT_TIME);
90     stateValue = HdfPowerHdiTest::ReadFile(stateBuf);
91     std::string state = stateValue;
92     auto it = state.find(SUSPEND_STATE);
93     EXPECT_TRUE(it != std::string::npos);
94 }
95 
96 /**
97   * @tc.name: HdfPowerHdiTest003
98   * @tc.desc: check StopSuspend
99   * @tc.type: FUNC
100   */
101 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest003, TestSize.Level1)
102 {
103     int32_t ret = g_powerInterface->StopSuspend();
104     EXPECT_EQ(0, ret) << "HdfPowerHdiTest003 failed";
105 }
106 
107 /**
108   * @tc.name: HdfPowerHdiTest005
109   * @tc.desc: check SuspendBlock
110   * @tc.type: FUNC
111   */
112 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest005, TestSize.Level1)
113 {
114     std::string testName = "HdfPowerHdiTest005";
115     int32_t ret = g_powerInterface->SuspendBlock(testName);
116     EXPECT_EQ(0, ret);
117 
118     char lockBuf[MAX_PATH] = {0};
119     std::string lockValue;
120 
121     ret = snprintf_s(lockBuf, MAX_PATH, sizeof(lockBuf) - 1, LOCK_PATH.c_str());
122     EXPECT_FALSE(ret < EOK);
123 
124     sleep(WAIT_TIME);
125     lockValue = HdfPowerHdiTest::ReadFile(lockBuf);
126     std::string lock = lockValue;
127     auto it = lock.find(testName);
128     EXPECT_TRUE(it != std::string::npos);
129     g_powerInterface->SuspendUnblock(testName);
130 }
131 
132 /**
133   * @tc.name: HdfPowerHdiTest006
134   * @tc.desc: check SuspendUnblock
135   * @tc.type: FUNC
136   */
137 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest006, TestSize.Level1)
138 {
139     std::string testName = "HdfPowerHdiTest006";
140     g_powerInterface->SuspendBlock(testName);
141     sleep(WAIT_TIME);
142     int32_t ret = g_powerInterface->SuspendUnblock(testName);
143     EXPECT_EQ(0, ret);
144 
145     char unLockBuf[MAX_PATH] = {0};
146     std::string unLockValue;
147 
148     ret = snprintf_s(unLockBuf, MAX_PATH, sizeof(unLockBuf) - 1, UNLOCK_PATH.c_str());
149     EXPECT_FALSE(ret < EOK);
150 
151     sleep(WAIT_TIME);
152     unLockValue = HdfPowerHdiTest::ReadFile(unLockBuf);
153     std::string unLock = unLockValue;
154     auto it = unLock.find(testName);
155     EXPECT_TRUE(it != std::string::npos);
156 }
157 
158 /**
159   * @tc.name: HdfPowerHdiTest007
160   * @tc.desc: check GetWakeupReason
161   * @tc.type: FUNC
162   */
163 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest007, TestSize.Level1)
164 {
165     std::string testName = "HdfPowerHdiTest007";
166     int32_t ret = g_powerInterface->GetWakeupReason(testName);
167 #ifdef DRIVER_PERIPHERAL_POWER_WAKEUP_CAUSE_PATH
168     EXPECT_EQ(0, ret);
169 #else
170     EXPECT_NE(0, ret);
171 #endif
172 }
173 
174 /**
175   * @tc.name: HdfPowerHdiTest008
176   * @tc.desc: check GetWakeupReason
177   * @tc.type: FUNC
178   */
179 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest008, TestSize.Level1)
180 {
181     std::string testName = "HdfPowerHdiTest008";
182     RunningLockInfo filledInfo;
183     filledInfo.name = testName;
184     filledInfo.type = RUNNINGLOCK_BUTT;
185     filledInfo.uid = 0;
186     filledInfo.pid = 0;
187     int32_t ret = g_powerInterface->HoldRunningLockExt(filledInfo, 0, testName);
188     EXPECT_NE(0, ret);
189     ret = g_powerInterface->UnholdRunningLockExt(filledInfo, 0, testName);
190     EXPECT_NE(0, ret);
191 }
192 }