• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "datashare_proxy.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include "accesstoken_kit.h"
21 #include "data_ability_observer_interface.h"
22 #include "datashare_connection.h"
23 #include "datashare_errno.h"
24 #include "datashare_helper.h"
25 #include "datashare_log.h"
26 #include "extension_manager_proxy.h"
27 #include "general_controller.h"
28 #include "general_controller_provider_impl.h"
29 #include "general_controller_service_impl.h"
30 
31 namespace OHOS {
32 namespace DataShare {
33 using namespace testing::ext;
34 using namespace OHOS::AAFwk;
35 class DataShareProxyTest : public testing::Test {
36 public:
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
41 };
42 
43 class RemoteObjectTest : public IRemoteObject {
44 public:
RemoteObjectTest(std::u16string descriptor)45     explicit RemoteObjectTest(std::u16string descriptor) : IRemoteObject(descriptor) {}
~RemoteObjectTest()46     ~RemoteObjectTest() {}
47 
GetObjectRefCount()48     int32_t GetObjectRefCount()
49     {
50         return 0;
51     }
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)52     int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
53     {
54         return 0;
55     }
AddDeathRecipient(const sptr<DeathRecipient> & recipient)56     bool AddDeathRecipient(const sptr<DeathRecipient> &recipient)
57     {
58         return true;
59     }
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)60     bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient)
61     {
62         return true;
63     }
Dump(int fd,const std::vector<std::u16string> & args)64     int Dump(int fd, const std::vector<std::u16string> &args)
65     {
66         return 0;
67     }
68 };
69 
70 class IDataAbilityObserverTest : public DataAbilityObserverStub {
71 public:
IDataAbilityObserverTest()72     IDataAbilityObserverTest() {}
~IDataAbilityObserverTest()73     ~IDataAbilityObserverTest()
74     {}
75 
OnChange()76     void OnChange()
77     {
78         GTEST_LOG_(INFO) << "OnChange enter";
79     }
80 };
81 
82 std::string DATA_SHARE_URI = "datashare:///com.acts.datasharetest";
83 
SetUpTestCase(void)84 void DataShareProxyTest::SetUpTestCase(void) {}
TearDownTestCase(void)85 void DataShareProxyTest::TearDownTestCase(void) {}
SetUp(void)86 void DataShareProxyTest::SetUp(void) {}
TearDown(void)87 void DataShareProxyTest::TearDown(void) {}
88 
89 /**
90 * @tc.name: DataShareProxy_RegisterObserverExtProvider_Test_001
91 * @tc.desc: Verify basic functionality of RegisterObserverExtProvider method
92 * @tc.type: FUNC
93 * @tc.precon: None
94 * @tc.step:
95     1. Create DataShareConnection with test URI and token
96     2. Initialize DataShareProxy and assign to connection
97     3. Get proxy instance and verify it's not null
98     4. Create test observer and call RegisterObserverExtProvider
99     5. Check returned result code
100 * @tc.expect:
101     1. All objects are successfully initialized
102     2. RegisterObserverExtProvider returns 0 (E_OK)
103 */
104 HWTEST_F(DataShareProxyTest, DataShareProxy_RegisterObserverExtProvider_Test_001, TestSize.Level0)
105 {
106     LOG_INFO("DataShareProxy_RegisterObserverExtProvider_Test_001::Start");
107 
108     Uri uri(DATA_SHARE_URI);
109     std::u16string tokenString = u"OHOS.DataShare.IDataShare";
110     sptr<IRemoteObject> token = new (std::nothrow) RemoteObjectTest(tokenString);
111     ASSERT_NE(token, nullptr);
112     sptr<DataShare::DataShareConnection> connection =
113         new (std::nothrow) DataShare::DataShareConnection(uri, token);
114     ASSERT_NE(connection, nullptr);
115 
116     // get proxy not null
117     std::shared_ptr<DataShareProxy> tokenProxy = std::make_shared<DataShareProxy>(token);
118     ASSERT_NE(tokenProxy, nullptr);
119 
120     connection->dataShareProxy_ = tokenProxy;
121     ASSERT_NE(connection->dataShareProxy_, nullptr);
122     auto proxy = connection->GetDataShareProxy(uri, token);
123     ASSERT_NE(proxy, nullptr);
124 
125     // Observer not null
126     sptr<IDataAbilityObserverTest> dataObserver = new (std::nothrow) IDataAbilityObserverTest();
127     ASSERT_NE(dataObserver, nullptr);
128     // datashare_stub returns default 0(E_OK)
129     int ret = proxy->RegisterObserverExtProvider(uri, dataObserver, true, { false });
130     EXPECT_EQ(ret, 0);
131 
132     LOG_INFO("DataShareProxy_RegisterObserverExtProvider_Test_001::End");
133 }
134 
135 /**
136 * @tc.name: DataShareProxy_UnregisterObserverExtProvider_Test_001
137 * @tc.desc: Verify basic functionality of UnregisterObserverExtProvider method
138 * @tc.type: FUNC
139 * @tc.precon: None
140 * @tc.step:
141     1. Create DataShareConnection with test URI and token
142     2. Initialize DataShareProxy and assign to connection
143     3. Get proxy instance and verify it's not null
144     4. Create test observer and call UnregisterObserverExtProvider
145     5. Check returned result code
146 * @tc.expect:
147     1. All objects are successfully initialized
148     2. UnregisterObserverExtProvider returns 0 (E_OK)
149 */
150 HWTEST_F(DataShareProxyTest, DataShareProxy_UnregisterObserverExtProvider_Test_001, TestSize.Level0)
151 {
152     LOG_INFO("DataShareProxy_UnregisterObserverExtProvider_Test_001::Start");
153 
154     Uri uri(DATA_SHARE_URI);
155     std::u16string tokenString = u"OHOS.DataShare.IDataShare";
156     sptr<IRemoteObject> token = new (std::nothrow) RemoteObjectTest(tokenString);
157     ASSERT_NE(token, nullptr);
158     sptr<DataShare::DataShareConnection> connection =
159         new (std::nothrow) DataShare::DataShareConnection(uri, token);
160     ASSERT_NE(connection, nullptr);
161 
162     // get proxy not null
163     std::shared_ptr<DataShareProxy> tokenProxy = std::make_shared<DataShareProxy>(token);
164     ASSERT_NE(tokenProxy, nullptr);
165     connection->dataShareProxy_ = tokenProxy;
166     auto proxy = connection->GetDataShareProxy(uri, token);
167     ASSERT_NE(proxy, nullptr);
168 
169     // observer  not null
170     sptr<IDataAbilityObserverTest> dataObserver = new (std::nothrow) IDataAbilityObserverTest();
171     ASSERT_NE(dataObserver, nullptr);
172     // datashare_stub returns default 0(E_OK)
173     int ret = proxy->UnregisterObserverExtProvider(uri, dataObserver);
174     EXPECT_EQ(ret, 0);
175 
176     LOG_INFO("DataShareProxy_UnregisterObserverExtProvider_Test_001::End");
177 }
178 
179 /**
180 * @tc.name: DataShareProxy_NotifyChangeExtProvider_Test_001
181 * @tc.desc: Verify basic functionality of NotifyChangeExtProvider method
182 * @tc.type: FUNC
183 * @tc.precon: None
184 * @tc.step:
185     1. Create DataShareConnection with test URI and token
186     2. Initialize DataShareProxy and assign to connection
187     3. Get proxy instance and verify it's not null
188     4. Create test ChangeInfo object and call NotifyChangeExtProvider
189     5. Check returned result code
190 * @tc.expect:
191     1. All objects are successfully initialized
192     2. NotifyChangeExtProvider returns 0 (E_OK)
193 */
194 HWTEST_F(DataShareProxyTest, DataShareProxy_NotifyChangeExtProvider_Test_001, TestSize.Level0)
195 {
196     LOG_INFO("DataShareProxy_NotifyChangeExtProvider_Test_001::Start");
197 
198     Uri uri(DATA_SHARE_URI);
199     std::u16string tokenString = u"OHOS.DataShare.IDataShare";
200     sptr<IRemoteObject> token = new (std::nothrow) RemoteObjectTest(tokenString);
201     ASSERT_NE(token, nullptr);
202     sptr<DataShare::DataShareConnection> connection =
203         new (std::nothrow) DataShare::DataShareConnection(uri, token);
204     ASSERT_NE(connection, nullptr);
205 
206     // get proxy not null
207     std::shared_ptr<DataShareProxy> tokenProxy = std::make_shared<DataShareProxy>(token);
208     ASSERT_NE(tokenProxy, nullptr);
209     connection->dataShareProxy_ = tokenProxy;
210     auto proxy = connection->GetDataShareProxy(uri, token);
211     ASSERT_NE(proxy, nullptr);
212 
213     ChangeInfo changeInfo = { ChangeInfo::ChangeType::INSERT, { uri } };
214     // datashare_stub returns default 0(E_OK)
215     int ret = proxy->NotifyChangeExtProvider(changeInfo);
216     EXPECT_EQ(ret, 0);
217 
218     LOG_INFO("DataShareProxy_NotifyChangeExtProvider_Test_001::End");
219 }
220 }
221 }