• 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 <cstring>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #define private public
21 #include "nweb_config_helper.h"
22 #include "nweb_log.h"
23 #include "system_properties_adapter_impl.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS::NWeb {
29 class SystemPropertiesAdapterTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35 };
36 
SetUpTestCase(void)37 void SystemPropertiesAdapterTest::SetUpTestCase(void)
38 {}
39 
TearDownTestCase(void)40 void SystemPropertiesAdapterTest::TearDownTestCase(void)
41 {}
42 
SetUp(void)43 void SystemPropertiesAdapterTest::SetUp(void)
44 {}
45 
TearDown(void)46 void SystemPropertiesAdapterTest::TearDown(void)
47 {}
48 
49 class SystemPropertiesObserverTest : public SystemPropertiesObserver {
50  public:
51    SystemPropertiesObserverTest() = default;
52    ~SystemPropertiesObserverTest() override = default;
53 
PropertiesUpdate(const char * value)54     void PropertiesUpdate(const char* value) override
55     {
56         if (strcmp(value, "true") == 0) {
57             update_value_ = true;
58         } else if (strcmp(value, "false") == 0) {
59             update_value_ = false;
60         } else {
61             WVLOG_E("SystemPropertiesObserverTest return value is invalid");
62         }
63     }
64 
UpdateValue()65    bool UpdateValue()
66    {
67      return update_value_;
68    }
69  private:
70     bool update_value_ = false;
71 };
72 
73 /**
74  * @tc.name: SystemPropertiesAdapterTest_GetDeviceInfoBrand_001
75  * @tc.desc: GetInstance.
76  * @tc.type: FUNC
77  * @tc.require:
78  */
79 HWTEST_F(SystemPropertiesAdapterTest, SystemPropertiesAdapterTest_GetDeviceInfoBrand_001, TestSize.Level1)
80 {
81     std::string model = SystemPropertiesAdapterImpl::GetInstance().GetDeviceInfoProductModel();
82     EXPECT_NE(model, "");
83     std::string deviceInfo = SystemPropertiesAdapterImpl::GetInstance().GetDeviceInfoBrand();
84     EXPECT_NE(deviceInfo, "");
85     int32_t result = SystemPropertiesAdapterImpl::GetInstance().GetDeviceInfoMajorVersion();
86     EXPECT_NE(result, -1);
87     SystemPropertiesAdapterImpl::GetInstance().GetResourceUseHapPathEnable();
88     SystemPropertiesAdapterImpl::GetInstance().GetProductDeviceType();
89     bool value = SystemPropertiesAdapterImpl::GetInstance().GetWebOptimizationValue();
90     EXPECT_TRUE(value);
91     system("param set web.optimization false");
92     value = SystemPropertiesAdapterImpl::GetInstance().GetWebOptimizationValue();
93     EXPECT_FALSE(value);
94     system("param set web.optimization true");
95     bool mode = SystemPropertiesAdapterImpl::GetInstance().IsAdvancedSecurityMode();
96     EXPECT_FALSE(mode);
97     string logMode = SystemPropertiesAdapterImpl::GetInstance().GetNetlogMode();
98     EXPECT_EQ(logMode, "None");
99     string siteIsolationMode = SystemPropertiesAdapterImpl::GetInstance().GetSiteIsolationMode();
100     EXPECT_EQ(siteIsolationMode, "none");
101 }
102 
103 /**
104  * @tc.name: SystemPropertiesAdapterTest_OptSystemPropertiesObserver_002
105  * @tc.desc: AttachSysPropObserver DetachSysPropObserver unittest.
106  * @tc.type: FUNC
107  * @tc.require:
108  */
109 HWTEST_F(SystemPropertiesAdapterTest, SystemPropertiesAdapterTest_OptSystemPropertiesObserver_002, TestSize.Level1)
110 {
111     system("param set web.render.dump false");
112     auto& system_properties_adapter = SystemPropertiesAdapterImpl::GetInstance();
113     system_properties_adapter.AttachSysPropObserver(PropertiesKey::PROP_RENDER_DUMP, nullptr);
114 
115     auto observer = std::make_shared<SystemPropertiesObserverTest>();
116     system_properties_adapter.AttachSysPropObserver(PropertiesKey::PROP_RENDER_DUMP, observer.get());
117     system("param set web.render.dump true");
118     bool result_first = observer->UpdateValue();
119     EXPECT_TRUE(result_first);
120     system("param set web.render.dump false");
121     bool result_second = observer->UpdateValue();
122     EXPECT_FALSE(result_second);
123 
124     system_properties_adapter.DetachSysPropObserver(PropertiesKey::PROP_RENDER_DUMP, nullptr);
125     system_properties_adapter.DetachSysPropObserver(PropertiesKey::PROP_RENDER_DUMP, observer.get());
126     system("param set web.render.dump true");
127     bool result_third = observer->UpdateValue();
128     EXPECT_FALSE(result_third);
129 }
130 
131 /**
132  * @tc.name: SystemPropertiesAdapterTest_GetOOPGPUEnable_003
133  * @tc.desc: GetInstance unittest.
134  * @tc.type: FUNC
135  * @tc.require:
136  */
137 HWTEST_F(SystemPropertiesAdapterTest, SystemPropertiesAdapterTest_GetOOPGPUEnable_003, TestSize.Level1)
138 {
139     system("param set web.oop.gpu None");
140     bool value = SystemPropertiesAdapterImpl::GetInstance().GetOOPGPUEnable();
141     EXPECT_FALSE(value);
142     SystemPropertiesAdapterImpl::GetInstance().SetOOPGPUDisable();
143     value = SystemPropertiesAdapterImpl::GetInstance().GetOOPGPUEnable();
144     EXPECT_FALSE(value);
145     system("param set web.oop.gpu false");
146     value = SystemPropertiesAdapterImpl::GetInstance().GetOOPGPUEnable();
147     EXPECT_FALSE(value);
148     system("param set web.oop.gpu true");
149     value = SystemPropertiesAdapterImpl::GetInstance().GetOOPGPUEnable();
150     EXPECT_TRUE(value);
151 }
152 
153 /**
154  * @tc.name: SystemPropertiesAdapterTest_GetVulkanEnable_004
155  * @tc.desc: GetInstance unittest.
156  * @tc.type: FUNC
157  * @tc.require:
158  */
159 HWTEST_F(SystemPropertiesAdapterTest, SystemPropertiesAdapterTest_GetVulkanEnable_004, TestSize.Level1)
160 {
161     system("param set web.ohos.vulkan false");
162     string value = SystemPropertiesAdapterImpl::GetInstance().GetVulkanStatus();
163     EXPECT_EQ(value, "false");
164 }
165 
166 /**
167  * @tc.name  : AnalysisFromConfig_ShouldReturnMobile_WhenFactoryLevelIsPhone
168  * @tc.number: SystemPropertiesAdapterImplTest_001
169  * @tc.desc  : Test AnalysisFromConfig function when factory level is phone.
170  */
171 HWTEST_F(SystemPropertiesAdapterTest, AnalysisFromConfig_ShouldReturnMobile_WhenFactoryLevelIsPhone, TestSize.Level0)
172 {
173     NWebConfigHelper::Instance().perfConfig_.emplace("factoryConfig/factoryLevel", "2");
174     ProductDeviceType result = SystemPropertiesAdapterImpl::GetInstance().AnalysisFromConfig();
175     EXPECT_EQ(result, ProductDeviceType::DEVICE_TYPE_MOBILE);
176     NWebConfigHelper::Instance().perfConfig_.clear();
177 }
178 
179 /**
180  * @tc.name  : AnalysisFromConfig_ShouldReturnTablet_WhenFactoryLevelIsTablet
181  * @tc.number: SystemPropertiesAdapterImplTest_002
182  * @tc.desc  : Test AnalysisFromConfig function when factory level is tablet.
183  */
184 HWTEST_F(SystemPropertiesAdapterTest, AnalysisFromConfig_ShouldReturnTablet_WhenFactoryLevelIsTablet, TestSize.Level0)
185 {
186     NWebConfigHelper::Instance().perfConfig_.emplace("factoryConfig/factoryLevel", "4");
187     ProductDeviceType result = SystemPropertiesAdapterImpl::GetInstance().AnalysisFromConfig();
188     EXPECT_EQ(result, ProductDeviceType::DEVICE_TYPE_TABLET);
189     NWebConfigHelper::Instance().perfConfig_.clear();
190 }
191 
192 /**
193  * @tc.name  : AnalysisFromConfig_ShouldReturn2In1_WhenFactoryLevelIsPC
194  * @tc.number: SystemPropertiesAdapterImplTest_003
195  * @tc.desc  : Test AnalysisFromConfig function when factory level is PC.
196  */
197 HWTEST_F(SystemPropertiesAdapterTest, AnalysisFromConfig_ShouldReturn2In1_WhenFactoryLevelIsPC, TestSize.Level0)
198 {
199     NWebConfigHelper::Instance().perfConfig_.emplace("factoryConfig/factoryLevel", "8");
200     ProductDeviceType result = SystemPropertiesAdapterImpl::GetInstance().AnalysisFromConfig();
201     EXPECT_EQ(result, ProductDeviceType::DEVICE_TYPE_2IN1);
202     NWebConfigHelper::Instance().perfConfig_.clear();
203 }
204 
205 /**
206  * @tc.name  : AnalysisFromConfig_ShouldReturnWearable_WhenFactoryLevelIsWearable
207  * @tc.number: SystemPropertiesAdapterImplTest_004
208  * @tc.desc  : Test AnalysisFromConfig function when factory level is Wearable.
209  */
210 HWTEST_F(SystemPropertiesAdapterTest, AnalysisFromConfig_ShouldReturnWearable_WhenFactoryLevelIsWearable,
211          TestSize.Level0)
212 {
213     NWebConfigHelper::Instance().perfConfig_.emplace("factoryConfig/factoryLevel", "16");
214     ProductDeviceType result = SystemPropertiesAdapterImpl::GetInstance().AnalysisFromConfig();
215     EXPECT_EQ(result, ProductDeviceType::DEVICE_TYPE_WEARABLE);
216     NWebConfigHelper::Instance().perfConfig_.clear();
217 }
218 
219 /**
220  * @tc.name  : AnalysisFromConfig_ShouldReturnWearable_WhenFactoryLevelIsWearable
221  * @tc.number: SystemPropertiesAdapterImplTest_005
222  * @tc.desc  : Test AnalysisFromConfig function when factory level is TV.
223  */
224 HWTEST_F(SystemPropertiesAdapterTest, AnalysisFromConfig_ShouldReturnTV_WhenFactoryLevelIsTV, TestSize.Level0)
225 {
226     NWebConfigHelper::Instance().perfConfig_.emplace("factoryConfig/factoryLevel", "32");
227     ProductDeviceType result = SystemPropertiesAdapterImpl::GetInstance().AnalysisFromConfig();
228     EXPECT_EQ(result, ProductDeviceType::DEVICE_TYPE_TV);
229     NWebConfigHelper::Instance().perfConfig_.clear();
230 }
231 
232 /**
233  * @tc.name  : AnalysisFromConfig_ShouldReturnUnknown_WhenFactoryLevelIsUnknown
234  * @tc.number: SystemPropertiesAdapterImplTest_006
235  * @tc.desc  : Test AnalysisFromConfig function when factory level is unknown.
236  */
237 HWTEST_F(SystemPropertiesAdapterTest, AnalysisFromConfig_ShouldReturnUnknown_WhenFactoryLevelIsUnknown, TestSize.Level0)
238 {
239     NWebConfigHelper::Instance().perfConfig_.emplace("factoryConfig/factoryLevel", "64");
240     ProductDeviceType result = SystemPropertiesAdapterImpl::GetInstance().AnalysisFromConfig();
241     EXPECT_EQ(result, ProductDeviceType::DEVICE_TYPE_UNKNOWN);
242     NWebConfigHelper::Instance().perfConfig_.clear();
243 }
244 
245 /**
246  * @tc.name: SystemPropertiesAdapterTest_GetPRPPreloadMode_007
247  * @tc.desc: GetInstance unittest.
248  * @tc.type: FUNC
249  * @tc.require:
250  */
251 HWTEST_F(SystemPropertiesAdapterTest, SystemPropertiesAdapterTest_GetPRPPreloadMode_007, TestSize.Level1)
252 {
253     system("param set web.prppreload.mode none");
254     string value = SystemPropertiesAdapterImpl::GetInstance().GetPRPPreloadMode();
255     EXPECT_EQ(value, "none");
256     system("param set web.prppreload.mode preconnect");
257     value = SystemPropertiesAdapterImpl::GetInstance().GetPRPPreloadMode();
258     EXPECT_EQ(value, "preconnect");
259     system("param set web.prppreload.mode preload");
260     value = SystemPropertiesAdapterImpl::GetInstance().GetPRPPreloadMode();
261     EXPECT_EQ(value, "preload");
262 }
263 } // namespace OHOS
264