1 /******************************************************************************
3 * Copyright (C) 2015 Google, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 #include "adapter/bluetooth_test.h"
22 #include "btcore/include/property.h"
27 // Each iteration of the test takes about 2 seconds to run, so choose a value
28 // that matches your time constraints. For example, 5 iterations would take
29 // about 10 seconds to run
30 const int kTestRepeatCount = 5;
36 TEST_F(BluetoothTest, AdapterEnableDisable) {
37 EXPECT_EQ(GetState(), BT_STATE_OFF) <<
38 "Test should be run with Adapter disabled";
40 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
41 semaphore_wait(adapter_state_changed_callback_sem_);
42 EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
44 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
45 semaphore_wait(adapter_state_changed_callback_sem_);
46 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
49 TEST_F(BluetoothTest, AdapterRepeatedEnableDisable) {
50 EXPECT_EQ(GetState(), BT_STATE_OFF)
51 << "Test should be run with Adapter disabled";
53 for (int i = 0; i < kTestRepeatCount; ++i) {
54 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
55 semaphore_wait(adapter_state_changed_callback_sem_);
56 EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
58 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
59 semaphore_wait(adapter_state_changed_callback_sem_);
60 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
64 TEST_F(BluetoothTest, AdapterSetGetName) {
65 bt_property_t *new_name = property_new_name("BluetoothTestName1");
67 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
68 semaphore_wait(adapter_state_changed_callback_sem_);
69 EXPECT_EQ(GetState(), BT_STATE_ON)
70 << "Test should be run with Adapter enabled";
72 // Enabling the interface will call the properties callback twice before
73 // ever reaching this point.
74 ClearSemaphore(adapter_properties_callback_sem_);
76 EXPECT_EQ(bt_interface()->get_adapter_property(BT_PROPERTY_BDNAME),
78 semaphore_wait(adapter_properties_callback_sem_);
79 EXPECT_GT(GetPropertiesChangedCount(), 0)
80 << "Expected at least one adapter property to change";
81 bt_property_t *name_property = GetProperty(BT_PROPERTY_BDNAME);
82 EXPECT_NE(name_property, nullptr);
83 if (property_equals(name_property, new_name)) {
84 property_free(new_name);
85 new_name = property_new_name("BluetoothTestName2");
87 std::string old_name((const char *)property_as_name(name_property)->name);
89 EXPECT_EQ(bt_interface()->set_adapter_property(new_name), BT_STATUS_SUCCESS);
90 semaphore_wait(adapter_properties_callback_sem_);
91 EXPECT_GT(GetPropertiesChangedCount(), 0)
92 << "Expected at least one adapter property to change";
93 EXPECT_TRUE(GetProperty(BT_PROPERTY_BDNAME))
94 << "The Bluetooth name property did not change.";
95 EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), new_name))
97 << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
98 << " does not match test value " << property_as_name(new_name)->name;
101 bt_property_t *old_name_property = property_new_name(old_name.c_str());
102 EXPECT_EQ(bt_interface()->set_adapter_property(old_name_property), BT_STATUS_SUCCESS);
103 semaphore_wait(adapter_properties_callback_sem_);
104 EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), old_name_property))
106 << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
107 << " does not match original name" << old_name;
109 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
110 semaphore_wait(adapter_state_changed_callback_sem_);
111 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
112 property_free(new_name);
113 property_free(old_name_property);
116 TEST_F(BluetoothTest, AdapterStartDiscovery) {
117 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
118 semaphore_wait(adapter_state_changed_callback_sem_);
119 EXPECT_EQ(GetState(), BT_STATE_ON)
120 << "Test should be run with Adapter enabled";
122 EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
123 semaphore_wait(discovery_state_changed_callback_sem_);
124 EXPECT_EQ(GetDiscoveryState(), BT_DISCOVERY_STARTED)
125 << "Unable to start discovery.";
127 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
128 semaphore_wait(adapter_state_changed_callback_sem_);
129 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
132 TEST_F(BluetoothTest, AdapterCancelDiscovery) {
133 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
134 semaphore_wait(adapter_state_changed_callback_sem_);
135 EXPECT_EQ(GetState(), BT_STATE_ON)
136 << "Test should be run with Adapter enabled";
138 EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
139 semaphore_wait(discovery_state_changed_callback_sem_);
140 EXPECT_EQ(bt_interface()->cancel_discovery(), BT_STATUS_SUCCESS);
141 semaphore_wait(discovery_state_changed_callback_sem_);
143 EXPECT_EQ(GetDiscoveryState(), BT_DISCOVERY_STOPPED)
144 << "Unable to stop discovery.";
146 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
147 semaphore_wait(adapter_state_changed_callback_sem_);
148 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";