OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "base/memory/scoped_ptr.h" |
| 6 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_api.h" |
| 7 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_event_router.h" |
| 8 #include "chrome/browser/extensions/extension_apitest.h" |
| 9 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 10 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 11 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 12 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 13 #include "device/bluetooth/test/mock_bluetooth_gatt_service.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 |
| 16 using device::BluetoothUUID; |
| 17 using device::BluetoothAdapter; |
| 18 using device::BluetoothDevice; |
| 19 using device::BluetoothGattService; |
| 20 using device::MockBluetoothAdapter; |
| 21 using device::MockBluetoothDevice; |
| 22 using device::MockBluetoothGattService; |
| 23 using testing::Return; |
| 24 using testing::_; |
| 25 |
| 26 namespace utils = extension_function_test_utils; |
| 27 |
| 28 namespace { |
| 29 |
| 30 const char kTestLeDeviceAddress[] = "11:22:33:44:55:66"; |
| 31 const char kTestLeDeviceName[] = "Test LE Device"; |
| 32 |
| 33 const char kTestServiceId0[] = "service_id0"; |
| 34 const char kTestServiceUuid0[] = "1234"; |
| 35 const char kTestServiceId1[] = "service_id1"; |
| 36 const char kTestServiceUuid1[] = "5678"; |
| 37 |
| 38 class BluetoothLowEnergyApiTest : public ExtensionApiTest { |
| 39 public: |
| 40 BluetoothLowEnergyApiTest() {} |
| 41 |
| 42 virtual ~BluetoothLowEnergyApiTest() {} |
| 43 |
| 44 virtual void SetUpOnMainThread() OVERRIDE { |
| 45 ExtensionApiTest::SetUpOnMainThread(); |
| 46 empty_extension_ = utils::CreateEmptyExtension(); |
| 47 SetUpMocks(); |
| 48 } |
| 49 |
| 50 virtual void CleanUpOnMainThread() OVERRIDE { |
| 51 EXPECT_CALL(*mock_adapter_, RemoveObserver(_)); |
| 52 } |
| 53 |
| 54 void SetUpMocks() { |
| 55 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>(); |
| 56 EXPECT_CALL(*mock_adapter_, GetDevices()) |
| 57 .WillOnce(Return(BluetoothAdapter::ConstDeviceList())); |
| 58 |
| 59 event_router()->SetAdapterForTesting(mock_adapter_); |
| 60 |
| 61 device_.reset( |
| 62 new testing::NiceMock<MockBluetoothDevice>(mock_adapter_, |
| 63 0, |
| 64 kTestLeDeviceName, |
| 65 kTestLeDeviceAddress, |
| 66 false /* paired */, |
| 67 true /* connected */)); |
| 68 |
| 69 service0_.reset(new testing::NiceMock<MockBluetoothGattService>( |
| 70 device_.get(), |
| 71 kTestServiceId0, |
| 72 BluetoothUUID(kTestServiceUuid0), |
| 73 true /* is_primary */, |
| 74 false /* is_local */)); |
| 75 |
| 76 service1_.reset(new testing::NiceMock<MockBluetoothGattService>( |
| 77 device_.get(), |
| 78 kTestServiceId1, |
| 79 BluetoothUUID(kTestServiceUuid1), |
| 80 false /* is_primary */, |
| 81 false /* is_local */)); |
| 82 } |
| 83 |
| 84 protected: |
| 85 extensions::BluetoothLowEnergyEventRouter* event_router() { |
| 86 return extensions::BluetoothLowEnergyAPI::Get(browser()->profile()) |
| 87 ->event_router(); |
| 88 } |
| 89 |
| 90 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_; |
| 91 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device_; |
| 92 scoped_ptr<testing::NiceMock<MockBluetoothGattService> > service0_; |
| 93 scoped_ptr<testing::NiceMock<MockBluetoothGattService> > service1_; |
| 94 |
| 95 private: |
| 96 scoped_refptr<extensions::Extension> empty_extension_; |
| 97 }; |
| 98 |
| 99 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetServices) { |
| 100 ResultCatcher catcher; |
| 101 catcher.RestrictToProfile(browser()->profile()); |
| 102 |
| 103 std::vector<BluetoothGattService*> services; |
| 104 services.push_back(service0_.get()); |
| 105 services.push_back(service1_.get()); |
| 106 |
| 107 EXPECT_CALL(*mock_adapter_, GetDevice(_)) |
| 108 .Times(3) |
| 109 .WillOnce(Return(static_cast<BluetoothDevice*>(NULL))) |
| 110 .WillRepeatedly(Return(device_.get())); |
| 111 |
| 112 EXPECT_CALL(*device_, GetGattServices()) |
| 113 .Times(2) |
| 114 .WillOnce(Return(std::vector<BluetoothGattService*>())) |
| 115 .WillOnce(Return(services)); |
| 116 |
| 117 // Load and wait for setup. |
| 118 ExtensionTestMessageListener listener("ready", true); |
| 119 ASSERT_TRUE(LoadExtension( |
| 120 test_data_dir_.AppendASCII("bluetooth_low_energy/get_services"))); |
| 121 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 122 |
| 123 listener.Reply("go"); |
| 124 |
| 125 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 126 } |
| 127 |
| 128 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetService) { |
| 129 ResultCatcher catcher; |
| 130 catcher.RestrictToProfile(browser()->profile()); |
| 131 |
| 132 event_router()->DeviceAdded(mock_adapter_, device_.get()); |
| 133 event_router()->GattServiceAdded(device_.get(), service0_.get()); |
| 134 |
| 135 EXPECT_CALL(*mock_adapter_, GetDevice(_)) |
| 136 .Times(3) |
| 137 .WillOnce(Return(static_cast<BluetoothDevice*>(NULL))) |
| 138 .WillRepeatedly(Return(device_.get())); |
| 139 |
| 140 EXPECT_CALL(*device_, GetGattService(kTestServiceId0)) |
| 141 .Times(2) |
| 142 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL))) |
| 143 .WillOnce(Return(service0_.get())); |
| 144 |
| 145 // Load and wait for setup. |
| 146 ExtensionTestMessageListener listener("ready", true); |
| 147 ASSERT_TRUE(LoadExtension( |
| 148 test_data_dir_.AppendASCII("bluetooth_low_energy/get_service"))); |
| 149 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 150 |
| 151 listener.Reply("go"); |
| 152 |
| 153 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 154 |
| 155 event_router()->GattServiceRemoved(device_.get(), service0_.get()); |
| 156 event_router()->DeviceRemoved(mock_adapter_, device_.get()); |
| 157 } |
| 158 |
| 159 } // namespace |
OLD | NEW |