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 "plugin_switch_test.h"
17 #include "plugin_mgr.h"
18 
19 using namespace std;
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 namespace ResourceSchedule {
24 const string TEST_PREFIX_SWITCH_PATH = "/data/test/resource/resschedfwk/parseswitch/";
25 
SetUpTestCase()26 void PluginSwitchTest::SetUpTestCase() {}
27 
TearDownTestCase()28 void PluginSwitchTest::TearDownTestCase() {}
29 
SetUp()30 void PluginSwitchTest::SetUp()
31 {
32     /**
33      * @tc.setup: initialize the member variable pluginSwitch_
34      */
35     pluginSwitch_ = make_shared<PluginSwitch>();
36 }
37 
TearDown()38 void PluginSwitchTest::TearDown()
39 {
40     /**
41      * @tc.teardown: clear pluginSwitch_
42      */
43     pluginSwitch_ = nullptr;
44 }
45 
46 /**
47  * @tc.name: Init plugin Switch LoadConfig001
48  * @tc.desc: Verify if can Init the plugin correctly
49  * @tc.type: FUNC
50  * @tc.require: issueI798UT
51  * @tc.author:xukuan
52  */
53 HWTEST_F(PluginSwitchTest, LoadConfig001, TestSize.Level1)
54 {
55     pluginSwitch_ = make_unique<PluginSwitch>();
56     std::vector<std::string> contents;
57     PluginMgr::GetInstance().GetConfigContent(-1, TEST_PREFIX_SWITCH_PATH + "fileNotExist", contents);
58     bool ret = false;
59     for (auto content : contents) {
60         ret = pluginSwitch_->LoadFromConfigContent(content);
61     }
62     EXPECT_TRUE(!ret);
63 }
64 
65 /**
66  * @tc.name: Init plugin Switch LoadConfig002
67  * @tc.desc: Verify if can load file while xml does not have pluginlist node
68  * @tc.type: FUNC
69  * @tc.require: issueI798UT
70  * @tc.author:xukuan
71  */
72 HWTEST_F(PluginSwitchTest, LoadConfig002, TestSize.Level1)
73 {
74     pluginSwitch_ = make_unique<PluginSwitch>();
75     std::vector<std::string> contents;
76     PluginMgr::GetInstance().GetConfigContent(-1, TEST_PREFIX_SWITCH_PATH + "not_exist_plugin.xml", contents);
77     bool ret = false;
78     for (auto content : contents) {
79         ret = pluginSwitch_->LoadFromConfigContent(content);
80     }
81     EXPECT_TRUE(!ret);
82 }
83 
84 /**
85  * @tc.name: Init plugin Switch LoadConfig003
86  * @tc.desc: Verify if can load file invalid format config file
87  * @tc.type: FUNC
88  * @tc.require: issueI798UT
89  * @tc.author:xukuan
90  */
91 HWTEST_F(PluginSwitchTest, LoadConfig003, TestSize.Level1)
92 {
93     std::vector<std::string> contents;
94     PluginMgr::GetInstance().GetConfigContent(-1, TEST_PREFIX_SWITCH_PATH + "invalid_format.xml", contents);
95     bool ret = false;
96     for (auto content : contents) {
97         ret = pluginSwitch_->LoadFromConfigContent(content);
98     }
99     EXPECT_TRUE(!ret);
100 }
101 
102 /**
103  * @tc.name: Init plugin Switch LoadConfig004
104  * @tc.desc: Verify if can load wrong root element config
105  * @tc.type: FUNC
106  * @tc.require: issueI798UT
107  * @tc.author:xukuan
108  */
109 HWTEST_F(PluginSwitchTest, LoadConfig004, TestSize.Level1)
110 {
111     std::vector<std::string> contents;
112     PluginMgr::GetInstance().GetConfigContent(-1, TEST_PREFIX_SWITCH_PATH + "root_element_wrong.xml", contents);
113     bool ret = false;
114     for (auto content : contents) {
115         ret = pluginSwitch_->LoadFromConfigContent(content);
116     }
117     EXPECT_TRUE(!ret);
118 }
119 
120 /**
121  * @tc.name: Init plugin Switch IsInvalidNode001
122  * @tc.desc: IsInvalidNode
123  * @tc.type: FUNC
124  * @tc.require: issueI798UT
125  * @tc.author:luolu
126  */
127 HWTEST_F(PluginSwitchTest, IsInvalidNode001, TestSize.Level1)
128 {
129     xmlNode currNode;
130     pluginSwitch_->IsInvalidNode(currNode);
131     EXPECT_TRUE(pluginSwitch_->IsInvalidNode(currNode));
132 }
133 } // namespace ResourceSchedule
134 } // namespace OHOS
135