Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(711)

Side by Side Diff: mojo/examples/test/example_unittest.cc

Issue 399653004: Add a Mojo example apptest that runs in mojo_shell. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync and rebase. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 // TODO(msw): Use mojo_main_chromium.cc mojo_environment_chromium for base/
6 // not mojo_main_standalone.cc and mojo_environment_standalone ???
7 // (a mojo_main_chromium app can test standalone services and apps)
8
9 #include "mojo/examples/test/example_service.mojom.h"
10 #include "mojo/examples/test/example_service_impl.h"
11 #include "mojo/public/cpp/application/application_delegate.h"
12 #include "mojo/public/cpp/application/application_impl.h"
13 #include "mojo/public/cpp/bindings/callback.h"
14 #include "mojo/public/cpp/environment/environment.h"
15 #include "mojo/public/cpp/system/macros.h"
16 #include "mojo/public/cpp/utility/run_loop.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace {
20
21 // TODO(msw): Avoid globals ...
22 mojo::ApplicationImpl* g_application_impl_hack = NULL;
23 mojo::RunLoop* g_run_loop_hack = NULL;
24
25 } // namespace
26
27 namespace mojo {
28
29 namespace {
30
31 // TODO(msw): Split example_service_unittest and example_client_unittest ???
32 class ExampleClientImpl : public InterfaceImpl<ExampleClient> {
33 public:
34 explicit ExampleClientImpl() : last_pong_value_(0) {}
35 virtual ~ExampleClientImpl() {}
36 int16_t last_pong_value() const { return last_pong_value_; }
37
38 protected:
39 // InterfaceImpl<ExampleClient> overrides.
40 virtual void Pong(uint16_t pong_value) MOJO_OVERRIDE {
41 last_pong_value_ = pong_value;
42 g_run_loop_hack->Quit();
43 }
44
45 private:
46 int16_t last_pong_value_;
47 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleClientImpl);
48 };
49
50 // TODO(msw): Split example_service_unittest and example_client_unittest ???
51 class ExampleServiceTest : public testing::Test {
52 public:
53 ExampleServiceTest() {
54 g_application_impl_hack->ConnectToService("mojo:mojo_example_service",
55 &example_service_);
56 example_service_.set_client(&example_client_);
57 }
58
59 virtual ~ExampleServiceTest() MOJO_OVERRIDE {
60 example_service_.reset();
61 }
62
63 protected:
64 ExampleServicePtr example_service_;
65 ExampleClientImpl example_client_;
66
67 private:
68 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest);
69 };
70
71 TEST_F(ExampleServiceTest, Ping) {
72 EXPECT_EQ(0, example_client_.last_pong_value());
73 example_service_->Ping(1);
74 g_run_loop_hack->Run();
75 EXPECT_EQ(1, example_client_.last_pong_value());
76 }
77
78 // TODO(msw): Attempt mojo::Callbacks without base::Bind ???
79 template <typename T>
80 struct SetAndQuit : public Callback<void()>::Runnable {
81 SetAndQuit(T* val, T result) : val_(val), result_(result) {}
82 virtual ~SetAndQuit() {}
83 virtual void Run() const MOJO_OVERRIDE{
84 *val_ = result_;
85 g_run_loop_hack->Quit();
86 }
87 T* val_;
88 T result_;
89 };
90
91 TEST_F(ExampleServiceTest, RunCallback) {
92 bool was_run = false;
93 example_service_->RunCallback(SetAndQuit<bool>(&was_run, true));
94 g_run_loop_hack->Run();
95 EXPECT_TRUE(was_run);
96 }
97
98 } // namespace
99
100 } // namespace mojo
101
102 extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain(
103 MojoHandle shell_handle) {
104 mojo::Environment env;
105 mojo::RunLoop loop;
106 g_run_loop_hack = &loop;
107
108 // TODO(msw): Use mojo::ApplicationDelegate::Create() ???
109 // Add a TestApplicationDelegate helper? What would it offer ???
110 mojo::ApplicationDelegate delegate;
Ben Goodger (Google) 2014/07/30 16:53:18 I think you can implement an ApplicationDelegate i
msw 2014/08/06 22:34:40 After moving the client service impl and applicati
111 mojo::ApplicationImpl app(&delegate);
112 app.BindShell(shell_handle);
113 g_application_impl_hack = &app;
114
115 // TODO(msw): Get actual commandline arguments ...
116 int argc = 0;
117 char** argv = NULL;
118 testing::InitGoogleTest(&argc, argv);
119
120 // TODO(msw): g_run_loop_hack->Quit(); or kill some KeepAlive ???
121 // See http://crbug.com/394477 and http://crbug.com/392685
122 return RUN_ALL_TESTS() == 0 ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN;
Ben Goodger (Google) 2014/07/30 16:53:18 lifetime issues being handled by timsteele it seem
msw 2014/08/06 22:34:40 Done.
123 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698