Index: device/bluetooth/bluetooth_task_manager_win_unittest.cc |
diff --git a/device/bluetooth/bluetooth_task_manager_win_unittest.cc b/device/bluetooth/bluetooth_task_manager_win_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9b31742a0c923532bccfb6be6b26b3bc650c3fda |
--- /dev/null |
+++ b/device/bluetooth/bluetooth_task_manager_win_unittest.cc |
@@ -0,0 +1,140 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/memory/ref_counted.h" |
+#include "base/message_loop.h" |
+#include "base/run_loop.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "device/bluetooth/bluetooth_task_manager_win.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+class BluetoothTaskObserver : public device::BluetoothTaskManagerWin::Observer { |
+ public: |
+ BluetoothTaskObserver() : num_updates_(0) { |
+ } |
+ |
+ virtual ~BluetoothTaskObserver() { |
+ } |
+ |
+ virtual void AdapterStateChanged( |
+ const device::BluetoothTaskManagerWin::AdapterState& state) { |
+ num_updates_++; |
+ } |
+ |
+ int num_updates() const { |
+ return num_updates_; |
+ } |
+ |
+ private: |
+ int num_updates_; |
+}; |
+ |
+} // namespace |
+ |
+namespace device { |
+ |
+class BluetoothTaskManagerWinTest : public testing::Test { |
+ public: |
+ BluetoothTaskManagerWinTest() |
+ : ui_thread_(content::BrowserThread::UI, &ui_loop_), |
+ task_manager_(new BluetoothTaskManagerWin()) { |
+ } |
+ |
+ virtual void SetUp() { |
+ task_manager_->AddObserver(&observer_); |
+ } |
+ |
+ virtual void TearDown() { |
+ task_manager_->RemoveObserver(&observer_); |
+ } |
+ |
+ int GetPollingIntervalMs() const { |
+ return BluetoothTaskManagerWin::kPollIntervalMs; |
+ } |
+ |
+ // It posts |PollAdapter()| directly to |bluetooth_task_runner_| to bypass |
+ // system-specific |HasBluetoothStack()| check. |
+ void StartPolling() { |
+ task_manager_->bluetooth_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&BluetoothTaskManagerWin::PollAdapter, task_manager_)); |
+ } |
+ |
+ void Shutdown() { |
+ task_manager_->Shutdown(); |
+ } |
+ |
+ protected: |
+ MessageLoopForUI ui_loop_; |
+ content::TestBrowserThread ui_thread_; |
+ scoped_refptr<BluetoothTaskManagerWin> task_manager_; |
+ BluetoothTaskObserver observer_; |
+}; |
+ |
+TEST_F(BluetoothTaskManagerWinTest, NotifyObserver) { |
+ StartPolling(); |
+ base::RunLoop run_loop; |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ run_loop.QuitClosure(), |
+ base::TimeDelta::FromMilliseconds(GetPollingIntervalMs())); |
+ |
+ run_loop.Run(); |
+ Shutdown(); |
+ EXPECT_TRUE(observer_.num_updates() > 0); |
+} |
+ |
+TEST_F(BluetoothTaskManagerWinTest, Polling) { |
+ int expected_num_updates = 3; |
+ StartPolling(); |
+ base::RunLoop run_loop; |
+ |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ run_loop.QuitClosure(), |
+ base::TimeDelta::FromMilliseconds( |
+ GetPollingIntervalMs() * expected_num_updates)); |
+ |
+ run_loop.Run(); |
+ Shutdown(); |
+ EXPECT_EQ(expected_num_updates, observer_.num_updates()); |
+} |
+ |
+TEST_F(BluetoothTaskManagerWinTest, Shutdown) { |
+ StartPolling(); |
+ Shutdown(); |
+ base::RunLoop run_loop; |
+ |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ run_loop.QuitClosure(), |
+ base::TimeDelta::FromMilliseconds(GetPollingIntervalMs() * 3)); |
+ |
+ run_loop.Run(); |
+ EXPECT_EQ(1, observer_.num_updates()); |
+} |
+ |
+TEST_F(BluetoothTaskManagerWinTest, PollAndStop) { |
+ int num_polls = 4; |
+ int num_tasks = 6; |
+ StartPolling(); |
+ base::RunLoop run_loop; |
+ |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind(&BluetoothTaskManagerWinTest::Shutdown, |
+ base::Unretained(this)), |
+ base::TimeDelta::FromMilliseconds(GetPollingIntervalMs() * num_polls)); |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ run_loop.QuitClosure(), |
+ base::TimeDelta::FromMilliseconds(GetPollingIntervalMs() * num_tasks)); |
+ |
+ run_loop.Run(); |
+ EXPECT_EQ(num_polls, observer_.num_updates()); |
+} |
+ |
+} // namespace device |