OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | |
6 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_chromeos.h" | |
7 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_factory.h" | |
8 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" | |
9 #include "chromeos/dbus/mock_bluetooth_adapter_client.h" | |
10 #include "chromeos/dbus/mock_bluetooth_device_client.h" | |
11 #include "chromeos/dbus/mock_bluetooth_manager_client.h" | |
12 #include "chromeos/dbus/mock_dbus_thread_manager.h" | |
13 #include "dbus/object_path.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 using ::testing::_; | |
17 using ::testing::Mock; | |
18 using ::testing::Return; | |
19 using ::testing::SaveArg; | |
20 | |
21 namespace chromeos { | |
22 | |
23 class BluetoothAdapterDevicesChromeOsTest : public testing::Test { | |
24 public: | |
25 virtual void SetUp() { | |
26 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager; | |
27 | |
28 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus()) | |
29 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); | |
30 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); | |
31 | |
32 mock_manager_client_ = | |
33 mock_dbus_thread_manager->mock_bluetooth_manager_client(); | |
34 mock_adapter_client_ = | |
35 mock_dbus_thread_manager->mock_bluetooth_adapter_client(); | |
36 mock_device_client_ = | |
37 mock_dbus_thread_manager->mock_bluetooth_device_client(); | |
38 | |
39 // Create the default adapter instance; | |
40 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
41 // a callback to obtain the adapter path. | |
42 BluetoothManagerClient::AdapterCallback adapter_callback; | |
43 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
44 .WillOnce(SaveArg<0>(&adapter_callback)); | |
45 | |
46 EXPECT_CALL(*mock_manager_client_, AddObserver(_)) | |
47 .Times(1); | |
48 EXPECT_CALL(*mock_adapter_client_, AddObserver(_)) | |
49 .Times(1); | |
50 | |
51 adapter_ = BluetoothAdapterFactory::DefaultAdapter(); | |
52 | |
53 // Call the adapter callback; | |
54 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
55 // the property set. | |
56 MockBluetoothAdapterClient::Properties adapter_properties; | |
57 adapter_properties.address.ReplaceValue(adapter_address_); | |
58 adapter_properties.powered.ReplaceValue(true); | |
59 | |
60 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path_)) | |
61 .WillRepeatedly(Return(&adapter_properties)); | |
62 | |
63 // Add an observer to the adapter; expect the usual set of changes to | |
64 // an adapter becoming present and then clear to clean up for the test. | |
65 adapter_->AddObserver(&adapter_observer_); | |
66 | |
67 EXPECT_CALL(adapter_observer_, AdapterPresentChanged(adapter_.get(), true)) | |
68 .Times(1); | |
69 EXPECT_CALL(adapter_observer_, AdapterPoweredChanged(adapter_.get(), true)) | |
70 .Times(1); | |
71 | |
72 adapter_callback.Run(adapter_path_, true); | |
73 | |
74 Mock::VerifyAndClearExpectations(mock_manager_client_); | |
75 Mock::VerifyAndClearExpectations(mock_adapter_client_); | |
76 Mock::VerifyAndClearExpectations(mock_device_client_); | |
77 Mock::VerifyAndClearExpectations(&adapter_observer_); | |
78 } | |
79 | |
80 virtual void TearDown() { | |
81 BluetoothAdapterChromeOs* adapter_chromeos = | |
82 static_cast<BluetoothAdapterChromeOs*>(adapter_.get()); | |
83 EXPECT_CALL(*mock_device_client_, RemoveObserver(adapter_chromeos)) | |
84 .Times(1); | |
85 EXPECT_CALL(*mock_adapter_client_, RemoveObserver(adapter_chromeos)) | |
86 .Times(1); | |
87 EXPECT_CALL(*mock_manager_client_, RemoveObserver(adapter_chromeos)) | |
88 .Times(1); | |
89 | |
90 adapter_ = NULL; | |
91 DBusThreadManager::Shutdown(); | |
92 } | |
93 | |
94 protected: | |
95 MockBluetoothManagerClient* mock_manager_client_; | |
96 MockBluetoothAdapterClient* mock_adapter_client_; | |
97 MockBluetoothDeviceClient* mock_device_client_; | |
98 | |
99 static const dbus::ObjectPath adapter_path_; | |
100 static const std::string adapter_address_; | |
101 scoped_refptr<BluetoothAdapter> adapter_; | |
102 | |
103 MockBluetoothAdapter::Observer adapter_observer_; | |
104 }; | |
105 | |
106 const dbus::ObjectPath BluetoothAdapterDevicesChromeOsTest::adapter_path_( | |
107 "/fake/hci0"); | |
108 const std::string BluetoothAdapterDevicesChromeOsTest::adapter_address_ = | |
109 "CA:FE:4A:C0:FE:FE"; | |
110 | |
111 TEST_F(BluetoothAdapterDevicesChromeOsTest, DeviceRemovedAfterFound) { | |
112 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_01"); | |
113 const std::string device_address = "BA:C0:11:00:00:01"; | |
114 | |
115 MockBluetoothDeviceClient::Properties device_properties; | |
116 device_properties.address.ReplaceValue(device_address); | |
117 device_properties.name.ReplaceValue("Fake Keyboard"); | |
118 device_properties.bluetooth_class.ReplaceValue(0x2540); | |
119 | |
120 // Inform the adapter that the device has been found; | |
121 // BluetoothAdapterClient::Observer::DeviceAdded will be called, passing | |
122 // the device object. | |
123 BluetoothDevice* device; | |
124 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) | |
125 .Times(1) | |
126 .WillOnce(SaveArg<1>(&device)); | |
127 | |
128 BluetoothAdapterChromeOs* adapter_chromeos = | |
129 static_cast<BluetoothAdapterChromeOs*>(adapter_.get()); | |
130 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos) | |
131 ->DeviceFound(adapter_path_, device_address, device_properties); | |
132 | |
133 // Now inform the adapter that the device has been added and assigned an | |
134 // object path; BluetoothDeviceClient::GetProperties will be called to | |
135 // obtain the property set; and | |
136 // BluetoothAdapterClient::Observer::DeviceChanged will be called passing | |
137 // the same device object as before. | |
138 EXPECT_CALL(*mock_device_client_, GetProperties(device_path)) | |
139 .WillRepeatedly(Return(&device_properties)); | |
140 | |
141 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_chromeos, device)) | |
142 .Times(1); | |
143 | |
144 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos) | |
145 ->DeviceCreated(adapter_path_, device_path); | |
146 | |
147 // Finally remove the adapter again; | |
148 // BluetoothAdapterClient::Observer::DeviceRemoved should be not called, | |
149 // instead BluetoothAdapterClient::Observer::DeviceChanged will be called. | |
150 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device)) | |
151 .Times(0); | |
152 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device)) | |
153 .Times(1); | |
154 | |
155 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos) | |
156 ->DeviceRemoved(adapter_path_, device_path); | |
157 | |
158 // Verify that the device is still visible, just no longer paired. | |
159 EXPECT_TRUE(device->IsVisible()); | |
160 EXPECT_FALSE(device->IsPaired()); | |
161 } | |
162 | |
163 } // namespace chromeos | |
OLD | NEW |