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