1 /*
2 * Copyright (C) 2022-2025 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 <chrono>
17 #include <cstdio>
18 #include <gtest/gtest.h>
19 #include <iostream>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <string>
23
24 #include "accesstoken_kit.h"
25 #include "syspara/parameter.h"
26 #include "system_ability_definition.h"
27 #define private public
28 #define protected public
29 #include "ui_appearance_ability.h"
30 #undef private
31 #undef protected
32 #include "ui_appearance_log.h"
33 #include "alarm_timer_manager.h"
34
35 using namespace testing::ext;
36 static constexpr int UISERVER_UID = 3050;
37
38 namespace OHOS {
39 namespace ArkUi::UiAppearance {
40
41 const int DAY_TO_SECOND = 24 * 60 * 60;
42 const int DAY_TO_MINUTE = 24 * 60;
43 const int SECOND_TO_MILLI = 1000;
44 const int MINUTE_TO_SECOND = 60;
45
46 class UiAppearanceAbilityTest : public UiAppearanceAbility {
47 public:
UiAppearanceAbilityTest()48 UiAppearanceAbilityTest() : UiAppearanceAbility(ARKUI_UI_APPEARANCE_SERVICE_ID, true) {}
~UiAppearanceAbilityTest()49 ~UiAppearanceAbilityTest() {}
OnStart()50 void OnStart() override
51 {
52 return;
53 }
54 };
55
56 class DarkModeTest : public testing::Test {
57 public:
58 static void SetUpTestCase(void);
59 static void TearDownTestCase(void);
60 void SetUp();
61 void TearDown();
62
GetUiAppearanceAbilityTest()63 static sptr<UiAppearanceAbilityTest> GetUiAppearanceAbilityTest()
64 {
65 return new UiAppearanceAbilityTest;
66 }
67
GetAlarmTimerManager()68 static std::shared_ptr<AlarmTimerManager> GetAlarmTimerManager()
69 {
70 return std::make_shared<AlarmTimerManager>();
71 }
72
73 private:
74 int userId_;
75 };
76
SetUpTestCase(void)77 void DarkModeTest::SetUpTestCase(void) {}
78
TearDownTestCase(void)79 void DarkModeTest::TearDownTestCase(void) {}
80
SetUp(void)81 void DarkModeTest::SetUp(void)
82 {
83 userId_ = geteuid();
84 seteuid(UISERVER_UID);
85 }
86
TearDown(void)87 void DarkModeTest::TearDown(void)
88 {
89 seteuid(userId_);
90 }
91
92 /**
93 * @tc.name: ui_appearance_test_001
94 * @tc.desc: Test SetDarkMode and GetDarkMode APIs when setting dark/light.
95 * @tc.type: FUNC
96 */
97 HWTEST_F(DarkModeTest, ui_appearance_test_001, TestSize.Level0)
98 {
99 LOGI("Test SetDarkMode and GetDarkMode APIs when setting dark/light.");
100
101 auto test = DarkModeTest::GetUiAppearanceAbilityTest();
102 int32_t result = -1;
103 test->SetDarkMode(DarkMode::ALWAYS_DARK, result);
104 EXPECT_EQ(result, 0);
105 int32_t mode = -1;
106 test->GetDarkMode(mode);
107 EXPECT_EQ(mode, DarkMode::ALWAYS_DARK);
108
109 test->SetDarkMode(DarkMode::ALWAYS_LIGHT, result);
110 EXPECT_EQ(result, 0);
111 test->GetDarkMode(mode);
112 EXPECT_EQ(mode, DarkMode::ALWAYS_LIGHT);
113 }
114
115 /**
116 * @tc.name: ui_appearance_test_002
117 * @tc.desc: Test SetDarkMode and GetDarkMode APIs when repeatedly setting dark/light.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(DarkModeTest, ui_appearance_test_002, TestSize.Level0)
121 {
122 LOGI("Test SetDarkMode and GetDarkMode APIs when repeatedly setting dark/light.");
123
124 auto test = DarkModeTest::GetUiAppearanceAbilityTest();
125 int32_t result = -1;
126 test->SetDarkMode(DarkMode::ALWAYS_DARK, result);
127 EXPECT_EQ(result, UiAppearanceAbilityErrCode::SUCCEEDED);
128 int32_t mode = -1;
129 test->GetDarkMode(mode);
130 EXPECT_EQ(mode, DarkMode::ALWAYS_DARK);
131
132 test->SetDarkMode(DarkMode::ALWAYS_DARK, result);
133 EXPECT_EQ(result, UiAppearanceAbilityErrCode::SYS_ERR);
134 test->GetDarkMode(mode);
135 EXPECT_EQ(mode, DarkMode::ALWAYS_DARK);
136
137 test->SetDarkMode(DarkMode::ALWAYS_LIGHT, result);
138 EXPECT_EQ(result, UiAppearanceAbilityErrCode::SUCCEEDED);
139 test->GetDarkMode(mode);
140 EXPECT_EQ(mode, DarkMode::ALWAYS_LIGHT);
141
142 test->SetDarkMode(DarkMode::ALWAYS_LIGHT, result);
143 EXPECT_EQ(result, UiAppearanceAbilityErrCode::SYS_ERR);
144 test->GetDarkMode(mode);
145 EXPECT_EQ(mode, DarkMode::ALWAYS_LIGHT);
146 }
147
148 /**
149 * @tc.name: ui_appearance_test_003
150 * @tc.desc: Test the SetDarkMode API when setting an unexpected value
151 * @tc.type: FUNC
152 */
153 HWTEST_F(DarkModeTest, ui_appearance_test_003, TestSize.Level0)
154 {
155 LOGI("Test the SetDarkMode API when setting an unexpected value.");
156
157 int32_t result = -1;
158 DarkModeTest::GetUiAppearanceAbilityTest()->SetDarkMode(DarkMode::UNKNOWN, result);
159 EXPECT_NE(result, 0);
160 }
161
162 /**
163 * @tc.name: ui_appearance_test_004
164 * @tc.desc: Test the font API
165 * @tc.type: FUNC
166 */
167 HWTEST_F(DarkModeTest, ui_appearance_test_004, TestSize.Level0)
168 {
169 LOGI("Test the font API");
170
171 std::string fontScale;
172 int32_t result = -1;
173 DarkModeTest::GetUiAppearanceAbilityTest()->GetFontScale(fontScale, result);
174 EXPECT_EQ(result, 0);
175
176 std::string fontWeightScale;
177 DarkModeTest::GetUiAppearanceAbilityTest()->GetFontWeightScale(fontWeightScale, result);
178 EXPECT_EQ(result, 0);
179 }
180
181 /**
182 * @tc.name: ui_appearance_test_005
183 * @tc.desc: Test the alarm_timer_manager
184 * @tc.type: FUNC
185 */
186 HWTEST_F(DarkModeTest, ui_appearance_test_005, TestSize.Level0)
187 {
188 LOGI("Test the alarm_timer_manager");
189
190 bool result = AlarmTimerManager::IsValidScheduleTime(12, 10);
191 EXPECT_EQ(result, false);
192
193 result = AlarmTimerManager::IsValidScheduleTime(DAY_TO_MINUTE + 10, DAY_TO_MINUTE + 12);
194 EXPECT_EQ(result, false);
195
196 result = AlarmTimerManager::IsValidScheduleTime(10, DAY_TO_MINUTE + 12);
197 EXPECT_EQ(result, false);
198
199 result = AlarmTimerManager::IsValidScheduleTime(10, 12);
200 EXPECT_EQ(result, true);
201 }
202
203 /**
204 * @tc.name: ui_appearance_test_006
205 * @tc.desc: Test the alarm_timer_manager
206 * @tc.type: FUNC
207 */
208 HWTEST_F(DarkModeTest, ui_appearance_test_006, TestSize.Level0)
209 {
210 LOGI("Test the alarm_timer_manager");
211
212 std::array<uint64_t, TRIGGER_ARRAY_SIZE> triggerTimeInterval = {0, 0};
213 std::time_t timestamp = std::time(nullptr);
214 if (timestamp == static_cast<std::time_t>(-1)) {
215 LOGE("fail to get timestamp");
216 }
217 std::tm *nowTime = std::localtime(×tamp);
218 auto current = nowTime->tm_hour * 60 + nowTime->tm_min;
219 if (nowTime != nullptr) {
220 nowTime->tm_hour = 0;
221 nowTime->tm_min = 0;
222 nowTime->tm_sec = 0;
223 }
224 std::time_t now_zero = std::mktime(nowTime);
225 uint64_t zeroTimestamp = static_cast<uint64_t>(now_zero * SECOND_TO_MILLI);
226
227 uint64_t step = DAY_TO_SECOND * SECOND_TO_MILLI;
228
229 uint64_t startTimestamp = zeroTimestamp + (current + 1) * MINUTE_TO_SECOND * SECOND_TO_MILLI;
230 AlarmTimerManager::SetTimerTriggerTime(current + 1, current + 2, triggerTimeInterval);
231 EXPECT_EQ(triggerTimeInterval[0], startTimestamp);
232
233 startTimestamp = zeroTimestamp + (current - 1) * MINUTE_TO_SECOND * SECOND_TO_MILLI;
234 AlarmTimerManager::SetTimerTriggerTime(current - 1, current + 2, triggerTimeInterval);
235 EXPECT_EQ(triggerTimeInterval[0], startTimestamp + step);
236 }
237
238 /**
239 * @tc.name: ui_appearance_test_007
240 * @tc.desc: Test the alarm_timer_manager
241 * @tc.type: FUNC
242 */
243 HWTEST_F(DarkModeTest, ui_appearance_test_007, TestSize.Level0)
244 {
245 LOGI("Test the alarm_timer_manager");
246
247 auto uiAppearanceTimerManager = DarkModeTest::GetAlarmTimerManager();
248 int res = uiAppearanceTimerManager->SetScheduleTime(
__anon9fba00530202()249 10, 12, 100, [](){}, [](){});
250 EXPECT_EQ(res, 0);
251
252 res = uiAppearanceTimerManager->RestartAllTimer();
253 EXPECT_EQ(res, 1);
254
255 uiAppearanceTimerManager->ClearTimerByUserId(100);
256 }
257
258 /**
259 * @tc.name: ui_appearance_test_008
260 * @tc.desc: Test the alarm_timer_manager
261 * @tc.type: FUNC
262 */
263 HWTEST_F(DarkModeTest, ui_appearance_test_008, TestSize.Level0)
264 {
265 LOGI("Test OnRemoveSystemAbility.");
266
267 auto test = DarkModeTest::GetUiAppearanceAbilityTest();
268 test->userSwitchUpdateConfigurationOnceFlag_.insert(100);
269 test->OnRemoveSystemAbility(APP_MGR_SERVICE_ID, "");
270 EXPECT_EQ(0, test->userSwitchUpdateConfigurationOnceFlag_.size());
271 }
272
273 /**
274 * @tc.name: ui_appearance_test_009
275 * @tc.desc: Test the alarm_timer_manager
276 * @tc.type: FUNC
277 */
278 HWTEST_F(DarkModeTest, ui_appearance_test_009, TestSize.Level0)
279 {
280 LOGI("Test BackGroundAppColorSwitch.");
281
282 auto test = DarkModeTest::GetUiAppearanceAbilityTest();
283 bool ret = test->BackGroundAppColorSwitch(nullptr, 0);
284 EXPECT_EQ(false, ret);
285 }
286 } // namespace ArkUi::UiAppearance
287 } // namespace OHOS
288