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/prefs/pref_service.h" 6 #include "base/prefs/scoped_user_pref_update.h" 7 #include "chrome/browser/extensions/extension_apitest.h" 8 #include "chrome/browser/extensions/extension_service.h" 9 #include "chrome/browser/extensions/extension_web_ui.h" 10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" 13 #include "chrome/common/extensions/manifest_url_handler.h" 14 #include "chrome/common/url_constants.h" 15 #include "chrome/test/base/ui_test_utils.h" 16 #include "content/public/browser/navigation_entry.h" 17 #include "content/public/browser/web_contents.h" 18 #include "extensions/common/constants.h" 19 20 using content::WebContents; 21 22 class ExtensionOverrideTest : public ExtensionApiTest { 23 protected: 24 bool CheckHistoryOverridesContainsNoDupes() { 25 // There should be no duplicate entries in the preferences. 26 const base::DictionaryValue* overrides = 27 browser()->profile()->GetPrefs()->GetDictionary( 28 ExtensionWebUI::kExtensionURLOverrides); 29 30 const base::ListValue* values = NULL; 31 if (!overrides->GetList("history", &values)) 32 return false; 33 34 std::set<std::string> seen_overrides; 35 for (size_t i = 0; i < values->GetSize(); ++i) { 36 std::string value; 37 if (!values->GetString(i, &value)) 38 return false; 39 40 if (seen_overrides.find(value) != seen_overrides.end()) 41 return false; 42 43 seen_overrides.insert(value); 44 } 45 46 return true; 47 } 48 }; 49 50 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, OverrideNewtab) { 51 ASSERT_TRUE(RunExtensionTest("override/newtab")) << message_; 52 { 53 ResultCatcher catcher; 54 // Navigate to the new tab page. The overridden new tab page 55 // will call chrome.test.notifyPass() . 56 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab/")); 57 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); 58 ASSERT_TRUE(tab->GetController().GetVisibleEntry()); 59 EXPECT_TRUE(tab->GetController().GetVisibleEntry()->GetURL(). 60 SchemeIs(extensions::kExtensionScheme)); 61 62 ASSERT_TRUE(catcher.GetNextResult()); 63 } 64 65 // TODO(erikkay) Load a second extension with the same override. 66 // Verify behavior, then unload the first and verify behavior, etc. 67 } 68 69 #if defined(OS_MACOSX) 70 // Hangy: http://crbug.com/70511 71 #define MAYBE_OverrideNewtabIncognito DISABLED_OverrideNewtabIncognito 72 #else 73 #define MAYBE_OverrideNewtabIncognito OverrideNewtabIncognito 74 #endif 75 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, MAYBE_OverrideNewtabIncognito) { 76 ASSERT_TRUE(RunExtensionTest("override/newtab")) << message_; 77 78 // Navigate an incognito tab to the new tab page. We should get the actual 79 // new tab page because we can't load chrome-extension URLs in incognito. 80 Browser* otr_browser = ui_test_utils::OpenURLOffTheRecord( 81 browser()->profile(), GURL("chrome://newtab/")); 82 WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents(); 83 ASSERT_TRUE(tab->GetController().GetVisibleEntry()); 84 EXPECT_FALSE(tab->GetController().GetVisibleEntry()->GetURL(). 85 SchemeIs(extensions::kExtensionScheme)); 86 } 87 88 // Times out consistently on Win, http://crbug.com/45173. 89 #if defined(OS_WIN) 90 #define MAYBE_OverrideHistory DISABLED_OverrideHistory 91 #else 92 #define MAYBE_OverrideHistory OverrideHistory 93 #endif // defined(OS_WIN) 94 95 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, MAYBE_OverrideHistory) { 96 ASSERT_TRUE(RunExtensionTest("override/history")) << message_; 97 { 98 ResultCatcher catcher; 99 // Navigate to the history page. The overridden history page 100 // will call chrome.test.notifyPass() . 101 ui_test_utils::NavigateToURL(browser(), GURL("chrome://history/")); 102 ASSERT_TRUE(catcher.GetNextResult()); 103 } 104 } 105 106 // Regression test for http://crbug.com/41442. 107 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, ShouldNotCreateDuplicateEntries) { 108 const extensions::Extension* extension = 109 LoadExtension(test_data_dir_.AppendASCII("override/history")); 110 ASSERT_TRUE(extension); 111 112 // Simulate several LoadExtension() calls happening over the lifetime of 113 // a preferences file without corresponding UnloadExtension() calls. 114 for (size_t i = 0; i < 3; ++i) { 115 ExtensionWebUI::RegisterChromeURLOverrides( 116 browser()->profile(), 117 extensions::URLOverrides::GetChromeURLOverrides(extension)); 118 } 119 120 ASSERT_TRUE(CheckHistoryOverridesContainsNoDupes()); 121 } 122 123 IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, ShouldCleanUpDuplicateEntries) { 124 // Simulate several LoadExtension() calls happening over the lifetime of 125 // a preferences file without corresponding UnloadExtension() calls. This is 126 // the same as the above test, except for that it is testing the case where 127 // the file already contains dupes when an extension is loaded. 128 base::ListValue* list = new base::ListValue(); 129 for (size_t i = 0; i < 3; ++i) 130 list->Append(new base::StringValue("http://www.google.com/")); 131 132 { 133 DictionaryPrefUpdate update(browser()->profile()->GetPrefs(), 134 ExtensionWebUI::kExtensionURLOverrides); 135 update.Get()->Set("history", list); 136 } 137 138 ASSERT_FALSE(CheckHistoryOverridesContainsNoDupes()); 139 140 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("override/history"))); 141 142 ASSERT_TRUE(CheckHistoryOverridesContainsNoDupes()); 143 } 144