Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(609)

Side by Side Diff: chrome/browser/extensions/api/bluetooth/bluetooth_event_router_unittest.cc

Issue 180163009: chrome.bluetooth API improvements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove unused code. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "device/bluetooth/test/mock_bluetooth_adapter.h" 20 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
20 #include "device/bluetooth/test/mock_bluetooth_device.h" 21 #include "device/bluetooth/test/mock_bluetooth_device.h"
21 #include "device/bluetooth/test/mock_bluetooth_profile.h" 22 #include "device/bluetooth/test/mock_bluetooth_profile.h"
22 #include "device/bluetooth/test/mock_bluetooth_socket.h" 23 #include "device/bluetooth/test/mock_bluetooth_socket.h"
23 #include "extensions/browser/event_router.h" 24 #include "extensions/browser/event_router.h"
24 #include "extensions/common/extension_builder.h" 25 #include "extensions/common/extension_builder.h"
25 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
27 28
28 namespace { 29 namespace {
29 30
30 const char kTestExtensionId[] = "test extension id"; 31 const char kTestExtensionId[] = "test extension id";
31 const char kAudioProfileUuid[] = "audio profile uuid"; 32 const char kAudioProfileUuid[] = "audio profile uuid";
32 const char kHealthProfileUuid[] = "health profile uuid"; 33 const char kHealthProfileUuid[] = "health profile uuid";
33 34
34 class FakeEventRouter : public extensions::EventRouter {
35 public:
36 explicit FakeEventRouter(Profile* profile) : EventRouter(profile, NULL) {}
37
38 virtual void DispatchEventToExtension(
39 const std::string& extension_id,
40 scoped_ptr<extensions::Event> event) OVERRIDE {
41 extension_id_ = extension_id;
42 event_ = event.Pass();
43 }
44
45 std::string extension_id() const {
46 return extension_id_;
47 }
48
49 const extensions::Event* event() const {
50 return event_.get();
51 }
52
53 private:
54 std::string extension_id_;
55 scoped_ptr<extensions::Event> event_;
56
57 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter);
58 };
59
60 class FakeExtensionSystem : public extensions::TestExtensionSystem {
61 public:
62 explicit FakeExtensionSystem(Profile* profile)
63 : extensions::TestExtensionSystem(profile) {}
64
65 virtual extensions::EventRouter* event_router() OVERRIDE {
66 if (!fake_event_router_)
67 fake_event_router_.reset(new FakeEventRouter(profile_));
68 return fake_event_router_.get();
69 }
70
71 private:
72 scoped_ptr<FakeEventRouter> fake_event_router_;
73
74 DISALLOW_COPY_AND_ASSIGN(FakeExtensionSystem);
75 };
76
77 KeyedService* BuildFakeExtensionSystem(content::BrowserContext* profile) {
78 return new FakeExtensionSystem(static_cast<Profile*>(profile));
79 }
80
81 } // namespace 35 } // namespace
82 36
83 namespace extensions { 37 namespace extensions {
84 38
85 namespace bluetooth = api::bluetooth; 39 namespace bluetooth = api::bluetooth;
86 40
87 class BluetoothEventRouterTest : public testing::Test { 41 class BluetoothEventRouterTest : public testing::Test {
88 public: 42 public:
89 BluetoothEventRouterTest() 43 BluetoothEventRouterTest()
90 : mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()), 44 : ui_thread_(content::BrowserThread::UI, &message_loop_),
45 mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()),
91 test_profile_(new TestingProfile()), 46 test_profile_(new TestingProfile()),
92 router_(test_profile_.get()), 47 router_(test_profile_.get()) {
93 ui_thread_(content::BrowserThread::UI, &message_loop_) {
94 router_.SetAdapterForTest(mock_adapter_); 48 router_.SetAdapterForTest(mock_adapter_);
95 } 49 }
96 50
97 virtual void TearDown() OVERRIDE { 51 virtual void TearDown() OVERRIDE {
98 // Some profile-dependent services rely on UI thread to clean up. We make 52 // Some profile-dependent services rely on UI thread to clean up. We make
99 // sure they are properly cleaned up by running the UI message loop until 53 // sure they are properly cleaned up by running the UI message loop until
100 // idle. 54 // idle.
101 test_profile_.reset(NULL); 55 test_profile_.reset(NULL);
102 base::RunLoop run_loop; 56 base::RunLoop run_loop;
103 run_loop.RunUntilIdle(); 57 run_loop.RunUntilIdle();
104 } 58 }
105 59
106 protected: 60 protected:
61 base::MessageLoopForUI message_loop_;
62 // Note: |ui_thread_| must be declared before |router_|.
63 content::TestBrowserThread ui_thread_;
107 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; 64 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_;
108 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_; 65 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_;
109 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_; 66 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_;
110 scoped_ptr<TestingProfile> test_profile_; 67 scoped_ptr<TestingProfile> test_profile_;
111 BluetoothEventRouter router_; 68 BluetoothEventRouter router_;
112 base::MessageLoopForUI message_loop_;
113 content::TestBrowserThread ui_thread_;
114 }; 69 };
115 70
116 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) { 71 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) {
117 router_.OnListenerAdded(); 72 router_.OnListenerAdded();
118 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); 73 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
119 router_.OnListenerRemoved(); 74 router_.OnListenerRemoved();
120 } 75 }
121 76
122 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) { 77 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) {
123 router_.OnListenerAdded(); 78 router_.OnListenerAdded();
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 extension, UnloadedExtensionInfo::REASON_DISABLE); 132 extension, UnloadedExtensionInfo::REASON_DISABLE);
178 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, 133 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
179 content::Source<Profile>(test_profile_.get()), 134 content::Source<Profile>(test_profile_.get()),
180 content::Details<UnloadedExtensionInfo>(&details)); 135 content::Details<UnloadedExtensionInfo>(&details));
181 136
182 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); 137 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid));
183 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); 138 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid));
184 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); 139 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
185 } 140 }
186 141
187 TEST_F(BluetoothEventRouterTest, DispatchConnectionEvent) {
188 router_.AddProfile(
189 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_);
190
191 FakeExtensionSystem* fake_extension_system =
192 static_cast<FakeExtensionSystem*>(ExtensionSystemFactory::GetInstance()->
193 SetTestingFactoryAndUse(test_profile_.get(),
194 &BuildFakeExtensionSystem));
195
196 testing::NiceMock<device::MockBluetoothDevice> mock_device(
197 mock_adapter_, 0, "device name", "device address", true, false);
198 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket(
199 new testing::NiceMock<device::MockBluetoothSocket>());
200
201 router_.DispatchConnectionEvent(kTestExtensionId,
202 kAudioProfileUuid,
203 &mock_device,
204 mock_socket);
205
206 FakeEventRouter* fake_event_router =
207 static_cast<FakeEventRouter*>(fake_extension_system->event_router());
208
209 EXPECT_STREQ(kTestExtensionId, fake_event_router->extension_id().c_str());
210 EXPECT_STREQ(bluetooth::OnConnection::kEventName,
211 fake_event_router->event()->event_name.c_str());
212
213 base::ListValue* event_args = fake_event_router->event()->event_args.get();
214 base::DictionaryValue* socket_value = NULL;
215 ASSERT_TRUE(event_args->GetDictionary(0, &socket_value));
216 int socket_id;
217 ASSERT_TRUE(socket_value->GetInteger("id", &socket_id));
218 EXPECT_EQ(mock_socket.get(), router_.GetSocket(socket_id).get());
219
220 base::DictionaryValue* profile_value = NULL;
221 ASSERT_TRUE(socket_value->GetDictionary("profile", &profile_value));
222 std::string uuid;
223 ASSERT_TRUE(profile_value->GetString("uuid", &uuid));
224 EXPECT_STREQ(kAudioProfileUuid, uuid.c_str());
225
226 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
227 router_.ReleaseSocket(socket_id);
228 }
229
230 TEST_F(BluetoothEventRouterTest, DoNotDispatchConnectionEvent) {
231 FakeExtensionSystem* fake_extension_system =
232 static_cast<FakeExtensionSystem*>(ExtensionSystemFactory::GetInstance()->
233 SetTestingFactoryAndUse(test_profile_.get(),
234 &BuildFakeExtensionSystem));
235 testing::NiceMock<device::MockBluetoothDevice> mock_device(
236 mock_adapter_, 0, "device name", "device address", true, false);
237 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket(
238 new testing::NiceMock<device::MockBluetoothSocket>());
239
240 // Connection event won't be dispatched for non-registered profiles.
241 router_.DispatchConnectionEvent("test extension id",
242 kAudioProfileUuid,
243 &mock_device,
244 mock_socket);
245 FakeEventRouter* fake_event_router =
246 static_cast<FakeEventRouter*>(fake_extension_system->event_router());
247 EXPECT_TRUE(fake_event_router->event() == NULL);
248
249 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
250 }
251
252 } // namespace extensions 142 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698