• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     EXPECT_EQ(defaulVal, val);
71 }
72 
73 /**
74 * @tc.name: ScreenLockPreferenceTest001
75 * @tc.desc: ScreenLockPreferenceTest Int.
76 * @tc.type: FUNC
77 * @tc.require:
78 * @tc.author:
79 */
80 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest002, TestSize.Level0)
81 {
82     SCLOCK_HILOGD("ScreenLockPreferenceTest Int");
83     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
84     if (preferencesUtil == nullptr) {
85         SCLOCK_HILOGE("preferencesUtil is nullptr!");
86         return;
87     }
88     int userId = 0;
89     int defaulVal = 0;
90     int result = preferencesUtil->SaveInt(std::to_string(userId), defaulVal);
91     int val = preferencesUtil->ObtainInt(std::to_string(userId), defaulVal);
92     SCLOCK_HILOGD("String.[result]:%{public}d, [val]:%{public}d", result, val);
93     preferencesUtil->RemoveKey(std::to_string(userId));
94     preferencesUtil->Refresh();
95     EXPECT_EQ(defaulVal, val);
96 }
97 
98 /**
99 * @tc.name: ScreenLockPreferenceTest001
100 * @tc.desc: ScreenLockPreferenceTest Bool.
101 * @tc.type: FUNC
102 * @tc.require:
103 * @tc.author:
104 */
105 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest003, TestSize.Level0)
106 {
107     SCLOCK_HILOGD("ScreenLockPreferenceTest Bool");
108     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
109     if (preferencesUtil == nullptr) {
110         SCLOCK_HILOGE("preferencesUtil is nullptr!");
111         return;
112     }
113     int userId = 0;
114     bool defaulVal = false;
115     int result = preferencesUtil->SaveBool(std::to_string(userId), defaulVal);
116     bool val = preferencesUtil->ObtainBool(std::to_string(userId), defaulVal);
117     SCLOCK_HILOGD("String.[result]:%{public}d, [val]:%{public}d", result, val);
118     preferencesUtil->RemoveKey(std::to_string(userId));
119     preferencesUtil->Refresh();
120     EXPECT_EQ(defaulVal, val);
121 }
122 
123 /**
124 * @tc.name: ScreenLockPreferenceTest001
125 * @tc.desc: ScreenLockPreferenceTest Long.
126 * @tc.type: FUNC
127 * @tc.require:
128 * @tc.author:
129 */
130 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest004, TestSize.Level0)
131 {
132     SCLOCK_HILOGD("ScreenLockPreferenceTest Long");
133     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
134     if (preferencesUtil == nullptr) {
135         SCLOCK_HILOGE("preferencesUtil is nullptr!");
136         return;
137     }
138     int userId = 0;
139     int64_t defaulVal = false;
140     preferencesUtil->SaveLong(std::to_string(userId), defaulVal);
141     int64_t val = preferencesUtil->ObtainLong(std::to_string(userId), defaulVal);
142     preferencesUtil->RemoveKey(std::to_string(userId));
143     preferencesUtil->Refresh();
144     EXPECT_EQ(defaulVal, val);
145 }
146 
147 /**
148 * @tc.name: ScreenLockPreferenceTest001
149 * @tc.desc: ScreenLockPreferenceTest Float.
150 * @tc.type: FUNC
151 * @tc.require:
152 * @tc.author:
153 */
154 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest005, TestSize.Level0)
155 {
156     SCLOCK_HILOGD("ScreenLockPreferenceTest Float");
157     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
158     if (preferencesUtil == nullptr) {
159         SCLOCK_HILOGE("preferencesUtil is nullptr!");
160         return;
161     }
162     int userId = 0;
163     float defaulVal = 1.0;
164     int result = preferencesUtil->SaveFloat(std::to_string(userId), defaulVal);
165     float val = preferencesUtil->ObtainFloat(std::to_string(userId), defaulVal);
166     SCLOCK_HILOGD("String.[result]:%{public}d, [val]:%{public}f", result, val);
167     preferencesUtil->RemoveKey(std::to_string(userId));
168     preferencesUtil->Refresh();
169     EXPECT_EQ(defaulVal, val);
170 }
171 
172 /**
173 * @tc.name: ScreenLockPreferenceTest001
174 * @tc.desc: ScreenLockPreferenceTest RmvAll.
175 * @tc.type: FUNC
176 * @tc.require:
177 * @tc.author:
178 */
179 HWTEST_F(ScreenLockPreferenceTest, ScreenLockPreferenceTest006, TestSize.Level0)
180 {
181     SCLOCK_HILOGD("ScreenLockPreferenceTest RmvAll");
182     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
183     if (preferencesUtil == nullptr) {
184         SCLOCK_HILOGE("preferencesUtil is nullptr!");
185         return;
186     }
187     preferencesUtil->RemoveAll();
188     preferencesUtil->RefreshSync();
189     EXPECT_NE(preferencesUtil, nullptr);
190 }
191 
192 
193 } // namespace ScreenLock
194 } // namespace OHOS
195