OLD | NEW |
| (Empty) |
1 // Copyright 2009 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/activex/update3web_control.h" | |
17 #include <atlbase.h> | |
18 #include <atlcom.h> | |
19 #include <windows.h> | |
20 #include "base/basictypes.h" | |
21 #include "base/error.h" | |
22 #include "omaha/testing/unit_test.h" | |
23 | |
24 namespace omaha { | |
25 | |
26 namespace { | |
27 | |
28 #define STUBMETHOD(identifier, ...) \ | |
29 STDMETHOD(identifier)(__VA_ARGS__) {\ | |
30 return E_NOTIMPL;\ | |
31 } | |
32 | |
33 #define STUBGETPROP(identifier, type) \ | |
34 STUBMETHOD(get_ ## identifier, type*) | |
35 | |
36 #define STUBPUTPROP(identifier, type) \ | |
37 STUBMETHOD(put_ ## identifier, type) | |
38 | |
39 #define STUBPROP(identifier, type) \ | |
40 STUBGETPROP(identifier, type)\ | |
41 STUBPUTPROP(identifier, type) | |
42 | |
43 class ATL_NO_VTABLE MockWebBrowser2 | |
44 : public CComObjectRootEx<CComObjectThreadModel>, | |
45 public IServiceProviderImpl<MockWebBrowser2>, | |
46 public IWebBrowser2 { | |
47 public: | |
48 MockWebBrowser2() : url_("something-like-a-url") {} | |
49 virtual ~MockWebBrowser2() {} | |
50 | |
51 DECLARE_NOT_AGGREGATABLE(MockWebBrowser2); | |
52 | |
53 BEGIN_COM_MAP(MockWebBrowser2) | |
54 COM_INTERFACE_ENTRY(IServiceProvider) | |
55 COM_INTERFACE_ENTRY(IWebBrowser2) | |
56 END_COM_MAP() | |
57 | |
58 BEGIN_SERVICE_MAP(MockWebBrowser2) | |
59 SERVICE_ENTRY(SID_SWebBrowserApp) | |
60 END_SERVICE_MAP() | |
61 | |
62 // IDispatch | |
63 STUBMETHOD(GetIDsOfNames, REFIID, OLECHAR**, unsigned int, LCID, DISPID*); | |
64 STUBMETHOD(GetTypeInfo, unsigned int, LCID, ITypeInfo**); | |
65 STUBMETHOD(GetTypeInfoCount, UINT*); | |
66 STUBMETHOD(Invoke, DISPID, REFIID, LCID, WORD, DISPPARAMS*, VARIANT*, | |
67 EXCEPINFO*, unsigned int*); | |
68 | |
69 // IWebBrowser2 | |
70 STUBPROP(AddressBar, VARIANT_BOOL); | |
71 STUBGETPROP(Application, IDispatch*); | |
72 STUBGETPROP(Busy, VARIANT_BOOL); | |
73 STUBMETHOD(ClientToWindow, int*, int*); | |
74 STUBGETPROP(Container, IDispatch*); | |
75 STUBGETPROP(Document, IDispatch*); | |
76 STUBMETHOD(ExecWB, OLECMDID, OLECMDEXECOPT, VARIANT*, VARIANT*); | |
77 STUBGETPROP(FullName, BSTR); | |
78 STUBPROP(FullScreen, VARIANT_BOOL); | |
79 STUBMETHOD(GetProperty, BSTR, VARIANT*); | |
80 STUBMETHOD(GoBack, VOID); | |
81 STUBMETHOD(GoForward, VOID); | |
82 STUBMETHOD(GoHome, VOID); | |
83 STUBMETHOD(GoSearch, VOID); | |
84 STUBPROP(Height, LONG); | |
85 STUBGETPROP(HWND, LONG); | |
86 STUBPROP(Left, LONG); | |
87 STUBGETPROP(LocationName, BSTR); | |
88 STDMETHOD(get_LocationURL)(BSTR* url) { | |
89 *url = CComBSTR(url_).Detach(); | |
90 return S_OK; | |
91 } | |
92 STUBPROP(MenuBar, VARIANT_BOOL); | |
93 STUBGETPROP(Name, BSTR); | |
94 STUBMETHOD(Navigate, BSTR, VARIANT*, VARIANT*, VARIANT*, VARIANT*); | |
95 STUBMETHOD(Navigate2, VARIANT*, VARIANT*, VARIANT*, VARIANT*, VARIANT*); | |
96 STUBPROP(Offline, VARIANT_BOOL); | |
97 STUBGETPROP(Path, BSTR); | |
98 STUBGETPROP(Parent, IDispatch*); | |
99 STUBMETHOD(PutProperty, BSTR, VARIANT); | |
100 STUBMETHOD(QueryStatusWB, OLECMDID, OLECMDF*); | |
101 STUBMETHOD(Quit, VOID); | |
102 STUBGETPROP(ReadyState, READYSTATE); | |
103 STUBMETHOD(Refresh, VOID); | |
104 STUBMETHOD(Refresh2, VARIANT*); | |
105 STUBPROP(RegisterAsBrowser, VARIANT_BOOL); | |
106 STUBPROP(RegisterAsDropTarget, VARIANT_BOOL); | |
107 STUBPROP(Resizable, VARIANT_BOOL); | |
108 STUBMETHOD(ShowBrowserBar, VARIANT*, VARIANT*, VARIANT*); | |
109 STUBPROP(Silent, VARIANT_BOOL); | |
110 STUBPROP(StatusBar, VARIANT_BOOL); | |
111 STUBPROP(StatusText, BSTR); | |
112 STUBMETHOD(Stop, VOID); | |
113 STUBPROP(TheaterMode, VARIANT_BOOL); | |
114 STUBPROP(ToolBar, int); | |
115 STUBPROP(Top, LONG); | |
116 STUBGETPROP(TopLevelContainer, VARIANT_BOOL); | |
117 STUBPROP(Type, BSTR); | |
118 STUBPROP(Visible, VARIANT_BOOL); | |
119 STUBPROP(Width, LONG); | |
120 | |
121 void set_url(const char* url) { url_ = url; } | |
122 | |
123 private: | |
124 CString url_; | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(MockWebBrowser2); | |
127 }; | |
128 | |
129 class ATL_NO_VTABLE MockHTMLDocument2 | |
130 : public CComObjectRootEx<CComObjectThreadModel>, | |
131 public IServiceProvider, | |
132 public IOleClientSite, | |
133 public IOleContainer, | |
134 public IHTMLDocument2 { | |
135 public: | |
136 MockHTMLDocument2() {} | |
137 virtual ~MockHTMLDocument2() {} | |
138 | |
139 DECLARE_NOT_AGGREGATABLE(MockHTMLDocument2); | |
140 | |
141 BEGIN_COM_MAP(MockHTMLDocument2) | |
142 COM_INTERFACE_ENTRY(IServiceProvider) | |
143 COM_INTERFACE_ENTRY(IOleClientSite) | |
144 COM_INTERFACE_ENTRY(IOleContainer) | |
145 COM_INTERFACE_ENTRY(IHTMLDocument2) | |
146 END_COM_MAP() | |
147 | |
148 // IServiceProvider | |
149 STUBMETHOD(QueryService, REFGUID, REFIID, void**); | |
150 | |
151 // IOleContainer | |
152 STDMETHOD(GetContainer)(IOleContainer** container) { | |
153 return QueryInterface(IID_PPV_ARGS(container)); | |
154 } | |
155 STUBMETHOD(GetMoniker, DWORD, DWORD, IMoniker**); | |
156 STUBMETHOD(OnShowWindow, BOOL); | |
157 STUBMETHOD(RequestNewObjectLayout); | |
158 STUBMETHOD(SaveObject); | |
159 STUBMETHOD(ShowObject); | |
160 | |
161 // IOleContainer | |
162 STUBMETHOD(EnumObjects, DWORD, IEnumUnknown**); | |
163 STUBMETHOD(LockContainer, BOOL); | |
164 | |
165 // IDispatch | |
166 STUBMETHOD(GetIDsOfNames, REFIID, OLECHAR**, unsigned int, LCID, DISPID*); | |
167 STUBMETHOD(GetTypeInfo, unsigned int, LCID, ITypeInfo**); | |
168 STUBMETHOD(GetTypeInfoCount, UINT*); | |
169 STUBMETHOD(Invoke, DISPID, REFIID, LCID, WORD, DISPPARAMS*, VARIANT*, | |
170 EXCEPINFO*, unsigned int*); | |
171 | |
172 // IParseDisplayName | |
173 STUBMETHOD(ParseDisplayName, IBindCtx*, LPOLESTR, ULONG*, IMoniker**); | |
174 | |
175 // IHTMLDocument2 | |
176 STUBGETPROP(activeElement, IHTMLElement*); | |
177 STUBPROP(alinkColor, VARIANT); | |
178 STUBGETPROP(all, IHTMLElementCollection*); | |
179 STUBGETPROP(anchors, IHTMLElementCollection*); | |
180 STUBGETPROP(applets, IHTMLElementCollection*); | |
181 STUBPROP(bgColor, VARIANT); | |
182 STUBGETPROP(body, IHTMLElement*); | |
183 STUBPROP(charset, BSTR); | |
184 STUBMETHOD(clear, VOID); | |
185 STUBMETHOD(close, VOID); | |
186 STUBPROP(cookie, BSTR); | |
187 STUBMETHOD(createElement, BSTR, IHTMLElement**); | |
188 STUBMETHOD(createStyleSheet, BSTR, LONG, IHTMLStyleSheet**); | |
189 STUBPROP(defaultCharset, BSTR); | |
190 STUBPROP(designMode, BSTR); | |
191 STUBPROP(domain, BSTR); | |
192 STUBPROP(elementFromPoint, BSTR); | |
193 STUBMETHOD(elementFromPoint, LONG, LONG, IHTMLElement**); | |
194 STUBGETPROP(embeds, IHTMLElementCollection*); | |
195 STUBMETHOD(execCommand, BSTR, VARIANT_BOOL, VARIANT, VARIANT_BOOL*); | |
196 STUBMETHOD(execCommandShowHelp, BSTR, VARIANT_BOOL*); | |
197 STUBPROP(expando, VARIANT_BOOL); | |
198 STUBPROP(fgColor, VARIANT); | |
199 STUBGETPROP(fileCreatedDate, BSTR); | |
200 STUBGETPROP(fileModifiedDate, BSTR); | |
201 STUBGETPROP(fileSize, BSTR); | |
202 STUBGETPROP(fileUpdatedDate, BSTR); | |
203 STUBGETPROP(forms, IHTMLElementCollection*); | |
204 STUBGETPROP(frames, IHTMLFramesCollection2*); | |
205 STUBGETPROP(images, IHTMLElementCollection*); | |
206 STUBGETPROP(lastModified, BSTR); | |
207 STUBPROP(linkColor, VARIANT); | |
208 STUBGETPROP(links, IHTMLElementCollection*); | |
209 STUBGETPROP(location, IHTMLLocation*); | |
210 STUBGETPROP(mimeType, BSTR); | |
211 STUBGETPROP(nameProp, BSTR); | |
212 STUBPROP(onafterupdate, VARIANT); | |
213 STUBPROP(onbeforeupdate, VARIANT); | |
214 STUBPROP(onclick, VARIANT); | |
215 STUBPROP(ondblclick, VARIANT); | |
216 STUBPROP(ondragstart, VARIANT); | |
217 STUBPROP(onerrorupdate, VARIANT); | |
218 STUBPROP(onhelp, VARIANT); | |
219 STUBPROP(onkeydown, VARIANT); | |
220 STUBPROP(onkeypress, VARIANT); | |
221 STUBPROP(onkeyup, VARIANT); | |
222 STUBPROP(onmousedown, VARIANT); | |
223 STUBPROP(onmousemove, VARIANT); | |
224 STUBPROP(onmouseout, VARIANT); | |
225 STUBPROP(onmouseover, VARIANT); | |
226 STUBPROP(onmouseup, VARIANT); | |
227 STUBPROP(onreadystatechange, VARIANT); | |
228 STUBPROP(onrowenter, VARIANT); | |
229 STUBPROP(onrowexit, VARIANT); | |
230 STUBPROP(onselectstart, VARIANT); | |
231 STUBMETHOD(open, BSTR, VARIANT, VARIANT, VARIANT, IDispatch**); | |
232 STUBGETPROP(parentWindow, IHTMLWindow2*); | |
233 STUBGETPROP(plugins, IHTMLElementCollection*); | |
234 STUBPROP(protocol, BSTR); | |
235 STUBMETHOD(queryCommandEnabled, BSTR, VARIANT_BOOL*); | |
236 STUBMETHOD(queryCommandIndeterm, BSTR, VARIANT_BOOL*); | |
237 STUBMETHOD(queryCommandState, BSTR, VARIANT_BOOL*); | |
238 STUBMETHOD(queryCommandSupported, BSTR, VARIANT_BOOL*); | |
239 STUBMETHOD(queryCommandText, BSTR, BSTR*); | |
240 STUBMETHOD(queryCommandValue, BSTR, VARIANT*); | |
241 STUBGETPROP(readyState, BSTR); | |
242 STUBGETPROP(referrer, BSTR); | |
243 STUBGETPROP(Script, IDispatch*); | |
244 STUBGETPROP(security, BSTR); | |
245 STUBGETPROP(selection, IHTMLSelectionObject*); | |
246 STUBGETPROP(scripts, IHTMLElementCollection*); | |
247 STUBGETPROP(styleSheets, IHTMLStyleSheetsCollection*); | |
248 STUBPROP(title, BSTR); | |
249 STUBMETHOD(toString, BSTR*); | |
250 STDMETHOD(get_URL)(BSTR* url) { | |
251 *url = CComBSTR("something-else-like-a-url").Detach(); | |
252 return S_OK; | |
253 } | |
254 STUBPUTPROP(URL, BSTR); | |
255 STUBPROP(vlinkColor, VARIANT); | |
256 STUBMETHOD(write, SAFEARRAY*); | |
257 STUBMETHOD(writeln, SAFEARRAY*); | |
258 | |
259 private: | |
260 DISALLOW_COPY_AND_ASSIGN(MockHTMLDocument2); | |
261 }; | |
262 | |
263 #undef STUBMETHOD | |
264 #undef STUBGETPROP | |
265 #undef STUBPUTPROP | |
266 #undef STUBPROP | |
267 | |
268 template <typename T> | |
269 HRESULT CComObjectCreatorHelper(CComObject<T>** ptr) { | |
270 if (!ptr) { | |
271 return E_POINTER; | |
272 } | |
273 CComObject<T>* raw_ptr = NULL; | |
274 RET_IF_FAILED(CComObject<T>::CreateInstance(&raw_ptr)); | |
275 raw_ptr->AddRef(); | |
276 *ptr = raw_ptr; | |
277 return S_OK; | |
278 } | |
279 | |
280 | |
281 } // namespace | |
282 | |
283 class Update3WebControlTest : public testing::Test { | |
284 protected: | |
285 virtual void SetUp() { | |
286 ASSERT_SUCCEEDED(CComObjectCreatorHelper(&control_)); | |
287 } | |
288 | |
289 virtual void TearDown() { | |
290 } | |
291 | |
292 HRESULT GetCurrentBrowserUrl(CString* url) { | |
293 return control_->site_lock_.GetCurrentBrowserUrl(control_, url); | |
294 } | |
295 | |
296 CComPtr<CComObject<Update3WebControl> > control_; | |
297 }; | |
298 | |
299 TEST_F(Update3WebControlTest, SiteLock) { | |
300 CComPtr<CComObject<MockWebBrowser2> > browser; | |
301 ASSERT_SUCCEEDED(CComObjectCreatorHelper(&browser)); | |
302 CComPtr<IUnknown> unknown; | |
303 ASSERT_SUCCEEDED(browser.QueryInterface(&unknown)); | |
304 ASSERT_SUCCEEDED(control_->SetSite(unknown)); | |
305 browser->set_url("http://www.google.com/pack/page.html"); | |
306 EXPECT_EQ(E_POINTER, | |
307 control_->getInstalledVersion(CComBSTR(), VARIANT_FALSE, NULL)); | |
308 } | |
309 | |
310 TEST_F(Update3WebControlTest, SiteLock_Negative) { | |
311 CComPtr<IWebBrowser2> browser; | |
312 ASSERT_SUCCEEDED(CComCoClass<MockWebBrowser2>::CreateInstance(&browser)); | |
313 ASSERT_SUCCEEDED(control_->SetSite(browser)); | |
314 EXPECT_EQ(GOOPDATE_E_ONECLICK_HOSTCHECK_FAILED, | |
315 control_->getInstalledVersion(CComBSTR(), VARIANT_FALSE, NULL)); | |
316 } | |
317 | |
318 TEST_F(Update3WebControlTest, GetCurrentBrowserUrl_FailIfNoSite) { | |
319 CString url; | |
320 EXPECT_FALSE(SUCCEEDED(GetCurrentBrowserUrl(&url))); | |
321 } | |
322 | |
323 TEST_F(Update3WebControlTest, GetCurrentBrowserUrl_WithIWebBrowser2) { | |
324 CComPtr<IUnknown> browser; | |
325 ASSERT_SUCCEEDED(CComCoClass<MockWebBrowser2>::CreateInstance(&browser)); | |
326 ASSERT_SUCCEEDED(control_->SetSite(browser)); | |
327 CString url; | |
328 EXPECT_EQ(S_OK, GetCurrentBrowserUrl(&url)); | |
329 EXPECT_STREQ(L"something-like-a-url", url); | |
330 } | |
331 | |
332 TEST_F(Update3WebControlTest, GetCurrentBrowserUrl_WithIHTMLDocument2) { | |
333 CComPtr<IUnknown> document; | |
334 ASSERT_SUCCEEDED(CComCoClass<MockHTMLDocument2>::CreateInstance(&document)); | |
335 ASSERT_SUCCEEDED(control_->SetSite(document)); | |
336 CString url; | |
337 EXPECT_EQ(S_OK, GetCurrentBrowserUrl(&url)); | |
338 EXPECT_STREQ(L"something-else-like-a-url", url); | |
339 } | |
340 | |
341 } // namespace omaha | |
OLD | NEW |