• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "preferences_helper.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include <cctype>
21 #include <iostream>
22 #include <string>
23 
24 #include "logger.h"
25 #include "preferences.h"
26 #include "preferences_errno.h"
27 #include "preferences_observer.h"
28 
29 using namespace testing::ext;
30 using namespace OHOS::NativePreferences;
31 
32 class PreferencesHelperTest : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     void SetUp();
37     void TearDown();
38 };
39 
SetUpTestCase(void)40 void PreferencesHelperTest::SetUpTestCase(void)
41 {
42 }
43 
TearDownTestCase(void)44 void PreferencesHelperTest::TearDownTestCase(void)
45 {
46 }
47 
SetUp(void)48 void PreferencesHelperTest::SetUp(void)
49 {
50 }
51 
TearDown(void)52 void PreferencesHelperTest::TearDown(void)
53 {
54 }
55 
56 /**
57  * @tc.name: NativePreferencesHelperTest_001
58  * @tc.desc: normal testcase of DeletePreferences
59  * @tc.type: FUNC
60  * @tc.require: SR000CU2BL
61  * @tc.author: xiuhongju
62  */
63 HWTEST_F(PreferencesHelperTest, NativePreferencesHelperTest_001, TestSize.Level1)
64 {
65     int errCode = E_OK;
66     std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences("/data/test/test_helper", errCode);
67     EXPECT_EQ(errCode, E_OK);
68 
69     pref->PutInt("key1", 2);
70     pref->PutString("key2", "test");
71     int ret = pref->FlushSync();
72     EXPECT_EQ(ret, E_OK);
73 
74     pref = nullptr;
75 
76     ret = PreferencesHelper::RemovePreferencesFromCache("/data/test/test_helper");
77     EXPECT_EQ(ret, E_OK);
78 
79     ret = PreferencesHelper::DeletePreferences("/data/test/test_helper");
80     EXPECT_EQ(ret, E_OK);
81 }
82 
83 /**
84  * @tc.name: NativePreferencesHelperTest_002
85  * @tc.desc: error testcase of Preferences
86  * @tc.type: FUNC
87  */
88 HWTEST_F(PreferencesHelperTest, NativePreferencesHelperTest_002, TestSize.Level1)
89 {
90     int errCode = E_OK;
91     std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences("", errCode);
92     EXPECT_EQ(errCode, E_EMPTY_FILE_PATH);
93 
94     pref = PreferencesHelper::GetPreferences("data/test/test_helper", errCode);
95     EXPECT_EQ(errCode, E_RELATIVE_PATH);
96 
97     pref = PreferencesHelper::GetPreferences(":data/test/test_helper", errCode);
98     EXPECT_EQ(errCode, E_RELATIVE_PATH);
99 
100     std::string path = "/data/test/test_helper" + std::string(4096, 't');
101     pref = PreferencesHelper::GetPreferences(path, errCode);
102     EXPECT_EQ(errCode, E_PATH_EXCEED_MAX_LENGTH);
103 
104     pref = PreferencesHelper::GetPreferences(":data/test/test_helper/", errCode);
105     EXPECT_EQ(errCode, E_RELATIVE_PATH);
106 }
107 
108 /**
109  * @tc.name: NativePreferencesHelperTest_003
110  * @tc.desc: error testcase of Preferences
111  * @tc.type: FUNC
112  */
113 HWTEST_F(PreferencesHelperTest, NativePreferencesHelperTest_003, TestSize.Level1)
114 {
115     int errCode = E_OK;
116     std::shared_ptr<Preferences> pref = PreferencesHelper::GetPreferences("", errCode);
117     EXPECT_EQ(errCode, E_EMPTY_FILE_PATH);
118 
119     pref = PreferencesHelper::GetPreferences("data/test/test_helper", errCode);
120     EXPECT_EQ(errCode, E_RELATIVE_PATH);
121 }
122 
123 /**
124  * @tc.name: NativePreferencesHelperTest_004
125  * @tc.desc: error testcase of DeletePreferences
126  * @tc.type: FUNC
127  */
128 HWTEST_F(PreferencesHelperTest, NativePreferencesHelperTest_004, TestSize.Level1)
129 {
130     int pref = PreferencesHelper::DeletePreferences("");
131     EXPECT_EQ(pref, E_EMPTY_FILE_PATH);
132 
133     pref = PreferencesHelper::RemovePreferencesFromCache("");
134     EXPECT_EQ(pref, E_EMPTY_FILE_PATH);
135 }