Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(956)

Side by Side Diff: chrome/browser/ui/search/local_ntp_browsertest.cc

Issue 2805133004: Local NTP: Deploy strict-dynamic CSP (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
26 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/notification_service.h" 27 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
29 #include "content/public/common/content_switches.h"
23 #include "content/public/test/browser_test_utils.h" 30 #include "content/public/test/browser_test_utils.h"
24 #include "content/public/test/test_navigation_observer.h" 31 #include "content/public/test/test_navigation_observer.h"
25 #include "content/public/test/test_utils.h" 32 #include "content/public/test/test_utils.h"
26 #include "net/test/embedded_test_server/embedded_test_server.h" 33 #include "net/test/embedded_test_server/embedded_test_server.h"
27 #include "ui/base/resource/resource_bundle.h" 34 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/gfx/geometry/point.h" 35 #include "ui/gfx/geometry/point.h"
29 #include "ui/gfx/geometry/rect.h" 36 #include "ui/gfx/geometry/rect.h"
30 #include "ui/gfx/geometry/vector2d.h" 37 #include "ui/gfx/geometry/vector2d.h"
31 38
39 namespace {
40
41 // Switches the browser language to French, and returns true iff successful.
42 bool SwitchToFrench() {
43 // Make sure the default language is not French.
44 std::string default_locale = g_browser_process->GetApplicationLocale();
45 EXPECT_NE("fr", default_locale);
46
47 // Switch browser language to French.
48 std::string loaded_locale =
49 ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("fr");
50
51 return loaded_locale == "fr";
52 }
53
54 } // namespace
55
56 // A test class that sets up local_ntp_browsertest.html (which is mostly a copy
57 // of the real local_ntp.html) as the NTP URL.
32 class LocalNTPTest : public InProcessBrowserTest, 58 class LocalNTPTest : public InProcessBrowserTest,
33 public InstantTestBase { 59 public InstantTestBase {
34 public: 60 public:
35 LocalNTPTest() {} 61 LocalNTPTest() {}
36 62
37 GURL other_url() { return https_test_server().GetURL("/simple.html"); } 63 GURL other_url() { return https_test_server().GetURL("/simple.html"); }
38 64
39 protected: 65 protected:
40 void SetUpInProcessBrowserTestFixture() override { 66 void SetUpInProcessBrowserTestFixture() override {
41 ASSERT_TRUE(https_test_server().Start()); 67 ASSERT_TRUE(https_test_server().Start());
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 IN_PROC_BROWSER_TEST_F(LocalNTPTest, 301 IN_PROC_BROWSER_TEST_F(LocalNTPTest,
276 NTPRespectsBrowserLanguageSetting) { 302 NTPRespectsBrowserLanguageSetting) {
277 // Make sure the default language is not French. 303 // Make sure the default language is not French.
278 std::string default_locale = g_browser_process->GetApplicationLocale(); 304 std::string default_locale = g_browser_process->GetApplicationLocale();
279 EXPECT_NE("fr", default_locale); 305 EXPECT_NE("fr", default_locale);
280 306
281 // Switch browser language to French. 307 // Switch browser language to French.
282 std::string loaded_locale = 308 std::string loaded_locale =
283 ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("fr"); 309 ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("fr");
284 310
285 // The platform cannot load the French locale (GetApplicationLocale() is 311 // If the platform cannot load the French locale (GetApplicationLocale() is
286 // platform specific, and has been observed to fail on a small number of 312 // platform specific, and has been observed to fail on a small number of
287 // platforms). Abort the test. 313 // platforms), abort the test.
288 if (loaded_locale != "fr") 314 if (!SwitchToFrench()) {
315 LOG(ERROR) << "Failed switching to French language, aborting test.";
289 return; 316 return;
317 }
290 318
291 g_browser_process->SetApplicationLocale(loaded_locale); 319 g_browser_process->SetApplicationLocale(loaded_locale);
292 PrefService* prefs = g_browser_process->local_state(); 320 PrefService* prefs = g_browser_process->local_state();
293 prefs->SetString(prefs::kApplicationLocale, loaded_locale); 321 prefs->SetString(prefs::kApplicationLocale, loaded_locale);
294 322
295 // Setup Instant. 323 // Setup Instant.
296 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser())); 324 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
297 FocusOmnibox(); 325 FocusOmnibox();
298 326
299 // Open a new tab. 327 // Open a new tab.
300 ui_test_utils::NavigateToURLWithDisposition( 328 ui_test_utils::NavigateToURLWithDisposition(
301 browser(), GURL(chrome::kChromeUINewTabURL), 329 browser(), GURL(chrome::kChromeUINewTabURL),
302 WindowOpenDisposition::NEW_FOREGROUND_TAB, 330 WindowOpenDisposition::NEW_FOREGROUND_TAB,
303 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | 331 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
304 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 332 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
305 333
306 // Verify that the NTP is in French. 334 // Verify that the NTP is in French.
307 content::WebContents* active_tab = 335 content::WebContents* active_tab =
308 browser()->tab_strip_model()->GetActiveWebContents(); 336 browser()->tab_strip_model()->GetActiveWebContents();
309 EXPECT_EQ(base::ASCIIToUTF16("Nouvel onglet"), active_tab->GetTitle()); 337 EXPECT_EQ(base::ASCIIToUTF16("Nouvel onglet"), active_tab->GetTitle());
310 } 338 }
339
340 // In contrast to LocalNTPTest, this one doesn't set up any special NTP
341 // wrangling. It just turns on the local NTP.
342 class LocalNTPSmokeTest : public InProcessBrowserTest {
343 public:
344 LocalNTPSmokeTest() {}
345
346 protected:
347 void SetUpCommandLine(base::CommandLine* cmdline) override {
348 cmdline->AppendSwitchASCII(switches::kEnableFeatures, "UseGoogleLocalNtp");
349 }
350
351 void SetUserSelectedDefaultSearchProvider(const std::string& base_url) {
352 TemplateURLData data;
353 data.SetShortName(base::UTF8ToUTF16(base_url));
354 data.SetKeyword(base::UTF8ToUTF16(base_url));
355 data.SetURL(base_url + "url?bar={searchTerms}");
356
357 TemplateURLService* template_url_service =
358 TemplateURLServiceFactory::GetForProfile(browser()->profile());
359 TemplateURL* template_url =
360 template_url_service->Add(base::MakeUnique<TemplateURL>(data));
361 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
362 }
363 };
364
365 IN_PROC_BROWSER_TEST_F(LocalNTPSmokeTest, GoogleNTPLoadsWithoutError) {
366 // Open a new blank tab.
367 ui_test_utils::NavigateToURLWithDisposition(
368 browser(), GURL("about:blank"), WindowOpenDisposition::NEW_FOREGROUND_TAB,
369 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
370 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
371 content::WebContents* active_tab =
372 browser()->tab_strip_model()->GetActiveWebContents();
373 ASSERT_FALSE(search::IsInstantNTP(active_tab));
374
375 // Attach a console observer, listening for any message ("*" pattern).
376 content::ConsoleObserverDelegate console_observer(active_tab, "*");
377 active_tab->SetDelegate(&console_observer);
378
379 // Navigate to the NTP.
380 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
381 ASSERT_TRUE(search::IsInstantNTP(active_tab));
382 ASSERT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl),
383 active_tab->GetController().GetVisibleEntry()->GetURL());
384
385 // We shouldn't have gotten any console error messages.
386 EXPECT_TRUE(console_observer.message().empty()) << console_observer.message();
387 }
388
389 IN_PROC_BROWSER_TEST_F(LocalNTPSmokeTest, NonGoogleNTPLoadsWithoutError) {
390 SetUserSelectedDefaultSearchProvider("https://www.example.com");
391
392 // Open a new blank tab.
393 ui_test_utils::NavigateToURLWithDisposition(
394 browser(), GURL("about:blank"), WindowOpenDisposition::NEW_FOREGROUND_TAB,
395 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
396 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
397 content::WebContents* active_tab =
398 browser()->tab_strip_model()->GetActiveWebContents();
399 ASSERT_FALSE(search::IsInstantNTP(active_tab));
400
401 // Attach a console observer, listening for any message ("*" pattern).
402 content::ConsoleObserverDelegate console_observer(active_tab, "*");
403 active_tab->SetDelegate(&console_observer);
404
405 // Navigate to the NTP.
406 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
407 ASSERT_TRUE(search::IsInstantNTP(active_tab));
408 ASSERT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl),
409 active_tab->GetController().GetVisibleEntry()->GetURL());
410
411 // We shouldn't have gotten any console error messages.
412 EXPECT_TRUE(console_observer.message().empty()) << console_observer.message();
413 }
414
415 IN_PROC_BROWSER_TEST_F(LocalNTPSmokeTest, FrenchGoogleNTPLoadsWithoutError) {
416 if (!SwitchToFrench()) {
417 LOG(ERROR) << "Failed switching to French language, aborting test.";
418 return;
419 }
420
421 // Open a new blank tab.
422 ui_test_utils::NavigateToURLWithDisposition(
423 browser(), GURL("about:blank"), WindowOpenDisposition::NEW_FOREGROUND_TAB,
424 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
425 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
426 content::WebContents* active_tab =
427 browser()->tab_strip_model()->GetActiveWebContents();
428 ASSERT_FALSE(search::IsInstantNTP(active_tab));
429
430 // Attach a console observer, listening for any message ("*" pattern).
431 content::ConsoleObserverDelegate console_observer(active_tab, "*");
432 active_tab->SetDelegate(&console_observer);
433
434 // Navigate to the NTP.
435 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
436 ASSERT_TRUE(search::IsInstantNTP(active_tab));
437 ASSERT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl),
438 active_tab->GetController().GetVisibleEntry()->GetURL());
439 // Make sure it's actually in French.
440 ASSERT_EQ(base::ASCIIToUTF16("Nouvel onglet"), active_tab->GetTitle());
441
442 // We shouldn't have gotten any console error messages.
443 EXPECT_TRUE(console_observer.message().empty()) << console_observer.message();
444 }
OLDNEW
« no previous file with comments | « chrome/browser/search/local_ntp_source.cc ('k') | chrome/browser/ui/search/new_tab_page_interceptor_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698