103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// found in the LICENSE file.
403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include <queue>
603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "device/serial/data_source_sender.h"
803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "device/serial/data_stream.mojom.h"
903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "extensions/renderer/api_test_base.h"
1003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "grit/extensions_renderer_resources.h"
1103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
1203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace extensions {
1303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
1403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// Runs tests defined in extensions/test/data/data_receiver_unittest.js
1503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)class DataReceiverTest : public ApiTestBase {
1603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) public:
1703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DataReceiverTest() {}
1803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
1903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
2003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    ApiTestBase::SetUp();
2103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    env()->RegisterModule("async_waiter", IDR_ASYNC_WAITER_JS);
2203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    env()->RegisterModule("data_receiver", IDR_DATA_RECEIVER_JS);
2303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    env()->RegisterModule("device/serial/data_stream.mojom",
2403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                          IDR_DATA_STREAM_MOJOM_JS);
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    env()->RegisterModule("device/serial/data_stream_serialization.mojom",
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                          IDR_DATA_STREAM_SERIALIZATION_MOJOM_JS);
2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    service_provider()->AddService(base::Bind(
2803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        &DataReceiverTest::CreateDataSource, base::Unretained(this)));
2903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
3003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
3103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (sender_.get()) {
3303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      sender_->ShutDown();
3403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      sender_ = NULL;
3503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    }
3603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    ApiTestBase::TearDown();
3703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
3803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
3903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  std::queue<int32_t> error_to_send_;
4003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  std::queue<std::string> data_to_send_;
4103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
4203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) private:
4303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  void CreateDataSource(
4403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      mojo::InterfaceRequest<device::serial::DataSource> request) {
4503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    sender_ = mojo::WeakBindToRequest(
4603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        new device::DataSourceSender(
4703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)            base::Bind(&DataReceiverTest::ReadyToSend, base::Unretained(this)),
4803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)            base::Bind(base::DoNothing)),
4903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        &request);
5003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
5103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
5203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  void ReadyToSend(scoped_ptr<device::WritableBuffer> buffer) {
5303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    if (data_to_send_.empty() && error_to_send_.empty())
5403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return;
5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    std::string data;
5703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    int32_t error = 0;
5803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    if (!data_to_send_.empty()) {
5903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      data = data_to_send_.front();
6003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      data_to_send_.pop();
6103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    }
6203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    if (!error_to_send_.empty()) {
6303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      error = error_to_send_.front();
6403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      error_to_send_.pop();
6503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    }
6603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    if (error == 2) {
6703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      sender_->ShutDown();
6803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      sender_ = NULL;
6903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return;
7003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    }
7103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    DCHECK(buffer->GetSize() >= static_cast<uint32_t>(data.size()));
7203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    memcpy(buffer->GetData(), data.c_str(), data.size());
7303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    if (error)
7403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      buffer->DoneWithError(data.size(), error);
7503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    else
7603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      buffer->Done(data.size());
7703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
7903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_refptr<device::DataSourceSender> sender_;
8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
8103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DataReceiverTest);
8203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)};
8303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
8403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)TEST_F(DataReceiverTest, Receive) {
8503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  data_to_send_.push("a");
8603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  RunTest("data_receiver_unittest.js", "testReceive");
8703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
8803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
8903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)TEST_F(DataReceiverTest, ReceiveError) {
9003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  error_to_send_.push(1);
9103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  RunTest("data_receiver_unittest.js", "testReceiveError");
9203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
9303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
9403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)TEST_F(DataReceiverTest, ReceiveDataAndError) {
9503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  data_to_send_.push("a");
9603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  data_to_send_.push("b");
9703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  error_to_send_.push(1);
9803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  RunTest("data_receiver_unittest.js", "testReceiveDataAndError");
9903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
10003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
10103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)TEST_F(DataReceiverTest, ReceiveErrorThenData) {
10203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  data_to_send_.push("");
10303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  data_to_send_.push("a");
10403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  error_to_send_.push(1);
10503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  RunTest("data_receiver_unittest.js", "testReceiveErrorThenData");
10603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
10703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(DataReceiverTest, ReceiveBeforeAndAfterSerialization) {
1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  data_to_send_.push("a");
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  data_to_send_.push("b");
1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  RunTest("data_receiver_unittest.js",
1121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          "testReceiveBeforeAndAfterSerialization");
1131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(DataReceiverTest, ReceiveErrorSerialization) {
1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  error_to_send_.push(1);
1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  error_to_send_.push(3);
1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  RunTest("data_receiver_unittest.js", "testReceiveErrorSerialization");
1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(DataReceiverTest, ReceiveDataAndErrorSerialization) {
1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  data_to_send_.push("a");
1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  data_to_send_.push("b");
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  error_to_send_.push(1);
1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  error_to_send_.push(3);
1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  RunTest("data_receiver_unittest.js", "testReceiveDataAndErrorSerialization");
1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(DataReceiverTest, SerializeDuringReceive) {
1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  data_to_send_.push("a");
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  RunTest("data_receiver_unittest.js", "testSerializeDuringReceive");
1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(DataReceiverTest, SerializeAfterClose) {
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  data_to_send_.push("a");
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  RunTest("data_receiver_unittest.js", "testSerializeAfterClose");
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
13903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)TEST_F(DataReceiverTest, SourceShutdown) {
14003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  error_to_send_.push(2);
14103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  RunTest("data_receiver_unittest.js", "testSourceShutdown");
14203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
14303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
14403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}  // namespace extensions
145