Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / renderer / api / serial / data_receiver_unittest.cc
blobf223cb3afba5d92c4fb8b2d049dfea72f66d8bcf
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_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 {
16 public:
17 DataReceiverTest() {}
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 service_provider()->AddService(base::Bind(
26 &DataReceiverTest::CreateDataSource, base::Unretained(this)));
29 virtual void TearDown() OVERRIDE {
30 if (sender_.get()) {
31 sender_->ShutDown();
32 sender_ = NULL;
34 ApiTestBase::TearDown();
37 std::queue<int32_t> error_to_send_;
38 std::queue<std::string> data_to_send_;
40 private:
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)),
47 &request);
50 void ReadyToSend(scoped_ptr<device::WritableBuffer> buffer) {
51 if (data_to_send_.empty() && error_to_send_.empty())
52 return;
54 std::string data;
55 int32_t error = 0;
56 if (!data_to_send_.empty()) {
57 data = data_to_send_.front();
58 data_to_send_.pop();
60 if (!error_to_send_.empty()) {
61 error = error_to_send_.front();
62 error_to_send_.pop();
64 if (error == 2) {
65 sender_->ShutDown();
66 sender_ = NULL;
67 return;
69 DCHECK(buffer->GetSize() >= static_cast<uint32_t>(data.size()));
70 memcpy(buffer->GetData(), data.c_str(), data.size());
71 if (error)
72 buffer->DoneWithError(data.size(), error);
73 else
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