OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
13 #include "chrome/browser/extensions/extension_system_factory.h" | 13 #include "chrome/browser/extensions/extension_system_factory.h" |
14 #include "chrome/browser/extensions/test_extension_system.h" | 14 #include "chrome/browser/extensions/test_extension_system.h" |
15 #include "chrome/common/extensions/api/bluetooth.h" | 15 #include "chrome/common/extensions/api/bluetooth.h" |
16 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
17 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
18 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "device/bluetooth/bluetooth_uuid.h" | 20 #include "device/bluetooth/bluetooth_uuid.h" |
21 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 21 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
22 #include "device/bluetooth/test/mock_bluetooth_device.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" | 23 #include "extensions/browser/event_router.h" |
26 #include "extensions/common/extension_builder.h" | 24 #include "extensions/common/extension_builder.h" |
27 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
29 | 27 |
30 namespace { | 28 namespace { |
31 | 29 |
32 const char kTestExtensionId[] = "test extension id"; | 30 const char kTestExtensionId[] = "test extension id"; |
33 const device::BluetoothUUID kAudioProfileUuid("1234"); | 31 const device::BluetoothUUID kAudioProfileUuid("1234"); |
34 const device::BluetoothUUID kHealthProfileUuid("4321"); | 32 const device::BluetoothUUID kHealthProfileUuid("4321"); |
(...skipping 21 matching lines...) Expand all Loading... |
56 test_profile_.reset(NULL); | 54 test_profile_.reset(NULL); |
57 base::RunLoop run_loop; | 55 base::RunLoop run_loop; |
58 run_loop.RunUntilIdle(); | 56 run_loop.RunUntilIdle(); |
59 } | 57 } |
60 | 58 |
61 protected: | 59 protected: |
62 base::MessageLoopForUI message_loop_; | 60 base::MessageLoopForUI message_loop_; |
63 // Note: |ui_thread_| must be declared before |router_|. | 61 // Note: |ui_thread_| must be declared before |router_|. |
64 content::TestBrowserThread ui_thread_; | 62 content::TestBrowserThread ui_thread_; |
65 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; | 63 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_; | 64 scoped_ptr<TestingProfile> test_profile_; |
69 BluetoothEventRouter router_; | 65 BluetoothEventRouter router_; |
70 }; | 66 }; |
71 | 67 |
72 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) { | 68 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) { |
73 router_.OnListenerAdded(); | 69 router_.OnListenerAdded(); |
74 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 70 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
75 router_.OnListenerRemoved(); | 71 router_.OnListenerRemoved(); |
76 } | 72 } |
77 | 73 |
78 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) { | 74 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) { |
79 router_.OnListenerAdded(); | 75 router_.OnListenerAdded(); |
80 router_.OnListenerAdded(); | 76 router_.OnListenerAdded(); |
81 router_.OnListenerAdded(); | 77 router_.OnListenerAdded(); |
82 router_.OnListenerRemoved(); | 78 router_.OnListenerRemoved(); |
83 router_.OnListenerRemoved(); | 79 router_.OnListenerRemoved(); |
84 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 80 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
85 router_.OnListenerRemoved(); | 81 router_.OnListenerRemoved(); |
86 } | 82 } |
87 | 83 |
88 TEST_F(BluetoothEventRouterTest, Profiles) { | |
89 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); | |
90 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); | |
91 | |
92 router_.AddProfile( | |
93 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_); | |
94 router_.AddProfile( | |
95 kHealthProfileUuid, kTestExtensionId, &mock_health_profile_); | |
96 EXPECT_TRUE(router_.HasProfile(kAudioProfileUuid)); | |
97 EXPECT_TRUE(router_.HasProfile(kHealthProfileUuid)); | |
98 | |
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)); | |
103 | |
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); | |
107 } | |
108 | |
109 TEST_F(BluetoothEventRouterTest, UnloadExtension) { | 84 TEST_F(BluetoothEventRouterTest, UnloadExtension) { |
110 scoped_refptr<const extensions::Extension> extension = | 85 scoped_refptr<const extensions::Extension> extension = |
111 extensions::ExtensionBuilder() | 86 extensions::ExtensionBuilder() |
112 .SetManifest(extensions::DictionaryBuilder() | 87 .SetManifest(extensions::DictionaryBuilder() |
113 .Set("name", "BT event router test") | 88 .Set("name", "BT event router test") |
114 .Set("version", "1.0") | 89 .Set("version", "1.0") |
115 .Set("manifest_version", 2)) | 90 .Set("manifest_version", 2)) |
116 .SetID(kTestExtensionId) | 91 .SetID(kTestExtensionId) |
117 .Build(); | 92 .Build(); |
118 | 93 |
119 router_.AddProfile( | |
120 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_); | |
121 router_.AddProfile( | |
122 kHealthProfileUuid, kTestExtensionId, &mock_health_profile_); | |
123 EXPECT_TRUE(router_.HasProfile(kAudioProfileUuid)); | |
124 EXPECT_TRUE(router_.HasProfile(kHealthProfileUuid)); | |
125 | |
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); | |
129 | |
130 content::NotificationService* notifier = | 94 content::NotificationService* notifier = |
131 content::NotificationService::current(); | 95 content::NotificationService::current(); |
132 UnloadedExtensionInfo details( | 96 UnloadedExtensionInfo details( |
133 extension, UnloadedExtensionInfo::REASON_DISABLE); | 97 extension, UnloadedExtensionInfo::REASON_DISABLE); |
134 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 98 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
135 content::Source<Profile>(test_profile_.get()), | 99 content::Source<Profile>(test_profile_.get()), |
136 content::Details<UnloadedExtensionInfo>(&details)); | 100 content::Details<UnloadedExtensionInfo>(&details)); |
137 | 101 |
138 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); | |
139 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); | |
140 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 102 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
141 } | 103 } |
142 | 104 |
143 } // namespace extensions | 105 } // namespace extensions |
OLD | NEW |