1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "renderer/renderer_server.h"
31
32 #include <memory>
33 #include <string>
34
35 #include "base/logging.h"
36 #include "base/port.h"
37 #include "base/system_util.h"
38 #include "base/util.h"
39 #include "ipc/ipc_test_util.h"
40 #include "protocol/renderer_command.pb.h"
41 #include "renderer/renderer_client.h"
42 #include "renderer/renderer_interface.h"
43 #include "testing/base/public/googletest.h"
44 #include "testing/base/public/gunit.h"
45
46 namespace mozc {
47 namespace renderer {
48 namespace {
49
50 class TestRenderer : public RendererInterface {
51 public:
TestRenderer()52 TestRenderer() : counter_(0), finished_(false) {}
53
Activate()54 bool Activate() { return true; }
55
IsAvailable() const56 bool IsAvailable() const { return true; }
57
ExecCommand(const commands::RendererCommand & command)58 bool ExecCommand(const commands::RendererCommand &command) {
59 if (finished_) {
60 return false;
61 }
62 counter_++;
63 return true;
64 }
65
Reset()66 void Reset() {
67 counter_ = 0;
68 }
69
counter() const70 int counter() const {
71 return counter_;
72 }
73
Shutdown()74 void Shutdown() {
75 finished_ = true;
76 }
77
78 private:
79 int counter_;
80 bool finished_;
81 };
82
83 class TestRendererServer : public RendererServer {
84 public:
TestRendererServer()85 TestRendererServer() {}
86
~TestRendererServer()87 virtual ~TestRendererServer() {}
88
StartMessageLoop()89 int StartMessageLoop() {
90 return 0;
91 }
92
93 // Not async for testing
AsyncExecCommand(string * proto_message)94 bool AsyncExecCommand(string *proto_message) {
95 commands::RendererCommand command;
96 command.ParseFromString(*proto_message);
97 delete proto_message;
98 return ExecCommandInternal(command);
99 }
100 };
101
102 // A renderer launcher which does nothing.
103 class DummyRendererLauncher : public RendererLauncherInterface {
104 public:
StartRenderer(const string & name,const string & renderer_path,bool disable_renderer_path_check,IPCClientFactoryInterface * ipc_client_factory_interface)105 void StartRenderer(const string &name,
106 const string &renderer_path,
107 bool disable_renderer_path_check,
108 IPCClientFactoryInterface *ipc_client_factory_interface) {
109 LOG(INFO) << name << " " << renderer_path;
110 }
111
ForceTerminateRenderer(const string & name)112 bool ForceTerminateRenderer(const string &name) {
113 return true;
114 }
115
OnFatal(RendererErrorType type)116 void OnFatal(RendererErrorType type) {
117 LOG(ERROR) << static_cast<int>(type);
118 }
119
IsAvailable() const120 virtual bool IsAvailable() const {
121 return true;
122 }
123
CanConnect() const124 virtual bool CanConnect() const {
125 return true;
126 }
127
SetPendingCommand(const commands::RendererCommand & command)128 virtual void SetPendingCommand(const commands::RendererCommand &command) {
129 }
130
set_suppress_error_dialog(bool suppress)131 virtual void set_suppress_error_dialog(bool suppress) {
132 }
133 };
134 } // namespace
135
136 class RendererServerTest : public ::testing::Test {
137 protected:
SetUp()138 virtual void SetUp() {
139 SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
140 }
141 };
142
TEST_F(RendererServerTest,IPCTest)143 TEST_F(RendererServerTest, IPCTest) {
144 SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
145 mozc::IPCClientFactoryOnMemory on_memory_client_factory;
146
147 std::unique_ptr<TestRendererServer> server(new TestRendererServer);
148 TestRenderer renderer;
149 server->SetRendererInterface(&renderer);
150 #ifdef OS_MACOSX
151 server->SetMachPortManager(on_memory_client_factory.OnMemoryPortManager());
152 #endif
153 renderer.Reset();
154
155 // listning event
156 server->StartServer();
157 Util::Sleep(1000);
158
159 DummyRendererLauncher launcher;
160 RendererClient client;
161 client.SetIPCClientFactory(&on_memory_client_factory);
162 client.DisableRendererServerCheck();
163 client.SetRendererLauncherInterface(&launcher);
164 commands::RendererCommand command;
165 command.set_type(commands::RendererCommand::NOOP);
166
167 // renderer is called via IPC
168 client.ExecCommand(command);
169 EXPECT_EQ(1, renderer.counter());
170
171 client.ExecCommand(command);
172 client.ExecCommand(command);
173 client.ExecCommand(command);
174 EXPECT_EQ(4, renderer.counter());
175
176 // Gracefully shutdown the server.
177 renderer.Shutdown();
178 client.ExecCommand(command);
179 server->Wait();
180 }
181
182 } // namespace renderer
183 } // namespace mozc
184