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 
30 using namespace testing::ext;
31 static constexpr int UISERVER_UID = 3050;
32 
33 namespace OHOS {
34 namespace ArkUi::UiAppearance {
35 class UiAppearanceAbilityTest : public UiAppearanceAbility {
36 public:
UiAppearanceAbilityTest()37     UiAppearanceAbilityTest() : UiAppearanceAbility(ARKUI_UI_APPEARANCE_SERVICE_ID, true) {}
~UiAppearanceAbilityTest()38     ~UiAppearanceAbilityTest() {}
39     void OnStart() override
40     {
41         return;
42     }
43 };
44 
45 class DarkModeTest : public testing::Test {
46 public:
47     static void SetUpTestCase(void);
48     static void TearDownTestCase(void);
49     void SetUp();
50     void TearDown();
51 
GetUiAppearanceAbilityTest()52     static sptr<UiAppearanceAbilityTest> GetUiAppearanceAbilityTest()
53     {
54         return new UiAppearanceAbilityTest;
55     }
56 
57 private:
58     int userId_;
59 };
60 
SetUpTestCase(void)61 void DarkModeTest::SetUpTestCase(void) {}
62 
TearDownTestCase(void)63 void DarkModeTest::TearDownTestCase(void) {}
64 
SetUp(void)65 void DarkModeTest::SetUp(void)
66 {
67     userId_ = geteuid();
68     seteuid(UISERVER_UID);
69 }
70 
TearDown(void)71 void DarkModeTest::TearDown(void)
72 {
73     seteuid(userId_);
74 }
75 
76 /**
77  * @tc.name: ui_appearance_test_001
78  * @tc.desc: Test SetDarkMode and GetDarkMode APIs when setting dark/light.
79  * @tc.type: FUNC
80  */
HWTEST_F(DarkModeTest, ui_appearance_test_001, TestSize.Level0)81 HWTEST_F(DarkModeTest, ui_appearance_test_001, TestSize.Level0)
82 {
83     LOGI("Test SetDarkMode and GetDarkMode APIs when setting dark/light.");
84 
85     auto test = DarkModeTest::GetUiAppearanceAbilityTest();
86     auto result = test->SetDarkMode(UiAppearanceAbilityInterface::DarkMode::ALWAYS_DARK);
87     EXPECT_EQ(result, 0);
88     auto mode = test->GetDarkMode();
89     EXPECT_EQ(mode, UiAppearanceAbilityInterface::DarkMode::ALWAYS_DARK);
90 
91     result = test->SetDarkMode(UiAppearanceAbilityInterface::DarkMode::ALWAYS_LIGHT);
92     EXPECT_EQ(result, 0);
93     mode = test->GetDarkMode();
94     EXPECT_EQ(mode, UiAppearanceAbilityInterface::DarkMode::ALWAYS_LIGHT);
95 }
96 
97 /**
98  * @tc.name: ui_appearance_test_002
99  * @tc.desc: Test SetDarkMode and GetDarkMode APIs when repeatedly setting dark/light.
100  * @tc.type: FUNC
101  */
HWTEST_F(DarkModeTest, ui_appearance_test_002, TestSize.Level0)102 HWTEST_F(DarkModeTest, ui_appearance_test_002, TestSize.Level0)
103 {
104     LOGI("Test SetDarkMode and GetDarkMode APIs when repeatedly setting dark/light.");
105 
106     auto test = DarkModeTest::GetUiAppearanceAbilityTest();
107     auto result = test->SetDarkMode(UiAppearanceAbilityInterface::DarkMode::ALWAYS_DARK);
108     EXPECT_EQ(result, UiAppearanceAbilityInterface::ErrCode::SUCCEEDED);
109     auto mode = test->GetDarkMode();
110     EXPECT_EQ(mode, UiAppearanceAbilityInterface::DarkMode::ALWAYS_DARK);
111 
112     result = test->SetDarkMode(UiAppearanceAbilityInterface::DarkMode::ALWAYS_DARK);
113     EXPECT_EQ(result, UiAppearanceAbilityInterface::ErrCode::SYS_ERR);
114     mode = test->GetDarkMode();
115     EXPECT_EQ(mode, UiAppearanceAbilityInterface::DarkMode::ALWAYS_DARK);
116 
117     result = test->SetDarkMode(UiAppearanceAbilityInterface::DarkMode::ALWAYS_LIGHT);
118     EXPECT_EQ(result, UiAppearanceAbilityInterface::ErrCode::SUCCEEDED);
119     mode = test->GetDarkMode();
120     EXPECT_EQ(mode, UiAppearanceAbilityInterface::DarkMode::ALWAYS_LIGHT);
121 
122     result = test->SetDarkMode(UiAppearanceAbilityInterface::DarkMode::ALWAYS_LIGHT);
123     EXPECT_EQ(result, UiAppearanceAbilityInterface::ErrCode::SYS_ERR);
124     mode = test->GetDarkMode();
125     EXPECT_EQ(mode, UiAppearanceAbilityInterface::DarkMode::ALWAYS_LIGHT);
126 }
127 
128 /**
129  * @tc.name: ui_appearance_test_003
130  * @tc.desc: Test the SetDarkMode API when setting an unexpected value
131  * @tc.type: FUNC
132  */
HWTEST_F(DarkModeTest, ui_appearance_test_003, TestSize.Level0)133 HWTEST_F(DarkModeTest, ui_appearance_test_003, TestSize.Level0)
134 {
135     LOGI("Test the SetDarkMode API when setting an unexpected value.");
136 
137     int result =
138         DarkModeTest::GetUiAppearanceAbilityTest()->SetDarkMode(UiAppearanceAbilityInterface::DarkMode::UNKNOWN);
139     EXPECT_NE(result, 0);
140 }
141 
142 /**
143  * @tc.name: ui_appearance_test_004
144  * @tc.desc: Test the font API
145  * @tc.type: FUNC
146  */
HWTEST_F(DarkModeTest, ui_appearance_test_004, TestSize.Level0)147 HWTEST_F(DarkModeTest, ui_appearance_test_004, TestSize.Level0)
148 {
149     LOGI("Test the font API");
150 
151     std::string fontScale;
152     int result =
153         DarkModeTest::GetUiAppearanceAbilityTest()->GetFontScale(fontScale);
154     EXPECT_EQ(result, 0);
155 
156     std::string fontWeightScale;
157     result =
158         DarkModeTest::GetUiAppearanceAbilityTest()->GetFontWeightScale(fontWeightScale);
159     EXPECT_EQ(result, 0);
160 }
161 } // namespace ArkUi::UiAppearance
162 } // namespace OHOS
163