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