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 "UTTest_device_manager_service_notify.h"
17 #include "device_manager_service_notify.h"
18 #include "dm_device_info.h"
19 #include "dm_constants.h"
20
21 #include <unistd.h>
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 DM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerServiceNotify);
SetUp()26 void DeviceManagerServiceNotifyTest::SetUp() {}
27
TearDown()28 void DeviceManagerServiceNotifyTest::TearDown() {}
29
SetUpTestCase()30 void DeviceManagerServiceNotifyTest::SetUpTestCase() {}
31
TearDownTestCase()32 void DeviceManagerServiceNotifyTest::TearDownTestCase() {}
33 namespace {
34
35 HWTEST_F(DeviceManagerServiceNotifyTest, RegisterCallBack_001, testing::ext::TestSize.Level0)
36 {
37 int32_t dmCommonNotifyEvent = 1;
38 ProcessInfo processInfo;
39 int32_t ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo);
40 ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
41
42 processInfo.pkgName = "pkgName";
43 processInfo.userId = 2;
44 ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo);
45 ASSERT_EQ(ret, DM_OK);
46
47 dmCommonNotifyEvent = 10;
48 ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo);
49 ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
50
51 dmCommonNotifyEvent = 2;
52 ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo);
53 ASSERT_EQ(ret, DM_OK);
54
55 std::set<ProcessInfo> processInfoSet;
56 processInfoSet.insert(processInfo);
57 DeviceManagerServiceNotify::GetInstance().callbackMap_
58 .insert(std::make_pair(static_cast<DmCommonNotifyEvent>(dmCommonNotifyEvent), processInfoSet));
59 ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo);
60 ASSERT_EQ(ret, DM_OK);
61
62 dmCommonNotifyEvent = 3;
63 DmCommonNotifyEvent notifyEvent = static_cast<DmCommonNotifyEvent>(dmCommonNotifyEvent);
64 processInfoSet.insert(processInfo);
65 DeviceManagerServiceNotify::GetInstance().callbackMap_[notifyEvent] = processInfoSet;
66 processInfo.pkgName = "proName";
67 processInfo.userId = 10;
68 ret = DeviceManagerServiceNotify::GetInstance().RegisterCallBack(dmCommonNotifyEvent, processInfo);
69 ASSERT_EQ(ret, DM_OK);
70
71 dmCommonNotifyEvent = 4;
72 std::set<ProcessInfo> processInfos;
73 DeviceManagerServiceNotify::GetInstance().GetCallBack(dmCommonNotifyEvent, processInfos);
74
75 dmCommonNotifyEvent = 3;
76 DeviceManagerServiceNotify::GetInstance().GetCallBack(dmCommonNotifyEvent, processInfos);
77
78 ProcessInfo processInfo1;
79 DeviceManagerServiceNotify::GetInstance().callbackMap_[notifyEvent].insert(processInfo1);
80 DeviceManagerServiceNotify::GetInstance().ClearDiedProcessCallback(processInfo1);
81 }
82 }
83 } // namespace DistributedHardware
84 } // namespace OHOS
85