OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/extensions/api/audio_modem/audio_modem_api.h" | 5 #include "chrome/browser/extensions/api/audio_modem/audio_modem_api.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 // Create a test AudioModemAPI and store the modem it uses. | 52 // Create a test AudioModemAPI and store the modem it uses. |
53 std::unique_ptr<KeyedService> ApiFactoryFunction(BrowserContext* context) { | 53 std::unique_ptr<KeyedService> ApiFactoryFunction(BrowserContext* context) { |
54 StubModem* modem = new StubModem; | 54 StubModem* modem = new StubModem; |
55 g_modems[context] = modem; | 55 g_modems[context] = modem; |
56 return base::WrapUnique(new AudioModemAPI( | 56 return base::WrapUnique(new AudioModemAPI( |
57 context, | 57 context, |
58 base::WrapUnique<audio_modem::WhispernetClient>(new StubWhispernetClient), | 58 base::WrapUnique<audio_modem::WhispernetClient>(new StubWhispernetClient), |
59 base::WrapUnique<audio_modem::Modem>(modem))); | 59 base::WrapUnique<audio_modem::Modem>(modem))); |
60 } | 60 } |
61 | 61 |
62 DictionaryValue* CreateParams(const std::string& audio_band) { | 62 std::unique_ptr<DictionaryValue> CreateParams(const std::string& audio_band) { |
63 DictionaryValue* params = new DictionaryValue; | 63 std::unique_ptr<DictionaryValue> params(new DictionaryValue); |
64 params->SetInteger("timeoutMillis", 60000); | 64 params->SetInteger("timeoutMillis", 60000); |
65 params->SetString("band", audio_band); | 65 params->SetString("band", audio_band); |
66 params->SetInteger("encoding.tokenLength", 4); | 66 params->SetInteger("encoding.tokenLength", 4); |
67 return params; | 67 return params; |
68 } | 68 } |
69 | 69 |
70 BinaryValue* CreateToken(const std::string& token) { | 70 std::unique_ptr<BinaryValue> CreateToken(const std::string& token) { |
71 return BinaryValue::CreateWithCopiedBuffer(token.c_str(), token.size()); | 71 return BinaryValue::CreateWithCopiedBuffer(token.c_str(), token.size()); |
72 } | 72 } |
73 | 73 |
74 std::unique_ptr<ListValue> CreateList(Value* single_elt) { | 74 std::unique_ptr<ListValue> CreateList(std::unique_ptr<Value> single_elt) { |
dcheng
2016/06/15 08:34:03
IIRC, we're passing something created by BinaryVal
| |
75 std::unique_ptr<ListValue> list(new ListValue); | 75 std::unique_ptr<ListValue> list(new ListValue); |
76 list->Append(single_elt); | 76 list->Append(std::move(single_elt)); |
77 return list; | 77 return list; |
78 } | 78 } |
79 | 79 |
80 std::unique_ptr<ListValue> CreateList(Value* elt1, Value* elt2) { | 80 std::unique_ptr<ListValue> CreateList(std::unique_ptr<Value> elt1, |
81 std::unique_ptr<Value> elt2) { | |
81 std::unique_ptr<ListValue> list(new ListValue); | 82 std::unique_ptr<ListValue> list(new ListValue); |
82 list->Append(elt1); | 83 list->Append(std::move(elt1)); |
83 list->Append(elt2); | 84 list->Append(std::move(elt2)); |
84 return list; | 85 return list; |
85 } | 86 } |
86 | 87 |
87 DictionaryValue* CreateReceivedToken(const std::string& token, | 88 DictionaryValue* CreateReceivedToken(const std::string& token, |
88 const std::string& audio_band) { | 89 const std::string& audio_band) { |
89 DictionaryValue* out = new DictionaryValue; | 90 DictionaryValue* out = new DictionaryValue; |
90 out->Set("token", CreateToken(token)); | 91 out->Set("token", CreateToken(token)); |
91 out->SetString("band", audio_band); | 92 out->SetString("band", audio_band); |
92 return out; | 93 return out; |
93 } | 94 } |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 events_by_extension_id_; | 220 events_by_extension_id_; |
220 }; | 221 }; |
221 | 222 |
222 TEST_F(AudioModemApiUnittest, TransmitBasic) { | 223 TEST_F(AudioModemApiUnittest, TransmitBasic) { |
223 // Start transmitting inaudibly. | 224 // Start transmitting inaudibly. |
224 EXPECT_EQ("success", RunFunction<AudioModemTransmitFunction>( | 225 EXPECT_EQ("success", RunFunction<AudioModemTransmitFunction>( |
225 CreateList(CreateParams("inaudible"), CreateToken("1234")))); | 226 CreateList(CreateParams("inaudible"), CreateToken("1234")))); |
226 EXPECT_TRUE(GetModem()->IsPlaying(INAUDIBLE)); | 227 EXPECT_TRUE(GetModem()->IsPlaying(INAUDIBLE)); |
227 | 228 |
228 // Can't cancel audible transmit - we haven't started it yet. | 229 // Can't cancel audible transmit - we haven't started it yet. |
229 EXPECT_EQ("invalidRequest", RunFunction<AudioModemStopTransmitFunction>( | 230 EXPECT_EQ("invalidRequest", |
230 CreateList(new StringValue("audible")))); | 231 RunFunction<AudioModemStopTransmitFunction>( |
232 CreateList(base::MakeUnique<StringValue>("audible")))); | |
231 | 233 |
232 // Start transmitting audibly. | 234 // Start transmitting audibly. |
233 EXPECT_EQ("success", RunFunction<AudioModemTransmitFunction>( | 235 EXPECT_EQ("success", RunFunction<AudioModemTransmitFunction>( |
234 CreateList(CreateParams("audible"), CreateToken("ABCD")))); | 236 CreateList(CreateParams("audible"), CreateToken("ABCD")))); |
235 EXPECT_TRUE(GetModem()->IsPlaying(AUDIBLE)); | 237 EXPECT_TRUE(GetModem()->IsPlaying(AUDIBLE)); |
236 | 238 |
237 // Stop audible transmit. We're still transmitting inaudibly. | 239 // Stop audible transmit. We're still transmitting inaudibly. |
238 EXPECT_EQ("success", RunFunction<AudioModemStopTransmitFunction>( | 240 EXPECT_EQ("success", RunFunction<AudioModemStopTransmitFunction>(CreateList( |
239 CreateList(new StringValue("audible")))); | 241 base::MakeUnique<StringValue>("audible")))); |
240 EXPECT_FALSE(GetModem()->IsPlaying(AUDIBLE)); | 242 EXPECT_FALSE(GetModem()->IsPlaying(AUDIBLE)); |
241 EXPECT_TRUE(GetModem()->IsPlaying(INAUDIBLE)); | 243 EXPECT_TRUE(GetModem()->IsPlaying(INAUDIBLE)); |
242 | 244 |
243 // Stop inaudible transmit. | 245 // Stop inaudible transmit. |
244 EXPECT_EQ("success", RunFunction<AudioModemStopTransmitFunction>( | 246 EXPECT_EQ("success", RunFunction<AudioModemStopTransmitFunction>(CreateList( |
245 CreateList(new StringValue("inaudible")))); | 247 base::MakeUnique<StringValue>("inaudible")))); |
246 EXPECT_FALSE(GetModem()->IsPlaying(INAUDIBLE)); | 248 EXPECT_FALSE(GetModem()->IsPlaying(INAUDIBLE)); |
247 } | 249 } |
248 | 250 |
249 TEST_F(AudioModemApiUnittest, ReceiveBasic) { | 251 TEST_F(AudioModemApiUnittest, ReceiveBasic) { |
250 // Start listening for audible tokens. | 252 // Start listening for audible tokens. |
251 EXPECT_EQ("success", RunFunction<AudioModemReceiveFunction>( | 253 EXPECT_EQ("success", RunFunction<AudioModemReceiveFunction>( |
252 CreateList(CreateParams("audible")))); | 254 CreateList(CreateParams("audible")))); |
253 EXPECT_TRUE(GetModem()->IsRecording(AUDIBLE)); | 255 EXPECT_TRUE(GetModem()->IsRecording(AUDIBLE)); |
254 | 256 |
255 // Can't cancel inaudible receive - we haven't started it yet. | 257 // Can't cancel inaudible receive - we haven't started it yet. |
256 EXPECT_EQ("invalidRequest", RunFunction<AudioModemStopReceiveFunction>( | 258 EXPECT_EQ("invalidRequest", |
257 CreateList(new StringValue("inaudible")))); | 259 RunFunction<AudioModemStopReceiveFunction>( |
260 CreateList(base::MakeUnique<StringValue>("inaudible")))); | |
258 | 261 |
259 // Send some audible tokens. | 262 // Send some audible tokens. |
260 std::vector<AudioToken> tokens; | 263 std::vector<AudioToken> tokens; |
261 tokens.push_back(AudioToken("1234", true)); | 264 tokens.push_back(AudioToken("1234", true)); |
262 tokens.push_back(AudioToken("ABCD", true)); | 265 tokens.push_back(AudioToken("ABCD", true)); |
263 tokens.push_back(AudioToken("abcd", false)); | 266 tokens.push_back(AudioToken("abcd", false)); |
264 GetModem()->DeliverTokens(tokens); | 267 GetModem()->DeliverTokens(tokens); |
265 | 268 |
266 // Check the tokens received. | 269 // Check the tokens received. |
267 EXPECT_EQ(1u, GetEvents().size()); | 270 EXPECT_EQ(1u, GetEvents().size()); |
(...skipping 14 matching lines...) Expand all Loading... | |
282 GetModem()->DeliverTokens(tokens); | 285 GetModem()->DeliverTokens(tokens); |
283 | 286 |
284 // Check the tokens received. | 287 // Check the tokens received. |
285 EXPECT_EQ(2u, GetEvents().size()); | 288 EXPECT_EQ(2u, GetEvents().size()); |
286 expected_tokens->Append(CreateReceivedToken("abcd", "inaudible")); | 289 expected_tokens->Append(CreateReceivedToken("abcd", "inaudible")); |
287 expected_tokens->Append(CreateReceivedToken("5678", "inaudible")); | 290 expected_tokens->Append(CreateReceivedToken("5678", "inaudible")); |
288 GetEvents()[1]->event_args->GetList(0, &received_tokens); | 291 GetEvents()[1]->event_args->GetList(0, &received_tokens); |
289 EXPECT_TRUE(received_tokens->Equals(expected_tokens.get())); | 292 EXPECT_TRUE(received_tokens->Equals(expected_tokens.get())); |
290 | 293 |
291 // Stop audible receive. We're still receiving inaudible. | 294 // Stop audible receive. We're still receiving inaudible. |
292 EXPECT_EQ("success", RunFunction<AudioModemStopReceiveFunction>( | 295 EXPECT_EQ("success", RunFunction<AudioModemStopReceiveFunction>(CreateList( |
293 CreateList(new StringValue("audible")))); | 296 base::MakeUnique<StringValue>("audible")))); |
294 EXPECT_FALSE(GetModem()->IsRecording(AUDIBLE)); | 297 EXPECT_FALSE(GetModem()->IsRecording(AUDIBLE)); |
295 EXPECT_TRUE(GetModem()->IsRecording(INAUDIBLE)); | 298 EXPECT_TRUE(GetModem()->IsRecording(INAUDIBLE)); |
296 | 299 |
297 // Stop inaudible receive. | 300 // Stop inaudible receive. |
298 EXPECT_EQ("success", RunFunction<AudioModemStopReceiveFunction>( | 301 EXPECT_EQ("success", RunFunction<AudioModemStopReceiveFunction>(CreateList( |
299 CreateList(new StringValue("inaudible")))); | 302 base::MakeUnique<StringValue>("inaudible")))); |
300 EXPECT_FALSE(GetModem()->IsRecording(INAUDIBLE)); | 303 EXPECT_FALSE(GetModem()->IsRecording(INAUDIBLE)); |
301 } | 304 } |
302 | 305 |
303 TEST_F(AudioModemApiUnittest, TransmitMultiple) { | 306 TEST_F(AudioModemApiUnittest, TransmitMultiple) { |
304 // Start transmit. | 307 // Start transmit. |
305 EXPECT_EQ("success", RunFunction<AudioModemTransmitFunction>( | 308 EXPECT_EQ("success", RunFunction<AudioModemTransmitFunction>( |
306 CreateList(CreateParams("audible"), CreateToken("1234")), | 309 CreateList(CreateParams("audible"), CreateToken("1234")), |
307 GetExtension("ext1"))); | 310 GetExtension("ext1"))); |
308 EXPECT_TRUE(GetModem()->IsPlaying(AUDIBLE)); | 311 EXPECT_TRUE(GetModem()->IsPlaying(AUDIBLE)); |
309 | 312 |
310 // Another extension can't interfere with the first one. | 313 // Another extension can't interfere with the first one. |
311 EXPECT_EQ("inUse", RunFunction<AudioModemTransmitFunction>( | 314 EXPECT_EQ("inUse", RunFunction<AudioModemTransmitFunction>( |
312 CreateList(CreateParams("audible"), CreateToken("ABCD")), | 315 CreateList(CreateParams("audible"), CreateToken("ABCD")), |
313 GetExtension("ext2"))); | 316 GetExtension("ext2"))); |
314 EXPECT_EQ("invalidRequest", RunFunction<AudioModemStopTransmitFunction>( | 317 EXPECT_EQ("invalidRequest", |
315 CreateList(new StringValue("audible")), GetExtension("ext2"))); | 318 RunFunction<AudioModemStopTransmitFunction>( |
319 CreateList(base::MakeUnique<StringValue>("audible")), | |
320 GetExtension("ext2"))); | |
316 EXPECT_TRUE(GetModem()->IsPlaying(AUDIBLE)); | 321 EXPECT_TRUE(GetModem()->IsPlaying(AUDIBLE)); |
317 | 322 |
318 // The other extension can use the other audio band, however. | 323 // The other extension can use the other audio band, however. |
319 EXPECT_EQ("success", RunFunction<AudioModemTransmitFunction>( | 324 EXPECT_EQ("success", RunFunction<AudioModemTransmitFunction>( |
320 CreateList(CreateParams("inaudible"), CreateToken("ABCD")), | 325 CreateList(CreateParams("inaudible"), CreateToken("ABCD")), |
321 GetExtension("ext2"))); | 326 GetExtension("ext2"))); |
322 EXPECT_TRUE(GetModem()->IsPlaying(INAUDIBLE)); | 327 EXPECT_TRUE(GetModem()->IsPlaying(INAUDIBLE)); |
323 | 328 |
324 // The first extension can change its token. | 329 // The first extension can change its token. |
325 // But the other band is still in use. | 330 // But the other band is still in use. |
326 EXPECT_EQ("success", RunFunction<AudioModemTransmitFunction>( | 331 EXPECT_EQ("success", RunFunction<AudioModemTransmitFunction>( |
327 CreateList(CreateParams("audible"), CreateToken("abcd")), | 332 CreateList(CreateParams("audible"), CreateToken("abcd")), |
328 GetExtension("ext1"))); | 333 GetExtension("ext1"))); |
329 EXPECT_EQ("inUse", RunFunction<AudioModemTransmitFunction>( | 334 EXPECT_EQ("inUse", RunFunction<AudioModemTransmitFunction>( |
330 CreateList(CreateParams("inaudible"), CreateToken("1234")), | 335 CreateList(CreateParams("inaudible"), CreateToken("1234")), |
331 GetExtension("ext1"))); | 336 GetExtension("ext1"))); |
332 | 337 |
333 // Stop transmission. | 338 // Stop transmission. |
334 EXPECT_EQ("success", RunFunction<AudioModemStopTransmitFunction>( | 339 EXPECT_EQ("success", RunFunction<AudioModemStopTransmitFunction>( |
335 CreateList(new StringValue("audible")), GetExtension("ext1"))); | 340 CreateList(base::MakeUnique<StringValue>("audible")), |
341 GetExtension("ext1"))); | |
336 EXPECT_FALSE(GetModem()->IsPlaying(AUDIBLE)); | 342 EXPECT_FALSE(GetModem()->IsPlaying(AUDIBLE)); |
337 EXPECT_EQ("success", RunFunction<AudioModemStopTransmitFunction>( | 343 EXPECT_EQ("success", |
338 CreateList(new StringValue("inaudible")), GetExtension("ext2"))); | 344 RunFunction<AudioModemStopTransmitFunction>( |
345 CreateList(base::MakeUnique<StringValue>("inaudible")), | |
346 GetExtension("ext2"))); | |
339 EXPECT_FALSE(GetModem()->IsPlaying(INAUDIBLE)); | 347 EXPECT_FALSE(GetModem()->IsPlaying(INAUDIBLE)); |
340 } | 348 } |
341 | 349 |
342 TEST_F(AudioModemApiUnittest, ReceiveMultiple) { | 350 TEST_F(AudioModemApiUnittest, ReceiveMultiple) { |
343 // Start receive. Multiple extensions can receive on the same band. | 351 // Start receive. Multiple extensions can receive on the same band. |
344 EXPECT_EQ("success", RunFunction<AudioModemReceiveFunction>( | 352 EXPECT_EQ("success", RunFunction<AudioModemReceiveFunction>( |
345 CreateList(CreateParams("inaudible")), GetExtension("ext1"))); | 353 CreateList(CreateParams("inaudible")), GetExtension("ext1"))); |
346 EXPECT_TRUE(GetModem()->IsRecording(INAUDIBLE)); | 354 EXPECT_TRUE(GetModem()->IsRecording(INAUDIBLE)); |
347 EXPECT_EQ("success", RunFunction<AudioModemReceiveFunction>( | 355 EXPECT_EQ("success", RunFunction<AudioModemReceiveFunction>( |
348 CreateList(CreateParams("inaudible")), GetExtension("ext2"))); | 356 CreateList(CreateParams("inaudible")), GetExtension("ext2"))); |
349 | 357 |
350 // Receive a token. | 358 // Receive a token. |
351 GetModem()->DeliverTokens(std::vector<AudioToken>( | 359 GetModem()->DeliverTokens(std::vector<AudioToken>( |
352 1, AudioToken("abcd", false))); | 360 1, AudioToken("abcd", false))); |
353 EXPECT_EQ(1u, GetEventsForExtension("ext1").size()); | 361 EXPECT_EQ(1u, GetEventsForExtension("ext1").size()); |
354 EXPECT_EQ(1u, GetEventsForExtension("ext2").size()); | 362 EXPECT_EQ(1u, GetEventsForExtension("ext2").size()); |
355 | 363 |
356 // Check the token received. | 364 // Check the token received. |
357 std::unique_ptr<DictionaryValue> expected_token( | 365 std::unique_ptr<DictionaryValue> expected_token( |
358 CreateReceivedToken("abcd", "inaudible")); | 366 CreateReceivedToken("abcd", "inaudible")); |
359 EXPECT_TRUE(ReceivedSingleToken(GetEventsForExtension("ext1")[0].get(), | 367 EXPECT_TRUE(ReceivedSingleToken(GetEventsForExtension("ext1")[0].get(), |
360 expected_token.get())); | 368 expected_token.get())); |
361 EXPECT_TRUE(ReceivedSingleToken(GetEventsForExtension("ext2")[0].get(), | 369 EXPECT_TRUE(ReceivedSingleToken(GetEventsForExtension("ext2")[0].get(), |
362 expected_token.get())); | 370 expected_token.get())); |
363 | 371 |
364 // If one extension stops, the modem is still receiving for the other. | 372 // If one extension stops, the modem is still receiving for the other. |
365 EXPECT_EQ("success", RunFunction<AudioModemStopReceiveFunction>( | 373 EXPECT_EQ("success", |
366 CreateList(new StringValue("inaudible")), GetExtension("ext1"))); | 374 RunFunction<AudioModemStopReceiveFunction>( |
375 CreateList(base::MakeUnique<StringValue>("inaudible")), | |
376 GetExtension("ext1"))); | |
367 EXPECT_TRUE(GetModem()->IsRecording(INAUDIBLE)); | 377 EXPECT_TRUE(GetModem()->IsRecording(INAUDIBLE)); |
368 | 378 |
369 // Receive another token. Should only go to ext2. | 379 // Receive another token. Should only go to ext2. |
370 GetModem()->DeliverTokens(std::vector<AudioToken>( | 380 GetModem()->DeliverTokens(std::vector<AudioToken>( |
371 1, AudioToken("1234", false))); | 381 1, AudioToken("1234", false))); |
372 EXPECT_EQ(1u, GetEventsForExtension("ext1").size()); | 382 EXPECT_EQ(1u, GetEventsForExtension("ext1").size()); |
373 EXPECT_EQ(2u, GetEventsForExtension("ext2").size()); | 383 EXPECT_EQ(2u, GetEventsForExtension("ext2").size()); |
374 expected_token.reset(CreateReceivedToken("1234", "inaudible")); | 384 expected_token.reset(CreateReceivedToken("1234", "inaudible")); |
375 EXPECT_TRUE(ReceivedSingleToken(GetEventsForExtension("ext2")[1].get(), | 385 EXPECT_TRUE(ReceivedSingleToken(GetEventsForExtension("ext2")[1].get(), |
376 expected_token.get())); | 386 expected_token.get())); |
377 | 387 |
378 EXPECT_EQ("success", RunFunction<AudioModemStopReceiveFunction>( | 388 EXPECT_EQ("success", |
379 CreateList(new StringValue("inaudible")), GetExtension("ext2"))); | 389 RunFunction<AudioModemStopReceiveFunction>( |
390 CreateList(base::MakeUnique<StringValue>("inaudible")), | |
391 GetExtension("ext2"))); | |
380 EXPECT_FALSE(GetModem()->IsRecording(INAUDIBLE)); | 392 EXPECT_FALSE(GetModem()->IsRecording(INAUDIBLE)); |
381 } | 393 } |
382 | 394 |
383 } // namespace extensions | 395 } // namespace extensions |
OLD | NEW |