summaryrefslogtreecommitdiffstats
path: root/chrome/browser/automation/automation_tab_helper_browsertest.cc
blob: 8f5ffe9d1055b8413ff6ae98db2f6be788515d75 (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
// Copyright (c) 2012 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.

#include <string>

#include "base/file_path.h"
#include "base/path_service.h"
#include "base/string16.h"
#include "base/stringprintf.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/automation/automation_tab_helper.h"
#include "chrome/browser/automation/mock_tab_event_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "net/base/net_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;

class MockNotificationObserver : public content::NotificationObserver {
 public:
  MockNotificationObserver() { }
  virtual ~MockNotificationObserver() { }

  MOCK_METHOD3(Observe, void(int type,
                             const content::NotificationSource& source,
                             const content::NotificationDetails& details));

  void Register(int type, const content::NotificationSource& source) {
    registrar_.Add(this, type, source);
  }

 private:
  content::NotificationRegistrar registrar_;

  DISALLOW_COPY_AND_ASSIGN(MockNotificationObserver);
};

class AutomationTabHelperBrowserTest : public InProcessBrowserTest {
 public:
  AutomationTabHelperBrowserTest() {
    EnableDOMAutomation();
  }
  virtual ~AutomationTabHelperBrowserTest() { }

  void SetUpInProcessBrowserTestFixture() {
    EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_));
    test_data_dir_ = test_data_dir_.AppendASCII("automation");
  }

  // Add default expectations for a client redirection initiated by script,
  // and quit the message loop when the redirect has completed. This expects
  // that the tab receives news of the redirect before the script returns.
  void ExpectClientRedirectAndBreak(
      MockTabEventObserver* mock_tab_observer,
      MockNotificationObserver* mock_notification_observer) {
    mock_notification_observer->Register(
        content::NOTIFICATION_DOM_OPERATION_RESPONSE,
        content::NotificationService::AllSources());

    testing::InSequence expect_in_sequence;
    EXPECT_CALL(*mock_tab_observer, OnFirstPendingLoad(_));
    EXPECT_CALL(*mock_notification_observer, Observe(
        testing::Eq(
            static_cast<int>(content::NOTIFICATION_DOM_OPERATION_RESPONSE)),
            _, _));
    EXPECT_CALL(*mock_tab_observer, OnNoMorePendingLoads(_))
        .WillOnce(testing::InvokeWithoutArgs(
            MessageLoopForUI::current(), &MessageLoop::Quit));
  }

  // Executes javascript to execute a given test case found in the current
  // page's script. If |wait_for_response| is true, this method will not
  // return until the javascript has been executed.
  // Use with [ASSERT|EXPECT]_NO_FATAL_FAILURE.
  void RunTestCaseInJavaScript(int test_case_number, bool wait_for_response) {
    std::string script = base::StringPrintf("runTestCase(%d);",
                                            test_case_number);
    content::RenderViewHost* host =
        browser()->GetActiveWebContents()->GetRenderViewHost();
    if (wait_for_response) {
      ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
          host, L"", ASCIIToWide(script)));
    } else {
      script += "window.domAutomationController.setAutomationId(0);"
                "window.domAutomationController.send(0);";
      host->ExecuteJavascriptInWebFrame(string16(), ASCIIToUTF16(script));
    }
  }

  // Returns the |AutomationTabHelper| for the first browser's first tab.
  AutomationTabHelper* tab_helper() {
    return browser()->GetTabContentsAt(0)->automation_tab_helper();
  }

 protected:
  FilePath test_data_dir_;
};

IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest, FormSubmission) {
  ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(
      test_data_dir_.AppendASCII("client_redirects.html")));

  MockNotificationObserver mock_notification_observer;
  MockTabEventObserver mock_observer(tab_helper());

  ExpectClientRedirectAndBreak(&mock_observer, &mock_notification_observer);

  ASSERT_NO_FATAL_FAILURE(RunTestCaseInJavaScript(1, false));
  ui_test_utils::RunMessageLoop();
}

IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest,
                       CancelFormSubmission) {
  ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(
      test_data_dir_.AppendASCII("client_redirects.html")));

  MockNotificationObserver mock_notification_observer;
  MockTabEventObserver mock_observer(tab_helper());

  testing::InSequence expect_in_sequence;
  EXPECT_CALL(mock_observer, OnFirstPendingLoad(_)).Times(0);

  ASSERT_NO_FATAL_FAILURE(RunTestCaseInJavaScript(2, true));
}

IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest,
                       JsRedirectToSite) {
  ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(
      test_data_dir_.AppendASCII("client_redirects.html")));

  MockNotificationObserver mock_notification_observer;
  MockTabEventObserver mock_observer(tab_helper());

  ExpectClientRedirectAndBreak(&mock_observer, &mock_notification_observer);

  ASSERT_NO_FATAL_FAILURE(RunTestCaseInJavaScript(3, false));
  ui_test_utils::RunMessageLoop();
}

IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest,
                       JsRedirectToUnknownSite) {
  ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(
      test_data_dir_.AppendASCII("client_redirects.html")));

  MockNotificationObserver mock_notification_observer;
  MockTabEventObserver mock_observer(tab_helper());

  ExpectClientRedirectAndBreak(&mock_observer, &mock_notification_observer);

  ASSERT_NO_FATAL_FAILURE(RunTestCaseInJavaScript(4, false));
  ui_test_utils::RunMessageLoop();
}

IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest,
                       MetaTagRedirect) {
  MockTabEventObserver mock_observer(tab_helper());

  testing::InSequence expect_in_sequence;
  EXPECT_CALL(mock_observer, OnFirstPendingLoad(_));
  EXPECT_CALL(mock_observer, OnNoMorePendingLoads(_));

  ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
      browser(),
      net::FilePathToFileURL(test_data_dir_.AppendASCII("meta_redirect.html")),
      2);
}

// Tests that the load stop event occurs after the window onload handler.
IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest,
                       LoadStopComesAfterOnLoad) {
  MockNotificationObserver mock_notification_observer;
  mock_notification_observer.Register(
      content::NOTIFICATION_DOM_OPERATION_RESPONSE,
      content::NotificationService::AllSources());
  MockTabEventObserver mock_tab_observer(tab_helper());

  EXPECT_CALL(mock_tab_observer, OnFirstPendingLoad(_));
  {
    testing::InSequence expect_in_sequence;
    EXPECT_CALL(mock_notification_observer, Observe(
        testing::Eq(
            static_cast<int>(content::NOTIFICATION_DOM_OPERATION_RESPONSE)),
            _, _));
    EXPECT_CALL(mock_tab_observer, OnNoMorePendingLoads(_));
  }

  ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(
      test_data_dir_.AppendASCII("sends_message_on_load.html")));
}

// Tests that a crashed tab reports that it has stopped loading.
IN_PROC_BROWSER_TEST_F(AutomationTabHelperBrowserTest,
                       CrashedTabStopsLoading) {
  MockTabEventObserver mock_tab_observer(tab_helper());

  testing::InSequence expect_in_sequence;
  EXPECT_CALL(mock_tab_observer, OnFirstPendingLoad(_));
  EXPECT_CALL(mock_tab_observer, OnNoMorePendingLoads(_));

  ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL));
}