Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / ntp / new_tab_ui_browsertest.cc
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 "base/command_line.h"
6 #include "base/logging.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/test/browser_test_utils.h"
17 #include "content/public/test/test_navigation_observer.h"
18 #include "url/gurl.h"
19
20 using content::OpenURLParams;
21 using content::Referrer;
22
23 namespace {
24
25 static bool had_console_errors = false;
26
27 bool HandleMessage(int severity,
28                    const char* file,
29                    int line,
30                    size_t message_start,
31                    const std::string& str) {
32   if (severity == logging::LOG_ERROR && file && file == std::string("CONSOLE"))
33     had_console_errors = true;
34   return false;
35 }
36
37 }  // namespace
38
39 class NewTabUIBrowserTest : public InProcessBrowserTest {
40  public:
41   NewTabUIBrowserTest() {
42     logging::SetLogMessageHandler(&HandleMessage);
43   }
44
45   ~NewTabUIBrowserTest() override { logging::SetLogMessageHandler(NULL); }
46
47   void TearDown() override {
48     InProcessBrowserTest::TearDown();
49     ASSERT_FALSE(had_console_errors);
50   }
51 };
52
53 // TODO(samarth): delete along with rest of NTP4 code.
54 // #if defined(OS_WIN)
55 // // Flaky on Windows (http://crbug.com/174819)
56 // #define MAYBE_LoadNTPInExistingProcess DISABLED_LoadNTPInExistingProcess
57 // #else
58 // #define MAYBE_LoadNTPInExistingProcess LoadNTPInExistingProcess
59 // #endif
60
61 // Ensure loading a NTP with an existing SiteInstance in a reused process
62 // doesn't cause us to kill the process.  See http://crbug.com/104258.
63 IN_PROC_BROWSER_TEST_F(NewTabUIBrowserTest, DISABLED_LoadNTPInExistingProcess) {
64   // Set max renderers to 1 to force running out of processes.
65   content::RenderProcessHost::SetMaxRendererProcessCount(1);
66
67   // Start server for simple page.
68   ASSERT_TRUE(test_server()->Start());
69
70   // Load a NTP in a new tab.
71   ui_test_utils::NavigateToURLWithDisposition(
72       browser(), GURL(chrome::kChromeUINewTabURL), NEW_FOREGROUND_TAB,
73       ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
74   EXPECT_EQ(1,
75             browser()->tab_strip_model()->GetWebContentsAt(1)->GetMaxPageID());
76
77   // Navigate that tab to another site.  This allows the NTP process to exit,
78   // but it keeps the NTP SiteInstance (and its max_page_id) alive in history.
79   {
80     // Wait not just for the navigation to finish, but for the NTP process to
81     // exit as well.
82     content::RenderProcessHostWatcher process_exited_observer(
83         browser()->tab_strip_model()->GetActiveWebContents(),
84         content::RenderProcessHostWatcher::WATCH_FOR_HOST_DESTRUCTION);
85     browser()->OpenURL(OpenURLParams(
86         test_server()->GetURL("files/title1.html"), Referrer(), CURRENT_TAB,
87         ui::PAGE_TRANSITION_TYPED, false));
88     process_exited_observer.Wait();
89   }
90
91   // Creating a NTP in another tab should not be affected, since page IDs
92   // are now specific to a tab.
93   ui_test_utils::NavigateToURLWithDisposition(
94       browser(), GURL(chrome::kChromeUINewTabURL), NEW_FOREGROUND_TAB,
95       ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
96   EXPECT_EQ(1,
97             browser()->tab_strip_model()->GetWebContentsAt(2)->GetMaxPageID());
98   chrome::CloseTab(browser());
99
100   // Open another Web UI page in a new tab.
101   ui_test_utils::NavigateToURLWithDisposition(
102       browser(), GURL(chrome::kChromeUISettingsURL), NEW_FOREGROUND_TAB,
103       ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
104   EXPECT_EQ(1,
105             browser()->tab_strip_model()->GetWebContentsAt(2)->GetMaxPageID());
106
107   // At this point, opening another NTP will use the existing WebUI process
108   // but its own SiteInstance, so the page IDs shouldn't affect each other.
109   ui_test_utils::NavigateToURLWithDisposition(
110       browser(), GURL(chrome::kChromeUINewTabURL), NEW_FOREGROUND_TAB,
111       ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
112   EXPECT_EQ(1,
113             browser()->tab_strip_model()->GetWebContentsAt(3)->GetMaxPageID());
114
115   // Navigating to the NTP in the original tab causes a BrowsingInstance
116   // swap, so it gets a new SiteInstance starting with page ID 1 again.
117   browser()->tab_strip_model()->ActivateTabAt(1, true);
118   ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
119   EXPECT_EQ(1,
120             browser()->tab_strip_model()->GetWebContentsAt(1)->GetMaxPageID());
121 }
122
123 // TODO(samarth): delete along with rest of NTP4 code.
124 // Loads chrome://hang/ into two NTP tabs, ensuring we don't crash.
125 // See http://crbug.com/59859.
126 // If this flakes, use http://crbug.com/87200.
127 IN_PROC_BROWSER_TEST_F(NewTabUIBrowserTest, DISABLED_ChromeHangInNTP) {
128   // Bring up a new tab page.
129   ui_test_utils::NavigateToURLWithDisposition(
130       browser(), GURL(chrome::kChromeUINewTabURL), NEW_FOREGROUND_TAB,
131       ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
132
133   // Navigate to chrome://hang/ to stall the process.
134   ui_test_utils::NavigateToURLWithDisposition(
135       browser(), GURL(content::kChromeUIHangURL), CURRENT_TAB, 0);
136
137   // Visit chrome://hang/ again in another NTP. Don't bother waiting for the
138   // NTP to load, because it's hung.
139   chrome::NewTab(browser());
140   browser()->OpenURL(OpenURLParams(
141       GURL(content::kChromeUIHangURL), Referrer(), CURRENT_TAB,
142       ui::PAGE_TRANSITION_TYPED, false));
143 }
144
145 // Navigate to incognito NTP. Fails if there are console errors.
146 IN_PROC_BROWSER_TEST_F(NewTabUIBrowserTest, ShowIncognito) {
147   ui_test_utils::NavigateToURL(CreateIncognitoBrowser(),
148                                GURL(chrome::kChromeUINewTabURL));
149 }
150
151 class NewTabUIProcessPerTabTest : public NewTabUIBrowserTest {
152  public:
153    NewTabUIProcessPerTabTest() {}
154
155    void SetUpCommandLine(CommandLine* command_line) override {
156      command_line->AppendSwitch(switches::kProcessPerTab);
157    }
158 };
159
160 // Navigates away from NTP before it commits, in process-per-tab mode.
161 // Ensures that we don't load the normal page in the NTP process (and thus
162 // crash), as in http://crbug.com/69224.
163 // If this flakes, use http://crbug.com/87200
164 IN_PROC_BROWSER_TEST_F(NewTabUIProcessPerTabTest, NavBeforeNTPCommits) {
165   // Bring up a new tab page.
166   ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
167
168   // Navigate to chrome://hang/ to stall the process.
169   ui_test_utils::NavigateToURLWithDisposition(
170       browser(), GURL(content::kChromeUIHangURL), CURRENT_TAB, 0);
171
172   // Visit a normal URL in another NTP that hasn't committed.
173   ui_test_utils::NavigateToURLWithDisposition(
174       browser(), GURL(chrome::kChromeUINewTabURL), NEW_FOREGROUND_TAB, 0);
175
176   // We don't use ui_test_utils::NavigateToURLWithDisposition because that waits
177   // for current loading to stop.
178   content::TestNavigationObserver observer(
179       browser()->tab_strip_model()->GetActiveWebContents());
180   browser()->OpenURL(OpenURLParams(
181       GURL("data:text/html,hello world"), Referrer(), CURRENT_TAB,
182       ui::PAGE_TRANSITION_TYPED, false));
183   observer.Wait();
184 }