Commit f500eea6 authored by bryeung@chromium.org's avatar bryeung@chromium.org

Bluetooth API: test discovery callback.

TEST=added
BUG=132616


Review URL: https://chromiumcodereview.appspot.com/10695161

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@146271 0039d316-1c4b-4281-b951-d872f2087c98
parent b93f9602
...@@ -33,6 +33,13 @@ class BluetoothApiTest : public PlatformAppApiTest { ...@@ -33,6 +33,13 @@ class BluetoothApiTest : public PlatformAppApiTest {
// The browser will clean this up when it is torn down // The browser will clean this up when it is torn down
mock_adapter_ = new testing::StrictMock<chromeos::MockBluetoothAdapter>; mock_adapter_ = new testing::StrictMock<chromeos::MockBluetoothAdapter>;
event_router()->SetAdapterForTest(mock_adapter_); event_router()->SetAdapterForTest(mock_adapter_);
device1_.reset(new testing::NiceMock<chromeos::MockBluetoothDevice>(
mock_adapter_, "d1", "11:12:13:14:15:16",
true /* paired */, false /* bonded */, true /* connected */));
device2_.reset(new testing::NiceMock<chromeos::MockBluetoothDevice>(
mock_adapter_, "d2", "21:22:23:24:25:26",
false /* paired */, true /* bonded */, false /* connected */));
} }
void expectBooleanResult(bool expected, void expectBooleanResult(bool expected,
...@@ -56,6 +63,8 @@ class BluetoothApiTest : public PlatformAppApiTest { ...@@ -56,6 +63,8 @@ class BluetoothApiTest : public PlatformAppApiTest {
protected: protected:
testing::StrictMock<chromeos::MockBluetoothAdapter>* mock_adapter_; testing::StrictMock<chromeos::MockBluetoothAdapter>* mock_adapter_;
scoped_ptr<testing::NiceMock<chromeos::MockBluetoothDevice> > device1_;
scoped_ptr<testing::NiceMock<chromeos::MockBluetoothDevice> > device2_;
chromeos::ExtensionBluetoothEventRouter* event_router() { chromeos::ExtensionBluetoothEventRouter* event_router() {
return browser()->profile()->GetExtensionService()-> return browser()->profile()->GetExtensionService()->
...@@ -127,19 +136,13 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, IsPowered) { ...@@ -127,19 +136,13 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, IsPowered) {
} }
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) {
testing::NiceMock<chromeos::MockBluetoothDevice> device1(
mock_adapter_, "d1", "11:12:13:14:15:16",
true /* paired */, false /* bonded */, true /* connected */);
testing::NiceMock<chromeos::MockBluetoothDevice> device2(
mock_adapter_, "d2", "21:22:23:24:25:26",
false /* paired */, true /* bonded */, false /* connected */);
chromeos::BluetoothAdapter::ConstDeviceList devices; chromeos::BluetoothAdapter::ConstDeviceList devices;
devices.push_back(&device1); devices.push_back(device1_.get());
devices.push_back(&device2); devices.push_back(device2_.get());
EXPECT_CALL(device1, ProvidesServiceWithUUID("foo")) EXPECT_CALL(*device1_, ProvidesServiceWithUUID("foo"))
.WillOnce(testing::Return(false)); .WillOnce(testing::Return(false));
EXPECT_CALL(device2, ProvidesServiceWithUUID("foo")) EXPECT_CALL(*device2_, ProvidesServiceWithUUID("foo"))
.WillOnce(testing::Return(true)); .WillOnce(testing::Return(true));
EXPECT_CALL(*mock_adapter_, GetDevices()) EXPECT_CALL(*mock_adapter_, GetDevices())
...@@ -236,12 +239,9 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetLocalOutOfBandPairingData) { ...@@ -236,12 +239,9 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetLocalOutOfBandPairingData) {
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) { IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) {
std::string device_address("11:12:13:14:15:16"); std::string device_address("11:12:13:14:15:16");
testing::NiceMock<chromeos::MockBluetoothDevice> device(
mock_adapter_, "d1", device_address,
true /* paired */, false /* bonded */, true /* connected */);
EXPECT_CALL(*mock_adapter_, GetDevice(device_address)) EXPECT_CALL(*mock_adapter_, GetDevice(device_address))
.WillOnce(testing::Return(&device)); .WillOnce(testing::Return(device1_.get()));
EXPECT_CALL(device, EXPECT_CALL(*device1_,
ClearOutOfBandPairingData(testing::Truly(CallClosure), ClearOutOfBandPairingData(testing::Truly(CallClosure),
testing::_)); testing::_));
...@@ -258,10 +258,10 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) { ...@@ -258,10 +258,10 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) {
// Try again with an error // Try again with an error
testing::Mock::VerifyAndClearExpectations(mock_adapter_); testing::Mock::VerifyAndClearExpectations(mock_adapter_);
testing::Mock::VerifyAndClearExpectations(&device); testing::Mock::VerifyAndClearExpectations(device1_.get());
EXPECT_CALL(*mock_adapter_, GetDevice(device_address)) EXPECT_CALL(*mock_adapter_, GetDevice(device_address))
.WillOnce(testing::Return(&device)); .WillOnce(testing::Return(device1_.get()));
EXPECT_CALL(device, EXPECT_CALL(*device1_,
ClearOutOfBandPairingData(testing::_, ClearOutOfBandPairingData(testing::_,
testing::Truly(CallClosure))); testing::Truly(CallClosure)));
...@@ -277,8 +277,6 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) { ...@@ -277,8 +277,6 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) {
} }
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
// TODO(bryeung): test that no events are sent now (crbug.com/132616)
// Try with a failure to start // Try with a failure to start
EXPECT_CALL(*mock_adapter_, EXPECT_CALL(*mock_adapter_,
SetDiscovering(true, SetDiscovering(true,
...@@ -300,8 +298,6 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { ...@@ -300,8 +298,6 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
(void)utils::RunFunctionAndReturnError(start_function, "[]", browser()); (void)utils::RunFunctionAndReturnError(start_function, "[]", browser());
// TODO(bryeung): test that events are sent now (crbug.com/132616)
// Reset to try stopping // Reset to try stopping
testing::Mock::VerifyAndClearExpectations(mock_adapter_); testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_, EXPECT_CALL(*mock_adapter_,
...@@ -312,8 +308,6 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { ...@@ -312,8 +308,6 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
(void)utils::RunFunctionAndReturnResult(stop_function, "[]", browser()); (void)utils::RunFunctionAndReturnResult(stop_function, "[]", browser());
// TODO(bryeung): test that no events are sent now (crbug.com/132616)
// Reset to try stopping with an error // Reset to try stopping with an error
testing::Mock::VerifyAndClearExpectations(mock_adapter_); testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_, EXPECT_CALL(*mock_adapter_,
...@@ -325,6 +319,34 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { ...@@ -325,6 +319,34 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
ASSERT_TRUE(!error.empty()); ASSERT_TRUE(!error.empty());
} }
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) {
EXPECT_CALL(*mock_adapter_,
SetDiscovering(true, testing::Truly(CallClosure), testing::_));
EXPECT_CALL(*mock_adapter_,
SetDiscovering(false, testing::Truly(CallClosure), testing::_));
ResultCatcher catcher;
catcher.RestrictToProfile(browser()->profile());
ExtensionTestMessageListener discovery_started("ready", true);
const extensions::Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("bluetooth"));
GURL page_url = extension->GetResourceURL("test_discovery.html");
ui_test_utils::NavigateToURL(browser(), page_url);
EXPECT_TRUE(discovery_started.WaitUntilSatisfied());
event_router()->DeviceAdded(mock_adapter_, device1_.get());
discovery_started.Reply("go");
ExtensionTestMessageListener discovery_stopped("ready", true);
EXPECT_TRUE(discovery_stopped.WaitUntilSatisfied());
event_router()->DeviceAdded(mock_adapter_, device2_.get());
discovery_stopped.Reply("go");
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Events) { IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Events) {
ResultCatcher catcher; ResultCatcher catcher;
catcher.RestrictToProfile(browser()->profile()); catcher.RestrictToProfile(browser()->profile());
......
<!--
* Copyright (c) 2012 The Chromium Authors. All rights reserved. Use of this
* source code is governed by a BSD-style license that can be found in the
* LICENSE file.
-->
<script src="test_discovery.js"></script>
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function testDiscovery() {
chrome.test.assertEq(kExpectedDeviceNames.length,
discoveredDevices.length);
for (var i = 0; i < kExpectedDeviceNames.length; ++i) {
chrome.test.assertEq(kExpectedDeviceNames[i], discoveredDevices[i].name);
}
chrome.test.succeed();
}
function startTests() {
chrome.test.runTests([testDiscovery]);
}
function sendReady(callback) {
chrome.test.sendMessage('ready', callback);
}
function stopDiscoveryAndContinue() {
chrome.experimental.bluetooth.stopDiscovery();
sendReady(startTests);
}
var kExpectedDeviceNames = ["d1"];
var discoveredDevices = [];
function recordDevice(device) {
discoveredDevices.push(device);
}
chrome.experimental.bluetooth.startDiscovery(
{ deviceCallback:recordDevice },
function() { sendReady(stopDiscoveryAndContinue); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment