Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(929)

Side by Side Diff: device/bluetooth/bluetooth_adapter_win_unittest.cc

Issue 11411130: Implemented BluetoothTaskManagerWin class. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added BluetoothTaskManagerWin class Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 10 #include "base/stringprintf.h"
13 #include "base/time.h" 11 #include "base/time.h"
14 #include "content/public/test/test_browser_thread.h" 12 #include "content/public/test/test_browser_thread.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_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 int kMaxPolling = 5;
22 20
23 } // namespace 21 } // namespace
24 22
25 namespace device { 23 namespace device {
26 24
27 class FakeBluetoothAdapterWin : public BluetoothAdapterWin { 25 class FakeBluetoothAdapterWin : public BluetoothAdapterWin {
28 public: 26 public:
29 FakeBluetoothAdapterWin() : BluetoothAdapterWin() {} 27 FakeBluetoothAdapterWin() : BluetoothAdapterWin() {}
30 28
31 virtual void UpdateAdapterState() { 29 HANDLE GetAdapterHandle() const {
32 address_ = adapter_address_; 30 return adapter_handle_;
33 }
34
35 void SetAdapterAddressForTest(const std::string& adapter_address) {
36 adapter_address_ = adapter_address;
37 } 31 }
38 32
39 private: 33 private:
40 virtual ~FakeBluetoothAdapterWin() {} 34 virtual ~FakeBluetoothAdapterWin() {}
41 std::string adapter_address_; 35
36 // BluetoothAdapterWin override.
37 virtual void UpdateAdapterState(HANDLE adapter_handle) OVERRIDE {
38 adapter_handle_ = adapter_handle;
39 }
40
41 HANDLE adapter_handle_;
42
42 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothAdapterWin); 43 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothAdapterWin);
43 }; 44 };
44 45
46 class FakeBluetoothManagerWin : public BluetoothManagerWin {
47 public:
48 FakeBluetoothManagerWin() : BluetoothManagerWin(), adapter_handle_(NULL) {}
49 virtual ~FakeBluetoothManagerWin() {
50 Stop();
51 }
52
53 void SetAdapterHandleForTest(HANDLE adapter_handle) {
54 adapter_handle_ = adapter_handle;
55 }
56
57 private:
58 // BluetoothManagerWin override.
59 virtual HANDLE FindAdapterHandle() OVERRIDE {
60 return adapter_handle_;
61 }
62
63 HANDLE adapter_handle_;
64 };
65
45 class BluetoothAdapterWinTest : public testing::Test { 66 class BluetoothAdapterWinTest : public testing::Test {
46 public: 67 public:
47 BluetoothAdapterWinTest() 68 BluetoothAdapterWinTest()
48 : ui_thread_(content::BrowserThread::UI, &loop_), 69 : ui_thread_(content::BrowserThread::UI, &loop_) {
49 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
50 } 70 }
51 71
52 virtual void SetUp() { 72 virtual void SetUp() {
53 adapter_ = new FakeBluetoothAdapterWin(); 73 adapter_ = new FakeBluetoothAdapterWin();
74 manager_ = new FakeBluetoothManagerWin();
75 SetAdapterHandleForTest(0);
76
77 // The ownership of manager_ is transferred to adapter_.
78 adapter_->SetBluetoothManagerForTest(manager_);
79
54 adapter_->TrackDefaultAdapter(); 80 adapter_->TrackDefaultAdapter();
55 } 81 }
56 82
57 void SetAdapterAddressForTest() { 83 void SetAdapterHandleForTest(int handle_index) {
58 adapter_->SetAdapterAddressForTest(kAdapterAddress); 84 manager_->SetAdapterHandleForTest(&adapter_handles[handle_index]);
59 } 85 }
60 86
61 int GetPollIntervalMs() const { 87 int GetPollIntervalMs() const {
62 return BluetoothAdapterWin::kPollIntervalMs; 88 return BluetoothAdapterWin::kPollIntervalMs;
63 } 89 }
64 90
65 protected: 91 protected:
66 scoped_refptr<FakeBluetoothAdapterWin> adapter_; 92 scoped_refptr<FakeBluetoothAdapterWin> adapter_;
93 FakeBluetoothManagerWin* manager_;
67 94
68 // Main message loop for the test. 95 // Main message loop for the test.
69 MessageLoopForUI loop_; 96 MessageLoopForUI loop_;
70 97
71 // Main thread for the test. 98 // Main thread for the test.
72 content::TestBrowserThread ui_thread_; 99 content::TestBrowserThread ui_thread_;
73 100
74 // NOTE: This should remain the last member so it'll be destroyed and 101 int adapter_handles[kMaxPolling];
75 // invalidate its weak pointers before any other members are destroyed.
76 base::WeakPtrFactory<BluetoothAdapterWinTest> weak_ptr_factory_;
77 }; 102 };
78 103
79 TEST_F(BluetoothAdapterWinTest, Polling) { 104 TEST_F(BluetoothAdapterWinTest, AdapterDestroyed) {
80 MessageLoop::current()->PostDelayedTask( 105 adapter_.release();
81 FROM_HERE,
82 base::Bind(
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 106
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 }
98
99 TEST_F(BluetoothAdapterWinTest, IsPresent) {
100 EXPECT_FALSE(adapter_->IsPresent());
101 SetAdapterAddressForTest();
102 base::RunLoop run_loop; 107 base::RunLoop run_loop;
103 MessageLoop::current()->PostDelayedTask( 108 MessageLoop::current()->PostDelayedTask(
104 FROM_HERE, 109 FROM_HERE,
105 run_loop.QuitClosure(), 110 run_loop.QuitClosure(),
106 base::TimeDelta::FromMilliseconds(GetPollIntervalMs())); 111 base::TimeDelta::FromMilliseconds(1));
107 run_loop.Run(); 112 run_loop.Run();
108 113
109 EXPECT_TRUE(adapter_->IsPresent()); 114 // BluetoothAdapterWin is destroyed successfully after terminating
115 // BluetoothManagerWin thread.
116 SUCCEED();
117 }
118
119 TEST_F(BluetoothAdapterWinTest, AdapterHandleReceivedFromManager) {
120 base::RunLoop run_loop;
121 MessageLoop::current()->PostDelayedTask(
122 FROM_HERE,
123 run_loop.QuitClosure(),
124 base::TimeDelta::FromMilliseconds(1));
125 run_loop.Run();
126
127 EXPECT_EQ(&adapter_handles[0], adapter_->GetAdapterHandle());
128 }
129
130 TEST_F(BluetoothAdapterWinTest, Polling) {
131 for (int i = 0; i < kMaxPolling; i++) {
132 manager_->message_loop()->PostDelayedTask(
133 FROM_HERE,
134 base::Bind(&BluetoothAdapterWinTest::SetAdapterHandleForTest,
135 base::Unretained(this),
136 i),
137 base::TimeDelta::FromMilliseconds(GetPollIntervalMs() * i + 1));
138 }
139 base::RunLoop run_loop;
140 MessageLoop::current()->PostDelayedTask(
141 FROM_HERE,
142 run_loop.QuitClosure(),
143 base::TimeDelta::FromMilliseconds(GetPollIntervalMs() * kMaxPolling + 1));
144 run_loop.Run();
145
146 EXPECT_EQ(&adapter_handles[kMaxPolling - 1], adapter_->GetAdapterHandle());
110 } 147 }
111 148
112 } // namespace device 149 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698