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