• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #include <cstdlib>
18 #include "disdveprofile_test.h"
19 #include "nlohmann/json.hpp"
20 #include <system>
21 #include "json/json.h"
22 #include <thread>
23 #include "device_profile_log.h"
24 #include <string>
25 #include "distributed_device_profile_client.h"
26 int32_t dpOk = 0;
27 int32_t dpError = -1;
InitDevice()28 int DPTest::InitDevice()
29 {
30     DHLOGI("demo test: InitDevice enter");
31     auto disDPTest = std::make_shared<DistributedDeviceProfileService>();
32     bool result = disDPTest->Init();
33     if(!result){
34        DHLOGI("demo test: InitDevice error");
35         return dpError;
36     }
37     return dpOk;
38 }
39 
threadSendF()40 int threadSendF()
41 {
42     DHLOGI("demo test: 请插入第一个手机");
43     int status = system("D:\\DP\\push.bat");
44     Sleep(40000);
45     if (status != 0) {
46        DHLOGI("demo test: send file fail");
47        return dpError;
48     }
49     DHLOGI("demo test: 请插入第二个手机");
50     int status = system("D:\\DP\\push.bat");
51     Sleep(40000);
52     if (status != 0) {
53        DHLOGI("demo test: send file fail");
54        return dpError;
55     }
56     return dpOk;
57 }
58 
SendFP()59 void DPTest::SendFP()
60 {
61     std::thread myThread(threadSendF);
62     myThread.join();
63 }
64 
GetDevProfileTest()65 std::string DPTest::GetDevProfileTest()
66 {
67     ServiceCharacteristicProfile profile;
68     std::string testServiceId = profile.GetServiceId();
69     DistributedDeviceProfileClient::GetInstance().GetDeviceProfile("", testServiceId, profile);
70     std::string jsonData = profile.GetCharacteristicProfileJson();
71     if(jsonData.empty()){
72         DHLOGI("demo test: GetDevProfileTest GetCharacteristicProfileJson error");
73         return "false";
74     }
75     std::string result = result.append("jsonData:" + jsonData + "\n");
76     return result;
77 }
78 
PutDevProfileTest()79 std::string DPTest::PutDevProfileTest()
80 {
81     // 声明并填充插入数据
82     ServiceCharacteristicProfile profile;
83     profile.SetServiceId("test");
84     profile.SetServiceType("test");
85     nlohmann::json j;
86     j["testVersion"] = "3.0.0";
87     j["testApiLevel"] = API_LEVEL;
88     profile.SetCharacteristicProfileJson(j.dump());
89     // 执行插入接口PutDeviceProfile
90     DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
91     std::string jsonData = profile.GetCharacteristicProfileJson();
92     if (jsonData.empty()) {
93         DHLOGI("demo test: PutDevProfileTest GetCharacteristicProfileJson error");
94         return "false";
95     }
96     std::string result = result.append("jsonData:" + jsonData + "\n");
97     return result;
98 }
99 /*写入serviceid,servicetype为空*/
PutDevProfileSerciceidTest()100 std::string DPTest::PutDevProfileSerciceidTest()
101 {
102     // 声明并填充插入数据
103     ServiceCharacteristicProfile profile;
104     profile.SetServiceId("");
105     profile.SetServiceType("");
106     nlohmann::json j;
107     j["testVersion"] = "3.0.0";
108     j["testApiLevel"] = API_LEVEL;
109     profile.SetCharacteristicProfileJson(j.dump());
110     // 执行插入接口PutDeviceProfile
111     DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
112     std::string jsonData = profile.GetCharacteristicProfileJson();
113     if (jsonData.empty()) {
114         DHLOGI("demo test: PutDevProfileTest error");
115         return "false";
116     }
117     std::string result = result.append("jsonData:" + jsonData + "\n");
118     return result;
119 }
120 
DeleteDevProfileTest()121 int DPTest::DeleteDevProfileTest()
122 {
123     std::string serviceId = "test";
124     DistributedDeviceProfileClient::GetInstance().DeleteDeviceProfile(serviceId);
125     if (ret != 0) {
126             DHLOGI("demo test: DeleteDevProfileTest error");
127             return dpError;
128     }
129     return dpOk;
130 }
131 
SyncDveProfileTest()132 int DPTest::SyncDveProfileTest(){
133     ServiceCharacteristicProfile profile;
134     SyncOptions syncOption;
135     syncOption.SetSyncMode((OHOS::DeviceProfile::SyncMode)atoi(mode.c_str()));
136     for (const auto& deviceId : deviceIds) {
137         syncOption.AddDevice(deviceId);
138     }
139 // 执行同步接口
140     int32_t ret = DistributedDeviceProfileClient::GetInstance().SyncDeviceProfile(syncOption,
141         std::make_shared<ProfileEventCallback>());
142     if (ret != ERR_OK) {
143         DHLOGI("demo test: SyncDveProfileTest error");
144         return dpError;
145     }
146 
147     return dpOk;
148 }
149 
SubscribeProfileEventsTest()150 int DPTest::SubscribeProfileEventsTest(){
151 
152     auto callback = std::make_shared<ProfileEventCallback>();
153     std::list<SubscribeInfo> subscribeInfos;
154     ServiceCharacteristicProfile profile;
155     std::string deviceId = JSON.stringify(dmDeviceInfo[0]["deviceId"]);
156     std::string ServiceIds = profile.GetServiceId();
157 // 订阅EVENT_PROFILE_CHANGED事件
158     ExtraInfo extraInfo;
159     extraInfo["deviceId"] = deviceId;
160     extraInfo["serviceIds"] = ServiceIds;
161     SubscribeInfo changeEventInfo;
162     changeEventInfo.profileEvent = ProfileEvent::EVENT_PROFILE_CHANGED;
163     changeEventInfo.extraInfo = std::move(extraInfo);
164     subscribeInfos.emplace_back(changeEventInfo);
165 
166 // 订阅EVENT_SYNC_COMPLETED事件
167     SubscribeInfo syncEventInfo;
168     syncEventInfo.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED;
169     subscribeInfos.emplace_back(syncEventInfo);
170 
171 // 执行订阅接口
172     std::list<ProfileEvent> failedEvents;
173     DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos,
174     callback, failedEvents);
175     if (failedEvents.empty()) {
176         DHLOGI("demo test: SubscribeProfileEvents error");
177         return dpError;
178     }
179 
180 // 解除订阅
181     std::list<ProfileEvent> profileEvents;
182     profileEvents.emplace_back(ProfileEvent::EVENT_PROFILE_CHANGED);
183     DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(profileEvents,
184     callback, failedEvents);
185     if (failedEvents.empty()) {j
186         DHLOGI("demo test: UnsubscribeProfileEvents error");
187         return dpError;
188     }
189     return dpOk;
190 }
191