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" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 content::WebContentsTester* web_contents_tester = | 90 content::WebContentsTester* web_contents_tester = |
91 content::WebContentsTester::For(web_contents); | 91 content::WebContentsTester::For(web_contents); |
92 web_contents_tester->NavigateAndCommit(tab_urls[i]); | 92 web_contents_tester->NavigateAndCommit(tab_urls[i]); |
93 web_contents->GetController().GetVisibleEntry()->SetTitle( | 93 web_contents->GetController().GetVisibleEntry()->SetTitle( |
94 base::ASCIIToUTF16(tab_titles[i])); | 94 base::ASCIIToUTF16(tab_titles[i])); |
95 } | 95 } |
96 | 96 |
97 const char* kTitleAndURLQueryInfo = | 97 const char* kTitleAndURLQueryInfo = |
98 "[{\"title\": \"Sample title\", \"url\": \"*://www.google.com/*\"}]"; | 98 "[{\"title\": \"Sample title\", \"url\": \"*://www.google.com/*\"}]"; |
99 | 99 |
100 // An extension without "tabs" permission will see all 3 tabs, because the | 100 // An extension without "tabs" permission will see none of the 3 tabs. |
101 // query_info filter will be ignored. | |
102 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); | 101 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); |
103 scoped_ptr<base::ListValue> tabs_list_without_permission( | 102 scoped_ptr<base::ListValue> tabs_list_without_permission( |
104 RunTabsQueryFunction(browser(), extension.get(), kTitleAndURLQueryInfo)); | 103 RunTabsQueryFunction(browser(), extension.get(), kTitleAndURLQueryInfo)); |
105 ASSERT_TRUE(tabs_list_without_permission); | 104 ASSERT_TRUE(tabs_list_without_permission); |
106 EXPECT_EQ(3u, tabs_list_without_permission->GetSize()); | 105 EXPECT_EQ(0u, tabs_list_without_permission->GetSize()); |
107 | 106 |
108 // An extension with "tabs" permission however will only see the third tab. | 107 // An extension with "tabs" permission however will see the third tab. |
109 scoped_refptr<const Extension> extension_with_permission = | 108 scoped_refptr<const Extension> extension_with_permission = |
110 ExtensionBuilder() | 109 ExtensionBuilder() |
111 .SetManifest(std::move( | 110 .SetManifest(std::move( |
112 DictionaryBuilder() | 111 DictionaryBuilder() |
113 .Set("name", "Extension with tabs permission") | 112 .Set("name", "Extension with tabs permission") |
114 .Set("version", "1.0") | 113 .Set("version", "1.0") |
115 .Set("manifest_version", 2) | 114 .Set("manifest_version", 2) |
116 .Set("permissions", std::move(ListBuilder().Append("tabs"))))) | 115 .Set("permissions", std::move(ListBuilder().Append("tabs"))))) |
117 .Build(); | 116 .Build(); |
118 scoped_ptr<base::ListValue> tabs_list_with_permission(RunTabsQueryFunction( | 117 scoped_ptr<base::ListValue> tabs_list_with_permission(RunTabsQueryFunction( |
119 browser(), extension_with_permission.get(), kTitleAndURLQueryInfo)); | 118 browser(), extension_with_permission.get(), 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; | 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]), third_tab_id); |
128 } | 127 } |
129 | 128 |
| 129 TEST_F(TabsApiUnitTest, QueryWithHostPermission) { |
| 130 GURL tab_urls[] = {GURL("http://www.google.com"), |
| 131 GURL("http://www.example.com"), |
| 132 GURL("https://www.google.com/test")}; |
| 133 std::string tab_titles[] = {"", "Sample title", "Sample title"}; |
| 134 |
| 135 // Add 3 web contentses to the browser. |
| 136 content::TestWebContentsFactory factory; |
| 137 content::WebContents* web_contentses[arraysize(tab_urls)]; |
| 138 for (size_t i = 0; i < arraysize(tab_urls); ++i) { |
| 139 content::WebContents* web_contents = factory.CreateWebContents(profile()); |
| 140 web_contentses[i] = web_contents; |
| 141 browser()->tab_strip_model()->AppendWebContents(web_contents, true); |
| 142 EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(), |
| 143 web_contents); |
| 144 content::WebContentsTester* web_contents_tester = |
| 145 content::WebContentsTester::For(web_contents); |
| 146 web_contents_tester->NavigateAndCommit(tab_urls[i]); |
| 147 web_contents->GetController().GetVisibleEntry()->SetTitle( |
| 148 base::ASCIIToUTF16(tab_titles[i])); |
| 149 } |
| 150 |
| 151 const char* kTitleAndURLQueryInfo = |
| 152 "[{\"title\": \"Sample title\", \"url\": \"*://www.google.com/*\"}]"; |
| 153 |
| 154 // An extension with "host" permission will only see the third tab. |
| 155 scoped_refptr<const Extension> extension_with_permission = |
| 156 ExtensionBuilder() |
| 157 .SetManifest( |
| 158 std::move(DictionaryBuilder() |
| 159 .Set("name", "Extension with tabs permission") |
| 160 .Set("version", "1.0") |
| 161 .Set("manifest_version", 2) |
| 162 .Set("permissions", std::move(ListBuilder().Append( |
| 163 "*://www.google.com/*"))))) |
| 164 .Build(); |
| 165 |
| 166 { |
| 167 scoped_ptr<base::ListValue> tabs_list_with_permission(RunTabsQueryFunction( |
| 168 browser(), extension_with_permission.get(), kTitleAndURLQueryInfo)); |
| 169 ASSERT_TRUE(tabs_list_with_permission); |
| 170 ASSERT_EQ(1u, tabs_list_with_permission->GetSize()); |
| 171 |
| 172 const base::DictionaryValue* third_tab_info; |
| 173 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(0, &third_tab_info)); |
| 174 int third_tab_id = -1; |
| 175 ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); |
| 176 EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contentses[2]), third_tab_id); |
| 177 } |
| 178 |
| 179 // Try the same without title, first and third tabs will match. |
| 180 const char* kURLQueryInfo = "[{\"url\": \"*://www.google.com/*\"}]"; |
| 181 { |
| 182 scoped_ptr<base::ListValue> tabs_list_with_permission(RunTabsQueryFunction( |
| 183 browser(), extension_with_permission.get(), kURLQueryInfo)); |
| 184 ASSERT_TRUE(tabs_list_with_permission); |
| 185 ASSERT_EQ(2u, tabs_list_with_permission->GetSize()); |
| 186 |
| 187 const base::DictionaryValue* first_tab_info; |
| 188 const base::DictionaryValue* third_tab_info; |
| 189 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(0, &first_tab_info)); |
| 190 ASSERT_TRUE(tabs_list_with_permission->GetDictionary(1, &third_tab_info)); |
| 191 |
| 192 std::vector<int> expected_tabs_ids; |
| 193 expected_tabs_ids.push_back(ExtensionTabUtil::GetTabId(web_contentses[0])); |
| 194 expected_tabs_ids.push_back(ExtensionTabUtil::GetTabId(web_contentses[2])); |
| 195 |
| 196 int first_tab_id = -1; |
| 197 ASSERT_TRUE(first_tab_info->GetInteger("id", &first_tab_id)); |
| 198 EXPECT_TRUE(ContainsValue(expected_tabs_ids, first_tab_id)); |
| 199 |
| 200 int third_tab_id = -1; |
| 201 ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); |
| 202 EXPECT_TRUE(ContainsValue(expected_tabs_ids, third_tab_id)); |
| 203 } |
| 204 } |
| 205 |
130 } // namespace extensions | 206 } // namespace extensions |
OLD | NEW |