Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: chrome/browser/chromeos/bluetooth/bluetooth_adapter_devices_chromeos_unittest.cc

Issue 10899037: Refactoring bluetooth API code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: I Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" 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"
6 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" 8 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h"
7 #include "chromeos/dbus/mock_bluetooth_adapter_client.h" 9 #include "chromeos/dbus/mock_bluetooth_adapter_client.h"
8 #include "chromeos/dbus/mock_bluetooth_device_client.h" 10 #include "chromeos/dbus/mock_bluetooth_device_client.h"
9 #include "chromeos/dbus/mock_bluetooth_manager_client.h" 11 #include "chromeos/dbus/mock_bluetooth_manager_client.h"
10 #include "chromeos/dbus/mock_dbus_thread_manager.h" 12 #include "chromeos/dbus/mock_dbus_thread_manager.h"
11 #include "dbus/object_path.h" 13 #include "dbus/object_path.h"
12 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
13 15
14 using ::testing::_; 16 using ::testing::_;
15 using ::testing::Mock; 17 using ::testing::Mock;
16 using ::testing::Return; 18 using ::testing::Return;
17 using ::testing::SaveArg; 19 using ::testing::SaveArg;
18 20
19 namespace chromeos { 21 namespace chromeos {
20 22
21 class BluetoothAdapterDevicesTest : public testing::Test { 23 class BluetoothAdapterDevicesChromeOsTest : public testing::Test {
22 public: 24 public:
23 virtual void SetUp() { 25 virtual void SetUp() {
24 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager; 26 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager;
25 27
26 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus()) 28 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus())
27 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); 29 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL)));
28 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); 30 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager);
29 31
30 mock_manager_client_ = 32 mock_manager_client_ =
31 mock_dbus_thread_manager->mock_bluetooth_manager_client(); 33 mock_dbus_thread_manager->mock_bluetooth_manager_client();
32 mock_adapter_client_ = 34 mock_adapter_client_ =
33 mock_dbus_thread_manager->mock_bluetooth_adapter_client(); 35 mock_dbus_thread_manager->mock_bluetooth_adapter_client();
34 mock_device_client_ = 36 mock_device_client_ =
35 mock_dbus_thread_manager->mock_bluetooth_device_client(); 37 mock_dbus_thread_manager->mock_bluetooth_device_client();
36 38
37 // Create the default adapter instance; 39 // Create the default adapter instance;
38 // BluetoothManagerClient::DefaultAdapter will be called once, passing 40 // BluetoothManagerClient::DefaultAdapter will be called once, passing
39 // a callback to obtain the adapter path. 41 // a callback to obtain the adapter path.
40 BluetoothManagerClient::AdapterCallback adapter_callback; 42 BluetoothManagerClient::AdapterCallback adapter_callback;
41 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) 43 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
42 .WillOnce(SaveArg<0>(&adapter_callback)); 44 .WillOnce(SaveArg<0>(&adapter_callback));
43 45
44 EXPECT_CALL(*mock_manager_client_, AddObserver(_)) 46 EXPECT_CALL(*mock_manager_client_, AddObserver(_))
45 .Times(1); 47 .Times(1);
46 EXPECT_CALL(*mock_adapter_client_, AddObserver(_)) 48 EXPECT_CALL(*mock_adapter_client_, AddObserver(_))
47 .Times(1); 49 .Times(1);
48 50
49 adapter_ = BluetoothAdapter::DefaultAdapter(); 51 adapter_ = BluetoothAdapterFactory::DefaultAdapter();
50 52
51 // Call the adapter callback; 53 // Call the adapter callback;
52 // BluetoothAdapterClient::GetProperties will be called once to obtain 54 // BluetoothAdapterClient::GetProperties will be called once to obtain
53 // the property set. 55 // the property set.
54 MockBluetoothAdapterClient::Properties adapter_properties; 56 MockBluetoothAdapterClient::Properties adapter_properties;
55 adapter_properties.address.ReplaceValue(adapter_address_); 57 adapter_properties.address.ReplaceValue(adapter_address_);
56 adapter_properties.powered.ReplaceValue(true); 58 adapter_properties.powered.ReplaceValue(true);
57 59
58 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path_)) 60 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path_))
59 .WillRepeatedly(Return(&adapter_properties)); 61 .WillRepeatedly(Return(&adapter_properties));
60 62
61 // Add an observer to the adapter; expect the usual set of changes to 63 // Add an observer to the adapter; expect the usual set of changes to
62 // an adapter becoming present and then clear to clean up for the test. 64 // an adapter becoming present and then clear to clean up for the test.
63 adapter_->AddObserver(&adapter_observer_); 65 adapter_->AddObserver(&adapter_observer_);
64 66
65 EXPECT_CALL(adapter_observer_, AdapterPresentChanged(adapter_.get(), true)) 67 EXPECT_CALL(adapter_observer_, AdapterPresentChanged(adapter_.get(), true))
66 .Times(1); 68 .Times(1);
67 EXPECT_CALL(adapter_observer_, AdapterPoweredChanged(adapter_.get(), true)) 69 EXPECT_CALL(adapter_observer_, AdapterPoweredChanged(adapter_.get(), true))
68 .Times(1); 70 .Times(1);
69 71
70 adapter_callback.Run(adapter_path_, true); 72 adapter_callback.Run(adapter_path_, true);
71 73
72 Mock::VerifyAndClearExpectations(mock_manager_client_); 74 Mock::VerifyAndClearExpectations(mock_manager_client_);
73 Mock::VerifyAndClearExpectations(mock_adapter_client_); 75 Mock::VerifyAndClearExpectations(mock_adapter_client_);
74 Mock::VerifyAndClearExpectations(mock_device_client_); 76 Mock::VerifyAndClearExpectations(mock_device_client_);
75 Mock::VerifyAndClearExpectations(&adapter_observer_); 77 Mock::VerifyAndClearExpectations(&adapter_observer_);
76 } 78 }
77 79
78 virtual void TearDown() { 80 virtual void TearDown() {
79 EXPECT_CALL(*mock_device_client_, RemoveObserver(adapter_.get())) 81 BluetoothAdapterChromeOs* adapter_chromeos =
82 static_cast<BluetoothAdapterChromeOs*>(adapter_.get());
83 ASSERT_TRUE(adapter_chromeos != NULL);
84 EXPECT_CALL(*mock_device_client_, RemoveObserver(adapter_chromeos))
80 .Times(1); 85 .Times(1);
81 EXPECT_CALL(*mock_adapter_client_, RemoveObserver(adapter_.get())) 86 EXPECT_CALL(*mock_adapter_client_, RemoveObserver(adapter_chromeos))
82 .Times(1); 87 .Times(1);
83 EXPECT_CALL(*mock_manager_client_, RemoveObserver(adapter_.get())) 88 EXPECT_CALL(*mock_manager_client_, RemoveObserver(adapter_chromeos))
84 .Times(1); 89 .Times(1);
85 90
86 adapter_ = NULL; 91 adapter_ = NULL;
87 DBusThreadManager::Shutdown(); 92 DBusThreadManager::Shutdown();
88 } 93 }
89 94
90 protected: 95 protected:
91 MockBluetoothManagerClient* mock_manager_client_; 96 MockBluetoothManagerClient* mock_manager_client_;
92 MockBluetoothAdapterClient* mock_adapter_client_; 97 MockBluetoothAdapterClient* mock_adapter_client_;
93 MockBluetoothDeviceClient* mock_device_client_; 98 MockBluetoothDeviceClient* mock_device_client_;
94 99
95 static const dbus::ObjectPath adapter_path_; 100 static const dbus::ObjectPath adapter_path_;
96 static const std::string adapter_address_; 101 static const std::string adapter_address_;
97 scoped_refptr<BluetoothAdapter> adapter_; 102 scoped_refptr<BluetoothAdapter> adapter_;
98 103
99 MockBluetoothAdapter::Observer adapter_observer_; 104 MockBluetoothAdapter::Observer adapter_observer_;
100 }; 105 };
101 106
102 const dbus::ObjectPath BluetoothAdapterDevicesTest::adapter_path_( 107 const dbus::ObjectPath BluetoothAdapterDevicesChromeOsTest::adapter_path_(
103 "/fake/hci0"); 108 "/fake/hci0");
104 const std::string BluetoothAdapterDevicesTest::adapter_address_ = 109 const std::string BluetoothAdapterDevicesChromeOsTest::adapter_address_ =
105 "CA:FE:4A:C0:FE:FE"; 110 "CA:FE:4A:C0:FE:FE";
106 111
107 TEST_F(BluetoothAdapterDevicesTest, DeviceRemovedAfterFound) { 112 TEST_F(BluetoothAdapterDevicesChromeOsTest, DeviceRemovedAfterFound) {
108 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_01"); 113 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_01");
109 const std::string device_address = "BA:C0:11:00:00:01"; 114 const std::string device_address = "BA:C0:11:00:00:01";
110 115
111 MockBluetoothDeviceClient::Properties device_properties; 116 MockBluetoothDeviceClient::Properties device_properties;
112 device_properties.address.ReplaceValue(device_address); 117 device_properties.address.ReplaceValue(device_address);
113 device_properties.name.ReplaceValue("Fake Keyboard"); 118 device_properties.name.ReplaceValue("Fake Keyboard");
114 device_properties.bluetooth_class.ReplaceValue(0x2540); 119 device_properties.bluetooth_class.ReplaceValue(0x2540);
115 120
116 // needs to be a supported class 121 // needs to be a supported class
117 122
118 // should create a bluetooth device, 123 // should create a bluetooth device,
119 // put it in the hash map, 124 // put it in the hash map,
120 // set visible to true, 125 // set visible to true,
121 // update its properties, 126 // update its properties,
122 127
123 // Inform the adapter that the device has been found; 128 // Inform the adapter that the device has been found;
124 // BluetoothAdapterClient::Observer::DeviceAdded will be called, passing 129 // BluetoothAdapterClient::Observer::DeviceAdded will be called, passing
125 // the device object. 130 // the device object.
126 BluetoothDevice* device; 131 BluetoothDevice* device;
127 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) 132 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _))
128 .Times(1) 133 .Times(1)
129 .WillOnce(SaveArg<1>(&device)); 134 .WillOnce(SaveArg<1>(&device));
130 135
131 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 136 BluetoothAdapterChromeOs* adapter_chromeos =
137 static_cast<BluetoothAdapterChromeOs*>(adapter_.get());
138 ASSERT_TRUE(adapter_chromeos != NULL);
139 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
132 ->DeviceFound(adapter_path_, device_address, device_properties); 140 ->DeviceFound(adapter_path_, device_address, device_properties);
133 141
134 // Now inform the adapter that the device has been added and assigned an 142 // Now inform the adapter that the device has been added and assigned an
135 // object path; BluetoothDeviceClient::GetProperties will be called to 143 // object path; BluetoothDeviceClient::GetProperties will be called to
136 // obtain the property set; and 144 // obtain the property set; and
137 // BluetoothAdapterClient::Observer::DeviceChanged will be called passing 145 // BluetoothAdapterClient::Observer::DeviceChanged will be called passing
138 // the same device object as before. 146 // the same device object as before.
139 EXPECT_CALL(*mock_device_client_, GetProperties(device_path)) 147 EXPECT_CALL(*mock_device_client_, GetProperties(device_path))
140 .WillRepeatedly(Return(&device_properties)); 148 .WillRepeatedly(Return(&device_properties));
141 149
142 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device)) 150 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_chromeos, device))
143 .Times(1); 151 .Times(1);
144 152
145 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 153 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
146 ->DeviceCreated(adapter_path_, device_path); 154 ->DeviceCreated(adapter_path_, device_path);
147 155
148 // Finally remove the adapter again; since this is a supported device 156 // Finally remove the adapter again; since this is a supported device
149 // BluetoothAdapterClient::Observer::DeviceRemoved should be not called, 157 // BluetoothAdapterClient::Observer::DeviceRemoved should be not called,
150 // instead BluetoothAdapterClient::Observer::DeviceChanged will be called. 158 // instead BluetoothAdapterClient::Observer::DeviceChanged will be called.
151 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device)) 159 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device))
152 .Times(0); 160 .Times(0);
153 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device)) 161 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device))
154 .Times(1); 162 .Times(1);
155 163
156 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 164 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
157 ->DeviceRemoved(adapter_path_, device_path); 165 ->DeviceRemoved(adapter_path_, device_path);
158 166
159 // Verify that the device is still visible, just no longer paired. 167 // Verify that the device is still visible, just no longer paired.
160 EXPECT_TRUE(device->IsVisible()); 168 EXPECT_TRUE(device->IsVisible());
161 EXPECT_FALSE(device->IsPaired()); 169 EXPECT_FALSE(device->IsPaired());
162 } 170 }
163 171
164 TEST_F(BluetoothAdapterDevicesTest, UnsupportedDeviceRemovedAfterFound) { 172 TEST_F(BluetoothAdapterDevicesChromeOsTest,
173 UnsupportedDeviceRemovedAfterFound) {
165 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_02"); 174 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_02");
166 const std::string device_address = "BA:C0:11:00:00:02"; 175 const std::string device_address = "BA:C0:11:00:00:02";
167 176
168 MockBluetoothDeviceClient::Properties device_properties; 177 MockBluetoothDeviceClient::Properties device_properties;
169 device_properties.address.ReplaceValue(device_address); 178 device_properties.address.ReplaceValue(device_address);
170 device_properties.name.ReplaceValue("Fake Computer"); 179 device_properties.name.ReplaceValue("Fake Computer");
171 device_properties.bluetooth_class.ReplaceValue(0x400100); 180 device_properties.bluetooth_class.ReplaceValue(0x400100);
172 181
173 // Inform the adapter that the unsupported device has been found; 182 // Inform the adapter that the unsupported device has been found;
174 // BluetoothAdapterClient::Observer::DeviceAdded should not be called 183 // BluetoothAdapterClient::Observer::DeviceAdded should not be called
175 // yet because this device is not supported so is hidden from the UI. 184 // yet because this device is not supported so is hidden from the UI.
176 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) 185 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _))
177 .Times(0); 186 .Times(0);
178 187
179 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 188 BluetoothAdapterChromeOs* adapter_chromeos =
189 static_cast<BluetoothAdapterChromeOs*>(adapter_.get());
190 ASSERT_TRUE(adapter_chromeos != NULL);
191 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
180 ->DeviceFound(adapter_path_, device_address, device_properties); 192 ->DeviceFound(adapter_path_, device_address, device_properties);
181 193
182 // Now inform the adapter the device has been added and assigned an 194 // Now inform the adapter the device has been added and assigned an
183 // object path; BluetoothDeviceClient::GetProperties will be called 195 // object path; BluetoothDeviceClient::GetProperties will be called
184 // to obtain the property set; and 196 // to obtain the property set; and
185 // BluetoothAdapterClient::Observer::DeviceAdded will be called, 197 // BluetoothAdapterClient::Observer::DeviceAdded will be called,
186 // passing the device object. 198 // passing the device object.
187 EXPECT_CALL(*mock_device_client_, GetProperties(device_path)) 199 EXPECT_CALL(*mock_device_client_, GetProperties(device_path))
188 .WillRepeatedly(Return(&device_properties)); 200 .WillRepeatedly(Return(&device_properties));
189 201
190 BluetoothDevice* device; 202 BluetoothDevice* device;
191 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) 203 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _))
192 .Times(1) 204 .Times(1)
193 .WillOnce(SaveArg<1>(&device)); 205 .WillOnce(SaveArg<1>(&device));
194 206
195 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 207 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
196 ->DeviceCreated(adapter_path_, device_path); 208 ->DeviceCreated(adapter_path_, device_path);
197 209
198 // Finally remove the device again; 210 // Finally remove the device again;
199 // BluetoothAdapterClient::Observer::DeviceRemoved will be called 211 // BluetoothAdapterClient::Observer::DeviceRemoved will be called
200 // before the device object is deleted. 212 // before the device object is deleted.
201 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device)) 213 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device))
202 .Times(1); 214 .Times(1);
203 215
204 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 216 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
205 ->DeviceRemoved(adapter_path_, device_path); 217 ->DeviceRemoved(adapter_path_, device_path);
206 } 218 }
207 219
208 } // namespace chromeos 220 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698