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 "gtest/gtest.h"
17 #define private public
18 #include "test/mock/core/common/mock_container.h"
19 #include "base/utils/system_properties.h"
20 #include "bridge/common/plugin_adapter/plugin_bridge.h"
21 #include "core/common/ace_engine.h"
22 #include "core/common/container.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS::Ace::Framework {
28 namespace {
29 const std::string INFO_STR_ONE = "{\"brand\":\"test_brand\","
30                                  "\"manufacturer\":\"test_manufacturer\","
31                                  "\"model\":\"test\","
32                                  "\"product\":\"phone 1.0\","
33                                  "\"apiVersion\":9,"
34                                  "\"releaseType\":\"Beta\","
35                                  "\"deviceType\":\"PHONE\","
36                                  "\"language\":\"zh-Hans\","
37                                  "\"region\":\"CN\","
38                                  "\"windowWidth\":100,"
39                                  "\"windowHeight\":100,"
40                                  "\"screenDensity\":1,"
41                                  "\"screenShape\":\"circle\"}";
42 
43 const std::string INFO_STR_TWO = "{\"brand\":\"test_brand\","
44                                  "\"manufacturer\":\"test_manufacturer\","
45                                  "\"model\":\"test\","
46                                  "\"product\":\"phone 2.0\","
47                                  "\"apiVersion\":10,"
48                                  "\"releaseType\":\"Beta\","
49                                  "\"deviceType\":\"PHONE\","
50                                  "\"language\":\"zh-Hans\","
51                                  "\"region\":\"CN\","
52                                  "\"windowWidth\":50,"
53                                  "\"windowHeight\":50,"
54                                  "\"screenDensity\":1,"
55                                  "\"screenShape\":\"rect\"}";
56 
57 const std::string INFO_STR_DEFAULT = "{\"language\":\"zh-Hans\","
58                                      "\"region\":\"CN\","
59                                      "\"screenDensity\":1,"
60                                      "\"screenShape\":\"rect\"}";
61 } // namespace
62 
63 class PluginAdapterTest : public testing::Test {
64 public:
65     static void SetUpTestCase();
66     static void TearDownTestCase();
67     void SetUp() override;
68     void TearDown() override;
69 };
70 
SetUpTestCase()71 void PluginAdapterTest::SetUpTestCase() {}
TearDownTestCase()72 void PluginAdapterTest::TearDownTestCase() {}
SetUp()73 void PluginAdapterTest::SetUp() {}
TearDown()74 void PluginAdapterTest::TearDown() {}
75 
76 /**
77  * @tc.name: GetDevInfoTest001
78  * @tc.desc: Initialize device information
79  * @tc.type: FUNC
80  */
81 HWTEST_F(PluginAdapterTest, GetDevInfoTest001, TestSize.Level1)
82 {
83     /**
84      * @tc.steps: step1. Use default device information.
85      * @tc.expected: step1. Get the device information as expected.
86      */
87     PluginBridge pluginBridge;
88     auto infoPair = pluginBridge.GetDeviceInfo();
89     EXPECT_EQ(infoPair.first, INFO_STR_DEFAULT);
90     EXPECT_EQ(infoPair.second, false);
91 }
92 
93 /**
94  * @tc.name: GetDevInfoTest002
95  * @tc.desc: Initialize device information
96  * @tc.type: FUNC
97  */
98 HWTEST_F(PluginAdapterTest, GetDevInfoTest002, TestSize.Level1)
99 {
100     /**
101      * @tc.steps: step1. Initialize device information.
102      * @tc.expected: step1. Get the device information as expected.
103      */
104     SystemProperties systemPro;
105     systemPro.InitDeviceInfo(100, 100, 1, 1.0, true);
106     systemPro.brand_ = "test_brand";
107     systemPro.manufacturer_ = "test_manufacturer";
108     systemPro.product_ = "phone 1.0";
109     systemPro.apiVersion_ = "9";
110     systemPro.releaseType_ = "Beta";
111     systemPro.model_ = "test";
112     systemPro.paramDeviceType_ = "PHONE";
113     PluginBridge pluginBridge;
114     auto container = AceType::MakeRefPtr<MockContainer>();
115     EXPECT_CALL(*container, GetViewWidth()).Times(2).WillOnce(Return(100)).WillOnce(Return(50));
116     EXPECT_CALL(*container, GetViewHeight()).Times(2).WillOnce(Return(100)).WillOnce(Return(50));
117     AceEngine::Get().AddContainer(-1, container);
118     auto infoPair = pluginBridge.GetDeviceInfo();
119     EXPECT_EQ(infoPair.first, INFO_STR_ONE);
120     EXPECT_EQ(infoPair.second, true);
121 }
122 
123 /**
124  * @tc.name: GetDevInfoTest003
125  * @tc.desc: Initialize device information
126  * @tc.type: FUNC
127  */
128 HWTEST_F(PluginAdapterTest, GetDevInfoTest003, TestSize.Level1)
129 {
130     /**
131      * @tc.steps: step1. Initialize device information.
132      * @tc.expected: step1. Get the device information as expected.
133      */
134     SystemProperties systemPro;
135     systemPro.InitDeviceInfo(100, 100, 1, 1.0, false);
136     systemPro.brand_ = "test_brand";
137     systemPro.manufacturer_ = "test_manufacturer";
138     systemPro.product_ = "phone 2.0";
139     systemPro.apiVersion_ = "10";
140     systemPro.releaseType_ = "Beta";
141     systemPro.model_ = "test";
142     systemPro.paramDeviceType_ = "PHONE";
143     PluginBridge pluginBridge;
144     auto container = AceType::MakeRefPtr<MockContainer>();
145     AceEngine::Get().AddContainer(-1, container);
146     auto infoPair = pluginBridge.GetDeviceInfo();
147     EXPECT_EQ(infoPair.first, INFO_STR_TWO);
148     EXPECT_EQ(infoPair.second, true);
149 }
150 
151 HWTEST_F(PluginAdapterTest, GetMemConfigProperty001, TestSize.Level1)
152 {
153     OHOS::system::SetParameter("persist.ark.mem_config_property", "jsHeap500");
154     std::string ret = SystemProperties::GetMemConfigProperty("persist.ark.mem_config_property", "");
155     EXPECT_EQ(ret, "jsHeap500");
156 }
157 } // namespace OHOS::Ace::Framework
158