Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / renderer / api / serial / data_sender_unittest.cc
blob9f129e077fe23511d0d8b4bab785c518ce69c4c3
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 virtual void SetUp() OVERRIDE {
20 ApiTestBase::SetUp();
21 env()->RegisterModule("async_waiter", IDR_ASYNC_WAITER_JS);
22 env()->RegisterModule("data_sender", IDR_DATA_SENDER_JS);
23 env()->RegisterModule("device/serial/data_stream.mojom",
24 IDR_DATA_STREAM_MOJOM_JS);
25 service_provider()->AddService(
26 base::Bind(&DataSenderTest::CreateDataSink, base::Unretained(this)));
29 virtual void TearDown() OVERRIDE {
30 if (receiver_.get()) {
31 receiver_->ShutDown();
32 receiver_ = NULL;
34 EXPECT_FALSE(buffer_);
35 buffer_.reset();
36 ApiTestBase::TearDown();
39 std::queue<int32_t> error_to_report_;
40 std::queue<std::string> expected_data_;
42 private:
43 void CreateDataSink(
44 mojo::InterfaceRequest<device::serial::DataSink> request) {
45 receiver_ = mojo::WeakBindToRequest(
46 new device::DataSinkReceiver(
47 base::Bind(&DataSenderTest::ReadyToReceive, base::Unretained(this)),
48 base::Bind(&DataSenderTest::OnCancel, base::Unretained(this)),
49 base::Bind(base::DoNothing)),
50 &request);
53 void ReadyToReceive(scoped_ptr<device::ReadOnlyBuffer> buffer) {
54 std::string data(buffer->GetData(), buffer->GetSize());
55 if (expected_data_.empty()) {
56 buffer_ = buffer.Pass();
57 return;
60 std::string& expected = expected_data_.front();
61 if (expected.size() > buffer->GetSize()) {
62 EXPECT_EQ(expected.substr(0, buffer->GetSize()), data);
63 expected = expected.substr(buffer->GetSize());
64 buffer->Done(buffer->GetSize());
65 return;
67 if (expected.size() < buffer->GetSize())
68 data = data.substr(0, expected.size());
69 EXPECT_EQ(expected, data);
70 expected_data_.pop();
71 int32_t error = 0;
72 if (!error_to_report_.empty()) {
73 error = error_to_report_.front();
74 error_to_report_.pop();
76 if (error)
77 buffer->DoneWithError(data.size(), error);
78 else
79 buffer->Done(data.size());
82 void OnCancel(int32_t error) {
83 ASSERT_TRUE(buffer_);
84 buffer_->DoneWithError(0, error);
85 buffer_.reset();
88 scoped_refptr<device::DataSinkReceiver> receiver_;
89 scoped_ptr<device::ReadOnlyBuffer> buffer_;
91 DISALLOW_COPY_AND_ASSIGN(DataSenderTest);
94 TEST_F(DataSenderTest, Send) {
95 expected_data_.push("aa");
96 RunTest("data_sender_unittest.js", "testSend");
99 TEST_F(DataSenderTest, LargeSend) {
100 std::string pattern = "123";
101 std::string expected_data;
102 for (int i = 0; i < 11; i++)
103 expected_data += pattern;
104 expected_data_.push(expected_data);
105 RunTest("data_sender_unittest.js", "testLargeSend");
108 TEST_F(DataSenderTest, SendError) {
109 expected_data_.push("");
110 expected_data_.push("a");
111 error_to_report_.push(1);
112 RunTest("data_sender_unittest.js", "testSendError");
115 TEST_F(DataSenderTest, SendErrorPartialSuccess) {
116 expected_data_.push(std::string(5, 'b'));
117 expected_data_.push("a");
118 error_to_report_.push(1);
119 RunTest("data_sender_unittest.js", "testSendErrorPartialSuccess");
122 TEST_F(DataSenderTest, SendErrorBetweenPackets) {
123 expected_data_.push(std::string(2, 'b'));
124 expected_data_.push("a");
125 error_to_report_.push(1);
126 RunTest("data_sender_unittest.js", "testSendErrorBetweenPackets");
129 TEST_F(DataSenderTest, SendErrorInSecondPacket) {
130 expected_data_.push(std::string(3, 'b'));
131 expected_data_.push("a");
132 error_to_report_.push(1);
133 RunTest("data_sender_unittest.js", "testSendErrorInSecondPacket");
136 TEST_F(DataSenderTest, SendErrorInLargeSend) {
137 expected_data_.push("123456789012");
138 expected_data_.push("a");
139 error_to_report_.push(1);
140 RunTest("data_sender_unittest.js", "testSendErrorInLargeSend");
143 TEST_F(DataSenderTest, SendErrorBeforeLargeSend) {
144 expected_data_.push(std::string(2, 'b'));
145 expected_data_.push("a");
146 error_to_report_.push(1);
147 RunTest("data_sender_unittest.js", "testSendErrorBeforeLargeSend");
150 TEST_F(DataSenderTest, CancelWithoutSend) {
151 RunTest("data_sender_unittest.js", "testCancelWithoutSend");
154 TEST_F(DataSenderTest, Cancel) {
155 RunTest("data_sender_unittest.js", "testCancel");
158 TEST_F(DataSenderTest, Close) {
159 RunTest("data_sender_unittest.js", "testClose");
162 } // namespace extensions