data_receiver_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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 <queue>
6
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"
11
12namespace extensions {
13
14// Runs tests defined in extensions/test/data/data_receiver_unittest.js
15class DataReceiverTest : public ApiTestBase {
16 public:
17  DataReceiverTest() {}
18
19  virtual void SetUp() OVERRIDE {
20    ApiTestBase::SetUp();
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    env()->RegisterModule("device/serial/data_stream_serialization.mojom",
26                          IDR_DATA_STREAM_SERIALIZATION_MOJOM_JS);
27    service_provider()->AddService(base::Bind(
28        &DataReceiverTest::CreateDataSource, base::Unretained(this)));
29  }
30
31  virtual void TearDown() OVERRIDE {
32    if (sender_.get()) {
33      sender_->ShutDown();
34      sender_ = NULL;
35    }
36    ApiTestBase::TearDown();
37  }
38
39  std::queue<int32_t> error_to_send_;
40  std::queue<std::string> data_to_send_;
41
42 private:
43  void CreateDataSource(
44      mojo::InterfaceRequest<device::serial::DataSource> request) {
45    sender_ = mojo::WeakBindToRequest(
46        new device::DataSourceSender(
47            base::Bind(&DataReceiverTest::ReadyToSend, base::Unretained(this)),
48            base::Bind(base::DoNothing)),
49        &request);
50  }
51
52  void ReadyToSend(scoped_ptr<device::WritableBuffer> buffer) {
53    if (data_to_send_.empty() && error_to_send_.empty())
54      return;
55
56    std::string data;
57    int32_t error = 0;
58    if (!data_to_send_.empty()) {
59      data = data_to_send_.front();
60      data_to_send_.pop();
61    }
62    if (!error_to_send_.empty()) {
63      error = error_to_send_.front();
64      error_to_send_.pop();
65    }
66    if (error == 2) {
67      sender_->ShutDown();
68      sender_ = NULL;
69      return;
70    }
71    DCHECK(buffer->GetSize() >= static_cast<uint32_t>(data.size()));
72    memcpy(buffer->GetData(), data.c_str(), data.size());
73    if (error)
74      buffer->DoneWithError(data.size(), error);
75    else
76      buffer->Done(data.size());
77  }
78
79  scoped_refptr<device::DataSourceSender> sender_;
80
81  DISALLOW_COPY_AND_ASSIGN(DataReceiverTest);
82};
83
84TEST_F(DataReceiverTest, Receive) {
85  data_to_send_.push("a");
86  RunTest("data_receiver_unittest.js", "testReceive");
87}
88
89TEST_F(DataReceiverTest, ReceiveError) {
90  error_to_send_.push(1);
91  RunTest("data_receiver_unittest.js", "testReceiveError");
92}
93
94TEST_F(DataReceiverTest, ReceiveDataAndError) {
95  data_to_send_.push("a");
96  data_to_send_.push("b");
97  error_to_send_.push(1);
98  RunTest("data_receiver_unittest.js", "testReceiveDataAndError");
99}
100
101TEST_F(DataReceiverTest, ReceiveErrorThenData) {
102  data_to_send_.push("");
103  data_to_send_.push("a");
104  error_to_send_.push(1);
105  RunTest("data_receiver_unittest.js", "testReceiveErrorThenData");
106}
107
108TEST_F(DataReceiverTest, ReceiveBeforeAndAfterSerialization) {
109  data_to_send_.push("a");
110  data_to_send_.push("b");
111  RunTest("data_receiver_unittest.js",
112          "testReceiveBeforeAndAfterSerialization");
113}
114
115TEST_F(DataReceiverTest, ReceiveErrorSerialization) {
116  error_to_send_.push(1);
117  error_to_send_.push(3);
118  RunTest("data_receiver_unittest.js", "testReceiveErrorSerialization");
119}
120
121TEST_F(DataReceiverTest, ReceiveDataAndErrorSerialization) {
122  data_to_send_.push("a");
123  data_to_send_.push("b");
124  error_to_send_.push(1);
125  error_to_send_.push(3);
126  RunTest("data_receiver_unittest.js", "testReceiveDataAndErrorSerialization");
127}
128
129TEST_F(DataReceiverTest, SerializeDuringReceive) {
130  data_to_send_.push("a");
131  RunTest("data_receiver_unittest.js", "testSerializeDuringReceive");
132}
133
134TEST_F(DataReceiverTest, SerializeAfterClose) {
135  data_to_send_.push("a");
136  RunTest("data_receiver_unittest.js", "testSerializeAfterClose");
137}
138
139TEST_F(DataReceiverTest, SourceShutdown) {
140  error_to_send_.push(2);
141  RunTest("data_receiver_unittest.js", "testSourceShutdown");
142}
143
144}  // namespace extensions
145