Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / bluetooth / bluetooth_event_router_unittest.cc
blob91ea0beee0e3cc8c51d5f97ef4af1fd472126beb
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.
5 #include <string>
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/test/test_browser_context.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "device/bluetooth/bluetooth_uuid.h"
15 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
16 #include "extensions/browser/api/bluetooth/bluetooth_event_router.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/extensions_test.h"
19 #include "extensions/common/api/bluetooth.h"
20 #include "extensions/common/extension_builder.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace {
26 const char kTestExtensionId[] = "test extension id";
27 const device::BluetoothUUID kAudioProfileUuid("1234");
28 const device::BluetoothUUID kHealthProfileUuid("4321");
30 MATCHER_P(IsFilterEqual, a, "") {
31 return arg.Equals(*a);
33 } // namespace
35 namespace extensions {
37 namespace bluetooth = api::bluetooth;
39 class BluetoothEventRouterTest : public ExtensionsTest {
40 public:
41 BluetoothEventRouterTest()
42 : ui_thread_(content::BrowserThread::UI, &message_loop_),
43 mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()),
44 notification_service_(content::NotificationService::Create()),
45 router_(new BluetoothEventRouter(browser_context())) {
46 router_->SetAdapterForTest(mock_adapter_);
49 void TearDown() override {
50 // It's important to destroy the router before the browser context keyed
51 // services so it removes itself as an ExtensionRegistry observer.
52 router_.reset(NULL);
53 ExtensionsTest::TearDown();
56 protected:
57 base::MessageLoopForUI message_loop_;
58 // Note: |ui_thread_| must be declared before |router_|.
59 content::TestBrowserThread ui_thread_;
60 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_;
61 scoped_ptr<content::NotificationService> notification_service_;
62 scoped_ptr<BluetoothEventRouter> router_;
65 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) {
66 router_->OnListenerAdded();
67 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
68 router_->OnListenerRemoved();
71 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) {
72 router_->OnListenerAdded();
73 router_->OnListenerAdded();
74 router_->OnListenerAdded();
75 router_->OnListenerRemoved();
76 router_->OnListenerRemoved();
77 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
78 router_->OnListenerRemoved();
81 TEST_F(BluetoothEventRouterTest, UnloadExtension) {
82 scoped_refptr<const Extension> extension =
83 ExtensionBuilder()
84 .SetManifest(DictionaryBuilder()
85 .Set("name", "BT event router test")
86 .Set("version", "1.0")
87 .Set("manifest_version", 2))
88 .SetID(kTestExtensionId)
89 .Build();
91 ExtensionRegistry::Get(browser_context())->TriggerOnUnloaded(
92 extension.get(), UnloadedExtensionInfo::REASON_DISABLE);
94 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
97 // This test check that calling SetDiscoveryFilter before StartDiscoverySession
98 // for given extension will start session with proper filter.
99 TEST_F(BluetoothEventRouterTest, SetDiscoveryFilter) {
100 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter(
101 new device::BluetoothDiscoveryFilter(
102 device::BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
104 discovery_filter->SetRSSI(-80);
105 discovery_filter->AddUUID(device::BluetoothUUID("1000"));
107 device::BluetoothDiscoveryFilter df(
108 device::BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
109 df.CopyFrom(*discovery_filter);
111 router_->SetDiscoveryFilter(discovery_filter.Pass(), mock_adapter_,
112 kTestExtensionId, base::Bind(&base::DoNothing),
113 base::Bind(&base::DoNothing));
115 EXPECT_CALL(*mock_adapter_, StartDiscoverySessionWithFilterRaw(
116 testing::Pointee(IsFilterEqual(&df)),
117 testing::_, testing::_)).Times(1);
119 router_->StartDiscoverySession(mock_adapter_, kTestExtensionId,
120 base::Bind(&base::DoNothing),
121 base::Bind(&base::DoNothing));
123 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
126 } // namespace extensions