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 SetAdapterAddressForTest(const std::string& adapter_address) { |
| 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 SetAdapterAddressForTest() { |
| 57 adapter_->SetAdapterAddressForTest(kAdapterAddress); |
| 58 } |
| 59 |
| 60 int GetPollIntervalMs() const { |
| 61 return adapter_->kPollIntervalMs; |
| 62 } |
| 63 |
| 64 protected: |
| 65 FakeBluetoothAdapterWin* adapter_; |
| 66 |
| 67 // Main message loop for the test. |
| 68 MessageLoopForUI loop_; |
| 69 |
| 70 // Main thread for the test. |
| 71 content::TestBrowserThread ui_thread_; |
| 72 |
| 73 // NOTE: This should remain the last member so it'll be destroyed and |
| 74 // invalidate its weak pointers before any other members are destroyed. |
| 75 base::WeakPtrFactory<BluetoothAdapterWinTest> weak_ptr_factory_; |
| 76 }; |
| 77 |
| 78 TEST_F(BluetoothAdapterWinTest, Polling) { |
| 79 MessageLoop::current()->PostDelayedTask( |
| 80 FROM_HERE, |
| 81 base::Bind( |
| 82 &BluetoothAdapterWinTest::SetAdapterAddressForTest, |
| 83 weak_ptr_factory_.GetWeakPtr()), |
| 84 base::TimeDelta::FromMilliseconds(GetPollIntervalMs() - 1)); |
| 85 EXPECT_STRNE(kAdapterAddress, adapter_->address().c_str()); |
| 86 base::RunLoop run_loop; |
| 87 |
| 88 MessageLoop::current()->PostDelayedTask( |
| 89 FROM_HERE, |
| 90 run_loop.QuitClosure(), |
| 91 base::TimeDelta::FromMilliseconds(GetPollIntervalMs() + 1)); |
| 92 |
| 93 run_loop.Run(); |
| 94 |
| 95 EXPECT_STREQ(kAdapterAddress, adapter_->address().c_str()); |
| 96 } |
| 97 |
| 98 TEST_F(BluetoothAdapterWinTest, IsPresent) { |
| 99 MockBluetoothAdapter::Observer adapter_observer; |
| 100 adapter_->AddObserver(&adapter_observer); |
| 101 EXPECT_FALSE(adapter_->IsPresent()); |
| 102 SetAdapterAddressForTest(); |
| 103 base::RunLoop run_loop; |
| 104 MessageLoop::current()->PostDelayedTask( |
| 105 FROM_HERE, |
| 106 run_loop.QuitClosure(), |
| 107 base::TimeDelta::FromMilliseconds(GetPollIntervalMs())); |
| 108 run_loop.Run(); |
| 109 |
| 110 EXPECT_TRUE(adapter_->IsPresent()); |
| 111 } |
| 112 |
| 113 } // namespace device |
OLD | NEW |