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 <string> | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/run_loop.h" | |
11 #include "base/stringprintf.h" | |
12 #include "base/time.h" | |
13 #include "content/public/test/test_browser_thread.h" | |
14 #include "device/bluetooth/bluetooth_adapter_win.h" | |
15 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace { | |
19 | |
20 const char* kAdapterAddress = "bluetooth adapter address"; | |
21 | |
22 } // namespace | |
23 | |
24 namespace device { | |
25 | |
26 class FakeBluetoothAdapterWin : public BluetoothAdapterWin { | |
27 public: | |
28 FakeBluetoothAdapterWin() : BluetoothAdapterWin() {} | |
29 | |
30 virtual void UpdateAdapterState() { | |
31 address_ = adapter_address_; | |
32 } | |
33 | |
34 void set_adapter_address(const std::string& adapter_address) { | |
bryeung
2012/11/09 17:59:09
SetAdapterAddressForTest is probably a better name
youngki
2012/11/09 19:14:04
Done.
| |
35 adapter_address_ = adapter_address; | |
36 } | |
37 | |
38 private: | |
39 virtual ~FakeBluetoothAdapterWin() {} | |
40 std::string adapter_address_; | |
41 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothAdapterWin); | |
42 }; | |
43 | |
44 class BluetoothAdapterWinTest : public testing::Test { | |
45 public: | |
46 BluetoothAdapterWinTest() | |
47 : ui_thread_(content::BrowserThread::UI, &loop_), | |
48 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
49 } | |
50 | |
51 virtual void SetUp() { | |
52 adapter_ = new FakeBluetoothAdapterWin; | |
53 adapter_->TrackDefaultAdapter(); | |
54 } | |
55 | |
56 void UpdateAdapterAddress() { | |
bryeung
2012/11/09 17:59:09
probably better to just call adapter_->SetAdapterA
youngki
2012/11/09 19:14:04
I still need this function to use as a closure for
| |
57 adapter_->set_adapter_address(kAdapterAddress); | |
58 } | |
59 | |
60 protected: | |
61 FakeBluetoothAdapterWin* adapter_; | |
62 | |
63 // Main message loop for the test. | |
64 MessageLoopForUI loop_; | |
65 | |
66 // Main thread for the test. | |
67 content::TestBrowserThread ui_thread_; | |
68 | |
69 // NOTE: This should remain the last member so it'll be destroyed and | |
70 // invalidate its weak pointers before any other members are destroyed. | |
71 base::WeakPtrFactory<BluetoothAdapterWinTest> weak_ptr_factory_; | |
72 }; | |
73 | |
74 TEST_F(BluetoothAdapterWinTest, Polling) { | |
75 MessageLoop::current()->PostDelayedTask( | |
76 FROM_HERE, | |
77 base::Bind( | |
78 &BluetoothAdapterWinTest::UpdateAdapterAddress, | |
79 weak_ptr_factory_.GetWeakPtr()), | |
80 base::TimeDelta::FromMilliseconds( | |
81 BluetoothAdapterWin::kPollIntervalMs - 1)); | |
82 EXPECT_STRNE(kAdapterAddress, adapter_->address().c_str()); | |
83 base::RunLoop run_loop; | |
84 | |
85 MessageLoop::current()->PostDelayedTask( | |
86 FROM_HERE, | |
87 run_loop.QuitClosure(), | |
88 base::TimeDelta::FromMilliseconds( | |
89 BluetoothAdapterWin::kPollIntervalMs + 1)); | |
90 | |
91 run_loop.Run(); | |
92 | |
93 EXPECT_STREQ(kAdapterAddress, adapter_->address().c_str()); | |
94 } | |
95 | |
96 TEST_F(BluetoothAdapterWinTest, IsPresent) { | |
97 MockBluetoothAdapter::Observer adapter_observer; | |
98 adapter_->AddObserver(&adapter_observer); | |
99 EXPECT_FALSE(adapter_->IsPresent()); | |
100 UpdateAdapterAddress(); | |
101 base::RunLoop run_loop; | |
102 MessageLoop::current()->PostDelayedTask( | |
103 FROM_HERE, | |
104 run_loop.QuitClosure(), | |
105 base::TimeDelta::FromMilliseconds( | |
106 BluetoothAdapterWin::kPollIntervalMs)); | |
107 run_loop.Run(); | |
108 | |
109 EXPECT_TRUE(adapter_->IsPresent()); | |
110 } | |
111 | |
112 } // namespace device | |
OLD | NEW |