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 "mojo/public/cpp/bindings/allocation_scope.h"
6 #include "mojo/public/cpp/bindings/array.h"
7 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
8 #include "mojo/public/cpp/bindings/lib/scratch_buffer.h"
9 #include "mojo/public/cpp/environment/environment.h"
10 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 // Tests that basic Array operations work.
18 TEST(ArrayTest, Basic) {
21 // 8 bytes for the array, with 8 bytes left over for elements.
22 internal::FixedBuffer buf(8 + 8*sizeof(char));
23 EXPECT_EQ(16u, buf.size());
25 Array<char>::Builder builder(8);
26 EXPECT_EQ(8u, builder.size());
27 for (size_t i = 0; i < builder.size(); ++i) {
28 char val = static_cast<char>(i*2);
30 EXPECT_EQ(val, builder.at(i));
32 Array<char> array = builder.Finish();
33 for (size_t i = 0; i < array.size(); ++i) {
34 char val = static_cast<char>(i) * 2;
35 EXPECT_EQ(val, array[i]);
39 // Tests that basic Array<bool> operations work, and that it's packed into 1
41 TEST(ArrayTest, Bool) {
44 // 8 bytes for the array header, with 8 bytes left over for elements.
45 internal::FixedBuffer buf(8 + 3);
46 EXPECT_EQ(16u, buf.size());
48 // An array of 64 bools can fit into 8 bytes.
49 Array<bool>::Builder builder(64);
50 EXPECT_EQ(64u, builder.size());
51 for (size_t i = 0; i < builder.size(); ++i) {
52 bool val = i % 3 == 0;
54 EXPECT_EQ(val, builder.at(i));
56 Array<bool> array = builder.Finish();
57 for (size_t i = 0; i < array.size(); ++i) {
58 bool val = i % 3 == 0;
59 EXPECT_EQ(val, array[i]);
63 // Tests that Array<Handle> supports transferring handles.
64 TEST(ArrayTest, Handle) {
67 AllocationScope scope;
69 ScopedMessagePipeHandle pipe0, pipe1;
70 CreateMessagePipe(&pipe0, &pipe1);
72 Array<MessagePipeHandle>::Builder handles_builder(2);
73 handles_builder[0] = pipe0.Pass();
74 handles_builder[1].reset(pipe1.release());
76 EXPECT_FALSE(pipe0.is_valid());
77 EXPECT_FALSE(pipe1.is_valid());
79 Array<MessagePipeHandle> handles = handles_builder.Finish();
80 EXPECT_TRUE(handles[0].is_valid());
81 EXPECT_TRUE(handles[1].is_valid());
83 pipe0 = handles[0].Pass();
84 EXPECT_TRUE(pipe0.is_valid());
85 EXPECT_FALSE(handles[0].is_valid());
88 // Tests that Array<Handle> supports closing handles.
89 TEST(ArrayTest, HandlesAreClosed) {
92 ScopedMessagePipeHandle msg_pipe0, msg_pipe1;
93 CreateMessagePipe(&msg_pipe0, &msg_pipe1);
95 ScopedHandle pipe0 = ScopedHandle::From(msg_pipe0.Pass());
96 ScopedHandle pipe1 = ScopedHandle::From(msg_pipe1.Pass());
98 MojoHandle pipe0_value = pipe0.get().value();
99 MojoHandle pipe1_value = pipe1.get().value();
102 AllocationScope scope;
104 Array<Handle>::Builder handles_builder(2);
105 handles_builder[0] = pipe0.Pass();
106 handles_builder[1].reset(pipe1.release());
108 MOJO_ALLOW_UNUSED Array<Handle> handles =
109 handles_builder.Finish();
112 // We expect the pipes to have been closed.
113 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe0_value));
114 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe1_value));
117 // Tests that Array<MessagePipeHandle> supports closing handles.
118 TEST(ArrayTest, MessagePipeHandlesAreClosed) {
121 ScopedMessagePipeHandle pipe0, pipe1;
122 CreateMessagePipe(&pipe0, &pipe1);
124 MojoHandle pipe0_value = pipe0.get().value();
125 MojoHandle pipe1_value = pipe1.get().value();
128 AllocationScope scope;
130 Array<MessagePipeHandle>::Builder handles_builder(2);
131 handles_builder[0] = pipe0.Pass();
132 handles_builder[1].reset(pipe1.release());
134 MOJO_ALLOW_UNUSED Array<MessagePipeHandle> handles =
135 handles_builder.Finish();
138 // We expect the pipes to have been closed.
139 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe0_value));
140 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe1_value));