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

Side by Side Diff: plugins/update/npapi/npfunction_host_unittest.cc

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 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
OLDNEW
(Empty)
1 // Copyright 2010 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // ========================================================================
15
16 #include "omaha/plugins/update/npapi/npfunction_host.h"
17 #include <atlbase.h>
18 #include <atlcom.h>
19 #include <string.h>
20 #include <vector>
21 #include "omaha/plugins/update/npapi/testing/dispatch_host_test_interface.h"
22 #include "omaha/plugins/update/npapi/testing/stubs.h"
23 #include "omaha/testing/unit_test.h"
24
25 namespace omaha {
26
27 class NpFunctionHostTest;
28
29 class MockFunctionNPObject : public NPObject {
30 public:
31 static MockFunctionNPObject* CreateInstance(NpFunctionHostTest* creator) {
32 NPObject* obj = NPN_CreateObject(NULL, &MockFunctionNPObject::kNPClass_);
33 MockFunctionNPObject* realobj = static_cast<MockFunctionNPObject*>(obj);
34 realobj->creator_ = creator;
35 return realobj;
36 }
37
38 static NPObject* Allocate(NPP npp, NPClass* class_functions) {
39 UNREFERENCED_PARAMETER(class_functions);
40 return new MockFunctionNPObject(npp);
41 }
42
43 static void Deallocate(NPObject* object) {
44 delete static_cast<MockFunctionNPObject*>(object);
45 }
46
47 static bool InvokeDefault(NPObject* object,
48 const NPVariant* args,
49 uint32_t arg_count,
50 NPVariant* result) {
51 MockFunctionNPObject* realobj = static_cast<MockFunctionNPObject*>(object);
52 return realobj->InvokeDefaultLocal(args, arg_count, result);
53 }
54
55 bool InvokeDefaultLocal(const NPVariant* args,
56 uint32_t arg_count,
57 NPVariant* result);
58
59 protected:
60 explicit MockFunctionNPObject(NPP npp) : npp_(npp), creator_(NULL) {}
61
62 static NPUTF8* NPN_ReallocateStringZ(const char* string) {
63 uint32 buflen = strlen(string) + 1;
64 NPUTF8* npnstr = reinterpret_cast<NPUTF8*>(NPN_MemAlloc(buflen));
65 memmove(npnstr, string, buflen);
66 return npnstr;
67 }
68
69 private:
70 NPP npp_;
71 NpFunctionHostTest* creator_;
72
73 // The NPObject vtable.
74 static NPClass kNPClass_;
75 };
76
77 class NpFunctionHostTest : public testing::Test {
78 protected:
79 friend class MockFunctionNPObject;
80
81 virtual void SetUp() {
82 function_ = MockFunctionNPObject::CreateInstance(this);
83 EXPECT_SUCCEEDED(NpFunctionHost::Create(NULL, function_, &host_));
84 }
85
86 virtual void TearDown() {
87 }
88
89 NPObject* function_;
90 CComPtr<IDispatch> host_;
91
92 std::vector<NPVariant> mock_args_;
93 NPVariant mock_result_;
94 };
95
96 NPClass MockFunctionNPObject::kNPClass_ = {
97 NP_CLASS_STRUCT_VERSION,
98 Allocate,
99 Deallocate,
100 NULL,
101 NULL,
102 NULL,
103 InvokeDefault,
104 NULL,
105 NULL,
106 NULL,
107 NULL,
108 NULL,
109 NULL,
110 };
111
112 bool MockFunctionNPObject::InvokeDefaultLocal(const NPVariant* args,
113 uint32_t arg_count,
114 NPVariant* result) {
115 const char* kMultiStringReturn = "multi";
116
117 // The mock NPObject exhibits the following external behavior:
118 // * If no arguments, return nothing
119 // * If one argument, return a boolean (true)
120 // * If two arguments, return a string ("multi")
121 // * Otherwise, treat it as an invoke failure.
122 // It also copies the arguments as supplied, and the intended NPVariant
123 // return value, to the test closure that created it.
124
125 creator_->mock_args_.resize(arg_count);
126 for (uint32_t i = 0; i < arg_count; ++i) {
127 creator_->mock_args_[i] = args[i];
128 }
129
130 switch (arg_count) {
131 case 0:
132 VOID_TO_NPVARIANT(*result);
133 break;
134 case 1:
135 BOOLEAN_TO_NPVARIANT(true, *result);
136 break;
137 case 2:
138 {
139 NPUTF8* utf8string = NPN_ReallocateStringZ(kMultiStringReturn);
140 STRINGZ_TO_NPVARIANT(utf8string, *result);
141 }
142 break;
143 default:
144 return false;
145 }
146
147 creator_->mock_result_ = *result;
148 return true;
149 }
150
151 TEST_F(NpFunctionHostTest, GetTypeInfoCount) {
152 UINT typeinfos_available = 1;
153 EXPECT_SUCCEEDED(host_->GetTypeInfoCount(&typeinfos_available));
154 EXPECT_EQ(0, typeinfos_available);
155 }
156
157 TEST_F(NpFunctionHostTest, GetTypeInfo_NotImplemented) {
158 ITypeInfo* typeinfo = NULL;
159
160 EXPECT_EQ(E_NOTIMPL, host_->GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT, &typeinfo));
161 }
162
163 TEST_F(NpFunctionHostTest, GetIDsOfNames_NotImplemented) {
164 LPOLESTR member_name = L"NonexistentMember";
165 DISPID member_dispid = 0;
166 EXPECT_EQ(E_NOTIMPL, host_->GetIDsOfNames(IID_NULL, &member_name, 1,
167 LOCALE_SYSTEM_DEFAULT,
168 &member_dispid));
169 }
170
171 TEST_F(NpFunctionHostTest, Invoke_NonMethod_NotSupported) {
172 EXPECT_EQ(DISP_E_MEMBERNOTFOUND, host_->Invoke(0, IID_NULL,
173 LOCALE_SYSTEM_DEFAULT,
174 DISPATCH_PROPERTYGET,
175 NULL,
176 NULL,
177 NULL,
178 NULL));
179 }
180
181 TEST_F(NpFunctionHostTest, Invoke_NamedArgs_NotSupported) {
182 DISPID param_name = 12;
183 DISPPARAMS params = {};
184 params.cNamedArgs = 1;
185 params.rgdispidNamedArgs = &param_name;
186 EXPECT_EQ(DISP_E_NONAMEDARGS, host_->Invoke(0, IID_NULL,
187 LOCALE_SYSTEM_DEFAULT,
188 DISPATCH_METHOD,
189 &params,
190 NULL,
191 NULL,
192 NULL));
193 }
194
195 TEST_F(NpFunctionHostTest, Invoke_NoArgs_NullDispParams) {
196 VARIANT retval = {};
197 EXPECT_SUCCEEDED(host_->Invoke(0, IID_NULL,
198 LOCALE_SYSTEM_DEFAULT,
199 DISPATCH_METHOD,
200 NULL,
201 &retval,
202 NULL,
203 NULL));
204
205 EXPECT_EQ(0, mock_args_.size());
206
207 EXPECT_TRUE(NPVARIANT_IS_VOID(mock_result_));
208 EXPECT_EQ(VT_EMPTY, retval.vt);
209 VariantClear(&retval);
210 }
211
212 TEST_F(NpFunctionHostTest, Invoke_NoArgs_ValidDispParams) {
213 VARIANT retval = {};
214 DISPPARAMS params = {};
215 EXPECT_SUCCEEDED(host_->Invoke(0, IID_NULL,
216 LOCALE_SYSTEM_DEFAULT,
217 DISPATCH_METHOD,
218 &params,
219 &retval,
220 NULL,
221 NULL));
222
223 EXPECT_EQ(0, mock_args_.size());
224
225 EXPECT_TRUE(NPVARIANT_IS_VOID(mock_result_));
226 EXPECT_EQ(VT_EMPTY, retval.vt);
227 VariantClear(&retval);
228 }
229
230 TEST_F(NpFunctionHostTest, Invoke_NoArgs_OneParam) {
231 const int kTestIntVal = 0xDEADBEEF;
232
233 VARIANT retval = {};
234 VARIANT firstparam = {};
235 firstparam.vt = VT_I4;
236 firstparam.intVal = kTestIntVal;
237
238 DISPPARAMS dispparams = {};
239 dispparams.cArgs = 1;
240 dispparams.rgvarg = &firstparam;
241
242 EXPECT_SUCCEEDED(host_->Invoke(0, IID_NULL,
243 LOCALE_SYSTEM_DEFAULT,
244 DISPATCH_METHOD,
245 &dispparams,
246 &retval,
247 NULL,
248 NULL));
249
250 EXPECT_EQ(1, mock_args_.size());
251 EXPECT_TRUE(NPVARIANT_IS_INT32(mock_args_[0]));
252 EXPECT_EQ(kTestIntVal, NPVARIANT_TO_INT32(mock_args_[0]));
253
254 EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(mock_result_));
255 EXPECT_EQ(true, NPVARIANT_TO_BOOLEAN(mock_result_));
256 EXPECT_EQ(VT_BOOL, retval.vt);
257 EXPECT_EQ(VARIANT_TRUE, retval.boolVal);
258 VariantClear(&retval);
259 }
260
261 TEST_F(NpFunctionHostTest, Invoke_NoArgs_TwoParams) {
262 const double kTestFloatVal = 3.1415927;
263
264 VARIANT retval = {};
265 VARIANT params[2] = {};
266 params[0].vt = VT_BOOL; // Invoke expects args in reverse order
267 params[0].intVal = VARIANT_TRUE;
268 params[1].vt = VT_R8;
269 params[1].dblVal = kTestFloatVal;
270
271 DISPPARAMS dispparams = {};
272 dispparams.cArgs = 2;
273 dispparams.rgvarg = params;
274 EXPECT_SUCCEEDED(host_->Invoke(0, IID_NULL,
275 LOCALE_SYSTEM_DEFAULT,
276 DISPATCH_METHOD,
277 &dispparams,
278 &retval,
279 NULL,
280 NULL));
281
282 EXPECT_EQ(2, mock_args_.size());
283 EXPECT_TRUE(NPVARIANT_IS_DOUBLE(mock_args_[0]));
284 EXPECT_EQ(kTestFloatVal, NPVARIANT_TO_DOUBLE(mock_args_[0]));
285 EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(mock_args_[1]));
286 EXPECT_EQ(true, NPVARIANT_TO_BOOLEAN(mock_args_[1]));
287
288 EXPECT_TRUE(NPVARIANT_IS_STRING(mock_result_));
289 // Don't check mock_result's contents; it will have been released by Invoke()
290 EXPECT_EQ(VT_BSTR, retval.vt);
291 EXPECT_STREQ(CString("multi"), CString(retval.bstrVal));
292 VariantClear(&retval);
293 }
294
295 TEST_F(NpFunctionHostTest, Invoke_NoArgs_ThreeParams) {
296 VARIANT retval = {};
297 VARIANT params[3] = {};
298 for (int i = 0; i < 3; ++i) {
299 params[i].vt = VT_BOOL;
300 params[i].intVal = VARIANT_TRUE;
301 }
302
303 DISPPARAMS dispparams = {};
304 dispparams.cArgs = 3;
305 dispparams.rgvarg = params;
306 EXPECT_EQ(E_FAIL, host_->Invoke(0, IID_NULL,
307 LOCALE_SYSTEM_DEFAULT,
308 DISPATCH_METHOD,
309 &dispparams,
310 &retval,
311 NULL,
312 NULL));
313 EXPECT_EQ(3, mock_args_.size());
314 }
315
316
317 } // namespace omaha
OLDNEW
« no previous file with comments | « plugins/update/npapi/npfunction_host.cc ('k') | plugins/update/npapi/testing/dispatch_host_test.rc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698