• 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 "distributed_communication_manager.h"
18 #include "distributed_sink_switch_controller.h"
19 #include "distributed_source_switch_controller.h"
20 
21 namespace OHOS {
22 namespace Telephony {
23 using namespace testing::ext;
24 
25 class DistributedDevSwitchTest : public testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase() {}
TearDownTestCase()28     static void TearDownTestCase() {}
SetUp()29     virtual void SetUp() {}
TearDown()30     virtual void TearDown() {}
31 };
32 
33 /**
34  * @tc.number   Telephony_DcDevSwitch_001
35  * @tc.name     test test normal branch
36  * @tc.desc     Function test
37  */
38 HWTEST_F(DistributedDevSwitchTest, Telephony_DcDevSwitch_001, Function | MediumTest | Level1)
39 {
40     int32_t direction = 0;
41     std::string devId = "UnitTestDeviceId";
42     std::string devName = "UnitTestDeviceName";
43     DistributedRole devRole = DistributedRole::SINK;
44     AudioDeviceType deviceType = AudioDeviceType::DEVICE_DISTRIBUTED_PHONE;
45     auto sourceSwitchController = std::make_shared<DistributedSourceSwitchController>();
46     ASSERT_NO_THROW(sourceSwitchController->OnDeviceOnline(devId, devName, deviceType));
47     ASSERT_NO_THROW(sourceSwitchController->OnDeviceOffline(devId, devName, deviceType));
48     ASSERT_NO_THROW(sourceSwitchController->OnDistributedAudioDeviceChange(devId, devName,
49         deviceType, static_cast<int32_t>(devRole)));
50     ASSERT_TRUE(sourceSwitchController->IsAudioOnSink());
51     ASSERT_NO_THROW(sourceSwitchController->OnRemoveSystemAbility());
52     ASSERT_FALSE(sourceSwitchController->IsAudioOnSink());
53     ASSERT_NO_THROW(sourceSwitchController->SwitchDevice(devId, 1));
54     auto sinkSwitchController = std::make_shared<DistributedSinkSwitchController>();
55     ASSERT_NO_THROW(sinkSwitchController->OnDeviceOnline(devId, devName, deviceType));
56     ASSERT_NO_THROW(sinkSwitchController->OnDeviceOffline(devId, devName, deviceType));
57     ASSERT_NO_THROW(sinkSwitchController->hfpListener_ = std::make_shared<DcCallHfpListener>());
58     ASSERT_NO_THROW(sinkSwitchController->OnDistributedAudioDeviceChange(devId, devName,
59         deviceType, static_cast<int32_t>(devRole)));
60     ASSERT_TRUE(sinkSwitchController->IsAudioOnSink());
61     ASSERT_NO_THROW(sinkSwitchController->hfpListener_ = std::make_shared<DcCallHfpListener>());
62     ASSERT_NO_THROW(sinkSwitchController->OnRemoveSystemAbility());
63     ASSERT_FALSE(sinkSwitchController->IsAudioOnSink());
64 }
65 
66 #ifdef ABILITY_BLUETOOTH_SUPPORT
67 /**
68  * @tc.number   Telephony_DcCallHfpListenerTest
69  * @tc.name     test hfp listeber normal func
70  * @tc.desc     Function test
71  */
72 HWTEST_F(DistributedDevSwitchTest, Telephony_DcCallHfpListenerTest, Function | MediumTest | Level1)
73 {
74     DcCallHfpListener listener;
75     DcCallSourceHfpListener sourceListener;
76     Bluetooth::BluetoothRemoteDevice device;
77     ASSERT_NO_THROW(listener.SetPreAction(1));
78     ASSERT_NO_THROW(listener.SwitchToBtHeadset(device));
79     ASSERT_NO_THROW(listener.OnHfpStackChanged(device, 0)); // WEAR_ACTION
80     ASSERT_NO_THROW(listener.OnHfpStackChanged(device, 1)); // UNWEAR_ACTION
81     ASSERT_NO_THROW(listener.OnHfpStackChanged(device, 2)); // ENABLE_FROM_REMOTE_ACTION
82     ASSERT_NO_THROW(listener.OnHfpStackChanged(device, 3)); // DISABLE_FROM_REMOTE_ACTION
83     ASSERT_NO_THROW(listener.OnHfpStackChanged(device, 6)); // USER_SELECTION_ACTION
84     ASSERT_NO_THROW(sourceListener.OnHfpStackChanged(device, 0));
85 
86     auto dcManager = DelayedSingleton<DistributedCommunicationManager>::GetInstance();
87     ASSERT_NO_THROW(dcManager->devSwitchController_ = std::make_shared<DistributedSinkSwitchController>());
88     dcManager->devSwitchController_->isAudioOnSink_ = true;
89     ASSERT_NO_THROW(sourceListener.IsNeedSwitchToSource(device, 1));
90     dcManager->devSwitchController_->isAudioOnSink_ = false;
91     ASSERT_NO_THROW(sourceListener.IsNeedSwitchToSource(device, 1));
92 }
93 #endif
94 } // namespace Telephony
95 } // namespace OHOS
96