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