1 /*
2 * Copyright (c) 2023 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: PowerParseSourceMockTest
55 * @tc.desc: test ParseSources(exception)
56 * @tc.type: FUNC
57 * @tc.require: issueI7G6OY
58 */
59 HWTEST_F(PowerParseSourceMockTest, PowerParseSourceMockTest001, TestSize.Level0)
60 {
61 GTEST_LOG_(INFO) << "PowerParseSourceMockTest001: start";
62
63 auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
64 if (pmsTest_ == nullptr) {
65 GTEST_LOG_(INFO) << "PowerParseSourceMockTest001: Failed to get PowerMgrService";
66 }
67
68 std::shared_ptr<SuspendSources> sources1 = SuspendSourceParser::ParseSources();
69 EXPECT_TRUE(SettingHelper::IsSuspendSourcesSettingValid() == true);
70
71 std::shared_ptr<WakeupSources> sources2 = WakeupSourceParser::ParseSources();
72 EXPECT_TRUE(SettingHelper::IsSuspendSourcesSettingValid() == true);
73
74 std::vector<std::string> tmp;
75 static const std::string jsonStr = "{\"powerkey\":";
76 std::shared_ptr<SuspendSources> sources3 = SuspendSourceParser::ParseSources(jsonStr);
77 tmp = sources3->getSourceKeys();
78 EXPECT_TRUE(tmp.size() != 0);
79 std::shared_ptr<WakeupSources> sources4 = WakeupSourceParser::ParseSources(jsonStr);
80 tmp = sources4->getSourceKeys();
81 EXPECT_TRUE(tmp.size() != 0);
82
83 static const std::string jsonStr2 =
84 "{\"powerkey\": {\"enable\": false},\"keyborad\": {\"enable\": false},\"mouse\": {\"enable\": "
85 "false},\"touchscreen\": {\"enable\": false,\"click\": 2},\"touchpad\": {\"enable\": false},\"pen\": "
86 "{\"enable\": false},\"lid\": {\"enable\": false},\"switch\": {\"enable\": false}}";
87 std::shared_ptr<WakeupSources> sources5 = WakeupSourceParser::ParseSources(jsonStr2);
88 tmp = sources5->getSourceKeys();
89 EXPECT_TRUE(tmp.size() != 0);
90
91 static const std::string jsonStr3 =
92 "{ \"powerkey\": { \"action\": 0, \"delayMs\": 1002 }, \"timeout\": { \"action\": 1, \"delayMs\": 2002 }, "
93 "\"lid\": { \"action\": 1, \"delayMs\": 3002 }, \"switch\": { \"action\": 1, \"delayMs\": 4002 } }";
94 std::shared_ptr<SuspendSources> sources6 = SuspendSourceParser::ParseSources(jsonStr3);
95 tmp = sources6->getSourceKeys();
96 EXPECT_TRUE(tmp.size() != 0);
97
98 GTEST_LOG_(INFO) << "PowerParseSourceMockTest001: end";
99 }
100 } // namespace