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 "client/client_mock.h"
31
32 #include "base/logging.h"
33
34 namespace mozc {
35 namespace client {
36
37 // Most of methods in ClientMock looks almost same. Here defines the
38 // boilerplates for those methods.
39 #define MockConstBoolImplementation(method_name, argument) \
40 bool ClientMock::method_name(argument) const { \
41 function_counter_[#method_name]++; \
42 std::map<string, bool>::const_iterator it = \
43 return_bool_values_.find(#method_name); \
44 if (it != return_bool_values_.end()) { \
45 return it->second; \
46 } \
47 return false; \
48 }
49 #define MockBoolImplementation(method_name, argument) \
50 bool ClientMock::method_name(argument) { \
51 function_counter_[#method_name]++; \
52 std::map<string, bool>::const_iterator it = \
53 return_bool_values_.find(#method_name); \
54 if (it != return_bool_values_.end()) { \
55 return it->second; \
56 } \
57 return false; \
58 }
59 #define MockVoidImplementation(method_name, argument) \
60 void ClientMock::method_name(argument) { \
61 function_counter_[#method_name]++; \
62 return; \
63 }
64
65 MockVoidImplementation(SetIPCClientFactory,
66 IPCClientFactoryInterface *client_factory);
67 MockVoidImplementation(SetServerLauncher,
68 ServerLauncherInterface *server_launcher);
69 MockConstBoolImplementation(IsValidRunLevel, void);
70 MockBoolImplementation(EnsureConnection, void);
71 MockBoolImplementation(EnsureSession, void);
72 MockBoolImplementation(CheckVersionOrRestartServer, void);
73 MockBoolImplementation(ClearUserHistory, void);
74 MockBoolImplementation(ClearUserPrediction, void);
75 MockBoolImplementation(ClearUnusedUserPrediction, void);
76 MockBoolImplementation(Shutdown, void);
77 MockBoolImplementation(SyncData, void);
78 MockBoolImplementation(Reload, void);
79 MockBoolImplementation(Cleanup, void);
80 MockVoidImplementation(Reset, void);
81 MockConstBoolImplementation(PingServer, void);
82 MockBoolImplementation(NoOperation, void);
83 MockVoidImplementation(EnableCascadingWindow, bool enable);
84 MockVoidImplementation(set_timeout, int timeout);
85 MockVoidImplementation(set_restricted, bool restricted);
86 MockVoidImplementation(set_server_program, const string &program_path);
87 MockVoidImplementation(set_suppress_error_dialog, bool suppress_error_dialog);
88 MockVoidImplementation(set_client_capability,
89 const commands::Capability &capability);
90 MockBoolImplementation(LaunchToolWithProtoBuf, const commands::Output &output);
91 MockBoolImplementation(OpenBrowser, const string &url);
92
93 #undef MockConstImplementation
94 #undef MockBoolImplementation
95 #undef MockVoidImplementation
96
97 // Another boilerplate for the method with an "output" as its second
98 // argument.
99 #define MockImplementationWithContextAndOutput(method_name, argtype) \
100 bool ClientMock::method_name(argtype argument, \
101 const commands::Context &context, \
102 commands::Output *output) { \
103 function_counter_[#method_name]++; \
104 called_##method_name##_.CopyFrom(argument); \
105 std::map<string, commands::Output>::const_iterator it = \
106 outputs_.find(#method_name); \
107 if (it != outputs_.end()) { \
108 output->CopyFrom(it->second); \
109 } \
110 std::map<string, bool>::const_iterator retval = \
111 return_bool_values_.find(#method_name); \
112 if (retval != return_bool_values_.end()) { \
113 return retval->second; \
114 } \
115 return false; \
116 }
117
118 MockImplementationWithContextAndOutput(SendKeyWithContext,
119 const commands::KeyEvent &);
120 MockImplementationWithContextAndOutput(TestSendKeyWithContext,
121 const commands::KeyEvent &);
122 MockImplementationWithContextAndOutput(SendCommandWithContext,
123 const commands::SessionCommand &);
124
125 #undef MockImplementationWithContextAndOutput
126
127
128 // Exceptional methods.
129 // GetConfig needs to obtain the "called_config_".
GetConfig(config::Config * config)130 bool ClientMock::GetConfig(config::Config *config) {
131 function_counter_["GetConfig"]++;
132 config->CopyFrom(called_config_);
133 std::map<string, bool>::const_iterator it =
134 return_bool_values_.find("GetConfig");
135 if (it != return_bool_values_.end()) {
136 return it->second;
137 }
138 return false;
139 }
140
141 // SetConfig needs to set the "called_config_".
SetConfig(const config::Config & config)142 bool ClientMock::SetConfig(const config::Config &config) {
143 function_counter_["SetConfig"]++;
144 called_config_.CopyFrom(config);
145 std::map<string, bool>::const_iterator it =
146 return_bool_values_.find("SetConfig");
147 if (it != return_bool_values_.end()) {
148 return it->second;
149 }
150 return false;
151 }
152
153 // LaunchTool arguments are quite different from other methods.
LaunchTool(const string & mode,const string & extra_arg)154 bool ClientMock::LaunchTool(const string &mode, const string &extra_arg) {
155 function_counter_["LaunchTool"]++;
156 return return_bool_values_["LaunchTool"];
157 }
158
159 // Other methods to deal with internal data such like operations over
160 // function counters or setting the expected return values.
ClearFunctionCounter()161 void ClientMock::ClearFunctionCounter() {
162 for (std::map<string, int>::iterator it = function_counter_.begin();
163 it != function_counter_.end(); it++) {
164 it->second = 0;
165 }
166 }
167
SetBoolFunctionReturn(string func_name,bool value)168 void ClientMock::SetBoolFunctionReturn(string func_name, bool value) {
169 return_bool_values_[func_name] = value;
170 }
171
GetFunctionCallCount(string key)172 int ClientMock::GetFunctionCallCount(string key) {
173 return function_counter_[key];
174 }
175
176 } // namespace client
177 } // namespace mozc
178