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

Side by Side Diff: mojo/shell/service_manager_unittest.cc

Issue 122173004: Add test for ServiceManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update new copyrights Created 6 years, 11 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
« no previous file with comments | « mojo/shell/service_manager.cc ('k') | mojo/shell/test.mojom » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "base/message_loop/message_loop.h"
6 #include "mojo/common/bindings_support_impl.h"
7 #include "mojo/public/bindings/lib/remote_ptr.h"
8 #include "mojo/shell/service_manager.h"
9 #include "mojom/shell.h"
10 #include "mojom/test.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace mojo {
14 namespace shell {
15 namespace {
16
17 class TestApp : public ShellClientStub {
18 public:
19 TestApp(ScopedMessagePipeHandle shell_handle)
20 : shell_(shell_handle.Pass()) {
21 shell_.SetPeer(this);
22 }
23 virtual ~TestApp() {
24 }
25 virtual void AcceptConnection(ScopedMessagePipeHandle client_handle)
26 MOJO_OVERRIDE {
27 service_.reset(new TestServiceImpl(this, client_handle.Pass()));
28 }
29 std::string GetLastTestString() {
30 return service_->last_test_string_;
31 }
32
33 private:
34 class TestServiceImpl : public TestServiceStub {
35 public:
36 TestServiceImpl(TestApp* service, ScopedMessagePipeHandle client_handle)
37 : service_(service),
38 client_(client_handle.Pass()) {
39 client_.SetPeer(this);
40 }
41 virtual ~TestServiceImpl() {
42 }
43 virtual void Test(const mojo::String& test_string) OVERRIDE {
44 last_test_string_ = test_string.To<std::string>();
45 client_->AckTest();
46 }
47 TestApp* service_;
48 RemotePtr<TestClient> client_;
49 std::string last_test_string_;
50 };
51 RemotePtr<Shell> shell_;
52 scoped_ptr<TestServiceImpl> service_;
53 };
54
55 class TestClientImpl : public TestClientStub {
56 public:
57 explicit TestClientImpl(ScopedMessagePipeHandle service_handle)
58 : service_(service_handle.Pass()),
59 quit_after_ack_(false) {
60 service_.SetPeer(this);
61 }
62 virtual ~TestClientImpl() {
63 }
64 virtual void AckTest() OVERRIDE {
65 if (quit_after_ack_)
66 base::MessageLoop::current()->QuitNow();
67 }
68 void Test(std::string test_string) {
69 AllocationScope scope;
70 quit_after_ack_ = true;
71 service_->Test(mojo::String(test_string));
72 }
73 RemotePtr<TestService> service_;
74 bool quit_after_ack_;
75 };
76
77 class ServiceManagerTest : public testing::Test,
78 public ServiceManager::Loader {
79 public:
80 ServiceManagerTest() {
81 }
82
83 virtual ~ServiceManagerTest() {
84 }
85
86 virtual void SetUp() OVERRIDE {
87 mojo::BindingsSupport::Set(&support_);
88 GURL test_url("test:testService");
89 service_manager_.reset(new ServiceManager);
90 service_manager_->SetLoaderForURL(this, test_url);
91 MessagePipe pipe;
92 test_client_.reset(new TestClientImpl(pipe.handle0.Pass()));
93 service_manager_->Connect(test_url, pipe.handle1.Pass());
94 }
95
96 virtual void TearDown() OVERRIDE {
97 test_client_.reset(NULL);
98 test_app_.reset(NULL);
99 service_manager_.reset(NULL);
100 mojo::BindingsSupport::Set(NULL);
101 }
102
103 virtual void Load(const GURL& url,
104 ScopedMessagePipeHandle shell_handle) OVERRIDE {
105 test_app_.reset(new TestApp(shell_handle.Pass()));
106 }
107
108 protected:
109 common::BindingsSupportImpl support_;
110 base::MessageLoop loop_;
111 scoped_ptr<TestApp> test_app_;
112 scoped_ptr<TestClientImpl> test_client_;
113 scoped_ptr<ServiceManager> service_manager_;
114 DISALLOW_COPY_AND_ASSIGN(ServiceManagerTest);
115 };
116
117 TEST_F(ServiceManagerTest, Basic) {
118 test_client_->Test("test");
119 loop_.Run();
120 EXPECT_EQ(std::string("test"), test_app_->GetLastTestString());
121 }
122
123 } // namespace
124 } // namespace shell
125 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/service_manager.cc ('k') | mojo/shell/test.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698