| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H_ | 5 #ifndef CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H_ |
| 6 #define CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H_ | 6 #define CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "app/gfx/native_widget_types.h" | 10 #include "app/gfx/native_widget_types.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/platform_thread.h" | 12 #include "base/platform_thread.h" |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "chrome/test/automation/automation_proxy.h" | 14 #include "chrome/test/automation/automation_proxy.h" |
| 15 #include "chrome/test/ui/ui_test.h" | 15 #include "chrome/test/ui/ui_test.h" |
| 16 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 | 18 |
| 18 class TabProxy; | 19 class TabProxy; |
| 19 class ExternalTabUITestMockClient; | 20 class ExternalTabUITestMockClient; |
| 20 | 21 |
| 21 class ExternalTabUITest : public UITest { | |
| 22 public: | |
| 23 // Override UITest's CreateAutomationProxy to provide the unit test | |
| 24 // with our special implementation of AutomationProxy. | |
| 25 // This function is called from within UITest::LaunchBrowserAndServer. | |
| 26 virtual AutomationProxy* CreateAutomationProxy(int execution_timeout); | |
| 27 protected: | |
| 28 // Filtered Inet will override automation callbacks for network resources. | |
| 29 virtual bool ShouldFilterInet() { | |
| 30 return false; | |
| 31 } | |
| 32 ExternalTabUITestMockClient* mock_; | |
| 33 }; | |
| 34 | |
| 35 // Base class for automation proxy testing. | 22 // Base class for automation proxy testing. |
| 36 class AutomationProxyVisibleTest : public UITest { | 23 class AutomationProxyVisibleTest : public UITest { |
| 37 protected: | 24 protected: |
| 38 AutomationProxyVisibleTest() { | 25 AutomationProxyVisibleTest() { |
| 39 show_window_ = true; | 26 show_window_ = true; |
| 40 } | 27 } |
| 41 }; | 28 }; |
| 42 | 29 |
| 43 // Automation proxy UITest that allows tests to override the automation | 30 // Used to implement external tab UI tests. |
| 44 // proxy used by the UITest base class. | 31 // |
| 45 template <class AutomationProxyClass> | 32 // We have to derive from AutomationProxy in order to hook up |
| 46 class CustomAutomationProxyTest : public AutomationProxyVisibleTest { | 33 // OnMessageReceived callbacks. |
| 34 class ExternalTabUITestMockClient : public AutomationProxy { |
| 35 public: |
| 36 explicit ExternalTabUITestMockClient(int execution_timeout); |
| 37 |
| 38 MOCK_METHOD2(OnDidNavigate, void(int tab_handle, |
| 39 const IPC::NavigationInfo& nav_info)); |
| 40 MOCK_METHOD4(OnForwardMessageToExternalHost, void(int handle, |
| 41 const std::string& message, const std::string& origin, |
| 42 const std::string& target)); |
| 43 MOCK_METHOD3(OnRequestStart, void(int tab_handle, int request_id, |
| 44 const IPC::AutomationURLRequest& request)); |
| 45 MOCK_METHOD3(OnRequestRead, void(int tab_handle, int request_id, |
| 46 int bytes_to_read)); |
| 47 MOCK_METHOD3(OnRequestEnd, void(int tab_handle, int request_id, |
| 48 const URLRequestStatus& status)); |
| 49 MOCK_METHOD3(OnSetCookieAsync, void(int tab_handle, const GURL& url, |
| 50 const std::string& cookie)); |
| 51 |
| 52 MOCK_METHOD1(HandleClosed, void(int handle)); |
| 53 |
| 54 // Action helpers for OnRequest* incoming messages. Create the message and |
| 55 // delegate sending to the base class. Apparently we do not have wrappers |
| 56 // in AutomationProxy for these messages. |
| 57 void ReplyStarted(const IPC::AutomationURLResponse* response, |
| 58 int tab_handle, int request_id); |
| 59 void ReplyData(const std::string* data, int tab_handle, int request_id); |
| 60 void ReplyEOF(int tab_handle, int request_id); |
| 61 void Reply404(int tab_handle, int request_id); |
| 62 |
| 63 // Test setup helpers |
| 64 scoped_refptr<TabProxy> CreateHostWindowAndTab( |
| 65 const IPC::ExternalTabSettings& settings); |
| 66 scoped_refptr<TabProxy> CreateTabWithUrl(const GURL& initial_url); |
| 67 |
| 68 // Destroys the host window. |
| 69 void DestroyHostWindow(); |
| 70 // Returns true if the host window exists. |
| 71 bool HostWindowExists(); |
| 72 |
| 73 static const IPC::ExternalTabSettings default_settings; |
| 47 protected: | 74 protected: |
| 48 CustomAutomationProxyTest() { | 75 gfx::NativeWindow host_window_; |
| 49 } | |
| 50 | 76 |
| 51 // Override UITest's CreateAutomationProxy to provide our the unit test | 77 // Simple dispatcher to above OnXXX methods. |
| 78 virtual void OnMessageReceived(const IPC::Message& msg); |
| 79 virtual void InvalidateHandle(const IPC::Message& message); |
| 80 }; |
| 81 |
| 82 // Base your external tab UI tests on this. |
| 83 class ExternalTabUITest : public UITest { |
| 84 public: |
| 85 // Override UITest's CreateAutomationProxy to provide the unit test |
| 52 // with our special implementation of AutomationProxy. | 86 // with our special implementation of AutomationProxy. |
| 53 // This function is called from within UITest::LaunchBrowserAndServer. | 87 // This function is called from within UITest::LaunchBrowserAndServer. |
| 54 virtual AutomationProxy* CreateAutomationProxy(int execution_timeout) { | 88 virtual AutomationProxy* CreateAutomationProxy(int execution_timeout); |
| 55 AutomationProxyClass* proxy = new AutomationProxyClass(execution_timeout); | 89 protected: |
| 56 return proxy; | 90 // Filtered Inet will override automation callbacks for network resources. |
| 91 virtual bool ShouldFilterInet() { |
| 92 return false; |
| 57 } | 93 } |
| 94 ExternalTabUITestMockClient* mock_; |
| 58 }; | 95 }; |
| 59 | 96 |
| 60 // A single-use AutomationProxy implementation that's good | |
| 61 // for a single navigation and a single ForwardMessageToExternalHost | |
| 62 // message. Once the ForwardMessageToExternalHost message is received | |
| 63 // the class posts a quit message to the thread on which the message | |
| 64 // was received. | |
| 65 class AutomationProxyForExternalTab : public AutomationProxy { | |
| 66 public: | |
| 67 // Allows us to reuse this mock for multiple tests. This is done | |
| 68 // by setting a state to trigger posting of Quit message to the | |
| 69 // wait loop. | |
| 70 enum QuitAfter { | |
| 71 QUIT_INVALID, | |
| 72 QUIT_AFTER_NAVIGATION, | |
| 73 QUIT_AFTER_MESSAGE, | |
| 74 }; | |
| 75 | |
| 76 explicit AutomationProxyForExternalTab(int execution_timeout); | |
| 77 ~AutomationProxyForExternalTab(); | |
| 78 | |
| 79 int messages_received() const { | |
| 80 return messages_received_; | |
| 81 } | |
| 82 | |
| 83 const std::string& message() const { | |
| 84 return message_; | |
| 85 } | |
| 86 | |
| 87 const std::string& origin() const { | |
| 88 return origin_; | |
| 89 } | |
| 90 | |
| 91 const std::string& target() const { | |
| 92 return target_; | |
| 93 } | |
| 94 | |
| 95 // Creates and sisplays a top-level window, that can be used as a parent | |
| 96 // to the external tab.window. | |
| 97 gfx::NativeWindow CreateHostWindow(); | |
| 98 scoped_refptr<TabProxy> CreateTabWithHostWindow(bool is_incognito, | |
| 99 const GURL& initial_url, gfx::NativeWindow* container_wnd, | |
| 100 gfx::NativeWindow* tab_wnd); | |
| 101 void DestroyHostWindow(); | |
| 102 | |
| 103 // Wait for the event to happen or timeout | |
| 104 bool WaitForNavigation(int timeout_ms); | |
| 105 bool WaitForMessage(int timeout_ms); | |
| 106 bool WaitForTabCleanup(TabProxy* tab, int timeout_ms); | |
| 107 | |
| 108 // Enters a message loop that processes window messages as well | |
| 109 // as calling MessageLoop::current()->RunAllPending() to process any | |
| 110 // incoming IPC messages. The timeout_ms parameter is the maximum | |
| 111 // time the loop will run. To end the loop earlier, post a quit message to | |
| 112 // the thread. | |
| 113 bool RunMessageLoop(int timeout_ms, gfx::NativeWindow window_to_monitor); | |
| 114 | |
| 115 protected: | |
| 116 #if defined(OS_WIN) | |
| 117 static const int kQuitLoopMessage = WM_APP + 11; | |
| 118 // Quit the message loop | |
| 119 void QuitLoop() { | |
| 120 DCHECK(IsWindow(host_window_)); | |
| 121 // We could post WM_QUIT but lets keep it out of accidental usage | |
| 122 // by anyone else peeking it. | |
| 123 PostMessage(host_window_, kQuitLoopMessage, 0, 0); | |
| 124 } | |
| 125 #endif // defined(OS_WIN) | |
| 126 | |
| 127 // Internal state to flag posting of a quit message to the loop | |
| 128 void set_quit_after(QuitAfter q) { | |
| 129 quit_after_ = q; | |
| 130 } | |
| 131 | |
| 132 virtual void OnMessageReceived(const IPC::Message& msg); | |
| 133 | |
| 134 void OnDidNavigate(int tab_handle, const IPC::NavigationInfo& nav_info); | |
| 135 void OnForwardMessageToExternalHost(int handle, | |
| 136 const std::string& message, | |
| 137 const std::string& origin, | |
| 138 const std::string& target); | |
| 139 | |
| 140 protected: | |
| 141 bool navigate_complete_; | |
| 142 int messages_received_; | |
| 143 std::string message_, origin_, target_; | |
| 144 QuitAfter quit_after_; | |
| 145 const wchar_t* host_window_class_; | |
| 146 gfx::NativeWindow host_window_; | |
| 147 }; | |
| 148 | |
| 149 // A test harness for testing external tabs. | |
| 150 typedef CustomAutomationProxyTest<AutomationProxyForExternalTab> | |
| 151 ExternalTabTestType; | |
| 152 | |
| 153 #endif // CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H_ | 97 #endif // CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H_ |
| OLD | NEW |