OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #include <string> | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/path_service.h" | |
9 #include "base/string16.h" | |
10 #include "base/stringprintf.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "chrome/browser/automation/automation_tab_helper.h" | |
13 #include "chrome/browser/automation/mock_tab_event_observer.h" | |
14 #include "chrome/browser/ui/browser.h" | |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
16 #include "chrome/common/chrome_notification_types.h" | |
17 #include "chrome/common/chrome_paths.h" | |
18 #include "chrome/common/url_constants.h" | |
19 #include "chrome/test/base/in_process_browser_test.h" | |
20 #include "chrome/test/base/ui_test_utils.h" | |
21 #include "content/public/browser/notification_details.h" | |
22 #include "content/public/browser/notification_observer.h" | |
23 #include "content/public/browser/notification_registrar.h" | |
24 #include "content/public/browser/notification_service.h" | |
25 #include "content/public/browser/notification_source.h" | |
26 #include "content/public/browser/render_view_host.h" | |
27 #include "content/public/browser/web_contents.h" | |
28 #include "content/public/test/browser_test_utils.h" | |
29 #include "net/base/net_util.h" | |
30 #include "testing/gmock/include/gmock/gmock.h" | |
31 #include "testing/gtest/include/gtest/gtest.h" | |
32 | |
33 using testing::_; | |
34 | |
35 class MockNotificationObserver : public content::NotificationObserver { | |
36 public: | |
37 MockNotificationObserver() { } | |
38 virtual ~MockNotificationObserver() { } | |
39 | |
40 MOCK_METHOD3(Observe, void(int type, | |
41 const content::NotificationSource& source, | |
42 const content::NotificationDetails& details)); | |
43 | |
44 void Register(int type, const content::NotificationSource& source) { | |
45 registrar_.Add(this, type, source); | |
46 } | |
47 | |
48 private: | |
49 content::NotificationRegistrar registrar_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(MockNotificationObserver); | |
52 }; | |
53 | |
54 class AutomationTabHelperBrowserTest : public InProcessBrowserTest { | |
55 public: | |
56 AutomationTabHelperBrowserTest() {} | |
57 virtual ~AutomationTabHelperBrowserTest() {} | |
58 | |
59 void SetUpInProcessBrowserTestFixture() { | |
60 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); | |
61 test_data_dir_ = test_data_dir_.AppendASCII("automation"); | |
62 } | |
63 | |
64 // Add default expectations for a client redirection initiated by script, | |
65 // and quit the message loop when the redirect has completed. This expects | |
66 // that the tab receives news of the redirect before the script returns. | |
67 void ExpectClientRedirectAndBreak( | |
68 MockTabEventObserver* mock_tab_observer, | |
69 MockNotificationObserver* mock_notification_observer) { | |
70 mock_notification_observer->Register( | |
71 content::NOTIFICATION_DOM_OPERATION_RESPONSE, | |
72 content::NotificationService::AllSources()); | |
73 | |
74 testing::InSequence expect_in_sequence; | |
75 EXPECT_CALL(*mock_tab_observer, OnFirstPendingLoad(_)); | |
76 EXPECT_CALL(*mock_notification_observer, Observe( | |
77 testing::Eq( | |
78 static_cast<int>(content::NOTIFICATION_DOM_OPERATION_RESPONSE)), | |
79 _, _)); | |
80 EXPECT_CALL(*mock_tab_observer, OnNoMorePendingLoads(_)) | |
81 .WillOnce(testing::InvokeWithoutArgs( | |
82 MessageLoopForUI::current(), &MessageLoop::Quit)); | |
83 } | |
84 | |
85 // Executes javascript to execute a given test case found in the current | |
86 // page's script. If |wait_for_response| is true, this method will not | |
87 // return until the javascript has been executed. | |
88 // Use with [ASSERT|EXPECT]_NO_FATAL_FAILURE. | |
89 void RunTestCaseInJavaScript(int test_case_number, bool wait_for_response) { | |
90 std::string script = base::StringPrintf("runTestCase(%d);", | |
91 test_case_number); | |
92 content::RenderViewHost* host = browser()->tab_strip_model()-> | |
93 GetActiveWebContents()->GetRenderViewHost(); | |
94 if (wait_for_response) { | |
95 ASSERT_TRUE(content::ExecuteScript(host, script)); | |
96 } else { | |
97 script += "window.domAutomationController.setAutomationId(0);" | |
98 "window.domAutomationController.send(0);"; | |
99 host->ExecuteJavascriptInWebFrame(string16(), ASCIIToUTF16(script)); | |
100 } | |
101 } | |
102 | |
103 // Returns the |AutomationTabHelper| for the first browser's first tab. | |
104 AutomationTabHelper* tab_helper() { | |
105 return AutomationTabHelper::FromWebContents( | |
106 browser()->tab_strip_model()->GetWebContentsAt(0)); | |
107 } | |
108 | |
109 protected: | |
110 base::FilePath test_data_dir_; | |
111 }; | |
112 | |
113 IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest, FormSubmission) { | |
114 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL( | |
115 test_data_dir_.AppendASCII("client_redirects.html"))); | |
116 | |
117 MockNotificationObserver mock_notification_observer; | |
118 MockTabEventObserver mock_observer(tab_helper()); | |
119 | |
120 ExpectClientRedirectAndBreak(&mock_observer, &mock_notification_observer); | |
121 | |
122 ASSERT_NO_FATAL_FAILURE(RunTestCaseInJavaScript(1, false)); | |
123 content::RunMessageLoop(); | |
124 } | |
125 | |
126 IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest, | |
127 CancelFormSubmission) { | |
128 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL( | |
129 test_data_dir_.AppendASCII("client_redirects.html"))); | |
130 | |
131 MockNotificationObserver mock_notification_observer; | |
132 MockTabEventObserver mock_observer(tab_helper()); | |
133 | |
134 testing::InSequence expect_in_sequence; | |
135 EXPECT_CALL(mock_observer, OnFirstPendingLoad(_)).Times(0); | |
136 | |
137 ASSERT_NO_FATAL_FAILURE(RunTestCaseInJavaScript(2, true)); | |
138 } | |
139 | |
140 IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest, | |
141 JsRedirectToSite) { | |
142 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL( | |
143 test_data_dir_.AppendASCII("client_redirects.html"))); | |
144 | |
145 MockNotificationObserver mock_notification_observer; | |
146 MockTabEventObserver mock_observer(tab_helper()); | |
147 | |
148 ExpectClientRedirectAndBreak(&mock_observer, &mock_notification_observer); | |
149 | |
150 ASSERT_NO_FATAL_FAILURE(RunTestCaseInJavaScript(3, false)); | |
151 content::RunMessageLoop(); | |
152 } | |
153 | |
154 IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest, | |
155 JsRedirectToUnknownSite) { | |
156 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL( | |
157 test_data_dir_.AppendASCII("client_redirects.html"))); | |
158 | |
159 MockNotificationObserver mock_notification_observer; | |
160 MockTabEventObserver mock_observer(tab_helper()); | |
161 | |
162 ExpectClientRedirectAndBreak(&mock_observer, &mock_notification_observer); | |
163 | |
164 ASSERT_NO_FATAL_FAILURE(RunTestCaseInJavaScript(4, false)); | |
165 content::RunMessageLoop(); | |
166 } | |
167 | |
168 IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest, | |
169 MetaTagRedirect) { | |
170 MockTabEventObserver mock_observer(tab_helper()); | |
171 | |
172 testing::InSequence expect_in_sequence; | |
173 EXPECT_CALL(mock_observer, OnFirstPendingLoad(_)); | |
174 EXPECT_CALL(mock_observer, OnNoMorePendingLoads(_)); | |
175 | |
176 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
177 browser(), | |
178 net::FilePathToFileURL(test_data_dir_.AppendASCII("meta_redirect.html")), | |
179 2); | |
180 } | |
181 | |
182 // Tests that the load stop event occurs after the window onload handler. | |
183 IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest, | |
184 LoadStopComesAfterOnLoad) { | |
185 MockNotificationObserver mock_notification_observer; | |
186 mock_notification_observer.Register( | |
187 content::NOTIFICATION_DOM_OPERATION_RESPONSE, | |
188 content::NotificationService::AllSources()); | |
189 MockTabEventObserver mock_tab_observer(tab_helper()); | |
190 | |
191 EXPECT_CALL(mock_tab_observer, OnFirstPendingLoad(_)); | |
192 { | |
193 testing::InSequence expect_in_sequence; | |
194 EXPECT_CALL(mock_notification_observer, Observe( | |
195 testing::Eq( | |
196 static_cast<int>(content::NOTIFICATION_DOM_OPERATION_RESPONSE)), | |
197 _, _)); | |
198 EXPECT_CALL(mock_tab_observer, OnNoMorePendingLoads(_)); | |
199 } | |
200 | |
201 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL( | |
202 test_data_dir_.AppendASCII("sends_message_on_load.html"))); | |
203 } | |
204 | |
205 // Tests that a crashed tab reports that it has stopped loading. | |
206 IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest, | |
207 CrashedTabStopsLoading) { | |
208 MockTabEventObserver mock_tab_observer(tab_helper()); | |
209 | |
210 testing::InSequence expect_in_sequence; | |
211 EXPECT_CALL(mock_tab_observer, OnFirstPendingLoad(_)); | |
212 EXPECT_CALL(mock_tab_observer, OnNoMorePendingLoads(_)); | |
213 | |
214 ui_test_utils::NavigateToURL(browser(), GURL(content::kChromeUICrashURL)); | |
215 } | |
OLD | NEW |