Commit e4ae4862 authored by Phillis Tang's avatar Phillis Tang Committed by Commit Bot

Update BluetoothBlueZTest to stop using |BluetoothDiscoverySession::Stop|

This patch updates some usage of |BluetoothDiscoverySession::Stop|
in BluetoothBlueZTest to just destroy the session and removes some
tests that are irrelevant after we get rid of the callbacks for
|BluetoothDiscoverySession:Stop| params

Bug: 991682
Change-Id: Id90d60790e4bb8654eca49d4f98fe5ce09fd646e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2076422Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarOvidio de Jesús Ruiz-Henríquez <odejesush@chromium.org>
Commit-Queue: Phillis Tang <phillis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#746035}
parent efb25c77
...@@ -175,14 +175,6 @@ class BluetoothBlueZTest : public testing::Test { ...@@ -175,14 +175,6 @@ class BluetoothBlueZTest : public testing::Test {
base::NullCallback()); base::NullCallback());
#endif #endif
for (const auto& session : discovery_sessions_) {
if (!session->IsActive())
continue;
callback_count_ = 0;
session->Stop(GetCallback(), GetErrorCallback());
base::RunLoop().Run();
ASSERT_EQ(1, callback_count_);
}
discovery_sessions_.clear(); discovery_sessions_.clear();
adapter_.reset(); adapter_.reset();
bluez::BluezDBusManager::Shutdown(); bluez::BluezDBusManager::Shutdown();
...@@ -319,15 +311,11 @@ class BluetoothBlueZTest : public testing::Test { ...@@ -319,15 +311,11 @@ class BluetoothBlueZTest : public testing::Test {
ASSERT_TRUE(IsAdapterDiscovering()); ASSERT_TRUE(IsAdapterDiscovering());
while (!observer.device_removed_count() && while (!observer.device_removed_count() &&
observer.last_device_address() != address) observer.last_device_address() != address) {
base::RunLoop().Run(); base::RunLoop().Run();
discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); }
base::RunLoop().Run();
ASSERT_EQ(1, callback_count_);
ASSERT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_FALSE(IsAdapterDiscovering()); discovery_sessions_.clear();
} }
// Run a discovery phase so we have devices that can be paired with. // Run a discovery phase so we have devices that can be paired with.
...@@ -732,77 +720,18 @@ TEST_F(BluetoothBlueZTest, StopDiscovery) { ...@@ -732,77 +720,18 @@ TEST_F(BluetoothBlueZTest, StopDiscovery) {
ASSERT_EQ((size_t)1, discovery_sessions_.size()); ASSERT_EQ((size_t)1, discovery_sessions_.size());
ASSERT_TRUE(discovery_sessions_[0]->IsActive()); ASSERT_TRUE(discovery_sessions_[0]->IsActive());
base::RunLoop stop_loop;
// Install an observer; aside from the callback, expect the // Install an observer; aside from the callback, expect the
// AdapterDiscoveringChanged method to be called and no longer to be // AdapterDiscoveringChanged method to be called and no longer to be
// discovering, // discovering,
TestBluetoothAdapterObserver observer(adapter_); TestBluetoothAdapterObserver observer(adapter_);
discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); observer.RegisterDiscoveringChangedWatcher(stop_loop.QuitClosure());
base::RunLoop().Run(); discovery_sessions_.clear();
EXPECT_EQ(1, callback_count_); stop_loop.Run();
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(1, observer.discovering_changed_count());
EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(observer.last_discovering());
EXPECT_FALSE(IsAdapterDiscovering()); EXPECT_FALSE(IsAdapterDiscovering());
discovery_sessions_.clear();
callback_count_ = 0;
// Test that the Stop callbacks get called even if the
// BluetoothDiscoverySession objects gets deleted
adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
adapter_->StartDiscoverySession(
base::Bind(&BluetoothBlueZTest::DiscoverySessionCallback,
base::Unretained(this)),
GetErrorCallback());
base::RunLoop().Run();
EXPECT_EQ(2, callback_count_);
EXPECT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_TRUE(adapter_->IsPowered());
ASSERT_TRUE(IsAdapterDiscovering());
ASSERT_EQ((size_t)1, discovery_sessions_.size());
ASSERT_TRUE(discovery_sessions_[0]->IsActive());
discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
discovery_sessions_.clear();
base::RunLoop().Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
}
TEST_F(BluetoothBlueZTest, StopDiscoveryInProgress) {
GetAdapter();
adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
callback_count_ = 0;
adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
adapter_->StartDiscoverySession(
base::Bind(&BluetoothBlueZTest::DiscoverySessionCallback,
base::Unretained(this)),
GetErrorCallback());
base::RunLoop().Run();
EXPECT_EQ(2, callback_count_);
EXPECT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_TRUE(adapter_->IsPowered());
ASSERT_TRUE(IsAdapterDiscovering());
ASSERT_EQ((size_t)1, discovery_sessions_.size());
ASSERT_TRUE(discovery_sessions_[0]->IsActive());
// First call to Stop() should succeed.
discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
// Second call to Stop() while the first is in progress should fail.
discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
base::RunLoop().Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(1, error_callback_count_);
discovery_sessions_.clear();
callback_count_ = 0;
error_callback_count_ = 0;
} }
TEST_F(BluetoothBlueZTest, Discovery) { TEST_F(BluetoothBlueZTest, Discovery) {
...@@ -949,7 +878,6 @@ TEST_F(BluetoothBlueZTest, StopAndStartDiscoverySimultaneously) { ...@@ -949,7 +878,6 @@ TEST_F(BluetoothBlueZTest, StopAndStartDiscoverySimultaneously) {
EXPECT_TRUE(IsAdapterDiscovering()); EXPECT_TRUE(IsAdapterDiscovering());
ASSERT_EQ(1u, discovery_sessions_.size()); ASSERT_EQ(1u, discovery_sessions_.size());
base::RunLoop stop_loop;
// Register loop to watch for Discovery changes. // Register loop to watch for Discovery changes.
base::RunLoop discovering_changed_loop; base::RunLoop discovering_changed_loop;
...@@ -959,7 +887,6 @@ TEST_F(BluetoothBlueZTest, StopAndStartDiscoverySimultaneously) { ...@@ -959,7 +887,6 @@ TEST_F(BluetoothBlueZTest, StopAndStartDiscoverySimultaneously) {
if (discovery_changed_count == 1) { if (discovery_changed_count == 1) {
EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(observer.last_discovering());
EXPECT_EQ(2, observer.discovering_changed_count()); EXPECT_EQ(2, observer.discovering_changed_count());
EXPECT_EQ(2, callback_count_);
EXPECT_FALSE(IsAdapterDiscovering()); EXPECT_FALSE(IsAdapterDiscovering());
} }
...@@ -968,10 +895,8 @@ TEST_F(BluetoothBlueZTest, StopAndStartDiscoverySimultaneously) { ...@@ -968,10 +895,8 @@ TEST_F(BluetoothBlueZTest, StopAndStartDiscoverySimultaneously) {
discovering_changed_loop.Quit(); discovering_changed_loop.Quit();
} }
})); }));
discovery_sessions_[0]->Stop(
base::Bind(&BluetoothBlueZTest::CallbackWithClosure, discovery_sessions_.clear();
base::Unretained(this), stop_loop.QuitClosure()),
GetErrorCallback());
EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(1, observer.discovering_changed_count());
// Queue up start to ensure all still works properly with a // Queue up start to ensure all still works properly with a
...@@ -982,9 +907,6 @@ TEST_F(BluetoothBlueZTest, StopAndStartDiscoverySimultaneously) { ...@@ -982,9 +907,6 @@ TEST_F(BluetoothBlueZTest, StopAndStartDiscoverySimultaneously) {
base::Unretained(this), start_loop_2.QuitClosure()), base::Unretained(this), start_loop_2.QuitClosure()),
GetErrorCallback()); GetErrorCallback());
// Finish stop call.
stop_loop.Run();
// Run loop waiting for DiscoveryChanged to be called in the observer // Run loop waiting for DiscoveryChanged to be called in the observer
// twice(once from stop and once from start). // twice(once from stop and once from start).
discovering_changed_loop.Run(); discovering_changed_loop.Run();
...@@ -993,7 +915,7 @@ TEST_F(BluetoothBlueZTest, StopAndStartDiscoverySimultaneously) { ...@@ -993,7 +915,7 @@ TEST_F(BluetoothBlueZTest, StopAndStartDiscoverySimultaneously) {
start_loop_2.Run(); start_loop_2.Run();
EXPECT_EQ(3, observer.discovering_changed_count()); EXPECT_EQ(3, observer.discovering_changed_count());
EXPECT_EQ(3, callback_count_); EXPECT_EQ(2, callback_count_);
EXPECT_TRUE(IsAdapterDiscovering()); EXPECT_TRUE(IsAdapterDiscovering());
ASSERT_EQ(1, NumActiveDiscoverySessions()); ASSERT_EQ(1, NumActiveDiscoverySessions());
} }
...@@ -1037,20 +959,14 @@ TEST_F(BluetoothBlueZTest, MultipleDiscoverySessions) { ...@@ -1037,20 +959,14 @@ TEST_F(BluetoothBlueZTest, MultipleDiscoverySessions) {
// Request to stop discovery twice. // Request to stop discovery twice.
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback()); discovery_sessions_.erase(discovery_sessions_.begin());
} }
// The observer should have received no additional discovering changed events, // The observer should have received no additional discovering changed events,
// the success callback should have been called 2 times and the adapter should // and the adapter should still be discovering.
// still be discovering.
EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(1, observer.discovering_changed_count());
EXPECT_EQ(5, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(observer.last_discovering());
EXPECT_TRUE(IsAdapterDiscovering()); EXPECT_TRUE(IsAdapterDiscovering());
EXPECT_FALSE(discovery_sessions_[0]->IsActive()); EXPECT_TRUE(discovery_sessions_[0]->IsActive());
EXPECT_FALSE(discovery_sessions_[1]->IsActive());
EXPECT_TRUE(discovery_sessions_[2]->IsActive());
// Request device discovery 3 times. // Request device discovery 3 times.
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
...@@ -1061,44 +977,28 @@ TEST_F(BluetoothBlueZTest, MultipleDiscoverySessions) { ...@@ -1061,44 +977,28 @@ TEST_F(BluetoothBlueZTest, MultipleDiscoverySessions) {
} }
// The observer should have received no additional discovering changed events, // The observer should have received no additional discovering changed events,
// the success callback should have been called 3 times and the adapter should // the adapter should still be discovering.
// still be discovering.
EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(1, observer.discovering_changed_count());
EXPECT_EQ(8, callback_count_); EXPECT_EQ(6, callback_count_);
EXPECT_EQ(0, error_callback_count_); EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(observer.last_discovering());
EXPECT_TRUE(IsAdapterDiscovering()); EXPECT_TRUE(IsAdapterDiscovering());
ASSERT_EQ(6u, discovery_sessions_.size()); ASSERT_EQ(4u, discovery_sessions_.size());
// Request to stop discovery 4 times. // Request to stop discovery 4 times.
for (int i = 2; i < 6; i++) { base::RunLoop stop_loop;
discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback()); observer.RegisterDiscoveringChangedWatcher(stop_loop.QuitClosure());
// Run the callbacks to set up state for new requests.
base::RunLoop().Run();
}
for (int i = 0; i < 4; i++) {
discovery_sessions_.erase(discovery_sessions_.begin());
}
stop_loop.Run();
// The observer should have received the discovering changed event exactly // The observer should have received the discovering changed event exactly
// once, the success callback should have been called 4 times and the adapter // once, the adapter should no longer be discovering.
// should no longer be discovering.
EXPECT_EQ(2, observer.discovering_changed_count()); EXPECT_EQ(2, observer.discovering_changed_count());
EXPECT_EQ(12, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_FALSE(observer.last_discovering());
EXPECT_FALSE(IsAdapterDiscovering());
// 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(GetCallback(), GetErrorCallback());
// The call should have failed.
EXPECT_EQ(2, observer.discovering_changed_count());
EXPECT_EQ(12, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(observer.last_discovering());
EXPECT_FALSE(IsAdapterDiscovering()); EXPECT_FALSE(IsAdapterDiscovering());
EXPECT_TRUE(discovery_sessions_.empty());
} }
// This unit test asserts that the reference counting logic works correctly in // This unit test asserts that the reference counting logic works correctly in
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment