summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkeybuk@chromium.org <keybuk@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-29 22:34:27 +0000
committerkeybuk@chromium.org <keybuk@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-29 22:34:27 +0000
commit5db73a293a2358c29d690bd24f8eceb6b4d9404d (patch)
treeafc55dddae030ee801c8fb923c68bb811f77da1e
parent7cb1648b5a3f1c4ff999c869ff2b568152d5af15 (diff)
downloadchromium_src-5db73a293a2358c29d690bd24f8eceb6b4d9404d.zip
chromium_src-5db73a293a2358c29d690bd24f8eceb6b4d9404d.tar.gz
chromium_src-5db73a293a2358c29d690bd24f8eceb6b4d9404d.tar.bz2
Bluetooth: fix issues with discovery API
Correct problems with the BluetoothAdapter discovery API. Split the single SetDiscovering() method into StartDiscovering() and StopDiscovering(), the API requires that the underlying implementation count the calls and only changes the adapter state on the first caller to StartDiscovering() and last caller to StopDiscovering(). BlueZ, used within Chromium OS, behaves in this manner. Make IsDiscovering() return true if the adapter is performing either step of discovery, move its previous meaning to a new IsScanning() method and accompanying AdapterScanningChanged() observer method. BUG=chromium-os:31117 TEST=device_unittests Review URL: https://codereview.chromium.org/11859018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@179442 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--ash/system/bluetooth/tray_bluetooth.cc28
-rw-r--r--ash/system/tray/system_tray_delegate.h7
-rw-r--r--ash/system/tray/test_system_tray_delegate.cc5
-rw-r--r--ash/system/tray/test_system_tray_delegate.h3
-rw-r--r--chrome/browser/chromeos/system/ash_system_tray_delegate.cc10
-rw-r--r--chrome/browser/extensions/api/bluetooth/bluetooth_api.cc20
-rw-r--r--chrome/browser/extensions/api/bluetooth/bluetooth_apitest.cc34
-rw-r--r--chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.cc33
-rw-r--r--chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h3
-rw-r--r--device/bluetooth/bluetooth_adapter.h48
-rw-r--r--device/bluetooth/bluetooth_adapter_chromeos.cc86
-rw-r--r--device/bluetooth/bluetooth_adapter_chromeos.h25
-rw-r--r--device/bluetooth/bluetooth_adapter_chromeos_unittest.cc882
-rw-r--r--device/bluetooth/bluetooth_adapter_win.cc14
-rw-r--r--device/bluetooth/bluetooth_adapter_win.h7
-rw-r--r--device/bluetooth/test/mock_bluetooth_adapter.h10
16 files changed, 1054 insertions, 161 deletions
diff --git a/ash/system/bluetooth/tray_bluetooth.cc b/ash/system/bluetooth/tray_bluetooth.cc
index fbe3e4b..67294a8 100644
--- a/ash/system/bluetooth/tray_bluetooth.cc
+++ b/ash/system/bluetooth/tray_bluetooth.cc
@@ -79,11 +79,11 @@ class BluetoothDetailedView : public TrayDetailsView,
virtual ~BluetoothDetailedView() {
// Stop discovering bluetooth devices when exiting BT detailed view.
- BluetoothSetDiscovering(false);
+ BluetoothStopDiscovering();
}
void Update() {
- BluetoothSetDiscovering(true);
+ BluetoothStartDiscovering();
UpdateBlueToothDeviceList();
// Update UI.
@@ -99,19 +99,21 @@ class BluetoothDetailedView : public TrayDetailsView,
AppendHeaderEntry();
}
- void BluetoothSetDiscovering(bool discovering) {
+ void BluetoothStartDiscovering() {
ash::SystemTrayDelegate* delegate =
ash::Shell::GetInstance()->system_tray_delegate();
- if (discovering) {
- bool bluetooth_enabled = delegate->GetBluetoothEnabled();
- if (!bluetooth_discovering_ && bluetooth_enabled)
- delegate->BluetoothSetDiscovering(true);
- bluetooth_discovering_ = bluetooth_enabled;
- } else { // Stop bluetooth discovering.
- if (bluetooth_discovering_) {
- bluetooth_discovering_ = false;
- delegate->BluetoothSetDiscovering(false);
- }
+ bool bluetooth_enabled = delegate->GetBluetoothEnabled();
+ if (!bluetooth_discovering_ && bluetooth_enabled)
+ delegate->BluetoothStartDiscovering();
+ bluetooth_discovering_ = bluetooth_enabled;
+ }
+
+ void BluetoothStopDiscovering() {
+ ash::SystemTrayDelegate* delegate =
+ ash::Shell::GetInstance()->system_tray_delegate();
+ if (bluetooth_discovering_) {
+ bluetooth_discovering_ = false;
+ delegate->BluetoothStopDiscovering();
}
}
diff --git a/ash/system/tray/system_tray_delegate.h b/ash/system/tray/system_tray_delegate.h
index 74187e1..f586b0b 100644
--- a/ash/system/tray/system_tray_delegate.h
+++ b/ash/system/tray/system_tray_delegate.h
@@ -189,8 +189,11 @@ class SystemTrayDelegate {
// Returns a list of available bluetooth devices.
virtual void GetAvailableBluetoothDevices(BluetoothDeviceList* devices) = 0;
- // Requests bluetooth start or stop discovering devices.
- virtual void BluetoothSetDiscovering(bool value) = 0;
+ // Requests bluetooth start discovering devices.
+ virtual void BluetoothStartDiscovering() = 0;
+
+ // Requests bluetooth stop discovering devices.
+ virtual void BluetoothStopDiscovering() = 0;
// Toggles connection to a specific bluetooth device.
virtual void ToggleBluetoothConnection(const std::string& address) = 0;
diff --git a/ash/system/tray/test_system_tray_delegate.cc b/ash/system/tray/test_system_tray_delegate.cc
index 48aaa96..67ec9ab 100644
--- a/ash/system/tray/test_system_tray_delegate.cc
+++ b/ash/system/tray/test_system_tray_delegate.cc
@@ -168,7 +168,10 @@ void TestSystemTrayDelegate::GetAvailableBluetoothDevices(
BluetoothDeviceList* list) {
}
-void TestSystemTrayDelegate::BluetoothSetDiscovering(bool value) {
+void TestSystemTrayDelegate::BluetoothStartDiscovering() {
+}
+
+void TestSystemTrayDelegate::BluetoothStopDiscovering() {
}
void TestSystemTrayDelegate::ToggleBluetoothConnection(
diff --git a/ash/system/tray/test_system_tray_delegate.h b/ash/system/tray/test_system_tray_delegate.h
index 23648ee..7800ac8 100644
--- a/ash/system/tray/test_system_tray_delegate.h
+++ b/ash/system/tray/test_system_tray_delegate.h
@@ -51,7 +51,8 @@ class TestSystemTrayDelegate : public SystemTrayDelegate {
virtual void RequestLockScreen() OVERRIDE;
virtual void RequestRestart() OVERRIDE;
virtual void GetAvailableBluetoothDevices(BluetoothDeviceList* list) OVERRIDE;
- virtual void BluetoothSetDiscovering(bool value) OVERRIDE;
+ virtual void BluetoothStartDiscovering() OVERRIDE;
+ virtual void BluetoothStopDiscovering() OVERRIDE;
virtual void ToggleBluetoothConnection(const std::string& address) OVERRIDE;
virtual void GetCurrentIME(IMEInfo* info) OVERRIDE;
virtual void GetAvailableIMEList(IMEInfoList* list) OVERRIDE;
diff --git a/chrome/browser/chromeos/system/ash_system_tray_delegate.cc b/chrome/browser/chromeos/system/ash_system_tray_delegate.cc
index b1c7180..c42734d 100644
--- a/chrome/browser/chromeos/system/ash_system_tray_delegate.cc
+++ b/chrome/browser/chromeos/system/ash_system_tray_delegate.cc
@@ -500,8 +500,14 @@ class SystemTrayDelegate : public ash::SystemTrayDelegate,
}
}
- virtual void BluetoothSetDiscovering(bool value) OVERRIDE {
- bluetooth_adapter_->SetDiscovering(value,
+ virtual void BluetoothStartDiscovering() OVERRIDE {
+ bluetooth_adapter_->StartDiscovering(
+ base::Bind(&base::DoNothing),
+ base::Bind(&BluetoothSetDiscoveringError));
+ }
+
+ virtual void BluetoothStopDiscovering() OVERRIDE {
+ bluetooth_adapter_->StopDiscovering(
base::Bind(&base::DoNothing),
base::Bind(&BluetoothSetDiscoveringError));
}
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc
index 636a3f5..3514383 100644
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc
@@ -542,12 +542,12 @@ bool BluetoothGetLocalOutOfBandPairingDataFunction::DoWork(
}
void BluetoothStartDiscoveryFunction::OnSuccessCallback() {
- GetEventRouter(profile())->SetResponsibleForDiscovery(true);
SendResponse(true);
}
void BluetoothStartDiscoveryFunction::OnErrorCallback() {
SetError(kStartDiscoveryFailed);
+ GetEventRouter(profile())->SetResponsibleForDiscovery(false);
SendResponse(false);
}
@@ -555,16 +555,15 @@ bool BluetoothStartDiscoveryFunction::DoWork(
scoped_refptr<BluetoothAdapter> adapter) {
GetEventRouter(profile())->SetSendDiscoveryEvents(true);
- // If the adapter is already discovering, there is nothing else to do.
- if (adapter->IsDiscovering()) {
- SendResponse(true);
- return true;
+ // If this profile is already discovering devices, there should be nothing
+ // else to do.
+ if (!GetEventRouter(profile())->IsResponsibleForDiscovery()) {
+ GetEventRouter(profile())->SetResponsibleForDiscovery(true);
+ adapter->StartDiscovering(
+ base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this),
+ base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this));
}
- adapter->SetDiscovering(true,
- base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this),
- base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this));
-
return true;
}
@@ -574,6 +573,7 @@ void BluetoothStopDiscoveryFunction::OnSuccessCallback() {
void BluetoothStopDiscoveryFunction::OnErrorCallback() {
SetError(kStopDiscoveryFailed);
+ GetEventRouter(profile())->SetResponsibleForDiscovery(true);
SendResponse(false);
}
@@ -581,7 +581,7 @@ bool BluetoothStopDiscoveryFunction::DoWork(
scoped_refptr<BluetoothAdapter> adapter) {
GetEventRouter(profile())->SetSendDiscoveryEvents(false);
if (GetEventRouter(profile())->IsResponsibleForDiscovery()) {
- adapter->SetDiscovering(false,
+ adapter->StopDiscovering(
base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this),
base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this));
}
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_apitest.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_apitest.cc
index a245283..24cc060 100644
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_apitest.cc
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_apitest.cc
@@ -228,11 +228,9 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) {
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
// Try with a failure to start
- EXPECT_CALL(*mock_adapter_, IsDiscovering()).WillOnce(testing::Return(false));
EXPECT_CALL(*mock_adapter_,
- SetDiscovering(true,
- testing::_,
- testing::Truly(CallClosure)));
+ StartDiscovering(testing::_,
+ testing::Truly(CallClosure)));
scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function;
start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
std::string error(
@@ -241,11 +239,9 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
// Reset for a successful start
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
- EXPECT_CALL(*mock_adapter_, IsDiscovering()).WillOnce(testing::Return(false));
EXPECT_CALL(*mock_adapter_,
- SetDiscovering(true,
- testing::Truly(CallClosure),
- testing::_));
+ StartDiscovering(testing::Truly(CallClosure),
+ testing::_));
start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
(void)utils::RunFunctionAndReturnError(start_function, "[]", browser());
@@ -253,9 +249,8 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
// Reset to try stopping
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_,
- SetDiscovering(false,
- testing::Truly(CallClosure),
- testing::_));
+ StopDiscovering(testing::Truly(CallClosure),
+ testing::_));
scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function;
stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
(void)utils::RunFunctionAndReturnSingleResult(stop_function, "[]", browser());
@@ -263,20 +258,18 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
// Reset to try stopping with an error
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_,
- SetDiscovering(false,
- testing::_,
- testing::Truly(CallClosure)));
+ StopDiscovering(testing::_,
+ testing::Truly(CallClosure)));
stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
error = utils::RunFunctionAndReturnError(stop_function, "[]", browser());
ASSERT_TRUE(!error.empty());
}
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) {
- EXPECT_CALL(*mock_adapter_, IsDiscovering()).WillOnce(testing::Return(false));
EXPECT_CALL(*mock_adapter_,
- SetDiscovering(true, testing::Truly(CallClosure), testing::_));
+ StartDiscovering(testing::Truly(CallClosure), testing::_));
EXPECT_CALL(*mock_adapter_,
- SetDiscovering(false, testing::Truly(CallClosure), testing::_));
+ StopDiscovering(testing::Truly(CallClosure), testing::_));
ResultCatcher catcher;
catcher.RestrictToProfile(browser()->profile());
@@ -306,7 +299,7 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryInProgress) {
// Fake that the adapter is discovering
EXPECT_CALL(*mock_adapter_, IsDiscovering())
- .Times(2).WillRepeatedly(testing::Return(true));
+ .WillOnce(testing::Return(true));
event_router()->AdapterDiscoveringChanged(mock_adapter_, true);
// Cache a device before the extension starts discovering
@@ -315,6 +308,11 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryInProgress) {
ResultCatcher catcher;
catcher.RestrictToProfile(browser()->profile());
+ EXPECT_CALL(*mock_adapter_,
+ StartDiscovering(testing::Truly(CallClosure), testing::_));
+ EXPECT_CALL(*mock_adapter_,
+ StopDiscovering(testing::Truly(CallClosure), testing::_));
+
ExtensionTestMessageListener discovery_started("ready", true);
ASSERT_TRUE(LoadExtension(
test_data_dir_.AppendASCII("bluetooth/discovery_in_progress")));
diff --git a/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.cc b/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.cc
index 9d8732a..4f32052 100644
--- a/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.cc
+++ b/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.cc
@@ -51,10 +51,17 @@ const char kConfirmPasskey[] = "bluetoothConfirmPasskey";
namespace chromeos {
namespace options {
-BluetoothOptionsHandler::BluetoothOptionsHandler() : weak_ptr_factory_(this) {
+BluetoothOptionsHandler::BluetoothOptionsHandler() : discovering_(false),
+ weak_ptr_factory_(this) {
}
BluetoothOptionsHandler::~BluetoothOptionsHandler() {
+ if (discovering_) {
+ adapter_->StopDiscovering(
+ base::Bind(&base::DoNothing),
+ base::Bind(&base::DoNothing));
+ discovering_ = false;
+ }
if (adapter_.get())
adapter_->RemoveObserver(this);
}
@@ -223,11 +230,13 @@ void BluetoothOptionsHandler::EnableChangeError() {
void BluetoothOptionsHandler::FindDevicesCallback(
const ListValue* args) {
- adapter_->SetDiscovering(
- true,
- base::Bind(&base::DoNothing),
- base::Bind(&BluetoothOptionsHandler::FindDevicesError,
- weak_ptr_factory_.GetWeakPtr()));
+ if (!discovering_) {
+ discovering_ = true;
+ adapter_->StartDiscovering(
+ base::Bind(&base::DoNothing),
+ base::Bind(&BluetoothOptionsHandler::FindDevicesError,
+ weak_ptr_factory_.GetWeakPtr()));
+ }
}
void BluetoothOptionsHandler::FindDevicesError() {
@@ -366,11 +375,13 @@ void BluetoothOptionsHandler::ForgetError(const std::string& address) {
void BluetoothOptionsHandler::StopDiscoveryCallback(
const ListValue* args) {
- adapter_->SetDiscovering(
- false,
- base::Bind(&base::DoNothing),
- base::Bind(&BluetoothOptionsHandler::StopDiscoveryError,
- weak_ptr_factory_.GetWeakPtr()));
+ if (discovering_) {
+ adapter_->StopDiscovering(
+ base::Bind(&base::DoNothing),
+ base::Bind(&BluetoothOptionsHandler::StopDiscoveryError,
+ weak_ptr_factory_.GetWeakPtr()));
+ discovering_ = false;
+ }
}
void BluetoothOptionsHandler::StopDiscoveryError() {
diff --git a/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h b/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h
index d085ccd..7fe9121 100644
--- a/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h
+++ b/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h
@@ -191,6 +191,9 @@ class BluetoothOptionsHandler
// Default bluetooth adapter, used for all operations.
scoped_refptr<device::BluetoothAdapter> adapter_;
+ // True while performing device discovery.
+ bool discovering_;
+
// Weak pointer factory for generating 'this' pointers that might live longer
// than this object does.
base::WeakPtrFactory<BluetoothOptionsHandler> weak_ptr_factory_;
diff --git a/device/bluetooth/bluetooth_adapter.h b/device/bluetooth/bluetooth_adapter.h
index fd7c983..75c0606 100644
--- a/device/bluetooth/bluetooth_adapter.h
+++ b/device/bluetooth/bluetooth_adapter.h
@@ -45,12 +45,18 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> {
// Called when the discovering state of the adapter |adapter| changes,
// when |discovering| is true the adapter is seeking new devices, false
- // means it is not. Note that device discovery involves both states when
- // the adapter is seeking new devices and states when it is not because
- // it is interrogating the devices it found.
+ // means it is not.
virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
bool discovering) {}
+ // Called when the scanning discovery state of the adapter |adapter|
+ // changes during discovery. When |scanning| is true the adapter is
+ // actively scanning for new devices and when false it is contacting
+ // the devices found to update information; the adapter will repeatedly
+ // cycle between both states during discovery.
+ virtual void AdapterScanningChanged(BluetoothAdapter* adapter,
+ bool scanning) {}
+
// Called when a new device |device| is added to the adapter |adapter|,
// either because it has been discovered or a connection made. |device|
// should not be cached, instead copy its address.
@@ -111,18 +117,34 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> {
const base::Closure& callback,
const ErrorCallback& error_callback) = 0;
- // Indicates whether the adapter is currently discovering new devices,
- // note that a typical discovery process has phases of this being true
- // followed by phases of being false when the adapter interrogates the
- // devices found.
+ // Indicates whether the adapter is currently discovering new devices.
virtual bool IsDiscovering() const = 0;
- // Requests that the adapter either begin discovering new devices when
- // |discovering| is true, or cease any discovery when false. On success,
- // callback will be called. On failure, |error_callback| will be called.
- virtual void SetDiscovering(bool discovering,
- const base::Closure& callback,
- const ErrorCallback& error_callback) = 0;
+ // Indicates whether the adapter is currently scanning for new devices
+ // during discovery; when false the adapter is contacting the devices found
+ // to obtain information.
+ virtual bool IsScanning() const = 0;
+
+ // Requests that the adapter begin discovering new devices, code must
+ // always call this method if they require the adapter be in discovery
+ // and should not make it conditional on the value of IsDiscovering()
+ // as other adapter users may be making the same request. Code must also
+ // call StopDiscovering() when done. On success |callback| will be called,
+ // on failure |error_callback| will be called instead.
+ //
+ // Since discovery may already be in progress when this method is called,
+ // callers should retrieve the current set of discovered devices by calling
+ // GetDevices() and checking for those with IsVisible() as true.
+ virtual void StartDiscovering(const base::Closure& callback,
+ const ErrorCallback& error_callback) = 0;
+
+ // Requests that an earlier call to StartDiscovering() be cancelled; the
+ // adapter may not actually cease discovering devices if other callers
+ // have called StartDiscovering() and not yet called this method. On
+ // success |callback| will be called, on failure |error_callback| will be
+ // called instead.
+ virtual void StopDiscovering(const base::Closure& callback,
+ const ErrorCallback& error_callback) = 0;
// Requests the list of devices from the adapter, all are returned
// including those currently connected and those paired. Use the
diff --git a/device/bluetooth/bluetooth_adapter_chromeos.cc b/device/bluetooth/bluetooth_adapter_chromeos.cc
index badbcd0..e15598a 100644
--- a/device/bluetooth/bluetooth_adapter_chromeos.cc
+++ b/device/bluetooth/bluetooth_adapter_chromeos.cc
@@ -28,7 +28,8 @@ namespace chromeos {
BluetoothAdapterChromeOs::BluetoothAdapterChromeOs() : BluetoothAdapter(),
track_default_(false),
powered_(false),
- discovering_(false),
+ scanning_(false),
+ discovering_count_(0),
weak_ptr_factory_(this) {
DBusThreadManager::Get()->GetBluetoothManagerClient()->
AddObserver(this);
@@ -88,28 +89,37 @@ void BluetoothAdapterChromeOs::SetPowered(bool powered,
}
bool BluetoothAdapterChromeOs::IsDiscovering() const {
- return discovering_;
+ return discovering_count_ > 0;
}
-void BluetoothAdapterChromeOs::SetDiscovering(
- bool discovering,
+bool BluetoothAdapterChromeOs::IsScanning() const {
+ return scanning_;
+}
+
+void BluetoothAdapterChromeOs::StartDiscovering(
const base::Closure& callback,
const ErrorCallback& error_callback) {
- if (discovering) {
- DBusThreadManager::Get()->GetBluetoothAdapterClient()->
- StartDiscovery(object_path_,
- base::Bind(&BluetoothAdapterChromeOs::OnStartDiscovery,
- weak_ptr_factory_.GetWeakPtr(),
- callback,
- error_callback));
- } else {
- DBusThreadManager::Get()->GetBluetoothAdapterClient()->
- StopDiscovery(object_path_,
- base::Bind(&BluetoothAdapterChromeOs::OnStopDiscovery,
- weak_ptr_factory_.GetWeakPtr(),
- callback,
- error_callback));
- }
+ // BlueZ counts discovery sessions, and permits multiple sessions for a
+ // single connection, so issue a StartDiscovery() call for every use
+ // within Chromium for the right behavior.
+ DBusThreadManager::Get()->GetBluetoothAdapterClient()->
+ StartDiscovery(object_path_,
+ base::Bind(
+ &BluetoothAdapterChromeOs::OnStartDiscovery,
+ weak_ptr_factory_.GetWeakPtr(),
+ callback, error_callback));
+}
+
+void BluetoothAdapterChromeOs::StopDiscovering(
+ const base::Closure& callback,
+ const ErrorCallback& error_callback) {
+ // Inform BlueZ to stop one of our open discovery sessions.
+ DBusThreadManager::Get()->GetBluetoothAdapterClient()->
+ StopDiscovery(object_path_,
+ base::Bind(
+ &BluetoothAdapterChromeOs::OnStopDiscovery,
+ weak_ptr_factory_.GetWeakPtr(),
+ callback, error_callback));
}
void BluetoothAdapterChromeOs::ReadLocalOutOfBandPairingData(
@@ -241,14 +251,18 @@ void BluetoothAdapterChromeOs::OnStartDiscovery(
const dbus::ObjectPath& adapter_path,
bool success) {
if (success) {
- DVLOG(1) << object_path_.value() << ": started discovery.";
+ if (discovering_count_++ == 0) {
+ DVLOG(1) << object_path_.value() << ": started discovery.";
+
+ // Clear devices found in previous discovery attempts
+ ClearDiscoveredDevices();
+
+ FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
+ AdapterDiscoveringChanged(this, true));
+ }
- // Clear devices found in previous discovery attempts
- ClearDiscoveredDevices();
callback.Run();
} else {
- // TODO(keybuk): in future, don't run the callback if the error was just
- // that we were already discovering.
error_callback.Run();
}
}
@@ -259,24 +273,36 @@ void BluetoothAdapterChromeOs::OnStopDiscovery(
const dbus::ObjectPath& adapter_path,
bool success) {
if (success) {
- DVLOG(1) << object_path_.value() << ": stopped discovery.";
+ if (--discovering_count_ == 0) {
+ DVLOG(1) << object_path_.value() << ": stopped discovery.";
+
+ FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
+ AdapterDiscoveringChanged(this, false));
+ } else if (discovering_count_ < 0) {
+ LOG(WARNING) << adapter_path.value() << ": call to StopDiscovering "
+ << "without matching StartDiscovering.";
+ error_callback.Run();
+ return;
+ }
+
callback.Run();
+
// Leave found devices available for perusing.
} else {
- // TODO(keybuk): in future, don't run the callback if the error was just
- // that we weren't discovering.
error_callback.Run();
}
}
void BluetoothAdapterChromeOs::DiscoveringChanged(bool discovering) {
- if (discovering == discovering_)
+ // The BlueZ discovering property actually just indicates whether the
+ // device is in an inquiry scan, so update our scanning property.
+ if (discovering == scanning_)
return;
- discovering_ = discovering;
+ scanning_ = discovering;
FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- AdapterDiscoveringChanged(this, discovering_));
+ AdapterScanningChanged(this, scanning_));
}
void BluetoothAdapterChromeOs::OnReadLocalData(
diff --git a/device/bluetooth/bluetooth_adapter_chromeos.h b/device/bluetooth/bluetooth_adapter_chromeos.h
index 79108f9..61adcd9 100644
--- a/device/bluetooth/bluetooth_adapter_chromeos.h
+++ b/device/bluetooth/bluetooth_adapter_chromeos.h
@@ -50,8 +50,11 @@ class BluetoothAdapterChromeOs
const base::Closure& callback,
const ErrorCallback& error_callback) OVERRIDE;
virtual bool IsDiscovering() const OVERRIDE;
- virtual void SetDiscovering(
- bool discovering,
+ virtual bool IsScanning() const OVERRIDE;
+ virtual void StartDiscovering(
+ const base::Closure& callback,
+ const ErrorCallback& error_callback) OVERRIDE;
+ virtual void StopDiscovering(
const base::Closure& callback,
const ErrorCallback& error_callback) OVERRIDE;
virtual void ReadLocalOutOfBandPairingData(
@@ -113,19 +116,23 @@ class BluetoothAdapterChromeOs
// and directly using values obtained from properties.
void PoweredChanged(bool powered);
- // Called by dbus:: in response to the method calls send by SetDiscovering().
- // |callback| and |error_callback| are the callbacks passed to
- // SetDiscovering().
+ // Called by BluetoothAdapterClient in response to the method call sent
+ // by StartDiscovering(), |callback| and |error_callback| are the callbacks
+ // provided to that method.
void OnStartDiscovery(const base::Closure& callback,
const ErrorCallback& error_callback,
const dbus::ObjectPath& adapter_path,
bool success);
+
+ // Called by BluetoothAdapterClient in response to the method call sent
+ // by StopDiscovering(), |callback| and |error_callback| are the callbacks
+ // provided to that method.
void OnStopDiscovery(const base::Closure& callback,
const ErrorCallback& error_callback,
const dbus::ObjectPath& adapter_path,
bool success);
- // Updates the tracked state of the adapter's discovering state to
+ // Updates the tracked state of the adapter's scanning state to
// |discovering| and notifies observers. Called on receipt of a property
// changed signal, and directly using values obtained from properties.
void DiscoveringChanged(bool discovering);
@@ -219,7 +226,11 @@ class BluetoothAdapterChromeOs
// Tracked adapter state, cached locally so we only send change notifications
// to observers on a genuine change.
bool powered_;
- bool discovering_;
+ bool scanning_;
+
+ // Count of callers to StartDiscovering() and StopDiscovering(), used to
+ // provide the IsDiscovering() status.
+ int discovering_count_;
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
diff --git a/device/bluetooth/bluetooth_adapter_chromeos_unittest.cc b/device/bluetooth/bluetooth_adapter_chromeos_unittest.cc
index 04fe988..30cac19 100644
--- a/device/bluetooth/bluetooth_adapter_chromeos_unittest.cc
+++ b/device/bluetooth/bluetooth_adapter_chromeos_unittest.cc
@@ -36,7 +36,7 @@ class BluetoothAdapterChromeOsTest : public testing::Test {
mock_adapter_client_ =
mock_dbus_thread_manager->mock_bluetooth_adapter_client();
- set_callback_called_ = false;
+ callback_called_ = false;
error_callback_called_ = false;
}
@@ -45,8 +45,8 @@ class BluetoothAdapterChromeOsTest : public testing::Test {
DBusThreadManager::Shutdown();
}
- void SetCallback() {
- set_callback_called_ = true;
+ void Callback() {
+ callback_called_ = true;
}
void ErrorCallback() {
@@ -61,7 +61,7 @@ class BluetoothAdapterChromeOsTest : public testing::Test {
MockBluetoothManagerClient* mock_manager_client_;
MockBluetoothAdapterClient* mock_adapter_client_;
- bool set_callback_called_;
+ bool callback_called_;
bool error_callback_called_;
scoped_refptr<BluetoothAdapter> adapter_;
@@ -1031,7 +1031,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterSetPowered) {
.WillOnce(SaveArg<1>(&set_callback));
adapter_->SetPowered(true,
- base::Bind(&BluetoothAdapterChromeOsTest::SetCallback,
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
base::Unretained(this)));
@@ -1043,7 +1043,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterSetPowered) {
set_callback.Run(true);
- EXPECT_TRUE(set_callback_called_);
+ EXPECT_TRUE(callback_called_);
EXPECT_FALSE(error_callback_called_);
}
@@ -1080,7 +1080,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterSetPoweredError) {
.WillOnce(SaveArg<1>(&set_callback));
adapter_->SetPowered(true,
- base::Bind(&BluetoothAdapterChromeOsTest::SetCallback,
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
base::Unretained(this)));
@@ -1092,7 +1092,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterSetPoweredError) {
set_callback.Run(false);
- EXPECT_FALSE(set_callback_called_);
+ EXPECT_FALSE(callback_called_);
EXPECT_TRUE(error_callback_called_);
}
@@ -1125,7 +1125,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
adapter_callback.Run(adapter_path, true);
// Adapter should have the correct property value.
- EXPECT_FALSE(adapter_->IsDiscovering());
+ EXPECT_FALSE(adapter_->IsScanning());
}
TEST_F(BluetoothAdapterChromeOsTest,
@@ -1154,20 +1154,20 @@ TEST_F(BluetoothAdapterChromeOsTest,
EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
.WillRepeatedly(Return(&adapter_properties));
- // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ // BluetoothAdapter::Observer::AdapterScanningChanged will be called.
MockBluetoothAdapter::Observer adapter_observer;
adapter_->AddObserver(&adapter_observer);
EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter_.get(), true))
.Times(1);
- EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter_.get(), true))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(), true))
.Times(1);
adapter_callback.Run(adapter_path, true);
// Adapter should have the correct property value.
- EXPECT_TRUE(adapter_->IsDiscovering());
+ EXPECT_TRUE(adapter_->IsScanning());
}
TEST_F(BluetoothAdapterChromeOsTest,
@@ -1188,7 +1188,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
// Call the adapter callback;
// BluetoothAdapterClient::GetProperties will be called once to obtain
- // the property set but BluetoothAdapter::Observer::AdapterDiscoveringChanged
+ // the property set but BluetoothAdapter::Observer::AdapterScanningChanged
// should not yet be called.
MockBluetoothAdapterClient::Properties adapter_properties;
adapter_properties.discovering.ReplaceValue(true);
@@ -1199,23 +1199,23 @@ TEST_F(BluetoothAdapterChromeOsTest,
EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
.WillRepeatedly(Return(&adapter_properties));
- EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter_.get(), _))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(), _))
.Times(0);
adapter_callback.Run(adapter_path, true);
// Adapter should not yet have the property value.
- EXPECT_FALSE(adapter_->IsDiscovering());
+ EXPECT_FALSE(adapter_->IsScanning());
// Tell the adapter the address now,
// BluetoothAdapter::Observer::AdapterPresentChanged and
- // BluetoothAdapter::Observer::AdapterDiscoveringChanged now must be called.
+ // BluetoothAdapter::Observer::AdapterScanningChanged now must be called.
adapter_properties.address.ReplaceValue(adapter_address);
EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter_.get(), true))
.Times(1);
- EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter_.get(), true))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(), true))
.Times(1);
BluetoothAdapterChromeOs* adapter_chromeos =
@@ -1226,7 +1226,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
adapter_properties.address.name());
// Adapter should have the correct property value.
- EXPECT_TRUE(adapter_->IsDiscovering());
+ EXPECT_TRUE(adapter_->IsScanning());
}
TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterDiscoveringPropertyChanged) {
@@ -1257,14 +1257,14 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterDiscoveringPropertyChanged) {
adapter_callback.Run(adapter_path, true);
// Adapter should have the correct property value.
- EXPECT_FALSE(adapter_->IsDiscovering());
+ EXPECT_FALSE(adapter_->IsScanning());
// Report that the property has been changed;
- // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ // BluetoothAdapter::Observer::AdapterScanningChanged will be called.
MockBluetoothAdapter::Observer adapter_observer;
adapter_->AddObserver(&adapter_observer);
- EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter_.get(), true))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(), true))
.Times(1);
adapter_properties.discovering.ReplaceValue(true);
@@ -1277,7 +1277,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterDiscoveringPropertyChanged) {
adapter_properties.discovering.name());
// Adapter should have the new property values.
- EXPECT_TRUE(adapter_->IsDiscovering());
+ EXPECT_TRUE(adapter_->IsScanning());
}
TEST_F(BluetoothAdapterChromeOsTest,
@@ -1309,15 +1309,15 @@ TEST_F(BluetoothAdapterChromeOsTest,
adapter_callback.Run(adapter_path, true);
// Adapter should have the correct property value.
- EXPECT_TRUE(adapter_->IsDiscovering());
+ EXPECT_TRUE(adapter_->IsScanning());
// Report that the property has been changed, but don't change the value;
- // BluetoothAdapter::Observer::AdapterDiscoveringChanged should not be
+ // BluetoothAdapter::Observer::AdapterScanningChanged should not be
// called.
MockBluetoothAdapter::Observer adapter_observer;
adapter_->AddObserver(&adapter_observer);
- EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter_.get(), _))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(), _))
.Times(0);
BluetoothAdapterChromeOs* adapter_chromeos =
@@ -1328,7 +1328,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
adapter_properties.discovering.name());
// Adapter should still have the same property values.
- EXPECT_TRUE(adapter_->IsDiscovering());
+ EXPECT_TRUE(adapter_->IsScanning());
}
TEST_F(BluetoothAdapterChromeOsTest,
@@ -1349,7 +1349,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
// Call the adapter callback;
// BluetoothAdapterClient::GetProperties will be called once to obtain
- // the property set but BluetoothAdapter::Observer::AdapterDiscoveringChanged
+ // the property set but BluetoothAdapter::Observer::AdapterScanningChanged
// should not yet be called.
MockBluetoothAdapterClient::Properties adapter_properties;
@@ -1359,7 +1359,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
.WillRepeatedly(Return(&adapter_properties));
- EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter_.get(), _))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(), _))
.Times(0);
adapter_callback.Run(adapter_path, true);
@@ -1369,7 +1369,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
// the adapter so it is not present.
adapter_properties.discovering.ReplaceValue(true);
- EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter_.get(), _))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(), _))
.Times(0);
BluetoothAdapterChromeOs* adapter_chromeos =
@@ -1380,17 +1380,17 @@ TEST_F(BluetoothAdapterChromeOsTest,
adapter_properties.discovering.name());
// Adapter should not yet have the property value.
- EXPECT_FALSE(adapter_->IsDiscovering());
+ EXPECT_FALSE(adapter_->IsScanning());
// Tell the adapter the address now,
// BluetoothAdapter::Observer::AdapterPresentChanged and
- // BluetoothAdapter::Observer::AdapterDiscoveringChanged now must be called.
+ // BluetoothAdapter::Observer::AdapterScanningChanged now must be called.
adapter_properties.address.ReplaceValue(adapter_address);
EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter_.get(), true))
.Times(1);
- EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter_.get(), true))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(), true))
.Times(1);
static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
@@ -1398,7 +1398,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
adapter_properties.address.name());
// Adapter should now have the correct property value.
- EXPECT_TRUE(adapter_->IsDiscovering());
+ EXPECT_TRUE(adapter_->IsScanning());
}
TEST_F(BluetoothAdapterChromeOsTest,
@@ -1440,7 +1440,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path))
.WillRepeatedly(Return(&new_adapter_properties));
- // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ // BluetoothAdapter::Observer::AdapterScanningChanged will be called.
MockBluetoothAdapter::Observer adapter_observer;
adapter_->AddObserver(&adapter_observer);
@@ -1453,8 +1453,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
.Times(1);
}
- EXPECT_CALL(adapter_observer,
- AdapterDiscoveringChanged(adapter_.get(), false))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(), false))
.Times(1);
BluetoothAdapterChromeOs* adapter_chromeos =
@@ -1464,7 +1463,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
->DefaultAdapterChanged(new_adapter_path);
// Adapter should have the new property value.
- EXPECT_FALSE(adapter_->IsDiscovering());
+ EXPECT_FALSE(adapter_->IsScanning());
}
TEST_F(BluetoothAdapterChromeOsTest,
@@ -1507,7 +1506,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path))
.WillRepeatedly(Return(&new_adapter_properties));
- // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called once
+ // BluetoothAdapter::Observer::AdapterScanningChanged will be called once
// to set the value to false for the previous adapter and once to set the
// value to true for the new adapter.
MockBluetoothAdapter::Observer adapter_observer;
@@ -1525,11 +1524,11 @@ TEST_F(BluetoothAdapterChromeOsTest,
{
InSequence s;
- EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter_.get(),
- false))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(),
+ false))
.Times(1);
- EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter_.get(),
- true))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(),
+ true))
.Times(1);
}
@@ -1540,7 +1539,7 @@ TEST_F(BluetoothAdapterChromeOsTest,
->DefaultAdapterChanged(new_adapter_path);
// Adapter should have the new property value.
- EXPECT_TRUE(adapter_->IsDiscovering());
+ EXPECT_TRUE(adapter_->IsScanning());
}
TEST_F(BluetoothAdapterChromeOsTest,
@@ -1572,14 +1571,13 @@ TEST_F(BluetoothAdapterChromeOsTest,
adapter_callback.Run(adapter_path, true);
// Report that the adapter has been removed;
- // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ // BluetoothAdapter::Observer::AdapterScanningChanged will be called.
MockBluetoothAdapter::Observer adapter_observer;
adapter_->AddObserver(&adapter_observer);
EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter_.get(), false))
.Times(1);
- EXPECT_CALL(adapter_observer,
- AdapterDiscoveringChanged(adapter_.get(), false))
+ EXPECT_CALL(adapter_observer, AdapterScanningChanged(adapter_.get(), false))
.Times(1);
BluetoothAdapterChromeOs* adapter_chromeos =
@@ -1589,6 +1587,798 @@ TEST_F(BluetoothAdapterChromeOsTest,
->AdapterRemoved(adapter_path);
// Adapter should have the new property value.
+ EXPECT_FALSE(adapter_->IsScanning());
+}
+
+TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterNotInitiallyDiscovering) {
+ const dbus::ObjectPath adapter_path("/fake/hci0");
+ const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
+
+ // Create the default adapter instance;
+ // BluetoothManagerClient::DefaultAdapter will be called once, passing
+ // a callback to obtain the adapter path.
+ BluetoothManagerClient::AdapterCallback adapter_callback;
+ EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
+ .WillOnce(SaveArg<0>(&adapter_callback));
+
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter,
+ base::Unretained(this)));
+
+ // Call the adapter callback;
+ // BluetoothAdapterClient::GetProperties will be called once to obtain
+ // the property set.
+ MockBluetoothAdapterClient::Properties adapter_properties;
+ adapter_properties.address.ReplaceValue(adapter_address);
+
+ EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
+ .WillRepeatedly(Return(&adapter_properties));
+
+ adapter_callback.Run(adapter_path, true);
+
+ // Adapter should have the correct property value.
+ EXPECT_FALSE(adapter_->IsDiscovering());
+}
+
+TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterStartDiscovering) {
+ const dbus::ObjectPath adapter_path("/fake/hci0");
+ const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
+
+ // Create the default adapter instance;
+ // BluetoothManagerClient::DefaultAdapter will be called once, passing
+ // a callback to obtain the adapter path.
+ BluetoothManagerClient::AdapterCallback adapter_callback;
+ EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
+ .WillOnce(SaveArg<0>(&adapter_callback));
+
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter,
+ base::Unretained(this)));
+
+ // Call the adapter callback;
+ // BluetoothAdapterClient::GetProperties will be called once to obtain
+ // the property set.
+ MockBluetoothAdapterClient::Properties adapter_properties;
+ adapter_properties.address.ReplaceValue(adapter_address);
+
+ EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
+ .WillRepeatedly(Return(&adapter_properties));
+
+ adapter_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices;
+ // BluetoothAdapterClient::StartDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback start_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ MockBluetoothAdapter::Observer adapter_observer;
+ adapter_->AddObserver(&adapter_observer);
+
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), true))
+ .Times(1);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ EXPECT_TRUE(callback_called_);
+ EXPECT_FALSE(error_callback_called_);
+
+ // Adapter should have the correct property value.
+ EXPECT_TRUE(adapter_->IsDiscovering());
+}
+
+TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterStartDiscoveringError) {
+ const dbus::ObjectPath adapter_path("/fake/hci0");
+ const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
+
+ // Create the default adapter instance;
+ // BluetoothManagerClient::DefaultAdapter will be called once, passing
+ // a callback to obtain the adapter path.
+ BluetoothManagerClient::AdapterCallback adapter_callback;
+ EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
+ .WillOnce(SaveArg<0>(&adapter_callback));
+
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter,
+ base::Unretained(this)));
+
+ // Call the adapter callback;
+ // BluetoothAdapterClient::GetProperties will be called once to obtain
+ // the property set.
+ MockBluetoothAdapterClient::Properties adapter_properties;
+ adapter_properties.address.ReplaceValue(adapter_address);
+
+ EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
+ .WillRepeatedly(Return(&adapter_properties));
+
+ adapter_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices;
+ // BluetoothAdapterClient::StartDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback start_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate failure,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged will not be called.
+ MockBluetoothAdapter::Observer adapter_observer;
+ adapter_->AddObserver(&adapter_observer);
+
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), _))
+ .Times(0);
+
+ start_discovery_callback.Run(adapter_path, false);
+
+ EXPECT_FALSE(callback_called_);
+ EXPECT_TRUE(error_callback_called_);
+
+ // Adapter should have the correct property value.
+ EXPECT_FALSE(adapter_->IsDiscovering());
+}
+
+TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterSecondStartDiscovering) {
+ const dbus::ObjectPath adapter_path("/fake/hci0");
+ const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
+
+ // Create the default adapter instance;
+ // BluetoothManagerClient::DefaultAdapter will be called once, passing
+ // a callback to obtain the adapter path.
+ BluetoothManagerClient::AdapterCallback adapter_callback;
+ EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
+ .WillOnce(SaveArg<0>(&adapter_callback));
+
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter,
+ base::Unretained(this)));
+
+ // Call the adapter callback;
+ // BluetoothAdapterClient::GetProperties will be called once to obtain
+ // the property set.
+ MockBluetoothAdapterClient::Properties adapter_properties;
+ adapter_properties.address.ReplaceValue(adapter_address);
+
+ EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
+ .WillRepeatedly(Return(&adapter_properties));
+
+ adapter_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices;
+ // BluetoothAdapterClient::StartDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback start_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ MockBluetoothAdapter::Observer adapter_observer;
+ adapter_->AddObserver(&adapter_observer);
+
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), true))
+ .Times(1);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices again;
+ // BluetoothAdapterClient::StartDiscovery will be called again.
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success, we do not expect
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged to be called
+ // since it has not changed.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), _))
+ .Times(0);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ EXPECT_TRUE(callback_called_);
+ EXPECT_FALSE(error_callback_called_);
+
+ // Adapter should have the correct property value.
+ EXPECT_TRUE(adapter_->IsDiscovering());
+}
+
+TEST_F(BluetoothAdapterChromeOsTest,
+ DefaultAdapterSecondStartDiscoveringError) {
+ const dbus::ObjectPath adapter_path("/fake/hci0");
+ const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
+
+ // Create the default adapter instance;
+ // BluetoothManagerClient::DefaultAdapter will be called once, passing
+ // a callback to obtain the adapter path.
+ BluetoothManagerClient::AdapterCallback adapter_callback;
+ EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
+ .WillOnce(SaveArg<0>(&adapter_callback));
+
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter,
+ base::Unretained(this)));
+
+ // Call the adapter callback;
+ // BluetoothAdapterClient::GetProperties will be called once to obtain
+ // the property set.
+ MockBluetoothAdapterClient::Properties adapter_properties;
+ adapter_properties.address.ReplaceValue(adapter_address);
+
+ EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
+ .WillRepeatedly(Return(&adapter_properties));
+
+ adapter_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices;
+ // BluetoothAdapterClient::StartDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback start_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ MockBluetoothAdapter::Observer adapter_observer;
+ adapter_->AddObserver(&adapter_observer);
+
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), true))
+ .Times(1);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices again;
+ // BluetoothAdapterClient::StartDiscovery will be called again.
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate failure, we do not expect
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged to be called
+ // since it has not really changed.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), _))
+ .Times(0);
+
+ start_discovery_callback.Run(adapter_path, false);
+
+ EXPECT_FALSE(callback_called_);
+ EXPECT_TRUE(error_callback_called_);
+
+ // Adapter should have the correct property value.
+ EXPECT_TRUE(adapter_->IsDiscovering());
+}
+
+TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterStopDiscovering) {
+ const dbus::ObjectPath adapter_path("/fake/hci0");
+ const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
+
+ // Create the default adapter instance;
+ // BluetoothManagerClient::DefaultAdapter will be called once, passing
+ // a callback to obtain the adapter path.
+ BluetoothManagerClient::AdapterCallback adapter_callback;
+ EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
+ .WillOnce(SaveArg<0>(&adapter_callback));
+
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter,
+ base::Unretained(this)));
+
+ // Call the adapter callback;
+ // BluetoothAdapterClient::GetProperties will be called once to obtain
+ // the property set.
+ MockBluetoothAdapterClient::Properties adapter_properties;
+ adapter_properties.address.ReplaceValue(adapter_address);
+
+ EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
+ .WillRepeatedly(Return(&adapter_properties));
+
+ adapter_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices;
+ // BluetoothAdapterClient::StartDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback start_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ MockBluetoothAdapter::Observer adapter_observer;
+ adapter_->AddObserver(&adapter_observer);
+
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), true))
+ .Times(1);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ // Ask the adapter to stop discovering devices;
+ // BluetoothAdapterClient::StopDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback stop_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StopDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&stop_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StopDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged should be called.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), false))
+ .Times(1);
+
+ stop_discovery_callback.Run(adapter_path, true);
+
+ EXPECT_TRUE(callback_called_);
+ EXPECT_FALSE(error_callback_called_);
+
+ // Adapter should have the correct property value.
+ EXPECT_FALSE(adapter_->IsDiscovering());
+}
+
+TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterStopDiscoveringError) {
+ const dbus::ObjectPath adapter_path("/fake/hci0");
+ const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
+
+ // Create the default adapter instance;
+ // BluetoothManagerClient::DefaultAdapter will be called once, passing
+ // a callback to obtain the adapter path.
+ BluetoothManagerClient::AdapterCallback adapter_callback;
+ EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
+ .WillOnce(SaveArg<0>(&adapter_callback));
+
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter,
+ base::Unretained(this)));
+
+ // Call the adapter callback;
+ // BluetoothAdapterClient::GetProperties will be called once to obtain
+ // the property set.
+ MockBluetoothAdapterClient::Properties adapter_properties;
+ adapter_properties.address.ReplaceValue(adapter_address);
+
+ EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
+ .WillRepeatedly(Return(&adapter_properties));
+
+ adapter_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices;
+ // BluetoothAdapterClient::StartDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback start_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ MockBluetoothAdapter::Observer adapter_observer;
+ adapter_->AddObserver(&adapter_observer);
+
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), true))
+ .Times(1);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ // Ask the adapter to stop discovering devices;
+ // BluetoothAdapterClient::StopDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback stop_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StopDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&stop_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StopDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate failure,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged will not be called.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), _))
+ .Times(0);
+
+ stop_discovery_callback.Run(adapter_path, false);
+
+ EXPECT_FALSE(callback_called_);
+ EXPECT_TRUE(error_callback_called_);
+
+ // Adapter should have the correct property value.
+ EXPECT_TRUE(adapter_->IsDiscovering());
+}
+
+TEST_F(BluetoothAdapterChromeOsTest,
+ DefaultAdapterStopDiscoveringAfterSecondStart) {
+ const dbus::ObjectPath adapter_path("/fake/hci0");
+ const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
+
+ // Create the default adapter instance;
+ // BluetoothManagerClient::DefaultAdapter will be called once, passing
+ // a callback to obtain the adapter path.
+ BluetoothManagerClient::AdapterCallback adapter_callback;
+ EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
+ .WillOnce(SaveArg<0>(&adapter_callback));
+
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter,
+ base::Unretained(this)));
+
+ // Call the adapter callback;
+ // BluetoothAdapterClient::GetProperties will be called once to obtain
+ // the property set.
+ MockBluetoothAdapterClient::Properties adapter_properties;
+ adapter_properties.address.ReplaceValue(adapter_address);
+
+ EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
+ .WillRepeatedly(Return(&adapter_properties));
+
+ adapter_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices;
+ // BluetoothAdapterClient::StartDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback start_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ MockBluetoothAdapter::Observer adapter_observer;
+ adapter_->AddObserver(&adapter_observer);
+
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), true))
+ .Times(1);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices again;
+ // BluetoothAdapterClient::StartDiscovery will be called again.
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success, we do not expect
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged to be called
+ // since it has not changed.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), _))
+ .Times(0);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ // Ask the adapter to stop discovering devices;
+ // BluetoothAdapterClient::StopDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback stop_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StopDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&stop_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StopDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success, we do not expect
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged to be called
+ // since there is still an open session.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), _))
+ .Times(0);
+
+ stop_discovery_callback.Run(adapter_path, true);
+
+ EXPECT_TRUE(callback_called_);
+ EXPECT_FALSE(error_callback_called_);
+
+ // Adapter should have the correct property value.
+ EXPECT_TRUE(adapter_->IsDiscovering());
+}
+
+TEST_F(BluetoothAdapterChromeOsTest,
+ DefaultAdapterStopDiscoveringAfterSecondStartError) {
+ const dbus::ObjectPath adapter_path("/fake/hci0");
+ const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
+
+ // Create the default adapter instance;
+ // BluetoothManagerClient::DefaultAdapter will be called once, passing
+ // a callback to obtain the adapter path.
+ BluetoothManagerClient::AdapterCallback adapter_callback;
+ EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
+ .WillOnce(SaveArg<0>(&adapter_callback));
+
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter,
+ base::Unretained(this)));
+
+ // Call the adapter callback;
+ // BluetoothAdapterClient::GetProperties will be called once to obtain
+ // the property set.
+ MockBluetoothAdapterClient::Properties adapter_properties;
+ adapter_properties.address.ReplaceValue(adapter_address);
+
+ EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
+ .WillRepeatedly(Return(&adapter_properties));
+
+ adapter_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices;
+ // BluetoothAdapterClient::StartDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback start_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ MockBluetoothAdapter::Observer adapter_observer;
+ adapter_->AddObserver(&adapter_observer);
+
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), true))
+ .Times(1);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices again;
+ // BluetoothAdapterClient::StartDiscovery will be called again.
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate failure, we do not expect
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged to be called
+ // since it has not changed.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), _))
+ .Times(0);
+
+ start_discovery_callback.Run(adapter_path, false);
+
+ // Ask the adapter to stop discovering devices;
+ // BluetoothAdapterClient::StopDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback stop_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StopDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&stop_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StopDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged should be called.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), false))
+ .Times(1);
+
+ stop_discovery_callback.Run(adapter_path, true);
+
+ EXPECT_TRUE(callback_called_);
+ EXPECT_FALSE(error_callback_called_);
+
+ // Adapter should have the correct property value.
+ EXPECT_FALSE(adapter_->IsDiscovering());
+}
+
+TEST_F(BluetoothAdapterChromeOsTest,
+ DefaultAdapterSecondStopDiscoveringAfterSecondStart) {
+ const dbus::ObjectPath adapter_path("/fake/hci0");
+ const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
+
+ // Create the default adapter instance;
+ // BluetoothManagerClient::DefaultAdapter will be called once, passing
+ // a callback to obtain the adapter path.
+ BluetoothManagerClient::AdapterCallback adapter_callback;
+ EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
+ .WillOnce(SaveArg<0>(&adapter_callback));
+
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter,
+ base::Unretained(this)));
+
+ // Call the adapter callback;
+ // BluetoothAdapterClient::GetProperties will be called once to obtain
+ // the property set.
+ MockBluetoothAdapterClient::Properties adapter_properties;
+ adapter_properties.address.ReplaceValue(adapter_address);
+
+ EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
+ .WillRepeatedly(Return(&adapter_properties));
+
+ adapter_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices;
+ // BluetoothAdapterClient::StartDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback start_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
+ MockBluetoothAdapter::Observer adapter_observer;
+ adapter_->AddObserver(&adapter_observer);
+
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), true))
+ .Times(1);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ // Ask the adapter to start discovering devices again;
+ // BluetoothAdapterClient::StartDiscovery will be called again.
+ EXPECT_CALL(*mock_adapter_client_, StartDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&start_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StartDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success, we do not expect
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged to be called
+ // since it has not changed.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), _))
+ .Times(0);
+
+ start_discovery_callback.Run(adapter_path, true);
+
+ // Ask the adapter to stop discovering devices;
+ // BluetoothAdapterClient::StopDiscovery will be called.
+ BluetoothAdapterClient::AdapterCallback stop_discovery_callback;
+ EXPECT_CALL(*mock_adapter_client_, StopDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&stop_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StopDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success, we do not expect
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged to be called
+ // since there is still an open session.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), _))
+ .Times(0);
+
+ stop_discovery_callback.Run(adapter_path, true);
+
+ // Ask the adapter to stop discovering devices again;
+ // BluetoothAdapterClient::StopDiscovery will be called.
+ EXPECT_CALL(*mock_adapter_client_, StopDiscovery(adapter_path, _))
+ .WillOnce(SaveArg<1>(&stop_discovery_callback));
+
+ callback_called_ = false;
+ error_callback_called_ = false;
+
+ adapter_->StopDiscovering(
+ base::Bind(&BluetoothAdapterChromeOsTest::Callback,
+ base::Unretained(this)),
+ base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
+ base::Unretained(this)));
+
+ // After returning to indicate success,
+ // BluetoothAdapter::Observer::AdapterDiscoveringChanged should be called.
+ EXPECT_CALL(adapter_observer,
+ AdapterDiscoveringChanged(adapter_.get(), false))
+ .Times(1);
+
+ stop_discovery_callback.Run(adapter_path, true);
+
+ EXPECT_TRUE(callback_called_);
+ EXPECT_FALSE(error_callback_called_);
+
+ // Adapter should have the correct property value.
EXPECT_FALSE(adapter_->IsDiscovering());
}
diff --git a/device/bluetooth/bluetooth_adapter_win.cc b/device/bluetooth/bluetooth_adapter_win.cc
index 128ccb3..8678668 100644
--- a/device/bluetooth/bluetooth_adapter_win.cc
+++ b/device/bluetooth/bluetooth_adapter_win.cc
@@ -65,8 +65,18 @@ bool BluetoothAdapterWin::IsDiscovering() const {
return false;
}
-void BluetoothAdapterWin::SetDiscovering(
- bool discovering,
+bool BluetoothAdapterWin::IsScanning() const {
+ NOTIMPLEMENTED();
+ return false;
+}
+
+void BluetoothAdapterWin::StartDiscovering(
+ const base::Closure& callback,
+ const ErrorCallback& error_callback) {
+ NOTIMPLEMENTED();
+}
+
+void BluetoothAdapterWin::StopDiscovering(
const base::Closure& callback,
const ErrorCallback& error_callback) {
NOTIMPLEMENTED();
diff --git a/device/bluetooth/bluetooth_adapter_win.h b/device/bluetooth/bluetooth_adapter_win.h
index 286319d..0feb0f0 100644
--- a/device/bluetooth/bluetooth_adapter_win.h
+++ b/device/bluetooth/bluetooth_adapter_win.h
@@ -35,8 +35,11 @@ class BluetoothAdapterWin : public BluetoothAdapter,
const base::Closure& callback,
const ErrorCallback& error_callback) OVERRIDE;
virtual bool IsDiscovering() const OVERRIDE;
- virtual void SetDiscovering(
- bool discovering,
+ virtual bool IsScanning() const OVERRIDE;
+ virtual void StartDiscovering(
+ const base::Closure& callback,
+ const ErrorCallback& error_callback) OVERRIDE;
+ virtual void StopDiscovering(
const base::Closure& callback,
const ErrorCallback& error_callback) OVERRIDE;
virtual ConstDeviceList GetDevices() const OVERRIDE;
diff --git a/device/bluetooth/test/mock_bluetooth_adapter.h b/device/bluetooth/test/mock_bluetooth_adapter.h
index 3e3619e..7d76a2b 100644
--- a/device/bluetooth/test/mock_bluetooth_adapter.h
+++ b/device/bluetooth/test/mock_bluetooth_adapter.h
@@ -24,6 +24,7 @@ class MockBluetoothAdapter : public BluetoothAdapter {
MOCK_METHOD2(AdapterPresentChanged, void(BluetoothAdapter*, bool));
MOCK_METHOD2(AdapterPoweredChanged, void(BluetoothAdapter*, bool));
MOCK_METHOD2(AdapterDiscoveringChanged, void(BluetoothAdapter*, bool));
+ MOCK_METHOD2(AdapterScanningChanged, void(BluetoothAdapter*, bool));
MOCK_METHOD2(DeviceAdded, void(BluetoothAdapter*, BluetoothDevice*));
MOCK_METHOD2(DeviceChanged, void(BluetoothAdapter*, BluetoothDevice*));
MOCK_METHOD2(DeviceRemoved, void(BluetoothAdapter*, BluetoothDevice*));
@@ -41,9 +42,12 @@ class MockBluetoothAdapter : public BluetoothAdapter {
const base::Closure& callback,
const ErrorCallback& error_callback));
MOCK_CONST_METHOD0(IsDiscovering, bool());
- MOCK_METHOD3(SetDiscovering,
- void(bool discovering,
- const base::Closure& callback,
+ MOCK_CONST_METHOD0(IsScanning, bool());
+ MOCK_METHOD2(StartDiscovering,
+ void(const base::Closure& callback,
+ const ErrorCallback& error_callback));
+ MOCK_METHOD2(StopDiscovering,
+ void(const base::Closure& callback,
const ErrorCallback& error_callback));
MOCK_CONST_METHOD0(GetDevices, BluetoothAdapter::ConstDeviceList());
MOCK_METHOD1(GetDevice, BluetoothDevice*(const std::string& address));