• 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 
20 #define protected public
21 #define private public
22 #include "base/log/log.h"
23 #include "core/common/resource/resource_manager.h"
24 #include "core/common/resource/resource_object.h"
25 #undef private
26 #undef protected
27 
28 using namespace testing;
29 using namespace testing::ext;
30 
31 namespace OHOS::Ace {
32 namespace {
MakeCacheKey(const std::string & bundleName,const std::string & moduleName)33 std::string MakeCacheKey(const std::string& bundleName, const std::string& moduleName)
34 {
35     return bundleName + "." + moduleName;
36 }
37 }
38 class ResourceManagerTest : public testing::Test {};
39 
40 /**
41  * @tc.name: CastToRegisterTest001
42  * @tc.desc: Test cast to register.
43  * @tc.type: FUNC
44  */
45 HWTEST_F(ResourceManagerTest, ResourceManagerTest001, TestSize.Level1)
46 {
47     /**
48      * @tc.steps: step1. Create ResourceAdapter.
49      * @tc.steps: step2. Add to resource manager.
50      * @tc.expect: resourceAdapters_ in ResourceManager is 1 (contains the default adapter)
51      */
52     auto resourceAdapter = ResourceAdapter::Create();
53     ResourceManager::GetInstance().AddResourceAdapter("", "", resourceAdapter);
54     EXPECT_EQ(ResourceManager::GetInstance().resourceAdapters_.size(), 1);
55 
56     /**
57      * @tc.steps: step3. Create a resource object.
58      * @tc.steps: step4. Add to resource manager.
59      * @tc.expect: Will create a new resourceAdapter with bundleName and moduleName and will
60         restore in ResourceManager
61      */
62     std::string bundleName = "com.example.test";
63     std::string moduleName = "entry";
64     auto resourceObject = AceType::MakeRefPtr<ResourceObject>(bundleName, moduleName);
65     auto resAdapterCreate = ResourceManager::GetInstance().GetOrCreateResourceAdapter(resourceObject);
66     EXPECT_EQ(ResourceManager::GetInstance().cache_.size(), 1);
67     EXPECT_NE(ResourceManager::GetInstance().cache_.find(MakeCacheKey(bundleName, moduleName)),
68         ResourceManager::GetInstance().cache_.end());
69 
70     /**
71      * @tc.steps: step5. Get resource adapter by bundleName and moduleName.
72      * @tc.expect: The resourceAdapter is equal to the adapter create in last step.
73      */
74     auto resAdapterGet = ResourceManager::GetInstance().GetResourceAdapter(bundleName, moduleName);
75     EXPECT_EQ(resAdapterCreate, resAdapterGet);
76 
77     /**
78      * @tc.steps: step6. Delete resourceAdapter by bundleName and moduleName.
79      * @tc.expect: The resourceAdapter of bundleName and moduleName is removed.
80      */
81     ResourceManager::GetInstance().RemoveResourceAdapter(bundleName, moduleName);
82     EXPECT_EQ(ResourceManager::GetInstance().resourceAdapters_.size(), 1);
83     EXPECT_EQ(ResourceManager::GetInstance().resourceAdapters_.find(MakeCacheKey(bundleName, moduleName)),
84         ResourceManager::GetInstance().resourceAdapters_.end());
85 
86     /**
87      * @tc.steps: step7. Clear resource manager.
88      * @tc.expect: The resourceAdapters_ is empty.
89      */
90     ResourceManager::GetInstance().Reset();
91     EXPECT_FALSE(ResourceManager::GetInstance().resourceAdapters_.empty());
92 }
93 } // namespace OHOS::Ace