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/message_loop.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "content/public/test/test_browser_thread.h" |
| 9 #include "device/bluetooth/bluetooth_task_manager_win.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 class BluetoothTaskObserver : public device::BluetoothTaskManagerWin::Observer { |
| 15 public: |
| 16 BluetoothTaskObserver() : num_updates_(0) { |
| 17 } |
| 18 |
| 19 virtual ~BluetoothTaskObserver() { |
| 20 } |
| 21 |
| 22 virtual void AdapterStateChanged( |
| 23 const device::BluetoothTaskManagerWin::AdapterState& state) { |
| 24 num_updates_++; |
| 25 } |
| 26 |
| 27 int num_updates() const { |
| 28 return num_updates_; |
| 29 } |
| 30 |
| 31 private: |
| 32 int num_updates_; |
| 33 }; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 namespace device { |
| 38 |
| 39 class BluetoothTaskManagerWinTest : public testing::Test { |
| 40 public: |
| 41 BluetoothTaskManagerWinTest() |
| 42 : ui_thread_(content::BrowserThread::UI, &ui_loop_), |
| 43 task_manager_(new BluetoothTaskManagerWin()) { |
| 44 } |
| 45 |
| 46 virtual void SetUp() { |
| 47 task_manager_->AddObserver(&observer_); |
| 48 } |
| 49 |
| 50 virtual void TearDown() { |
| 51 task_manager_->RemoveObserver(&observer_); |
| 52 } |
| 53 |
| 54 int GetPollingIntervalMs() const { |
| 55 return BluetoothTaskManagerWin::kPollIntervalMs; |
| 56 } |
| 57 |
| 58 // It posts |PollAdapter()| directly to |bluetooth_task_runner_| to bypass |
| 59 // system-specific |HasBluetoothStack()| check. |
| 60 void StartPolling() { |
| 61 task_manager_->bluetooth_task_runner_->PostTask( |
| 62 FROM_HERE, |
| 63 base::Bind(&BluetoothTaskManagerWin::PollAdapter, task_manager_)); |
| 64 } |
| 65 |
| 66 void Shutdown() { |
| 67 task_manager_->Shutdown(); |
| 68 } |
| 69 |
| 70 protected: |
| 71 MessageLoopForUI ui_loop_; |
| 72 content::TestBrowserThread ui_thread_; |
| 73 scoped_refptr<BluetoothTaskManagerWin> task_manager_; |
| 74 BluetoothTaskObserver observer_; |
| 75 }; |
| 76 |
| 77 TEST_F(BluetoothTaskManagerWinTest, NotifyObserver) { |
| 78 StartPolling(); |
| 79 base::RunLoop run_loop; |
| 80 MessageLoop::current()->PostDelayedTask( |
| 81 FROM_HERE, |
| 82 run_loop.QuitClosure(), |
| 83 base::TimeDelta::FromMilliseconds(GetPollingIntervalMs())); |
| 84 |
| 85 run_loop.Run(); |
| 86 Shutdown(); |
| 87 EXPECT_TRUE(observer_.num_updates() > 0); |
| 88 } |
| 89 |
| 90 TEST_F(BluetoothTaskManagerWinTest, Polling) { |
| 91 int expected_num_updates = 3; |
| 92 StartPolling(); |
| 93 base::RunLoop run_loop; |
| 94 |
| 95 MessageLoop::current()->PostDelayedTask( |
| 96 FROM_HERE, |
| 97 run_loop.QuitClosure(), |
| 98 base::TimeDelta::FromMilliseconds( |
| 99 GetPollingIntervalMs() * expected_num_updates)); |
| 100 |
| 101 run_loop.Run(); |
| 102 Shutdown(); |
| 103 EXPECT_EQ(expected_num_updates, observer_.num_updates()); |
| 104 } |
| 105 |
| 106 TEST_F(BluetoothTaskManagerWinTest, Shutdown) { |
| 107 StartPolling(); |
| 108 Shutdown(); |
| 109 base::RunLoop run_loop; |
| 110 |
| 111 MessageLoop::current()->PostDelayedTask( |
| 112 FROM_HERE, |
| 113 run_loop.QuitClosure(), |
| 114 base::TimeDelta::FromMilliseconds(GetPollingIntervalMs() * 3)); |
| 115 |
| 116 run_loop.Run(); |
| 117 EXPECT_EQ(1, observer_.num_updates()); |
| 118 } |
| 119 |
| 120 TEST_F(BluetoothTaskManagerWinTest, PollAndStop) { |
| 121 int num_polls = 4; |
| 122 int num_tasks = 6; |
| 123 StartPolling(); |
| 124 base::RunLoop run_loop; |
| 125 |
| 126 MessageLoop::current()->PostDelayedTask( |
| 127 FROM_HERE, |
| 128 base::Bind(&BluetoothTaskManagerWinTest::Shutdown, |
| 129 base::Unretained(this)), |
| 130 base::TimeDelta::FromMilliseconds(GetPollingIntervalMs() * num_polls)); |
| 131 MessageLoop::current()->PostDelayedTask( |
| 132 FROM_HERE, |
| 133 run_loop.QuitClosure(), |
| 134 base::TimeDelta::FromMilliseconds(GetPollingIntervalMs() * num_tasks)); |
| 135 |
| 136 run_loop.Run(); |
| 137 EXPECT_EQ(num_polls, observer_.num_updates()); |
| 138 } |
| 139 |
| 140 } // namespace device |
OLD | NEW |