• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024-2024 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 "telephony_errors.h"
18 #include "distributed_device_observer.h"
19 #include "distributed_sink_switch_controller.h"
20 #include "distributed_source_switch_controller.h"
21 
22 namespace OHOS {
23 namespace Telephony {
24 using namespace testing::ext;
25 
26 class DistributedDevObserverTest : public testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase() {}
TearDownTestCase()29     static void TearDownTestCase() {}
SetUp()30     virtual void SetUp() {}
TearDown()31     virtual void TearDown() {}
32 };
33 
34 /**
35  * @tc.number   Telephony_DcDeviceObserver_001
36  * @tc.name     test normal branch
37  * @tc.desc     normal branch test
38  */
39 HWTEST_F(DistributedDevObserverTest, Telephony_DcDeviceObserver_001, Function | MediumTest | Level1)
40 {
41     int32_t devRole = 0;
42     int32_t devType = -1;
43     int32_t dcPhone = 0;
44     int32_t dcPad = 1;
45     int32_t dcPc = 2;
46     int32_t dcCallSaId = 66198;
47     int32_t otherSaId = -1;
48     std::string devId = "UnitTestDeviceId";
49     std::string devName = "UnitTestDeviceName";
50     AudioDeviceType deviceType = AudioDeviceType::DEVICE_DISTRIBUTED_PHONE;
51     auto deviceObserver = std::make_shared<DistributedDeviceObserver>();
52     auto deviceCallback = std::make_shared<DistributedDeviceCallback>();
53     auto saListener = sptr<DistributedSystemAbilityListener>::MakeSptr();
54     std::shared_ptr<IDistributedDeviceStateCallback> callback = nullptr;
55     ASSERT_NO_THROW(deviceObserver->Init());
56     ASSERT_NO_THROW(deviceObserver->RegisterDevStatusCallback(callback));
57     ASSERT_NO_THROW(deviceObserver->RegisterDevStatusCallback(callback)); // duplicate register
58     ASSERT_NO_THROW(deviceObserver->UnRegisterDevCallback());
59 
60     callback = std::make_shared<DistributedSourceSwitchController>();
61     ASSERT_NO_THROW(deviceObserver->RegisterDevStatusCallback(callback));
62     ASSERT_NO_THROW(deviceObserver->Init());
63     ASSERT_NO_THROW(deviceObserver->OnDeviceOnline(devId, devName, deviceType));
64     ASSERT_NO_THROW(deviceObserver->OnDeviceOffline(devId, devName, deviceType));
65     ASSERT_NO_THROW(deviceObserver->OnRemoveSystemAbility());
66     deviceObserver->callbacks_.push_back(callback);
67     ASSERT_NO_THROW(deviceObserver->OnDistributedAudioDeviceChange(devId, devName,
68         static_cast<AudioDeviceType>(devType), devRole));
69     ASSERT_NO_THROW(saListener->OnAddSystemAbility(otherSaId, devId));
70     ASSERT_NO_THROW(saListener->OnAddSystemAbility(dcCallSaId, devId));
71     ASSERT_NO_THROW(saListener->OnRemoveSystemAbility(otherSaId, devId));
72     ASSERT_NO_THROW(saListener->OnRemoveSystemAbility(dcCallSaId, devId));
73     int32_t ret = deviceCallback->OnDistributedAudioDeviceChange(devId, devName, devType, devRole);
74     ASSERT_TRUE(ret == TELEPHONY_ERROR);
75 
76     devType = 1;
77     ret = deviceCallback->OnDistributedAudioDeviceChange(devId, devName, devType, devRole);
78     ASSERT_TRUE(ret == TELEPHONY_SUCCESS);
79 
80     AudioDeviceType convertRes = deviceCallback->ConvertDeviceType(-1);
81     EXPECT_TRUE(convertRes == AudioDeviceType::DEVICE_UNKNOWN);
82     convertRes = deviceCallback->ConvertDeviceType(dcPhone);
83     EXPECT_TRUE(convertRes == AudioDeviceType::DEVICE_DISTRIBUTED_PHONE);
84     convertRes = deviceCallback->ConvertDeviceType(dcPad);
85     EXPECT_TRUE(convertRes == AudioDeviceType::DEVICE_DISTRIBUTED_PAD);
86     convertRes = deviceCallback->ConvertDeviceType(dcPc);
87     EXPECT_TRUE(convertRes == AudioDeviceType::DEVICE_DISTRIBUTED_PC);
88 }
89 
90 /**
91  * @tc.number   Telephony_DcDeviceObserver_002
92  * @tc.name     test normal branch
93  * @tc.desc     normal branch test
94  */
95 HWTEST_F(DistributedDevObserverTest, Telephony_DcDeviceObserver_002, Function | MediumTest | Level1)
96 {
97     std::string devId = "UnitTestDeviceId";
98     std::string devName = "UnitTestDeviceName";
99     AudioDeviceType deviceType = AudioDeviceType::DEVICE_DISTRIBUTED_PHONE;
100     auto deviceObserver = std::make_shared<DistributedDeviceObserver>();
101     std::shared_ptr<IDistributedDeviceStateCallback> callback = nullptr;
102     ASSERT_NO_THROW(deviceObserver->Init());
103     ASSERT_NO_THROW(deviceObserver->RegisterDevStatusCallback(callback));
104     ASSERT_NO_THROW(deviceObserver->RegisterDevStatusCallback(callback)); // duplicate register
105     ASSERT_NO_THROW(deviceObserver->UnRegisterDevCallback());
106 
107     callback = std::make_shared<DistributedSinkSwitchController>();
108     ASSERT_NO_THROW(deviceObserver->RegisterDevStatusCallback(callback));
109     ASSERT_NO_THROW(deviceObserver->OnDeviceOnline(devId, devName, deviceType));
110     ASSERT_NO_THROW(deviceObserver->OnDeviceOffline(devId, devName, deviceType));
111     ASSERT_NO_THROW(deviceObserver->OnRemoveSystemAbility());
112 }
113 
114 /**
115  * @tc.number   Telephony_DcDeviceObserver_DeviceOnlineOffline_001
116  * @tc.name     test device online and device offline
117  * @tc.desc     Function test
118  */
119 HWTEST_F(DistributedDevObserverTest, Telephony_DcDeviceObserver_DeviceOnlineOffline_001, Function | MediumTest | Level1)
120 {
121     int32_t devRole = 0;
122     int32_t devType = -1;
123     std::string devId = "UnitTestDeviceId";
124     std::string devName = "UnitTestDeviceName";
125     auto deviceCallback = std::make_shared<DistributedDeviceCallback>();
126 
127     int32_t ret = deviceCallback->OnDistributedDeviceOnline(devId, devName, devType, devRole);
128     ASSERT_TRUE(ret == TELEPHONY_ERROR);
129 
130     ret = deviceCallback->OnDistributedDeviceOffline(devId, devName, devType, devRole);
131     ASSERT_TRUE(ret == TELEPHONY_ERROR);
132 
133     devType = 1;
134     ret = deviceCallback->OnDistributedDeviceOnline(devId, devName, devType, devRole);
135     ASSERT_TRUE(ret == TELEPHONY_SUCCESS);
136 
137     ret = deviceCallback->OnDistributedDeviceOffline(devId, devName, devType, devRole);
138     ASSERT_TRUE(ret == TELEPHONY_SUCCESS);
139 }
140 
141 } // namespace Telephony
142 } // namespace OHOS
143