diff options
author | armansito@chromium.org <armansito@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-09 02:43:24 +0000 |
---|---|---|
committer | armansito@chromium.org <armansito@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-09 02:43:24 +0000 |
commit | eed36d56b5c0eaeb74b64ecb568ca473d857d27f (patch) | |
tree | ac77166d4bff3fc5b0135470b1b94653dc7f428e /device | |
parent | ef59a1c16f4f3bc4937f1f6891d5d2e02d746660 (diff) | |
download | chromium_src-eed36d56b5c0eaeb74b64ecb568ca473d857d27f.zip chromium_src-eed36d56b5c0eaeb74b64ecb568ca473d857d27f.tar.gz chromium_src-eed36d56b5c0eaeb74b64ecb568ca473d857d27f.tar.bz2 |
device/bluetooth: Remove BluetoothAdapter::Start|StopDiscovering.
This CL removes all remaining references to the deprecated
device::BluetoothAdapter::StartDiscovering and
device::BluetoothAdapter::StopDiscovering methods. This CL also does
a minor refactor, so that BluetoothAdapter now only tracks active
discovery sessions.
BUG=346985
TEST=Compile chrome; run device_unittests.
Review URL: https://codereview.chromium.org/188473002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255809 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'device')
-rw-r--r-- | device/bluetooth/bluetooth_adapter.cc | 22 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter.h | 53 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_unittest.cc | 19 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_win_unittest.cc | 72 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_chromeos_unittest.cc | 367 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_discovery_session.cc | 8 | ||||
-rw-r--r-- | device/bluetooth/test/mock_bluetooth_adapter.h | 6 |
7 files changed, 297 insertions, 250 deletions
diff --git a/device/bluetooth/bluetooth_adapter.cc b/device/bluetooth/bluetooth_adapter.cc index 2868159..ffa27ef 100644 --- a/device/bluetooth/bluetooth_adapter.cc +++ b/device/bluetooth/bluetooth_adapter.cc @@ -29,16 +29,6 @@ void BluetoothAdapter::StartDiscoverySession( error_callback); } -void BluetoothAdapter::StartDiscovering(const base::Closure& callback, - const ErrorCallback& error_callback) { - AddDiscoverySession(callback, error_callback); -} - -void BluetoothAdapter::StopDiscovering(const base::Closure& callback, - const ErrorCallback& error_callback) { - RemoveDiscoverySession(callback, error_callback); -} - BluetoothAdapter::DeviceList BluetoothAdapter::GetDevices() { ConstDeviceList const_devices = const_cast<const BluetoothAdapter *>(this)->GetDevices(); @@ -119,15 +109,21 @@ void BluetoothAdapter::OnStartDiscoverySession( } void BluetoothAdapter::MarkDiscoverySessionsAsInactive() { + // As sessions are marked as inactive they will notify the adapter that they + // have become inactive, upon which the adapter will remove them from + // |discovery_sessions_|. To avoid invalidating the iterator, make a copy + // here. + std::set<BluetoothDiscoverySession*> temp(discovery_sessions_); for (std::set<BluetoothDiscoverySession*>::iterator - iter = discovery_sessions_.begin(); - iter != discovery_sessions_.end(); ++iter) { + iter = temp.begin(); + iter != temp.end(); ++iter) { (*iter)->MarkAsInactive(); } } -void BluetoothAdapter::DiscoverySessionDestroyed( +void BluetoothAdapter::DiscoverySessionBecameInactive( BluetoothDiscoverySession* discovery_session) { + DCHECK(!discovery_session->IsActive()); discovery_sessions_.erase(discovery_session); } diff --git a/device/bluetooth/bluetooth_adapter.h b/device/bluetooth/bluetooth_adapter.h index badcccd..e713ff4 100644 --- a/device/bluetooth/bluetooth_adapter.h +++ b/device/bluetooth/bluetooth_adapter.h @@ -145,34 +145,22 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { // The returned BluetoothDiscoverySession is owned by the caller and it's the // owner's responsibility to properly clean it up and stop the session when // device discovery is no longer needed. + // + // If clients desire device discovery to run, they should always call this + // method and never make it conditional on the value of IsDiscovering(), as + // another client might cause discovery to stop unexpectedly. Hence, clients + // should always obtain a BluetoothDiscoverySession and call + // BluetoothDiscoverySession::Stop when done. When this method gets called, + // device discovery may actually be in progress. Clients can call GetDevices() + // and check for those with IsPaired() as false to obtain the list of devices + // that have been discovered so far. Otherwise, clients can be notified of all + // new and lost devices can by implementing the Observer methods "DeviceAdded" + // and "DeviceRemoved". typedef base::Callback<void(scoped_ptr<BluetoothDiscoverySession>)> DiscoverySessionCallback; virtual void StartDiscoverySession(const DiscoverySessionCallback& callback, const ErrorCallback& error_callback); - // DEPRECATED: Use StartDiscoverySession instead. - // 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 IsPaired() as false. - virtual void StartDiscovering(const base::Closure& callback, - const ErrorCallback& error_callback); - - // DEPRECATED: Use BluetoothDiscoverySession::Stop instead. - // 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); - // Requests the list of devices from the adapter, all are returned // including those currently connected and those paired. Use the // returned device pointers to determine which they are. @@ -275,13 +263,10 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { void MarkDiscoverySessionsAsInactive(); // Removes |discovery_session| from |discovery_sessions_|, if its in there. - // Called by DiscoverySession when an instance is destroyed. - void DiscoverySessionDestroyed(BluetoothDiscoverySession* discovery_session); - - // List of all DiscoverySession objects. We keep raw pointers, with the - // assumption that a DiscoverySession will remove itself from this list when - // it gets destroyed. - std::set<BluetoothDiscoverySession*> discovery_sessions_; + // Called by DiscoverySession when an instance is destroyed or becomes + // inactive. + void DiscoverySessionBecameInactive( + BluetoothDiscoverySession* discovery_session); // Devices paired with, connected to, discovered by, or visible to the // adapter. The key is the Bluetooth address of the device and the value @@ -296,6 +281,14 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { std::list<PairingDelegatePair> pairing_delegates_; private: + // List of active DiscoverySession objects. This is used to notify sessions to + // become inactive in case of an unexpected change to the adapter discovery + // state. We keep raw pointers, with the invariant that a DiscoverySession + // will remove itself from this list when it gets destroyed or becomes + // inactive by calling DiscoverySessionBecameInactive(), hence no pointers to + // deallocated sessions are kept. + std::set<BluetoothDiscoverySession*> discovery_sessions_; + // Note: This should remain the last member so it'll be destroyed and // invalidate its weak pointers before any other members are destroyed. base::WeakPtrFactory<BluetoothAdapter> weak_ptr_factory_; diff --git a/device/bluetooth/bluetooth_adapter_unittest.cc b/device/bluetooth/bluetooth_adapter_unittest.cc index f4653c2..aac32da 100644 --- a/device/bluetooth/bluetooth_adapter_unittest.cc +++ b/device/bluetooth/bluetooth_adapter_unittest.cc @@ -69,16 +69,19 @@ class TestBluetoothAdapter : public BluetoothAdapter { return false; } - virtual void StartDiscovering( - const base::Closure& callback, + virtual void StartDiscoverySession( + const DiscoverySessionCallback& callback, const ErrorCallback& error_callback) OVERRIDE { } - virtual void StopDiscovering( - const base::Closure& callback, + virtual void ReadLocalOutOfBandPairingData( + const BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& callback, const ErrorCallback& error_callback) OVERRIDE { } + protected: + virtual ~TestBluetoothAdapter() {} + virtual void AddDiscoverySession( const base::Closure& callback, const ErrorCallback& error_callback) OVERRIDE { @@ -89,14 +92,6 @@ class TestBluetoothAdapter : public BluetoothAdapter { const ErrorCallback& error_callback) OVERRIDE { } - virtual void ReadLocalOutOfBandPairingData( - const BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& callback, - const ErrorCallback& error_callback) OVERRIDE { - } - - protected: - virtual ~TestBluetoothAdapter() {} - virtual void RemovePairingDelegateInternal( BluetoothDevice::PairingDelegate* pairing_delegate) OVERRIDE { } diff --git a/device/bluetooth/bluetooth_adapter_win_unittest.cc b/device/bluetooth/bluetooth_adapter_win_unittest.cc index 4ea7a1e..9a9a23d 100644 --- a/device/bluetooth/bluetooth_adapter_win_unittest.cc +++ b/device/bluetooth/bluetooth_adapter_win_unittest.cc @@ -131,6 +131,18 @@ class BluetoothAdapterWinTest : public testing::Test { num_stop_discovery_error_callbacks_++; } + void CallAddDiscoverySession( + const base::Closure& callback, + const BluetoothAdapter::ErrorCallback& error_callback) { + adapter_win_->AddDiscoverySession(callback, error_callback); + } + + void CallRemoveDiscoverySession( + const base::Closure& callback, + const BluetoothAdapter::ErrorCallback& error_callback) { + adapter_win_->RemoveDiscoverySession(callback, error_callback); + } + protected: scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_; @@ -194,7 +206,7 @@ TEST_F(BluetoothAdapterWinTest, AdapterInitialized) { TEST_F(BluetoothAdapterWinTest, SingleStartDiscovery) { bluetooth_task_runner_->ClearPendingTasks(); - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); @@ -210,7 +222,7 @@ TEST_F(BluetoothAdapterWinTest, SingleStartDiscovery) { } TEST_F(BluetoothAdapterWinTest, SingleStartDiscoveryFailure) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), base::Bind( &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks, @@ -226,7 +238,7 @@ TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveries) { bluetooth_task_runner_->ClearPendingTasks(); int num_discoveries = 5; for (int i = 0; i < num_discoveries; i++) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Bind( &BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, base::Unretained(this)), @@ -246,7 +258,7 @@ TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveries) { TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesFailure) { int num_discoveries = 5; for (int i = 0; i < num_discoveries; i++) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), base::Bind( &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks, @@ -260,7 +272,7 @@ TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesFailure) { } TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesAfterDiscovering) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); @@ -272,7 +284,7 @@ TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesAfterDiscovering) { bluetooth_task_runner_->ClearPendingTasks(); for (int i = 0; i < 5; i++) { int num_start_discovery_callbacks = num_start_discovery_callbacks_; - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Bind( &BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, base::Unretained(this)), @@ -287,7 +299,7 @@ TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesAfterDiscovering) { } TEST_F(BluetoothAdapterWinTest, StartDiscoveryAfterDiscoveringFailure) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), base::Bind( &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks, @@ -297,7 +309,7 @@ TEST_F(BluetoothAdapterWinTest, StartDiscoveryAfterDiscoveringFailure) { EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_EQ(1, num_start_discovery_error_callbacks_); - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); @@ -308,11 +320,11 @@ TEST_F(BluetoothAdapterWinTest, StartDiscoveryAfterDiscoveringFailure) { } TEST_F(BluetoothAdapterWinTest, SingleStopDiscovery) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), BluetoothAdapter::ErrorCallback()); adapter_win_->DiscoveryStarted(true); ui_task_runner_->ClearPendingTasks(); - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); @@ -330,14 +342,14 @@ TEST_F(BluetoothAdapterWinTest, SingleStopDiscovery) { TEST_F(BluetoothAdapterWinTest, MultipleStopDiscoveries) { int num_discoveries = 5; for (int i = 0; i < num_discoveries; i++) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), BluetoothAdapter::ErrorCallback()); } adapter_win_->DiscoveryStarted(true); ui_task_runner_->ClearPendingTasks(); bluetooth_task_runner_->ClearPendingTasks(); for (int i = 0; i < num_discoveries - 1; i++) { - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); @@ -345,7 +357,7 @@ TEST_F(BluetoothAdapterWinTest, MultipleStopDiscoveries) { ui_task_runner_->RunPendingTasks(); EXPECT_EQ(i + 1, num_stop_discovery_callbacks_); } - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); @@ -360,23 +372,23 @@ TEST_F(BluetoothAdapterWinTest, MultipleStopDiscoveries) { TEST_F(BluetoothAdapterWinTest, StartDiscoveryAndStartDiscoveryAndStopDiscoveries) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); adapter_win_->DiscoveryStarted(true); - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); ui_task_runner_->ClearPendingTasks(); bluetooth_task_runner_->ClearPendingTasks(); - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty()); - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); @@ -385,27 +397,27 @@ TEST_F(BluetoothAdapterWinTest, TEST_F(BluetoothAdapterWinTest, StartDiscoveryAndStopDiscoveryAndStartDiscovery) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), BluetoothAdapter::ErrorCallback()); adapter_win_->DiscoveryStarted(true); EXPECT_TRUE(adapter_->IsDiscovering()); - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Closure(), BluetoothAdapter::ErrorCallback()); adapter_win_->DiscoveryStopped(); EXPECT_FALSE(adapter_->IsDiscovering()); - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), BluetoothAdapter::ErrorCallback()); adapter_win_->DiscoveryStarted(true); EXPECT_TRUE(adapter_->IsDiscovering()); } TEST_F(BluetoothAdapterWinTest, StartDiscoveryBeforeDiscoveryStopped) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), BluetoothAdapter::ErrorCallback()); adapter_win_->DiscoveryStarted(true); - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Closure(), BluetoothAdapter::ErrorCallback()); - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), BluetoothAdapter::ErrorCallback()); bluetooth_task_runner_->ClearPendingTasks(); adapter_win_->DiscoveryStopped(); @@ -413,7 +425,7 @@ TEST_F(BluetoothAdapterWinTest, StartDiscoveryBeforeDiscoveryStopped) { } TEST_F(BluetoothAdapterWinTest, StopDiscoveryWithoutStartDiscovery) { - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Closure(), base::Bind( &BluetoothAdapterWinTest::IncrementNumStopDiscoveryErrorCallbacks, @@ -422,9 +434,9 @@ TEST_F(BluetoothAdapterWinTest, StopDiscoveryWithoutStartDiscovery) { } TEST_F(BluetoothAdapterWinTest, StopDiscoveryBeforeDiscoveryStarted) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), BluetoothAdapter::ErrorCallback()); - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Closure(), BluetoothAdapter::ErrorCallback()); bluetooth_task_runner_->ClearPendingTasks(); adapter_win_->DiscoveryStarted(true); @@ -435,14 +447,14 @@ TEST_F(BluetoothAdapterWinTest, StartAndStopBeforeDiscoveryStarted) { int num_expected_start_discoveries = 3; int num_expected_stop_discoveries = 2; for (int i = 0; i < num_expected_start_discoveries; i++) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Bind( &BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks, base::Unretained(this)), BluetoothAdapter::ErrorCallback()); } for (int i = 0; i < num_expected_stop_discoveries; i++) { - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Bind( &BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, base::Unretained(this)), @@ -457,12 +469,12 @@ TEST_F(BluetoothAdapterWinTest, StartAndStopBeforeDiscoveryStarted) { } TEST_F(BluetoothAdapterWinTest, StopDiscoveryBeforeDiscoveryStartedAndFailed) { - adapter_win_->StartDiscovering( + CallAddDiscoverySession( base::Closure(), base::Bind( &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks, base::Unretained(this))); - adapter_win_->StopDiscovering( + CallRemoveDiscoverySession( base::Bind( &BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks, base::Unretained(this)), diff --git a/device/bluetooth/bluetooth_chromeos_unittest.cc b/device/bluetooth/bluetooth_chromeos_unittest.cc index dba5cde..442f2f8 100644 --- a/device/bluetooth/bluetooth_chromeos_unittest.cc +++ b/device/bluetooth/bluetooth_chromeos_unittest.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/memory/scoped_vector.h" #include "base/message_loop/message_loop.h" #include "base/strings/utf_string_conversions.h" #include "chromeos/dbus/fake_bluetooth_adapter_client.h" @@ -43,6 +44,7 @@ class TestObserver : public BluetoothAdapter::Observer { last_device_(NULL), adapter_(adapter) { } + virtual ~TestObserver() {} virtual void AdapterPresentChanged(BluetoothAdapter* adapter, @@ -246,9 +248,15 @@ class BluetoothChromeOSTest : public testing::Test { } virtual void TearDown() { - if (last_discovery_session_.get() && last_discovery_session_->IsActive()) { + for (ScopedVector<BluetoothDiscoverySession>::iterator iter = + discovery_sessions_.begin(); + iter != discovery_sessions_.end(); + ++iter) { + BluetoothDiscoverySession* session = *iter; + if (!session->IsActive()) + continue; callback_count_ = 0; - last_discovery_session_->Stop( + session->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, @@ -256,7 +264,7 @@ class BluetoothChromeOSTest : public testing::Test { message_loop_.Run(); ASSERT_EQ(1, callback_count_); } - last_discovery_session_.reset(); + discovery_sessions_.clear(); adapter_ = NULL; DBusThreadManager::Shutdown(); } @@ -270,7 +278,7 @@ class BluetoothChromeOSTest : public testing::Test { void DiscoverySessionCallback( scoped_ptr<BluetoothDiscoverySession> discovery_session) { ++callback_count_; - last_discovery_session_ = discovery_session.Pass(); + discovery_sessions_.push_back(discovery_session.release()); QuitMessageLoop(); } @@ -317,14 +325,16 @@ class BluetoothChromeOSTest : public testing::Test { base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); base::MessageLoop::current()->Run(); ASSERT_EQ(2, callback_count_); ASSERT_EQ(0, error_callback_count_); + ASSERT_EQ((size_t)1, discovery_sessions_.size()); + ASSERT_TRUE(discovery_sessions_[0]->IsActive()); callback_count_ = 0; ASSERT_TRUE(adapter_->IsPowered()); @@ -334,7 +344,7 @@ class BluetoothChromeOSTest : public testing::Test { observer.last_device_address_ != address) base::MessageLoop::current()->Run(); - adapter_->StopDiscovering( + discovery_sessions_[0]->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, @@ -366,7 +376,7 @@ class BluetoothChromeOSTest : public testing::Test { int error_callback_count_; enum BluetoothDevice::ConnectErrorCode last_connect_error_; std::string last_client_error_; - scoped_ptr<BluetoothDiscoverySession> last_discovery_session_; + ScopedVector<BluetoothDiscoverySession> discovery_sessions_; private: // Some tests use a message loop since background processing is simulated; @@ -640,8 +650,8 @@ TEST_F(BluetoothChromeOSTest, StopDiscovery) { base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); @@ -652,6 +662,8 @@ TEST_F(BluetoothChromeOSTest, StopDiscovery) { ASSERT_TRUE(adapter_->IsPowered()); ASSERT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)1, discovery_sessions_.size()); + ASSERT_TRUE(discovery_sessions_[0]->IsActive()); // Install an observer; aside from the callback, expect the // AdapterDiscoveringChanged method to be called and no longer to be @@ -659,80 +671,7 @@ TEST_F(BluetoothChromeOSTest, StopDiscovery) { TestObserver observer(adapter_); adapter_->AddObserver(&observer); - adapter_->StopDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, - base::Unretained(this)), - base::Bind(&BluetoothChromeOSTest::ErrorCallback, - base::Unretained(this))); - message_loop_.Run(); - EXPECT_EQ(1, callback_count_); - EXPECT_EQ(0, error_callback_count_); - - EXPECT_EQ(1, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); - - EXPECT_FALSE(adapter_->IsDiscovering()); -} - -TEST_F(BluetoothChromeOSTest, StopDiscoveryAfterTwoStarts) { - GetAdapter(); - - adapter_->SetPowered( - true, - base::Bind(&BluetoothChromeOSTest::Callback, - base::Unretained(this)), - base::Bind(&BluetoothChromeOSTest::ErrorCallback, - base::Unretained(this))); - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, - base::Unretained(this)), - base::Bind(&BluetoothChromeOSTest::ErrorCallback, - base::Unretained(this))); - message_loop_.Run(); - EXPECT_EQ(2, callback_count_); - EXPECT_EQ(0, error_callback_count_); - callback_count_ = 0; - - ASSERT_TRUE(adapter_->IsPowered()); - ASSERT_TRUE(adapter_->IsDiscovering()); - - // Install an observer and start discovering again; only the callback - // should be called since we were already discovering to begin with. - TestObserver observer(adapter_); - adapter_->AddObserver(&observer); - - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, - base::Unretained(this)), - base::Bind(&BluetoothChromeOSTest::ErrorCallback, - base::Unretained(this))); - message_loop_.Run(); - EXPECT_EQ(1, callback_count_); - EXPECT_EQ(0, error_callback_count_); - callback_count_ = 0; - - EXPECT_EQ(0, observer.discovering_changed_count_); - - // Stop discovering; only the callback should be called since we're still - // discovering. The adapter should be still discovering. - adapter_->StopDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, - base::Unretained(this)), - base::Bind(&BluetoothChromeOSTest::ErrorCallback, - base::Unretained(this))); - message_loop_.Run(); - EXPECT_EQ(1, callback_count_); - EXPECT_EQ(0, error_callback_count_); - callback_count_ = 0; - - EXPECT_EQ(0, observer.discovering_changed_count_); - - EXPECT_TRUE(adapter_->IsDiscovering()); - - // Stop discovering one more time; aside from the callback, expect the - // AdapterDiscoveringChanged method to be called and no longer to be - // discovering, - adapter_->StopDiscovering( + discovery_sessions_[0]->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, @@ -761,8 +700,8 @@ TEST_F(BluetoothChromeOSTest, Discovery) { base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); @@ -773,6 +712,8 @@ TEST_F(BluetoothChromeOSTest, Discovery) { ASSERT_TRUE(adapter_->IsPowered()); ASSERT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)1, discovery_sessions_.size()); + ASSERT_TRUE(discovery_sessions_[0]->IsActive()); // First device to appear. message_loop_.Run(); @@ -802,8 +743,8 @@ TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) { base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); @@ -811,6 +752,8 @@ TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) { EXPECT_EQ(2, callback_count_); EXPECT_EQ(0, error_callback_count_); callback_count_ = 0; + ASSERT_EQ((size_t)1, discovery_sessions_.size()); + ASSERT_TRUE(discovery_sessions_[0]->IsActive()); // Stop the timers that the simulation uses fake_bluetooth_device_client_->EndDiscoverySimulation( @@ -821,6 +764,7 @@ TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) { fake_bluetooth_adapter_client_->SetVisible(false); ASSERT_FALSE(adapter_->IsPresent()); + ASSERT_FALSE(discovery_sessions_[0]->IsActive()); // Install an observer; expect the AdapterPresentChanged, // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called @@ -888,8 +832,8 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { // Request device discovery 3 times. for (int i = 0; i < 3; i++) { - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); @@ -905,10 +849,11 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)3, discovery_sessions_.size()); // Request to stop discovery twice. for (int i = 0; i < 2; i++) { - adapter_->StopDiscovering( + discovery_sessions_[i]->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, @@ -923,11 +868,15 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + EXPECT_TRUE(adapter_->IsDiscovering()); + EXPECT_FALSE(discovery_sessions_[0]->IsActive()); + EXPECT_FALSE(discovery_sessions_[1]->IsActive()); + EXPECT_TRUE(discovery_sessions_[2]->IsActive()); // Request device discovery 3 times. for (int i = 0; i < 3; i++) { - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); @@ -941,10 +890,11 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)6, discovery_sessions_.size()); // Request to stop discovery 4 times. - for (int i = 0; i < 4; i++) { - adapter_->StopDiscovering( + for (int i = 2; i < 6; i++) { + discovery_sessions_[i]->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, @@ -962,8 +912,12 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { EXPECT_FALSE(observer.last_discovering_); EXPECT_FALSE(adapter_->IsDiscovering()); - // Request to stop discovery once. - adapter_->StopDiscovering( + // All discovery sessions should be inactive. + for (int i = 0; i < 6; i++) + EXPECT_FALSE(discovery_sessions_[i]->IsActive()); + + // Request to stop discovery on of the inactive sessions. + discovery_sessions_[0]->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, @@ -1003,8 +957,8 @@ TEST_F(BluetoothChromeOSTest, // Request device discovery 3 times. for (int i = 0; i < 3; i++) { - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); @@ -1020,6 +974,10 @@ TEST_F(BluetoothChromeOSTest, EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)3, discovery_sessions_.size()); + + for (int i = 0; i < 3; i++) + EXPECT_TRUE(discovery_sessions_[i]->IsActive()); // Stop the timers that the simulation uses fake_bluetooth_device_client_->EndDiscoverySimulation( @@ -1031,7 +989,7 @@ TEST_F(BluetoothChromeOSTest, // Stop device discovery behind the adapter. The adapter and the observer // should be notified of the change and the reference count should be reset. // Even though FakeBluetoothAdapterClient does its own reference counting and - // we called 3 BluetoothAdapter::StartDiscovering 3 times, the + // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the // FakeBluetoothAdapterClient's count should be only 1 and a single call to // FakeBluetoothAdapterClient::StopDiscovery should work. fake_bluetooth_adapter_client_->StopDiscovery( @@ -1047,10 +1005,15 @@ TEST_F(BluetoothChromeOSTest, EXPECT_FALSE(observer.last_discovering_); EXPECT_FALSE(adapter_->IsDiscovering()); + // All discovery session instances should have been updated. + for (int i = 0; i < 3; i++) + EXPECT_FALSE(discovery_sessions_[i]->IsActive()); + discovery_sessions_.clear(); + // It should be possible to successfully start discovery. for (int i = 0; i < 2; i++) { - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); @@ -1062,6 +1025,10 @@ TEST_F(BluetoothChromeOSTest, EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)2, discovery_sessions_.size()); + + for (int i = 0; i < 2; i++) + EXPECT_TRUE(discovery_sessions_[i]->IsActive()); fake_bluetooth_device_client_->EndDiscoverySimulation( dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); @@ -1077,6 +1044,10 @@ TEST_F(BluetoothChromeOSTest, EXPECT_FALSE(observer.last_discovering_); EXPECT_FALSE(adapter_->IsDiscovering()); + for (int i = 0; i < 2; i++) + EXPECT_FALSE(discovery_sessions_[i]->IsActive()); + discovery_sessions_.clear(); + fake_bluetooth_adapter_client_->SetVisible(true); ASSERT_TRUE(adapter_->IsPresent()); EXPECT_EQ(5, observer.discovering_changed_count_); @@ -1089,8 +1060,8 @@ TEST_F(BluetoothChromeOSTest, // a reference count that is equal to 1. Pretend that this was done by an // application other than us. Starting and stopping discovery will succeed // but it won't cause the discovery state to change. - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); @@ -1100,8 +1071,10 @@ TEST_F(BluetoothChromeOSTest, EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)1, discovery_sessions_.size()); + EXPECT_TRUE(discovery_sessions_[0]->IsActive()); - adapter_->StopDiscovering( + discovery_sessions_[0]->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, @@ -1112,10 +1085,12 @@ TEST_F(BluetoothChromeOSTest, EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + EXPECT_FALSE(discovery_sessions_[0]->IsActive()); + discovery_sessions_.clear(); // Start discovery again. - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); @@ -1125,6 +1100,8 @@ TEST_F(BluetoothChromeOSTest, EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)1, discovery_sessions_.size()); + EXPECT_TRUE(discovery_sessions_[0]->IsActive()); // Stop discovery via D-Bus. The fake client's reference count will drop but // the discovery state won't change since our BluetoothAdapter also just @@ -1144,7 +1121,7 @@ TEST_F(BluetoothChromeOSTest, // Now end the discovery session. This should change the adapter's discovery // state. - adapter_->StopDiscovering( + discovery_sessions_[0]->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, @@ -1155,6 +1132,86 @@ TEST_F(BluetoothChromeOSTest, EXPECT_EQ(0, error_callback_count_); EXPECT_FALSE(observer.last_discovering_); EXPECT_FALSE(adapter_->IsDiscovering()); + EXPECT_FALSE(discovery_sessions_[0]->IsActive()); + + adapter_->RemoveObserver(&observer); +} + +TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) { + GetAdapter(); + adapter_->SetPowered( + true, + base::Bind(&BluetoothChromeOSTest::Callback, + base::Unretained(this)), + base::Bind(&BluetoothChromeOSTest::ErrorCallback, + base::Unretained(this))); + EXPECT_EQ(1, callback_count_); + EXPECT_EQ(0, error_callback_count_); + EXPECT_TRUE(adapter_->IsPowered()); + callback_count_ = 0; + + TestObserver observer(adapter_); + adapter_->AddObserver(&observer); + + EXPECT_EQ(0, observer.discovering_changed_count_); + EXPECT_FALSE(observer.last_discovering_); + EXPECT_FALSE(adapter_->IsDiscovering()); + + // Request device discovery 3 times. + for (int i = 0; i < 3; i++) { + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, + base::Unretained(this)), + base::Bind(&BluetoothChromeOSTest::ErrorCallback, + base::Unretained(this))); + } + // Run only once, as there should have been one D-Bus call. + message_loop_.Run(); + + // The observer should have received the discovering changed event exactly + // once, the success callback should have been called 3 times and the adapter + // should be discovering. + EXPECT_EQ(1, observer.discovering_changed_count_); + EXPECT_EQ(3, callback_count_); + EXPECT_EQ(0, error_callback_count_); + EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)3, discovery_sessions_.size()); + + for (int i = 0; i < 3; i++) + EXPECT_TRUE(discovery_sessions_[i]->IsActive()); + + // Stop the timers that the simulation uses + fake_bluetooth_device_client_->EndDiscoverySimulation( + dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)); + + ASSERT_TRUE(adapter_->IsPowered()); + ASSERT_TRUE(adapter_->IsDiscovering()); + + // Delete all but one discovery session. + discovery_sessions_.pop_back(); + discovery_sessions_.pop_back(); + ASSERT_EQ((size_t)1, discovery_sessions_.size()); + EXPECT_TRUE(discovery_sessions_[0]->IsActive()); + EXPECT_TRUE(adapter_->IsDiscovering()); + + // Stop device discovery behind the adapter. The one active discovery session + // should become inactive, but more importantly, we shouldn't run into any + // memory errors as the sessions that we explicitly deleted should get + // cleaned up. + fake_bluetooth_adapter_client_->StopDiscovery( + dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), + base::Bind(&BluetoothChromeOSTest::Callback, + base::Unretained(this)), + base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, + base::Unretained(this))); + message_loop_.Run(); + EXPECT_EQ(2, observer.discovering_changed_count_); + EXPECT_EQ(4, callback_count_); + EXPECT_EQ(0, error_callback_count_); + EXPECT_FALSE(observer.last_discovering_); + EXPECT_FALSE(adapter_->IsDiscovering()); + EXPECT_FALSE(discovery_sessions_[0]->IsActive()); } TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { @@ -1179,8 +1236,8 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { EXPECT_FALSE(adapter_->IsDiscovering()); // Request to start discovery. The call should be pending. - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); @@ -1194,98 +1251,100 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { EXPECT_EQ(1, observer.discovering_changed_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); - - // Try to stop discovery. This should fail while there is a call pending. - adapter_->StopDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, - base::Unretained(this)), - base::Bind(&BluetoothChromeOSTest::ErrorCallback, - base::Unretained(this))); - EXPECT_EQ(0, callback_count_); - EXPECT_EQ(1, error_callback_count_); - EXPECT_EQ(1, observer.discovering_changed_count_); - EXPECT_TRUE(observer.last_discovering_); - EXPECT_TRUE(adapter_->IsDiscovering()); + EXPECT_TRUE(discovery_sessions_.empty()); // Request to start discovery twice. These should get queued and there should // be no change in state. for (int i = 0; i < 2; i++) { - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); } EXPECT_EQ(0, callback_count_); - EXPECT_EQ(1, error_callback_count_); + EXPECT_EQ(0, error_callback_count_); EXPECT_EQ(1, observer.discovering_changed_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + EXPECT_TRUE(discovery_sessions_.empty()); // Process the pending call. The queued calls should execute and the discovery // session reference count should increase. message_loop_.Run(); EXPECT_EQ(3, callback_count_); - EXPECT_EQ(1, error_callback_count_); + EXPECT_EQ(0, error_callback_count_); EXPECT_EQ(1, observer.discovering_changed_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)3, discovery_sessions_.size()); // Verify the reference count by removing sessions 3 times. The last request // should remain pending. for (int i = 0; i < 3; i++) { - adapter_->StopDiscovering( + discovery_sessions_[i]->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); } EXPECT_EQ(5, callback_count_); - EXPECT_EQ(1, error_callback_count_); + EXPECT_EQ(0, error_callback_count_); EXPECT_EQ(2, observer.discovering_changed_count_); EXPECT_FALSE(observer.last_discovering_); EXPECT_FALSE(adapter_->IsDiscovering()); + EXPECT_FALSE(discovery_sessions_[0]->IsActive()); + EXPECT_FALSE(discovery_sessions_[1]->IsActive()); + EXPECT_TRUE(discovery_sessions_[2]->IsActive()); - // Request to stop should fail. - adapter_->StopDiscovering( + // Request to stop the session whose call is pending should fail. + discovery_sessions_[2]->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); EXPECT_EQ(5, callback_count_); - EXPECT_EQ(2, error_callback_count_); + EXPECT_EQ(1, error_callback_count_); EXPECT_EQ(2, observer.discovering_changed_count_); EXPECT_FALSE(observer.last_discovering_); EXPECT_FALSE(adapter_->IsDiscovering()); + EXPECT_TRUE(discovery_sessions_[2]->IsActive()); // Request to start should get queued. - adapter_->StartDiscovering( - base::Bind(&BluetoothChromeOSTest::Callback, + adapter_->StartDiscoverySession( + base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, base::Unretained(this))); EXPECT_EQ(5, callback_count_); - EXPECT_EQ(2, error_callback_count_); + EXPECT_EQ(1, error_callback_count_); EXPECT_EQ(2, observer.discovering_changed_count_); EXPECT_FALSE(observer.last_discovering_); EXPECT_FALSE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)3, discovery_sessions_.size()); // Run the pending request. message_loop_.Run(); EXPECT_EQ(6, callback_count_); - EXPECT_EQ(2, error_callback_count_); + EXPECT_EQ(1, error_callback_count_); EXPECT_EQ(3, observer.discovering_changed_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)3, discovery_sessions_.size()); + EXPECT_FALSE(discovery_sessions_[2]->IsActive()); // The queued request to start discovery should have been issued but is still // pending. Run the loop and verify. message_loop_.Run(); EXPECT_EQ(7, callback_count_); - EXPECT_EQ(2, error_callback_count_); + EXPECT_EQ(1, error_callback_count_); EXPECT_EQ(3, observer.discovering_changed_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); + ASSERT_EQ((size_t)4, discovery_sessions_.size()); + EXPECT_TRUE(discovery_sessions_[3]->IsActive()); + + adapter_->RemoveObserver(&observer); } TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { @@ -1308,7 +1367,7 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { EXPECT_EQ(0, observer.discovering_changed_count_); EXPECT_FALSE(observer.last_discovering_); EXPECT_FALSE(adapter_->IsDiscovering()); - EXPECT_FALSE(last_discovery_session_.get()); + EXPECT_TRUE(discovery_sessions_.empty()); // Request a new discovery session. adapter_->StartDiscoverySession( @@ -1322,8 +1381,8 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); - EXPECT_TRUE(last_discovery_session_.get()); - EXPECT_TRUE(last_discovery_session_->IsActive()); + ASSERT_EQ((size_t)1, discovery_sessions_.size()); + EXPECT_TRUE(discovery_sessions_[0]->IsActive()); // Start another session. A new one should be returned in the callback, which // in turn will destroy the previous session. Adapter should still be @@ -1339,13 +1398,8 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); - EXPECT_TRUE(last_discovery_session_.get()); - EXPECT_TRUE(last_discovery_session_->IsActive()); - - // Hold on to the current discovery session to prevent it from getting - // destroyed during the next call to StartDiscoverySession. - scoped_ptr<BluetoothDiscoverySession> discovery_session = - last_discovery_session_.Pass(); + ASSERT_EQ((size_t)2, discovery_sessions_.size()); + EXPECT_TRUE(discovery_sessions_[0]->IsActive()); // Request a new session. adapter_->StartDiscoverySession( @@ -1359,13 +1413,13 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); - EXPECT_TRUE(last_discovery_session_.get()); - EXPECT_TRUE(last_discovery_session_->IsActive()); - EXPECT_NE(last_discovery_session_.get(), discovery_session); + ASSERT_EQ((size_t)3, discovery_sessions_.size()); + EXPECT_TRUE(discovery_sessions_[1]->IsActive()); + EXPECT_NE(discovery_sessions_[0], discovery_sessions_[1]); // Stop the previous discovery session. The session should end but discovery // should continue. - discovery_session->Stop( + discovery_sessions_[0]->Stop( base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), base::Bind(&BluetoothChromeOSTest::ErrorCallback, @@ -1376,21 +1430,20 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(observer.last_discovering_); EXPECT_TRUE(adapter_->IsDiscovering()); - EXPECT_TRUE(last_discovery_session_.get()); - EXPECT_TRUE(last_discovery_session_->IsActive()); - EXPECT_FALSE(discovery_session->IsActive()); + ASSERT_EQ((size_t)3, discovery_sessions_.size()); + EXPECT_FALSE(discovery_sessions_[0]->IsActive()); + EXPECT_TRUE(discovery_sessions_[1]->IsActive()); // Delete the current active session. Discovery should eventually stop. - last_discovery_session_.reset(); - while (observer.last_discovering_) { + discovery_sessions_.clear(); + while (observer.last_discovering_) message_loop_.RunUntilIdle(); - } + EXPECT_EQ(2, observer.discovering_changed_count_); EXPECT_EQ(4, callback_count_); EXPECT_EQ(0, error_callback_count_); EXPECT_FALSE(observer.last_discovering_); EXPECT_FALSE(adapter_->IsDiscovering()); - EXPECT_FALSE(last_discovery_session_.get()); adapter_->RemoveObserver(&observer); } diff --git a/device/bluetooth/bluetooth_discovery_session.cc b/device/bluetooth/bluetooth_discovery_session.cc index 6818a3c..ba5f5fd 100644 --- a/device/bluetooth/bluetooth_discovery_session.cc +++ b/device/bluetooth/bluetooth_discovery_session.cc @@ -25,7 +25,7 @@ BluetoothDiscoverySession::~BluetoothDiscoverySession() { return; } Stop(base::Bind(&base::DoNothing), base::Bind(&base::DoNothing)); - adapter_->DiscoverySessionDestroyed(this); + MarkAsInactive(); } bool BluetoothDiscoverySession::IsActive() const { @@ -50,12 +50,16 @@ void BluetoothDiscoverySession::Stop( } void BluetoothDiscoverySession::OnStop(const base::Closure& callback) { - active_ = false; + MarkAsInactive(); callback.Run(); } void BluetoothDiscoverySession::MarkAsInactive() { + if (!active_) + return; active_ = false; + DCHECK(adapter_.get()); + adapter_->DiscoverySessionBecameInactive(this); } } // namespace device diff --git a/device/bluetooth/test/mock_bluetooth_adapter.h b/device/bluetooth/test/mock_bluetooth_adapter.h index 3a959e9..32f40d1 100644 --- a/device/bluetooth/test/mock_bluetooth_adapter.h +++ b/device/bluetooth/test/mock_bluetooth_adapter.h @@ -55,12 +55,6 @@ class MockBluetoothAdapter : public BluetoothAdapter { MOCK_METHOD2(StartDiscoverySession, void(const DiscoverySessionCallback& callback, const ErrorCallback& error_callback)); - 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)); MOCK_CONST_METHOD1(GetDevice, |