• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 iSoftStone Information Technology (Group) 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 "gtest/gtest.h"
17 #include "mock_storage.h"
18 
19 #include "base/utils/utils.h"
20 #include "core/common/storage/storage_proxy.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS::Ace {
26 class StorageTest : public testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase() {}
TearDownTestCase()29     static void TearDownTestCase() {}
SetUp()30     void SetUp() {}
TearDown()31     void TearDown() {}
32 };
33 
34 /**
35  * @tc.name: CastToStorageTest001
36  * @tc.desc: Test cast to storage.
37  * @tc.type: FUNC
38  */
39 HWTEST_F(StorageTest, CastToStorageTest001, TestSize.Level1)
40 {
41     /**
42      * @tc.steps: step1. call the GetInstance first.
43      * @tc.expected: step1. the return value is null.
44      */
45     StorageProxy* firstTestGetInstance;
46     firstTestGetInstance = StorageProxy::GetInstance();
47     EXPECT_NE(firstTestGetInstance, nullptr);
48 
49     /**
50      * @tc.steps: step2. call the GetInstance second.
51      * @tc.expected: step2. the return value is the same as first.
52      */
53     StorageProxy* secondTestGetInstance;
54     secondTestGetInstance = StorageProxy::GetInstance();
55     EXPECT_EQ(secondTestGetInstance, firstTestGetInstance);
56 }
57 
58 /**
59  * @tc.name: CastToStorageTest002
60  * @tc.desc: Test cast to storage.
61  * @tc.type: FUNC
62  */
63 HWTEST_F(StorageTest, CastToStorageTest002, TestSize.Level1)
64 {
65     /**
66      * @tc.steps: step1. call the SetDelegate with null.
67      * @tc.steps: step2. call the GetStorage and take the return value with delegate.
68      * @tc.expected: step2. the return stroage is null.
69      */
70     StorageProxy::GetInstance()->SetDelegate(nullptr);
71 
72     RefPtr<Storage> stroage = StorageProxy::GetInstance()->GetStorage();
73 
74     EXPECT_EQ(stroage, nullptr);
75 }
76 
77 /**
78  * @tc.name: CastToStorageTest003
79  * @tc.desc: Test cast to storage.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(StorageTest, CastToStorageTest003, TestSize.Level1)
83 {
84     /**
85      * @tc.steps: step1. call the SetDistributedDelegate with null.
86      * @tc.steps: step2. call the GetStorage and take the return value with delegate.
87      * @tc.expected: step2. the return stroage is null.
88      */
89     StorageProxy::GetInstance()->SetDistributedDelegate(nullptr);
90 
__anon68282c200102(const std::string& onlineStatus) 91     auto notifier = [](const std::string& onlineStatus) { return; };
92     RefPtr<TaskExecutor> taskExecutor;
93     std::string testSessionId = "testGetStorage";
94     RefPtr<Storage> distributedStorage = StorageProxy::GetInstance()->GetStorage(testSessionId, notifier, taskExecutor);
95 
96     EXPECT_EQ(distributedStorage, nullptr);
97 }
98 
99 /**
100  * @tc.name: CastToStorageTest004
101  * @tc.desc: Test cast to storage.
102  * @tc.type: FUNC
103  */
104 HWTEST_F(StorageTest, CastToStorageTest004, TestSize.Level1)
105 {
106     /**
107      * @tc.steps: step1. call the SetDelegate with not null.
108      * @tc.steps: step2. call the GetStorage and take the return value with delegate.
109      * @tc.expected: step2. the return storage is not null, and the GetString function is ok.
110      */
111     StorageProxy::GetInstance()->SetDelegate(std::make_unique<MockStorageProxyImpl>());
112 
113     RefPtr<Storage> stroage = StorageProxy::GetInstance()->GetStorage();
114     EXPECT_NE(stroage, nullptr);
115 
116     std::string testString = stroage->GetString("test");
117     EXPECT_EQ(testString, FALSE_TEST);
118 }
119 
120 /**
121  * @tc.name: CastToStorageTest005
122  * @tc.desc: Test cast to storage.
123  * @tc.type: FUNC
124  */
125 HWTEST_F(StorageTest, CastToStorageTest005, TestSize.Level1)
126 {
127     /**
128      * @tc.steps: step1. call the SetDistributedDelegate with not null.
129      * @tc.steps: step2. call the GetStorage and take the return value with delegate.
130      * @tc.expected: step2. the return storage is not null, and the GetString function is ok.
131      */
132     StorageProxy::GetInstance()->SetDistributedDelegate(std::make_unique<MockDistributedStorageProxyImpl>());
133 
__anon68282c200202(const std::string& onlineStatus) 134     auto notifier = [](const std::string& onlineStatus) { return; };
135     RefPtr<TaskExecutor> taskExecutor;
136     std::string testSessionId = "testGetStorage";
137     RefPtr<Storage> distributedStorage = StorageProxy::GetInstance()->GetStorage(testSessionId, notifier, taskExecutor);
138     EXPECT_NE(distributedStorage, nullptr);
139 
140     std::string testString = distributedStorage->GetString("test");
141     EXPECT_EQ(testString, FALSE_TEST);
142 }
143 } // namespace OHOS::Ace
144