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/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.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" |
| 16 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 const char* kAdapterAddress = "bluetooth adapter address"; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 namespace device { |
| 26 |
| 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 { |
| 46 public: |
| 47 BluetoothAdapterWinTest() |
| 48 : ui_thread_(content::BrowserThread::UI, &loop_), |
| 49 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 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 } |
| 64 |
| 65 protected: |
| 66 scoped_refptr<FakeBluetoothAdapterWin> adapter_; |
| 67 |
| 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 }; |
| 78 |
| 79 TEST_F(BluetoothAdapterWinTest, Polling) { |
| 80 MessageLoop::current()->PostDelayedTask( |
| 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 |
| 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; |
| 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 |
| 112 } // namespace device |
OLD | NEW |