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 "bluetooth_state_test.h"
17
18 #include "bluetooth_state_listener.h"
19 #include "bluetooth_state_adapter.h"
20 #include "../test_log.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace MechBodyController {
27 namespace {
28 const std::string TAG = "BluetoothState";
29 }
SetUpTestCase()30 void BluetoothStateTest::SetUpTestCase()
31 {
32 }
33
TearDownTestCase()34 void BluetoothStateTest::TearDownTestCase()
35 {
36 }
37
SetUp()38 void BluetoothStateTest::SetUp()
39 {
40 }
41
TearDown()42 void BluetoothStateTest::TearDown()
43 {
44 }
45
46
47 /**
48 * @tc.name: testBluetoothStateInit001
49 * @tc.desc: test get bluetooth state from bluetooth service
50 * @tc.type: FUNC
51 * @tc.require: I5Y2VH
52 */
53 HWTEST_F(BluetoothStateTest, testBluetoothStateInit001, TestSize.Level1)
54 {
55 DTEST_LOG << "BluetoothStateTest testBluetoothStateInit001 start" << std::endl;
56 std::shared_ptr<BluetoothStateListener> bluetoothStateListener = BluetoothStateListener::GetInstance();
57 EXPECT_NO_FATAL_FAILURE(bluetoothStateListener->InitBluetoothState());
58 DTEST_LOG << "BluetoothStateTest testBluetoothStateInit001 end" << std::endl;
59 }
60
61 /**
62 * @tc.name: testOnStateChanged001
63 * @tc.desc: test OnStateChanged from bluetooth service
64 * @tc.type: FUNC
65 * @tc.require: I5Y2VH
66 */
67 HWTEST_F(BluetoothStateTest, testOnStateChanged001, TestSize.Level1)
68 {
69 DTEST_LOG << "BluetoothStateTest testOnStateChanged001 start" << std::endl;
70 std::shared_ptr<BluetoothStateListener> bluetoothStateListener = BluetoothStateListener::GetInstance();
71 int transport = static_cast<int>(Bluetooth::BTTransport::ADAPTER_BREDR);
72 int status = 0;
73 EXPECT_NO_FATAL_FAILURE(bluetoothStateListener->OnStateChanged(transport, status));
74
75 transport = static_cast<int>(Bluetooth::BTTransport::ADAPTER_BLE);
76 EXPECT_NO_FATAL_FAILURE(bluetoothStateListener->OnStateChanged(transport, status));
77
78 transport = -1;
79 EXPECT_NO_FATAL_FAILURE(bluetoothStateListener->OnStateChanged(transport, status));
80 DTEST_LOG << "BluetoothStateTest testOnStateChanged001 end" << std::endl;
81 }
82
83 /**
84 * @tc.name: testBluetoothStateChange001
85 * @tc.desc: test get bluetooth state from bluetooth service
86 * @tc.type: FUNC
87 * @tc.require: I5Y2VH
88 */
89 HWTEST_F(BluetoothStateTest, testBluetoothStateChange001, TestSize.Level1)
90 {
91 DTEST_LOG << "BluetoothStateTest testBluetoothStateChange001 start" << std::endl;
92 BluetoothStateAdapter::GetInstance().UpdateBTState(true);
93 BluetoothStateAdapter::GetInstance().UpdateBLEState(true);
94 EXPECT_TRUE(BluetoothStateAdapter::GetInstance().IsBluetoothActive());
95
96 BluetoothStateAdapter::GetInstance().UpdateBTState(true);
97 BluetoothStateAdapter::GetInstance().UpdateBLEState(false);
98 EXPECT_TRUE(BluetoothStateAdapter::GetInstance().IsBluetoothActive());
99
100 BluetoothStateAdapter::GetInstance().UpdateBTState(false);
101 BluetoothStateAdapter::GetInstance().UpdateBLEState(true);
102 EXPECT_TRUE(BluetoothStateAdapter::GetInstance().IsBluetoothActive());
103
104 BluetoothStateAdapter::GetInstance().UpdateBTState(false);
105 BluetoothStateAdapter::GetInstance().UpdateBLEState(false);
106 EXPECT_FALSE(BluetoothStateAdapter::GetInstance().IsBluetoothActive());
107 DTEST_LOG << "luetoothStateTest testBluetoothStateChange001 end" << std::endl;
108 }
109
110 } // DistributedSchedule
111 } // namespace OHOS