summaryrefslogtreecommitdiffstats
path: root/mojo/public/tests
diff options
context:
space:
mode:
authordarin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-28 06:03:43 +0000
committerdarin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-28 06:03:43 +0000
commit811f5f2f1776b0f3ca85928779080e1cc3e283a7 (patch)
tree33657d55d5cf0b9cadcb35a9ae8efdc92d3e68e0 /mojo/public/tests
parent9bda4b5057f853a6f9ad8ea9601e335f4f868fe4 (diff)
downloadchromium_src-811f5f2f1776b0f3ca85928779080e1cc3e283a7.zip
chromium_src-811f5f2f1776b0f3ca85928779080e1cc3e283a7.tar.gz
chromium_src-811f5f2f1776b0f3ca85928779080e1cc3e283a7.tar.bz2
Mojo: re-organize public/bindings/ directory
The plan: - mojo/public/{subdir} contains public headers and scripts (if any). - mojo/public/{subdir}/lib contains private headers and .cc files (if any). - mojo/public/{subdir}/tests contains test files. This CL only implements the plan for the public/bindings/ directory. Other directories will follow. In addition, bindings header files are broken up a bit. In particular, buffer.{h,cc} are carved up so that buffer.h can only contain public bits. This necessitates creation of allocation_scope.h. I extracted type_converter.h instead of letting it be buried inside array_internal.h. This CL nukes bindings/sample/. R=viettrungluu@chromium.org Review URL: https://codereview.chromium.org/141703007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@247409 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/public/tests')
-rw-r--r--mojo/public/tests/bindings/DEPS4
-rw-r--r--mojo/public/tests/bindings/array_unittest.cc84
-rw-r--r--mojo/public/tests/bindings/buffer_unittest.cc112
-rw-r--r--mojo/public/tests/bindings/connector_unittest.cc235
-rw-r--r--mojo/public/tests/bindings/handle_passing_unittest.cc175
-rw-r--r--mojo/public/tests/bindings/math_calculator.mojom19
-rw-r--r--mojo/public/tests/bindings/remote_ptr_unittest.cc178
-rw-r--r--mojo/public/tests/bindings/sample_factory.mojom30
-rw-r--r--mojo/public/tests/bindings/sample_service.mojom51
-rw-r--r--mojo/public/tests/bindings/sample_service_unittest.cc330
-rw-r--r--mojo/public/tests/bindings/test_structs.mojom19
-rw-r--r--mojo/public/tests/bindings/type_conversion_unittest.cc233
12 files changed, 0 insertions, 1470 deletions
diff --git a/mojo/public/tests/bindings/DEPS b/mojo/public/tests/bindings/DEPS
deleted file mode 100644
index eac29ee..0000000
--- a/mojo/public/tests/bindings/DEPS
+++ /dev/null
@@ -1,4 +0,0 @@
-include_rules = [
- "-base",
- "+testing",
-]
diff --git a/mojo/public/tests/bindings/array_unittest.cc b/mojo/public/tests/bindings/array_unittest.cc
deleted file mode 100644
index dac4b50..0000000
--- a/mojo/public/tests/bindings/array_unittest.cc
+++ /dev/null
@@ -1,84 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "mojo/public/bindings/lib/array.h"
-#include "mojo/public/environment/environment.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace test {
-
-// Tests that basic Array operations work.
-TEST(ArrayTest, Basic) {
- Environment env;
-
- // 8 bytes for the array, with 8 bytes left over for elements.
- internal::FixedBuffer buf(8 + 8*sizeof(char));
- EXPECT_EQ(16u, buf.size());
-
- Array<char>::Builder builder(8);
- EXPECT_EQ(8u, builder.size());
- for (size_t i = 0; i < builder.size(); ++i) {
- char val = static_cast<char>(i*2);
- builder[i] = val;
- EXPECT_EQ(val, builder.at(i));
- }
- Array<char> array = builder.Finish();
- for (size_t i = 0; i < array.size(); ++i) {
- char val = static_cast<char>(i) * 2;
- EXPECT_EQ(val, array[i]);
- }
-}
-
-// Tests that basic Array<bool> operations work, and that it's packed into 1
-// bit per element.
-TEST(ArrayTest, Bool) {
- Environment env;
-
- // 8 bytes for the array header, with 8 bytes left over for elements.
- internal::FixedBuffer buf(8 + 3);
- EXPECT_EQ(16u, buf.size());
-
- // An array of 64 bools can fit into 8 bytes.
- Array<bool>::Builder builder(64);
- EXPECT_EQ(64u, builder.size());
- for (size_t i = 0; i < builder.size(); ++i) {
- bool val = i % 3 == 0;
- builder[i] = val;
- EXPECT_EQ(val, builder.at(i));
- }
- Array<bool> array = builder.Finish();
- for (size_t i = 0; i < array.size(); ++i) {
- bool val = i % 3 == 0;
- EXPECT_EQ(val, array[i]);
- }
-}
-
-// Tests that Array<Handle> supports transferring handles.
-TEST(ArrayTest, Handle) {
- Environment env;
-
- AllocationScope scope;
-
- ScopedMessagePipeHandle pipe0, pipe1;
- CreateMessagePipe(&pipe0, &pipe1);
-
- Array<MessagePipeHandle>::Builder handles_builder(2);
- handles_builder[0] = pipe0.Pass();
- handles_builder[1].reset(pipe1.release());
-
- EXPECT_FALSE(pipe0.is_valid());
- EXPECT_FALSE(pipe1.is_valid());
-
- Array<MessagePipeHandle> handles = handles_builder.Finish();
- EXPECT_TRUE(handles[0].is_valid());
- EXPECT_TRUE(handles[1].is_valid());
-
- pipe0 = handles[0].Pass();
- EXPECT_TRUE(pipe0.is_valid());
- EXPECT_FALSE(handles[0].is_valid());
-}
-
-} // namespace test
-} // namespace mojo
diff --git a/mojo/public/tests/bindings/buffer_unittest.cc b/mojo/public/tests/bindings/buffer_unittest.cc
deleted file mode 100644
index 06cb4ca..0000000
--- a/mojo/public/tests/bindings/buffer_unittest.cc
+++ /dev/null
@@ -1,112 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "mojo/public/bindings/lib/bindings_serialization.h"
-#include "mojo/public/bindings/lib/buffer.h"
-#include "mojo/public/environment/environment.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace test {
-
-bool IsZero(void* p_buf, size_t size) {
- char* buf = reinterpret_cast<char*>(p_buf);
- for (size_t i = 0; i < size; ++i) {
- if (buf[i] != 0)
- return false;
- }
- return true;
-}
-
-// Tests small and large allocations in ScratchBuffer.
-TEST(ScratchBufferTest, Basic) {
- Environment env;
-
- // Test that a small allocation is placed on the stack.
- internal::ScratchBuffer buf;
- void* small = buf.Allocate(10);
- EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf)));
- EXPECT_TRUE(IsZero(small, 10));
-
- // Large allocations won't be on the stack.
- void* large = buf.Allocate(100*1024);
- EXPECT_TRUE(IsZero(large, 100*1024));
- EXPECT_FALSE(large >= &buf && large < (&buf + sizeof(buf)));
-
- // But another small allocation should be back on the stack.
- small = buf.Allocate(10);
- EXPECT_TRUE(IsZero(small, 10));
- EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf)));
-}
-
-// Tests that Buffer::current() returns the correct value.
-TEST(ScratchBufferTest, Stacked) {
- Environment env;
-
- EXPECT_FALSE(Buffer::current());
-
- {
- internal::ScratchBuffer a;
- EXPECT_EQ(&a, Buffer::current());
-
- {
- internal::ScratchBuffer b;
- EXPECT_EQ(&b, Buffer::current());
- }
- }
-
- EXPECT_FALSE(Buffer::current());
-}
-
-// Tests that FixedBuffer allocates memory aligned to 8 byte boundaries.
-TEST(FixedBufferTest, Alignment) {
- Environment env;
-
- internal::FixedBuffer buf(internal::Align(10) * 2);
- ASSERT_EQ(buf.size(), 16u * 2);
-
- void* a = buf.Allocate(10);
- ASSERT_TRUE(a);
- EXPECT_TRUE(IsZero(a, 10));
- EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(a) % 8);
-
- void* b = buf.Allocate(10);
- ASSERT_TRUE(b);
- EXPECT_TRUE(IsZero(b, 10));
- EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(b) % 8);
-
- // Any more allocations would result in an assert, but we can't test that.
-}
-
-// Tests that FixedBuffer::Leak passes ownership to the caller.
-TEST(FixedBufferTest, Leak) {
- Environment env;
-
- void* ptr = NULL;
- void* buf_ptr = NULL;
- {
- internal::FixedBuffer buf(8);
- ASSERT_EQ(8u, buf.size());
-
- ptr = buf.Allocate(8);
- ASSERT_TRUE(ptr);
- void* buf_ptr = buf.Leak();
-
- // The buffer should point to the first element allocated.
- // TODO(mpcomplete): Is this a reasonable expectation?
- EXPECT_EQ(ptr, buf_ptr);
-
- // The FixedBuffer should be empty now.
- EXPECT_EQ(0u, buf.size());
- EXPECT_FALSE(buf.Leak());
- }
-
- // Since we called Leak, ptr is still writable after FixedBuffer went out of
- // scope.
- memset(ptr, 1, 8);
- free(buf_ptr);
-}
-
-} // namespace test
-} // namespace mojo
diff --git a/mojo/public/tests/bindings/connector_unittest.cc b/mojo/public/tests/bindings/connector_unittest.cc
deleted file mode 100644
index a814ffc..0000000
--- a/mojo/public/tests/bindings/connector_unittest.cc
+++ /dev/null
@@ -1,235 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include <stdlib.h>
-#include <string.h>
-
-#include "mojo/public/bindings/lib/connector.h"
-#include "mojo/public/bindings/lib/message_queue.h"
-#include "mojo/public/environment/environment.h"
-#include "mojo/public/system/macros.h"
-#include "mojo/public/utility/run_loop.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace test {
-
-class MessageAccumulator : public MessageReceiver {
- public:
- MessageAccumulator() {
- }
-
- virtual bool Accept(Message* message) MOJO_OVERRIDE {
- queue_.Push(message);
- return true;
- }
-
- bool IsEmpty() const {
- return queue_.IsEmpty();
- }
-
- void Pop(Message* message) {
- queue_.Pop(message);
- }
-
- private:
- MessageQueue queue_;
-};
-
-class ConnectorTest : public testing::Test {
- public:
- ConnectorTest() {
- }
-
- virtual void SetUp() MOJO_OVERRIDE {
- CreateMessagePipe(&handle0_, &handle1_);
- }
-
- virtual void TearDown() MOJO_OVERRIDE {
- }
-
- void AllocMessage(const char* text, Message* message) {
- size_t payload_size = strlen(text) + 1; // Plus null terminator.
- size_t num_bytes = sizeof(MessageHeader) + payload_size;
- message->data = static_cast<MessageData*>(malloc(num_bytes));
- message->data->header.num_bytes = static_cast<uint32_t>(num_bytes);
- message->data->header.name = 1;
- memcpy(message->data->payload, text, payload_size);
- }
-
- void PumpMessages() {
- loop_.RunUntilIdle();
- }
-
- protected:
- ScopedMessagePipeHandle handle0_;
- ScopedMessagePipeHandle handle1_;
-
- private:
- Environment env_;
- RunLoop loop_;
-};
-
-TEST_F(ConnectorTest, Basic) {
- internal::Connector connector0(handle0_.Pass());
- internal::Connector connector1(handle1_.Pass());
-
- const char kText[] = "hello world";
-
- Message message;
- AllocMessage(kText, &message);
-
- connector0.Accept(&message);
-
- MessageAccumulator accumulator;
- connector1.SetIncomingReceiver(&accumulator);
-
- PumpMessages();
-
- ASSERT_FALSE(accumulator.IsEmpty());
-
- Message message_received;
- accumulator.Pop(&message_received);
-
- EXPECT_EQ(std::string(kText),
- std::string(
- reinterpret_cast<char*>(message_received.data->payload)));
-}
-
-TEST_F(ConnectorTest, Basic_EarlyIncomingReceiver) {
- internal::Connector connector0(handle0_.Pass());
- internal::Connector connector1(handle1_.Pass());
-
- MessageAccumulator accumulator;
- connector1.SetIncomingReceiver(&accumulator);
-
- const char kText[] = "hello world";
-
- Message message;
- AllocMessage(kText, &message);
-
- connector0.Accept(&message);
-
- PumpMessages();
-
- ASSERT_FALSE(accumulator.IsEmpty());
-
- Message message_received;
- accumulator.Pop(&message_received);
-
- EXPECT_EQ(std::string(kText),
- std::string(
- reinterpret_cast<char*>(message_received.data->payload)));
-}
-
-TEST_F(ConnectorTest, Basic_TwoMessages) {
- internal::Connector connector0(handle0_.Pass());
- internal::Connector connector1(handle1_.Pass());
-
- const char* kText[] = { "hello", "world" };
-
- for (size_t i = 0; i < MOJO_ARRAYSIZE(kText); ++i) {
- Message message;
- AllocMessage(kText[i], &message);
-
- connector0.Accept(&message);
- }
-
- MessageAccumulator accumulator;
- connector1.SetIncomingReceiver(&accumulator);
-
- PumpMessages();
-
- for (size_t i = 0; i < MOJO_ARRAYSIZE(kText); ++i) {
- ASSERT_FALSE(accumulator.IsEmpty());
-
- Message message_received;
- accumulator.Pop(&message_received);
-
- EXPECT_EQ(std::string(kText[i]),
- std::string(
- reinterpret_cast<char*>(message_received.data->payload)));
- }
-}
-
-TEST_F(ConnectorTest, WriteToClosedPipe) {
- // Leak this, so the closed handle isn't closed again.
- MojoHandle mojo_handle = handle0_.get().value();
- internal::Connector* connector0 = new internal::Connector(handle0_.Pass());
-
- const char kText[] = "hello world";
-
- Message message;
- AllocMessage(kText, &message);
-
- // Close handle out from under the connection
- MojoClose(mojo_handle);
-
- bool ok = connector0->Accept(&message);
- EXPECT_FALSE(ok);
-
- EXPECT_TRUE(connector0->encountered_error());
-}
-
-// Enable this test once MojoWriteMessage supports passing handles.
-TEST_F(ConnectorTest, MessageWithHandles) {
- internal::Connector connector0(handle0_.Pass());
- internal::Connector connector1(handle1_.Pass());
-
- const char kText[] = "hello world";
-
- Message message;
- AllocMessage(kText, &message);
-
- ScopedMessagePipeHandle handles[2];
- CreateMessagePipe(&handles[0], &handles[1]);
- message.handles.push_back(handles[0].release());
-
- connector0.Accept(&message);
-
- // The message should have been transferred, releasing the handles.
- EXPECT_TRUE(message.handles.empty());
-
- MessageAccumulator accumulator;
- connector1.SetIncomingReceiver(&accumulator);
-
- PumpMessages();
-
- ASSERT_FALSE(accumulator.IsEmpty());
-
- Message message_received;
- accumulator.Pop(&message_received);
-
- EXPECT_EQ(std::string(kText),
- std::string(
- reinterpret_cast<char*>(message_received.data->payload)));
- ASSERT_EQ(1U, message_received.handles.size());
-
- // Now send a message to the transferred handle and confirm it's sent through
- // to the orginal pipe.
- // TODO(vtl): Do we need a better way of "downcasting" the handle types?
- ScopedMessagePipeHandle smph;
- smph.reset(MessagePipeHandle(message_received.handles[0].value()));
- message_received.handles[0] = Handle(); // |smph| now owns this handle.
-
- internal::Connector connector_received(smph.Pass());
- internal::Connector connector_original(handles[1].Pass());
-
- AllocMessage(kText, &message);
-
- connector_received.Accept(&message);
- connector_original.SetIncomingReceiver(&accumulator);
- PumpMessages();
-
- ASSERT_FALSE(accumulator.IsEmpty());
-
- accumulator.Pop(&message_received);
-
- EXPECT_EQ(std::string(kText),
- std::string(
- reinterpret_cast<char*>(message_received.data->payload)));
-}
-
-} // namespace test
-} // namespace mojo
diff --git a/mojo/public/tests/bindings/handle_passing_unittest.cc b/mojo/public/tests/bindings/handle_passing_unittest.cc
deleted file mode 100644
index ca2d2709..0000000
--- a/mojo/public/tests/bindings/handle_passing_unittest.cc
+++ /dev/null
@@ -1,175 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "mojo/public/bindings/lib/remote_ptr.h"
-#include "mojo/public/environment/environment.h"
-#include "mojo/public/tests/test_support.h"
-#include "mojo/public/utility/run_loop.h"
-#include "mojom/sample_factory.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace test {
-namespace {
-
-const char kText1[] = "hello";
-const char kText2[] = "world";
-
-class SampleFactoryImpl : public sample::Factory {
- public:
- explicit SampleFactoryImpl(ScopedMessagePipeHandle pipe)
- : client_(pipe.Pass(), this) {
- }
-
- virtual void DoStuff(const sample::Request& request,
- ScopedMessagePipeHandle pipe) MOJO_OVERRIDE {
- std::string text1;
- if (pipe.is_valid())
- EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1));
-
- std::string text2;
- if (request.pipe().is_valid()) {
- EXPECT_TRUE(ReadTextMessage(request.pipe().get(), &text2));
-
- // Ensure that simply accessing request.pipe() does not close it.
- EXPECT_TRUE(request.pipe().is_valid());
- }
-
- ScopedMessagePipeHandle pipe0;
- if (!text2.empty()) {
- CreateMessagePipe(&pipe0, &pipe1_);
- EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2));
- }
-
- AllocationScope scope;
- sample::Response::Builder response;
- response.set_x(2);
- response.set_pipe(pipe0.Pass());
- client_->DidStuff(response.Finish(), text1);
- }
-
- private:
- RemotePtr<sample::FactoryClient> client_;
- ScopedMessagePipeHandle pipe1_;
-};
-
-class SampleFactoryClientImpl : public sample::FactoryClient {
- public:
- explicit SampleFactoryClientImpl(ScopedMessagePipeHandle pipe)
- : factory_(pipe.Pass(), this),
- got_response_(false) {
- }
-
- void Start() {
- expected_text_reply_ = kText1;
-
- ScopedMessagePipeHandle pipe0;
- CreateMessagePipe(&pipe0, &pipe1_);
-
- EXPECT_TRUE(WriteTextMessage(pipe1_.get(), kText1));
-
- ScopedMessagePipeHandle pipe2;
- CreateMessagePipe(&pipe2, &pipe3_);
-
- EXPECT_TRUE(WriteTextMessage(pipe3_.get(), kText2));
-
- AllocationScope scope;
- sample::Request::Builder request;
- request.set_x(1);
- request.set_pipe(pipe2.Pass());
- factory_->DoStuff(request.Finish(), pipe0.Pass());
- }
-
- void StartNoPipes() {
- expected_text_reply_.clear();
-
- AllocationScope scope;
- sample::Request::Builder request;
- request.set_x(1);
- factory_->DoStuff(request.Finish(), ScopedMessagePipeHandle().Pass());
- }
-
- bool got_response() const {
- return got_response_;
- }
-
- virtual void DidStuff(const sample::Response& response,
- const String& text_reply) MOJO_OVERRIDE {
- EXPECT_EQ(expected_text_reply_, text_reply.To<std::string>());
-
- if (response.pipe().is_valid()) {
- std::string text2;
- EXPECT_TRUE(ReadTextMessage(response.pipe().get(), &text2));
-
- // Ensure that simply accessing response.pipe() does not close it.
- EXPECT_TRUE(response.pipe().is_valid());
-
- EXPECT_EQ(std::string(kText2), text2);
-
- // Do some more tests of handle passing:
- ScopedMessagePipeHandle p = response.pipe().Pass();
- EXPECT_TRUE(p.is_valid());
- EXPECT_FALSE(response.pipe().is_valid());
- }
-
- got_response_ = true;
- }
-
- private:
- RemotePtr<sample::Factory> factory_;
- ScopedMessagePipeHandle pipe1_;
- ScopedMessagePipeHandle pipe3_;
- std::string expected_text_reply_;
- bool got_response_;
-};
-
-} // namespace
-
-class HandlePassingTest : public testing::Test {
- public:
- void PumpMessages() {
- loop_.RunUntilIdle();
- }
-
- private:
- Environment env_;
- RunLoop loop_;
-};
-
-TEST_F(HandlePassingTest, Basic) {
- ScopedMessagePipeHandle pipe0;
- ScopedMessagePipeHandle pipe1;
- CreateMessagePipe(&pipe0, &pipe1);
-
- SampleFactoryImpl factory(pipe0.Pass());
- SampleFactoryClientImpl factory_client(pipe1.Pass());
-
- factory_client.Start();
-
- EXPECT_FALSE(factory_client.got_response());
-
- PumpMessages();
-
- EXPECT_TRUE(factory_client.got_response());
-}
-
-TEST_F(HandlePassingTest, PassInvalid) {
- ScopedMessagePipeHandle pipe0;
- ScopedMessagePipeHandle pipe1;
- CreateMessagePipe(&pipe0, &pipe1);
-
- SampleFactoryImpl factory(pipe0.Pass());
- SampleFactoryClientImpl factory_client(pipe1.Pass());
-
- factory_client.StartNoPipes();
-
- EXPECT_FALSE(factory_client.got_response());
-
- PumpMessages();
-
- EXPECT_TRUE(factory_client.got_response());
-}
-
-} // namespace test
-} // namespace mojo
diff --git a/mojo/public/tests/bindings/math_calculator.mojom b/mojo/public/tests/bindings/math_calculator.mojom
deleted file mode 100644
index 949a902..0000000
--- a/mojo/public/tests/bindings/math_calculator.mojom
+++ /dev/null
@@ -1,19 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-module math {
-
-[Peer=CalculatorUI]
-interface Calculator {
- void Clear() @0;
- void Add(double value @0) @1;
- void Multiply(double value @0) @2;
-};
-
-[Peer=Calculator]
-interface CalculatorUI {
- void Output(double value @0) @0;
-};
-
-}
diff --git a/mojo/public/tests/bindings/remote_ptr_unittest.cc b/mojo/public/tests/bindings/remote_ptr_unittest.cc
deleted file mode 100644
index f4f3b23..0000000
--- a/mojo/public/tests/bindings/remote_ptr_unittest.cc
+++ /dev/null
@@ -1,178 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "mojo/public/bindings/lib/remote_ptr.h"
-#include "mojo/public/environment/environment.h"
-#include "mojo/public/utility/run_loop.h"
-#include "mojom/math_calculator.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace test {
-
-class MathCalculatorImpl : public math::Calculator {
- public:
- virtual ~MathCalculatorImpl() {}
-
- explicit MathCalculatorImpl(ScopedMessagePipeHandle pipe)
- : ui_(pipe.Pass(), this),
- total_(0.0) {
- }
-
- virtual void Clear() MOJO_OVERRIDE {
- ui_->Output(total_);
- }
-
- virtual void Add(double value) MOJO_OVERRIDE {
- total_ += value;
- ui_->Output(total_);
- }
-
- virtual void Multiply(double value) MOJO_OVERRIDE {
- total_ *= value;
- ui_->Output(total_);
- }
-
- private:
- RemotePtr<math::CalculatorUI> ui_;
- double total_;
-};
-
-class MathCalculatorUIImpl : public math::CalculatorUI {
- public:
- explicit MathCalculatorUIImpl(ScopedMessagePipeHandle pipe)
- : calculator_(pipe.Pass(), this),
- output_(0.0) {
- }
-
- bool encountered_error() const {
- return calculator_.encountered_error();
- }
-
- void Add(double value) {
- calculator_->Add(value);
- }
-
- void Subtract(double value) {
- calculator_->Add(-value);
- }
-
- void Multiply(double value) {
- calculator_->Multiply(value);
- }
-
- void Divide(double value) {
- calculator_->Multiply(1.0 / value);
- }
-
- double GetOutput() const {
- return output_;
- }
-
- private:
- // math::CalculatorUI implementation:
- virtual void Output(double value) MOJO_OVERRIDE {
- output_ = value;
- }
-
- RemotePtr<math::Calculator> calculator_;
- double output_;
-};
-
-class RemotePtrTest : public testing::Test {
- public:
- RemotePtrTest() {
- CreateMessagePipe(&pipe0_, &pipe1_);
- }
-
- virtual ~RemotePtrTest() {
- }
-
- void PumpMessages() {
- loop_.RunUntilIdle();
- }
-
- protected:
- ScopedMessagePipeHandle pipe0_;
- ScopedMessagePipeHandle pipe1_;
-
- private:
- Environment env_;
- RunLoop loop_;
-};
-
-TEST_F(RemotePtrTest, EndToEnd) {
- // Suppose this is instantiated in a process that has pipe0_.
- MathCalculatorImpl calculator(pipe0_.Pass());
-
- // Suppose this is instantiated in a process that has pipe1_.
- MathCalculatorUIImpl calculator_ui(pipe1_.Pass());
-
- calculator_ui.Add(2.0);
- calculator_ui.Multiply(5.0);
-
- PumpMessages();
-
- EXPECT_EQ(10.0, calculator_ui.GetOutput());
-}
-
-TEST_F(RemotePtrTest, Movable) {
- RemotePtr<math::Calculator> a;
- RemotePtr<math::Calculator> b(pipe0_.Pass(), NULL);
-
- EXPECT_TRUE(a.is_null());
- EXPECT_FALSE(b.is_null());
-
- a = b.Pass();
-
- EXPECT_FALSE(a.is_null());
- EXPECT_TRUE(b.is_null());
-}
-
-TEST_F(RemotePtrTest, Resettable) {
- RemotePtr<math::Calculator> a;
-
- EXPECT_TRUE(a.is_null());
-
- MessagePipeHandle handle = pipe0_.get();
-
- a.reset(pipe0_.Pass(), NULL);
-
- EXPECT_FALSE(a.is_null());
-
- a.reset();
-
- EXPECT_TRUE(a.is_null());
-
- // Test that handle was closed.
- EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle));
-}
-
-TEST_F(RemotePtrTest, EncounteredError) {
- MathCalculatorImpl* calculator = new MathCalculatorImpl(pipe0_.Pass());
-
- MathCalculatorUIImpl calculator_ui(pipe1_.Pass());
-
- calculator_ui.Add(2.0);
- PumpMessages();
- EXPECT_EQ(2.0, calculator_ui.GetOutput());
- EXPECT_FALSE(calculator_ui.encountered_error());
-
- calculator_ui.Multiply(5.0);
- EXPECT_FALSE(calculator_ui.encountered_error());
-
- // Close the other side of the pipe.
- delete calculator;
-
- // The state change isn't picked up locally yet.
- EXPECT_FALSE(calculator_ui.encountered_error());
-
- PumpMessages();
-
- // OK, now we see the error.
- EXPECT_TRUE(calculator_ui.encountered_error());
-}
-
-} // namespace test
-} // namespace mojo
diff --git a/mojo/public/tests/bindings/sample_factory.mojom b/mojo/public/tests/bindings/sample_factory.mojom
deleted file mode 100644
index 7ae6b34..0000000
--- a/mojo/public/tests/bindings/sample_factory.mojom
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-module sample {
-
-// This sample shows how handles to MessagePipes can be sent as both parameters
-// to methods as well as fields on structs.
-
-struct Request {
- int32 x;
- handle<message_pipe> pipe;
-};
-
-struct Response {
- int32 x;
- handle<message_pipe> pipe;
-};
-
-[Peer=FactoryClient]
-interface Factory {
- void DoStuff(Request request, handle<message_pipe> pipe);
-};
-
-[Peer=Factory]
-interface FactoryClient {
- void DidStuff(Response response, string text);
-};
-
-} // module sample
diff --git a/mojo/public/tests/bindings/sample_service.mojom b/mojo/public/tests/bindings/sample_service.mojom
deleted file mode 100644
index b7d328a..0000000
--- a/mojo/public/tests/bindings/sample_service.mojom
+++ /dev/null
@@ -1,51 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-module sample {
-
-enum BarType {
- BAR_VERTICAL = 1 << 0,
- BAR_HORIZONTAL = (1 << 1) + 0,
- BAR_BOTH = BAR_VERTICAL | BAR_HORIZONTAL,
- BAR_INVALID
-};
-
-struct Bar {
- uint8 alpha = (0x100 - 1) @0;
- uint8 beta @1;
- uint8 gamma @2;
- int32 type @3;
-};
-
-[RequiredFields=7]
-struct Foo {
- string name = "Fooby" @8;
- int32 x @0;
- int32 y @1;
- bool a = true @2;
- bool b @3;
- bool c @4;
- Bar bar @5;
- Bar[] extra_bars @7;
- uint8[] data = [1,2,3] @6;
- handle<message_pipe> source @9;
- handle<data_pipe_consumer>[] input_streams @10;
- handle<data_pipe_producer>[] output_streams @11;
-};
-
-[Peer=ServiceClient]
-interface Service {
- enum BazOptions {
- BAZ_REGULAR = 0,
- BAZ_EXTRA
- };
- void Frobinate(Foo foo @0, int32 baz @1, handle<message_pipe> port @2) @0;
-};
-
-[Peer=Service]
-interface ServiceClient {
- void DidFrobinate(int32 result @0) @0;
-};
-
-}
diff --git a/mojo/public/tests/bindings/sample_service_unittest.cc b/mojo/public/tests/bindings/sample_service_unittest.cc
deleted file mode 100644
index 62b144b..0000000
--- a/mojo/public/tests/bindings/sample_service_unittest.cc
+++ /dev/null
@@ -1,330 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include <algorithm>
-#include <ostream>
-#include <string>
-
-#include "mojo/public/environment/environment.h"
-#include "mojom/sample_service.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-
-template <>
-class TypeConverter<sample::Bar, int32_t> {
- public:
- static int32_t ConvertTo(const sample::Bar& bar) {
- return static_cast<int32_t>(bar.alpha()) << 16 |
- static_cast<int32_t>(bar.beta()) << 8 |
- static_cast<int32_t>(bar.gamma());
- }
-};
-
-} // namespace mojo
-
-namespace sample {
-namespace {
-
-// Set this variable to true to print the message in hex.
-bool g_dump_message_as_hex = false;
-
-// Set this variable to true to print the message in human readable form.
-bool g_dump_message_as_text = false;
-
-// Make a sample |Foo|.
-Foo MakeFoo() {
- mojo::String name("foopy");
-
- Bar::Builder bar;
- bar.set_alpha(20);
- bar.set_beta(40);
- bar.set_gamma(60);
- bar.set_type(BAR_VERTICAL);
-
- mojo::Array<Bar>::Builder extra_bars(3);
- for (size_t i = 0; i < extra_bars.size(); ++i) {
- BarType type = i % 2 == 0 ? BAR_VERTICAL : BAR_HORIZONTAL;
- Bar::Builder bar;
- uint8_t base = static_cast<uint8_t>(i * 100);
- bar.set_alpha(base);
- bar.set_beta(base + 20);
- bar.set_gamma(base + 40);
- bar.set_type(type);
- extra_bars[i] = bar.Finish();
- }
-
- mojo::Array<uint8_t>::Builder data(10);
- for (size_t i = 0; i < data.size(); ++i)
- data[i] = static_cast<uint8_t>(data.size() - i);
-
- mojo::Array<mojo::DataPipeConsumerHandle>::Builder input_streams(2);
- mojo::Array<mojo::DataPipeProducerHandle>::Builder output_streams(2);
- for (size_t i = 0; i < input_streams.size(); ++i) {
- MojoCreateDataPipeOptions options;
- options.struct_size = sizeof(MojoCreateDataPipeOptions);
- options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
- options.element_num_bytes = 1;
- options.capacity_num_bytes = 1024;
- mojo::ScopedDataPipeProducerHandle producer;
- mojo::ScopedDataPipeConsumerHandle consumer;
- mojo::CreateDataPipe(&options, &producer, &consumer);
- input_streams[i] = consumer.Pass();
- output_streams[i] = producer.Pass();
- }
-
- mojo::ScopedMessagePipeHandle pipe0, pipe1;
- mojo::CreateMessagePipe(&pipe0, &pipe1);
-
- Foo::Builder foo;
- foo.set_name(name);
- foo.set_x(1);
- foo.set_y(2);
- foo.set_a(false);
- foo.set_b(true);
- foo.set_c(false);
- foo.set_bar(bar.Finish());
- foo.set_extra_bars(extra_bars.Finish());
- foo.set_data(data.Finish());
- foo.set_source(pipe1.Pass());
- foo.set_input_streams(input_streams.Finish());
- foo.set_output_streams(output_streams.Finish());
-
- return foo.Finish();
-}
-
-// Check that the given |Foo| is identical to the one made by |MakeFoo()|.
-void CheckFoo(const Foo& foo) {
- const std::string kName("foopy");
- ASSERT_FALSE(foo.name().is_null());
- EXPECT_EQ(kName.size(), foo.name().size());
- for (size_t i = 0; i < std::min(kName.size(), foo.name().size()); i++) {
- // Test both |operator[]| and |at|.
- EXPECT_EQ(kName[i], foo.name().at(i)) << i;
- EXPECT_EQ(kName[i], foo.name()[i]) << i;
- }
- EXPECT_EQ(kName, foo.name().To<std::string>());
-
- EXPECT_EQ(1, foo.x());
- EXPECT_EQ(2, foo.y());
- EXPECT_FALSE(foo.a());
- EXPECT_TRUE(foo.b());
- EXPECT_FALSE(foo.c());
-
- EXPECT_EQ(20, foo.bar().alpha());
- EXPECT_EQ(40, foo.bar().beta());
- EXPECT_EQ(60, foo.bar().gamma());
- EXPECT_EQ(BAR_VERTICAL, foo.bar().type());
-
- EXPECT_EQ(3u, foo.extra_bars().size());
- for (size_t i = 0; i < foo.extra_bars().size(); i++) {
- uint8_t base = static_cast<uint8_t>(i * 100);
- BarType type = i % 2 == 0 ? BAR_VERTICAL : BAR_HORIZONTAL;
- EXPECT_EQ(base, foo.extra_bars()[i].alpha()) << i;
- EXPECT_EQ(base + 20, foo.extra_bars()[i].beta()) << i;
- EXPECT_EQ(base + 40, foo.extra_bars()[i].gamma()) << i;
- EXPECT_EQ(type, foo.extra_bars()[i].type()) << i;
- }
-
- EXPECT_EQ(10u, foo.data().size());
- for (size_t i = 0; i < foo.data().size(); ++i) {
- EXPECT_EQ(static_cast<uint8_t>(foo.data().size() - i), foo.data()[i]) << i;
- }
-
- EXPECT_FALSE(foo.input_streams().is_null());
- EXPECT_EQ(2u, foo.input_streams().size());
-
- EXPECT_FALSE(foo.output_streams().is_null());
- EXPECT_EQ(2u, foo.output_streams().size());
-}
-
-void PrintSpacer(int depth) {
- for (int i = 0; i < depth; ++i)
- std::cout << " ";
-}
-
-void Print(int depth, const char* name, bool value) {
- PrintSpacer(depth);
- std::cout << name << ": " << (value ? "true" : "false") << std::endl;
-}
-
-void Print(int depth, const char* name, int32_t value) {
- PrintSpacer(depth);
- std::cout << name << ": " << value << std::endl;
-}
-
-void Print(int depth, const char* name, uint8_t value) {
- PrintSpacer(depth);
- std::cout << name << ": " << uint32_t(value) << std::endl;
-}
-
-void Print(int depth, const char* name, mojo::Handle value) {
- PrintSpacer(depth);
- std::cout << name << ": 0x" << std::hex << value.value() << std::endl;
-}
-
-void Print(int depth, const char* name, const mojo::String& str) {
- std::string s = str.To<std::string>();
- PrintSpacer(depth);
- std::cout << name << ": \"" << str.To<std::string>() << "\"" << std::endl;
-}
-
-void Print(int depth, const char* name, const Bar& bar) {
- PrintSpacer(depth);
- std::cout << name << ":" << std::endl;
- if (!bar.is_null()) {
- ++depth;
- Print(depth, "alpha", bar.alpha());
- Print(depth, "beta", bar.beta());
- Print(depth, "gamma", bar.gamma());
- Print(depth, "packed", bar.To<int32_t>());
- --depth;
- }
-}
-
-template <typename T>
-void Print(int depth, const char* name,
- const mojo::Passable<T>& passable) {
- Print(depth, name, passable.get());
-}
-
-template <typename T>
-void Print(int depth, const char* name, const mojo::Array<T>& array) {
- PrintSpacer(depth);
- std::cout << name << ":" << std::endl;
- if (!array.is_null()) {
- ++depth;
- for (size_t i = 0; i < array.size(); ++i) {
- std::stringstream buf;
- buf << i;
- Print(depth, buf.str().data(), array.at(i));
- }
- --depth;
- }
-}
-
-void Print(int depth, const char* name, const Foo& foo) {
- PrintSpacer(depth);
- std::cout << name << ":" << std::endl;
- if (!foo.is_null()) {
- ++depth;
- Print(depth, "name", foo.name());
- Print(depth, "x", foo.x());
- Print(depth, "y", foo.y());
- Print(depth, "a", foo.a());
- Print(depth, "b", foo.b());
- Print(depth, "c", foo.c());
- Print(depth, "bar", foo.bar());
- Print(depth, "extra_bars", foo.extra_bars());
- Print(depth, "data", foo.data());
- Print(depth, "source", foo.source().get());
- Print(depth, "input_streams", foo.input_streams());
- Print(depth, "output_streams", foo.output_streams());
- --depth;
- }
-}
-
-void DumpHex(const uint8_t* bytes, uint32_t num_bytes) {
- for (uint32_t i = 0; i < num_bytes; ++i) {
- std::cout << std::setw(2) << std::setfill('0') << std::hex <<
- uint32_t(bytes[i]);
-
- if (i % 16 == 15) {
- std::cout << std::endl;
- continue;
- }
-
- if (i % 2 == 1)
- std::cout << " ";
- if (i % 8 == 7)
- std::cout << " ";
- }
-}
-
-class ServiceImpl : public Service {
- public:
- virtual void Frobinate(const Foo& foo, int32_t baz,
- mojo::ScopedMessagePipeHandle port)
- MOJO_OVERRIDE {
- // Users code goes here to handle the incoming Frobinate message.
-
- // We mainly check that we're given the expected arguments.
- CheckFoo(foo);
- EXPECT_EQ(BAZ_EXTRA, baz);
-
- if (g_dump_message_as_text) {
- // Also dump the Foo structure and all of its members.
- std::cout << "Frobinate:" << std::endl;
- int depth = 1;
- Print(depth, "foo", foo);
- Print(depth, "baz", baz);
- Print(depth, "port", port.get());
- }
- }
-};
-
-class SimpleMessageReceiver : public mojo::MessageReceiver {
- public:
- virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE {
- // Imagine some IPC happened here.
-
- if (g_dump_message_as_hex) {
- DumpHex(reinterpret_cast<const uint8_t*>(message->data),
- message->data->header.num_bytes);
- }
-
- // In the receiving process, an implementation of ServiceStub is known to
- // the system. It receives the incoming message.
- ServiceImpl impl;
-
- ServiceStub stub(&impl);
- return stub.Accept(message);
- }
-};
-
-} // namespace
-
-TEST(BindingsSampleTest, Basic) {
- mojo::Environment env;
- SimpleMessageReceiver receiver;
-
- // User has a proxy to a Service somehow.
- Service* service = new ServiceProxy(&receiver);
-
- // User constructs a message to send.
-
- // Notice that it doesn't matter in what order the structs / arrays are
- // allocated. Here, the various members of Foo are allocated before Foo is
- // allocated.
-
- mojo::AllocationScope scope;
-
- Foo foo = MakeFoo();
- CheckFoo(foo);
-
- mojo::ScopedMessagePipeHandle port0, port1;
- mojo::CreateMessagePipe(&port0, &port1);
-
- service->Frobinate(foo, Service::BAZ_EXTRA, port0.Pass());
-}
-
-TEST(BindingsSampleTest, DefaultValues) {
- mojo::Environment env;
- SimpleMessageReceiver receiver;
- mojo::AllocationScope scope;
-
- Bar bar = Bar::Builder().Finish();
- EXPECT_EQ(255, bar.alpha());
-
- Foo foo = Foo::Builder().Finish();
- ASSERT_FALSE(foo.name().is_null());
- EXPECT_EQ("Fooby", foo.name().To<std::string>());
- EXPECT_TRUE(foo.a());
- EXPECT_EQ(3u, foo.data().size());
- EXPECT_EQ(1, foo.data()[0]);
- EXPECT_EQ(2, foo.data()[1]);
- EXPECT_EQ(3, foo.data()[2]);
-}
-
-} // namespace sample
diff --git a/mojo/public/tests/bindings/test_structs.mojom b/mojo/public/tests/bindings/test_structs.mojom
deleted file mode 100644
index c205e84..0000000
--- a/mojo/public/tests/bindings/test_structs.mojom
+++ /dev/null
@@ -1,19 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-module test_structs {
-
-struct Rect {
- int32 x;
- int32 y;
- int32 width;
- int32 height;
-};
-
-struct NamedRegion {
- string name;
- Rect[] rects;
-};
-
-}
diff --git a/mojo/public/tests/bindings/type_conversion_unittest.cc b/mojo/public/tests/bindings/type_conversion_unittest.cc
deleted file mode 100644
index ad0d1f7..0000000
--- a/mojo/public/tests/bindings/type_conversion_unittest.cc
+++ /dev/null
@@ -1,233 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "mojo/public/environment/environment.h"
-#include "mojom/test_structs.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace {
-
-struct RedmondRect {
- int32_t left;
- int32_t top;
- int32_t right;
- int32_t bottom;
-};
-
-struct RedmondNamedRegion {
- std::string name;
- std::vector<RedmondRect> rects;
-};
-
-} // namespace
-
-template <>
-class TypeConverter<test_structs::Rect, RedmondRect> {
- public:
- static test_structs::Rect ConvertFrom(const RedmondRect& input, Buffer* buf) {
- test_structs::Rect::Builder rect(buf);
- rect.set_x(input.left);
- rect.set_y(input.top);
- rect.set_width(input.right - input.left);
- rect.set_height(input.bottom - input.top);
- return rect.Finish();
- }
- static RedmondRect ConvertTo(const test_structs::Rect& input) {
- RedmondRect rect;
- rect.left = input.x();
- rect.top = input.y();
- rect.right = input.x() + input.width();
- rect.bottom = input.y() + input.height();
- return rect;
- }
-};
-
-template <>
-class TypeConverter<test_structs::NamedRegion, RedmondNamedRegion> {
- public:
- static test_structs::NamedRegion ConvertFrom(const RedmondNamedRegion& input,
- Buffer* buf) {
- test_structs::NamedRegion::Builder region(buf);
- region.set_name(String(input.name, buf));
- region.set_rects(mojo::Array<test_structs::Rect>(input.rects, buf));
- return region.Finish();
- }
- static RedmondNamedRegion ConvertTo(const test_structs::NamedRegion& input) {
- RedmondNamedRegion region;
- region.name = input.name().To<std::string>();
- region.rects = input.rects().To<std::vector<RedmondRect> >();
- return region;
- }
-};
-
-namespace test {
-
-class TypeConversionTest : public testing::Test {
- private:
- Environment env_;
-};
-
-TEST_F(TypeConversionTest, String) {
- AllocationScope scope;
-
- const char kText[6] = "hello";
-
- String a = std::string(kText);
- String b(kText);
- String c(static_cast<const char*>(kText));
-
- EXPECT_EQ(std::string(kText), a.To<std::string>());
- EXPECT_EQ(std::string(kText), b.To<std::string>());
- EXPECT_EQ(std::string(kText), c.To<std::string>());
-}
-
-TEST_F(TypeConversionTest, String_Null) {
- String a;
- EXPECT_TRUE(a.is_null());
- EXPECT_EQ(std::string(), a.To<std::string>());
-
- String b(static_cast<const char*>(NULL));
- EXPECT_TRUE(b.is_null());
-}
-
-TEST_F(TypeConversionTest, String_Empty) {
- AllocationScope scope;
- String a = String::Builder(0).Finish();
- EXPECT_EQ(std::string(), a.To<std::string>());
-
- String b = std::string();
- EXPECT_FALSE(b.is_null());
- EXPECT_EQ(std::string(), b.To<std::string>());
-}
-
-TEST_F(TypeConversionTest, String_ShallowCopy) {
- AllocationScope scope;
-
- String a("hello");
- String b(a);
-
- EXPECT_EQ(&a[0], &b[0]);
- EXPECT_EQ(a.To<std::string>(), b.To<std::string>());
-}
-
-TEST_F(TypeConversionTest, StringWithEmbeddedNull) {
- AllocationScope scope;
-
- const std::string kText("hel\0lo", 6);
-
- String a(kText);
- EXPECT_EQ(kText, a.To<std::string>());
-
- // Expect truncation:
- String b(kText.c_str());
- EXPECT_EQ(std::string("hel"), b.To<std::string>());
-}
-
-TEST_F(TypeConversionTest, CustomTypeConverter) {
- AllocationScope scope;
-
- test_structs::Rect::Builder rect_builder;
- rect_builder.set_x(10);
- rect_builder.set_y(20);
- rect_builder.set_width(50);
- rect_builder.set_height(45);
- test_structs::Rect rect = rect_builder.Finish();
-
- RedmondRect rr = rect.To<RedmondRect>();
- EXPECT_EQ(10, rr.left);
- EXPECT_EQ(20, rr.top);
- EXPECT_EQ(60, rr.right);
- EXPECT_EQ(65, rr.bottom);
-
- test_structs::Rect rect2(rr);
- EXPECT_EQ(rect.x(), rect2.x());
- EXPECT_EQ(rect.y(), rect2.y());
- EXPECT_EQ(rect.width(), rect2.width());
- EXPECT_EQ(rect.height(), rect2.height());
-}
-
-TEST_F(TypeConversionTest, CustomTypeConverter_Array_Null) {
- Array<test_structs::Rect> rects;
-
- std::vector<RedmondRect> redmond_rects =
- rects.To<std::vector<RedmondRect> >();
-
- EXPECT_TRUE(redmond_rects.empty());
-}
-
-TEST_F(TypeConversionTest, CustomTypeConverter_Array) {
- AllocationScope scope;
-
- const RedmondRect kBase = { 10, 20, 30, 40 };
-
- Array<test_structs::Rect>::Builder rects_builder(10);
- for (size_t i = 0; i < rects_builder.size(); ++i) {
- RedmondRect rr = kBase;
- rr.left += static_cast<int32_t>(i);
- rr.top += static_cast<int32_t>(i);
- rects_builder[i] = test_structs::Rect(rr);
- }
- Array<test_structs::Rect> rects = rects_builder.Finish();
-
- std::vector<RedmondRect> redmond_rects =
- rects.To<std::vector<RedmondRect> >();
-
- // Note: assignment broken in two lines tests default constructor with
- // assignment operator. We will also test conversion constructor.
- Array<test_structs::Rect> rects2;
- rects2 = redmond_rects;
-
- EXPECT_EQ(rects.size(), rects2.size());
- for (size_t i = 0; i < rects.size(); ++i) {
- EXPECT_EQ(rects[i].x(), rects2[i].x());
- EXPECT_EQ(rects[i].y(), rects2[i].y());
- EXPECT_EQ(rects[i].width(), rects2[i].width());
- EXPECT_EQ(rects[i].height(), rects2[i].height());
- }
-
- // Test conversion constructor.
- Array<test_structs::Rect> rects3(redmond_rects);
-
- EXPECT_EQ(rects.size(), rects3.size());
- for (size_t i = 0; i < rects.size(); ++i) {
- EXPECT_EQ(rects[i].x(), rects3[i].x());
- EXPECT_EQ(rects[i].y(), rects3[i].y());
- EXPECT_EQ(rects[i].width(), rects3[i].width());
- EXPECT_EQ(rects[i].height(), rects3[i].height());
- }
-}
-
-TEST_F(TypeConversionTest, CustomTypeConverter_Nested) {
- AllocationScope scope;
-
- RedmondNamedRegion redmond_region;
- redmond_region.name = "foopy";
-
- const RedmondRect kBase = { 10, 20, 30, 40 };
-
- for (size_t i = 0; i < 10; ++i) {
- RedmondRect rect = kBase;
- rect.left += static_cast<int32_t>(i);
- rect.top += static_cast<int32_t>(i);
- redmond_region.rects.push_back(rect);
- }
-
- // Round-trip through generated struct and TypeConverter.
-
- test_structs::NamedRegion copy = redmond_region;
- RedmondNamedRegion redmond_region2 = copy.To<RedmondNamedRegion>();
-
- EXPECT_EQ(redmond_region.name, redmond_region2.name);
- EXPECT_EQ(redmond_region.rects.size(), redmond_region2.rects.size());
- for (size_t i = 0; i < redmond_region.rects.size(); ++i) {
- EXPECT_EQ(redmond_region.rects[i].left, redmond_region2.rects[i].left);
- EXPECT_EQ(redmond_region.rects[i].top, redmond_region2.rects[i].top);
- EXPECT_EQ(redmond_region.rects[i].right, redmond_region2.rects[i].right);
- EXPECT_EQ(redmond_region.rects[i].bottom, redmond_region2.rects[i].bottom);
- }
-}
-
-} // namespace test
-} // namespace mojo