1fc223305Sopenharmony_ci/*
2fc223305Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3fc223305Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fc223305Sopenharmony_ci * you may not use this file except in compliance with the License.
5fc223305Sopenharmony_ci * You may obtain a copy of the License at
6fc223305Sopenharmony_ci *
7fc223305Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8fc223305Sopenharmony_ci *
9fc223305Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fc223305Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fc223305Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fc223305Sopenharmony_ci * See the License for the specific language governing permissions and
13fc223305Sopenharmony_ci * limitations under the License.
14fc223305Sopenharmony_ci */
15fc223305Sopenharmony_ci#include <cctype>
16fc223305Sopenharmony_ci#include <condition_variable>
17fc223305Sopenharmony_ci#include <cstdint>
18fc223305Sopenharmony_ci#include <fcntl.h>
19fc223305Sopenharmony_ci#include <filesystem>
20fc223305Sopenharmony_ci#include <functional>
21fc223305Sopenharmony_ci#include <gtest/gtest.h>
22fc223305Sopenharmony_ci#include <iostream>
23fc223305Sopenharmony_ci#include <list>
24fc223305Sopenharmony_ci#include <map>
25fc223305Sopenharmony_ci#include <memory>
26fc223305Sopenharmony_ci#include <mutex>
27fc223305Sopenharmony_ci#include <string>
28fc223305Sopenharmony_ci#include <sys/mman.h>
29fc223305Sopenharmony_ci#include <thread>
30fc223305Sopenharmony_ci#include <vector>
31fc223305Sopenharmony_ci
32fc223305Sopenharmony_ci#include "log_print.h"
33fc223305Sopenharmony_ci#include "preferences_test_utils.h"
34fc223305Sopenharmony_ci#include "oh_preferences_impl.h"
35fc223305Sopenharmony_ci#include "oh_preferences.h"
36fc223305Sopenharmony_ci#include "oh_preferences_err_code.h"
37fc223305Sopenharmony_ci#include "oh_preferences_value.h"
38fc223305Sopenharmony_ci#include "oh_preferences_option.h"
39fc223305Sopenharmony_ci#include "preferences_helper.h"
40fc223305Sopenharmony_ci
41fc223305Sopenharmony_ciusing namespace testing::ext;
42fc223305Sopenharmony_ciusing namespace testing;
43fc223305Sopenharmony_ciusing namespace OHOS::PreferencesNdk;
44fc223305Sopenharmony_ciusing namespace OHOS::NativePreferences;
45fc223305Sopenharmony_cinamespace {
46fc223305Sopenharmony_ciclass PreferencesNdkTest : public testing::Test {
47fc223305Sopenharmony_cipublic:
48fc223305Sopenharmony_ci    static void SetUpTestCase(void);
49fc223305Sopenharmony_ci    static void TearDownTestCase(void);
50fc223305Sopenharmony_ci    void SetUp();
51fc223305Sopenharmony_ci    void TearDown();
52fc223305Sopenharmony_ci};
53fc223305Sopenharmony_ci
54fc223305Sopenharmony_civoid PreferencesNdkTest::SetUpTestCase(void)
55fc223305Sopenharmony_ci{
56fc223305Sopenharmony_ci    NdkTestUtils::CreateDirectoryRecursively("/data/test/");
57fc223305Sopenharmony_ci}
58fc223305Sopenharmony_civoid PreferencesNdkTest::TearDownTestCase(void) {}
59fc223305Sopenharmony_ci
60fc223305Sopenharmony_civoid PreferencesNdkTest::SetUp(void)
61fc223305Sopenharmony_ci{
62fc223305Sopenharmony_ci    NdkTestUtils::CreateDirectoryRecursively("/data/test/");
63fc223305Sopenharmony_ci}
64fc223305Sopenharmony_ci
65fc223305Sopenharmony_civoid PreferencesNdkTest::TearDown(void) {}
66fc223305Sopenharmony_ci
67fc223305Sopenharmony_cienum class PrefDataType { UNASSIGNED, INT, STRING, BOOL };
68fc223305Sopenharmony_ci
69fc223305Sopenharmony_ciconst uint32_t INVALID_INDEX = 100;
70fc223305Sopenharmony_ci
71fc223305Sopenharmony_cistatic OH_PreferencesOption *GetCommonOption()
72fc223305Sopenharmony_ci{
73fc223305Sopenharmony_ci    OH_PreferencesOption *option = OH_PreferencesOption_Create();
74fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetFileName(option, "testdb"), PREFERENCES_OK);
75fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetBundleName(option, "com.uttest"), PREFERENCES_OK);
76fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetDataGroupId(option, "123"), PREFERENCES_OK);
77fc223305Sopenharmony_ci    return option;
78fc223305Sopenharmony_ci}
79fc223305Sopenharmony_ci
80fc223305Sopenharmony_cistd::map<std::string, int> g_intDataMap = {
81fc223305Sopenharmony_ci    {"ndktest_int_key_1", -2147483648},
82fc223305Sopenharmony_ci    {"ndktest_int_key_2", -1},
83fc223305Sopenharmony_ci    {"ndktest_int_key_3", 0},
84fc223305Sopenharmony_ci    {"ndktest_int_key_4", 1},
85fc223305Sopenharmony_ci    {"ndktest_int_key_5", 2147483647}
86fc223305Sopenharmony_ci};
87fc223305Sopenharmony_ci
88fc223305Sopenharmony_cistd::map<std::string, std::string> g_stringDataMap = {
89fc223305Sopenharmony_ci    {"ndktest_string_key_1", "2679b2c70120214984b3aec34fc849dc996f40e3cdb60f3b3eaf8abe2559439a"},
90fc223305Sopenharmony_ci    {"ndktest_string_key_2", "+88780079687688"},
91fc223305Sopenharmony_ci    {"ndktest_string_key_3", "/data/storage/el2/base/files/Thumbnail_1717209543267.jpg"},
92fc223305Sopenharmony_ci    {"ndktest_string_key_4", "A NEW PHONE"},
93fc223305Sopenharmony_ci    {"ndktest_string_key_5", "https://upfile-drcn.platform.hicloud.com/"}
94fc223305Sopenharmony_ci};
95fc223305Sopenharmony_ci
96fc223305Sopenharmony_cistd::map<std::string, bool> g_boolDataMap = {
97fc223305Sopenharmony_ci    {"ndktest_bool_key_1", true},
98fc223305Sopenharmony_ci    {"ndktest_bool_key_2", false},
99fc223305Sopenharmony_ci    {"ndktest_bool_key_3", false},
100fc223305Sopenharmony_ci    {"ndktest_bool_key_4", true},
101fc223305Sopenharmony_ci    {"ndktest_bool_key_5", true}
102fc223305Sopenharmony_ci};
103fc223305Sopenharmony_ci
104fc223305Sopenharmony_ciint g_changeNum = 0;
105fc223305Sopenharmony_civoid DataChangeObserverCallback(void *context, const OH_PreferencesPair *pairs, uint32_t count)
106fc223305Sopenharmony_ci{
107fc223305Sopenharmony_ci    for (uint32_t i = 0; i < count; i++) {
108fc223305Sopenharmony_ci        const OH_PreferencesValue *pValue = OH_PreferencesPair_GetPreferencesValue(pairs, i);
109fc223305Sopenharmony_ci        Preference_ValueType type = OH_PreferencesValue_GetValueType(pValue);
110fc223305Sopenharmony_ci        const char *pKey = OH_PreferencesPair_GetKey(pairs, i);
111fc223305Sopenharmony_ci        EXPECT_NE(pKey, nullptr);
112fc223305Sopenharmony_ci        if (type == Preference_ValueType::PREFERENCE_TYPE_INT) {
113fc223305Sopenharmony_ci            int intV = 0;
114fc223305Sopenharmony_ci            OH_PreferencesValue_GetInt(pValue, &intV);
115fc223305Sopenharmony_ci        } else if (type == Preference_ValueType::PREFERENCE_TYPE_BOOL) {
116fc223305Sopenharmony_ci            bool boolV = false;
117fc223305Sopenharmony_ci            OH_PreferencesValue_GetBool(pValue, &boolV);
118fc223305Sopenharmony_ci        } else if (type == Preference_ValueType::PREFERENCE_TYPE_STRING) {
119fc223305Sopenharmony_ci            char *stringV = nullptr;
120fc223305Sopenharmony_ci            uint32_t len = 0;
121fc223305Sopenharmony_ci            OH_PreferencesValue_GetString(pValue, &stringV, &len);
122fc223305Sopenharmony_ci            OH_Preferences_FreeString(stringV);
123fc223305Sopenharmony_ci        }
124fc223305Sopenharmony_ci        g_changeNum++;
125fc223305Sopenharmony_ci    }
126fc223305Sopenharmony_ci}
127fc223305Sopenharmony_ci
128fc223305Sopenharmony_ciint PreferencesFlush(OH_Preferences *preference)
129fc223305Sopenharmony_ci{
130fc223305Sopenharmony_ci    OH_PreferencesImpl *pref = static_cast<OH_PreferencesImpl *>(preference);
131fc223305Sopenharmony_ci    std::shared_ptr<OHOS::NativePreferences::Preferences> nativePreferences = pref->GetNativePreferences();
132fc223305Sopenharmony_ci    return nativePreferences->FlushSync();
133fc223305Sopenharmony_ci}
134fc223305Sopenharmony_ci
135fc223305Sopenharmony_civoid SetAllValuesWithCheck(OH_Preferences *pref)
136fc223305Sopenharmony_ci{
137fc223305Sopenharmony_ci    for (auto &[key, value] : g_intDataMap) {
138fc223305Sopenharmony_ci        EXPECT_EQ(OH_Preferences_SetInt(pref, key.c_str(), value), PREFERENCES_OK);
139fc223305Sopenharmony_ci        EXPECT_EQ(PreferencesFlush(pref), OHOS::NativePreferences::E_OK);
140fc223305Sopenharmony_ci        int res = 0;
141fc223305Sopenharmony_ci        EXPECT_EQ(OH_Preferences_GetInt(pref, key.c_str(), &res), PREFERENCES_OK);
142fc223305Sopenharmony_ci        EXPECT_EQ(res, value);
143fc223305Sopenharmony_ci    }
144fc223305Sopenharmony_ci    for (auto &[key, value] : g_stringDataMap) {
145fc223305Sopenharmony_ci        EXPECT_EQ(OH_Preferences_SetString(pref, key.c_str(), value.c_str()), PREFERENCES_OK);
146fc223305Sopenharmony_ci        EXPECT_EQ(PreferencesFlush(pref), OHOS::NativePreferences::E_OK);
147fc223305Sopenharmony_ci        char *res = nullptr;
148fc223305Sopenharmony_ci        uint32_t len = 0;
149fc223305Sopenharmony_ci        EXPECT_EQ(OH_Preferences_GetString(pref, key.c_str(), &res, &len), PREFERENCES_OK);
150fc223305Sopenharmony_ci        EXPECT_EQ(strcmp(res, value.c_str()), 0);
151fc223305Sopenharmony_ci        OH_Preferences_FreeString(res);
152fc223305Sopenharmony_ci    }
153fc223305Sopenharmony_ci    for (auto &[key, value] : g_boolDataMap) {
154fc223305Sopenharmony_ci        EXPECT_EQ(OH_Preferences_SetBool(pref, key.c_str(), value), PREFERENCES_OK);
155fc223305Sopenharmony_ci        EXPECT_EQ(PreferencesFlush(pref), OHOS::NativePreferences::E_OK);
156fc223305Sopenharmony_ci        bool res;
157fc223305Sopenharmony_ci        EXPECT_EQ(OH_Preferences_GetBool(pref, key.c_str(), &res), PREFERENCES_OK);
158fc223305Sopenharmony_ci        EXPECT_EQ(res, value);
159fc223305Sopenharmony_ci    }
160fc223305Sopenharmony_ci}
161fc223305Sopenharmony_ci
162fc223305Sopenharmony_civoid CheckTargetTypeValues(OH_Preferences *pref, bool exist, PrefDataType pdt)
163fc223305Sopenharmony_ci{
164fc223305Sopenharmony_ci    if (pdt == PrefDataType::INT) {
165fc223305Sopenharmony_ci        for (auto &[key, value] : g_intDataMap) {
166fc223305Sopenharmony_ci            int res;
167fc223305Sopenharmony_ci            if (exist) {
168fc223305Sopenharmony_ci                EXPECT_EQ(OH_Preferences_GetInt(pref, key.c_str(), &res), PREFERENCES_OK);
169fc223305Sopenharmony_ci                EXPECT_EQ(res, value);
170fc223305Sopenharmony_ci            } else {
171fc223305Sopenharmony_ci                EXPECT_EQ(OH_Preferences_GetInt(pref, key.c_str(), &res), PREFERENCES_ERROR_KEY_NOT_FOUND);
172fc223305Sopenharmony_ci            }
173fc223305Sopenharmony_ci        }
174fc223305Sopenharmony_ci    } else if (pdt == PrefDataType::STRING) {
175fc223305Sopenharmony_ci        for (auto &[key, value] : g_stringDataMap) {
176fc223305Sopenharmony_ci            char *res = nullptr;
177fc223305Sopenharmony_ci            uint32_t len = 0;
178fc223305Sopenharmony_ci            if (exist) {
179fc223305Sopenharmony_ci                EXPECT_EQ(OH_Preferences_GetString(pref, key.c_str(), &res, &len), PREFERENCES_OK);
180fc223305Sopenharmony_ci                EXPECT_EQ(strcmp(res, value.c_str()), 0);
181fc223305Sopenharmony_ci            } else {
182fc223305Sopenharmony_ci                EXPECT_EQ(OH_Preferences_GetString(pref, key.c_str(), &res, &len), PREFERENCES_ERROR_KEY_NOT_FOUND);
183fc223305Sopenharmony_ci            }
184fc223305Sopenharmony_ci            OH_Preferences_FreeString(res);
185fc223305Sopenharmony_ci        }
186fc223305Sopenharmony_ci    } else if (pdt == PrefDataType::BOOL) {
187fc223305Sopenharmony_ci        for (auto &[key, value] : g_boolDataMap) {
188fc223305Sopenharmony_ci            bool res;
189fc223305Sopenharmony_ci            if (exist) {
190fc223305Sopenharmony_ci                EXPECT_EQ(OH_Preferences_GetBool(pref, key.c_str(), &res), PREFERENCES_OK);
191fc223305Sopenharmony_ci                EXPECT_EQ(res, value);
192fc223305Sopenharmony_ci            } else {
193fc223305Sopenharmony_ci                EXPECT_EQ(OH_Preferences_GetBool(pref, key.c_str(), &res), PREFERENCES_ERROR_KEY_NOT_FOUND);
194fc223305Sopenharmony_ci            }
195fc223305Sopenharmony_ci        }
196fc223305Sopenharmony_ci    }
197fc223305Sopenharmony_ci}
198fc223305Sopenharmony_ci
199fc223305Sopenharmony_ci/**
200fc223305Sopenharmony_ci * @tc.name: NDKPreferencesGetTest_001
201fc223305Sopenharmony_ci * @tc.desc: 测试先put int类型的kv,再get相同的key,值相同
202fc223305Sopenharmony_ci * @tc.type: FUNC
203fc223305Sopenharmony_ci * @tc.require: NA
204fc223305Sopenharmony_ci * @tc.author: Song Yixiu
205fc223305Sopenharmony_ci */
206fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesGetTest_001, TestSize.Level1)
207fc223305Sopenharmony_ci{
208fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
209fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
210fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
211fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
212fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
213fc223305Sopenharmony_ci
214fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
215fc223305Sopenharmony_ci
216fc223305Sopenharmony_ci    const char *key = "test_key_int";
217fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_SetInt(pref, key, 12), PREFERENCES_OK);
218fc223305Sopenharmony_ci    int ret;
219fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_GetInt(pref, key, &ret), PREFERENCES_OK);
220fc223305Sopenharmony_ci    EXPECT_EQ(ret, 12);
221fc223305Sopenharmony_ci
222fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
223fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
224fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
225fc223305Sopenharmony_ci}
226fc223305Sopenharmony_ci
227fc223305Sopenharmony_ci/**
228fc223305Sopenharmony_ci * @tc.name: NDKPreferencesGetTest_002
229fc223305Sopenharmony_ci * @tc.desc: 测试先put string类型的kv,再get相同的key,值相同
230fc223305Sopenharmony_ci * @tc.type: FUNC
231fc223305Sopenharmony_ci * @tc.require: NA
232fc223305Sopenharmony_ci * @tc.author: Song Yixiu
233fc223305Sopenharmony_ci */
234fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesGetTest_002, TestSize.Level1)
235fc223305Sopenharmony_ci{
236fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
237fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
238fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
239fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
240fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
241fc223305Sopenharmony_ci
242fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
243fc223305Sopenharmony_ci
244fc223305Sopenharmony_ci    const char *key = "test_key_string";
245fc223305Sopenharmony_ci    const char *value = "test_value";
246fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_SetString(pref, key, value), PREFERENCES_OK);
247fc223305Sopenharmony_ci    char *ret = nullptr;
248fc223305Sopenharmony_ci    uint32_t len = 0;
249fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_GetString(pref, key, &ret, &len), PREFERENCES_OK);
250fc223305Sopenharmony_ci    EXPECT_EQ(strcmp(ret, value), 0);
251fc223305Sopenharmony_ci    OH_Preferences_FreeString(ret);
252fc223305Sopenharmony_ci
253fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
254fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
255fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
256fc223305Sopenharmony_ci}
257fc223305Sopenharmony_ci
258fc223305Sopenharmony_ci/**
259fc223305Sopenharmony_ci * @tc.name: NDKPreferencesGetTest_003
260fc223305Sopenharmony_ci * @tc.desc: 测试先put bool类型的kv,再get相同的key,值相同
261fc223305Sopenharmony_ci * @tc.type: FUNC
262fc223305Sopenharmony_ci * @tc.require: NA
263fc223305Sopenharmony_ci * @tc.author: Song Yixiu
264fc223305Sopenharmony_ci */
265fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesGetTest_003, TestSize.Level1)
266fc223305Sopenharmony_ci{
267fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
268fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
269fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
270fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
271fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
272fc223305Sopenharmony_ci
273fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
274fc223305Sopenharmony_ci
275fc223305Sopenharmony_ci    const char *key = "test_key_bool";
276fc223305Sopenharmony_ci    bool value = true;
277fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_SetBool(pref, key, value), PREFERENCES_OK);
278fc223305Sopenharmony_ci    bool ret;
279fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_GetBool(pref, key, &ret), PREFERENCES_OK);
280fc223305Sopenharmony_ci    EXPECT_EQ(ret, value);
281fc223305Sopenharmony_ci
282fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
283fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
284fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
285fc223305Sopenharmony_ci}
286fc223305Sopenharmony_ci
287fc223305Sopenharmony_ci/**
288fc223305Sopenharmony_ci * @tc.name: NDKPreferencesGetTest_004
289fc223305Sopenharmony_ci * @tc.desc: 测试get不存在的kv
290fc223305Sopenharmony_ci * @tc.type: FUNC
291fc223305Sopenharmony_ci * @tc.require: NA
292fc223305Sopenharmony_ci * @tc.author: Song Yixiu
293fc223305Sopenharmony_ci */
294fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesGetTest_004, TestSize.Level1)
295fc223305Sopenharmony_ci{
296fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
297fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
298fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
299fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
300fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
301fc223305Sopenharmony_ci
302fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, false, PrefDataType::INT);
303fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, false, PrefDataType::STRING);
304fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, false, PrefDataType::BOOL);
305fc223305Sopenharmony_ci
306fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
307fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
308fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
309fc223305Sopenharmony_ci}
310fc223305Sopenharmony_ci
311fc223305Sopenharmony_ci/**
312fc223305Sopenharmony_ci * @tc.name: NDKPreferencesPutTest_001
313fc223305Sopenharmony_ci * @tc.desc: 测试先get不存在的kv,再put进kv,然后get
314fc223305Sopenharmony_ci * @tc.type: FUNC
315fc223305Sopenharmony_ci * @tc.require: NA
316fc223305Sopenharmony_ci * @tc.author: Song Yixiu
317fc223305Sopenharmony_ci */
318fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesPutTest_001, TestSize.Level1)
319fc223305Sopenharmony_ci{
320fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
321fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
322fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
323fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
324fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
325fc223305Sopenharmony_ci
326fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, false, PrefDataType::INT);
327fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, false, PrefDataType::STRING);
328fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, false, PrefDataType::BOOL);
329fc223305Sopenharmony_ci
330fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
331fc223305Sopenharmony_ci
332fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, true, PrefDataType::INT);
333fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, true, PrefDataType::STRING);
334fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, true, PrefDataType::BOOL);
335fc223305Sopenharmony_ci
336fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
337fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
338fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
339fc223305Sopenharmony_ci}
340fc223305Sopenharmony_ci
341fc223305Sopenharmony_ci/**
342fc223305Sopenharmony_ci * @tc.name: NDKPreferencesUpdateTest_001
343fc223305Sopenharmony_ci * @tc.desc: 测试先put int类型的kv,然后get,然后再put新的值进行更新,再get
344fc223305Sopenharmony_ci * @tc.type: FUNC
345fc223305Sopenharmony_ci * @tc.require: NA
346fc223305Sopenharmony_ci * @tc.author: Song Yixiu
347fc223305Sopenharmony_ci */
348fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesUpdateTest_001, TestSize.Level1)
349fc223305Sopenharmony_ci{
350fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
351fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
352fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
353fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
354fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
355fc223305Sopenharmony_ci
356fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
357fc223305Sopenharmony_ci
358fc223305Sopenharmony_ci    const char *key = "ndktest_int_key_1";
359fc223305Sopenharmony_ci    int newValue = 10;
360fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_SetInt(pref, key, newValue), PREFERENCES_OK);
361fc223305Sopenharmony_ci    int ret;
362fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_GetInt(pref, key, &ret), PREFERENCES_OK);
363fc223305Sopenharmony_ci    EXPECT_EQ(ret, newValue);
364fc223305Sopenharmony_ci
365fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
366fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
367fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
368fc223305Sopenharmony_ci}
369fc223305Sopenharmony_ci
370fc223305Sopenharmony_ci/**
371fc223305Sopenharmony_ci * @tc.name: NDKPreferencesUpdateTest_002
372fc223305Sopenharmony_ci * @tc.desc: 测试先put string类型的kv,然后get,然后再put新的值进行更新,再get
373fc223305Sopenharmony_ci * @tc.type: FUNC
374fc223305Sopenharmony_ci * @tc.require: NA
375fc223305Sopenharmony_ci * @tc.author: Song Yixiu
376fc223305Sopenharmony_ci */
377fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesUpdateTest_002, TestSize.Level1)
378fc223305Sopenharmony_ci{
379fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
380fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
381fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
382fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
383fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
384fc223305Sopenharmony_ci
385fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
386fc223305Sopenharmony_ci
387fc223305Sopenharmony_ci    const char *key = "ndktest_string_key_1";
388fc223305Sopenharmony_ci    const char *newValue = "adlkj1#$&sdioj9i0i841a61aa4gh44o98()!@@.,.{:/',}";
389fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_SetString(pref, key, newValue), PREFERENCES_OK);
390fc223305Sopenharmony_ci    char *ret = nullptr;
391fc223305Sopenharmony_ci    uint32_t len = 0;
392fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_GetString(pref, key, &ret, &len), PREFERENCES_OK);
393fc223305Sopenharmony_ci    EXPECT_EQ(strcmp(ret, newValue), 0);
394fc223305Sopenharmony_ci    OH_Preferences_FreeString(ret);
395fc223305Sopenharmony_ci
396fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
397fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
398fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
399fc223305Sopenharmony_ci}
400fc223305Sopenharmony_ci
401fc223305Sopenharmony_ci/**
402fc223305Sopenharmony_ci * @tc.name: NDKPreferencesUpdateTest_003
403fc223305Sopenharmony_ci * @tc.desc: 测试先put bool类型的kv,然后get,然后再put新的值进行更新,再get
404fc223305Sopenharmony_ci * @tc.type: FUNC
405fc223305Sopenharmony_ci * @tc.require: NA
406fc223305Sopenharmony_ci * @tc.author: Song Yixiu
407fc223305Sopenharmony_ci */
408fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesUpdateTest_003, TestSize.Level1)
409fc223305Sopenharmony_ci{
410fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
411fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
412fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
413fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
414fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
415fc223305Sopenharmony_ci
416fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
417fc223305Sopenharmony_ci
418fc223305Sopenharmony_ci    const char *key = "ndktest_bool_key_1";
419fc223305Sopenharmony_ci    bool newValue = false;
420fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_SetBool(pref, key, newValue), PREFERENCES_OK);
421fc223305Sopenharmony_ci    bool ret;
422fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_GetBool(pref, key, &ret), PREFERENCES_OK);
423fc223305Sopenharmony_ci    EXPECT_EQ(ret, newValue);
424fc223305Sopenharmony_ci
425fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
426fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
427fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
428fc223305Sopenharmony_ci}
429fc223305Sopenharmony_ci
430fc223305Sopenharmony_ci/**
431fc223305Sopenharmony_ci * @tc.name: NDKPreferencesDeleteTest_001
432fc223305Sopenharmony_ci * @tc.desc: test Delete exist key
433fc223305Sopenharmony_ci * @tc.type: FUNC
434fc223305Sopenharmony_ci * @tc.require: NA
435fc223305Sopenharmony_ci * @tc.author: Liu Xiaolong
436fc223305Sopenharmony_ci */
437fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesDeleteTest_001, TestSize.Level1)
438fc223305Sopenharmony_ci{
439fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
440fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
441fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
442fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
443fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
444fc223305Sopenharmony_ci
445fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
446fc223305Sopenharmony_ci
447fc223305Sopenharmony_ci    for (auto &[key, value] : g_stringDataMap) {
448fc223305Sopenharmony_ci        EXPECT_EQ(OH_Preferences_Delete(pref, key.c_str()), PREFERENCES_OK);
449fc223305Sopenharmony_ci    }
450fc223305Sopenharmony_ci
451fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, true, PrefDataType::INT);
452fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, false, PrefDataType::STRING);
453fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, true, PrefDataType::BOOL);
454fc223305Sopenharmony_ci
455fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
456fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
457fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
458fc223305Sopenharmony_ci}
459fc223305Sopenharmony_ci
460fc223305Sopenharmony_ci/**
461fc223305Sopenharmony_ci * @tc.name: NDKPreferencesDeleteTest_002
462fc223305Sopenharmony_ci * @tc.desc: test Delete non-exist key
463fc223305Sopenharmony_ci * @tc.type: FUNC
464fc223305Sopenharmony_ci * @tc.require: NA
465fc223305Sopenharmony_ci * @tc.author: Liu Xiaolong
466fc223305Sopenharmony_ci */
467fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesDeleteTest_002, TestSize.Level1)
468fc223305Sopenharmony_ci{
469fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
470fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
471fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
472fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
473fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
474fc223305Sopenharmony_ci
475fc223305Sopenharmony_ci    CheckTargetTypeValues(pref, false, PrefDataType::INT);
476fc223305Sopenharmony_ci
477fc223305Sopenharmony_ci    for (auto &[key, value] : g_intDataMap) {
478fc223305Sopenharmony_ci        EXPECT_EQ(OH_Preferences_Delete(pref, key.c_str()), PREFERENCES_OK);
479fc223305Sopenharmony_ci    }
480fc223305Sopenharmony_ci
481fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
482fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
483fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
484fc223305Sopenharmony_ci}
485fc223305Sopenharmony_ci
486fc223305Sopenharmony_cistatic void RegisterObsInFunc(OH_Preferences *pref)
487fc223305Sopenharmony_ci{
488fc223305Sopenharmony_ci    const char *keys[] = {"ndktest_int_key_1", "ndktest_string_key_1", "ndktest_bool_key_1"};
489fc223305Sopenharmony_ci
490fc223305Sopenharmony_ci    int ret = OH_Preferences_RegisterDataObserver(pref, nullptr, DataChangeObserverCallback, keys, 3);
491fc223305Sopenharmony_ci    ASSERT_EQ(ret, PREFERENCES_OK);
492fc223305Sopenharmony_ci}
493fc223305Sopenharmony_ci
494fc223305Sopenharmony_cistatic void UnRegisterObsInFunc(OH_Preferences *pref)
495fc223305Sopenharmony_ci{
496fc223305Sopenharmony_ci    const char *keys[] = {"ndktest_int_key_1", "ndktest_string_key_1", "ndktest_bool_key_1"};
497fc223305Sopenharmony_ci
498fc223305Sopenharmony_ci    int ret = OH_Preferences_UnregisterDataObserver(pref, nullptr, DataChangeObserverCallback, keys, 3);
499fc223305Sopenharmony_ci    ASSERT_EQ(ret, PREFERENCES_OK);
500fc223305Sopenharmony_ci}
501fc223305Sopenharmony_ci
502fc223305Sopenharmony_ci/**
503fc223305Sopenharmony_ci * @tc.name: NDKPreferencesObserverTest_001
504fc223305Sopenharmony_ci * @tc.desc: test Observer
505fc223305Sopenharmony_ci * @tc.type: FUNC
506fc223305Sopenharmony_ci * @tc.require: NA
507fc223305Sopenharmony_ci * @tc.author: Liu Xiaolong
508fc223305Sopenharmony_ci */
509fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesObserverTest_001, TestSize.Level1)
510fc223305Sopenharmony_ci{
511fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
512fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
513fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
514fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
515fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
516fc223305Sopenharmony_ci
517fc223305Sopenharmony_ci    RegisterObsInFunc(pref);
518fc223305Sopenharmony_ci
519fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
520fc223305Sopenharmony_ci
521fc223305Sopenharmony_ci    for (auto &[key, value] : g_intDataMap) {
522fc223305Sopenharmony_ci        EXPECT_EQ(OH_Preferences_Delete(pref, key.c_str()), PREFERENCES_OK);
523fc223305Sopenharmony_ci        EXPECT_EQ(PreferencesFlush(pref), OHOS::NativePreferences::E_OK);
524fc223305Sopenharmony_ci    }
525fc223305Sopenharmony_ci
526fc223305Sopenharmony_ci    for (auto &[key, value] : g_stringDataMap) {
527fc223305Sopenharmony_ci        const char *newValue = "update_string_value_109uokadnf894u5";
528fc223305Sopenharmony_ci        EXPECT_EQ(OH_Preferences_SetString(pref, key.c_str(), newValue), PREFERENCES_OK);
529fc223305Sopenharmony_ci        EXPECT_EQ(PreferencesFlush(pref), OHOS::NativePreferences::E_OK);
530fc223305Sopenharmony_ci    }
531fc223305Sopenharmony_ci
532fc223305Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
533fc223305Sopenharmony_ci
534fc223305Sopenharmony_ci    EXPECT_EQ(g_changeNum, 5);
535fc223305Sopenharmony_ci
536fc223305Sopenharmony_ci    UnRegisterObsInFunc(pref);
537fc223305Sopenharmony_ci
538fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
539fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
540fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
541fc223305Sopenharmony_ci}
542fc223305Sopenharmony_ci
543fc223305Sopenharmony_ci/**
544fc223305Sopenharmony_ci * @tc.name: NDKPreferencesObserverTest_002
545fc223305Sopenharmony_ci * @tc.desc: test Observer
546fc223305Sopenharmony_ci * @tc.type: FUNC
547fc223305Sopenharmony_ci * @tc.require: NA
548fc223305Sopenharmony_ci * @tc.author: bluhuang
549fc223305Sopenharmony_ci */
550fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesObserverTest_002, TestSize.Level1)
551fc223305Sopenharmony_ci{
552fc223305Sopenharmony_ci    // cannot get callback when unregister
553fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
554fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
555fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
556fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
557fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
558fc223305Sopenharmony_ci
559fc223305Sopenharmony_ci    const char *keys[] = {"ndktest_int_key_1", "ndktest_string_key_1", "ndktest_bool_key_1"};
560fc223305Sopenharmony_ci    g_changeNum = 0;
561fc223305Sopenharmony_ci    int ret = OH_Preferences_RegisterDataObserver(pref, nullptr, DataChangeObserverCallback, keys, 3);
562fc223305Sopenharmony_ci    ASSERT_EQ(ret, PREFERENCES_OK);
563fc223305Sopenharmony_ci    const char *empty[] = {};
564fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_UnregisterDataObserver(pref, nullptr, DataChangeObserverCallback, empty, 0),
565fc223305Sopenharmony_ci        PREFERENCES_OK);
566fc223305Sopenharmony_ci
567fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
568fc223305Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
569fc223305Sopenharmony_ci    EXPECT_EQ(g_changeNum, 0);
570fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
571fc223305Sopenharmony_ci
572fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
573fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
574fc223305Sopenharmony_ci}
575fc223305Sopenharmony_ci
576fc223305Sopenharmony_ci/**
577fc223305Sopenharmony_ci * @tc.name: NDKPreferencesObserverTest_003
578fc223305Sopenharmony_ci * @tc.desc: test Observer
579fc223305Sopenharmony_ci * @tc.type: FUNC
580fc223305Sopenharmony_ci * @tc.require: NA
581fc223305Sopenharmony_ci * @tc.author: bluhuang
582fc223305Sopenharmony_ci */
583fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesObserverTest_003, TestSize.Level1)
584fc223305Sopenharmony_ci{
585fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
586fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
587fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
588fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
589fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
590fc223305Sopenharmony_ci
591fc223305Sopenharmony_ci    const char *keys[] = {"ndktest_int_key_1", "ndktest_string_key_1", "ndktest_bool_key_1"};
592fc223305Sopenharmony_ci    g_changeNum = 0;
593fc223305Sopenharmony_ci    ASSERT_EQ(OH_Preferences_RegisterDataObserver(nullptr, nullptr, DataChangeObserverCallback, keys, 3),
594fc223305Sopenharmony_ci        PREFERENCES_ERROR_INVALID_PARAM);
595fc223305Sopenharmony_ci    ASSERT_EQ(OH_Preferences_RegisterDataObserver(pref, nullptr, nullptr, keys, 3), PREFERENCES_ERROR_INVALID_PARAM);
596fc223305Sopenharmony_ci    ASSERT_EQ(OH_Preferences_RegisterDataObserver(pref, nullptr, DataChangeObserverCallback, nullptr, 3),
597fc223305Sopenharmony_ci        PREFERENCES_ERROR_INVALID_PARAM);
598fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_UnregisterDataObserver(nullptr, nullptr, DataChangeObserverCallback, keys, 0),
599fc223305Sopenharmony_ci        PREFERENCES_ERROR_INVALID_PARAM);
600fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_UnregisterDataObserver(pref, nullptr, nullptr, keys, 0),
601fc223305Sopenharmony_ci        PREFERENCES_ERROR_INVALID_PARAM);
602fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_UnregisterDataObserver(pref, nullptr, DataChangeObserverCallback, nullptr, 0),
603fc223305Sopenharmony_ci        PREFERENCES_ERROR_INVALID_PARAM);
604fc223305Sopenharmony_ci}
605fc223305Sopenharmony_ci
606fc223305Sopenharmony_ci/**
607fc223305Sopenharmony_ci * @tc.name: NDKPreferencesObserverTest_004
608fc223305Sopenharmony_ci * @tc.desc: test Observer
609fc223305Sopenharmony_ci * @tc.type: FUNC
610fc223305Sopenharmony_ci * @tc.require: NA
611fc223305Sopenharmony_ci * @tc.author: bluhuang
612fc223305Sopenharmony_ci */
613fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesObserverTest_004, TestSize.Level1)
614fc223305Sopenharmony_ci{
615fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
616fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
617fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
618fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
619fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
620fc223305Sopenharmony_ci
621fc223305Sopenharmony_ci    const char *keys1[] = {"ndktest_int_key_1", "ndktest_string_key_1", "ndktest_bool_key_1"};
622fc223305Sopenharmony_ci    g_changeNum = 0;
623fc223305Sopenharmony_ci    double obj = 1.1;
624fc223305Sopenharmony_ci    void *context1 = &obj;
625fc223305Sopenharmony_ci    ASSERT_EQ(OH_Preferences_RegisterDataObserver(pref, (void*) context1, DataChangeObserverCallback, keys1, 3),
626fc223305Sopenharmony_ci        PREFERENCES_OK);
627fc223305Sopenharmony_ci
628fc223305Sopenharmony_ci    const char *keys2[] = {"ndktest_int_key_5"};
629fc223305Sopenharmony_ci    double obj2 = 2.2;
630fc223305Sopenharmony_ci    void *context2 = &obj2;
631fc223305Sopenharmony_ci    ASSERT_EQ(OH_Preferences_RegisterDataObserver(pref, (void*) context2, DataChangeObserverCallback, keys2, 1),
632fc223305Sopenharmony_ci        PREFERENCES_OK);
633fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
634fc223305Sopenharmony_ci
635fc223305Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
636fc223305Sopenharmony_ci
637fc223305Sopenharmony_ci    EXPECT_EQ(g_changeNum, 4);
638fc223305Sopenharmony_ci
639fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_UnregisterDataObserver(pref, nullptr, DataChangeObserverCallback, keys1, 3),
640fc223305Sopenharmony_ci        PREFERENCES_OK);
641fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_UnregisterDataObserver(pref, nullptr, DataChangeObserverCallback, keys2, 1),
642fc223305Sopenharmony_ci        PREFERENCES_OK);
643fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
644fc223305Sopenharmony_ci
645fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
646fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
647fc223305Sopenharmony_ci}
648fc223305Sopenharmony_ci
649fc223305Sopenharmony_ci/**
650fc223305Sopenharmony_ci * @tc.name: NDKPreferencesObserverTest_005
651fc223305Sopenharmony_ci * @tc.desc: test Observer
652fc223305Sopenharmony_ci * @tc.type: FUNC
653fc223305Sopenharmony_ci * @tc.require: NA
654fc223305Sopenharmony_ci * @tc.author: bluhuang
655fc223305Sopenharmony_ci */
656fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesObserverTest_005, TestSize.Level1)
657fc223305Sopenharmony_ci{
658fc223305Sopenharmony_ci    // cancel part of all registerd key
659fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
660fc223305Sopenharmony_ci    OH_PreferencesOption *option = GetCommonOption();
661fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
662fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
663fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
664fc223305Sopenharmony_ci
665fc223305Sopenharmony_ci    const char *keys[] = {"ndktest_int_key_1", "ndktest_string_key_1", "ndktest_bool_key_1"};
666fc223305Sopenharmony_ci    g_changeNum = 0;
667fc223305Sopenharmony_ci    ASSERT_EQ(OH_Preferences_RegisterDataObserver(pref, nullptr, DataChangeObserverCallback, keys, 3),
668fc223305Sopenharmony_ci        PREFERENCES_OK);
669fc223305Sopenharmony_ci    const char *cancel[] = {"ndktest_bool_key_1"};
670fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_UnregisterDataObserver(pref, nullptr, DataChangeObserverCallback, cancel, 1),
671fc223305Sopenharmony_ci        PREFERENCES_OK);
672fc223305Sopenharmony_ci
673fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
674fc223305Sopenharmony_ci
675fc223305Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
676fc223305Sopenharmony_ci    EXPECT_EQ(g_changeNum, 2);
677fc223305Sopenharmony_ci
678fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
679fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
680fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
681fc223305Sopenharmony_ci}
682fc223305Sopenharmony_ci
683fc223305Sopenharmony_civoid NullTestCallback(void *context, const OH_PreferencesPair *pairs, uint32_t count)
684fc223305Sopenharmony_ci{
685fc223305Sopenharmony_ci    for (uint32_t i = 0; i < count; i++) {
686fc223305Sopenharmony_ci        EXPECT_EQ(OH_PreferencesPair_GetPreferencesValue(nullptr, INVALID_INDEX), nullptr);
687fc223305Sopenharmony_ci        EXPECT_EQ(OH_PreferencesPair_GetPreferencesValue(pairs, INVALID_INDEX), nullptr);
688fc223305Sopenharmony_ci
689fc223305Sopenharmony_ci        EXPECT_EQ(OH_PreferencesValue_GetValueType(nullptr), Preference_ValueType::PREFERENCE_TYPE_NULL);
690fc223305Sopenharmony_ci
691fc223305Sopenharmony_ci        EXPECT_EQ(OH_PreferencesPair_GetKey(nullptr, INVALID_INDEX), nullptr);
692fc223305Sopenharmony_ci        EXPECT_EQ(OH_PreferencesPair_GetKey(pairs, INVALID_INDEX), nullptr);
693fc223305Sopenharmony_ci
694fc223305Sopenharmony_ci        const OH_PreferencesValue *pValue = OH_PreferencesPair_GetPreferencesValue(pairs, i);
695fc223305Sopenharmony_ci        Preference_ValueType type = OH_PreferencesValue_GetValueType(pValue);
696fc223305Sopenharmony_ci        const char *pKey = OH_PreferencesPair_GetKey(pairs, i);
697fc223305Sopenharmony_ci        EXPECT_NE(pKey, nullptr);
698fc223305Sopenharmony_ci        if (type == Preference_ValueType::PREFERENCE_TYPE_INT) {
699fc223305Sopenharmony_ci            EXPECT_EQ(OH_PreferencesValue_GetInt(nullptr, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
700fc223305Sopenharmony_ci            EXPECT_EQ(OH_PreferencesValue_GetInt(pValue, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
701fc223305Sopenharmony_ci
702fc223305Sopenharmony_ci            bool boolV = false;
703fc223305Sopenharmony_ci            EXPECT_EQ(OH_PreferencesValue_GetBool(pValue, &boolV), PREFERENCES_ERROR_KEY_NOT_FOUND);
704fc223305Sopenharmony_ci        } else if (type == Preference_ValueType::PREFERENCE_TYPE_BOOL) {
705fc223305Sopenharmony_ci            EXPECT_EQ(OH_PreferencesValue_GetBool(nullptr, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
706fc223305Sopenharmony_ci            EXPECT_EQ(OH_PreferencesValue_GetBool(pValue, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
707fc223305Sopenharmony_ci
708fc223305Sopenharmony_ci            char *stringV = nullptr;
709fc223305Sopenharmony_ci            uint32_t len = 0;
710fc223305Sopenharmony_ci            EXPECT_EQ(OH_PreferencesValue_GetString(pValue, &stringV, &len), PREFERENCES_ERROR_KEY_NOT_FOUND);
711fc223305Sopenharmony_ci        } else if (type == Preference_ValueType::PREFERENCE_TYPE_STRING) {
712fc223305Sopenharmony_ci            char *stringV = nullptr;
713fc223305Sopenharmony_ci            EXPECT_EQ(OH_PreferencesValue_GetString(nullptr, nullptr, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
714fc223305Sopenharmony_ci            EXPECT_EQ(OH_PreferencesValue_GetString(pValue, nullptr, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
715fc223305Sopenharmony_ci            EXPECT_EQ(OH_PreferencesValue_GetString(pValue, &stringV, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
716fc223305Sopenharmony_ci
717fc223305Sopenharmony_ci            int intV = 0;
718fc223305Sopenharmony_ci            EXPECT_EQ(OH_PreferencesValue_GetInt(pValue, &intV), PREFERENCES_ERROR_KEY_NOT_FOUND);
719fc223305Sopenharmony_ci        }
720fc223305Sopenharmony_ci    }
721fc223305Sopenharmony_ci}
722fc223305Sopenharmony_ci
723fc223305Sopenharmony_ci/**
724fc223305Sopenharmony_ci * @tc.name: NDKPreferencesNullInputTest_001
725fc223305Sopenharmony_ci * @tc.desc: test NULL Input
726fc223305Sopenharmony_ci * @tc.type: FUNC
727fc223305Sopenharmony_ci * @tc.require: NA
728fc223305Sopenharmony_ci * @tc.author: Liu Xiaolong
729fc223305Sopenharmony_ci */
730fc223305Sopenharmony_ciHWTEST_F(PreferencesNdkTest, NDKPreferencesNullInputTest_001, TestSize.Level1)
731fc223305Sopenharmony_ci{
732fc223305Sopenharmony_ci    int errCode = PREFERENCES_OK;
733fc223305Sopenharmony_ci    OH_PreferencesOption *option = OH_PreferencesOption_Create();
734fc223305Sopenharmony_ci
735fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetFileName(nullptr, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
736fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetFileName(option, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
737fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetFileName(option, ""), PREFERENCES_ERROR_INVALID_PARAM);
738fc223305Sopenharmony_ci
739fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetBundleName(nullptr, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
740fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetBundleName(option, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
741fc223305Sopenharmony_ci
742fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetDataGroupId(nullptr, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
743fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetDataGroupId(option, nullptr), PREFERENCES_ERROR_INVALID_PARAM);
744fc223305Sopenharmony_ci
745fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_Destroy(nullptr), PREFERENCES_ERROR_INVALID_PARAM);
746fc223305Sopenharmony_ci
747fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetFileName(option, "testdb"), PREFERENCES_OK);
748fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetBundleName(option, "com.uttest"), PREFERENCES_OK);
749fc223305Sopenharmony_ci    EXPECT_EQ(OH_PreferencesOption_SetDataGroupId(option, "123"), PREFERENCES_OK);
750fc223305Sopenharmony_ci    OH_Preferences *pref = OH_Preferences_Open(option, &errCode);
751fc223305Sopenharmony_ci    (void)OH_PreferencesOption_Destroy(option);
752fc223305Sopenharmony_ci    ASSERT_EQ(errCode, PREFERENCES_OK);
753fc223305Sopenharmony_ci
754fc223305Sopenharmony_ci    const char *keys[] = {"ndktest_int_key_1", "ndktest_string_key_1", "ndktest_bool_key_1"};
755fc223305Sopenharmony_ci    ASSERT_EQ(OH_Preferences_RegisterDataObserver(pref, nullptr, NullTestCallback, keys, 3), PREFERENCES_OK);
756fc223305Sopenharmony_ci
757fc223305Sopenharmony_ci    SetAllValuesWithCheck(pref);
758fc223305Sopenharmony_ci
759fc223305Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
760fc223305Sopenharmony_ci
761fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_UnregisterDataObserver(pref, nullptr, NullTestCallback, keys, 3), PREFERENCES_OK);
762fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(pref), PREFERENCES_OK);
763fc223305Sopenharmony_ci
764fc223305Sopenharmony_ci    EXPECT_EQ(OH_Preferences_Close(nullptr), PREFERENCES_ERROR_INVALID_PARAM);
765fc223305Sopenharmony_ci
766fc223305Sopenharmony_ci    EXPECT_EQ(OHOS::NativePreferences::PreferencesHelper::DeletePreferences("/data/test/testdb"),
767fc223305Sopenharmony_ci        OHOS::NativePreferences::E_OK);
768fc223305Sopenharmony_ci}
769fc223305Sopenharmony_ci}
770