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

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: Address code review feedback. 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 KeyedService* 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 KeyedService* BuildFakeExtensionSystem(content::BrowserContext* profile) {
86 return new FakeExtensionSystem(static_cast<Profile*>(profile));
87 }
88
89 } // namespace
90
91 namespace extensions {
92
93 namespace bluetooth = api::bluetooth;
94
95 class ExtensionBluetoothApiTest : public testing::Test {
96 public:
97 ExtensionBluetoothApiTest()
98 : profile_manager_(TestingBrowserProcess::GetGlobal()),
99 mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()),
100 test_profile_(NULL) {}
101
102 virtual void SetUp() OVERRIDE {
103 testing::Test::SetUp();
104 ASSERT_TRUE(profile_manager_.SetUp());
105 test_profile_ = profile_manager_.CreateTestingProfile("test");
106 ApiResourceManager<BluetoothApiSocket>::GetFactoryInstance()
107 ->SetTestingFactoryAndUse(test_profile_, ApiResourceManagerTestFactory);
108 }
109
110 virtual void TearDown() OVERRIDE {
111 // Some profile-dependent services rely on UI thread to clean up. We make
112 // sure they are properly cleaned up by running the UI message loop until
113 // idle.
114 test_profile_ = NULL;
115 profile_manager_.DeleteTestingProfile("test");
116 base::RunLoop run_loop;
117 run_loop.RunUntilIdle();
118 }
119
120 protected:
121 BluetoothAPI* bluetooth_api() {
122 if (!bluetooth_api_) {
123 bluetooth_api_.reset(new BluetoothAPI(test_profile_));
124 }
125 return bluetooth_api_.get();
126 }
127
128 content::TestBrowserThreadBundle thread_bundle_;
129 TestingProfileManager profile_manager_;
130 scoped_refptr<testing::StrictMock<device::MockBluetoothAdapter> >
131 mock_adapter_;
132 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_;
133 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_;
134 // Profiles are weak pointers, owned by |profile_manager_|.
135 TestingProfile* test_profile_;
136
137 private:
138 scoped_ptr<BluetoothAPI> bluetooth_api_;
139 };
140
141 TEST_F(ExtensionBluetoothApiTest, DispatchConnectionEvent) {
142 FakeExtensionSystem* fake_extension_system =
143 static_cast<FakeExtensionSystem*>(
144 ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse(
145 test_profile_, &BuildFakeExtensionSystem));
146
147 testing::NiceMock<device::MockBluetoothDevice> mock_device(
148 mock_adapter_.get(), 0, "device name", "device address", true, false);
149 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket(
150 new testing::NiceMock<device::MockBluetoothSocket>());
151
152 bluetooth_api()->event_router()->AddProfile(
153 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_);
154
155 bluetooth_api()->DispatchConnectionEvent(
156 kTestExtensionId, kAudioProfileUuid, &mock_device, mock_socket);
157 base::RunLoop().RunUntilIdle();
158
159 FakeEventRouter* fake_event_router =
160 static_cast<FakeEventRouter*>(fake_extension_system->event_router());
161
162 ASSERT_TRUE(fake_event_router->event());
163 EXPECT_STREQ(kTestExtensionId, fake_event_router->extension_id().c_str());
164 EXPECT_STREQ(bluetooth::OnConnection::kEventName,
165 fake_event_router->event()->event_name.c_str());
166
167 base::ListValue* event_args = fake_event_router->event()->event_args.get();
168 base::DictionaryValue* socket_value = NULL;
169 ASSERT_TRUE(event_args->GetDictionary(0, &socket_value));
170 int socket_id;
171 ASSERT_TRUE(socket_value->GetInteger("id", &socket_id));
172
173 // TODO(rpaquay)
174 // EXPECT_EQ(mock_socket.get(),
175 // bluetooth_api_.bluetooth_socket_manager()->Get(kTestExtensionId,
176 // socket_id));
177
178 base::DictionaryValue* profile_value = NULL;
179 ASSERT_TRUE(socket_value->GetDictionary("profile", &profile_value));
180 std::string profile_uuid;
181 ASSERT_TRUE(profile_value->GetString("uuid", &profile_uuid));
182 EXPECT_STREQ(kAudioProfileUuid, profile_uuid.c_str());
183
184 // EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
185 // TODO(rpaquay)
186 bluetooth_api()->socket_data()->Remove(kTestExtensionId, socket_id);
187 }
188
189 TEST_F(ExtensionBluetoothApiTest, DoNotDispatchConnectionEvent) {
190 FakeExtensionSystem* fake_extension_system =
191 static_cast<FakeExtensionSystem*>(
192 ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse(
193 test_profile_, &BuildFakeExtensionSystem));
194 testing::NiceMock<device::MockBluetoothDevice> mock_device(
195 mock_adapter_.get(), 0, "device name", "device address", true, false);
196 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket(
197 new testing::NiceMock<device::MockBluetoothSocket>());
198
199 // Connection event won't be dispatched for non-registered profiles.
200 bluetooth_api()->DispatchConnectionEvent(
201 "test extension id", kAudioProfileUuid, &mock_device, mock_socket);
202 base::RunLoop().RunUntilIdle();
203
204 FakeEventRouter* fake_event_router =
205 static_cast<FakeEventRouter*>(fake_extension_system->event_router());
206 EXPECT_TRUE(fake_event_router->event() == NULL);
207
208 // TODO(rpaquay)
209 // EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
210 }
211
212 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698