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 "gtest/gtest.h"
17
18 #define private public
19
20 #include "ans_inner_errors.h"
21 #include "distributed_data_define.h"
22 #include "distributed_device_data_service.h"
23
24 namespace OHOS {
25 namespace Notification {
26
27 using namespace testing::ext;
28
29 namespace {
30 const std::string DEVICE_ID = "abcd";
31 const std::string DEVICE_TYPE = "pc";
32 }
33 class DistributedDeviceDataServiceTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
SetUp()37 void SetUp() {};
TearDown()38 void TearDown() {};
39 };
40
SetUpTestCase()41 void DistributedDeviceDataServiceTest::SetUpTestCase()
42 {
43 GTEST_LOG_(INFO) << "SetUp Case start";
44 DistributedDeviceDataService::GetInstance().ResetTargetDevice(DEVICE_TYPE, DEVICE_ID);
45 GTEST_LOG_(INFO) << "SetUp end";
46 }
47
TearDownTestCase()48 void DistributedDeviceDataServiceTest::TearDownTestCase()
49 {
50 GTEST_LOG_(INFO) << "TearDown case";
51 }
52
53 /**
54 * @tc.name: Device sync switch check
55 * @tc.desc: Test device data service
56 * @tc.type: FUNC
57 * @tc.require: issue
58 */
59 HWTEST_F(DistributedDeviceDataServiceTest, DeviceData_00001, Function | SmallTest | Level1)
60 {
61 // add device sync switch data failed, because deviceType or deviceId is empty.
62 int32_t result = DistributedDeviceDataService::GetInstance().SetDeviceSyncSwitch("", "", true, true);
63 ASSERT_EQ(result, (int)ERR_ANS_INVALID_PARAM);
64 result = DistributedDeviceDataService::GetInstance().SetDeviceSyncSwitch(DEVICE_TYPE, "", true, true);
65 ASSERT_EQ(result, (int)ERR_ANS_INVALID_PARAM);
66
67 // add device sync switch data
68 result = DistributedDeviceDataService::GetInstance().SetDeviceSyncSwitch(DEVICE_TYPE,
69 DEVICE_ID, true, true);
70 ASSERT_EQ(result, (int)ERR_OK);
71
72 bool liveView = DistributedDeviceDataService::GetInstance().GetDeviceLiveViewEnable(DEVICE_TYPE, DEVICE_ID);
73 ASSERT_EQ(liveView, true);
74
75 liveView = DistributedDeviceDataService::GetInstance().GetDeviceLiveViewEnable("", DEVICE_ID);
76 ASSERT_EQ(liveView, false);
77 liveView = DistributedDeviceDataService::GetInstance().GetDeviceLiveViewEnable(DEVICE_TYPE, "");
78 ASSERT_EQ(liveView, false);
79
80 bool notification = DistributedDeviceDataService::GetInstance().GetDeviceNotificationEnable(DEVICE_TYPE, DEVICE_ID);
81 ASSERT_EQ(true, notification);
82
83 notification = DistributedDeviceDataService::GetInstance().GetDeviceNotificationEnable("", DEVICE_ID);
84 ASSERT_EQ(false, notification);
85 notification = DistributedDeviceDataService::GetInstance().GetDeviceNotificationEnable(DEVICE_TYPE, "");
86 ASSERT_EQ(false, notification);
87
88 // change device sync switch data
89 result = DistributedDeviceDataService::GetInstance().SetDeviceSyncSwitch(DEVICE_TYPE,
90 DEVICE_ID, true, false);
91
92 notification = DistributedDeviceDataService::GetInstance().GetDeviceLiveViewEnable(DEVICE_TYPE, DEVICE_ID);
93 ASSERT_EQ(false, notification);
94 result = DistributedDeviceDataService::GetInstance().SetDeviceSyncSwitch("", DEVICE_ID, true, true);
95 ASSERT_EQ(result, (int)ERR_ANS_INVALID_PARAM);
96 result = DistributedDeviceDataService::GetInstance().SetDeviceSyncSwitch(DEVICE_TYPE, "", true, true);
97 ASSERT_EQ(result, (int)ERR_ANS_INVALID_PARAM);
98 // clear data
99 DistributedDeviceDataService::GetInstance().ResetTargetDevice("", DEVICE_ID);
100 DistributedDeviceDataService::GetInstance().ResetTargetDevice(DEVICE_TYPE, "");
101 DistributedDeviceDataService::GetInstance().ResetTargetDevice(DEVICE_TYPE, DEVICE_ID);
102 }
103
104 /**
105 * @tc.name: Device sync installed bundle check
106 * @tc.desc: Test device data service
107 * @tc.type: FUNC
108 * @tc.require: issue
109 */
110 HWTEST_F(DistributedDeviceDataServiceTest, DeviceData_00002, Function | SmallTest | Level1)
111 {
112 // clear data
113 DistributedDeviceDataService::GetInstance().ResetTargetDevice(DEVICE_TYPE, DEVICE_ID);
114 StringAnonymous(DEVICE_ID);
115 // add device installed bundles failed, because deviceType or deviceId is empty.
116 int32_t result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList("",
117 DEVICE_ID, BundleListOperationType::ADD_BUNDLES, {}, {});
118 ASSERT_EQ(result, (int)ERR_ANS_INVALID_PARAM);
119 result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(DEVICE_TYPE,
120 "", BundleListOperationType::ADD_BUNDLES, {}, {});
121 ASSERT_EQ(result, (int)ERR_ANS_INVALID_PARAM);
122 result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(DEVICE_TYPE,
123 DEVICE_ID, BundleListOperationType::REMOVE_BUNDLES, {}, {});
124 ASSERT_EQ(result, (int)ERR_ANS_INVALID_PARAM);
125
126 // add device installed bundles
127 std::vector<std::string> bundleList = { "ohos.com.test1", "ohos.com.test2" };
128 std::vector<std::string> labelList = { "test1", "test2" };
129 result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(DEVICE_TYPE,
130 DEVICE_ID, BundleListOperationType::ADD_BUNDLES, bundleList, labelList);
131 ASSERT_EQ(result, (int)ERR_OK);
132 result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(DEVICE_TYPE,
133 DEVICE_ID, BundleListOperationType::ADD_BUNDLES, { "ohos.com.test0" }, { "test0" });
134 ASSERT_EQ(result, (int)ERR_OK);
135 result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList("Pad",
136 DEVICE_ID, BundleListOperationType::ADD_BUNDLES, { "ohos.com.test0" }, { "test0" });
137 ASSERT_EQ(result, (int)ERR_OK);
138 result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(DEVICE_TYPE,
139 "abcdef", BundleListOperationType::ADD_BUNDLES, { "ohos.com.test0" }, { "test0" });
140 ASSERT_EQ(result, (int)ERR_OK);
141 bool exist = DistributedDeviceDataService::GetInstance().CheckDeviceBundleExist(DEVICE_TYPE,
142 DEVICE_ID, "ohos.com.test1", "");
143 ASSERT_EQ(exist, true);
144
145 exist = DistributedDeviceDataService::GetInstance().CheckDeviceBundleExist("",
146 DEVICE_ID, "ohos.com.test1", "");
147 ASSERT_EQ(exist, false);
148
149 // remove device installed bundles
150 result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(DEVICE_TYPE,
151 DEVICE_ID, BundleListOperationType::REMOVE_BUNDLES, { "ohos.com.test1" }, { "test1" });
152 ASSERT_EQ(result, (int)ERR_OK);
153 exist = DistributedDeviceDataService::GetInstance().CheckDeviceBundleExist(DEVICE_TYPE,
154 DEVICE_ID, "ohos.com.test1", "");
155 ASSERT_EQ(exist, false);
156 exist = DistributedDeviceDataService::GetInstance().CheckDeviceBundleExist(DEVICE_TYPE,
157 DEVICE_ID, "ohos.com.test2", "");
158 ASSERT_EQ(exist, true);
159
160 exist = DistributedDeviceDataService::GetInstance().CheckDeviceBundleExist(DEVICE_TYPE,
161 DEVICE_ID, "", "test2");
162 ASSERT_EQ(exist, true);
163
164 // clear device installed bundles
165 result = DistributedDeviceDataService::GetInstance().SetTargetDeviceBundleList(DEVICE_TYPE,
166 DEVICE_ID, BundleListOperationType::RELEASE_BUNDLES, {}, {});
167 ASSERT_EQ(result, (int)ERR_OK);
168 exist = DistributedDeviceDataService::GetInstance().CheckDeviceBundleExist(DEVICE_TYPE,
169 DEVICE_ID, "ohos.com.test2", "");
170 ASSERT_EQ(exist, false);
171 }
172 }
173 }
174