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 <cstdint>
17 #include <list>
18 #include <string>
19 #include <sys/time.h>
20 
21 #include "sclock_log.h"
22 #include "screenlock_common.h"
23 #include "securec.h"
24 #include "preferences_util.h"
25 #include "screenlock_preferences_test.h"
26 
27 
28 namespace OHOS {
29 namespace ScreenLock {
30 using namespace testing::ext;
31 
SetUpTestCase()32 void ScreenLockPreferenceTest::SetUpTestCase()
33 {
34 }
35 
TearDownTestCase()36 void ScreenLockPreferenceTest::TearDownTestCase()
37 {
38 }
39 
SetUp()40 void ScreenLockPreferenceTest::SetUp()
41 {
42 }
43 
TearDown()44 void ScreenLockPreferenceTest::TearDown()
45 {
46 }
47 
48 /**
49 * @tc.name: ScreenLockPreferenceTest001
50 * @tc.desc: ScreenLockPreferenceTest String.
51 * @tc.type: FUNC
52 * @tc.require:
53 * @tc.author:
54 */
55 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest001, TestSize.Level0)
56 {
57     SCLOCK_HILOGD("ScreenLockPreferenceTest String");
58     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
59     if (preferencesUtil == nullptr) {
60         SCLOCK_HILOGE("preferencesUtil is nullptr!");
61         return;
62     }
63     int userId = 0;
64     std::string defaulVal = "test";
65     int result = preferencesUtil->SaveString(std::to_string(userId), defaulVal);
66     std::string val = preferencesUtil->ObtainString(std::to_string(userId), defaulVal);
67     SCLOCK_HILOGD("String.[result]:%{public}d, [val]:%{public}s", result, val.c_str());
68     preferencesUtil->RemoveKey(std::to_string(userId));
69     preferencesUtil->Refresh();
70 }
71 
72 /**
73 * @tc.name: ScreenLockPreferenceTest001
74 * @tc.desc: ScreenLockPreferenceTest Int.
75 * @tc.type: FUNC
76 * @tc.require:
77 * @tc.author:
78 */
79 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest002, TestSize.Level0)
80 {
81     SCLOCK_HILOGD("ScreenLockPreferenceTest Int");
82     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
83     if (preferencesUtil == nullptr) {
84         SCLOCK_HILOGE("preferencesUtil is nullptr!");
85         return;
86     }
87     int userId = 0;
88     int defaulVal = 0;
89     int result = preferencesUtil->SaveInt(std::to_string(userId), defaulVal);
90     int val = preferencesUtil->ObtainInt(std::to_string(userId), defaulVal);
91     SCLOCK_HILOGD("String.[result]:%{public}d, [val]:%{public}d", result, val);
92     preferencesUtil->RemoveKey(std::to_string(userId));
93     preferencesUtil->Refresh();
94 }
95 
96 /**
97 * @tc.name: ScreenLockPreferenceTest001
98 * @tc.desc: ScreenLockPreferenceTest Bool.
99 * @tc.type: FUNC
100 * @tc.require:
101 * @tc.author:
102 */
103 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest003, TestSize.Level0)
104 {
105     SCLOCK_HILOGD("ScreenLockPreferenceTest Bool");
106     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
107     if (preferencesUtil == nullptr) {
108         SCLOCK_HILOGE("preferencesUtil is nullptr!");
109         return;
110     }
111     int userId = 0;
112     bool defaulVal = false;
113     int result = preferencesUtil->SaveBool(std::to_string(userId), defaulVal);
114     bool val = preferencesUtil->ObtainBool(std::to_string(userId), defaulVal);
115     SCLOCK_HILOGD("String.[result]:%{public}d, [val]:%{public}d", result, val);
116     preferencesUtil->RemoveKey(std::to_string(userId));
117     preferencesUtil->Refresh();
118 }
119 
120 /**
121 * @tc.name: ScreenLockPreferenceTest001
122 * @tc.desc: ScreenLockPreferenceTest Long.
123 * @tc.type: FUNC
124 * @tc.require:
125 * @tc.author:
126 */
127 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest004, TestSize.Level0)
128 {
129     SCLOCK_HILOGD("ScreenLockPreferenceTest Long");
130     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
131     if (preferencesUtil == nullptr) {
132         SCLOCK_HILOGE("preferencesUtil is nullptr!");
133         return;
134     }
135     int userId = 0;
136     int64_t defaulVal = false;
137     preferencesUtil->SaveLong(std::to_string(userId), defaulVal);
138     preferencesUtil->ObtainLong(std::to_string(userId), defaulVal);
139     preferencesUtil->RemoveKey(std::to_string(userId));
140     preferencesUtil->Refresh();
141 }
142 
143 /**
144 * @tc.name: ScreenLockPreferenceTest001
145 * @tc.desc: ScreenLockPreferenceTest Float.
146 * @tc.type: FUNC
147 * @tc.require:
148 * @tc.author:
149 */
150 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest005, TestSize.Level0)
151 {
152     SCLOCK_HILOGD("ScreenLockPreferenceTest Float");
153     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
154     if (preferencesUtil == nullptr) {
155         SCLOCK_HILOGE("preferencesUtil is nullptr!");
156         return;
157     }
158     int userId = 0;
159     float defaulVal = 1.0;
160     int result = preferencesUtil->SaveFloat(std::to_string(userId), defaulVal);
161     float val = preferencesUtil->ObtainFloat(std::to_string(userId), defaulVal);
162     SCLOCK_HILOGD("String.[result]:%{public}d, [val]:%{public}f", result, val);
163     preferencesUtil->RemoveKey(std::to_string(userId));
164     preferencesUtil->Refresh();
165 }
166 
167 /**
168 * @tc.name: ScreenLockPreferenceTest001
169 * @tc.desc: ScreenLockPreferenceTest RmvAll.
170 * @tc.type: FUNC
171 * @tc.require:
172 * @tc.author:
173 */
174 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest006, TestSize.Level0)
175 {
176     SCLOCK_HILOGD("ScreenLockPreferenceTest RmvAll");
177     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
178     if (preferencesUtil == nullptr) {
179         SCLOCK_HILOGE("preferencesUtil is nullptr!");
180         return;
181     }
182     preferencesUtil->RemoveAll();
183     preferencesUtil->RefreshSync();
184 }
185 
186 
187 } // namespace ScreenLock
188 } // namespace OHOS
189