1// Copyright (c) 2011 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 "chrome/test/ui/ui_test.h"
6
7#include "base/test/test_timeouts.h"
8#include "chrome/app/chrome_command_ids.h"
9#include "chrome/browser/prefs/pref_value_store.h"
10#include "chrome/browser/sync/signin_manager.h"
11#include "chrome/browser/ui/webui/new_tab_ui.h"
12#include "chrome/common/chrome_switches.h"
13#include "chrome/common/json_pref_store.h"
14#include "chrome/common/pref_names.h"
15#include "chrome/common/url_constants.h"
16#include "chrome/test/automation/browser_proxy.h"
17#include "chrome/test/automation/tab_proxy.h"
18#include "chrome/test/automation/window_proxy.h"
19#include "chrome/test/testing_pref_service.h"
20
21class NewTabUITest : public UITest {
22 public:
23  NewTabUITest() {
24    dom_automation_enabled_ = true;
25    // Set home page to the empty string so that we can set the home page using
26    // preferences.
27    set_homepage("");
28
29    // Setup the DEFAULT_THEME profile (has fake history entries).
30    set_template_user_data(UITest::ComputeTypicalUserDataSource(
31        ProxyLauncher::DEFAULT_THEME));
32  }
33};
34
35TEST_F(NewTabUITest, NTPHasThumbnails) {
36  // Switch to the "new tab" tab, which should be any new tab after the
37  // first (the first is about:blank).
38  scoped_refptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
39  ASSERT_TRUE(window.get());
40
41  // Bring up a new tab page.
42  ASSERT_TRUE(window->RunCommand(IDC_NEW_TAB));
43
44  scoped_refptr<TabProxy> tab = window->GetActiveTab();
45  ASSERT_TRUE(tab.get());
46
47  // TopSites should return at least 3 non-filler pages.
48  // 8 - 3 = max 5 filler pages.
49  ASSERT_TRUE(WaitUntilJavaScriptCondition(tab, L"",
50      L"window.domAutomationController.send("
51      L"document.getElementsByClassName('filler').length <= 5)",
52      TestTimeouts::action_max_timeout_ms()));
53}
54
55// Sometimes hangs: http://crbug.com/70157
56TEST_F(NewTabUITest, DISABLED_NTPHasLoginName) {
57  scoped_refptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
58  ASSERT_TRUE(window.get());
59
60  ASSERT_TRUE(window->SetStringPreference(prefs::kGoogleServicesUsername,
61                                          "user@gmail.com"));
62  // Bring up a new tab page.
63  ASSERT_TRUE(window->RunCommand(IDC_NEW_TAB));
64
65  scoped_refptr<TabProxy> tab = window->GetActiveTab();
66  ASSERT_TRUE(tab.get());
67
68  std::wstring displayed_username;
69  // The login span should be eventually populated and have the
70  // correct value.
71  ASSERT_TRUE(WaitUntilJavaScriptCondition(tab, L"",
72      L"window.domAutomationController.send("
73      L"document.getElementById('login-username').innerText.length > 0)",
74      TestTimeouts::action_max_timeout_ms()));
75
76  ASSERT_TRUE(tab->ExecuteAndExtractString(
77      L"",
78      L"window.domAutomationController.send("
79      L"document.getElementById('login-username').innerText)",
80      &displayed_username));
81
82  EXPECT_EQ(L"user@gmail.com", displayed_username);
83}
84
85// Loads about:hang into two NTP tabs, ensuring we don't crash.
86// See http://crbug.com/59859.
87TEST_F(NewTabUITest, AboutHangInNTP) {
88  scoped_refptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
89  ASSERT_TRUE(window.get());
90
91  // Bring up a new tab page.
92  ASSERT_TRUE(window->RunCommand(IDC_NEW_TAB));
93  scoped_refptr<TabProxy> tab = window->GetActiveTab();
94  ASSERT_TRUE(tab.get());
95
96  // Navigate to about:hang to stall the process.
97  ASSERT_TRUE(tab->NavigateToURLAsync(GURL(chrome::kAboutHangURL)));
98
99  // Visit about:hang again in another NTP.  Don't bother waiting for the
100  // NTP to load, because it's hung.
101  ASSERT_TRUE(window->RunCommandAsync(IDC_NEW_TAB));
102  scoped_refptr<TabProxy> tab2 = window->GetActiveTab();
103  ASSERT_TRUE(tab2.get());
104  ASSERT_TRUE(tab2->NavigateToURLAsync(GURL(chrome::kAboutHangURL)));
105}
106
107// Allows testing NTP in process-per-tab mode.
108class NewTabUIProcessPerTabTest : public NewTabUITest {
109 public:
110  NewTabUIProcessPerTabTest() : NewTabUITest() {}
111
112 protected:
113  virtual void SetUp() {
114    launch_arguments_.AppendSwitch(switches::kProcessPerTab);
115    UITest::SetUp();
116  }
117};
118
119// Navigates away from NTP before it commits, in process-per-tab mode.
120// Ensures that we don't load the normal page in the NTP process (and thus
121// crash), as in http://crbug.com/69224.
122TEST_F(NewTabUIProcessPerTabTest, NavBeforeNTPCommits) {
123  scoped_refptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
124  ASSERT_TRUE(window.get());
125
126  // Bring up a new tab page.
127  ASSERT_TRUE(window->RunCommand(IDC_NEW_TAB));
128  scoped_refptr<TabProxy> tab = window->GetActiveTab();
129  ASSERT_TRUE(tab.get());
130
131  // Navigate to about:hang to stall the process.
132  ASSERT_TRUE(tab->NavigateToURLAsync(GURL(chrome::kAboutHangURL)));
133
134  // Visit a normal URL in another NTP that hasn't committed.
135  ASSERT_TRUE(window->RunCommandAsync(IDC_NEW_TAB));
136  scoped_refptr<TabProxy> tab2 = window->GetActiveTab();
137  ASSERT_TRUE(tab2.get());
138  ASSERT_TRUE(tab2->NavigateToURL(GURL("data:text/html,hello world")));
139}
140
141// Fails about ~5% of the time on all platforms. http://crbug.com/45001
142TEST_F(NewTabUITest, FLAKY_ChromeInternalLoadsNTP) {
143  scoped_refptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
144  ASSERT_TRUE(window.get());
145
146  // Go to the "new tab page" using its old url, rather than chrome://newtab.
147  scoped_refptr<TabProxy> tab = window->GetTab(0);
148  ASSERT_TRUE(tab.get());
149  ASSERT_TRUE(tab->NavigateToURLAsync(GURL("chrome-internal:")));
150  int load_time;
151  ASSERT_TRUE(automation()->WaitForInitialNewTabUILoad(&load_time));
152
153  // Ensure there are some thumbnails loaded in the page.
154  int thumbnails_count = -1;
155  ASSERT_TRUE(tab->ExecuteAndExtractInt(L"",
156      L"window.domAutomationController.send("
157      L"document.getElementsByClassName('thumbnail-container').length)",
158      &thumbnails_count));
159  EXPECT_GT(thumbnails_count, 0);
160}
161
162TEST_F(NewTabUITest, UpdateUserPrefsVersion) {
163  // PrefService with JSON user-pref file only, no enforced or advised prefs.
164  scoped_ptr<PrefService> prefs(new TestingPrefService);
165
166  // Does the migration
167  NewTabUI::RegisterUserPrefs(prefs.get());
168
169  ASSERT_EQ(NewTabUI::current_pref_version(),
170            prefs->GetInteger(prefs::kNTPPrefVersion));
171
172  // Reset the version
173  prefs->ClearPref(prefs::kNTPPrefVersion);
174  ASSERT_EQ(0, prefs->GetInteger(prefs::kNTPPrefVersion));
175
176  bool migrated = NewTabUI::UpdateUserPrefsVersion(prefs.get());
177  ASSERT_TRUE(migrated);
178  ASSERT_EQ(NewTabUI::current_pref_version(),
179            prefs->GetInteger(prefs::kNTPPrefVersion));
180
181  migrated = NewTabUI::UpdateUserPrefsVersion(prefs.get());
182  ASSERT_FALSE(migrated);
183}
184