Home | History | Annotate | Download | only in prefs
      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
      3 // found in the LICENSE file.
      4 
      5 #include <string>
      6 
      7 #include "base/path_service.h"
      8 #include "base/strings/utf_string_conversions.h"
      9 #include "chrome/browser/download/download_prefs.h"
     10 #include "chrome/browser/prefs/pref_service_syncable.h"
     11 #include "chrome/browser/ui/browser.h"
     12 #include "chrome/browser/ui/browser_commands.h"
     13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     14 #include "chrome/common/chrome_paths.h"
     15 #include "chrome/common/pref_names.h"
     16 #include "chrome/test/base/in_process_browser_test.h"
     17 #include "chrome/test/base/testing_profile.h"
     18 #include "chrome/test/base/ui_test_utils.h"
     19 #include "content/public/browser/web_contents.h"
     20 #include "content/public/test/browser_test_utils.h"
     21 #include "content/public/test/download_test_observer.h"
     22 
     23 using content::BrowserContext;
     24 using content::DownloadManager;
     25 
     26 class PrefsFunctionalTest : public InProcessBrowserTest {
     27  protected:
     28   // Create a DownloadTestObserverTerminal that will wait for the
     29   // specified number of downloads to finish.
     30   scoped_ptr<content::DownloadTestObserver> CreateWaiter(Browser* browser,
     31                                                          int num_downloads) {
     32     DownloadManager* download_manager =
     33         BrowserContext::GetDownloadManager(browser->profile());
     34 
     35     content::DownloadTestObserver* downloads_observer =
     36          new content::DownloadTestObserverTerminal(
     37              download_manager,
     38              num_downloads,
     39              content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
     40     return make_scoped_ptr(downloads_observer);
     41   }
     42 };
     43 
     44 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestDownloadDirPref) {
     45   ASSERT_TRUE(test_server()->Start());
     46 
     47   DownloadManager* download_manager =
     48       BrowserContext::GetDownloadManager(browser()->profile());
     49   base::FilePath download_dir =
     50       (DownloadPrefs::FromDownloadManager(download_manager))->DownloadPath();
     51   base::FilePath new_download_dir = download_dir.AppendASCII("my_downloads");
     52   base::FilePath downloaded_pkg =
     53       new_download_dir.AppendASCII("a_zip_file.zip");
     54 
     55   // If the directory exists, delete it.
     56   if (base::PathExists(new_download_dir)) {
     57     base::DeleteFile(new_download_dir, true);
     58   }
     59 
     60   // Create the new downloads directory.
     61   file_util::CreateDirectory(new_download_dir);
     62   // Set pref to download in new_download_dir.
     63   browser()->profile()->GetPrefs()->SetFilePath(
     64       prefs::kDownloadDefaultDirectory,
     65       new_download_dir);
     66 
     67   // Create a downloads observer.
     68   scoped_ptr<content::DownloadTestObserver> downloads_observer(
     69       CreateWaiter(browser(), 1));
     70   ui_test_utils::NavigateToURL(
     71       browser(),
     72       test_server()->GetURL("files/downloads/a_zip_file.zip"));
     73   // Waits for the download to complete.
     74   downloads_observer->WaitForFinished();
     75   EXPECT_TRUE(base::PathExists(downloaded_pkg));
     76 }
     77 
     78 // Verify image content settings show or hide images.
     79 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestImageContentSettings) {
     80   ASSERT_TRUE(test_server()->Start());
     81 
     82   ui_test_utils::NavigateToURL(
     83       browser(),
     84       test_server()->GetURL("files/settings/image_page.html"));
     85 
     86   bool result = false;
     87   std::string script =
     88       "for (i=0; i < document.images.length; i++) {"
     89       "  if ((document.images[i].naturalWidth != 0) &&"
     90       "      (document.images[i].naturalHeight != 0)) {"
     91       "    window.domAutomationController.send(true);"
     92       "  }"
     93       "}"
     94       "window.domAutomationController.send(false);";
     95   EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
     96       browser()->tab_strip_model()->GetActiveWebContents(),
     97       script,
     98       &result));
     99   EXPECT_TRUE(result);
    100 
    101   base::DictionaryValue value;
    102   value.SetInteger("images", 2);
    103   browser()->profile()->GetPrefs()->Set(prefs::kDefaultContentSettings,
    104                                         value);
    105 
    106   ui_test_utils::NavigateToURL(
    107       browser(),
    108       test_server()->GetURL("files/settings/image_page.html"));
    109 
    110   result = false;
    111   EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
    112       browser()->tab_strip_model()->GetActiveWebContents(),
    113       script,
    114       &result));
    115   EXPECT_FALSE(result);
    116 }
    117 
    118 // Verify that enabling/disabling Javascript in prefs works.
    119 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestJavascriptEnableDisable) {
    120   ASSERT_TRUE(test_server()->Start());
    121 
    122   EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
    123       prefs::kWebKitJavascriptEnabled));
    124   ui_test_utils::NavigateToURL(
    125       browser(),
    126       test_server()->GetURL("files/javaScriptTitle.html"));
    127   EXPECT_EQ(ASCIIToUTF16("Title from script javascript enabled"),
    128             browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
    129   browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled,
    130                                                false);
    131   ui_test_utils::NavigateToURL(
    132       browser(),
    133       test_server()->GetURL("files/javaScriptTitle.html"));
    134   EXPECT_EQ(ASCIIToUTF16("This is html title"),
    135             browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
    136 }
    137 
    138 // Verify DNS prefetching pref.
    139 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestNetworkPredictionEnabledPref) {
    140   EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
    141       prefs::kNetworkPredictionEnabled));
    142   browser()->profile()->GetPrefs()->SetBoolean(prefs::kNetworkPredictionEnabled,
    143                                                false);
    144   EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
    145       prefs::kNetworkPredictionEnabled));
    146 }
    147 
    148 // Verify restore for bookmark bar visibility.
    149 IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest,
    150                        TestSessionRestoreShowBookmarkBar) {
    151   EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
    152       prefs::kShowBookmarkBar));
    153   browser()->profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
    154   EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
    155       prefs::kShowBookmarkBar));
    156 
    157   EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
    158       prefs::kShowBookmarkBar));
    159   EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
    160 }
    161