Index: device/bluetooth/bluetooth_adapter_win_unittest.cc |
diff --git a/device/bluetooth/bluetooth_adapter_win_unittest.cc b/device/bluetooth/bluetooth_adapter_win_unittest.cc |
index e8fcb6fcac13194e734d711228d2d01729354bc6..192d2531a7e9258bebcfcdcd20bd98c54fcb2653 100644 |
--- a/device/bluetooth/bluetooth_adapter_win_unittest.cc |
+++ b/device/bluetooth/bluetooth_adapter_win_unittest.cc |
@@ -5,20 +5,18 @@ |
#include <string> |
#include "base/memory/ref_counted.h" |
-#include "base/memory/scoped_ptr.h" |
-#include "base/memory/weak_ptr.h" |
#include "base/message_loop.h" |
#include "base/run_loop.h" |
#include "base/stringprintf.h" |
#include "base/time.h" |
#include "content/public/test/test_browser_thread.h" |
#include "device/bluetooth/bluetooth_adapter_win.h" |
-#include "device/bluetooth/test/mock_bluetooth_adapter.h" |
+#include "device/bluetooth/bluetooth_manager_win.h" |
#include "testing/gtest/include/gtest/gtest.h" |
namespace { |
-const char* kAdapterAddress = "bluetooth adapter address"; |
+const int kMaxPolling = 5; |
} // namespace |
@@ -28,34 +26,62 @@ class FakeBluetoothAdapterWin : public BluetoothAdapterWin { |
public: |
FakeBluetoothAdapterWin() : BluetoothAdapterWin() {} |
- virtual void UpdateAdapterState() { |
- address_ = adapter_address_; |
- } |
- |
- void SetAdapterAddressForTest(const std::string& adapter_address) { |
- adapter_address_ = adapter_address; |
+ HANDLE GetAdapterHandle() const { |
+ return adapter_handle_; |
} |
private: |
virtual ~FakeBluetoothAdapterWin() {} |
- std::string adapter_address_; |
+ |
+ // BluetoothAdapterWin override. |
+ virtual void UpdateAdapterState(HANDLE adapter_handle) OVERRIDE { |
+ adapter_handle_ = adapter_handle; |
+ } |
+ |
+ HANDLE adapter_handle_; |
+ |
DISALLOW_COPY_AND_ASSIGN(FakeBluetoothAdapterWin); |
}; |
+class FakeBluetoothManagerWin : public BluetoothManagerWin { |
+ public: |
+ FakeBluetoothManagerWin() : BluetoothManagerWin(), adapter_handle_(NULL) {} |
+ virtual ~FakeBluetoothManagerWin() { |
+ Stop(); |
+ } |
+ |
+ void SetAdapterHandleForTest(HANDLE adapter_handle) { |
+ adapter_handle_ = adapter_handle; |
+ } |
+ |
+ private: |
+ // BluetoothManagerWin override. |
+ virtual HANDLE FindAdapterHandle() OVERRIDE { |
+ return adapter_handle_; |
+ } |
+ |
+ HANDLE adapter_handle_; |
+}; |
+ |
class BluetoothAdapterWinTest : public testing::Test { |
public: |
BluetoothAdapterWinTest() |
- : ui_thread_(content::BrowserThread::UI, &loop_), |
- ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
+ : ui_thread_(content::BrowserThread::UI, &loop_) { |
} |
virtual void SetUp() { |
adapter_ = new FakeBluetoothAdapterWin(); |
+ manager_ = new FakeBluetoothManagerWin(); |
+ SetAdapterHandleForTest(0); |
+ |
+ // The ownership of manager_ is transferred to adapter_. |
+ adapter_->SetBluetoothManagerForTest(manager_); |
+ |
adapter_->TrackDefaultAdapter(); |
} |
- void SetAdapterAddressForTest() { |
- adapter_->SetAdapterAddressForTest(kAdapterAddress); |
+ void SetAdapterHandleForTest(int handle_index) { |
+ manager_->SetAdapterHandleForTest(&adapter_handles[handle_index]); |
} |
int GetPollIntervalMs() const { |
@@ -64,6 +90,7 @@ class BluetoothAdapterWinTest : public testing::Test { |
protected: |
scoped_refptr<FakeBluetoothAdapterWin> adapter_; |
+ FakeBluetoothManagerWin* manager_; |
// Main message loop for the test. |
MessageLoopForUI loop_; |
@@ -71,42 +98,52 @@ class BluetoothAdapterWinTest : public testing::Test { |
// Main thread for the test. |
content::TestBrowserThread ui_thread_; |
- // 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<BluetoothAdapterWinTest> weak_ptr_factory_; |
+ int adapter_handles[kMaxPolling]; |
}; |
-TEST_F(BluetoothAdapterWinTest, Polling) { |
+TEST_F(BluetoothAdapterWinTest, AdapterDestroyed) { |
+ adapter_.release(); |
+ |
+ base::RunLoop run_loop; |
MessageLoop::current()->PostDelayedTask( |
FROM_HERE, |
- base::Bind( |
- &BluetoothAdapterWinTest::SetAdapterAddressForTest, |
- weak_ptr_factory_.GetWeakPtr()), |
- base::TimeDelta::FromMilliseconds(GetPollIntervalMs() - 1)); |
- EXPECT_STRNE(kAdapterAddress, adapter_->address().c_str()); |
- base::RunLoop run_loop; |
+ run_loop.QuitClosure(), |
+ base::TimeDelta::FromMilliseconds(1)); |
+ run_loop.Run(); |
+ // BluetoothAdapterWin is destroyed successfully after terminating |
+ // BluetoothManagerWin thread. |
+ SUCCEED(); |
+} |
+ |
+TEST_F(BluetoothAdapterWinTest, AdapterHandleReceivedFromManager) { |
+ base::RunLoop run_loop; |
MessageLoop::current()->PostDelayedTask( |
FROM_HERE, |
run_loop.QuitClosure(), |
- base::TimeDelta::FromMilliseconds(GetPollIntervalMs() + 1)); |
- |
+ base::TimeDelta::FromMilliseconds(1)); |
run_loop.Run(); |
- EXPECT_STREQ(kAdapterAddress, adapter_->address().c_str()); |
+ EXPECT_EQ(&adapter_handles[0], adapter_->GetAdapterHandle()); |
} |
-TEST_F(BluetoothAdapterWinTest, IsPresent) { |
- EXPECT_FALSE(adapter_->IsPresent()); |
- SetAdapterAddressForTest(); |
+TEST_F(BluetoothAdapterWinTest, Polling) { |
+ for (int i = 0; i < kMaxPolling; i++) { |
+ manager_->message_loop()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind(&BluetoothAdapterWinTest::SetAdapterHandleForTest, |
+ base::Unretained(this), |
+ i), |
+ base::TimeDelta::FromMilliseconds(GetPollIntervalMs() * i + 1)); |
+ } |
base::RunLoop run_loop; |
MessageLoop::current()->PostDelayedTask( |
FROM_HERE, |
run_loop.QuitClosure(), |
- base::TimeDelta::FromMilliseconds(GetPollIntervalMs())); |
+ base::TimeDelta::FromMilliseconds(GetPollIntervalMs() * kMaxPolling + 1)); |
run_loop.Run(); |
- EXPECT_TRUE(adapter_->IsPresent()); |
+ EXPECT_EQ(&adapter_handles[kMaxPolling - 1], adapter_->GetAdapterHandle()); |
} |
} // namespace device |