• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 # Copyright (c) 2023 iSoftStone Information Technology (Group) 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 <string>
17 
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20 #include "base/utils/resource_configuration.h"
21 
22 #define protected public
23 #define private public
24 #include "base/log/log.h"
25 #include "core/common/resource/resource_manager.h"
26 #include "core/common/resource/resource_object.h"
27 #undef private
28 #undef protected
29 
30 using namespace testing;
31 using namespace testing::ext;
32 
33 namespace OHOS::Ace {
34 namespace {
35 constexpr int32_t DEFAULT_INSTANCE_ID = 0;
36 
MakeCacheKey(const std::string & bundleName,const std::string & moduleName,int32_t instanceId)37 std::string MakeCacheKey(const std::string& bundleName, const std::string& moduleName, int32_t instanceId)
38 {
39     return bundleName + "." + moduleName + "." + std::to_string(instanceId);
40 }
41 }
42 class ResourceManagerTest : public testing::Test {};
43 
44 /**
45  * @tc.name: ResourceManagerTest001
46  * @tc.desc: Test resourceManager.
47  * @tc.type: FUNC
48  */
49 HWTEST_F(ResourceManagerTest, ResourceManagerTest001, TestSize.Level1)
50 {
51     /**
52      * @tc.steps: step1. Create ResourceAdapter.
53      * @tc.steps: step2. Add to resource manager.
54      * @tc.expect: resourceAdapters_ in ResourceManager is 1 (contains the default adapter)
55      */
56     auto resourceAdapter = ResourceAdapter::Create();
57     ResourceManager::GetInstance().AddResourceAdapter("", "", DEFAULT_INSTANCE_ID, resourceAdapter);
58     EXPECT_EQ(ResourceManager::GetInstance().resourceAdapters_.size(), 1);
59 
60     /**
61      * @tc.steps: step3. Create a resource object.
62      * @tc.steps: step4. Add to resource manager.
63      * @tc.expect: Will create a new resourceAdapter with bundleName and moduleName and will
64         restore in ResourceManager
65      */
66     std::string bundleName = "com.example.test";
67     std::string moduleName = "entry";
68     auto resourceObject = AceType::MakeRefPtr<ResourceObject>(bundleName, moduleName, DEFAULT_INSTANCE_ID);
69     auto resAdapterCreate = ResourceManager::GetInstance().GetOrCreateResourceAdapter(resourceObject);
70     EXPECT_EQ(ResourceManager::GetInstance().cache_.size(), 1);
71     EXPECT_NE(ResourceManager::GetInstance().cache_.find(MakeCacheKey(bundleName, moduleName, DEFAULT_INSTANCE_ID)),
72         ResourceManager::GetInstance().cache_.end());
73 
74     /**
75      * @tc.steps: step5. Get resource adapter by bundleName and moduleName.
76      * @tc.expect: The resourceAdapter is equal to the adapter create in last step.
77      */
78     auto resAdapterGet = ResourceManager::GetInstance().GetResourceAdapter(bundleName, moduleName, DEFAULT_INSTANCE_ID);
79     EXPECT_EQ(resAdapterCreate, resAdapterGet);
80 
81     /**
82      * @tc.steps: step6. Delete resourceAdapter by bundleName and moduleName.
83      * @tc.expect: The resourceAdapter of bundleName and moduleName is removed.
84      */
85     ResourceManager::GetInstance().RemoveResourceAdapter(bundleName, moduleName, DEFAULT_INSTANCE_ID);
86     EXPECT_EQ(ResourceManager::GetInstance().resourceAdapters_.size(), 1);
87     EXPECT_EQ(ResourceManager::GetInstance().resourceAdapters_.find(
88                   MakeCacheKey(bundleName, moduleName, DEFAULT_INSTANCE_ID)),
89         ResourceManager::GetInstance().resourceAdapters_.end());
90 
91     /**
92      * @tc.steps: step7. Clear resource manager.
93      * @tc.expect: The resourceAdapters_ is empty.
94      */
95     ResourceManager::GetInstance().Reset();
96     EXPECT_FALSE(ResourceManager::GetInstance().resourceAdapters_.empty());
97 
98     /**
99      * @tc.steps: step8. Get resourceAdapter with empty bundleName and moduleName.
100      * @tc.expect: true.
101      */
102     EXPECT_EQ(ResourceManager::GetInstance().GetResourceAdapter(DEFAULT_INSTANCE_ID), resourceAdapter);
103 
104     ResourceManager::GetInstance().resourceAdapters_.clear();
105 }
106 
107 /**
108  * @tc.name: ResourceManagerTest002
109  * @tc.desc: Test resourceManager.
110  * @tc.type: FUNC
111  */
112 HWTEST_F(ResourceManagerTest, ResourceManagerTest002, TestSize.Level1)
113 {
114     /**
115      * @tc.steps: step1. create empty bundleName and moduleName.
116      * @tc.steps: step2. call MakeCacheKey.
117      * @tc.expect: The key is empty.
118      */
119     std::string bundleName = "";
120     std::string moduleName = "";
121     std::string key = ResourceManager::GetInstance().MakeCacheKey(bundleName, moduleName, DEFAULT_INSTANCE_ID);
122     EXPECT_EQ(key, std::to_string(DEFAULT_INSTANCE_ID));
123 
124     /**
125      * @tc.steps: step3. create bundleName and moduleName.
126      * @tc.steps: step4. call MakeCacheKey.
127      * @tc.expect: The key is bundleName.moduleName.
128      */
129     bundleName = "com.example.test";
130     moduleName = "entry";
131     key = ResourceManager::GetInstance().MakeCacheKey(bundleName, moduleName, DEFAULT_INSTANCE_ID);
132     EXPECT_EQ(key, MakeCacheKey(bundleName, moduleName, DEFAULT_INSTANCE_ID));
133 
134     /**
135      * @tc.steps: step5. create bundleName and moduleName.
136      * @tc.steps: step6. call MakeCacheKey.
137      * @tc.expect: The key is bundleName.moduleName.
138      */
139     bundleName = "com.example.test";
140     moduleName = "";
141     key = ResourceManager::GetInstance().MakeCacheKey(bundleName, moduleName, DEFAULT_INSTANCE_ID);
142     EXPECT_EQ(key, MakeCacheKey(bundleName, moduleName, DEFAULT_INSTANCE_ID));
143 
144     /**
145      * @tc.steps: step7. create bundleName and moduleName.
146      * @tc.steps: step8. call MakeCacheKey.
147      * @tc.expect: The key is bundleName.moduleName.
148      */
149     bundleName = "";
150     moduleName = "entry";
151     key = ResourceManager::GetInstance().MakeCacheKey(bundleName, moduleName, DEFAULT_INSTANCE_ID);
152     EXPECT_EQ(key, MakeCacheKey(bundleName, moduleName, DEFAULT_INSTANCE_ID));
153 
154     ResourceManager::GetInstance().Reset();
155     ResourceManager::GetInstance().resourceAdapters_.clear();
156 }
157 
158 /**
159  * @tc.name: ResourceManagerTest003
160  * @tc.desc: Test resourceManager.
161  * @tc.type: FUNC
162  */
163 HWTEST_F(ResourceManagerTest, ResourceManagerTest003, TestSize.Level1)
164 {
165     /**
166      * @tc.steps: step1. create bundleName and moduleName.
167      * @tc.steps: step2. create ResourceObject
168      * @tc.steps: step3. create ResourceAdapter
169      * @tc.steps: step4. IsResourceAdapterRecord
170      * @tc.expect: true.
171      */
172     std::string bundleName = "com.example.test";
173     std::string moduleName = "entry";
174     auto resourceObject = AceType::MakeRefPtr<ResourceObject>(bundleName, moduleName, DEFAULT_INSTANCE_ID);
175     auto resAdapterCreate = ResourceManager::GetInstance().GetOrCreateResourceAdapter(resourceObject);
176     auto result = ResourceManager::GetInstance().IsResourceAdapterRecord(bundleName, moduleName, DEFAULT_INSTANCE_ID);
177     EXPECT_TRUE(result);
178 
179     /**
180      * @tc.steps: step5. call IsResourceAdapterRecord with empty bundleName and moduleName
181      * @tc.expect: false.
182      */
183     result = ResourceManager::GetInstance().IsResourceAdapterRecord("", "", DEFAULT_INSTANCE_ID);
184     EXPECT_FALSE(result);
185 
186     /**
187      * @tc.steps: step6. remove other resourceAdapter from resourceManager
188      * @tc.steps: step7. call IsResourceAdapterRecord with bundleName and moduleName
189      * @tc.expect: true.
190      */
191     ResourceManager::GetInstance().RemoveResourceAdapter("", "", DEFAULT_INSTANCE_ID);
192     result = ResourceManager::GetInstance().IsResourceAdapterRecord(bundleName, moduleName, DEFAULT_INSTANCE_ID);
193     EXPECT_TRUE(result);
194 
195     /**
196      * @tc.steps: step8. remove resourceAdapter from resourceManager
197      * @tc.steps: step9. call IsResourceAdapterRecord with bundleName and moduleName
198      * @tc.expect: false.
199      */
200     ResourceManager::GetInstance().RemoveResourceAdapter(bundleName, moduleName, DEFAULT_INSTANCE_ID);
201     result = ResourceManager::GetInstance().IsResourceAdapterRecord(bundleName, moduleName, DEFAULT_INSTANCE_ID);
202     EXPECT_FALSE(result);
203 
204     ResourceManager::GetInstance().Reset();
205     ResourceManager::GetInstance().resourceAdapters_.clear();
206 }
207 
208 /**
209  * @tc.name: ResourceManagerTest004
210  * @tc.desc: Test resourceManager.
211  * @tc.type: FUNC
212  */
213 HWTEST_F(ResourceManagerTest, ResourceManagerTest004, TestSize.Level1)
214 {
215     /**
216      * @tc.steps: step1. create bundleName and moduleName.
217      * @tc.steps: step2. create ResourceObject
218      * @tc.steps: step3. create ResourceAdapter
219      * @tc.steps: step4. IsResourceAdapterRecord
220      * @tc.expect: true.
221      */
222     std::string bundleName = "com.example.test";
223     std::string moduleName = "entry";
224     auto resourceObject = AceType::MakeRefPtr<ResourceObject>(bundleName, moduleName, DEFAULT_INSTANCE_ID);
225     auto resAdapterCreate = ResourceManager::GetInstance().GetOrCreateResourceAdapter(resourceObject);
226     auto result = ResourceManager::GetInstance().IsResourceAdapterRecord(bundleName, moduleName, DEFAULT_INSTANCE_ID);
227     EXPECT_TRUE(result);
228 
229     /**
230      * @tc.steps: step4. create new bundleName and moduleName
231      * @tc.steps: step5. call RegisterMainResourceAdapter
232      * @tc.steps: step6. call IsResourceAdapterRecord
233      * @tc.expect: true.
234      */
235     bundleName = "com.example.test";
236     moduleName = "entry2";
237     ResourceManager::GetInstance().RegisterMainResourceAdapter(
238         bundleName, moduleName, DEFAULT_INSTANCE_ID, resAdapterCreate);
239     result = ResourceManager::GetInstance().IsResourceAdapterRecord(bundleName, moduleName, DEFAULT_INSTANCE_ID);
240     EXPECT_TRUE(result);
241 
242     /**
243      * @tc.steps: step7. create ResourceConfiguration
244      * @tc.steps: step8. call UpdateResourceConfig
245      * @tc.expect: resourceAdapters_ has a element.
246      */
247     ResourceConfiguration resConfig;
248     ResourceManager::GetInstance().UpdateResourceConfig(resConfig);
249     EXPECT_EQ(ResourceManager::GetInstance().resourceAdapters_.size(), 1);
250 
251     /**
252      * @tc.steps: step9. create ColorMode
253      * @tc.steps: step10. call UpdateResourceConfig
254      * @tc.expect: resourceAdapters_ has a element.
255      */
256     ColorMode colorMode = ColorMode::DARK;
257     ResourceManager::GetInstance().UpdateColorMode(colorMode);
258     EXPECT_EQ(ResourceManager::GetInstance().resourceAdapters_.size(), 1);
259 }
260 } // namespace OHOS::Ace