OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "content/public/test/test_browser_thread.h" |
| 10 #include "device/bluetooth/bluetooth_polling_thread_win.h" |
| 11 #include "device/bluetooth/bluetooth_task_manager_win.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 class BluetoothTaskObserver : public device::BluetoothTaskManagerWin::Observer { |
| 17 public: |
| 18 BluetoothTaskObserver() : num_updates_(0) { |
| 19 } |
| 20 |
| 21 virtual ~BluetoothTaskObserver() { |
| 22 } |
| 23 |
| 24 virtual void AdapterStateChanged( |
| 25 const device::BluetoothTaskManagerWin::AdapterState& state) { |
| 26 num_updates_++; |
| 27 } |
| 28 |
| 29 int num_updates() const { |
| 30 return num_updates_; |
| 31 } |
| 32 |
| 33 private: |
| 34 int num_updates_; |
| 35 }; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 namespace device { |
| 40 |
| 41 class BluetoothPollingThreadWinTest : public testing::Test { |
| 42 public: |
| 43 BluetoothPollingThreadWinTest() |
| 44 : ui_thread_(content::BrowserThread::UI, &ui_loop_), |
| 45 task_manager_(new BluetoothTaskManagerWin()) { |
| 46 } |
| 47 |
| 48 virtual void SetUp() { |
| 49 observer_.reset(new BluetoothTaskObserver()); |
| 50 task_manager_->AddObserver(observer_.get()); |
| 51 thread_.reset(new BluetoothPollingThreadWin(task_manager_)); |
| 52 } |
| 53 |
| 54 virtual void TearDown() { |
| 55 task_manager_->RemoveObserver(observer_.get()); |
| 56 } |
| 57 |
| 58 int GetPollingIntervalMs() const { |
| 59 return BluetoothPollingThreadWin::kPollIntervalMs; |
| 60 } |
| 61 |
| 62 void StopThread() { |
| 63 thread_->Cancel(); |
| 64 } |
| 65 |
| 66 protected: |
| 67 MessageLoopForUI ui_loop_; |
| 68 content::TestBrowserThread ui_thread_; |
| 69 scoped_refptr<BluetoothTaskManagerWin> task_manager_; |
| 70 scoped_ptr<BluetoothTaskObserver> observer_; |
| 71 scoped_ptr<BluetoothPollingThreadWin> thread_; |
| 72 }; |
| 73 |
| 74 TEST_F(BluetoothPollingThreadWinTest, Polling) { |
| 75 int expected_num_updates = 3; |
| 76 thread_->StartPolling(); |
| 77 base::RunLoop run_loop; |
| 78 |
| 79 MessageLoop::current()->PostDelayedTask( |
| 80 FROM_HERE, |
| 81 run_loop.QuitClosure(), |
| 82 base::TimeDelta::FromMilliseconds( |
| 83 GetPollingIntervalMs() * expected_num_updates)); |
| 84 |
| 85 run_loop.Run(); |
| 86 StopThread(); |
| 87 EXPECT_EQ(expected_num_updates, observer_->num_updates()); |
| 88 } |
| 89 |
| 90 TEST_F(BluetoothPollingThreadWinTest, StopThread) { |
| 91 thread_->StartPolling(); |
| 92 StopThread(); |
| 93 base::RunLoop run_loop; |
| 94 |
| 95 MessageLoop::current()->PostDelayedTask( |
| 96 FROM_HERE, |
| 97 run_loop.QuitClosure(), |
| 98 base::TimeDelta::FromMilliseconds(GetPollingIntervalMs() * 3)); |
| 99 |
| 100 run_loop.Run(); |
| 101 EXPECT_EQ(0, observer_->num_updates()); |
| 102 } |
| 103 |
| 104 TEST_F(BluetoothPollingThreadWinTest, PollAndStop) { |
| 105 int num_polls = 4; |
| 106 int num_tasks = 6; |
| 107 thread_->StartPolling(); |
| 108 base::RunLoop run_loop; |
| 109 |
| 110 MessageLoop::current()->PostDelayedTask( |
| 111 FROM_HERE, |
| 112 base::Bind( |
| 113 &BluetoothPollingThreadWinTest::StopThread, |
| 114 base::Unretained(this)), |
| 115 base::TimeDelta::FromMilliseconds(GetPollingIntervalMs() * num_polls)); |
| 116 MessageLoop::current()->PostDelayedTask( |
| 117 FROM_HERE, |
| 118 run_loop.QuitClosure(), |
| 119 base::TimeDelta::FromMilliseconds(GetPollingIntervalMs() * num_tasks)); |
| 120 |
| 121 run_loop.Run(); |
| 122 EXPECT_EQ(num_polls, observer_->num_updates()); |
| 123 } |
| 124 |
| 125 } // namespace device |
OLD | NEW |