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 #include "nweb_log.h"
21 #include "system_properties_adapter_impl.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS::NWeb {
27 class SystemPropertiesAdapterTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33 };
34 
SetUpTestCase(void)35 void SystemPropertiesAdapterTest::SetUpTestCase(void)
36 {}
37 
TearDownTestCase(void)38 void SystemPropertiesAdapterTest::TearDownTestCase(void)
39 {}
40 
SetUp(void)41 void SystemPropertiesAdapterTest::SetUp(void)
42 {}
43 
TearDown(void)44 void SystemPropertiesAdapterTest::TearDown(void)
45 {}
46 
47 class SystemPropertiesObserverTest : public SystemPropertiesObserver {
48  public:
49    SystemPropertiesObserverTest() = default;
50    ~SystemPropertiesObserverTest() override = default;
51 
PropertiesUpdate(const char * value)52     void PropertiesUpdate(const char* value) override
53     {
54         if (strcmp(value, "true") == 0) {
55             updateValue = true;
56         } else if (strcmp(value, "false") == 0) {
57             updateValue = false;
58         } else {
59             WVLOG_E("SystemPropertiesObserverTest return value is invalid");
60         }
61     }
62 
UpdateValue()63     bool UpdateValue()
64     {
65         return updateValue;
66     }
67  private:
68     bool updateValue = false;
69 };
70 
71 /**
72  * @tc.name: SystemPropertiesAdapterTest_GetDeviceInfoBrand_001
73  * @tc.desc: GetInstance.
74  * @tc.type: FUNC
75  * @tc.require:
76  */
77 HWTEST_F(SystemPropertiesAdapterTest, SystemPropertiesAdapterTest_GetDeviceInfoBrand_001, TestSize.Level1)
78 {
79     std::string model = SystemPropertiesAdapterImpl::GetInstance().GetDeviceInfoProductModel();
80     EXPECT_NE(model, "");
81     std::string deviceInfo = SystemPropertiesAdapterImpl::GetInstance().GetDeviceInfoBrand();
82     EXPECT_NE(deviceInfo, "");
83     int32_t result = SystemPropertiesAdapterImpl::GetInstance().GetDeviceInfoMajorVersion();
84     EXPECT_NE(result, -1);
85     SystemPropertiesAdapterImpl::GetInstance().GetResourceUseHapPathEnable();
86     SystemPropertiesAdapterImpl::GetInstance().GetProductDeviceType();
87     bool value = SystemPropertiesAdapterImpl::GetInstance().GetWebOptimizationValue();
88     EXPECT_TRUE(value);
89     system("param set web.optimization false");
90     value = SystemPropertiesAdapterImpl::GetInstance().GetWebOptimizationValue();
91     EXPECT_FALSE(value);
92     system("param set web.optimization true");
93     bool mode = SystemPropertiesAdapterImpl::GetInstance().IsAdvancedSecurityMode();
94     EXPECT_FALSE(mode);
95     string logMode = SystemPropertiesAdapterImpl::GetInstance().GetNetlogMode();
96     EXPECT_EQ(logMode, "None");
97     string siteIsolationMode = SystemPropertiesAdapterImpl::GetInstance().GetSiteIsolationMode();
98     EXPECT_EQ(siteIsolationMode, "none");
99 }
100 
101 /**
102  * @tc.name: SystemPropertiesAdapterTest_OptSystemPropertiesObserver_002
103  * @tc.desc: AttachSysPropObserver DetachSysPropObserver unittest.
104  * @tc.type: FUNC
105  * @tc.require:
106  */
107 HWTEST_F(SystemPropertiesAdapterTest, SystemPropertiesAdapterTest_OptSystemPropertiesObserver_002, TestSize.Level1)
108 {
109     system("param set web.render.dump false");
110     auto& system_properties_adapter = SystemPropertiesAdapterImpl::GetInstance();
111     system_properties_adapter.AttachSysPropObserver(PropertiesKey::PROP_RENDER_DUMP, nullptr);
112 
113     auto observer = std::make_shared<SystemPropertiesObserverTest>();
114     system_properties_adapter.AttachSysPropObserver(PropertiesKey::PROP_RENDER_DUMP, observer.get());
115     system("param set web.render.dump true");
116     bool resultFirst = observer->UpdateValue();
117     EXPECT_TRUE(resultFirst);
118     system("param set web.render.dump false");
119     bool resultSecond = observer->UpdateValue();
120     EXPECT_FALSE(resultSecond);
121 
122     system_properties_adapter.DetachSysPropObserver(PropertiesKey::PROP_RENDER_DUMP, nullptr);
123     system_properties_adapter.DetachSysPropObserver(PropertiesKey::PROP_RENDER_DUMP, observer.get());
124     system("param set web.render.dump true");
125     bool resultThird = observer->UpdateValue();
126     EXPECT_FALSE(resultThird);
127 }
128 
129 /**
130  * @tc.name: SystemPropertiesAdapterTest_GetOOPGPUEnable_003
131  * @tc.desc: GetInstance unittest.
132  * @tc.type: FUNC
133  * @tc.require:
134  */
135 HWTEST_F(SystemPropertiesAdapterTest, SystemPropertiesAdapterTest_GetOOPGPUEnable_003, TestSize.Level1)
136 {
137     system("param set web.oop.gpu None");
138     bool value = SystemPropertiesAdapterImpl::GetInstance().GetOOPGPUEnable();
139     EXPECT_FALSE(value);
140     SystemPropertiesAdapterImpl::GetInstance().SetOOPGPUDisable();
141     value = SystemPropertiesAdapterImpl::GetInstance().GetOOPGPUEnable();
142     EXPECT_FALSE(value);
143     system("param set web.oop.gpu false");
144     value = SystemPropertiesAdapterImpl::GetInstance().GetOOPGPUEnable();
145     EXPECT_FALSE(value);
146     system("param set web.oop.gpu true");
147     value = SystemPropertiesAdapterImpl::GetInstance().GetOOPGPUEnable();
148     EXPECT_TRUE(value);
149 }
150 } // namespace OHOS
151