| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/menus/menu_model.h" | 5 #include "app/menus/menu_model.h" |
| 6 #include "chrome/app/chrome_dll_resource.h" | 6 #include "chrome/app/chrome_dll_resource.h" |
| 7 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/browser_list.h" |
| 8 #include "chrome/browser/extensions/extension_browsertest.h" | 9 #include "chrome/browser/extensions/extension_browsertest.h" |
| 9 #include "chrome/browser/extensions/extension_test_message_listener.h" | 10 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 10 #include "chrome/browser/extensions/extensions_service.h" | 11 #include "chrome/browser/extensions/extensions_service.h" |
| 11 #include "chrome/browser/profile.h" | 12 #include "chrome/browser/profile.h" |
| 12 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 13 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 13 #include "chrome/browser/tab_contents/tab_contents.h" | 14 #include "chrome/browser/tab_contents/tab_contents.h" |
| 14 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/test/ui_test_utils.h" | 16 #include "chrome/test/ui_test_utils.h" |
| 16 #include "net/base/mock_host_resolver.h" | 17 #include "net/base/mock_host_resolver.h" |
| 17 #include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h" | 18 #include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest { | 116 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest { |
| 116 public: | 117 public: |
| 117 // Helper to load an extension from context_menus/|subdirectory| in the | 118 // Helper to load an extension from context_menus/|subdirectory| in the |
| 118 // extensions test data dir. | 119 // extensions test data dir. |
| 119 bool LoadContextMenuExtension(std::string subdirectory) { | 120 bool LoadContextMenuExtension(std::string subdirectory) { |
| 120 FilePath extension_dir = | 121 FilePath extension_dir = |
| 121 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); | 122 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); |
| 122 return LoadExtension(extension_dir); | 123 return LoadExtension(extension_dir); |
| 123 } | 124 } |
| 124 | 125 |
| 125 TestRenderViewContextMenu* CreateMenu(const GURL& page_url, | 126 bool LoadContextMenuExtensionIncognito(std::string subdirectory) { |
| 127 FilePath extension_dir = |
| 128 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); |
| 129 return LoadExtensionIncognito(extension_dir); |
| 130 } |
| 131 |
| 132 TestRenderViewContextMenu* CreateMenu(Browser* browser, |
| 133 const GURL& page_url, |
| 126 const GURL& link_url) { | 134 const GURL& link_url) { |
| 127 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 135 TabContents* tab_contents = browser->GetSelectedTabContents(); |
| 128 WebContextMenuData data; | 136 WebContextMenuData data; |
| 129 ContextMenuParams params(data); | 137 ContextMenuParams params(data); |
| 130 params.page_url = page_url; | 138 params.page_url = page_url; |
| 131 params.link_url = link_url; | 139 params.link_url = link_url; |
| 132 TestRenderViewContextMenu* menu = | 140 TestRenderViewContextMenu* menu = |
| 133 new TestRenderViewContextMenu(tab_contents, params); | 141 new TestRenderViewContextMenu(tab_contents, params); |
| 134 menu->Init(); | 142 menu->Init(); |
| 135 return menu; | 143 return menu; |
| 136 } | 144 } |
| 137 | 145 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 166 } | 174 } |
| 167 return result; | 175 return result; |
| 168 } | 176 } |
| 169 | 177 |
| 170 // This creates a test menu for a page with |page_url| and |link_url|, looks | 178 // This creates a test menu for a page with |page_url| and |link_url|, looks |
| 171 // for an extension item with the given |label|, and returns true if the item | 179 // for an extension item with the given |label|, and returns true if the item |
| 172 // was found. | 180 // was found. |
| 173 bool MenuHasItemWithLabel(const GURL& page_url, | 181 bool MenuHasItemWithLabel(const GURL& page_url, |
| 174 const GURL& link_url, | 182 const GURL& link_url, |
| 175 const std::string& label) { | 183 const std::string& label) { |
| 176 scoped_ptr<TestRenderViewContextMenu> menu(CreateMenu(page_url, link_url)); | 184 scoped_ptr<TestRenderViewContextMenu> menu( |
| 185 CreateMenu(browser(), page_url, link_url)); |
| 177 return menu->HasExtensionItemWithLabel(label); | 186 return menu->HasExtensionItemWithLabel(label); |
| 178 } | 187 } |
| 179 }; | 188 }; |
| 180 | 189 |
| 181 // Tests adding a simple context menu item. | 190 // Tests adding a simple context menu item. |
| 182 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Simple) { | 191 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Simple) { |
| 183 ExtensionTestMessageListener listener1("created item", false); | 192 ExtensionTestMessageListener listener1("created item", false); |
| 184 ExtensionTestMessageListener listener2("onclick fired", false); | 193 ExtensionTestMessageListener listener2("onclick fired", false); |
| 185 ASSERT_TRUE(LoadContextMenuExtension("simple")); | 194 ASSERT_TRUE(LoadContextMenuExtension("simple")); |
| 186 | 195 |
| 187 // Wait for the extension to tell us it's created an item. | 196 // Wait for the extension to tell us it's created an item. |
| 188 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | 197 ASSERT_TRUE(listener1.WaitUntilSatisfied()); |
| 189 | 198 |
| 190 GURL page_url("http://www.google.com"); | 199 GURL page_url("http://www.google.com"); |
| 191 | 200 |
| 192 // Create and build our test context menu. | 201 // Create and build our test context menu. |
| 193 scoped_ptr<TestRenderViewContextMenu> menu(CreateMenu(page_url, GURL())); | 202 scoped_ptr<TestRenderViewContextMenu> menu( |
| 203 CreateMenu(browser(), page_url, GURL())); |
| 194 | 204 |
| 195 // Look for the extension item in the menu, and execute it. | 205 // Look for the extension item in the menu, and execute it. |
| 196 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 206 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
| 197 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); | 207 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); |
| 198 menu->ExecuteCommand(command_id); | 208 menu->ExecuteCommand(command_id); |
| 199 | 209 |
| 200 // Wait for the extension's script to tell us its onclick fired. | 210 // Wait for the extension's script to tell us its onclick fired. |
| 201 ASSERT_TRUE(listener2.WaitUntilSatisfied()); | 211 ASSERT_TRUE(listener2.WaitUntilSatisfied()); |
| 202 } | 212 } |
| 203 | 213 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 // Make sure we have an item registered with a long title. | 252 // Make sure we have an item registered with a long title. |
| 243 size_t limit = RenderViewContextMenu::kMaxExtensionItemTitleLength; | 253 size_t limit = RenderViewContextMenu::kMaxExtensionItemTitleLength; |
| 244 ExtensionMenuItem::List items = GetItems(); | 254 ExtensionMenuItem::List items = GetItems(); |
| 245 ASSERT_EQ(1u, items.size()); | 255 ASSERT_EQ(1u, items.size()); |
| 246 ExtensionMenuItem* item = items.at(0); | 256 ExtensionMenuItem* item = items.at(0); |
| 247 ASSERT_GT(item->title().size(), limit); | 257 ASSERT_GT(item->title().size(), limit); |
| 248 | 258 |
| 249 // Create a context menu, then find the item's label. It should be properly | 259 // Create a context menu, then find the item's label. It should be properly |
| 250 // truncated. | 260 // truncated. |
| 251 GURL url("http://foo.com/"); | 261 GURL url("http://foo.com/"); |
| 252 scoped_ptr<TestRenderViewContextMenu> menu(CreateMenu(url, GURL())); | 262 scoped_ptr<TestRenderViewContextMenu> menu( |
| 263 CreateMenu(browser(), url, GURL())); |
| 253 | 264 |
| 254 string16 label; | 265 string16 label; |
| 255 ASSERT_TRUE(menu->GetItemLabel(item->id(), &label)); | 266 ASSERT_TRUE(menu->GetItemLabel(item->id(), &label)); |
| 256 ASSERT_TRUE(label.size() <= limit); | 267 ASSERT_TRUE(label.size() <= limit); |
| 257 } | 268 } |
| 258 | 269 |
| 259 // Checks that in |menu|, the item at |index| has type |expected_type| and a | 270 // Checks that in |menu|, the item at |index| has type |expected_type| and a |
| 260 // label of |expected_label|. | 271 // label of |expected_label|. |
| 261 static void ExpectLabelAndType(const char* expected_label, | 272 static void ExpectLabelAndType(const char* expected_label, |
| 262 MenuModel::ItemType expected_type, | 273 MenuModel::ItemType expected_type, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 | 317 |
| 307 // Navigate to test1.html inside the extension, which should create a bunch | 318 // Navigate to test1.html inside the extension, which should create a bunch |
| 308 // of items at the top-level (but they'll get pushed into an auto-generated | 319 // of items at the top-level (but they'll get pushed into an auto-generated |
| 309 // parent). | 320 // parent). |
| 310 ExtensionTestMessageListener listener1("test1 create finished", false); | 321 ExtensionTestMessageListener listener1("test1 create finished", false); |
| 311 ui_test_utils::NavigateToURL(browser(), | 322 ui_test_utils::NavigateToURL(browser(), |
| 312 GURL(extension->GetResourceURL("test1.html"))); | 323 GURL(extension->GetResourceURL("test1.html"))); |
| 313 listener1.WaitUntilSatisfied(); | 324 listener1.WaitUntilSatisfied(); |
| 314 | 325 |
| 315 GURL url("http://www.google.com/"); | 326 GURL url("http://www.google.com/"); |
| 316 scoped_ptr<TestRenderViewContextMenu> menu(CreateMenu(url, GURL())); | 327 scoped_ptr<TestRenderViewContextMenu> menu( |
| 328 CreateMenu(browser(), url, GURL())); |
| 317 | 329 |
| 318 // The top-level item should be an "automagic parent" with the extension's | 330 // The top-level item should be an "automagic parent" with the extension's |
| 319 // name. | 331 // name. |
| 320 MenuModel* model = NULL; | 332 MenuModel* model = NULL; |
| 321 int index = 0; | 333 int index = 0; |
| 322 string16 label; | 334 string16 label; |
| 323 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( | 335 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( |
| 324 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); | 336 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); |
| 325 EXPECT_EQ(UTF8ToUTF16(extension->name()), model->GetLabelAt(index)); | 337 EXPECT_EQ(UTF8ToUTF16(extension->name()), model->GetLabelAt(index)); |
| 326 ASSERT_EQ(MenuModel::TYPE_SUBMENU, model->GetTypeAt(index)); | 338 ASSERT_EQ(MenuModel::TYPE_SUBMENU, model->GetTypeAt(index)); |
| 327 | 339 |
| 328 // Get the submenu and verify the items there. | 340 // Get the submenu and verify the items there. |
| 329 MenuModel* submenu = model->GetSubmenuModelAt(index); | 341 MenuModel* submenu = model->GetSubmenuModelAt(index); |
| 330 ASSERT_TRUE(submenu != NULL); | 342 ASSERT_TRUE(submenu != NULL); |
| 331 VerifyMenuForSeparatorsTest(*submenu); | 343 VerifyMenuForSeparatorsTest(*submenu); |
| 332 | 344 |
| 333 // Now run our second test - navigate to test2.html which creates an explicit | 345 // Now run our second test - navigate to test2.html which creates an explicit |
| 334 // parent node and populates that with the same items as in test1. | 346 // parent node and populates that with the same items as in test1. |
| 335 ExtensionTestMessageListener listener2("test2 create finished", false); | 347 ExtensionTestMessageListener listener2("test2 create finished", false); |
| 336 ui_test_utils::NavigateToURL(browser(), | 348 ui_test_utils::NavigateToURL(browser(), |
| 337 GURL(extension->GetResourceURL("test2.html"))); | 349 GURL(extension->GetResourceURL("test2.html"))); |
| 338 listener2.WaitUntilSatisfied(); | 350 listener2.WaitUntilSatisfied(); |
| 339 menu.reset(CreateMenu(url, GURL())); | 351 menu.reset(CreateMenu(browser(), url, GURL())); |
| 340 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( | 352 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( |
| 341 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); | 353 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); |
| 342 EXPECT_EQ(UTF8ToUTF16("parent"), model->GetLabelAt(index)); | 354 EXPECT_EQ(UTF8ToUTF16("parent"), model->GetLabelAt(index)); |
| 343 submenu = model->GetSubmenuModelAt(index); | 355 submenu = model->GetSubmenuModelAt(index); |
| 344 ASSERT_TRUE(submenu != NULL); | 356 ASSERT_TRUE(submenu != NULL); |
| 345 VerifyMenuForSeparatorsTest(*submenu); | 357 VerifyMenuForSeparatorsTest(*submenu); |
| 346 } | 358 } |
| 347 | 359 |
| 348 // Tests that targetUrlPattern keeps items from appearing when there is no | 360 // Tests that targetUrlPattern keeps items from appearing when there is no |
| 349 // target url. | 361 // target url. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 361 // A matching target url - the item should appear. | 373 // A matching target url - the item should appear. |
| 362 ASSERT_TRUE(MenuHasItemWithLabel(google_url, | 374 ASSERT_TRUE(MenuHasItemWithLabel(google_url, |
| 363 google_url, | 375 google_url, |
| 364 std::string("item1"))); | 376 std::string("item1"))); |
| 365 | 377 |
| 366 // A non-matching target url - the item should not appear. | 378 // A non-matching target url - the item should not appear. |
| 367 ASSERT_FALSE(MenuHasItemWithLabel(google_url, | 379 ASSERT_FALSE(MenuHasItemWithLabel(google_url, |
| 368 non_google_url, | 380 non_google_url, |
| 369 std::string("item1"))); | 381 std::string("item1"))); |
| 370 } | 382 } |
| 383 |
| 384 // Tests adding a simple context menu item. |
| 385 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, IncognitoSplit) { |
| 386 ExtensionTestMessageListener created("created item regular", false); |
| 387 ExtensionTestMessageListener created_incognito("created item incognito", |
| 388 false); |
| 389 |
| 390 ExtensionTestMessageListener onclick("onclick fired regular", false); |
| 391 ExtensionTestMessageListener onclick_incognito("onclick fired incognito", |
| 392 false); |
| 393 |
| 394 // Open an incognito window. |
| 395 ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank")); |
| 396 |
| 397 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); |
| 398 |
| 399 // Wait for the extension's processes to tell us they've created an item. |
| 400 ASSERT_TRUE(created.WaitUntilSatisfied()); |
| 401 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); |
| 402 |
| 403 GURL page_url("http://www.google.com"); |
| 404 |
| 405 // Create and build our test context menu. |
| 406 Browser* browser_incognito = BrowserList::FindBrowserWithType( |
| 407 browser()->profile()->GetOffTheRecordProfile(), |
| 408 Browser::TYPE_NORMAL, false); |
| 409 ASSERT_TRUE(browser_incognito); |
| 410 scoped_ptr<TestRenderViewContextMenu> menu( |
| 411 CreateMenu(browser(), page_url, GURL())); |
| 412 scoped_ptr<TestRenderViewContextMenu> menu_incognito( |
| 413 CreateMenu(browser_incognito, page_url, GURL())); |
| 414 |
| 415 // Look for the extension item in the menu, and execute it. |
| 416 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
| 417 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); |
| 418 menu->ExecuteCommand(command_id); |
| 419 |
| 420 // Wait for the extension's script to tell us its onclick fired. Ensure |
| 421 // that the incognito version doesn't fire until we explicitly click the |
| 422 // incognito menu item. |
| 423 ASSERT_TRUE(onclick.WaitUntilSatisfied()); |
| 424 EXPECT_FALSE(onclick_incognito.was_satisfied()); |
| 425 |
| 426 ASSERT_TRUE(menu_incognito->IsCommandIdEnabled(command_id)); |
| 427 menu_incognito->ExecuteCommand(command_id); |
| 428 ASSERT_TRUE(onclick_incognito.WaitUntilSatisfied()); |
| 429 } |
| OLD | NEW |