Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / renderer / api / serial / data_sender_unittest.cc
blob461f012afcec547f696d3275a5b0e3f8fa29904a
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.
5 #include <queue>
7 #include "device/serial/data_sink_receiver.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_sender_unittest.js
15 class DataSenderTest : public ApiTestBase {
16 public:
17 DataSenderTest() {}
19 void SetUp() override {
20 ApiTestBase::SetUp();
21 service_provider()->AddService(
22 base::Bind(&DataSenderTest::CreateDataSink, base::Unretained(this)));
25 void TearDown() override {
26 if (receiver_.get()) {
27 receiver_->ShutDown();
28 receiver_ = NULL;
30 EXPECT_FALSE(buffer_);
31 buffer_.reset();
32 ApiTestBase::TearDown();
35 std::queue<int32_t> error_to_report_;
36 std::queue<std::string> expected_data_;
38 private:
39 void CreateDataSink(
40 mojo::InterfaceRequest<device::serial::DataSink> request) {
41 receiver_ = new device::DataSinkReceiver(
42 request.Pass(),
43 base::Bind(&DataSenderTest::ReadyToReceive, base::Unretained(this)),
44 base::Bind(&DataSenderTest::OnCancel, base::Unretained(this)),
45 base::Bind(base::DoNothing));
48 void ReadyToReceive(scoped_ptr<device::ReadOnlyBuffer> buffer) {
49 std::string data(buffer->GetData(), buffer->GetSize());
50 if (expected_data_.empty()) {
51 buffer_ = buffer.Pass();
52 return;
55 std::string& expected = expected_data_.front();
56 if (expected.size() > buffer->GetSize()) {
57 EXPECT_EQ(expected.substr(0, buffer->GetSize()), data);
58 expected = expected.substr(buffer->GetSize());
59 buffer->Done(buffer->GetSize());
60 return;
62 if (expected.size() < buffer->GetSize())
63 data = data.substr(0, expected.size());
64 EXPECT_EQ(expected, data);
65 expected_data_.pop();
66 int32_t error = 0;
67 if (!error_to_report_.empty()) {
68 error = error_to_report_.front();
69 error_to_report_.pop();
71 if (error)
72 buffer->DoneWithError(data.size(), error);
73 else
74 buffer->Done(data.size());
77 void OnCancel(int32_t error) {
78 ASSERT_TRUE(buffer_);
79 buffer_->DoneWithError(0, error);
80 buffer_.reset();
83 scoped_refptr<device::DataSinkReceiver> receiver_;
84 scoped_ptr<device::ReadOnlyBuffer> buffer_;
86 DISALLOW_COPY_AND_ASSIGN(DataSenderTest);
89 TEST_F(DataSenderTest, Send) {
90 expected_data_.push("aa");
91 RunTest("data_sender_unittest.js", "testSend");
94 TEST_F(DataSenderTest, LargeSend) {
95 std::string pattern = "123";
96 std::string expected_data;
97 for (int i = 0; i < 11; i++)
98 expected_data += pattern;
99 expected_data_.push(expected_data);
100 RunTest("data_sender_unittest.js", "testLargeSend");
103 TEST_F(DataSenderTest, SendError) {
104 expected_data_.push("");
105 expected_data_.push("a");
106 error_to_report_.push(1);
107 RunTest("data_sender_unittest.js", "testSendError");
110 TEST_F(DataSenderTest, SendErrorPartialSuccess) {
111 expected_data_.push(std::string(5, 'b'));
112 expected_data_.push("a");
113 error_to_report_.push(1);
114 RunTest("data_sender_unittest.js", "testSendErrorPartialSuccess");
117 TEST_F(DataSenderTest, SendErrorBetweenPackets) {
118 expected_data_.push(std::string(2, 'b'));
119 expected_data_.push("a");
120 error_to_report_.push(1);
121 RunTest("data_sender_unittest.js", "testSendErrorBetweenPackets");
124 TEST_F(DataSenderTest, SendErrorInSecondPacket) {
125 expected_data_.push(std::string(3, 'b'));
126 expected_data_.push("a");
127 error_to_report_.push(1);
128 RunTest("data_sender_unittest.js", "testSendErrorInSecondPacket");
131 TEST_F(DataSenderTest, SendErrorInLargeSend) {
132 expected_data_.push("123456789012");
133 expected_data_.push("a");
134 error_to_report_.push(1);
135 RunTest("data_sender_unittest.js", "testSendErrorInLargeSend");
138 TEST_F(DataSenderTest, SendErrorBeforeLargeSend) {
139 expected_data_.push(std::string(2, 'b'));
140 expected_data_.push("a");
141 error_to_report_.push(1);
142 RunTest("data_sender_unittest.js", "testSendErrorBeforeLargeSend");
145 TEST_F(DataSenderTest, CancelWithoutSend) {
146 RunTest("data_sender_unittest.js", "testCancelWithoutSend");
149 TEST_F(DataSenderTest, Cancel) {
150 RunTest("data_sender_unittest.js", "testCancel");
153 TEST_F(DataSenderTest, Close) {
154 RunTest("data_sender_unittest.js", "testClose");
157 TEST_F(DataSenderTest, SendAfterSerialization) {
158 expected_data_.push("aa");
159 RunTest("data_sender_unittest.js", "testSendAfterSerialization");
162 TEST_F(DataSenderTest, SendErrorAfterSerialization) {
163 expected_data_.push("");
164 expected_data_.push("a");
165 error_to_report_.push(1);
166 RunTest("data_sender_unittest.js", "testSendErrorAfterSerialization");
169 TEST_F(DataSenderTest, CancelAfterSerialization) {
170 RunTest("data_sender_unittest.js", "testCancelAfterSerialization");
173 TEST_F(DataSenderTest, SerializeCancelsSendsInProgress) {
174 RunTest("data_sender_unittest.js", "testSerializeCancelsSendsInProgress");
177 TEST_F(DataSenderTest, SerializeWaitsForCancel) {
178 RunTest("data_sender_unittest.js", "testSerializeWaitsForCancel");
181 TEST_F(DataSenderTest, SerializeAfterClose) {
182 RunTest("data_sender_unittest.js", "testSerializeAfterClose");
185 } // namespace extensions