1 /*
2 * Copyright (c) 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 <filesystem>
17 #include <random>
18 #include <gtest/gtest.h>
19
20 #include "mmi_log.h"
21 #include "multimodal_input_preferences_manager.h"
22
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "PreferencesManagerTestWithMock"
25
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr char DATA_ROOT_PATH[] { "/data/service/el1/public/multimodalinput/" };
30 constexpr char SETTING_FILE_NAME[] { "PreferencesManagerTestWithMock_preferencess.xml" };
31 }
32 using namespace testing::ext;
33
34 class PreferencesManagerTestWithMock : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase()42 void PreferencesManagerTestWithMock::SetUpTestCase()
43 {}
44
TearDownTestCase()45 void PreferencesManagerTestWithMock::TearDownTestCase()
46 {
47 std::filesystem::path testSettingPath { std::string(DATA_ROOT_PATH) + SETTING_FILE_NAME };
48
49 if (std::filesystem::exists(testSettingPath)) {
50 std::filesystem::remove(testSettingPath);
51 }
52 }
53
SetUp()54 void PreferencesManagerTestWithMock::SetUp()
55 {}
56
TearDown()57 void PreferencesManagerTestWithMock::TearDown()
58 {}
59
60 /**
61 * @tc.name: PreferencesManagerTestWithMock_InitPreferences_001
62 * @tc.desc: Test InitPreferences
63 * @tc.type: FUNC
64 * @tc.require:
65 */
66 HWTEST_F(PreferencesManagerTestWithMock, PreferencesManagerTestWithMock_InitPreferences_001, TestSize.Level1)
67 {
68 ASSERT_NO_FATAL_FAILURE(IPreferenceManager::GetInstance()->InitPreferences());
69 }
70
71 /**
72 * @tc.name: PreferencesManagerTestWithMock_GetPreferencesSettings_001
73 * @tc.desc: Test GetIntValue
74 * @tc.type: FUNC
75 * @tc.require:
76 */
77 HWTEST_F(PreferencesManagerTestWithMock, PreferencesManagerTestWithMock_GetIntValue_001, TestSize.Level1)
78 {
79 std::string unknownSetting { "unknown-setting" };
80 constexpr int32_t defaultValue { 123 };
81 ASSERT_NO_FATAL_FAILURE(IPreferenceManager::GetInstance()->GetIntValue(unknownSetting, defaultValue));
82 int32_t setting = IPreferenceManager::GetInstance()->GetIntValue(unknownSetting, defaultValue);
83 EXPECT_EQ(setting, defaultValue);
84 }
85
86 /**
87 * @tc.name: PreferencesManagerTestWithMock_GetIntValue_002
88 * @tc.desc: Test GetIntValue
89 * @tc.type: FUNC
90 * @tc.require:
91 */
92 HWTEST_F(PreferencesManagerTestWithMock, PreferencesManagerTestWithMock_GetIntValue_002, TestSize.Level1)
93 {
94 std::string settingName { "int-setting-01" };
95 std::string settingFileName { SETTING_FILE_NAME };
96 std::random_device rd;
97 std::mt19937 gen(rd());
98 int32_t nTests { 4096 };
99 double probability { 0.4 };
100 std::binomial_distribution<int32_t> distribution(nTests, probability);
101 auto settingValue = distribution(gen);
102 constexpr int32_t defaultValue { 123 };
103
104 auto ret = IPreferenceManager::GetInstance()->SetIntValue(settingName, settingFileName, settingValue);
105 ASSERT_EQ(ret, RET_OK);
106 auto setting = IPreferenceManager::GetInstance()->GetIntValue(settingName, defaultValue);
107 EXPECT_EQ(setting, settingValue);
108 }
109
110 /**
111 * @tc.name: PreferencesManagerTestWithMock_GetPreferencesSettings_001
112 * @tc.desc: Test GetBoolValue
113 * @tc.type: FUNC
114 * @tc.require:
115 */
116 HWTEST_F(PreferencesManagerTestWithMock, PreferencesManagerTestWithMock_GetBoolValue_001, TestSize.Level1)
117 {
118 std::string unknownSetting { "unknown-setting" };
119 ASSERT_NO_FATAL_FAILURE(IPreferenceManager::GetInstance()->GetBoolValue(unknownSetting, true));
120 bool setting = IPreferenceManager::GetInstance()->GetBoolValue(unknownSetting, true);
121 EXPECT_TRUE(setting);
122 }
123
124 /**
125 * @tc.name: PreferencesManagerTestWithMock_GetBoolValue_002
126 * @tc.desc: Test GetBoolValue
127 * @tc.type: FUNC
128 * @tc.require:
129 */
130 HWTEST_F(PreferencesManagerTestWithMock, PreferencesManagerTestWithMock_GetBoolValue_002, TestSize.Level1)
131 {
132 std::string settingName { "bool-setting-01" };
133 std::string settingFileName { SETTING_FILE_NAME };
134 constexpr bool settingValue { true };
135 constexpr bool defaultValue { false };
136 auto ret = IPreferenceManager::GetInstance()->SetBoolValue(settingName, settingFileName, settingValue);
137 ASSERT_EQ(ret, RET_OK);
138 auto setting = IPreferenceManager::GetInstance()->GetBoolValue(settingName, defaultValue);
139 EXPECT_TRUE(setting);
140 }
141
142 /**
143 * @tc.name: PreferencesManagerTestWithMock_GetShortKeyDuration_001
144 * @tc.desc: Test GetShortKeyDuration
145 * @tc.type: FUNC
146 * @tc.require:
147 */
148 HWTEST_F(PreferencesManagerTestWithMock, PreferencesManagerTestWithMock_GetShortKeyDuration_001, TestSize.Level1)
149 {
150 std::string testSetting { "unknown-shortkey-setting" };
151 ASSERT_NO_FATAL_FAILURE(IPreferenceManager::GetInstance()->GetShortKeyDuration(testSetting));
152 }
153
154 /**
155 * @tc.name: PreferencesManagerTestWithMock_GetShortKeyDuration_002
156 * @tc.desc: Test GetShortKeyDuration
157 * @tc.type: FUNC
158 * @tc.require:
159 */
160 HWTEST_F(PreferencesManagerTestWithMock, PreferencesManagerTestWithMock_GetShortKeyDuration_002, TestSize.Level1)
161 {
162 std::random_device rd;
163 std::mt19937 gen(rd());
164 int32_t nTests { 4096 };
165 double probability { 0.4 };
166 std::binomial_distribution<int32_t> distribution(nTests, probability);
167 auto settingValue = distribution(gen);
168 std::string testSetting { "unknown-shortkey-setting" };
169
170 auto ret = IPreferenceManager::GetInstance()->SetShortKeyDuration(testSetting, settingValue);
171 EXPECT_EQ(ret, RET_OK);
172 auto setting = IPreferenceManager::GetInstance()->GetShortKeyDuration(testSetting);
173 EXPECT_EQ(setting, settingValue);
174 }
175
176 /**
177 * @tc.name: PreferencesManagerTestWithMock_IsInitPreference_001
178 * @tc.desc: Test IsInitPreference
179 * @tc.type: FUNC
180 * @tc.require:
181 */
182 HWTEST_F(PreferencesManagerTestWithMock, PreferencesManagerTestWithMock_IsInitPreference_001, TestSize.Level1)
183 {
184 auto ret = IPreferenceManager::GetInstance()->InitPreferences();
185 if (ret != RET_OK) {
186 EXPECT_FALSE(IPreferenceManager::GetInstance()->IsInitPreference());
187 } else {
188 EXPECT_TRUE(IPreferenceManager::GetInstance()->IsInitPreference());
189 }
190 }
191 } // namespace MMI
192 } // namespace OHOS
193