Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / bindings / tests / array_unittest.cc
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.
4
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"
12
13 namespace mojo {
14 namespace test {
15 namespace {
16
17 // Tests that basic Array operations work.
18 TEST(ArrayTest, Basic) {
19   Environment env;
20
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());
24
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);
29     builder[i] = val;
30     EXPECT_EQ(val, builder.at(i));
31   }
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]);
36   }
37 }
38
39 // Tests that basic Array<bool> operations work, and that it's packed into 1
40 // bit per element.
41 TEST(ArrayTest, Bool) {
42   Environment env;
43
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());
47
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;
53     builder[i] = val;
54     EXPECT_EQ(val, builder.at(i));
55   }
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]);
60   }
61 }
62
63 // Tests that Array<Handle> supports transferring handles.
64 TEST(ArrayTest, Handle) {
65   Environment env;
66
67   AllocationScope scope;
68
69   ScopedMessagePipeHandle pipe0, pipe1;
70   CreateMessagePipe(&pipe0, &pipe1);
71
72   Array<MessagePipeHandle>::Builder handles_builder(2);
73   handles_builder[0] = pipe0.Pass();
74   handles_builder[1].reset(pipe1.release());
75
76   EXPECT_FALSE(pipe0.is_valid());
77   EXPECT_FALSE(pipe1.is_valid());
78
79   Array<MessagePipeHandle> handles = handles_builder.Finish();
80   EXPECT_TRUE(handles[0].is_valid());
81   EXPECT_TRUE(handles[1].is_valid());
82
83   pipe0 = handles[0].Pass();
84   EXPECT_TRUE(pipe0.is_valid());
85   EXPECT_FALSE(handles[0].is_valid());
86 }
87
88 // Tests that Array<Handle> supports closing handles.
89 TEST(ArrayTest, HandlesAreClosed) {
90   Environment env;
91
92   ScopedMessagePipeHandle msg_pipe0, msg_pipe1;
93   CreateMessagePipe(&msg_pipe0, &msg_pipe1);
94
95   ScopedHandle pipe0 = ScopedHandle::From(msg_pipe0.Pass());
96   ScopedHandle pipe1 = ScopedHandle::From(msg_pipe1.Pass());
97
98   MojoHandle pipe0_value = pipe0.get().value();
99   MojoHandle pipe1_value = pipe1.get().value();
100
101   {
102     AllocationScope scope;
103
104     Array<Handle>::Builder handles_builder(2);
105     handles_builder[0] = pipe0.Pass();
106     handles_builder[1].reset(pipe1.release());
107
108     MOJO_ALLOW_UNUSED Array<Handle> handles =
109         handles_builder.Finish();
110   }
111
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));
115 }
116
117 // Tests that Array<MessagePipeHandle> supports closing handles.
118 TEST(ArrayTest, MessagePipeHandlesAreClosed) {
119   Environment env;
120
121   ScopedMessagePipeHandle pipe0, pipe1;
122   CreateMessagePipe(&pipe0, &pipe1);
123
124   MojoHandle pipe0_value = pipe0.get().value();
125   MojoHandle pipe1_value = pipe1.get().value();
126
127   {
128     AllocationScope scope;
129
130     Array<MessagePipeHandle>::Builder handles_builder(2);
131     handles_builder[0] = pipe0.Pass();
132     handles_builder[1].reset(pipe1.release());
133
134     MOJO_ALLOW_UNUSED Array<MessagePipeHandle> handles =
135         handles_builder.Finish();
136   }
137
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));
141 }
142
143 }  // namespace
144 }  // namespace test
145 }  // namespace mojo