| 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 "base/strings/utf_string_conversions.h" | 5 #include "base/strings/utf_string_conversions.h" |
| 6 #include "chrome/browser/extensions/api/tabs/tabs_api.h" | 6 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
| 7 #include "chrome/browser/extensions/extension_function_test_utils.h" | 7 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 8 #include "chrome/browser/extensions/extension_service_test_base.h" | 8 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 9 #include "chrome/browser/extensions/extension_tab_util.h" | 9 #include "chrome/browser/extensions/extension_tab_util.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/test/base/test_browser_window.h" | 12 #include "chrome/test/base/test_browser_window.h" |
| 13 #include "content/public/browser/navigation_entry.h" | 13 #include "content/public/browser/navigation_entry.h" |
| 14 #include "content/public/test/test_web_contents_factory.h" | |
| 15 #include "content/public/test/web_contents_tester.h" | 14 #include "content/public/test/web_contents_tester.h" |
| 16 #include "extensions/common/extension_builder.h" | 15 #include "extensions/common/extension_builder.h" |
| 17 #include "extensions/common/test_util.h" | 16 #include "extensions/common/test_util.h" |
| 18 | 17 |
| 19 namespace extensions { | 18 namespace extensions { |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 std::unique_ptr<base::ListValue> RunTabsQueryFunction( | 22 std::unique_ptr<base::ListValue> RunTabsQueryFunction( |
| 24 Browser* browser, | 23 Browser* browser, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 ExtensionServiceTestBase::TearDown(); | 70 ExtensionServiceTestBase::TearDown(); |
| 72 } | 71 } |
| 73 | 72 |
| 74 TEST_F(TabsApiUnitTest, QueryWithoutTabsPermission) { | 73 TEST_F(TabsApiUnitTest, QueryWithoutTabsPermission) { |
| 75 GURL tab_urls[] = {GURL("http://www.google.com"), | 74 GURL tab_urls[] = {GURL("http://www.google.com"), |
| 76 GURL("http://www.example.com"), | 75 GURL("http://www.example.com"), |
| 77 GURL("https://www.google.com")}; | 76 GURL("https://www.google.com")}; |
| 78 std::string tab_titles[] = {"", "Sample title", "Sample title"}; | 77 std::string tab_titles[] = {"", "Sample title", "Sample title"}; |
| 79 | 78 |
| 80 // Add 3 web contentses to the browser. | 79 // Add 3 web contentses to the browser. |
| 81 content::TestWebContentsFactory factory; | 80 std::unique_ptr<content::WebContents> web_contentses[arraysize(tab_urls)]; |
| 82 content::WebContents* web_contentses[arraysize(tab_urls)]; | |
| 83 for (size_t i = 0; i < arraysize(tab_urls); ++i) { | 81 for (size_t i = 0; i < arraysize(tab_urls); ++i) { |
| 84 content::WebContents* web_contents = factory.CreateWebContents(profile()); | 82 content::WebContents* web_contents = |
| 85 web_contentses[i] = web_contents; | 83 content::WebContentsTester::CreateTestWebContents(profile(), nullptr); |
| 84 web_contentses[i].reset(web_contents); |
| 86 browser()->tab_strip_model()->AppendWebContents(web_contents, true); | 85 browser()->tab_strip_model()->AppendWebContents(web_contents, true); |
| 87 EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(), | 86 EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(), |
| 88 web_contents); | 87 web_contents); |
| 89 content::WebContentsTester* web_contents_tester = | 88 content::WebContentsTester* web_contents_tester = |
| 90 content::WebContentsTester::For(web_contents); | 89 content::WebContentsTester::For(web_contents); |
| 91 web_contents_tester->NavigateAndCommit(tab_urls[i]); | 90 web_contents_tester->NavigateAndCommit(tab_urls[i]); |
| 92 web_contents->GetController().GetVisibleEntry()->SetTitle( | 91 web_contents->GetController().GetVisibleEntry()->SetTitle( |
| 93 base::ASCIIToUTF16(tab_titles[i])); | 92 base::ASCIIToUTF16(tab_titles[i])); |
| 94 } | 93 } |
| 95 | 94 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 117 std::unique_ptr<base::ListValue> tabs_list_with_permission( | 116 std::unique_ptr<base::ListValue> tabs_list_with_permission( |
| 118 RunTabsQueryFunction(browser(), extension_with_permission.get(), | 117 RunTabsQueryFunction(browser(), extension_with_permission.get(), |
| 119 kTitleAndURLQueryInfo)); | 118 kTitleAndURLQueryInfo)); |
| 120 ASSERT_TRUE(tabs_list_with_permission); | 119 ASSERT_TRUE(tabs_list_with_permission); |
| 121 ASSERT_EQ(1u, tabs_list_with_permission->GetSize()); | 120 ASSERT_EQ(1u, tabs_list_with_permission->GetSize()); |
| 122 | 121 |
| 123 const base::DictionaryValue* third_tab_info; | 122 const base::DictionaryValue* third_tab_info; |
| 124 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(0, &third_tab_info)); | 123 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(0, &third_tab_info)); |
| 125 int third_tab_id = -1; | 124 int third_tab_id = -1; |
| 126 ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); | 125 ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); |
| 127 EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contentses[2]), third_tab_id); | 126 EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contentses[2].get()), third_tab_id); |
| 128 } | 127 } |
| 129 | 128 |
| 130 TEST_F(TabsApiUnitTest, QueryWithHostPermission) { | 129 TEST_F(TabsApiUnitTest, QueryWithHostPermission) { |
| 131 GURL tab_urls[] = {GURL("http://www.google.com"), | 130 GURL tab_urls[] = {GURL("http://www.google.com"), |
| 132 GURL("http://www.example.com"), | 131 GURL("http://www.example.com"), |
| 133 GURL("https://www.google.com/test")}; | 132 GURL("https://www.google.com/test")}; |
| 134 std::string tab_titles[] = {"", "Sample title", "Sample title"}; | 133 std::string tab_titles[] = {"", "Sample title", "Sample title"}; |
| 135 | 134 |
| 136 // Add 3 web contentses to the browser. | 135 // Add 3 web contentses to the browser. |
| 137 content::TestWebContentsFactory factory; | 136 std::unique_ptr<content::WebContents> web_contentses[arraysize(tab_urls)]; |
| 138 content::WebContents* web_contentses[arraysize(tab_urls)]; | |
| 139 for (size_t i = 0; i < arraysize(tab_urls); ++i) { | 137 for (size_t i = 0; i < arraysize(tab_urls); ++i) { |
| 140 content::WebContents* web_contents = factory.CreateWebContents(profile()); | 138 content::WebContents* web_contents = |
| 141 web_contentses[i] = web_contents; | 139 content::WebContentsTester::CreateTestWebContents(profile(), nullptr); |
| 140 web_contentses[i].reset(web_contents); |
| 142 browser()->tab_strip_model()->AppendWebContents(web_contents, true); | 141 browser()->tab_strip_model()->AppendWebContents(web_contents, true); |
| 143 EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(), | 142 EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(), |
| 144 web_contents); | 143 web_contents); |
| 145 content::WebContentsTester* web_contents_tester = | 144 content::WebContentsTester* web_contents_tester = |
| 146 content::WebContentsTester::For(web_contents); | 145 content::WebContentsTester::For(web_contents); |
| 147 web_contents_tester->NavigateAndCommit(tab_urls[i]); | 146 web_contents_tester->NavigateAndCommit(tab_urls[i]); |
| 148 web_contents->GetController().GetVisibleEntry()->SetTitle( | 147 web_contents->GetController().GetVisibleEntry()->SetTitle( |
| 149 base::ASCIIToUTF16(tab_titles[i])); | 148 base::ASCIIToUTF16(tab_titles[i])); |
| 150 } | 149 } |
| 151 | 150 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 169 std::unique_ptr<base::ListValue> tabs_list_with_permission( | 168 std::unique_ptr<base::ListValue> tabs_list_with_permission( |
| 170 RunTabsQueryFunction(browser(), extension_with_permission.get(), | 169 RunTabsQueryFunction(browser(), extension_with_permission.get(), |
| 171 kTitleAndURLQueryInfo)); | 170 kTitleAndURLQueryInfo)); |
| 172 ASSERT_TRUE(tabs_list_with_permission); | 171 ASSERT_TRUE(tabs_list_with_permission); |
| 173 ASSERT_EQ(1u, tabs_list_with_permission->GetSize()); | 172 ASSERT_EQ(1u, tabs_list_with_permission->GetSize()); |
| 174 | 173 |
| 175 const base::DictionaryValue* third_tab_info; | 174 const base::DictionaryValue* third_tab_info; |
| 176 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(0, &third_tab_info)); | 175 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(0, &third_tab_info)); |
| 177 int third_tab_id = -1; | 176 int third_tab_id = -1; |
| 178 ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); | 177 ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); |
| 179 EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contentses[2]), third_tab_id); | 178 EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contentses[2].get()), |
| 179 third_tab_id); |
| 180 } | 180 } |
| 181 | 181 |
| 182 // Try the same without title, first and third tabs will match. | 182 // Try the same without title, first and third tabs will match. |
| 183 const char* kURLQueryInfo = "[{\"url\": \"*://www.google.com/*\"}]"; | 183 const char* kURLQueryInfo = "[{\"url\": \"*://www.google.com/*\"}]"; |
| 184 { | 184 { |
| 185 std::unique_ptr<base::ListValue> tabs_list_with_permission( | 185 std::unique_ptr<base::ListValue> tabs_list_with_permission( |
| 186 RunTabsQueryFunction(browser(), extension_with_permission.get(), | 186 RunTabsQueryFunction(browser(), extension_with_permission.get(), |
| 187 kURLQueryInfo)); | 187 kURLQueryInfo)); |
| 188 ASSERT_TRUE(tabs_list_with_permission); | 188 ASSERT_TRUE(tabs_list_with_permission); |
| 189 ASSERT_EQ(2u, tabs_list_with_permission->GetSize()); | 189 ASSERT_EQ(2u, tabs_list_with_permission->GetSize()); |
| 190 | 190 |
| 191 const base::DictionaryValue* first_tab_info; | 191 const base::DictionaryValue* first_tab_info; |
| 192 const base::DictionaryValue* third_tab_info; | 192 const base::DictionaryValue* third_tab_info; |
| 193 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(0, &first_tab_info)); | 193 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(0, &first_tab_info)); |
| 194 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(1, &third_tab_info)); | 194 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(1, &third_tab_info)); |
| 195 | 195 |
| 196 std::vector<int> expected_tabs_ids; | 196 std::vector<int> expected_tabs_ids; |
| 197 expected_tabs_ids.push_back(ExtensionTabUtil::GetTabId(web_contentses[0])); | 197 expected_tabs_ids.push_back( |
| 198 expected_tabs_ids.push_back(ExtensionTabUtil::GetTabId(web_contentses[2])); | 198 ExtensionTabUtil::GetTabId(web_contentses[0].get())); |
| 199 expected_tabs_ids.push_back( |
| 200 ExtensionTabUtil::GetTabId(web_contentses[2].get())); |
| 199 | 201 |
| 200 int first_tab_id = -1; | 202 int first_tab_id = -1; |
| 201 ASSERT_TRUE(first_tab_info->GetInteger("id", &first_tab_id)); | 203 ASSERT_TRUE(first_tab_info->GetInteger("id", &first_tab_id)); |
| 202 EXPECT_TRUE(ContainsValue(expected_tabs_ids, first_tab_id)); | 204 EXPECT_TRUE(ContainsValue(expected_tabs_ids, first_tab_id)); |
| 203 | 205 |
| 204 int third_tab_id = -1; | 206 int third_tab_id = -1; |
| 205 ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); | 207 ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); |
| 206 EXPECT_TRUE(ContainsValue(expected_tabs_ids, third_tab_id)); | 208 EXPECT_TRUE(ContainsValue(expected_tabs_ids, third_tab_id)); |
| 207 } | 209 } |
| 208 } | 210 } |
| 209 | 211 |
| 210 } // namespace extensions | 212 } // namespace extensions |
| OLD | NEW |