Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / renderer / api / serial / data_receiver_unittest.cc
blob623fcbf99a15662ca72487443897d2dcb976cf33
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 "gin/dictionary.h"
11 #include "gin/wrappable.h"
12 #include "grit/extensions_renderer_resources.h"
14 namespace extensions {
16 class DataReceiverFactory : public gin::Wrappable<DataReceiverFactory> {
17 public:
18 using Callback = base::Callback<void(
19 mojo::InterfaceRequest<device::serial::DataSource>,
20 mojo::InterfacePtr<device::serial::DataSourceClient>)>;
21 static gin::Handle<DataReceiverFactory> Create(v8::Isolate* isolate,
22 const Callback& callback) {
23 return gin::CreateHandle(isolate,
24 new DataReceiverFactory(callback, isolate));
27 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
28 v8::Isolate* isolate) override {
29 return Wrappable<DataReceiverFactory>::GetObjectTemplateBuilder(isolate)
30 .SetMethod("create", &DataReceiverFactory::CreateReceiver);
33 gin::Dictionary CreateReceiver() {
34 mojo::InterfacePtr<device::serial::DataSource> sink;
35 mojo::InterfacePtr<device::serial::DataSourceClient> client;
36 mojo::InterfaceRequest<device::serial::DataSourceClient> client_request =
37 mojo::GetProxy(&client);
38 callback_.Run(mojo::GetProxy(&sink), client.Pass());
40 gin::Dictionary result = gin::Dictionary::CreateEmpty(isolate_);
41 result.Set("source", sink.PassInterface().PassHandle().release());
42 result.Set("client", client_request.PassMessagePipe().release());
43 return result;
46 static gin::WrapperInfo kWrapperInfo;
48 private:
49 DataReceiverFactory(const Callback& callback, v8::Isolate* isolate)
50 : callback_(callback), isolate_(isolate) {}
52 base::Callback<void(mojo::InterfaceRequest<device::serial::DataSource>,
53 mojo::InterfacePtr<device::serial::DataSourceClient>)>
54 callback_;
55 v8::Isolate* isolate_;
58 gin::WrapperInfo DataReceiverFactory::kWrapperInfo = {gin::kEmbedderNativeGin};
60 // Runs tests defined in extensions/test/data/data_receiver_unittest.js
61 class DataReceiverTest : public ApiTestBase {
62 public:
63 DataReceiverTest() {}
65 void SetUp() override {
66 ApiTestBase::SetUp();
67 gin::ModuleRegistry::From(env()->context()->v8_context())
68 ->AddBuiltinModule(env()->isolate(),
69 "device/serial/data_receiver_test_factory",
70 DataReceiverFactory::Create(
71 env()->isolate(),
72 base::Bind(&DataReceiverTest::CreateDataSource,
73 base::Unretained(this))).ToV8());
76 void TearDown() override {
77 if (sender_.get()) {
78 sender_->ShutDown();
79 sender_ = NULL;
81 ApiTestBase::TearDown();
84 std::queue<int32_t> error_to_send_;
85 std::queue<std::string> data_to_send_;
87 private:
88 void CreateDataSource(
89 mojo::InterfaceRequest<device::serial::DataSource> request,
90 mojo::InterfacePtr<device::serial::DataSourceClient> client) {
91 sender_ = new device::DataSourceSender(
92 request.Pass(), client.Pass(),
93 base::Bind(&DataReceiverTest::ReadyToSend, base::Unretained(this)),
94 base::Bind(base::DoNothing));
97 void ReadyToSend(scoped_ptr<device::WritableBuffer> buffer) {
98 if (data_to_send_.empty() && error_to_send_.empty())
99 return;
101 std::string data;
102 int32_t error = 0;
103 if (!data_to_send_.empty()) {
104 data = data_to_send_.front();
105 data_to_send_.pop();
107 if (!error_to_send_.empty()) {
108 error = error_to_send_.front();
109 error_to_send_.pop();
111 DCHECK(buffer->GetSize() >= static_cast<uint32_t>(data.size()));
112 memcpy(buffer->GetData(), data.c_str(), data.size());
113 if (error)
114 buffer->DoneWithError(data.size(), error);
115 else
116 buffer->Done(data.size());
119 scoped_refptr<device::DataSourceSender> sender_;
121 DISALLOW_COPY_AND_ASSIGN(DataReceiverTest);
124 TEST_F(DataReceiverTest, Receive) {
125 data_to_send_.push("a");
126 RunTest("data_receiver_unittest.js", "testReceive");
129 TEST_F(DataReceiverTest, ReceiveError) {
130 error_to_send_.push(1);
131 RunTest("data_receiver_unittest.js", "testReceiveError");
134 TEST_F(DataReceiverTest, ReceiveDataAndError) {
135 data_to_send_.push("a");
136 data_to_send_.push("b");
137 error_to_send_.push(1);
138 RunTest("data_receiver_unittest.js", "testReceiveDataAndError");
141 TEST_F(DataReceiverTest, ReceiveErrorThenData) {
142 data_to_send_.push("");
143 data_to_send_.push("a");
144 error_to_send_.push(1);
145 RunTest("data_receiver_unittest.js", "testReceiveErrorThenData");
148 TEST_F(DataReceiverTest, ReceiveBeforeAndAfterSerialization) {
149 data_to_send_.push("a");
150 data_to_send_.push("b");
151 RunTest("data_receiver_unittest.js",
152 "testReceiveBeforeAndAfterSerialization");
155 TEST_F(DataReceiverTest, ReceiveErrorSerialization) {
156 error_to_send_.push(1);
157 error_to_send_.push(3);
158 RunTest("data_receiver_unittest.js", "testReceiveErrorSerialization");
161 TEST_F(DataReceiverTest, ReceiveDataAndErrorSerialization) {
162 data_to_send_.push("a");
163 data_to_send_.push("b");
164 error_to_send_.push(1);
165 error_to_send_.push(3);
166 RunTest("data_receiver_unittest.js", "testReceiveDataAndErrorSerialization");
169 TEST_F(DataReceiverTest, SerializeDuringReceive) {
170 data_to_send_.push("a");
171 RunTest("data_receiver_unittest.js", "testSerializeDuringReceive");
174 TEST_F(DataReceiverTest, SerializeAfterClose) {
175 data_to_send_.push("a");
176 RunTest("data_receiver_unittest.js", "testSerializeAfterClose");
179 } // namespace extensions