OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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 #ifndef CHROME_FRAME_TEST_CHROME_FRAME_UNITTESTS_H_ |
| 5 #define CHROME_FRAME_TEST_CHROME_FRAME_UNITTESTS_H_ |
| 6 |
| 7 #include <atlbase.h> |
| 8 #include <atlcom.h> |
| 9 #include <string> |
| 10 #include <exdisp.h> |
| 11 #include <exdispid.h> |
| 12 |
| 13 #include "base/ref_counted.h" |
| 14 #include "base/scoped_handle_win.h" |
| 15 #include "googleurl/src/gurl.h" |
| 16 #include "chrome_frame/test/http_server.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 // Class that: |
| 20 // 1) Starts the local webserver, |
| 21 // 2) Supports launching browsers - Internet Explorer and Firefox with local url |
| 22 // 3) Wait the webserver to finish. It is supposed the test webpage to shutdown |
| 23 // the server by navigating to "kill" page |
| 24 // 4) Supports read the posted results from the test webpage to the "dump" |
| 25 // webserver directory |
| 26 class ChromeFrameTestWithWebServer: public testing::Test { |
| 27 protected: |
| 28 enum BrowserKind { INVALID, IE, FIREFOX, OPERA, SAFARI, CHROME }; |
| 29 |
| 30 bool LaunchBrowser(BrowserKind browser, const wchar_t* url); |
| 31 bool WaitForTestToComplete(int milliseconds); |
| 32 // Waits for the page to notify us of the window.onload event firing. |
| 33 // Note that the milliseconds value is only approximate. |
| 34 bool WaitForOnLoad(int milliseconds); |
| 35 bool ReadResultFile(const std::wstring& file_name, std::string* data); |
| 36 |
| 37 // Launches the specified browser and waits for the test to complete |
| 38 // (see WaitForTestToComplete). Then checks that the outcome is OK. |
| 39 // This function uses EXPECT_TRUE and ASSERT_TRUE for all steps performed |
| 40 // hence no return value. |
| 41 void SimpleBrowserTest(BrowserKind browser, const wchar_t* page, |
| 42 const wchar_t* result_file_to_check); |
| 43 |
| 44 // Same as SimpleBrowserTest but if the browser isn't installed (LaunchBrowser |
| 45 // fails), the function will print out a warning but not treat the test |
| 46 // as failed. |
| 47 // Currently this is how we run Opera tests. |
| 48 void OptionalBrowserTest(BrowserKind browser, const wchar_t* page, |
| 49 const wchar_t* result_file_to_check); |
| 50 |
| 51 // Test if chrome frame correctly reports its version. |
| 52 void VersionTest(BrowserKind browser, const wchar_t* page, |
| 53 const wchar_t* result_file_to_check); |
| 54 |
| 55 void CloseBrowser(); |
| 56 |
| 57 // Ensures (well, at least tries to ensure) that the browser window has focus. |
| 58 bool BringBrowserToTop(); |
| 59 |
| 60 // Returns true iff the specified result file contains 'expected result'. |
| 61 bool CheckResultFile(const std::wstring& file_name, |
| 62 const std::string& expected_result); |
| 63 |
| 64 virtual void SetUp(); |
| 65 virtual void TearDown(); |
| 66 |
| 67 // Important: kind means "sheep" in Icelandic. ?:-o |
| 68 const char* ToString(BrowserKind kind) { |
| 69 switch (kind) { |
| 70 case IE: |
| 71 return "IE"; |
| 72 case FIREFOX: |
| 73 return "Firefox"; |
| 74 case OPERA: |
| 75 return "Opera"; |
| 76 case CHROME: |
| 77 return "Chrome"; |
| 78 case SAFARI: |
| 79 return "Safari"; |
| 80 default: |
| 81 NOTREACHED(); |
| 82 break; |
| 83 } |
| 84 return ""; |
| 85 } |
| 86 |
| 87 BrowserKind browser_; |
| 88 std::wstring results_dir_; |
| 89 ScopedHandle browser_handle_; |
| 90 ChromeFrameHTTPServer server_; |
| 91 }; |
| 92 |
| 93 // This class sets up event sinks to the IWebBrowser interface. Currently it |
| 94 // subscribes to the following events:- |
| 95 // 1. DISPID_BEFORENAVIGATE2 |
| 96 // 2. DISPID_NAVIGATEERROR |
| 97 // 3. DISPID_NAVIGATECOMPLETE2 |
| 98 // Other events can be subscribed to on an if needed basis. |
| 99 class WebBrowserEventSink |
| 100 : public CComObjectRootEx<CComSingleThreadModel>, |
| 101 public IDispEventSimpleImpl<0, WebBrowserEventSink, |
| 102 &DIID_DWebBrowserEvents2> { |
| 103 public: |
| 104 WebBrowserEventSink() |
| 105 : navigation_failed_(false), |
| 106 main_thread_id_(0) { |
| 107 } |
| 108 |
| 109 BEGIN_COM_MAP(WebBrowserEventSink) |
| 110 END_COM_MAP() |
| 111 |
| 112 BEGIN_SINK_MAP(WebBrowserEventSink) |
| 113 SINK_ENTRY_INFO(0, DIID_DWebBrowserEvents2, DISPID_BEFORENAVIGATE2, |
| 114 OnBeforeNavigate2, &kBeforeNavigate2Info) |
| 115 SINK_ENTRY_INFO(0, DIID_DWebBrowserEvents2, DISPID_NAVIGATECOMPLETE2, |
| 116 OnNavigateComplete2, &kNavigateComplete2Info) |
| 117 SINK_ENTRY_INFO(0, DIID_DWebBrowserEvents2, DISPID_NAVIGATEERROR, |
| 118 OnNavigateError, &kNavigateErrorInfo) |
| 119 END_SINK_MAP() |
| 120 |
| 121 STDMETHOD_(void, OnNavigateError)(IDispatch* dispatch, VARIANT* url, |
| 122 VARIANT* frame_name, VARIANT* status_code, |
| 123 VARIANT* cancel) { |
| 124 navigation_failed_ = true; |
| 125 } |
| 126 |
| 127 STDMETHOD(OnBeforeNavigate2)(IDispatch* dispatch, VARIANT* url, VARIANT* |
| 128 flags, VARIANT* target_frame_name, |
| 129 VARIANT* post_data, VARIANT* headers, |
| 130 VARIANT_BOOL* cancel) { |
| 131 DLOG(INFO) << __FUNCTION__; |
| 132 // If a navigation fails then IE issues a navigation to an interstitial |
| 133 // page. Catch this to track navigation errors as the NavigateError |
| 134 // notification does not seem to fire reliably. |
| 135 GURL crack_url(url->bstrVal); |
| 136 if (crack_url.scheme() == "res") { |
| 137 navigation_failed_ = true; |
| 138 } |
| 139 return S_OK; |
| 140 } |
| 141 |
| 142 STDMETHOD_(void, OnNavigateComplete2)(IDispatch* dispatch, VARIANT* url) { |
| 143 DLOG(INFO) << __FUNCTION__; |
| 144 } |
| 145 |
| 146 bool navigation_failed() const { |
| 147 return navigation_failed_; |
| 148 } |
| 149 |
| 150 void set_main_thread_id(DWORD thread_id) { |
| 151 main_thread_id_ = thread_id; |
| 152 } |
| 153 |
| 154 protected: |
| 155 bool navigation_failed_; |
| 156 |
| 157 static _ATL_FUNC_INFO kBeforeNavigate2Info; |
| 158 static _ATL_FUNC_INFO kNavigateComplete2Info; |
| 159 static _ATL_FUNC_INFO kNavigateErrorInfo; |
| 160 DWORD main_thread_id_; |
| 161 }; |
| 162 |
| 163 #endif // CHROME_FRAME_TEST_CHROME_FRAME_UNITTESTS_H_ |
| 164 |
OLD | NEW |