Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 #include <string.h> | |
| 6 | 7 |
| 8 #include <algorithm> | |
| 7 #include <string> | 9 #include <string> |
| 8 | 10 |
| 9 #include "mojo/public/bindings/sample/generated/sample_service.h" | 11 #include "mojo/public/bindings/sample/generated/sample_service.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | 13 |
| 11 namespace sample { | 14 namespace sample { |
| 12 | 15 |
| 16 // Make a sample |Foo| in the given |ScratchBuffer|. | |
| 17 Foo* MakeFoo(mojo::ScratchBuffer* buf) { | |
| 18 const std::string kName("foopy"); | |
| 19 mojo::String* name = mojo::String::NewCopyOf(buf, kName); | |
| 20 | |
| 21 Bar* bar = Bar::New(buf); | |
| 22 bar->set_alpha(20); | |
| 23 bar->set_beta(40); | |
| 24 bar->set_gamma(60); | |
| 25 | |
| 26 mojo::Array<Bar*>* extra_bars = mojo::Array<Bar*>::New(buf, 3); | |
| 27 for (size_t i = 0; i < extra_bars->size(); ++i) { | |
| 28 Bar* bar = Bar::New(buf); | |
| 29 uint8_t base = static_cast<uint8_t>(i * 100); | |
| 30 bar->set_alpha(base); | |
| 31 bar->set_beta(base + 20); | |
| 32 bar->set_gamma(base + 40); | |
| 33 (*extra_bars)[i] = bar; | |
| 34 } | |
| 35 | |
| 36 mojo::Array<uint8_t>* data = mojo::Array<uint8_t>::New(buf, 10); | |
| 37 for (size_t i = 0; i < data->size(); ++i) | |
| 38 (*data)[i] = static_cast<uint8_t>(data->size() - i); | |
| 39 | |
| 40 mojo::Array<mojo::Handle>* files = mojo::Array<mojo::Handle>::New(buf, 4); | |
| 41 for (size_t i = 0; i < files->size(); ++i) | |
| 42 (*files)[i].value = static_cast<MojoHandle>(0xFFFF - i); | |
| 43 | |
| 44 Foo* foo = Foo::New(buf); | |
| 45 foo->set_name(name); | |
| 46 foo->set_x(1); | |
| 47 foo->set_y(2); | |
| 48 foo->set_a(false); | |
| 49 foo->set_b(true); | |
| 50 foo->set_c(false); | |
| 51 foo->set_bar(bar); | |
| 52 foo->set_extra_bars(extra_bars); | |
| 53 foo->set_data(data); | |
| 54 foo->set_files(files); | |
| 55 | |
| 56 return foo; | |
| 57 } | |
| 58 | |
| 59 // Check that the given |Foo| is identical to the one made by |MakeFoo()|. | |
| 60 void CheckFoo(const Foo* foo) { | |
| 61 const std::string kName("foopy"); | |
| 62 EXPECT_EQ(kName.size(), foo->name()->size()); | |
| 63 for (size_t i = 0; i < std::min(kName.size(), foo->name()->size()); i++) { | |
| 64 // Test both |operator[]| and |at|. | |
| 65 EXPECT_EQ(kName[i], foo->name()->at(i)) << i; | |
| 66 EXPECT_EQ(kName[i], (*foo->name())[i]) << i; | |
| 67 } | |
| 68 EXPECT_EQ(kName, foo->name()->To<std::string>()); | |
| 69 | |
| 70 EXPECT_EQ(1, foo->x()); | |
| 71 EXPECT_EQ(2, foo->y()); | |
| 72 EXPECT_FALSE(foo->a()); | |
| 73 EXPECT_TRUE(foo->b()); | |
| 74 EXPECT_FALSE(foo->c()); | |
| 75 | |
| 76 EXPECT_EQ(20, foo->bar()->alpha()); | |
| 77 EXPECT_EQ(40, foo->bar()->beta()); | |
| 78 EXPECT_EQ(60, foo->bar()->gamma()); | |
| 79 | |
| 80 EXPECT_EQ(3u, foo->extra_bars()->size()); | |
| 81 for (size_t i = 0; i < foo->extra_bars()->size(); i++) { | |
| 82 uint8_t base = static_cast<uint8_t>(i * 100); | |
| 83 EXPECT_EQ(base, (*foo->extra_bars())[i]->alpha()) << i; | |
| 84 EXPECT_EQ(base + 20, (*foo->extra_bars())[i]->beta()) << i; | |
| 85 EXPECT_EQ(base + 40, (*foo->extra_bars())[i]->gamma()) << i; | |
| 86 } | |
| 87 | |
| 88 EXPECT_EQ(10u, foo->data()->size()); | |
| 89 for (size_t i = 0; i < foo->data()->size(); ++i) { | |
| 90 EXPECT_EQ(static_cast<uint8_t>(foo->data()->size() - i), | |
| 91 foo->data()->at(i)) << i; | |
| 92 } | |
| 93 | |
| 94 EXPECT_EQ(4u, foo->files()->size()); | |
| 95 for (size_t i = 0; i < foo->files()->size(); ++i) | |
| 96 EXPECT_EQ(static_cast<MojoHandle>(0xFFFF - i), | |
| 97 foo->files()->at(i).value) << i; | |
| 98 } | |
| 99 | |
| 100 | |
| 13 static void PrintSpacer(int depth) { | 101 static void PrintSpacer(int depth) { |
| 14 for (int i = 0; i < depth; ++i) | 102 for (int i = 0; i < depth; ++i) |
| 15 printf(" "); | 103 printf(" "); |
| 16 } | 104 } |
| 17 | 105 |
| 18 static void Print(int depth, const char* name, bool value) { | 106 static void Print(int depth, const char* name, bool value) { |
| 19 PrintSpacer(depth); | 107 PrintSpacer(depth); |
| 20 printf("%s: %s\n", name, value ? "true" : "false"); | 108 printf("%s: %s\n", name, value ? "true" : "false"); |
| 21 } | 109 } |
| 22 | 110 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 Print(depth, "files", foo->files()); | 172 Print(depth, "files", foo->files()); |
| 85 --depth; | 173 --depth; |
| 86 } | 174 } |
| 87 } | 175 } |
| 88 | 176 |
| 89 class ServiceImpl : public ServiceStub { | 177 class ServiceImpl : public ServiceStub { |
| 90 public: | 178 public: |
| 91 virtual void Frobinate(const Foo* foo, bool baz, mojo::Handle port) | 179 virtual void Frobinate(const Foo* foo, bool baz, mojo::Handle port) |
| 92 MOJO_OVERRIDE { | 180 MOJO_OVERRIDE { |
| 93 // Users code goes here to handle the incoming Frobinate message. | 181 // Users code goes here to handle the incoming Frobinate message. |
| 94 // We'll just dump the Foo structure and all of its members. | |
| 95 | 182 |
| 183 // We mainly check that we're given the expected arguments. | |
| 184 CheckFoo(foo); | |
| 185 EXPECT_TRUE(baz); | |
| 186 EXPECT_EQ(static_cast<MojoHandle>(10), port.value); | |
| 187 | |
| 188 // Also dump the Foo structure and all of its members. | |
| 189 // TODO(vtl): Remove this? Or make it optional, so that the test spews less? | |
|
darin (slow to review)
2013/11/11 21:59:40
This spewage has been useful for developing the bi
| |
| 96 printf("Frobinate:\n"); | 190 printf("Frobinate:\n"); |
| 97 | |
| 98 int depth = 1; | 191 int depth = 1; |
| 99 Print(depth, "foo", foo); | 192 Print(depth, "foo", foo); |
| 100 Print(depth, "baz", baz); | 193 Print(depth, "baz", baz); |
| 101 Print(depth, "port", port); | 194 Print(depth, "port", port); |
| 102 } | 195 } |
| 103 }; | 196 }; |
| 104 | 197 |
| 105 class SimpleMessageReceiver : public mojo::MessageReceiver { | 198 class SimpleMessageReceiver : public mojo::MessageReceiver { |
| 106 public: | 199 public: |
| 107 virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE { | 200 virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE { |
| 108 // Imagine some IPC happened here. | 201 // Imagine some IPC happened here. |
| 109 | 202 |
| 110 // In the receiving process, an implementation of ServiceStub is known to | 203 // In the receiving process, an implementation of ServiceStub is known to |
| 111 // the system. It receives the incoming message. | 204 // the system. It receives the incoming message. |
| 112 ServiceImpl impl; | 205 ServiceImpl impl; |
| 113 | 206 |
| 114 ServiceStub* stub = &impl; | 207 ServiceStub* stub = &impl; |
| 115 return stub->Accept(message); | 208 return stub->Accept(message); |
| 116 } | 209 } |
| 117 }; | 210 }; |
| 118 | 211 |
| 119 void Exercise() { | 212 TEST(BindingsSampleTest, Basic) { |
| 120 SimpleMessageReceiver receiver; | 213 SimpleMessageReceiver receiver; |
| 121 | 214 |
| 122 // User has a proxy to a Service somehow. | 215 // User has a proxy to a Service somehow. |
| 123 Service* service = new ServiceProxy(&receiver); | 216 Service* service = new ServiceProxy(&receiver); |
| 124 | 217 |
| 125 // User constructs a message to send. | 218 // User constructs a message to send. |
| 126 | 219 |
| 127 // Notice that it doesn't matter in what order the structs / arrays are | 220 // Notice that it doesn't matter in what order the structs / arrays are |
| 128 // allocated. Here, the various members of Foo are allocated before Foo is | 221 // allocated. Here, the various members of Foo are allocated before Foo is |
| 129 // allocated. | 222 // allocated. |
| 130 | 223 |
| 131 mojo::ScratchBuffer buf; | 224 mojo::ScratchBuffer buf; |
| 132 | 225 Foo* foo = MakeFoo(&buf); |
| 133 Bar* bar = Bar::New(&buf); | 226 CheckFoo(foo); |
| 134 bar->set_alpha(20); | |
| 135 bar->set_beta(40); | |
| 136 bar->set_gamma(60); | |
| 137 | |
| 138 const char kName[] = "foopy"; | |
| 139 mojo::String* name = mojo::String::NewCopyOf(&buf, std::string(kName)); | |
| 140 | |
| 141 mojo::Array<Bar*>* extra_bars = mojo::Array<Bar*>::New(&buf, 3); | |
| 142 for (size_t i = 0; i < extra_bars->size(); ++i) { | |
| 143 Bar* bar = Bar::New(&buf); | |
| 144 uint8_t base = static_cast<uint8_t>(i * 100); | |
| 145 bar->set_alpha(base); | |
| 146 bar->set_beta(base + 20); | |
| 147 bar->set_gamma(base + 40); | |
| 148 (*extra_bars)[i] = bar; | |
| 149 } | |
| 150 | |
| 151 mojo::Array<uint8_t>* data = mojo::Array<uint8_t>::New(&buf, 10); | |
| 152 for (size_t i = 0; i < data->size(); ++i) | |
| 153 (*data)[i] = static_cast<uint8_t>(data->size() - i); | |
| 154 | |
| 155 mojo::Array<mojo::Handle>* files = mojo::Array<mojo::Handle>::New(&buf, 4); | |
| 156 for (size_t i = 0; i < files->size(); ++i) | |
| 157 (*files)[i].value = static_cast<MojoHandle>(0xFFFF - i); | |
| 158 | |
| 159 Foo* foo = Foo::New(&buf); | |
| 160 foo->set_name(name); | |
| 161 foo->set_x(1); | |
| 162 foo->set_y(2); | |
| 163 foo->set_a(false); | |
| 164 foo->set_b(true); | |
| 165 foo->set_c(false); | |
| 166 foo->set_bar(bar); | |
| 167 foo->set_extra_bars(extra_bars); | |
| 168 foo->set_data(data); | |
| 169 foo->set_files(files); | |
| 170 | 227 |
| 171 mojo::Handle port = { 10 }; | 228 mojo::Handle port = { 10 }; |
| 172 | 229 |
| 173 service->Frobinate(foo, true, port); | 230 service->Frobinate(foo, true, port); |
| 174 } | 231 } |
| 175 | 232 |
| 176 } // namespace sample | 233 } // namespace sample |
| 177 | |
| 178 int main() { | |
| 179 // TODO(vtl): Convert this to a gtest and use mojo_run_all_unittests. | |
| 180 // sample::Exercise(); | |
| 181 return 0; | |
| 182 } | |
| OLD | NEW |