OLD | NEW |
---|---|
(Empty) | |
1 #include "base/message_loop/message_loop.h" | |
viettrungluu
2014/01/03 17:44:49
nit: Missing lame copyright boilerplate.
DaveMoore
2014/01/03 18:05:49
Done.
| |
2 #include "mojo/common/bindings_support_impl.h" | |
3 #include "mojo/public/bindings/lib/remote_ptr.h" | |
4 #include "mojo/shell/service_manager.h" | |
5 #include "mojom/shell.h" | |
6 #include "mojom/test.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 | |
9 namespace mojo { | |
10 namespace shell { | |
11 namespace { | |
12 | |
13 class TestApp : public ShellClientStub { | |
14 public: | |
15 TestApp(ScopedMessagePipeHandle shell_handle) | |
16 : shell_(shell_handle.Pass()) { | |
17 shell_.SetPeer(this); | |
18 } | |
19 virtual ~TestApp() { | |
20 } | |
21 virtual void AcceptConnection(ScopedMessagePipeHandle client_handle) | |
22 MOJO_OVERRIDE { | |
viettrungluu
2014/01/03 17:44:49
Typically, I think our pattern is to use the regul
DaveMoore
2014/01/03 18:05:49
Done.
| |
23 service_.reset(new TestServiceImpl(this, client_handle.Pass())); | |
24 } | |
25 std::string GetLastTestString() { | |
26 return service_->last_test_string_; | |
27 } | |
28 | |
29 private: | |
30 class TestServiceImpl : public TestServiceStub { | |
31 public: | |
32 TestServiceImpl(TestApp* service, ScopedMessagePipeHandle client_handle) | |
33 : service_(service), | |
34 client_(client_handle.Pass()) { | |
35 client_.SetPeer(this); | |
36 } | |
37 virtual ~TestServiceImpl() { | |
38 } | |
39 virtual void Test(const mojo::String& test_string) MOJO_OVERRIDE { | |
viettrungluu
2014/01/03 17:44:49
...
DaveMoore
2014/01/03 18:05:49
Done.
| |
40 last_test_string_ = test_string.To<std::string>(); | |
41 client_->AckTest(); | |
42 } | |
43 TestApp* service_; | |
44 RemotePtr<TestClient> client_; | |
45 std::string last_test_string_; | |
46 }; | |
47 RemotePtr<Shell> shell_; | |
48 scoped_ptr<TestServiceImpl> service_; | |
49 }; | |
50 | |
51 class TestClientImpl : public TestClientStub { | |
52 public: | |
53 explicit TestClientImpl(ScopedMessagePipeHandle service_handle) | |
54 : service_(service_handle.Pass()), | |
55 quit_after_ack_(false) { | |
56 service_.SetPeer(this); | |
57 } | |
58 virtual ~TestClientImpl() { | |
59 } | |
60 virtual void AckTest() MOJO_OVERRIDE { | |
viettrungluu
2014/01/03 17:44:49
...
DaveMoore
2014/01/03 18:05:49
Done.
| |
61 if (quit_after_ack_) | |
62 base::MessageLoop::current()->QuitNow(); | |
63 } | |
64 void Test(std::string test_string) { | |
65 AllocationScope scope; | |
66 quit_after_ack_ = true; | |
67 service_->Test(mojo::String(test_string)); | |
68 } | |
69 RemotePtr<TestService> service_; | |
70 bool quit_after_ack_; | |
71 }; | |
72 | |
73 class ServiceManagerTest : public testing::Test, | |
74 public ServiceManager::Loader { | |
75 public: | |
76 ServiceManagerTest() | |
77 : test_app_(NULL), | |
78 test_client_(NULL), | |
79 service_manager_(NULL) { | |
80 } | |
81 | |
82 virtual ~ServiceManagerTest() { | |
83 } | |
84 | |
85 virtual void SetUp() OVERRIDE { | |
86 mojo::BindingsSupport::Set(&support_); | |
87 GURL test_url("test:testService"); | |
88 service_manager_ = new ServiceManager(); | |
89 service_manager_->SetLoaderForURL(this, test_url); | |
90 MessagePipe pipe; | |
91 test_client_ = new TestClientImpl(pipe.handle0.Pass()); | |
92 service_manager_->Connect(test_url, pipe.handle1.Pass()); | |
93 } | |
94 | |
95 virtual void TearDown() OVERRIDE { | |
96 delete test_client_; | |
97 delete test_app_; | |
98 delete service_manager_; | |
99 mojo::BindingsSupport::Set(NULL); | |
100 } | |
101 | |
102 virtual void Load(const GURL& url, | |
103 ScopedMessagePipeHandle shell_handle) MOJO_OVERRIDE { | |
viettrungluu
2014/01/03 17:44:49
...
DaveMoore
2014/01/03 18:05:49
Done.
| |
104 test_app_ = new TestApp(shell_handle.Pass()); | |
105 } | |
106 | |
107 protected: | |
108 common::BindingsSupportImpl support_; | |
109 base::MessageLoop loop_; | |
110 TestApp* test_app_; | |
viettrungluu
2014/01/03 17:44:49
I think you should use scoped_ptrs here (and .rese
DaveMoore
2014/01/03 18:05:49
I went back and forth on this, but didn't see any
viettrungluu
2014/01/03 18:23:14
a) It's more idiomatic (in Chromium), and makes ow
| |
111 TestClientImpl* test_client_; | |
112 ServiceManager* service_manager_; | |
113 DISALLOW_COPY_AND_ASSIGN(ServiceManagerTest); | |
114 }; | |
115 | |
116 TEST_F(ServiceManagerTest, Basic) { | |
117 test_client_->Test("test"); | |
118 loop_.Run(); | |
119 EXPECT_EQ(std::string("test"), test_app_->GetLastTestString()); | |
120 } | |
121 | |
122 } // namespace | |
123 } // namespace shell | |
124 } // namespace mojo | |
OLD | NEW |