1 /*
2 * Copyright (c) 2022-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 "devicestatus_service_test.h"
17
18 #include <iostream>
19 #include <chrono>
20 #include <thread>
21 #include <gtest/gtest.h>
22 #include <if_system_ability_manager.h>
23 #include <ipc_skeleton.h>
24 #include <string_ex.h>
25
26 #include "devicestatus_common.h"
27 #include "devicestatus_client.h"
28 #include "devicestatus_dumper.h"
29 #include "devicestatus_service.h"
30
31 using namespace testing::ext;
32 using namespace OHOS::Msdp;
33 using namespace OHOS;
34 using namespace std;
35
OnDevicestatusChanged(const DevicestatusDataUtils::DevicestatusData & devicestatusData)36 void DevicestatusServiceTest::DevicestatusServiceTestCallback::OnDevicestatusChanged(const \
37 DevicestatusDataUtils::DevicestatusData& devicestatusData)
38 {
39 GTEST_LOG_(INFO) << "DevicestatusServiceTestCallback type: " << devicestatusData.type;
40 GTEST_LOG_(INFO) << "DevicestatusServiceTestCallback value: " << devicestatusData.value;
41 EXPECT_TRUE(devicestatusData.type == DevicestatusDataUtils::DevicestatusType::TYPE_RELATIVE_STILL && \
42 devicestatusData.value >= DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID) << \
43 "DevicestatusServiceTestCallback failed";
44 }
45
46 /**
47 * @tc.name: DevicestatusCallbackTest
48 * @tc.desc: test devicestatus callback in proxy
49 * @tc.type: FUNC
50 */
51 HWTEST_F (DevicestatusServiceTest, DevicestatusCallbackTest, TestSize.Level0)
52 {
53 DevicestatusDataUtils::DevicestatusType type = DevicestatusDataUtils::DevicestatusType::TYPE_RELATIVE_STILL;
54 auto& devicestatusClient = DevicestatusClient::GetInstance();
55 sptr<IdevicestatusCallback> cb = new DevicestatusServiceTestCallback();
56 GTEST_LOG_(INFO) << "Start register";
57 devicestatusClient.SubscribeCallback(type, cb);
58 GTEST_LOG_(INFO) << "Cancell register";
59 devicestatusClient.UnSubscribeCallback(type, cb);
60 }
61
62 /**
63 * @tc.name: GetDevicestatusDataTest
64 * @tc.desc: test get devicestatus data in proxy
65 * @tc.type: FUNC
66 */
67 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest001, TestSize.Level0)
68 {
69 DEV_HILOGI(SERVICE, "GetDevicestatusDataTest001 Enter");
70 DevicestatusDataUtils::DevicestatusType type = DevicestatusDataUtils::DevicestatusType::TYPE_STILL;
71 auto& devicestatusClient = DevicestatusClient::GetInstance();
72 DevicestatusDataUtils::DevicestatusData data = devicestatusClient.GetDevicestatusData(type);
73 GTEST_LOG_(INFO) << "type: " << data.type;
74 GTEST_LOG_(INFO) << "value: " << data.value;
75 EXPECT_TRUE(data.type == DevicestatusDataUtils::DevicestatusType::TYPE_STILL && \
76 data.value >= DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID) << "GetDevicestatusData failed";
77 DEV_HILOGI(SERVICE, "GetDevicestatusDataTest001 end");
78 }
79
80 /**
81 * @tc.name: GetDevicestatusDataTest
82 * @tc.desc: test get devicestatus data in proxy
83 * @tc.type: FUNC
84 */
85 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest002, TestSize.Level0)
86 {
87 DEV_HILOGI(SERVICE, "GetDevicestatusDataTest002 Enter");
88 DevicestatusDataUtils::DevicestatusType type = DevicestatusDataUtils::DevicestatusType::TYPE_RELATIVE_STILL;
89 auto& devicestatusClient = DevicestatusClient::GetInstance();
90 DevicestatusDataUtils::DevicestatusData data = devicestatusClient.GetDevicestatusData(type);
91 GTEST_LOG_(INFO) << "type: " << data.type;
92 GTEST_LOG_(INFO) << "value: " << data.value;
93 EXPECT_TRUE(data.type == DevicestatusDataUtils::DevicestatusType::TYPE_RELATIVE_STILL && \
94 data.value >= DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID) << "GetDevicestatusData failed";
95 DEV_HILOGI(SERVICE, "GetDevicestatusDataTest002 end");
96 }
97
98 /**
99 * @tc.name: GetDevicestatusDataTest
100 * @tc.desc: test get devicestatus data in proxy
101 * @tc.type: FUNC
102 */
103 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest003, TestSize.Level0)
104 {
105 DEV_HILOGI(SERVICE, "GetDevicestatusDataTest003 Enter");
106 DevicestatusDataUtils::DevicestatusType type = DevicestatusDataUtils::DevicestatusType::TYPE_CAR_BLUETOOTH;
107 auto& devicestatusClient = DevicestatusClient::GetInstance();
108 DevicestatusDataUtils::DevicestatusData data = devicestatusClient.GetDevicestatusData(type);
109 GTEST_LOG_(INFO) << "type: " << data.type;
110 GTEST_LOG_(INFO) << "value: " << data.value;
111 EXPECT_TRUE(data.type == DevicestatusDataUtils::DevicestatusType::TYPE_CAR_BLUETOOTH && \
112 data.value == DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID) << "GetDevicestatusData failed";
113 DEV_HILOGI(SERVICE, "GetDevicestatusDataTest003 end");
114 }
115
116 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest004, TestSize.Level0)
117 {
118 GTEST_LOG_(INFO) << "GetDevicestatusDataTest004 Enter";
119 DevicestatusDataUtils::DevicestatusType type = DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN;
120 auto& devicestatusClient = DevicestatusClient::GetInstance();
121 DevicestatusDataUtils::DevicestatusData data = devicestatusClient.GetDevicestatusData(type);
122 GTEST_LOG_(INFO) << "type: " << data.type;
123 GTEST_LOG_(INFO) << "value: " << data.value;
124 EXPECT_TRUE(data.type == DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN && \
125 data.value == DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID) << "GetDevicestatusDataTest004 failed";
126 GTEST_LOG_(INFO) << "GetDevicestatusDataTest004 end";
127 }
128
129 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest005, TestSize.Level0)
130 {
131 GTEST_LOG_(INFO) << "GetDevicestatusDataTest005 Enter";
132 DevicestatusDataUtils::DevicestatusType type = DevicestatusDataUtils::DevicestatusType::TYPE_INVALID;
133 auto& devicestatusClient = DevicestatusClient::GetInstance();
134 DevicestatusDataUtils::DevicestatusData data = devicestatusClient.GetDevicestatusData(type);
135 GTEST_LOG_(INFO) << "type: " << data.type;
136 GTEST_LOG_(INFO) << "value: " << data.value;
137 EXPECT_TRUE(data.type == DevicestatusDataUtils::DevicestatusType::TYPE_INVALID && \
138 data.value == DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID) << "GetDevicestatusDataTest005 failed";
139 GTEST_LOG_(INFO) << "GetDevicestatusDataTest005 end";
140 }
141
142 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest006, TestSize.Level0)
143 {
144 GTEST_LOG_(INFO) << "GetDevicestatusDataTest006 Enter";
145 DevicestatusDataUtils::DevicestatusType type = static_cast<DevicestatusDataUtils::DevicestatusType>(10);
146 auto& devicestatusClient = DevicestatusClient::GetInstance();
147 DevicestatusDataUtils::DevicestatusData data = devicestatusClient.GetDevicestatusData(type);
148 GTEST_LOG_(INFO) << "type: " << data.type;
149 GTEST_LOG_(INFO) << "value: " << data.value;
150 EXPECT_TRUE(data.type == DevicestatusDataUtils::DevicestatusType::TYPE_INVALID && \
151 data.value == DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID) << "GetDevicestatusDataTest006 failed";
152 GTEST_LOG_(INFO) << "GetDevicestatusDataTest006 end";
153 }
154
155 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest007, TestSize.Level0)
156 {
157 GTEST_LOG_(INFO) << "GetDevicestatusDataTest007 Enter";
158 auto devicestatusService = DelayedSpSingleton<DevicestatusService>::GetInstance();
159 devicestatusService->OnDump();
160 GTEST_LOG_(INFO) << "GetDevicestatusDataTest007 end";
161 }
162
163 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest008, TestSize.Level0)
164 {
165 GTEST_LOG_(INFO) << "GetDevicestatusDataTest008 Enter";
166 auto devicestatusService = DelayedSpSingleton<DevicestatusService>::GetInstance();
167 std::vector<std::u16string> args;
168 int32_t ret = devicestatusService->Dump(-1, args);
169 EXPECT_TRUE(ret == RET_ERR);
170 ret = devicestatusService->Dump(1, args);
171 EXPECT_TRUE(ret == RET_ERR);
172 args.emplace_back(ARGS_H);
173 ret = devicestatusService->Dump(1, args);
174 EXPECT_TRUE(ret == RET_OK);
175 GTEST_LOG_(INFO) << "GetDevicestatusDataTest008 end";
176 }
177
178 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest009, TestSize.Level0)
179 {
180 GTEST_LOG_(INFO) << "GetDevicestatusDataTest009 Enter";
181 auto devicestatusCBDeathRecipient = new (std::nothrow) DevicestatusManager::DevicestatusCallbackDeathRecipient();
182 devicestatusCBDeathRecipient->OnRemoteDied(nullptr);
183 GTEST_LOG_(INFO) << "GetDevicestatusDataTest009 end";
184 }
185
186 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest010, TestSize.Level0)
187 {
188 GTEST_LOG_(INFO) << "GetDevicestatusDataTest010 Enter";
189 auto devicestatusService = DelayedSpSingleton<DevicestatusService>::GetInstance();
190 auto devicestatusManager = std::make_shared<DevicestatusManager>(devicestatusService);
191 Security::AccessToken::AccessTokenID tokenId = static_cast<Security::AccessToken::AccessTokenID>(1);
192 std::string packageName;
193 devicestatusManager->GetPackageName(tokenId, packageName);
194 GTEST_LOG_(INFO) << "GetDevicestatusDataTest010 end";
195 }
196
197 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest011, TestSize.Level0)
198 {
199 GTEST_LOG_(INFO) << "GetDevicestatusDataTest011 Enter";
200 auto devicestatusService = DelayedSpSingleton<DevicestatusService>::GetInstance();
201 devicestatusService->OnStop();
202 devicestatusService->OnStart();
203 devicestatusService->OnStart();
204 devicestatusService->OnStop();
205 GTEST_LOG_(INFO) << "GetDevicestatusDataTest011 end";
206 }
207
208 HWTEST_F (DevicestatusServiceTest, GetDevicestatusDataTest012, TestSize.Level0)
209 {
210 GTEST_LOG_(INFO) << "GetDevicestatusDataTest012 Enter";
211 auto devicestatusService = DelayedSpSingleton<DevicestatusService>::GetInstance();
212 auto devicestatusManager = std::make_shared<DevicestatusManager>(devicestatusService);
213 bool result = devicestatusManager->Init();
214 EXPECT_TRUE(result);
215 int32_t ret = devicestatusManager->LoadAlgorithm();
216 EXPECT_TRUE(ret == RET_OK);
217 ret = devicestatusManager->UnloadAlgorithm();
218 EXPECT_TRUE(ret == RET_OK);
219 DelayedSpSingleton<DevicestatusService>::DestroyInstance();
220 GTEST_LOG_(INFO) << "GetDevicestatusDataTest012 end";
221 }