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.
7 #include "device/serial/data_source_sender.h"
8 #include "device/serial/data_stream.mojom.h"
9 #include "extensions/renderer/api_test_base.h"
10 #include "grit/extensions_renderer_resources.h"
12 namespace extensions
{
14 // Runs tests defined in extensions/test/data/data_receiver_unittest.js
15 class DataReceiverTest
: public ApiTestBase
{
19 virtual void SetUp() OVERRIDE
{
21 env()->RegisterModule("async_waiter", IDR_ASYNC_WAITER_JS
);
22 env()->RegisterModule("data_receiver", IDR_DATA_RECEIVER_JS
);
23 env()->RegisterModule("device/serial/data_stream.mojom",
24 IDR_DATA_STREAM_MOJOM_JS
);
25 service_provider()->AddService(base::Bind(
26 &DataReceiverTest::CreateDataSource
, base::Unretained(this)));
29 virtual void TearDown() OVERRIDE
{
34 ApiTestBase::TearDown();
37 std::queue
<int32_t> error_to_send_
;
38 std::queue
<std::string
> data_to_send_
;
41 void CreateDataSource(
42 mojo::InterfaceRequest
<device::serial::DataSource
> request
) {
43 sender_
= mojo::WeakBindToRequest(
44 new device::DataSourceSender(
45 base::Bind(&DataReceiverTest::ReadyToSend
, base::Unretained(this)),
46 base::Bind(base::DoNothing
)),
50 void ReadyToSend(scoped_ptr
<device::WritableBuffer
> buffer
) {
51 if (data_to_send_
.empty() && error_to_send_
.empty())
56 if (!data_to_send_
.empty()) {
57 data
= data_to_send_
.front();
60 if (!error_to_send_
.empty()) {
61 error
= error_to_send_
.front();
69 DCHECK(buffer
->GetSize() >= static_cast<uint32_t>(data
.size()));
70 memcpy(buffer
->GetData(), data
.c_str(), data
.size());
72 buffer
->DoneWithError(data
.size(), error
);
74 buffer
->Done(data
.size());
77 scoped_refptr
<device::DataSourceSender
> sender_
;
79 DISALLOW_COPY_AND_ASSIGN(DataReceiverTest
);
82 TEST_F(DataReceiverTest
, Receive
) {
83 data_to_send_
.push("a");
84 RunTest("data_receiver_unittest.js", "testReceive");
87 TEST_F(DataReceiverTest
, ReceiveError
) {
88 error_to_send_
.push(1);
89 RunTest("data_receiver_unittest.js", "testReceiveError");
92 TEST_F(DataReceiverTest
, ReceiveDataAndError
) {
93 data_to_send_
.push("a");
94 data_to_send_
.push("b");
95 error_to_send_
.push(1);
96 RunTest("data_receiver_unittest.js", "testReceiveDataAndError");
99 TEST_F(DataReceiverTest
, ReceiveErrorThenData
) {
100 data_to_send_
.push("");
101 data_to_send_
.push("a");
102 error_to_send_
.push(1);
103 RunTest("data_receiver_unittest.js", "testReceiveErrorThenData");
106 TEST_F(DataReceiverTest
, SourceShutdown
) {
107 error_to_send_
.push(2);
108 RunTest("data_receiver_unittest.js", "testSourceShutdown");
111 } // namespace extensions