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

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

Issue 180163009: chrome.bluetooth API improvements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix threading ownership related to ApiResourceManager<BluetoothApiSocket> and BluetoothSocketEventD… Created 6 years, 9 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
(Empty)
1 // Copyright 2014 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/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_api.h"
13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h"
14 #include "chrome/browser/extensions/event_names.h"
15 #include "chrome/browser/extensions/extension_system_factory.h"
16 #include "chrome/browser/extensions/test_extension_system.h"
17 #include "chrome/common/extensions/api/bluetooth.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "chrome/test/base/testing_profile_manager.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
24 #include "device/bluetooth/test/mock_bluetooth_device.h"
25 #include "device/bluetooth/test/mock_bluetooth_profile.h"
26 #include "device/bluetooth/test/mock_bluetooth_socket.h"
27 #include "extensions/browser/event_router.h"
28 #include "extensions/common/extension_builder.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31
32 namespace {
33
34 const char kTestExtensionId[] = "test extension id";
35 const char kAudioProfileUuid[] = "audio profile uuid";
36
37 static BrowserContextKeyedService* ApiResourceManagerTestFactory(
38 content::BrowserContext* context) {
39 content::BrowserThread::ID id;
40 CHECK(content::BrowserThread::GetCurrentThreadIdentifier(&id));
41 return extensions::ApiResourceManager<
42 extensions::BluetoothApiSocket>::CreateApiResourceManagerForTest(context,
43 id);
44 }
45
46 class FakeEventRouter : public extensions::EventRouter {
47 public:
48 explicit FakeEventRouter(Profile* profile) : EventRouter(profile, NULL) {}
49
50 virtual void DispatchEventToExtension(const std::string& extension_id,
51 scoped_ptr<extensions::Event> event)
52 OVERRIDE {
53 extension_id_ = extension_id;
54 event_ = event.Pass();
55 }
56
57 std::string extension_id() const { return extension_id_; }
58
59 const extensions::Event* event() const { return event_.get(); }
60
61 private:
62 std::string extension_id_;
63 scoped_ptr<extensions::Event> event_;
64
65 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter);
66 };
67
68 class FakeExtensionSystem : public extensions::TestExtensionSystem {
69 public:
70 explicit FakeExtensionSystem(Profile* profile)
71 : extensions::TestExtensionSystem(profile) {}
72
73 virtual extensions::EventRouter* event_router() OVERRIDE {
74 if (!fake_event_router_)
75 fake_event_router_.reset(new FakeEventRouter(profile_));
76 return fake_event_router_.get();
77 }
78
79 private:
80 scoped_ptr<FakeEventRouter> fake_event_router_;
81
82 DISALLOW_COPY_AND_ASSIGN(FakeExtensionSystem);
83 };
84
85 BrowserContextKeyedService* BuildFakeExtensionSystem(
86 content::BrowserContext* profile) {
87 return new FakeExtensionSystem(static_cast<Profile*>(profile));
88 }
89
90 } // namespace
91
92 namespace extensions {
93
94 namespace bluetooth = api::bluetooth;
95
96 class ExtensionBluetoothApiTest : public testing::Test {
97 public:
98 ExtensionBluetoothApiTest()
99 : profile_manager_(TestingBrowserProcess::GetGlobal()),
100 mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()),
101 test_profile_(NULL) {}
102
103 virtual void SetUp() OVERRIDE {
104 testing::Test::SetUp();
105 ASSERT_TRUE(profile_manager_.SetUp());
106 test_profile_ = profile_manager_.CreateTestingProfile("test");
107 ApiResourceManager<BluetoothApiSocket>::GetFactoryInstance()
108 ->SetTestingFactoryAndUse(test_profile_, ApiResourceManagerTestFactory);
109 }
110
111 virtual void TearDown() OVERRIDE {
112 // Some profile-dependent services rely on UI thread to clean up. We make
113 // sure they are properly cleaned up by running the UI message loop until
114 // idle.
115 test_profile_ = NULL;
116 profile_manager_.DeleteTestingProfile("test");
117 base::RunLoop run_loop;
118 run_loop.RunUntilIdle();
119 }
120
121 protected:
122 BluetoothAPI* bluetooth_api() {
123 if (!bluetooth_api_) {
124 bluetooth_api_.reset(new BluetoothAPI(test_profile_));
125 }
126 return bluetooth_api_.get();
127 }
128
129 content::TestBrowserThreadBundle thread_bundle_;
130 TestingProfileManager profile_manager_;
131 scoped_refptr<testing::StrictMock<device::MockBluetoothAdapter> >
132 mock_adapter_;
133 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_;
134 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_;
135 // Profiles are weak pointers, owned by |profile_manager_|.
136 TestingProfile* test_profile_;
137
138 private:
139 scoped_ptr<BluetoothAPI> bluetooth_api_;
140 };
141
142 TEST_F(ExtensionBluetoothApiTest, DispatchConnectionEvent) {
143 FakeExtensionSystem* fake_extension_system =
144 static_cast<FakeExtensionSystem*>(
145 ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse(
146 test_profile_, &BuildFakeExtensionSystem));
147
148 testing::NiceMock<device::MockBluetoothDevice> mock_device(
149 mock_adapter_.get(), 0, "device name", "device address", true, false);
150 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket(
151 new testing::NiceMock<device::MockBluetoothSocket>());
152
153 bluetooth_api()->event_router()->AddProfile(
154 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_);
155
156 bluetooth_api()->DispatchConnectionEvent(
157 kTestExtensionId, kAudioProfileUuid, &mock_device, mock_socket);
158 base::RunLoop().RunUntilIdle();
159
160 FakeEventRouter* fake_event_router =
161 static_cast<FakeEventRouter*>(fake_extension_system->event_router());
162
163 ASSERT_TRUE(fake_event_router->event());
164 EXPECT_STREQ(kTestExtensionId, fake_event_router->extension_id().c_str());
165 EXPECT_STREQ(bluetooth::OnConnection::kEventName,
166 fake_event_router->event()->event_name.c_str());
167
168 base::ListValue* event_args = fake_event_router->event()->event_args.get();
169 base::DictionaryValue* socket_value = NULL;
170 ASSERT_TRUE(event_args->GetDictionary(0, &socket_value));
171 int socket_id;
172 ASSERT_TRUE(socket_value->GetInteger("id", &socket_id));
173
174 // TODO(rpaquay)
175 // EXPECT_EQ(mock_socket.get(),
176 // bluetooth_api_.bluetooth_socket_manager()->Get(kTestExtensionId,
177 // socket_id));
178
179 base::DictionaryValue* profile_value = NULL;
180 ASSERT_TRUE(socket_value->GetDictionary("profile", &profile_value));
181 std::string profile_uuid;
182 ASSERT_TRUE(profile_value->GetString("uuid", &profile_uuid));
183 EXPECT_STREQ(kAudioProfileUuid, profile_uuid.c_str());
184
185 // EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
186 // TODO(rpaquay)
187 bluetooth_api()->socket_data()->Remove(kTestExtensionId, socket_id);
188 }
189
190 TEST_F(ExtensionBluetoothApiTest, DoNotDispatchConnectionEvent) {
191 FakeExtensionSystem* fake_extension_system =
192 static_cast<FakeExtensionSystem*>(
193 ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse(
194 test_profile_, &BuildFakeExtensionSystem));
195 testing::NiceMock<device::MockBluetoothDevice> mock_device(
196 mock_adapter_.get(), 0, "device name", "device address", true, false);
197 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket(
198 new testing::NiceMock<device::MockBluetoothSocket>());
199
200 // Connection event won't be dispatched for non-registered profiles.
201 bluetooth_api()->DispatchConnectionEvent(
202 "test extension id", kAudioProfileUuid, &mock_device, mock_socket);
203 base::RunLoop().RunUntilIdle();
204
205 FakeEventRouter* fake_event_router =
206 static_cast<FakeEventRouter*>(fake_extension_system->event_router());
207 EXPECT_TRUE(fake_event_router->event() == NULL);
208
209 // TODO(rpaquay)
210 // EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
211 }
212
213 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698