| 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 #include <string.h> |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "mojom/sample_service.h" | 11 #include "mojom/sample_service.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace sample { | 14 namespace sample { |
| 15 | 15 |
| 16 // Set this variable to true to print the binary message in hex. |
| 17 bool g_dump_message_as_hex = true; |
| 18 |
| 16 // Make a sample |Foo| in the given |ScratchBuffer|. | 19 // Make a sample |Foo| in the given |ScratchBuffer|. |
| 17 Foo* MakeFoo(mojo::ScratchBuffer* buf) { | 20 Foo* MakeFoo(mojo::ScratchBuffer* buf) { |
| 18 const std::string kName("foopy"); | 21 const std::string kName("foopy"); |
| 19 mojo::String* name = mojo::String::NewCopyOf(buf, kName); | 22 mojo::String* name = mojo::String::NewCopyOf(buf, kName); |
| 20 | 23 |
| 21 Bar* bar = Bar::New(buf); | 24 Bar* bar = Bar::New(buf); |
| 22 bar->set_alpha(20); | 25 bar->set_alpha(20); |
| 23 bar->set_beta(40); | 26 bar->set_beta(40); |
| 24 bar->set_gamma(60); | 27 bar->set_gamma(60); |
| 25 | 28 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 Print(depth, "b", foo->b()); | 170 Print(depth, "b", foo->b()); |
| 168 Print(depth, "c", foo->c()); | 171 Print(depth, "c", foo->c()); |
| 169 Print(depth, "bar", foo->bar()); | 172 Print(depth, "bar", foo->bar()); |
| 170 Print(depth, "extra_bars", foo->extra_bars()); | 173 Print(depth, "extra_bars", foo->extra_bars()); |
| 171 Print(depth, "data", foo->data()); | 174 Print(depth, "data", foo->data()); |
| 172 Print(depth, "files", foo->files()); | 175 Print(depth, "files", foo->files()); |
| 173 --depth; | 176 --depth; |
| 174 } | 177 } |
| 175 } | 178 } |
| 176 | 179 |
| 180 static void DumpHex(const uint8_t* bytes, uint32_t num_bytes) { |
| 181 for (uint32_t i = 0; i < num_bytes; ++i) { |
| 182 printf("%02x", bytes[i]); |
| 183 |
| 184 if (i % 16 == 15) { |
| 185 printf("\n"); |
| 186 continue; |
| 187 } |
| 188 |
| 189 if (i % 2 == 1) |
| 190 printf(" "); |
| 191 if (i % 8 == 7) |
| 192 printf(" "); |
| 193 } |
| 194 } |
| 195 |
| 177 class ServiceImpl : public ServiceStub { | 196 class ServiceImpl : public ServiceStub { |
| 178 public: | 197 public: |
| 179 virtual void Frobinate(const Foo* foo, bool baz, mojo::Handle port) | 198 virtual void Frobinate(const Foo* foo, bool baz, mojo::Handle port) |
| 180 MOJO_OVERRIDE { | 199 MOJO_OVERRIDE { |
| 181 // Users code goes here to handle the incoming Frobinate message. | 200 // Users code goes here to handle the incoming Frobinate message. |
| 182 | 201 |
| 183 // We mainly check that we're given the expected arguments. | 202 // We mainly check that we're given the expected arguments. |
| 184 CheckFoo(foo); | 203 CheckFoo(foo); |
| 185 EXPECT_TRUE(baz); | 204 EXPECT_TRUE(baz); |
| 186 EXPECT_EQ(static_cast<MojoHandle>(10), port.value); | 205 EXPECT_EQ(static_cast<MojoHandle>(10), port.value); |
| 187 | 206 |
| 188 // Also dump the Foo structure and all of its members. | 207 // Also dump the Foo structure and all of its members. |
| 189 // TODO(vtl): Make it optional, so that the test spews less? | 208 // TODO(vtl): Make it optional, so that the test spews less? |
| 190 printf("Frobinate:\n"); | 209 printf("Frobinate:\n"); |
| 191 int depth = 1; | 210 int depth = 1; |
| 192 Print(depth, "foo", foo); | 211 Print(depth, "foo", foo); |
| 193 Print(depth, "baz", baz); | 212 Print(depth, "baz", baz); |
| 194 Print(depth, "port", port); | 213 Print(depth, "port", port); |
| 195 } | 214 } |
| 196 }; | 215 }; |
| 197 | 216 |
| 198 class SimpleMessageReceiver : public mojo::MessageReceiver { | 217 class SimpleMessageReceiver : public mojo::MessageReceiver { |
| 199 public: | 218 public: |
| 200 virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE { | 219 virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE { |
| 201 // Imagine some IPC happened here. | 220 // Imagine some IPC happened here. |
| 202 | 221 |
| 222 if (g_dump_message_as_hex) { |
| 223 DumpHex(reinterpret_cast<const uint8_t*>(message->data), |
| 224 message->data->header.num_bytes); |
| 225 } |
| 226 |
| 203 // In the receiving process, an implementation of ServiceStub is known to | 227 // In the receiving process, an implementation of ServiceStub is known to |
| 204 // the system. It receives the incoming message. | 228 // the system. It receives the incoming message. |
| 205 ServiceImpl impl; | 229 ServiceImpl impl; |
| 206 | 230 |
| 207 ServiceStub* stub = &impl; | 231 ServiceStub* stub = &impl; |
| 208 return stub->Accept(message); | 232 return stub->Accept(message); |
| 209 } | 233 } |
| 210 }; | 234 }; |
| 211 | 235 |
| 212 TEST(BindingsSampleTest, Basic) { | 236 TEST(BindingsSampleTest, Basic) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 224 mojo::ScratchBuffer buf; | 248 mojo::ScratchBuffer buf; |
| 225 Foo* foo = MakeFoo(&buf); | 249 Foo* foo = MakeFoo(&buf); |
| 226 CheckFoo(foo); | 250 CheckFoo(foo); |
| 227 | 251 |
| 228 mojo::Handle port = { 10 }; | 252 mojo::Handle port = { 10 }; |
| 229 | 253 |
| 230 service->Frobinate(foo, true, port); | 254 service->Frobinate(foo, true, port); |
| 231 } | 255 } |
| 232 | 256 |
| 233 } // namespace sample | 257 } // namespace sample |
| OLD | NEW |