OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/memory/weak_ptr.h" | |
10 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
11 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
12 #include "base/stringprintf.h" | |
13 #include "base/time.h" | |
14 #include "content/public/test/test_browser_thread.h" | 10 #include "content/public/test/test_browser_thread.h" |
| 11 #include "device/bluetooth/bluetooth_adapter.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_factory.h" |
15 #include "device/bluetooth/bluetooth_adapter_win.h" | 13 #include "device/bluetooth/bluetooth_adapter_win.h" |
16 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 14 #include "device/bluetooth/bluetooth_task_manager_win.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
18 | 16 |
19 namespace { | 17 namespace { |
20 | 18 |
21 const char* kAdapterAddress = "bluetooth adapter address"; | 19 const char* kAdapterAddress = "Bluetooth Adapter Address"; |
| 20 const char* kAdapterName = "Bluetooth Adapter Name"; |
22 | 21 |
23 } // namespace | 22 } // namespace |
24 | 23 |
25 namespace device { | 24 namespace device { |
26 | 25 |
27 class FakeBluetoothAdapterWin : public BluetoothAdapterWin { | |
28 public: | |
29 FakeBluetoothAdapterWin() : BluetoothAdapterWin() {} | |
30 | |
31 virtual void UpdateAdapterState() { | |
32 address_ = adapter_address_; | |
33 } | |
34 | |
35 void SetAdapterAddressForTest(const std::string& adapter_address) { | |
36 adapter_address_ = adapter_address; | |
37 } | |
38 | |
39 private: | |
40 virtual ~FakeBluetoothAdapterWin() {} | |
41 std::string adapter_address_; | |
42 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothAdapterWin); | |
43 }; | |
44 | |
45 class BluetoothAdapterWinTest : public testing::Test { | 26 class BluetoothAdapterWinTest : public testing::Test { |
46 public: | 27 public: |
47 BluetoothAdapterWinTest() | 28 BluetoothAdapterWinTest() |
48 : ui_thread_(content::BrowserThread::UI, &loop_), | 29 : ui_thread_(content::BrowserThread::UI, &ui_loop_), |
49 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 30 adapter_(NULL), |
| 31 adapter_win_(NULL) { |
50 } | 32 } |
51 | 33 |
52 virtual void SetUp() { | 34 virtual void SetUp() { |
53 adapter_ = new FakeBluetoothAdapterWin(); | 35 adapter_ = BluetoothAdapterFactory::DefaultAdapter(); |
54 adapter_->TrackDefaultAdapter(); | 36 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); |
55 } | 37 } |
56 | 38 |
57 void SetAdapterAddressForTest() { | 39 virtual void TearDown() { |
58 adapter_->SetAdapterAddressForTest(kAdapterAddress); | 40 ReleaseAdapter(); |
59 } | 41 } |
60 | 42 |
61 int GetPollIntervalMs() const { | 43 void ReleaseAdapter() { |
62 return BluetoothAdapterWin::kPollIntervalMs; | 44 adapter_ = NULL; |
| 45 adapter_win_ = NULL; |
63 } | 46 } |
64 | 47 |
65 protected: | 48 protected: |
66 scoped_refptr<FakeBluetoothAdapterWin> adapter_; | 49 MessageLoopForUI ui_loop_; |
67 | |
68 // Main message loop for the test. | |
69 MessageLoopForUI loop_; | |
70 | |
71 // Main thread for the test. | |
72 content::TestBrowserThread ui_thread_; | 50 content::TestBrowserThread ui_thread_; |
73 | 51 scoped_refptr<BluetoothAdapter> adapter_; |
74 // NOTE: This should remain the last member so it'll be destroyed and | 52 BluetoothAdapterWin* adapter_win_; |
75 // invalidate its weak pointers before any other members are destroyed. | |
76 base::WeakPtrFactory<BluetoothAdapterWinTest> weak_ptr_factory_; | |
77 }; | 53 }; |
78 | 54 |
79 TEST_F(BluetoothAdapterWinTest, Polling) { | 55 TEST_F(BluetoothAdapterWinTest, AdapterNotPresent) { |
| 56 BluetoothTaskManagerWin::AdapterState state; |
| 57 adapter_win_->AdapterStateChanged(state); |
| 58 EXPECT_FALSE(adapter_win_->IsPresent()); |
| 59 } |
| 60 |
| 61 TEST_F(BluetoothAdapterWinTest, AdapterPresent) { |
| 62 BluetoothTaskManagerWin::AdapterState state; |
| 63 state.address = kAdapterAddress; |
| 64 state.name = kAdapterName; |
| 65 adapter_win_->AdapterStateChanged(state); |
| 66 EXPECT_TRUE(adapter_win_->IsPresent()); |
| 67 } |
| 68 |
| 69 TEST_F(BluetoothAdapterWinTest, AdapterDestroyed) { |
| 70 base::RunLoop run_loop; |
80 MessageLoop::current()->PostDelayedTask( | 71 MessageLoop::current()->PostDelayedTask( |
81 FROM_HERE, | 72 FROM_HERE, |
82 base::Bind( | 73 base::Bind( |
83 &BluetoothAdapterWinTest::SetAdapterAddressForTest, | 74 &BluetoothAdapterWinTest::ReleaseAdapter, |
84 weak_ptr_factory_.GetWeakPtr()), | 75 base::Unretained(this)), |
85 base::TimeDelta::FromMilliseconds(GetPollIntervalMs() - 1)); | 76 base::TimeDelta::FromMilliseconds(1)); |
86 EXPECT_STRNE(kAdapterAddress, adapter_->address().c_str()); | |
87 base::RunLoop run_loop; | |
88 | |
89 MessageLoop::current()->PostDelayedTask( | 77 MessageLoop::current()->PostDelayedTask( |
90 FROM_HERE, | 78 FROM_HERE, |
91 run_loop.QuitClosure(), | 79 run_loop.QuitClosure(), |
92 base::TimeDelta::FromMilliseconds(GetPollIntervalMs() + 1)); | 80 base::TimeDelta::FromMilliseconds(2)); |
93 | 81 |
94 run_loop.Run(); | 82 run_loop.Run(); |
95 | 83 |
96 EXPECT_STREQ(kAdapterAddress, adapter_->address().c_str()); | 84 // Adapter has been destroyed gracefully. |
| 85 SUCCEED(); |
97 } | 86 } |
98 | 87 |
99 TEST_F(BluetoothAdapterWinTest, IsPresent) { | |
100 EXPECT_FALSE(adapter_->IsPresent()); | |
101 SetAdapterAddressForTest(); | |
102 base::RunLoop run_loop; | |
103 MessageLoop::current()->PostDelayedTask( | |
104 FROM_HERE, | |
105 run_loop.QuitClosure(), | |
106 base::TimeDelta::FromMilliseconds(GetPollIntervalMs())); | |
107 run_loop.Run(); | |
108 | |
109 EXPECT_TRUE(adapter_->IsPresent()); | |
110 } | |
111 | 88 |
112 } // namespace device | 89 } // namespace device |
OLD | NEW |