Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "base/command_line.h" | |
| 5 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 6 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
| 7 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/search/search.h" | 9 #include "chrome/browser/search/search.h" |
| 10 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 9 #include "chrome/browser/ui/browser_commands.h" | 11 #include "chrome/browser/ui/browser_commands.h" |
| 10 #include "chrome/browser/ui/search/instant_test_utils.h" | 12 #include "chrome/browser/ui/search/instant_test_utils.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 14 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
| 15 #include "chrome/test/base/interactive_test_utils.h" | 17 #include "chrome/test/base/interactive_test_utils.h" |
| 16 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 17 #include "components/omnibox/browser/omnibox_edit_model.h" | 19 #include "components/omnibox/browser/omnibox_edit_model.h" |
| 18 #include "components/omnibox/browser/omnibox_view.h" | 20 #include "components/omnibox/browser/omnibox_view.h" |
| 19 #include "components/omnibox/common/omnibox_focus_state.h" | 21 #include "components/omnibox/common/omnibox_focus_state.h" |
| 20 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
| 23 #include "components/search_engines/template_url.h" | |
| 24 #include "components/search_engines/template_url_data.h" | |
| 25 #include "components/search_engines/template_url_service.h" | |
| 21 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/common/content_switches.h" | |
| 23 #include "content/public/test/browser_test_utils.h" | 29 #include "content/public/test/browser_test_utils.h" |
| 24 #include "content/public/test/test_navigation_observer.h" | 30 #include "content/public/test/test_navigation_observer.h" |
| 25 #include "content/public/test/test_utils.h" | 31 #include "content/public/test/test_utils.h" |
| 26 #include "net/test/embedded_test_server/embedded_test_server.h" | 32 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 27 #include "ui/base/resource/resource_bundle.h" | 33 #include "ui/base/resource/resource_bundle.h" |
| 28 #include "ui/gfx/geometry/point.h" | 34 #include "ui/gfx/geometry/point.h" |
| 29 #include "ui/gfx/geometry/rect.h" | 35 #include "ui/gfx/geometry/rect.h" |
| 30 #include "ui/gfx/geometry/vector2d.h" | 36 #include "ui/gfx/geometry/vector2d.h" |
| 31 | 37 |
| 38 namespace { | |
| 39 | |
| 40 // Switches the browser language to French, and returns true iff successful. | |
| 41 bool SwitchToFrench() { | |
| 42 // Make sure the default language is not French. | |
| 43 std::string default_locale = g_browser_process->GetApplicationLocale(); | |
| 44 EXPECT_NE("fr", default_locale); | |
| 45 | |
| 46 // Switch browser language to French. | |
| 47 std::string loaded_locale = | |
| 48 ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("fr"); | |
| 49 | |
| 50 return loaded_locale == "fr"; | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 // A test class that sets up local_ntp_browsertest.html (which is mostly a copy | |
| 56 // of the real local_ntp.html) as the NTP URL. | |
| 32 class LocalNTPTest : public InProcessBrowserTest, | 57 class LocalNTPTest : public InProcessBrowserTest, |
| 33 public InstantTestBase { | 58 public InstantTestBase { |
| 34 public: | 59 public: |
| 35 LocalNTPTest() {} | 60 LocalNTPTest() {} |
| 36 | 61 |
| 37 GURL other_url() { return https_test_server().GetURL("/simple.html"); } | 62 GURL other_url() { return https_test_server().GetURL("/simple.html"); } |
| 38 | 63 |
| 39 protected: | 64 protected: |
| 40 void SetUpInProcessBrowserTestFixture() override { | 65 void SetUpInProcessBrowserTestFixture() override { |
| 41 ASSERT_TRUE(https_test_server().Start()); | 66 ASSERT_TRUE(https_test_server().Start()); |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 IN_PROC_BROWSER_TEST_F(LocalNTPTest, | 300 IN_PROC_BROWSER_TEST_F(LocalNTPTest, |
| 276 NTPRespectsBrowserLanguageSetting) { | 301 NTPRespectsBrowserLanguageSetting) { |
| 277 // Make sure the default language is not French. | 302 // Make sure the default language is not French. |
| 278 std::string default_locale = g_browser_process->GetApplicationLocale(); | 303 std::string default_locale = g_browser_process->GetApplicationLocale(); |
| 279 EXPECT_NE("fr", default_locale); | 304 EXPECT_NE("fr", default_locale); |
| 280 | 305 |
| 281 // Switch browser language to French. | 306 // Switch browser language to French. |
| 282 std::string loaded_locale = | 307 std::string loaded_locale = |
| 283 ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("fr"); | 308 ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("fr"); |
| 284 | 309 |
| 285 // The platform cannot load the French locale (GetApplicationLocale() is | 310 // If the platform cannot load the French locale (GetApplicationLocale() is |
| 286 // platform specific, and has been observed to fail on a small number of | 311 // platform specific, and has been observed to fail on a small number of |
| 287 // platforms). Abort the test. | 312 // platforms), abort the test. |
| 288 if (loaded_locale != "fr") | 313 if (!SwitchToFrench()) { |
| 314 LOG(ERROR) << "Failed switching to French language, aborting test."; | |
| 289 return; | 315 return; |
| 316 } | |
| 290 | 317 |
| 291 g_browser_process->SetApplicationLocale(loaded_locale); | 318 g_browser_process->SetApplicationLocale(loaded_locale); |
| 292 PrefService* prefs = g_browser_process->local_state(); | 319 PrefService* prefs = g_browser_process->local_state(); |
| 293 prefs->SetString(prefs::kApplicationLocale, loaded_locale); | 320 prefs->SetString(prefs::kApplicationLocale, loaded_locale); |
| 294 | 321 |
| 295 // Setup Instant. | 322 // Setup Instant. |
| 296 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser())); | 323 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser())); |
| 297 FocusOmnibox(); | 324 FocusOmnibox(); |
| 298 | 325 |
| 299 // Open a new tab. | 326 // Open a new tab. |
| 300 ui_test_utils::NavigateToURLWithDisposition( | 327 ui_test_utils::NavigateToURLWithDisposition( |
| 301 browser(), GURL(chrome::kChromeUINewTabURL), | 328 browser(), GURL(chrome::kChromeUINewTabURL), |
| 302 WindowOpenDisposition::NEW_FOREGROUND_TAB, | 329 WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 303 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | 330 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | |
| 304 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 331 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 305 | 332 |
| 306 // Verify that the NTP is in French. | 333 // Verify that the NTP is in French. |
| 307 content::WebContents* active_tab = | 334 content::WebContents* active_tab = |
| 308 browser()->tab_strip_model()->GetActiveWebContents(); | 335 browser()->tab_strip_model()->GetActiveWebContents(); |
| 309 EXPECT_EQ(base::ASCIIToUTF16("Nouvel onglet"), active_tab->GetTitle()); | 336 EXPECT_EQ(base::ASCIIToUTF16("Nouvel onglet"), active_tab->GetTitle()); |
| 310 } | 337 } |
| 338 | |
| 339 // In contrast to LocalNTPTest, this one doesn't set up any special NTP | |
| 340 // wrangling. It just turns on the local NTP. | |
| 341 class LocalNTPSmokeTest : public InProcessBrowserTest { | |
| 342 public: | |
| 343 LocalNTPSmokeTest() {} | |
| 344 | |
| 345 protected: | |
| 346 void SetUpCommandLine(base::CommandLine* cmdline) override { | |
| 347 cmdline->AppendSwitchASCII(switches::kEnableFeatures, "UseGoogleLocalNtp"); | |
| 348 } | |
| 349 | |
| 350 void SetUserSelectedDefaultSearchProvider(const std::string& base_url) { | |
| 351 TemplateURLData data; | |
| 352 data.SetShortName(base::UTF8ToUTF16(base_url)); | |
| 353 data.SetKeyword(base::UTF8ToUTF16(base_url)); | |
| 354 data.SetURL(base_url + "url?bar={searchTerms}"); | |
| 355 | |
| 356 TemplateURLService* template_url_service = | |
| 357 TemplateURLServiceFactory::GetForProfile(browser()->profile()); | |
| 358 TemplateURL* template_url = | |
| 359 template_url_service->Add(base::MakeUnique<TemplateURL>(data)); | |
| 360 template_url_service->SetUserSelectedDefaultSearchProvider(template_url); | |
| 361 } | |
| 362 }; | |
| 363 | |
| 364 IN_PROC_BROWSER_TEST_F(LocalNTPSmokeTest, GoogleNTPLoadsWithoutError) { | |
| 365 // Open a new blank tab. | |
| 366 ui_test_utils::NavigateToURLWithDisposition( | |
| 367 browser(), GURL("about:blank"), WindowOpenDisposition::NEW_FOREGROUND_TAB, | |
| 368 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | |
| 369 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 370 content::WebContents* active_tab = | |
| 371 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 372 ASSERT_FALSE(search::IsInstantNTP(active_tab)); | |
| 373 | |
| 374 // Attach a console observer, listening for any message ("*" pattern). | |
| 375 auto console_observer = | |
| 376 base::MakeUnique<content::ConsoleObserverDelegate>(active_tab, "*"); | |
|
sfiera
2017/04/10 08:53:21
Is there a reason to use a unique_ptr here instead
Marc Treib
2017/04/10 10:11:18
Nope, I don't think so. That's what you get for co
| |
| 377 active_tab->SetDelegate(console_observer.get()); | |
| 378 | |
| 379 // Navigate to the NTP. | |
| 380 ui_test_utils::NavigateToURLWithDisposition( | |
| 381 browser(), GURL(chrome::kChromeUINewTabURL), | |
| 382 WindowOpenDisposition::CURRENT_TAB, | |
| 383 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 384 ASSERT_TRUE(search::IsInstantNTP(active_tab)); | |
|
sfiera
2017/04/10 08:53:22
Can we also assert that this is the local NTP?
Marc Treib
2017/04/10 10:11:18
Good idea, done.
| |
| 385 | |
| 386 // We shouldn't have gotten any console error messages. | |
| 387 EXPECT_TRUE(console_observer->message().empty()) | |
| 388 << console_observer->message(); | |
| 389 } | |
| 390 | |
| 391 IN_PROC_BROWSER_TEST_F(LocalNTPSmokeTest, NonGoogleNTPLoadsWithoutError) { | |
| 392 SetUserSelectedDefaultSearchProvider("https://www.example.com"); | |
| 393 | |
| 394 // Open a new blank tab. | |
| 395 ui_test_utils::NavigateToURLWithDisposition( | |
| 396 browser(), GURL("about:blank"), WindowOpenDisposition::NEW_FOREGROUND_TAB, | |
| 397 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | |
| 398 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 399 content::WebContents* active_tab = | |
| 400 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 401 ASSERT_FALSE(search::IsInstantNTP(active_tab)); | |
| 402 | |
| 403 // Attach a console observer, listening for any message ("*" pattern). | |
| 404 auto console_observer = | |
| 405 base::MakeUnique<content::ConsoleObserverDelegate>(active_tab, "*"); | |
| 406 active_tab->SetDelegate(console_observer.get()); | |
| 407 | |
| 408 // Navigate to the NTP. | |
| 409 ui_test_utils::NavigateToURLWithDisposition( | |
| 410 browser(), GURL(chrome::kChromeUINewTabURL), | |
| 411 WindowOpenDisposition::CURRENT_TAB, | |
| 412 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 413 ASSERT_TRUE(search::IsInstantNTP(active_tab)); | |
| 414 | |
| 415 // We shouldn't have gotten any console error messages. | |
| 416 EXPECT_TRUE(console_observer->message().empty()) | |
| 417 << console_observer->message(); | |
| 418 } | |
| 419 | |
| 420 IN_PROC_BROWSER_TEST_F(LocalNTPSmokeTest, FrenchGoogleNTPLoadsWithoutError) { | |
| 421 if (!SwitchToFrench()) { | |
| 422 LOG(ERROR) << "Failed switching to French language, aborting test."; | |
| 423 return; | |
| 424 } | |
| 425 | |
| 426 // Open a new blank tab. | |
| 427 ui_test_utils::NavigateToURLWithDisposition( | |
| 428 browser(), GURL("about:blank"), WindowOpenDisposition::NEW_FOREGROUND_TAB, | |
| 429 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | |
| 430 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 431 content::WebContents* active_tab = | |
| 432 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 433 ASSERT_FALSE(search::IsInstantNTP(active_tab)); | |
| 434 | |
| 435 // Attach a console observer, listening for any message ("*" pattern). | |
| 436 auto console_observer = | |
| 437 base::MakeUnique<content::ConsoleObserverDelegate>(active_tab, "*"); | |
| 438 active_tab->SetDelegate(console_observer.get()); | |
| 439 | |
| 440 // Navigate to the NTP. | |
| 441 ui_test_utils::NavigateToURLWithDisposition( | |
| 442 browser(), GURL(chrome::kChromeUINewTabURL), | |
| 443 WindowOpenDisposition::CURRENT_TAB, | |
| 444 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 445 ASSERT_TRUE(search::IsInstantNTP(active_tab)); | |
| 446 // Make sure it's actually in French. | |
| 447 ASSERT_EQ(base::ASCIIToUTF16("Nouvel onglet"), active_tab->GetTitle()); | |
| 448 | |
| 449 // We shouldn't have gotten any console error messages. | |
| 450 EXPECT_TRUE(console_observer->message().empty()) | |
| 451 << console_observer->message(); | |
| 452 } | |
| OLD | NEW |