1 /*
2  * Copyright (c) 2024 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 "theme_manager_test.h"
17 
18 #include "resource_manager_test_common.h"
19 #include "theme_pack_manager.h"
20 using namespace OHOS::Global::Resource;
21 using namespace testing::ext;
22 namespace {
23 class ThemeManagerTest : public testing::Test {
24 public:
25     static void SetUpTestCase(void);
26 
27     static void TearDownTestCase(void);
28 
29     void SetUp();
30 
31     void TearDown();
32 
ThemeManagerTest()33     ThemeManagerTest()
34     {}
35 
~ThemeManagerTest()36     ~ThemeManagerTest()
37     {}
38 
39 public:
40     std::shared_ptr<ResourceManager> rm;
41     std::shared_ptr<ResourceManagerTestCommon> rmc;
42     std::shared_ptr<ThemePackManager> tm;
43 };
44 
SetUpTestCase(void)45 void ThemeManagerTest::SetUpTestCase(void)
46 {
47     // step 1: input testsuit setup step
48     g_logLevel = LOG_DEBUG;
49 }
50 
TearDownTestCase(void)51 void ThemeManagerTest::TearDownTestCase(void)
52 {
53     // step 2: input testsuit teardown step
54 }
55 
SetUp(void)56 void ThemeManagerTest::SetUp(void)
57 {
58     this->tm = ThemePackManager::GetThemePackManager();
59     std::string hapPath;
60     std::vector<std::string> overlayPath;
61     ResConfigImpl *rc = new ResConfigImpl;
62     int32_t appType = 0;
63     int32_t userId = 100; // userId is 100
64     this->rm = CreateResourceManager("ohos.global.test.all", "entry", hapPath, overlayPath, *rc, appType, userId);
65     this->rmc = std::make_shared<ResourceManagerTestCommon>(rm);
66 }
67 
TearDown(void)68 void ThemeManagerTest::TearDown(void)
69 {
70     RESMGR_HILOGD(RESMGR_TAG, "ThemeManagerTest teardown");
71 }
72 
73 /*
74  * @tc.name: ThemeManagerTestLoadThemeSkinResourceTest001
75  * @tc.desc: Test LoadThemeSkinResource function, file case.
76  * @tc.type: FUNC
77  */
78 HWTEST_F(ThemeManagerTest, ThemeManagerTestLoadThemeSkinResourceTest001, TestSize.Level1)
79 {
80    // success cases
81     bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
82     ASSERT_TRUE(ret);
83     int id = rmc->GetResId("base_only", ResType::COLOR);
84     uint32_t outValue;
85     std::vector<std::string> rootDirs;
86     std::string rootDir = "/data/test/theme/skin/ohos.global.test.all";
87     rootDirs.emplace_back(rootDir);
88     int32_t userId = 100; // userId is 100
89     // the bundlename is invalid
90     tm->LoadThemeSkinResource("ohos.global.test", "entry", rootDirs, userId);
91     rm->GetColorById(id, outValue);
92     ASSERT_EQ(0, outValue); // base_only APP value is #000000
93 }
94 
95 /*
96  * @tc.name: ThemeManagerTestLoadThemeSkinResourceTest003
97  * @tc.desc: Test AddResource function, file case.
98  * @tc.type: FUNC
99  */
100 HWTEST_F(ThemeManagerTest, ThemeManagerTestLoadThemeSkinResourceTest002, TestSize.Level1)
101 {
102    // success cases
103     bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
104     ASSERT_TRUE(ret);
105     int id = rmc->GetResId("base_only", ResType::COLOR);
106     uint32_t outValue;
107     rm->GetColorById(id, outValue);
108     ASSERT_EQ(0, outValue); // base_only APP value is #000000
109 
110     std::vector<std::string> rootDirs;
111     std::string rootDir = "/data/test/theme/skin/ohos.global.test.all";
112     rootDirs.emplace_back(rootDir);
113     int32_t userId = 100; // userId is 100
114     tm->LoadThemeSkinResource("ohos.global.test.all", "entry", rootDirs, userId);
115     rm->GetColorById(id, outValue);
116     ASSERT_EQ(4294967295, outValue); // base_only theme value is #ffffff(4294967295)
117 }
118 
119 /*
120  * @tc.name: ThemeManagerTestLoadThemeIconsResourceTest001
121  * @tc.desc: Test GetThemeIcons function, file case.
122  * @tc.type: FUNC
123  */
124 HWTEST_F(ThemeManagerTest, ThemeManagerTestLoadThemeIconsResourceTest001, TestSize.Level1)
125 {
126     bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
127     ASSERT_TRUE(ret);
128     std::vector<std::string> rootDirs;
129     std::string rootDir = "/data/test/theme/icons/ohos.global.test.all";
130     uint32_t resId = 16777306;
131     std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo;
132     std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo;
133     RState state = rm->GetThemeIcons(resId, foregroundInfo, backgroundInfo);
134     EXPECT_TRUE(state == ERROR_CODE_RES_ID_NOT_FOUND);
135 
136     rootDirs.emplace_back(rootDir);
137     int32_t userId = 100; // userId is 100
138     tm->LoadThemeIconsResource("ohos.global.test.all", "entry", rootDirs, userId);
139     state = rm->GetThemeIcons(resId, foregroundInfo, backgroundInfo);
140     EXPECT_TRUE(state == SUCCESS);
141 }
142 
143 /*
144  * @tc.name: ThemeManagerTestHasIconInThemeTest001
145  * @tc.desc: Test HasIconInTheme function, file case.
146  * @tc.type: FUNC
147  */
148 HWTEST_F(ThemeManagerTest, ThemeManagerTestHasIconInThemeTest001, TestSize.Level1)
149 {
150     bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
151     ASSERT_TRUE(ret);
152     std::vector<std::string> rootDirs;
153     std::string rootDir = "/data/test/theme/icons/ohos.global.test.all";
154     rootDirs.emplace_back(rootDir);
155     int32_t userId = 100; // userId is 100
156     tm->LoadThemeIconsResource("ohos.global.test.all", "entry", rootDirs, userId);
157     bool result = rm->HasIconInTheme("ohos.global.test");
158     EXPECT_TRUE(result == false);
159 
160     result = rm->HasIconInTheme("ohos.global.test.all");
161     EXPECT_TRUE(result == true);
162 }
163 
164 /*
165  * @tc.name: ThemeManagerTestGetOtherIconsInfoTest001
166  * @tc.desc: Test GetOtherIconsInfo function, file case.
167  * @tc.type: FUNC
168  */
169 HWTEST_F(ThemeManagerTest, ThemeManagerTestGetOtherIconsInfoTest001, TestSize.Level1)
170 {
171     bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
172     ASSERT_TRUE(ret);
173     std::vector<std::string> rootDirs;
174     std::string rootDir = "/data/test/theme/icons/other_icons";
175     rootDirs.emplace_back(rootDir);
176     int32_t userId = 100; // userId is 100
177     tm->LoadThemeIconsResource("other_icons", "", rootDirs, userId);
178     std::unique_ptr<uint8_t[]> outValue;
179     size_t len;
180     RState state = rm->GetOtherIconsInfo("other_icons", outValue, len, true);
181     EXPECT_EQ(state, ERROR_CODE_RES_NOT_FOUND_BY_NAME);
182 
183     state = rm->GetOtherIconsInfo("foreground", outValue, len, false);
184     EXPECT_EQ(state, SUCCESS);
185 }
186 
187 /*
188  * @tc.name: ThemeManagerTestGetThemeIconFromCacheTest001
189  * @tc.desc: Test GetThemeIconFromCache function, file case.
190  * @tc.type: FUNC
191  */
192 HWTEST_F(ThemeManagerTest, ThemeManagerTestGetThemeIconFromCacheTest001, TestSize.Level1)
193 {
194     bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
195     ASSERT_TRUE(ret);
196     std::vector<std::string> rootDirs;
197     std::string rootDir = "/data/test/theme/icons/other_icons";
198     rootDirs.emplace_back(rootDir);
199     int32_t userId = 100; // userId is 100
200     tm->LoadThemeIconsResource("other_icons", "", rootDirs, userId);
201     std::unique_ptr<uint8_t[]> outValue;
202     size_t len;
203     RState state = tm->GetThemeIconFromCache("other_icons_background", outValue, len);
204     EXPECT_EQ(state, NOT_FOUND);
205     state = tm->GetOtherIconsInfo("background", outValue, len, false, userId);
206     EXPECT_EQ(state, SUCCESS);
207     state = tm->GetThemeIconFromCache("other_icons_background", outValue, len);
208     EXPECT_EQ(state, SUCCESS);
209 }
210 
211 /*
212  * @tc.name: ThemeManagerTestIsUpdateByUserIdTest001
213  * @tc.desc: Test IsUpdateByUserId function, file case.
214  * @tc.type: FUNC
215  */
216 HWTEST_F(ThemeManagerTest, ThemeManagerTestIsUpdateByUserIdTest001, TestSize.Level1)
217 {
218     int32_t userId = 100;
219     tm->UpdateUserId(userId);
220     bool result = tm->IsUpdateByUserId(userId);
221     ASSERT_FALSE(result);
222 
223     userId = 101;
224     result = tm->IsUpdateByUserId(userId);
225     ASSERT_TRUE(result);
226 }
227 }
228