1 /*
2  * Copyright (c) 2023-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 #include "power_parsesources_mock_test.h"
16 #include <fstream>
17 #include <thread>
18 #include <unistd.h>
19 
20 #include <datetime_ex.h>
21 #include <input_manager.h>
22 #include <securec.h>
23 
24 #include "power_mgr_service.h"
25 #include "power_state_callback_stub.h"
26 #include "power_state_machine.h"
27 #include "setting_helper.h"
28 
29 using namespace testing::ext;
30 using namespace OHOS::PowerMgr;
31 using namespace OHOS;
32 using namespace std;
33 static sptr<PowerMgrService> g_service;
34 
IsSuspendSourcesSettingValid()35 bool SettingHelper::IsSuspendSourcesSettingValid()
36 {
37     return true;
38 }
39 
ParseSourcesProc( std::shared_ptr<SuspendSources>& parseSources, Json::Value& valueObj, std::string& key)40 bool SuspendSourceParser::ParseSourcesProc(
41     std::shared_ptr<SuspendSources>& parseSources, Json::Value& valueObj, std::string& key)
42 {
43     return false;
44 }
45 
ParseSourcesProc( std::shared_ptr<WakeupSources>& parseSources, Json::Value& valueObj, std::string& key)46 bool WakeupSourceParser::ParseSourcesProc(
47     std::shared_ptr<WakeupSources>& parseSources, Json::Value& valueObj, std::string& key)
48 {
49     return false;
50 }
51 
52 namespace {
53 /**
54  * @tc.name: PowerParseSourceMockTest001
55  * @tc.desc: test ParseSources(exception)
56  * @tc.type: FUNC
57  * @tc.require: issueI7G6OY
58  */
HWTEST_F(PowerParseSourceMockTest, PowerParseSourceMockTest001, TestSize.Level0)59 HWTEST_F(PowerParseSourceMockTest, PowerParseSourceMockTest001, TestSize.Level0)
60 {
61     POWER_HILOGD(LABEL_TEST, "PowerParseSourceMockTest001 start.");
62     GTEST_LOG_(INFO) << "PowerParseSourceMockTest001: start";
63 
64     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
65     if (pmsTest_ == nullptr) {
66         GTEST_LOG_(INFO) << "PowerParseSourceMockTest001: Failed to get PowerMgrService";
67     }
68 
69     std::shared_ptr<SuspendSources> sources1 = SuspendSourceParser::ParseSources();
70     EXPECT_TRUE(SettingHelper::IsSuspendSourcesSettingValid() == true);
71 
72     std::shared_ptr<WakeupSources> sources2 = WakeupSourceParser::ParseSources();
73     EXPECT_TRUE(SettingHelper::IsSuspendSourcesSettingValid() == true);
74 
75     std::vector<std::string> tmp;
76     static const std::string jsonStr = "{\"powerkey\":";
77     std::shared_ptr<SuspendSources> sources3 = SuspendSourceParser::ParseSources(jsonStr);
78     tmp = sources3->getSourceKeys();
79     EXPECT_TRUE(tmp.size() != 0);
80     std::shared_ptr<WakeupSources> sources4 = WakeupSourceParser::ParseSources(jsonStr);
81     tmp = sources4->getSourceKeys();
82     EXPECT_TRUE(tmp.size() != 0);
83 
84     static const std::string jsonStr2 =
85         "{\"powerkey\": {\"enable\": false},\"keyborad\": {\"enable\": false},\"mouse\": {\"enable\": "
86         "false},\"touchscreen\": {\"enable\": false,\"click\": 2},\"touchpad\": {\"enable\": false},\"pen\": "
87         "{\"enable\": false},\"lid\": {\"enable\": false},\"switch\": {\"enable\": false}}";
88     std::shared_ptr<WakeupSources> sources5 = WakeupSourceParser::ParseSources(jsonStr2);
89     tmp = sources5->getSourceKeys();
90     EXPECT_TRUE(tmp.size() != 0);
91 
92     static const std::string jsonStr3 =
93         "{ \"powerkey\": { \"action\": 0, \"delayMs\": 1002 }, \"timeout\": { \"action\": 1, \"delayMs\": 2002 }, "
94         "\"lid\": { \"action\": 1, \"delayMs\": 3002 }, \"switch\": { \"action\": 1, \"delayMs\": 4002 } }";
95     std::shared_ptr<SuspendSources> sources6 = SuspendSourceParser::ParseSources(jsonStr3);
96     tmp = sources6->getSourceKeys();
97     EXPECT_TRUE(tmp.size() != 0);
98     POWER_HILOGD(LABEL_TEST, "PowerParseSourceMockTest001 end.");
99     GTEST_LOG_(INFO) << "PowerParseSourceMockTest001:  end";
100 }
101 } // namespace