OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/extensions/extension_browsertest.h" | 5 #include "chrome/browser/extensions/extension_browsertest.h" |
6 #include "chrome/browser/extensions/extension_tab_util.h" | 6 #include "chrome/browser/extensions/extension_tab_util.h" |
7 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
9 #include "chrome/test/base/ui_test_utils.h" | 9 #include "chrome/test/base/ui_test_utils.h" |
10 #include "content/public/test/browser_test_utils.h" | 10 #include "content/public/test/browser_test_utils.h" |
11 #include "extensions/common/manifest_handlers/options_page_info.h" | 11 #include "extensions/common/manifest_handlers/options_page_info.h" |
12 | 12 |
13 namespace extensions { | 13 namespace extensions { |
14 | 14 |
| 15 namespace { |
| 16 |
| 17 const GURL& GetActiveUrl(Browser* browser) { |
| 18 return browser->tab_strip_model() |
| 19 ->GetActiveWebContents() |
| 20 ->GetLastCommittedURL(); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
15 using ExtensionTabUtilBrowserTest = ExtensionBrowserTest; | 25 using ExtensionTabUtilBrowserTest = ExtensionBrowserTest; |
16 | 26 |
17 IN_PROC_BROWSER_TEST_F(ExtensionTabUtilBrowserTest, OpenExtensionsOptionsPage) { | 27 IN_PROC_BROWSER_TEST_F(ExtensionTabUtilBrowserTest, OpenExtensionsOptionsPage) { |
18 // Load an extension with an options page. | 28 // Load an extension with an options page that opens in a tab and one that |
19 const Extension* extension = | 29 // opens in the chrome://extensions page in a view. |
| 30 const Extension* options_in_tab = |
20 LoadExtension(test_data_dir_.AppendASCII("options_page")); | 31 LoadExtension(test_data_dir_.AppendASCII("options_page")); |
21 ASSERT_TRUE(extension); | 32 const Extension* options_in_view = |
22 ASSERT_TRUE(OptionsPageInfo::HasOptionsPage(extension)); | 33 LoadExtension(test_data_dir_.AppendASCII("options_page_in_view")); |
| 34 ASSERT_TRUE(options_in_tab); |
| 35 ASSERT_TRUE(options_in_view); |
| 36 ASSERT_TRUE(OptionsPageInfo::HasOptionsPage(options_in_tab)); |
| 37 ASSERT_TRUE(OptionsPageInfo::HasOptionsPage(options_in_view)); |
23 | 38 |
24 // Start at the new tab page, and then open the extension options page. | 39 // Start at the new tab page, and then open the extension options page. |
25 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab")); | 40 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab")); |
26 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 41 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
27 GURL options_url = OptionsPageInfo::GetOptionsPage(extension); | 42 GURL options_url = OptionsPageInfo::GetOptionsPage(options_in_tab); |
28 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(extension, browser())); | 43 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(options_in_tab, browser())); |
29 | 44 |
30 // Opening the options page should take the new tab and use it, so we should | 45 // Opening the options page should take the new tab and use it, so we should |
31 // have only one tab, and it should be open to the options page. | 46 // have only one tab, and it should be open to the options page. |
32 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 47 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
33 EXPECT_TRUE(content::WaitForLoadStop( | 48 EXPECT_TRUE(content::WaitForLoadStop( |
34 browser()->tab_strip_model()->GetActiveWebContents())); | 49 browser()->tab_strip_model()->GetActiveWebContents())); |
35 EXPECT_EQ(options_url, | 50 EXPECT_EQ(options_url, GetActiveUrl(browser())); |
36 browser()->tab_strip_model()-> | |
37 GetActiveWebContents()->GetLastCommittedURL()); | |
38 | 51 |
39 // Calling OpenOptionsPage again shouldn't result in any new tabs, since we | 52 // Calling OpenOptionsPage again shouldn't result in any new tabs, since we |
40 // re-use the existing options page. | 53 // re-use the existing options page. |
41 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(extension, browser())); | 54 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(options_in_tab, browser())); |
42 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 55 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
43 EXPECT_TRUE(content::WaitForLoadStop( | 56 EXPECT_TRUE(content::WaitForLoadStop( |
44 browser()->tab_strip_model()->GetActiveWebContents())); | 57 browser()->tab_strip_model()->GetActiveWebContents())); |
45 EXPECT_EQ(options_url, | 58 EXPECT_EQ(options_url, GetActiveUrl(browser())); |
46 browser()->tab_strip_model()-> | |
47 GetActiveWebContents()->GetLastCommittedURL()); | |
48 | 59 |
49 // Navigate to google.com (something non-newtab, non-options). Calling | 60 // Navigate to google.com (something non-newtab, non-options). Calling |
50 // OpenOptionsPage() should create a new tab and navigate it to the options | 61 // OpenOptionsPage() should create a new tab and navigate it to the options |
51 // page. So we should have two total tabs, with the active tab pointing to | 62 // page. So we should have two total tabs, with the active tab pointing to |
52 // options. | 63 // options. |
53 ui_test_utils::NavigateToURL(browser(), GURL("http://www.google.com/")); | 64 ui_test_utils::NavigateToURL(browser(), GURL("http://www.google.com/")); |
54 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(extension, browser())); | 65 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(options_in_tab, browser())); |
55 EXPECT_EQ(2, browser()->tab_strip_model()->count()); | 66 EXPECT_EQ(2, browser()->tab_strip_model()->count()); |
56 EXPECT_TRUE(content::WaitForLoadStop( | 67 EXPECT_TRUE(content::WaitForLoadStop( |
57 browser()->tab_strip_model()->GetActiveWebContents())); | 68 browser()->tab_strip_model()->GetActiveWebContents())); |
58 EXPECT_EQ(options_url, | 69 EXPECT_EQ(options_url, GetActiveUrl(browser())); |
59 browser()->tab_strip_model()-> | |
60 GetActiveWebContents()->GetLastCommittedURL()); | |
61 | 70 |
62 // Navigate the tab to a different extension URL, and call OpenOptionsPage(). | 71 // Navigate the tab to a different extension URL, and call OpenOptionsPage(). |
63 // We should not reuse the current tab since it's opened to a page that isn't | 72 // We should not reuse the current tab since it's opened to a page that isn't |
64 // the options page, and we don't want to arbitrarily close extension content. | 73 // the options page, and we don't want to arbitrarily close extension content. |
65 // Regression test for crbug.com/587581. | 74 // Regression test for crbug.com/587581. |
66 ui_test_utils::NavigateToURL( | 75 ui_test_utils::NavigateToURL(browser(), |
67 browser(), extension->GetResourceURL("other.html")); | 76 options_in_tab->GetResourceURL("other.html")); |
68 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(extension, browser())); | 77 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(options_in_tab, browser())); |
69 EXPECT_EQ(3, browser()->tab_strip_model()->count()); | 78 EXPECT_EQ(3, browser()->tab_strip_model()->count()); |
70 EXPECT_TRUE(content::WaitForLoadStop( | 79 EXPECT_TRUE(content::WaitForLoadStop( |
71 browser()->tab_strip_model()->GetActiveWebContents())); | 80 browser()->tab_strip_model()->GetActiveWebContents())); |
72 EXPECT_EQ(options_url, | 81 EXPECT_EQ(options_url, GetActiveUrl(browser())); |
73 browser()->tab_strip_model()-> | |
74 GetActiveWebContents()->GetLastCommittedURL()); | |
75 | 82 |
76 // If the user navigates to the options page e.g. by typing in the url, it | 83 // If the user navigates to the options page e.g. by typing in the url, it |
77 // should not override the currently-open tab. | 84 // should not override the currently-open tab. |
78 ui_test_utils::NavigateToURLWithDisposition( | 85 ui_test_utils::NavigateToURLWithDisposition( |
79 browser(), | 86 browser(), |
80 options_url, | 87 options_url, |
81 NEW_FOREGROUND_TAB, | 88 NEW_FOREGROUND_TAB, |
82 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 89 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
83 EXPECT_EQ(4, browser()->tab_strip_model()->count()); | 90 EXPECT_EQ(4, browser()->tab_strip_model()->count()); |
84 EXPECT_EQ(options_url, | 91 EXPECT_EQ(options_url, GetActiveUrl(browser())); |
85 browser()->tab_strip_model()-> | 92 |
86 GetActiveWebContents()->GetLastCommittedURL()); | 93 // Test the extension that has the options page open in a view inside |
| 94 // chrome://extensions. |
| 95 // Triggering OpenOptionsPage() should create a new tab, since there are none |
| 96 // to override. |
| 97 options_url = GURL("chrome://extensions/?options=" + options_in_view->id()); |
| 98 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(options_in_view, browser())); |
| 99 EXPECT_EQ(5, browser()->tab_strip_model()->count()); |
| 100 EXPECT_TRUE(content::WaitForLoadStop( |
| 101 browser()->tab_strip_model()->GetActiveWebContents())); |
| 102 EXPECT_EQ(options_url, GetActiveUrl(browser())); |
| 103 |
| 104 // Calling it a second time should not create a new tab, since one already |
| 105 // exists with that options page open. |
| 106 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(options_in_view, browser())); |
| 107 EXPECT_EQ(5, browser()->tab_strip_model()->count()); |
| 108 EXPECT_TRUE(content::WaitForLoadStop( |
| 109 browser()->tab_strip_model()->GetActiveWebContents())); |
| 110 EXPECT_EQ(options_url, GetActiveUrl(browser())); |
| 111 |
| 112 // Navigate to chrome://extensions (no options). Calling OpenOptionsPage() |
| 113 // should override that tab rather than opening a new tab. crbug.com/595253. |
| 114 ui_test_utils::NavigateToURL(browser(), GURL("chrome://extensions")); |
| 115 EXPECT_TRUE(ExtensionTabUtil::OpenOptionsPage(options_in_view, browser())); |
| 116 EXPECT_EQ(5, browser()->tab_strip_model()->count()); |
| 117 EXPECT_TRUE(content::WaitForLoadStop( |
| 118 browser()->tab_strip_model()->GetActiveWebContents())); |
| 119 EXPECT_EQ(options_url, GetActiveUrl(browser())); |
87 } | 120 } |
88 | 121 |
89 } // namespace extensions | 122 } // namespace extensions |
OLD | NEW |