• 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_1/ipower_interface.h"
21 #include "v1_1/power_types.h"
22 
23 using namespace OHOS::HDI;
24 using namespace OHOS::HDI::Power::V1_1;
25 using namespace testing::ext;
26 
27 namespace {
28 sptr<IPowerInterface> g_powerInterface = nullptr;
29 std::mutex g_mutex;
30 const uint32_t MAX_PATH = 256;
31 const uint32_t WAIT_TIME = 1;
32 const std::string SUSPEND_STATE = "mem";
33 const std::string SUSPEND_STATE_PATH = "/sys/power/state";
34 const std::string LOCK_PATH = "/sys/power/wake_lock";
35 const std::string UNLOCK_PATH = "/sys/power/wake_unlock";
36 class HdfPowerHdiTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     std::string ReadFile(const std::string& file);
40 };
41 
SetUpTestCase()42 void HdfPowerHdiTest::SetUpTestCase()
43 {
44     g_powerInterface = IPowerInterface::Get(true);
45 }
46 
ReadFile(const std::string & file)47 std::string HdfPowerHdiTest::ReadFile(const std::string& file)
48 {
49     std::ifstream ifs;
50     ifs.open(file);
51     if (!ifs.is_open()) {
52         return "";
53     }
54     std::string line;
55     std::getline(ifs, line);
56 
57     ifs.close();
58     return line;
59 }
60 }
61 
62 namespace {
63 /**
64   * @tc.name: HdfPowerHdiTest001
65   * @tc.desc: Get a client and check whether the client is empty.
66   * @tc.type: FUNC
67   */
68 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest001, TestSize.Level1)
69 {
70     ASSERT_NE(nullptr, g_powerInterface);
71 }
72 
73 /**
74   * @tc.name: HdfPowerHdiTest002
75   * @tc.desc: check startsuspend
76   * @tc.type: FUNC
77   */
78 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest002, TestSize.Level1)
79 {
80     int32_t ret = g_powerInterface->StartSuspend();
81     EXPECT_EQ(0, ret);
82 
83     char stateBuf[MAX_PATH] = {0};
84     std::string stateValue;
85 
86     ret = snprintf_s(stateBuf, MAX_PATH, sizeof(stateBuf) - 1, SUSPEND_STATE_PATH.c_str());
87     EXPECT_FALSE(ret < EOK);
88     sleep(WAIT_TIME);
89     stateValue = HdfPowerHdiTest::ReadFile(stateBuf);
90     std::string state = stateValue;
91     auto it = state.find(SUSPEND_STATE);
92     EXPECT_TRUE(it != std::string::npos);
93 }
94 
95 /**
96   * @tc.name: HdfPowerHdiTest003
97   * @tc.desc: check StopSuspend
98   * @tc.type: FUNC
99   */
100 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest003, TestSize.Level1)
101 {
102     int32_t ret = g_powerInterface->StopSuspend();
103     EXPECT_EQ(0, ret) << "HdfPowerHdiTest003 failed";
104 }
105 
106 /**
107   * @tc.name: HdfPowerHdiTest005
108   * @tc.desc: check SuspendBlock
109   * @tc.type: FUNC
110   */
111 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest005, TestSize.Level1)
112 {
113     std::string testName = "HdfPowerHdiTest005";
114     int32_t ret = g_powerInterface->SuspendBlock(testName);
115     EXPECT_EQ(0, ret);
116 
117     char lockBuf[MAX_PATH] = {0};
118     std::string lockValue;
119 
120     ret = snprintf_s(lockBuf, MAX_PATH, sizeof(lockBuf) - 1, LOCK_PATH.c_str());
121     EXPECT_FALSE(ret < EOK);
122 
123     sleep(WAIT_TIME);
124     lockValue = HdfPowerHdiTest::ReadFile(lockBuf);
125     std::string lock = lockValue;
126     auto it = lock.find(testName);
127     EXPECT_TRUE(it != std::string::npos);
128     g_powerInterface->SuspendUnblock(testName);
129 }
130 
131 /**
132   * @tc.name: HdfPowerHdiTest006
133   * @tc.desc: check SuspendUnblock
134   * @tc.type: FUNC
135   */
136 HWTEST_F(HdfPowerHdiTest, HdfPowerHdiTest006, TestSize.Level1)
137 {
138     std::string testName = "HdfPowerHdiTest006";
139     g_powerInterface->SuspendBlock(testName);
140     sleep(WAIT_TIME);
141     int32_t ret = g_powerInterface->SuspendUnblock(testName);
142     EXPECT_EQ(0, ret);
143 
144     char unLockBuf[MAX_PATH] = {0};
145     std::string unLockValue;
146 
147     ret = snprintf_s(unLockBuf, MAX_PATH, sizeof(unLockBuf) - 1, UNLOCK_PATH.c_str());
148     EXPECT_FALSE(ret < EOK);
149 
150     sleep(WAIT_TIME);
151     unLockValue = HdfPowerHdiTest::ReadFile(unLockBuf);
152     std::string unLock = unLockValue;
153     auto it = unLock.find(testName);
154     EXPECT_TRUE(it != std::string::npos);
155 }
156 }