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_client_test.h"
17
18 #include <if_system_ability_manager.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21
22 #include "devicestatus_common.h"
23
24 using namespace testing::ext;
25 using namespace OHOS::Msdp;
26 using namespace OHOS;
27 using namespace std;
28
29 static std::shared_ptr<DevicestatusCallbackProxy> callbackProxy_;
30 static std::shared_ptr<DeviceStatusAgent> agent_;
31
SetUpTestCase()32 void DevicestatusClientTest::SetUpTestCase()
33 {
34 GTEST_LOG_(INFO) << "SetUpTestCase start";
35 agent_ = std::make_shared<DeviceStatusAgent>();
36 GTEST_LOG_(INFO) << "SetUpTestCase end";
37 }
38
TearDownTestCase()39 void DevicestatusClientTest::TearDownTestCase()
40 {
41 }
42
SetUp()43 void DevicestatusClientTest::SetUp()
44 {
45 }
46
TearDown()47 void DevicestatusClientTest::TearDown()
48 {
49 }
50
51 namespace {
52 /**
53 * @tc.name: DevicestatusClientTest001
54 * @tc.desc: test OnRemoteDied
55 * @tc.type: FUNC
56 */
57 HWTEST_F (DevicestatusClientTest, DevicestatusClientTest001, TestSize.Level1)
58 {
59 GTEST_LOG_(INFO) << "DevicestatusClientTest001 start";
60 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61 if (sam == nullptr) {
62 GTEST_LOG_(INFO) << "GetSystemAbilityManager failed";
63 return;
64 }
65 sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(MSDP_DEVICESTATUS_SERVICE_ID);
66 if (remoteObject_ == nullptr) {
67 GTEST_LOG_(INFO) << "CheckSystemAbility failed";
68 return;
69 }
70 auto& client = DevicestatusClient::GetInstance();
71 DEV_HILOGE(INNERKIT, "test OnRemoteDied start");
72 sptr<IRemoteObject::DeathRecipient> deathRecipient_ =
73 sptr<IRemoteObject::DeathRecipient>(new DevicestatusClient::DevicestatusDeathRecipient());
74 deathRecipient_->OnRemoteDied(remoteObject_);
75
76 DEV_HILOGE(INNERKIT, "test OnRemoteDied end");
77 EXPECT_TRUE(client.devicestatusProxy_ == nullptr);
78 GTEST_LOG_(INFO) << "DevicestatusClientTest001 end";
79 }
80
81 /**
82 * @tc.name: DevicestatusClientTest002
83 * @tc.desc: test OnDevicestatusChanged
84 * @tc.type: FUNC
85 */
86 HWTEST_F (DevicestatusClientTest, DevicestatusClientTest002, TestSize.Level1)
87 {
88 GTEST_LOG_(INFO) << "DevicestatusClientTest002 start";
89 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
90 if (sam == nullptr) {
91 GTEST_LOG_(INFO) << "GetSystemAbilityManager failed";
92 return;
93 }
94 sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(MSDP_DEVICESTATUS_SERVICE_ID);
95 if (remoteObject_ == nullptr) {
96 GTEST_LOG_(INFO) << "CheckSystemAbility failed";
97 return;
98 }
99 callbackProxy_ = std::make_shared<DevicestatusCallbackProxy>(remoteObject_);
100 DevicestatusDataUtils::DevicestatusData devicestatusData = {
101 DevicestatusDataUtils::DevicestatusType::TYPE_STILL,
102 DevicestatusDataUtils::DevicestatusValue::VALUE_ENTER
103 };
104 DEV_HILOGE(INNERKIT, "test OnDevicestatusChanged start");
105 callbackProxy_->OnDevicestatusChanged(devicestatusData);
106 DEV_HILOGE(INNERKIT, "test OnDevicestatusChanged end");
107 GTEST_LOG_(INFO) << "DevicestatusClientTest002 end";
108 }
109
110 /**
111 * @tc.name: DevicestatusClientTest003
112 * @tc.desc: test null proxy subscribe
113 * @tc.type: FUNC
114 */
115 HWTEST_F (DevicestatusClientTest, DevicestatusClientTest003, TestSize.Level1)
116 {
117 GTEST_LOG_(INFO) << "DevicestatusClientTest003 start";
118 sptr<IdevicestatusCallback> callback =
119 sptr<IdevicestatusCallback>(new DeviceStatusAgent::DeviceStatusAgentCallback(agent_));
120 auto& client = DevicestatusClient::GetInstance();
121 client.devicestatusProxy_ = nullptr;
122 DEV_HILOGE(INNERKIT, "test SubscribeCallback start");
123 client.SubscribeCallback(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, callback);
124 DEV_HILOGE(INNERKIT, "test SubscribeCallback end");
125 GTEST_LOG_(INFO) << "DevicestatusClientTest003 end";
126 }
127
128 /**
129 * @tc.name: DevicestatusClientTest004
130 * @tc.desc: test null proxy unsubscribe
131 * @tc.type: FUNC
132 */
133 HWTEST_F (DevicestatusClientTest, DevicestatusClientTest004, TestSize.Level1)
134 {
135 GTEST_LOG_(INFO) << "DevicestatusClientTest004 start";
136 sptr<IdevicestatusCallback> callback =
137 sptr<IdevicestatusCallback>(new DeviceStatusAgent::DeviceStatusAgentCallback(agent_));
138 auto& client = DevicestatusClient::GetInstance();
139 client.devicestatusProxy_ = nullptr;
140 DEV_HILOGE(INNERKIT, "test UnSubscribeCallback start");
141 client.UnSubscribeCallback(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, callback);
142 DEV_HILOGE(INNERKIT, "test UnSubscribeCallback end");
143 GTEST_LOG_(INFO) << "DevicestatusClientTest004 end";
144 }
145
146 /**
147 * @tc.name: DevicestatusClientTest005
148 * @tc.desc: test null proxy unsubscribe
149 * @tc.type: FUNC
150 */
151 HWTEST_F (DevicestatusClientTest, DevicestatusClientTest005, TestSize.Level1)
152 {
153 GTEST_LOG_(INFO) << "DevicestatusClientTest005 start";
154 auto& client = DevicestatusClient::GetInstance();
155 client.devicestatusProxy_ = nullptr;
156 DEV_HILOGE(INNERKIT, "test GetDevicestatusData start");
157 auto data = client.GetDevicestatusData(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
158 DEV_HILOGE(INNERKIT, "test GetDevicestatusData end");
159 EXPECT_TRUE(data.type == DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN && \
160 data.value == DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID) << "GetDevicestatusData failed";
161 GTEST_LOG_(INFO) << "DevicestatusClientTest005 end";
162 }
163
164 /**
165 * @tc.name: DevicestatusClientTest006
166 * @tc.desc: test null proxy destructor
167 * @tc.type: FUNC
168 */
169 HWTEST_F (DevicestatusClientTest, DevicestatusClientTest006, TestSize.Level1)
170 {
171 GTEST_LOG_(INFO) << "DevicestatusClientTest006 start";
172 DevicestatusClient* client1 = new(std::nothrow) DevicestatusClient();
173 delete client1;
174 client1 = nullptr;
175
176 DevicestatusClient* client2 = new(std::nothrow) DevicestatusClient();
177 client2->devicestatusProxy_ = nullptr;
178 delete client2;
179 client2 = nullptr;
180 GTEST_LOG_(INFO) << "DevicestatusClientTest006 end";
181 }
182
183 /**
184 * @tc.name: DevicestatusClientTest007
185 * @tc.desc: test null OnRemoteDied
186 * @tc.type: FUNC
187 */
188 HWTEST_F (DevicestatusClientTest, DevicestatusClientTest007, TestSize.Level1)
189 {
190 GTEST_LOG_(INFO) << "DevicestatusClientTest007 start";
191 DEV_HILOGE(INNERKIT, "test OnRemoteDied start");
192 sptr<IRemoteObject::DeathRecipient> deathRecipient_ =
193 sptr<IRemoteObject::DeathRecipient>(new DevicestatusClient::DevicestatusDeathRecipient());
194 deathRecipient_->OnRemoteDied(nullptr);
195 DEV_HILOGE(INNERKIT, "test OnRemoteDied end");
196 auto& client = DevicestatusClient::GetInstance();
197 EXPECT_TRUE(client.devicestatusProxy_ != nullptr);
198 GTEST_LOG_(INFO) << "DevicestatusClientTest007 end";
199 }
200
201 /**
202 * @tc.name: DevicestatusClientTest008
203 * @tc.desc: test proxy double subscribe
204 * @tc.type: FUNC
205 */
206 HWTEST_F (DevicestatusClientTest, DevicestatusClientTest008, TestSize.Level1)
207 {
208 GTEST_LOG_(INFO) << "DevicestatusClientTest008 start";
209 sptr<IdevicestatusCallback> callback =
210 sptr<IdevicestatusCallback>(new (std::nothrow) DeviceStatusAgent::DeviceStatusAgentCallback(agent_));
211 auto& client = DevicestatusClient::GetInstance();
212 DEV_HILOGE(INNERKIT, "test SubscribeCallback start");
213 client.SubscribeCallback(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, callback);
214 client.SubscribeCallback(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, callback);
215 DEV_HILOGE(INNERKIT, "test SubscribeCallback end");
216 GTEST_LOG_(INFO) << "DevicestatusClientTest008 end";
217 }
218 }
219