summaryrefslogtreecommitdiffstats
path: root/chrome_frame/test/mock_ie_event_sink_test.h
blob: 4a1e31dabfef1b8c4528e9474782dbc37cd5b41b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_FRAME_TEST_MOCK_IE_EVENT_SINK_TEST_H_
#define CHROME_FRAME_TEST_MOCK_IE_EVENT_SINK_TEST_H_

#include <atlbase.h>
#include <atlcom.h>
#include <string>

#include "base/file_path.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "chrome_frame/test/chrome_frame_test_utils.h"
#include "chrome_frame/test/chrome_frame_ui_test_utils.h"
#include "chrome_frame/test/ie_event_sink.h"
#include "chrome_frame/test/test_server.h"
#include "chrome_frame/test/test_with_web_server.h"
#include "chrome_frame/test/win_event_receiver.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/xulrunner-sdk/win/include/accessibility/AccessibleEventId.h"

namespace chrome_frame_test {

// Convenience enum for specifying whether a load occurred in IE or CF.
enum LoadedInRenderer {
  IN_IE = 0,
  IN_CF
};

// This mocks an IEEventListener, providing methods for expecting certain
// sequences of events.
class MockIEEventSink : public IEEventListener {
 public:
  MockIEEventSink() {
    CComObject<IEEventSink>::CreateInstance(&event_sink_);
    event_sink_->AddRef();
  }

  ~MockIEEventSink() {
    Detach();
    int reference_count = event_sink_->reference_count();
    DLOG_IF(ERROR, reference_count != 1)
        << "Event sink is still referenced externally: ref count = "
        << reference_count;
    event_sink_->Release();
  }

  // Override IEEventListener methods.
  MOCK_METHOD7(OnBeforeNavigate2, void (IDispatch* dispatch,  // NOLINT
                                        VARIANT* url,
                                        VARIANT* flags,
                                        VARIANT* target_frame_name,
                                        VARIANT* post_data,
                                        VARIANT* headers,
                                        VARIANT_BOOL* cancel));
  MOCK_METHOD2(OnNavigateComplete2, void (IDispatch* dispatch,  // NOLINT
                                          VARIANT* url));
  MOCK_METHOD5(OnNewWindow3, void (IDispatch** dispatch,  // NOLINT
                                   VARIANT_BOOL* cancel,
                                   DWORD flags,
                                   BSTR url_context,
                                   BSTR url));
  MOCK_METHOD2(OnNewWindow2, void (IDispatch** dispatch,  // NOLINT
                                   VARIANT_BOOL* cancel));
  MOCK_METHOD5(OnNavigateError, void (IDispatch* dispatch,  // NOLINT
                                      VARIANT* url,
                                      VARIANT* frame_name,
                                      VARIANT* status_code,
                                      VARIANT* cancel));
  MOCK_METHOD2(OnFileDownload, void (VARIANT_BOOL active_doc,  // NOLINT
                                     VARIANT_BOOL* cancel));
  MOCK_METHOD0(OnQuit, void ());  // NOLINT
  MOCK_METHOD1(OnLoadError, void (const wchar_t* url));  // NOLINT
  MOCK_METHOD3(OnMessage, void (const wchar_t* message,  // NOLINT
                                const wchar_t* origin,
                                const wchar_t* source));
  MOCK_METHOD2(OnNewBrowserWindow, void (IDispatch* dispatch,  // NOLINT
                                         const wchar_t* url));

  // Convenience OnLoad method which is called once when a page is loaded with
  // |is_cf| set to whether the renderer is CF or not.
  MOCK_METHOD2(OnLoad, void (bool is_cf, const wchar_t* url));  // NOLINT

  // Attach |dispatch| to the event sink and begin listening to the source's
  // events.
  void Attach(IDispatch* dispatch) {
    event_sink_->set_listener(this);
    event_sink_->Attach(dispatch);
  }

  void Detach() {
    event_sink_->set_listener(NULL);
    event_sink_->Uninitialize();
  }

  // Expect a normal navigation to |url| to occur in CF or IE.
  void ExpectNavigation(bool is_cf, const std::wstring& url);

  // Similar as above, but should be used in cases where IE6 with CF may not
  // generate an OnBeforeNavigate event during the navigation. This occurs
  // during in-page navigation (via anchors), form submission, window.open
  // to target "self", and possibly others.
  void ExpectNavigationOptionalBefore(bool is_cf, const std::wstring& url);

  // Expect a navigation in a new window created by a window.open call to |url|.
  // |parent_cf| signifies whether the parent frame was loaded in CF, while
  // |new_window_cf| signifies whether to expect the new page to be loaded in
  // CF.
  void ExpectJavascriptWindowOpenNavigation(bool parent_cf, bool new_window_cf,
                                            const std::wstring& url);

  // Expect a new window to open. The new event sink will be attached to
  // |new_window_mock|.
  void ExpectNewWindow(MockIEEventSink* new_window_mock);

  // Expects any and all navigations.
  void ExpectAnyNavigations();

  void ExpectDocumentReadystate(int ready_state);

  IEEventSink* event_sink() { return event_sink_; }

 private:
  // Override IE's OnDocumentComplete to call our OnLoad, iff it is IE actually
  // rendering the page.
  virtual void OnDocumentComplete(IDispatch* dispatch, VARIANT* url);

  // Override CF's OnLoad to call our OnLoad.
  virtual void OnLoad(const wchar_t* url) {
    OnLoad(IN_CF, url);
  }

  // Helper method for expecting navigations. |before_cardinality| specifies
  // the cardinality for the BeforeNavigate expectation and
  // |complete_cardinality| specifies the cardinality for the NavigateComplete
  // expectation. Returns the set of expectations added.
  // Note: Prefer making a new Expect... method before making this public.
  testing::ExpectationSet ExpectNavigationCardinality(const std::wstring& url,
      testing::Cardinality before_cardinality,
      testing::Cardinality complete_cardinality);

  // It may be necessary to create this on the heap. Otherwise, if the
  // reference count is greater than zero, a debug assert will be triggered
  // in the destructor. This happens at least when IE crashes. In that case,
  // DispEventUnadvise and CoDisconnectObject are not sufficient to decrement
  // the reference count.
  // TODO(kkania): Investigate if the above is true.
  CComObject<IEEventSink>* event_sink_;
};

// This mocks a PropertyNotifySinkListener, providing methods for
// expecting certain sequences of events.
class MockPropertyNotifySinkListener : public PropertyNotifySinkListener {
 public:
  MockPropertyNotifySinkListener() : cookie_(0), sink_(NULL) {
    CComObject<PropertyNotifySinkImpl>::CreateInstance(&sink_);
    sink_->AddRef();
    sink_->set_listener(this);
  }

  ~MockPropertyNotifySinkListener() {
    Detach();
    sink_->set_listener(NULL);
    DLOG_IF(ERROR, sink_->m_dwRef != 1)
        << "Event sink is still referenced externally: ref count = "
        << sink_->m_dwRef;
    sink_->Release();
  }

  // Override PropertyNotifySinkListener methods.
  MOCK_METHOD1(OnChanged, void (DISPID dispid));  // NOLINT

  bool Attach(IUnknown* obj) {
    DCHECK_EQ(cookie_, 0UL);
    DCHECK(obj);
    HRESULT hr = AtlAdvise(obj, sink_->GetUnknown(), IID_IPropertyNotifySink,
                           &cookie_);
    if (SUCCEEDED(hr)) {
      event_source_ = obj;
    } else {
      LOG(ERROR) << StringPrintf("AtlAdvise: 0x%08X", hr);
      cookie_ = 0;
    }

    return SUCCEEDED(hr);
  }

  void Detach() {
    if (event_source_) {
      DCHECK_NE(cookie_, 0UL);
      AtlUnadvise(event_source_, IID_IPropertyNotifySink, cookie_);
      event_source_.Release();
      cookie_ = 0;
    }
  }

 private:
  CComObject<PropertyNotifySinkImpl>* sink_;
  DWORD cookie_;
  ScopedComPtr<IUnknown> event_source_;
};


// Mocks a window observer so that tests can detect new windows.
class MockWindowObserver : public WindowObserver {
 public:
  // Override WindowObserver methods.
  MOCK_METHOD2(OnWindowDetected, void (HWND hwnd,  // NOLINT
                                       const std::string& caption));

  // Watch for all windows of the given class type.
  void WatchWindow(const wchar_t* window_class) {
    DCHECK(window_class);
    window_watcher_.AddObserver(this, WideToUTF8(window_class));
  }

 private:
  WindowWatchdog window_watcher_;
};

class MockAccEventObserver : public AccEventObserver {
 public:
  MOCK_METHOD1(OnAccDocLoad, void (HWND));  // NOLINT
  MOCK_METHOD3(OnAccValueChange, void (HWND, AccObject*,  // NOLINT
                                       const std::wstring&));
  MOCK_METHOD1(OnMenuPopup, void (HWND));  // NOLINT
};

// This test fixture provides common methods needed for testing CF
// integration with IE. gMock is used to verify that IE is reporting correct
// navigational events and MockWebServer is used to verify that the correct
// requests are going out.
class MockIEEventSinkTest {
 public:
  MockIEEventSinkTest();

  ~MockIEEventSinkTest() {
    // Detach manually here so that it occurs before |last_resort_close_ie_|
    // is destroyed.
    ie_mock_.Detach();
  }

  // Launches IE as a COM server and sets |ie_mock_| as the event sink, then
  // navigates to the given url. Then the timed message loop is run until
  // |ie_mock_| receives OnQuit or the timeout is exceeded.
  void LaunchIEAndNavigate(const std::wstring& url);

  // Same as above but allows the timeout to be specified.
  void LaunchIENavigateAndLoop(const std::wstring& url, int timeout);

  // Returns the url for the test file given. |relative_path| should be
  // relative to the test data directory.
  std::wstring GetTestUrl(const std::wstring& relative_path);

  // Returns the absolute FilePath for the test file given. |relative_path|
  // should be relative to the test data directory.
  FilePath GetTestFilePath(const std::wstring& relative_path);

  // Returns the url for an html page just containing some text. Iff |use_cf|
  // is true, the chrome_frame meta tag will be included too.
  std::wstring GetSimplePageUrl() {
    return GetTestUrl(L"simple.html");
  }

  // Returns the url for an html page just containing one link to the simple
  // page mentioned above.
  std::wstring GetLinkPageUrl() {
    return GetTestUrl(L"link.html");
  }

  // Returns the url for an html page containing several anchors pointing
  // to different parts of the page. |index| specifies what fragment to
  // append to the url. If zero, no fragment is appended. The highest fragment
  // is #a4.
  std::wstring GetAnchorPageUrl(int index) {
    DCHECK_LT(index, 5);
    std::wstring base_name = L"anchor.html";
    if (index > 0)
      base_name += std::wstring(L"#a") + base::IntToString16(index);
    return GetTestUrl(base_name);
  }

 protected:
  CloseIeAtEndOfScope last_resort_close_ie_;
  chrome_frame_test::TimedMsgLoop loop_;
  testing::StrictMock<MockIEEventSink> ie_mock_;
  testing::StrictMock<MockWebServer> server_mock_;

 private:
  DISALLOW_COPY_AND_ASSIGN(MockIEEventSinkTest);
};

}  // namespace chrome_frame_test

#endif  // CHROME_FRAME_TEST_MOCK_IE_EVENT_SINK_TEST_H_