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: test RegisterObserverExtProvider default func
92 * @tc.type: FUNC
93 */
94 HWTEST_F(DataShareProxyTest, DataShareProxy_RegisterObserverExtProvider_Test_001, TestSize.Level0)
95 {
96 LOG_INFO("DataShareProxy_RegisterObserverExtProvider_Test_001::Start");
97
98 Uri uri(DATA_SHARE_URI);
99 std::u16string tokenString = u"OHOS.DataShare.IDataShare";
100 sptr<IRemoteObject> token = new (std::nothrow) RemoteObjectTest(tokenString);
101 ASSERT_NE(token, nullptr);
102 sptr<DataShare::DataShareConnection> connection =
103 new (std::nothrow) DataShare::DataShareConnection(uri, token);
104 ASSERT_NE(connection, nullptr);
105
106 // get proxy not null
107 std::shared_ptr<DataShareProxy> tokenProxy = std::make_shared<DataShareProxy>(token);
108 ASSERT_NE(tokenProxy, nullptr);
109
110 connection->dataShareProxy_ = tokenProxy;
111 ASSERT_NE(connection->dataShareProxy_, nullptr);
112 auto proxy = connection->GetDataShareProxy(uri, token);
113 ASSERT_NE(proxy, nullptr);
114
115 // Observer not null
116 sptr<IDataAbilityObserverTest> dataObserver = new (std::nothrow) IDataAbilityObserverTest();
117 ASSERT_NE(dataObserver, nullptr);
118 // datashare_stub returns default 0(E_OK)
119 int ret = proxy->RegisterObserverExtProvider(uri, dataObserver, true, { false });
120 EXPECT_EQ(ret, 0);
121
122 LOG_INFO("DataShareProxy_RegisterObserverExtProvider_Test_001::End");
123 }
124
125 /**
126 * @tc.name: DataShareProxy_UnregisterObserverExtProvider_Test_001
127 * @tc.desc: test UnregisterObserverExtProvider default func
128 * @tc.type: FUNC
129 */
130 HWTEST_F(DataShareProxyTest, DataShareProxy_UnregisterObserverExtProvider_Test_001, TestSize.Level0)
131 {
132 LOG_INFO("DataShareProxy_UnregisterObserverExtProvider_Test_001::Start");
133
134 Uri uri(DATA_SHARE_URI);
135 std::u16string tokenString = u"OHOS.DataShare.IDataShare";
136 sptr<IRemoteObject> token = new (std::nothrow) RemoteObjectTest(tokenString);
137 ASSERT_NE(token, nullptr);
138 sptr<DataShare::DataShareConnection> connection =
139 new (std::nothrow) DataShare::DataShareConnection(uri, token);
140 ASSERT_NE(connection, nullptr);
141
142 // get proxy not null
143 std::shared_ptr<DataShareProxy> tokenProxy = std::make_shared<DataShareProxy>(token);
144 ASSERT_NE(tokenProxy, nullptr);
145 connection->dataShareProxy_ = tokenProxy;
146 auto proxy = connection->GetDataShareProxy(uri, token);
147 ASSERT_NE(proxy, nullptr);
148
149 // observer not null
150 sptr<IDataAbilityObserverTest> dataObserver = new (std::nothrow) IDataAbilityObserverTest();
151 ASSERT_NE(dataObserver, nullptr);
152 // datashare_stub returns default 0(E_OK)
153 int ret = proxy->UnregisterObserverExtProvider(uri, dataObserver);
154 EXPECT_EQ(ret, 0);
155
156 LOG_INFO("DataShareProxy_UnregisterObserverExtProvider_Test_001::End");
157 }
158
159 /**
160 * @tc.name: DataShareProxy_NotifyChangeExtProvider_Test_001
161 * @tc.desc: test NotifyChangeExtProvider default func
162 * @tc.type: FUNC
163 */
164 HWTEST_F(DataShareProxyTest, DataShareProxy_NotifyChangeExtProvider_Test_001, TestSize.Level0)
165 {
166 LOG_INFO("DataShareProxy_NotifyChangeExtProvider_Test_001::Start");
167
168 Uri uri(DATA_SHARE_URI);
169 std::u16string tokenString = u"OHOS.DataShare.IDataShare";
170 sptr<IRemoteObject> token = new (std::nothrow) RemoteObjectTest(tokenString);
171 ASSERT_NE(token, nullptr);
172 sptr<DataShare::DataShareConnection> connection =
173 new (std::nothrow) DataShare::DataShareConnection(uri, token);
174 ASSERT_NE(connection, nullptr);
175
176 // get proxy not null
177 std::shared_ptr<DataShareProxy> tokenProxy = std::make_shared<DataShareProxy>(token);
178 ASSERT_NE(tokenProxy, nullptr);
179 connection->dataShareProxy_ = tokenProxy;
180 auto proxy = connection->GetDataShareProxy(uri, token);
181 ASSERT_NE(proxy, nullptr);
182
183 ChangeInfo changeInfo = { ChangeInfo::ChangeType::INSERT, { uri } };
184 // datashare_stub returns default 0(E_OK)
185 int ret = proxy->NotifyChangeExtProvider(changeInfo);
186 EXPECT_EQ(ret, 0);
187
188 LOG_INFO("DataShareProxy_NotifyChangeExtProvider_Test_001::End");
189 }
190 }
191 }