| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_FRAME_TEST_MOCK_IE_EVENT_SINK_TEST_H_ |
| 6 #define CHROME_FRAME_TEST_MOCK_IE_EVENT_SINK_TEST_H_ |
| 7 |
| 8 #include <atlbase.h> |
| 9 #include <atlcom.h> |
| 10 #include <string> |
| 11 |
| 12 #include "chrome_frame/test/chrome_frame_test_utils.h" |
| 13 #include "chrome_frame/test/ie_event_sink.h" |
| 14 #include "chrome_frame/test/test_server.h" |
| 15 #include "chrome_frame/test/test_with_web_server.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace chrome_frame_test { |
| 20 |
| 21 // Convenience enum for specifying whether a load occurred in IE or CF. |
| 22 enum LoadedInRenderer { |
| 23 IN_IE = 0, |
| 24 IN_CF |
| 25 }; |
| 26 |
| 27 // This mocks an IEEventListener, providing methods for expecting certain |
| 28 // sequences of events. |
| 29 class MockIEEventSink : public IEEventListener { |
| 30 public: |
| 31 ~MockIEEventSink() { |
| 32 Detach(); |
| 33 } |
| 34 |
| 35 // Override IEEventListener methods. |
| 36 MOCK_METHOD7(OnBeforeNavigate2, void (IDispatch* dispatch, // NOLINT |
| 37 VARIANT* url, |
| 38 VARIANT* flags, |
| 39 VARIANT* target_frame_name, |
| 40 VARIANT* post_data, |
| 41 VARIANT* headers, |
| 42 VARIANT_BOOL* cancel)); |
| 43 MOCK_METHOD2(OnNavigateComplete2, void (IDispatch* dispatch, // NOLINT |
| 44 VARIANT* url)); |
| 45 MOCK_METHOD5(OnNewWindow3, void (IDispatch** dispatch, // NOLINT |
| 46 VARIANT_BOOL* cancel, |
| 47 DWORD flags, |
| 48 BSTR url_context, |
| 49 BSTR url)); |
| 50 MOCK_METHOD2(OnNewWindow2, void (IDispatch** dispatch, // NOLINT |
| 51 VARIANT_BOOL* cancel)); |
| 52 MOCK_METHOD5(OnNavigateError, void (IDispatch* dispatch, // NOLINT |
| 53 VARIANT* url, |
| 54 VARIANT* frame_name, |
| 55 VARIANT* status_code, |
| 56 VARIANT* cancel)); |
| 57 MOCK_METHOD2(OnFileDownload, void (VARIANT_BOOL active_doc, // NOLINT |
| 58 VARIANT_BOOL* cancel)); |
| 59 MOCK_METHOD0(OnQuit, void ()); // NOLINT |
| 60 MOCK_METHOD1(OnLoadError, void (const wchar_t* url)); // NOLINT |
| 61 MOCK_METHOD3(OnMessage, void (const wchar_t* message, // NOLINT |
| 62 const wchar_t* origin, |
| 63 const wchar_t* source)); |
| 64 MOCK_METHOD2(OnNewBrowserWindow, void (IDispatch* dispatch, // NOLINT |
| 65 const wchar_t* url)); |
| 66 |
| 67 // Convenience OnLoad method which is called once when a page is loaded with |
| 68 // |is_cf| set to whether the renderer is CF or not. |
| 69 MOCK_METHOD2(OnLoad, void (bool is_cf, const wchar_t* url)); // NOLINT |
| 70 |
| 71 // Attach |dispatch| to the event sink and begin listening to the sink's |
| 72 // events. |
| 73 void Attach(IDispatch* dispatch) { |
| 74 event_sink_.set_listener(this); |
| 75 event_sink_.Attach(dispatch); |
| 76 } |
| 77 |
| 78 void Detach() { |
| 79 event_sink_.set_listener(NULL); |
| 80 event_sink_.Uninitialize(); |
| 81 } |
| 82 |
| 83 // Expect a normal navigation to |url| to occur in CF or IE. |
| 84 void ExpectNavigation(bool is_cf, const std::wstring& url); |
| 85 |
| 86 // Same as above, but used when the new navigation is to a diffrent fragment |
| 87 // in the same page. |
| 88 void ExpectAnchorNavigation(bool is_cf, const std::wstring& url); |
| 89 |
| 90 // Expect a navigation in a new window created by a window.open call to |url|. |
| 91 // |parent_cf| signifies whether the parent frame was loaded in CF, while |
| 92 // |new_window_cf| signifies whether to expect the new page to be loaded in |
| 93 // CF. |
| 94 void ExpectJavascriptWindowOpenNavigation(bool parent_cf, bool new_window_cf, |
| 95 const std::wstring& url); |
| 96 |
| 97 // Expect a new window to open. The new event sink will be attached to |
| 98 // |new_window_mock|. |
| 99 void ExpectNewWindow(MockIEEventSink* new_window_mock); |
| 100 |
| 101 // Expects any and all navigations. |
| 102 void ExpectAnyNavigations(); |
| 103 |
| 104 IEEventSink* event_sink() { return &event_sink_; } |
| 105 |
| 106 private: |
| 107 // Override IE's OnDocumentComplete to call our OnLoad, iff it is IE actually |
| 108 // rendering the page. |
| 109 virtual void OnDocumentComplete(IDispatch* dispatch, VARIANT* url) { |
| 110 if (!event_sink_.IsCFRendering()) |
| 111 OnLoad(IN_IE, V_BSTR(url)); |
| 112 } |
| 113 |
| 114 // Override CF's OnLoad to call our OnLoad. |
| 115 virtual void OnLoad(const wchar_t* url) { |
| 116 OnLoad(IN_CF, url); |
| 117 } |
| 118 |
| 119 // Helper method for expecting navigations. |before_cardinality| specifies |
| 120 // the cardinality for the BeforeNavigate expectation and |
| 121 // |complete_cardinality| specifies the cardinality for the NavigateComplete |
| 122 // expectation. Returns the set of expectations added. |
| 123 // Note: Prefer making a new Expect... method before making this public. |
| 124 testing::ExpectationSet ExpectNavigationCardinality(const std::wstring& url, |
| 125 testing::Cardinality before_cardinality, |
| 126 testing::Cardinality complete_cardinality); |
| 127 |
| 128 CComObjectStackEx<IEEventSink> event_sink_; |
| 129 }; |
| 130 |
| 131 // Mocks a window observer so that tests can detect new windows. |
| 132 class MockWindowObserver : public WindowObserver { |
| 133 public: |
| 134 // Override WindowObserver methods. |
| 135 MOCK_METHOD2(OnWindowDetected, void (HWND hwnd, // NOLINT |
| 136 const std::string& caption)); |
| 137 |
| 138 // Watch for all windows of the given class type. |
| 139 void WatchWindow(const wchar_t* window_class) { |
| 140 DCHECK(window_class); |
| 141 window_watcher_.AddObserver(this, WideToUTF8(window_class)); |
| 142 } |
| 143 |
| 144 private: |
| 145 WindowWatchdog window_watcher_; |
| 146 }; |
| 147 |
| 148 // This test fixture provides common methods needed for testing CF |
| 149 // integration with IE. gMock is used to verify that IE is reporting correct |
| 150 // navigational events and MockWebServer is used to verify that the correct |
| 151 // requests are going out. |
| 152 class MockIEEventSinkTest { |
| 153 public: |
| 154 MockIEEventSinkTest(); |
| 155 |
| 156 ~MockIEEventSinkTest() { |
| 157 // Detach manually here so that it occurs before |last_resort_close_ie_| |
| 158 // is destroyed. |
| 159 ie_mock_.Detach(); |
| 160 } |
| 161 |
| 162 // Launches IE as a COM server and sets |ie_mock_| as the event sink, then |
| 163 // navigates to the given url. Then the timed message loop is run until |
| 164 // |ie_mock_| receives OnQuit or the timeout is exceeded. |
| 165 void LaunchIEAndNavigate(const std::wstring& url); |
| 166 |
| 167 // Same as above but allows the timeout to be specified. |
| 168 void LaunchIENavigateAndLoop(const std::wstring& url, int timeout); |
| 169 |
| 170 // Returns the url for the test file given. |relative_path| should be |
| 171 // relative to the test data directory. |
| 172 std::wstring GetTestUrl(const std::wstring& relative_path); |
| 173 |
| 174 // Returns the absolute FilePath for the test file given. |relative_path| |
| 175 // should be relative to the test data directory. |
| 176 FilePath GetTestFilePath(const std::wstring& relative_path); |
| 177 |
| 178 // Returns the url for an html page just containing some text. Iff |use_cf| |
| 179 // is true, the chrome_frame meta tag will be included too. |
| 180 std::wstring GetSimplePageUrl() { |
| 181 return GetTestUrl(L"simple.html"); |
| 182 } |
| 183 |
| 184 // Returns the url for an html page just containing one link to the simple |
| 185 // page mentioned above. |
| 186 std::wstring GetLinkPageUrl() { |
| 187 return GetTestUrl(L"link.html"); |
| 188 } |
| 189 |
| 190 // Returns the url for an html page containing several anchors pointing |
| 191 // to different parts of the page. |index| specifies what fragment to |
| 192 // append to the url. If zero, no fragment is appended. The highest fragment |
| 193 // is #a4. |
| 194 std::wstring GetAnchorPageUrl(int index) { |
| 195 DCHECK_LT(index, 5); |
| 196 std::wstring base_name = L"anchor.html"; |
| 197 if (index > 0) |
| 198 base_name += std::wstring(L"#a") + IntToWString(index); |
| 199 return GetTestUrl(base_name); |
| 200 } |
| 201 |
| 202 protected: |
| 203 CloseIeAtEndOfScope last_resort_close_ie_; |
| 204 chrome_frame_test::TimedMsgLoop loop_; |
| 205 testing::StrictMock<MockIEEventSink> ie_mock_; |
| 206 testing::StrictMock<MockWebServer> server_mock_; |
| 207 |
| 208 private: |
| 209 DISALLOW_COPY_AND_ASSIGN(MockIEEventSinkTest); |
| 210 }; |
| 211 |
| 212 } // namespace chrome_frame_test |
| 213 |
| 214 #endif // CHROME_FRAME_TEST_MOCK_IE_EVENT_SINK_TEST_H_ |
| OLD | NEW |