Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / extensions / page_action_browsertest.cc
blobc0cebeff32eb5e4abf2df7cc6072cac67884ef8e
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.
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/extensions/extension_action.h"
7 #include "chrome/browser/extensions/extension_action_manager.h"
8 #include "chrome/browser/extensions/extension_action_test_util.h"
9 #include "chrome/browser/extensions/extension_browsertest.h"
10 #include "chrome/browser/extensions/extension_tab_util.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/switches.h"
19 namespace extensions {
20 namespace {
22 const std::string kFeedPage = "files/feeds/feed.html";
23 const std::string kNoFeedPage = "files/feeds/no_feed.html";
24 const std::string kLocalization =
25 "files/extensions/browsertest/title_localized_pa/simple.html";
27 const std::string kHashPageA =
28 "files/extensions/api_test/page_action/hash_change/test_page_A.html";
29 const std::string kHashPageAHash = kHashPageA + "#asdf";
30 const std::string kHashPageB =
31 "files/extensions/api_test/page_action/hash_change/test_page_B.html";
33 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, PageActionCrash25562) {
34 ASSERT_TRUE(test_server()->Start());
36 base::CommandLine::ForCurrentProcess()->AppendSwitch(
37 switches::kAllowLegacyExtensionManifests);
39 // This page action will not show an icon, since it doesn't specify one but
40 // is included here to test for a crash (http://crbug.com/25562).
41 ASSERT_TRUE(LoadExtension(
42 test_data_dir_.AppendASCII("browsertest")
43 .AppendASCII("crash_25562")));
45 // Navigate to the feed page.
46 GURL feed_url = test_server()->GetURL(kFeedPage);
47 ui_test_utils::NavigateToURL(browser(), feed_url);
48 // We should now have one page action ready to go in the LocationBar.
49 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
52 // Tests that we can load page actions in the Omnibox.
53 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, PageAction) {
54 ASSERT_TRUE(test_server()->Start());
56 ASSERT_TRUE(LoadExtension(
57 test_data_dir_.AppendASCII("subscribe_page_action")));
59 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(0));
61 // Navigate to the feed page.
62 GURL feed_url = test_server()->GetURL(kFeedPage);
63 ui_test_utils::NavigateToURL(browser(), feed_url);
64 // We should now have one page action ready to go in the LocationBar.
65 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
67 // Navigate to a page with no feed.
68 GURL no_feed = test_server()->GetURL(kNoFeedPage);
69 ui_test_utils::NavigateToURL(browser(), no_feed);
70 // Make sure the page action goes away.
71 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(0));
74 // Tests that we don't lose the page action icon on in-page navigations.
75 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, PageActionInPageNavigation) {
76 ASSERT_TRUE(test_server()->Start());
78 base::FilePath extension_path(test_data_dir_.AppendASCII("api_test")
79 .AppendASCII("page_action")
80 .AppendASCII("hash_change"));
81 ASSERT_TRUE(LoadExtension(extension_path));
83 // Page action should become visible when we navigate here.
84 GURL feed_url = test_server()->GetURL(kHashPageA);
85 ui_test_utils::NavigateToURL(browser(), feed_url);
86 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
88 // In-page navigation, page action should remain.
89 feed_url = test_server()->GetURL(kHashPageAHash);
90 ui_test_utils::NavigateToURL(browser(), feed_url);
91 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
93 // Not an in-page navigation, page action should go away.
94 feed_url = test_server()->GetURL(kHashPageB);
95 ui_test_utils::NavigateToURL(browser(), feed_url);
96 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(0));
99 // Tests that the location bar forgets about unloaded page actions.
100 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, UnloadPageAction) {
101 ASSERT_TRUE(test_server()->Start());
103 base::FilePath extension_path(
104 test_data_dir_.AppendASCII("subscribe_page_action"));
105 ASSERT_TRUE(LoadExtension(extension_path));
107 // Navigation prompts the location bar to load page actions.
108 GURL feed_url = test_server()->GetURL(kFeedPage);
109 ui_test_utils::NavigateToURL(browser(), feed_url);
110 content::WebContents* tab =
111 browser()->tab_strip_model()->GetActiveWebContents();
112 EXPECT_EQ(1u, extension_action_test_util::GetTotalPageActionCount(tab));
114 UnloadExtension(last_loaded_extension_id());
116 // Make sure the page action goes away when it's unloaded.
117 EXPECT_EQ(0u, extension_action_test_util::GetTotalPageActionCount(tab));
120 // Tests that we can load page actions in the Omnibox.
121 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, PageActionRefreshCrash) {
122 base::TimeTicks start_time = base::TimeTicks::Now();
124 ExtensionRegistry* registry =
125 extensions::ExtensionRegistry::Get(browser()->profile());
127 size_t size_before = registry->enabled_extensions().size();
129 base::FilePath base_path = test_data_dir_.AppendASCII("browsertest")
130 .AppendASCII("crash_44415");
131 // Load extension A.
132 const Extension* extensionA = LoadExtension(base_path.AppendASCII("ExtA"));
133 ASSERT_TRUE(extensionA);
134 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
135 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
137 LOG(INFO) << "Load extension A done : "
138 << (base::TimeTicks::Now() - start_time).InMilliseconds()
139 << " ms" << std::flush;
141 // Load extension B.
142 const Extension* extensionB = LoadExtension(base_path.AppendASCII("ExtB"));
143 ASSERT_TRUE(extensionB);
144 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(2));
145 ASSERT_EQ(size_before + 2, registry->enabled_extensions().size());
147 LOG(INFO) << "Load extension B done : "
148 << (base::TimeTicks::Now() - start_time).InMilliseconds()
149 << " ms" << std::flush;
151 std::string idA = extensionA->id();
152 ReloadExtension(extensionA->id());
153 // ExtensionA has changed, so refetch it.
154 ASSERT_EQ(size_before + 2, registry->enabled_extensions().size());
155 extensionA = registry->enabled_extensions().GetByID(idA);
157 LOG(INFO) << "Reload extension A done: "
158 << (base::TimeTicks::Now() - start_time).InMilliseconds()
159 << " ms" << std::flush;
161 ReloadExtension(extensionB->id());
163 LOG(INFO) << "Reload extension B done: "
164 << (base::TimeTicks::Now() - start_time).InMilliseconds()
165 << " ms" << std::flush;
167 // This is where it would crash, before http://crbug.com/44415 was fixed.
168 ReloadExtension(extensionA->id());
170 LOG(INFO) << "Test completed : "
171 << (base::TimeTicks::Now() - start_time).InMilliseconds()
172 << " ms" << std::flush;
175 // Tests that tooltips of a page action icon can be specified using UTF8.
176 // See http://crbug.com/25349.
177 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, TitleLocalizationPageAction) {
178 ASSERT_TRUE(test_server()->Start());
180 ExtensionRegistry* registry =
181 extensions::ExtensionRegistry::Get(browser()->profile());
182 const size_t size_before = registry->enabled_extensions().size();
184 base::FilePath extension_path(test_data_dir_.AppendASCII("browsertest")
185 .AppendASCII("title_localized_pa"));
186 const Extension* extension = LoadExtension(extension_path);
187 ASSERT_TRUE(extension);
189 // Any navigation prompts the location bar to load the page action.
190 GURL url = test_server()->GetURL(kLocalization);
191 ui_test_utils::NavigateToURL(browser(), url);
192 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
194 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
196 EXPECT_STREQ(base::WideToUTF8(L"Hreggvi\u00F0ur: l10n page action").c_str(),
197 extension->description().c_str());
198 EXPECT_STREQ(base::WideToUTF8(L"Hreggvi\u00F0ur is my name").c_str(),
199 extension->name().c_str());
200 int tab_id = ExtensionTabUtil::GetTabId(
201 browser()->tab_strip_model()->GetActiveWebContents());
202 EXPECT_STREQ(base::WideToUTF8(L"Hreggvi\u00F0ur").c_str(),
203 ExtensionActionManager::Get(browser()->profile())->
204 GetPageAction(*extension)->
205 GetTitle(tab_id).c_str());
208 } // namespace
209 } // namespace extensions