Index: device/bluetooth/bluetooth_polling_thread_win_unittest.cc |
diff --git a/device/bluetooth/bluetooth_polling_thread_win_unittest.cc b/device/bluetooth/bluetooth_polling_thread_win_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..62d0b5fcdb089d13e2bdd051647a557cf567aab6 |
--- /dev/null |
+++ b/device/bluetooth/bluetooth_polling_thread_win_unittest.cc |
@@ -0,0 +1,125 @@ |
+// 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/memory/scoped_ptr.h" |
+#include "base/message_loop.h" |
+#include "base/run_loop.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "device/bluetooth/bluetooth_polling_thread_win.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 BluetoothPollingThreadWinTest : public testing::Test { |
+ public: |
+ BluetoothPollingThreadWinTest() |
+ : ui_thread_(content::BrowserThread::UI, &ui_loop_), |
+ task_manager_(new BluetoothTaskManagerWin()) { |
+ } |
+ |
+ virtual void SetUp() { |
+ observer_.reset(new BluetoothTaskObserver()); |
+ task_manager_->AddObserver(observer_.get()); |
+ thread_.reset(new BluetoothPollingThreadWin(task_manager_)); |
+ } |
+ |
+ virtual void TearDown() { |
+ task_manager_->RemoveObserver(observer_.get()); |
+ } |
+ |
+ int GetPollingIntervalMs() const { |
+ return BluetoothPollingThreadWin::kPollIntervalMs; |
+ } |
+ |
+ void StopThread() { |
+ thread_->Cancel(); |
+ } |
+ |
+protected: |
+ MessageLoopForUI ui_loop_; |
+ content::TestBrowserThread ui_thread_; |
+ scoped_refptr<BluetoothTaskManagerWin> task_manager_; |
+ scoped_ptr<BluetoothTaskObserver> observer_; |
+ scoped_ptr<BluetoothPollingThreadWin> thread_; |
+}; |
+ |
+TEST_F(BluetoothPollingThreadWinTest, Polling) { |
+ int expected_num_updates = 3; |
+ thread_->StartPolling(); |
+ base::RunLoop run_loop; |
+ |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ run_loop.QuitClosure(), |
+ base::TimeDelta::FromMilliseconds( |
+ GetPollingIntervalMs() * expected_num_updates)); |
+ |
+ run_loop.Run(); |
+ StopThread(); |
+ EXPECT_EQ(expected_num_updates, observer_->num_updates()); |
+} |
+ |
+TEST_F(BluetoothPollingThreadWinTest, StopThread) { |
+ thread_->StartPolling(); |
+ StopThread(); |
+ base::RunLoop run_loop; |
+ |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ run_loop.QuitClosure(), |
+ base::TimeDelta::FromMilliseconds(GetPollingIntervalMs() * 3)); |
+ |
+ run_loop.Run(); |
+ EXPECT_EQ(0, observer_->num_updates()); |
+} |
+ |
+TEST_F(BluetoothPollingThreadWinTest, PollAndStop) { |
+ int num_polls = 4; |
+ int num_tasks = 6; |
+ thread_->StartPolling(); |
+ base::RunLoop run_loop; |
+ |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind( |
+ &BluetoothPollingThreadWinTest::StopThread, |
+ 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 |