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.
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 "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h"
13 #include "chrome/browser/extensions/extension_system_factory.h"
14 #include "chrome/browser/extensions/test_extension_system.h"
15 #include "chrome/common/extensions/api/bluetooth.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/test/test_browser_thread.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "device/bluetooth/bluetooth_uuid.h"
21 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
22 #include "device/bluetooth/test/mock_bluetooth_device.h"
23 #include "device/bluetooth/test/mock_bluetooth_profile.h"
24 #include "device/bluetooth/test/mock_bluetooth_socket.h"
25 #include "extensions/browser/event_router.h"
26 #include "extensions/common/extension_builder.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
32 const char kTestExtensionId[] = "test extension id";
33 const device::BluetoothUUID kAudioProfileUuid("1234");
34 const device::BluetoothUUID kHealthProfileUuid("4321");
38 namespace extensions {
40 namespace bluetooth = api::bluetooth;
42 class BluetoothEventRouterTest : public testing::Test {
44 BluetoothEventRouterTest()
45 : ui_thread_(content::BrowserThread::UI, &message_loop_),
46 mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()),
47 test_profile_(new TestingProfile()),
48 router_(test_profile_.get()) {
49 router_.SetAdapterForTest(mock_adapter_);
52 virtual void TearDown() OVERRIDE {
53 // Some profile-dependent services rely on UI thread to clean up. We make
54 // sure they are properly cleaned up by running the UI message loop until
56 test_profile_.reset(NULL);
57 base::RunLoop run_loop;
58 run_loop.RunUntilIdle();
62 base::MessageLoopForUI message_loop_;
63 // Note: |ui_thread_| must be declared before |router_|.
64 content::TestBrowserThread ui_thread_;
65 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_;
66 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_;
67 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_;
68 scoped_ptr<TestingProfile> test_profile_;
69 BluetoothEventRouter router_;
72 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) {
73 router_.OnListenerAdded();
74 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
75 router_.OnListenerRemoved();
78 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) {
79 router_.OnListenerAdded();
80 router_.OnListenerAdded();
81 router_.OnListenerAdded();
82 router_.OnListenerRemoved();
83 router_.OnListenerRemoved();
84 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
85 router_.OnListenerRemoved();
88 TEST_F(BluetoothEventRouterTest, Profiles) {
89 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid));
90 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid));
93 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_);
95 kHealthProfileUuid, kTestExtensionId, &mock_health_profile_);
96 EXPECT_TRUE(router_.HasProfile(kAudioProfileUuid));
97 EXPECT_TRUE(router_.HasProfile(kHealthProfileUuid));
99 EXPECT_CALL(mock_audio_profile_, Unregister()).Times(1);
100 router_.RemoveProfile(kAudioProfileUuid);
101 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid));
102 EXPECT_TRUE(router_.HasProfile(kHealthProfileUuid));
104 // Make sure remaining profiles are unregistered in destructor.
105 EXPECT_CALL(mock_health_profile_, Unregister()).Times(1);
106 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
109 TEST_F(BluetoothEventRouterTest, UnloadExtension) {
110 scoped_refptr<const extensions::Extension> extension =
111 extensions::ExtensionBuilder()
112 .SetManifest(extensions::DictionaryBuilder()
113 .Set("name", "BT event router test")
114 .Set("version", "1.0")
115 .Set("manifest_version", 2))
116 .SetID(kTestExtensionId)
120 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_);
122 kHealthProfileUuid, kTestExtensionId, &mock_health_profile_);
123 EXPECT_TRUE(router_.HasProfile(kAudioProfileUuid));
124 EXPECT_TRUE(router_.HasProfile(kHealthProfileUuid));
126 // Unloading the extension should unregister all profiles added by it.
127 EXPECT_CALL(mock_audio_profile_, Unregister()).Times(1);
128 EXPECT_CALL(mock_health_profile_, Unregister()).Times(1);
130 content::NotificationService* notifier =
131 content::NotificationService::current();
132 UnloadedExtensionInfo details(
133 extension, UnloadedExtensionInfo::REASON_DISABLE);
134 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
135 content::Source<Profile>(test_profile_.get()),
136 content::Details<UnloadedExtensionInfo>(&details));
138 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid));
139 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid));
140 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
143 } // namespace extensions