1 // Copyright (c) 2011 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. 4 5 #include "base/utf_string_conversions.h" 6 #include "chrome/app/chrome_command_ids.h" 7 #include "chrome/browser/extensions/extension_browsertest.h" 8 #include "chrome/browser/extensions/extension_service.h" 9 #include "chrome/browser/extensions/extension_test_message_listener.h" 10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/tab_contents/render_view_context_menu.h" 12 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser_list.h" 14 #include "chrome/test/ui_test_utils.h" 15 #include "content/browser/tab_contents/tab_contents.h" 16 #include "net/base/mock_host_resolver.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" 18 #include "ui/base/models/menu_model.h" 19 #include "webkit/glue/context_menu.h" 20 21 using ui::MenuModel; 22 using WebKit::WebContextMenuData; 23 24 // This test class helps us sidestep platform-specific issues with popping up a 25 // real context menu, while still running through the actual code in 26 // RenderViewContextMenu where extension items get added and executed. 27 class TestRenderViewContextMenu : public RenderViewContextMenu { 28 public: 29 TestRenderViewContextMenu(TabContents* tab_contents, 30 const ContextMenuParams& params) 31 : RenderViewContextMenu(tab_contents, params) {} 32 33 virtual ~TestRenderViewContextMenu() {} 34 35 bool HasExtensionItemWithLabel(const std::string& label) { 36 string16 label16 = UTF8ToUTF16(label); 37 std::map<int, ExtensionMenuItem::Id>::iterator i; 38 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { 39 const ExtensionMenuItem::Id& id = i->second; 40 string16 tmp_label; 41 EXPECT_TRUE(GetItemLabel(id, &tmp_label)); 42 if (tmp_label == label16) 43 return true; 44 } 45 return false; 46 } 47 48 // Looks in the menu for an extension item with |id|, and if it is found and 49 // has a label, that is put in |result| and we return true. Otherwise returns 50 // false. 51 bool GetItemLabel(const ExtensionMenuItem::Id& id, string16* result) { 52 int command_id = 0; 53 if (!FindCommandId(id, &command_id)) 54 return false; 55 56 MenuModel* model = NULL; 57 int index = -1; 58 if (!GetMenuModelAndItemIndex(command_id, &model, &index)) { 59 return false; 60 } 61 *result = model->GetLabelAt(index); 62 return true; 63 } 64 65 // Searches for an menu item with |command_id|. If it's found, the return 66 // value is true and the model and index where it appears in that model are 67 // returned in |found_model| and |found_index|. Otherwise returns false. 68 bool GetMenuModelAndItemIndex(int command_id, 69 MenuModel** found_model, 70 int *found_index) { 71 std::vector<MenuModel*> models_to_search; 72 models_to_search.push_back(&menu_model_); 73 74 while (!models_to_search.empty()) { 75 MenuModel* model = models_to_search.back(); 76 models_to_search.pop_back(); 77 for (int i = 0; i < model->GetItemCount(); i++) { 78 if (model->GetCommandIdAt(i) == command_id) { 79 *found_model = model; 80 *found_index = i; 81 return true; 82 } else if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) { 83 models_to_search.push_back(model->GetSubmenuModelAt(i)); 84 } 85 } 86 } 87 88 return false; 89 } 90 91 protected: 92 // These two functions implement pure virtual methods of 93 // RenderViewContextMenu. 94 virtual bool GetAcceleratorForCommandId(int command_id, 95 ui::Accelerator* accelerator) { 96 // None of our commands have accelerators, so always return false. 97 return false; 98 } 99 virtual void PlatformInit() {} 100 101 102 // Given an extension menu item id, tries to find the corresponding command id 103 // in the menu. 104 bool FindCommandId(const ExtensionMenuItem::Id& id, int* command_id) { 105 std::map<int, ExtensionMenuItem::Id>::const_iterator i; 106 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { 107 if (i->second == id) { 108 *command_id = i->first; 109 return true; 110 } 111 } 112 return false; 113 } 114 }; 115 116 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest { 117 public: 118 // Helper to load an extension from context_menus/|subdirectory| in the 119 // extensions test data dir. 120 bool LoadContextMenuExtension(std::string subdirectory) { 121 FilePath extension_dir = 122 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory); 123 return LoadExtension(extension_dir); 124 } 125 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, 134 const GURL& link_url, 135 const GURL& frame_url) { 136 TabContents* tab_contents = browser->GetSelectedTabContents(); 137 WebContextMenuData data; 138 ContextMenuParams params(data); 139 params.page_url = page_url; 140 params.link_url = link_url; 141 params.frame_url = frame_url; 142 TestRenderViewContextMenu* menu = 143 new TestRenderViewContextMenu(tab_contents, params); 144 menu->Init(); 145 return menu; 146 } 147 148 // Shortcut to return the current ExtensionMenuManager. 149 ExtensionMenuManager* menu_manager() { 150 return browser()->profile()->GetExtensionService()->menu_manager(); 151 } 152 153 // Returns a pointer to the currently loaded extension with |name|, or null 154 // if not found. 155 const Extension* GetExtensionNamed(std::string name) { 156 const ExtensionList* extensions = 157 browser()->profile()->GetExtensionService()->extensions(); 158 ExtensionList::const_iterator i; 159 for (i = extensions->begin(); i != extensions->end(); ++i) { 160 if ((*i)->name() == name) { 161 return *i; 162 } 163 } 164 return NULL; 165 } 166 167 // This gets all the items that any extension has registered for possible 168 // inclusion in context menus. 169 ExtensionMenuItem::List GetItems() { 170 ExtensionMenuItem::List result; 171 std::set<std::string> extension_ids = menu_manager()->ExtensionIds(); 172 std::set<std::string>::iterator i; 173 for (i = extension_ids.begin(); i != extension_ids.end(); ++i) { 174 const ExtensionMenuItem::List* list = menu_manager()->MenuItems(*i); 175 result.insert(result.end(), list->begin(), list->end()); 176 } 177 return result; 178 } 179 180 // This creates a test menu for a page with |page_url| and |link_url|, looks 181 // for an extension item with the given |label|, and returns true if the item 182 // was found. 183 bool MenuHasItemWithLabel(const GURL& page_url, 184 const GURL& link_url, 185 const GURL& frame_url, 186 const std::string& label) { 187 scoped_ptr<TestRenderViewContextMenu> menu( 188 CreateMenu(browser(), page_url, link_url, frame_url)); 189 return menu->HasExtensionItemWithLabel(label); 190 } 191 }; 192 193 // Tests adding a simple context menu item. 194 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Simple) { 195 ExtensionTestMessageListener listener1("created item", false); 196 ExtensionTestMessageListener listener2("onclick fired", false); 197 ASSERT_TRUE(LoadContextMenuExtension("simple")); 198 199 // Wait for the extension to tell us it's created an item. 200 ASSERT_TRUE(listener1.WaitUntilSatisfied()); 201 202 GURL page_url("http://www.google.com"); 203 204 // Create and build our test context menu. 205 scoped_ptr<TestRenderViewContextMenu> menu( 206 CreateMenu(browser(), page_url, GURL(), GURL())); 207 208 // Look for the extension item in the menu, and execute it. 209 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; 210 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); 211 menu->ExecuteCommand(command_id); 212 213 // Wait for the extension's script to tell us its onclick fired. 214 ASSERT_TRUE(listener2.WaitUntilSatisfied()); 215 } 216 217 // Tests that setting "documentUrlPatterns" for an item properly restricts 218 // those items to matching pages. 219 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Patterns) { 220 ExtensionTestMessageListener listener("created items", false); 221 222 ASSERT_TRUE(LoadContextMenuExtension("patterns")); 223 224 // Wait for the js test code to create its two items with patterns. 225 ASSERT_TRUE(listener.WaitUntilSatisfied()); 226 227 // Check that a document url that should match the items' patterns appears. 228 GURL google_url("http://www.google.com"); 229 ASSERT_TRUE(MenuHasItemWithLabel(google_url, 230 GURL(), 231 GURL(), 232 std::string("test_item1"))); 233 ASSERT_TRUE(MenuHasItemWithLabel(google_url, 234 GURL(), 235 GURL(), 236 std::string("test_item2"))); 237 238 // Now check with a non-matching url. 239 GURL test_url("http://www.test.com"); 240 ASSERT_FALSE(MenuHasItemWithLabel(test_url, 241 GURL(), 242 GURL(), 243 std::string("test_item1"))); 244 ASSERT_FALSE(MenuHasItemWithLabel(test_url, 245 GURL(), 246 GURL(), 247 std::string("test_item2"))); 248 } 249 250 // Tests registering an item with a very long title that should get truncated in 251 // the actual menu displayed. 252 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, LongTitle) { 253 ExtensionTestMessageListener listener("created", false); 254 255 // Load the extension and wait until it's created a menu item. 256 ASSERT_TRUE(LoadContextMenuExtension("long_title")); 257 ASSERT_TRUE(listener.WaitUntilSatisfied()); 258 259 // Make sure we have an item registered with a long title. 260 size_t limit = RenderViewContextMenu::kMaxExtensionItemTitleLength; 261 ExtensionMenuItem::List items = GetItems(); 262 ASSERT_EQ(1u, items.size()); 263 ExtensionMenuItem* item = items.at(0); 264 ASSERT_GT(item->title().size(), limit); 265 266 // Create a context menu, then find the item's label. It should be properly 267 // truncated. 268 GURL url("http://foo.com/"); 269 scoped_ptr<TestRenderViewContextMenu> menu( 270 CreateMenu(browser(), url, GURL(), GURL())); 271 272 string16 label; 273 ASSERT_TRUE(menu->GetItemLabel(item->id(), &label)); 274 ASSERT_TRUE(label.size() <= limit); 275 } 276 277 // Checks that in |menu|, the item at |index| has type |expected_type| and a 278 // label of |expected_label|. 279 static void ExpectLabelAndType(const char* expected_label, 280 MenuModel::ItemType expected_type, 281 const MenuModel& menu, 282 int index) { 283 EXPECT_EQ(expected_type, menu.GetTypeAt(index)); 284 EXPECT_EQ(UTF8ToUTF16(expected_label), menu.GetLabelAt(index)); 285 } 286 287 // In the separators test we build a submenu with items and separators in two 288 // different ways - this is used to verify the results in both cases. 289 static void VerifyMenuForSeparatorsTest(const MenuModel& menu) { 290 // We expect to see the following items in the menu: 291 // radio1 292 // radio2 293 // --separator-- (automatically added) 294 // normal1 295 // --separator-- 296 // normal2 297 // --separator-- 298 // radio3 299 // radio4 300 // --separator-- 301 // normal3 302 303 int index = 0; 304 ASSERT_EQ(11, menu.GetItemCount()); 305 ExpectLabelAndType("radio1", MenuModel::TYPE_RADIO, menu, index++); 306 ExpectLabelAndType("radio2", MenuModel::TYPE_RADIO, menu, index++); 307 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++)); 308 ExpectLabelAndType("normal1", MenuModel::TYPE_COMMAND, menu, index++); 309 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++)); 310 ExpectLabelAndType("normal2", MenuModel::TYPE_COMMAND, menu, index++); 311 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++)); 312 ExpectLabelAndType("radio3", MenuModel::TYPE_RADIO, menu, index++); 313 ExpectLabelAndType("radio4", MenuModel::TYPE_RADIO, menu, index++); 314 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++)); 315 ExpectLabelAndType("normal3", MenuModel::TYPE_COMMAND, menu, index++); 316 } 317 318 // Tests a number of cases for auto-generated and explicitly added separators. 319 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Separators) { 320 // Load the extension. 321 ASSERT_TRUE(LoadContextMenuExtension("separators")); 322 const Extension* extension = GetExtensionNamed("Separators Test"); 323 ASSERT_TRUE(extension != NULL); 324 325 // Navigate to test1.html inside the extension, which should create a bunch 326 // of items at the top-level (but they'll get pushed into an auto-generated 327 // parent). 328 ExtensionTestMessageListener listener1("test1 create finished", false); 329 ui_test_utils::NavigateToURL(browser(), 330 GURL(extension->GetResourceURL("test1.html"))); 331 listener1.WaitUntilSatisfied(); 332 333 GURL url("http://www.google.com/"); 334 scoped_ptr<TestRenderViewContextMenu> menu( 335 CreateMenu(browser(), url, GURL(), GURL())); 336 337 // The top-level item should be an "automagic parent" with the extension's 338 // name. 339 MenuModel* model = NULL; 340 int index = 0; 341 string16 label; 342 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( 343 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); 344 EXPECT_EQ(UTF8ToUTF16(extension->name()), model->GetLabelAt(index)); 345 ASSERT_EQ(MenuModel::TYPE_SUBMENU, model->GetTypeAt(index)); 346 347 // Get the submenu and verify the items there. 348 MenuModel* submenu = model->GetSubmenuModelAt(index); 349 ASSERT_TRUE(submenu != NULL); 350 VerifyMenuForSeparatorsTest(*submenu); 351 352 // Now run our second test - navigate to test2.html which creates an explicit 353 // parent node and populates that with the same items as in test1. 354 ExtensionTestMessageListener listener2("test2 create finished", false); 355 ui_test_utils::NavigateToURL(browser(), 356 GURL(extension->GetResourceURL("test2.html"))); 357 listener2.WaitUntilSatisfied(); 358 menu.reset(CreateMenu(browser(), url, GURL(), GURL())); 359 ASSERT_TRUE(menu->GetMenuModelAndItemIndex( 360 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index)); 361 EXPECT_EQ(UTF8ToUTF16("parent"), model->GetLabelAt(index)); 362 submenu = model->GetSubmenuModelAt(index); 363 ASSERT_TRUE(submenu != NULL); 364 VerifyMenuForSeparatorsTest(*submenu); 365 } 366 367 // Tests that targetUrlPattern keeps items from appearing when there is no 368 // target url. 369 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, TargetURLs) { 370 ExtensionTestMessageListener listener("created items", false); 371 ASSERT_TRUE(LoadContextMenuExtension("target_urls")); 372 ASSERT_TRUE(listener.WaitUntilSatisfied()); 373 374 GURL google_url("http://www.google.com"); 375 GURL non_google_url("http://www.foo.com"); 376 377 // No target url - the item should not appear. 378 ASSERT_FALSE(MenuHasItemWithLabel( 379 google_url, GURL(), GURL(), std::string("item1"))); 380 381 // A matching target url - the item should appear. 382 ASSERT_TRUE(MenuHasItemWithLabel(google_url, 383 google_url, 384 GURL(), 385 std::string("item1"))); 386 387 // A non-matching target url - the item should not appear. 388 ASSERT_FALSE(MenuHasItemWithLabel(google_url, 389 non_google_url, 390 GURL(), 391 std::string("item1"))); 392 } 393 394 // Tests adding of context menus in incognito mode. 395 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, IncognitoSplit) { 396 ExtensionTestMessageListener created("created item regular", false); 397 ExtensionTestMessageListener created_incognito("created item incognito", 398 false); 399 400 ExtensionTestMessageListener onclick("onclick fired regular", false); 401 ExtensionTestMessageListener onclick_incognito("onclick fired incognito", 402 false); 403 404 // Open an incognito window. 405 ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank")); 406 407 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito")); 408 409 // Wait for the extension's processes to tell us they've created an item. 410 ASSERT_TRUE(created.WaitUntilSatisfied()); 411 ASSERT_TRUE(created_incognito.WaitUntilSatisfied()); 412 413 GURL page_url("http://www.google.com"); 414 415 // Create and build our test context menu. 416 Browser* browser_incognito = BrowserList::FindBrowserWithType( 417 browser()->profile()->GetOffTheRecordProfile(), 418 Browser::TYPE_NORMAL, false); 419 ASSERT_TRUE(browser_incognito); 420 scoped_ptr<TestRenderViewContextMenu> menu( 421 CreateMenu(browser(), page_url, GURL(), GURL())); 422 scoped_ptr<TestRenderViewContextMenu> menu_incognito( 423 CreateMenu(browser_incognito, page_url, GURL(), GURL())); 424 425 // Look for the extension item in the menu, and execute it. 426 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; 427 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id)); 428 menu->ExecuteCommand(command_id); 429 430 // Wait for the extension's script to tell us its onclick fired. Ensure 431 // that the incognito version doesn't fire until we explicitly click the 432 // incognito menu item. 433 ASSERT_TRUE(onclick.WaitUntilSatisfied()); 434 EXPECT_FALSE(onclick_incognito.was_satisfied()); 435 436 ASSERT_TRUE(menu_incognito->IsCommandIdEnabled(command_id)); 437 menu_incognito->ExecuteCommand(command_id); 438 ASSERT_TRUE(onclick_incognito.WaitUntilSatisfied()); 439 } 440 441 // Tests that items with a context of frames only appear when the menu is 442 // invoked in a frame. 443 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Frames) { 444 ExtensionTestMessageListener listener("created items", false); 445 ASSERT_TRUE(LoadContextMenuExtension("frames")); 446 ASSERT_TRUE(listener.WaitUntilSatisfied()); 447 448 GURL page_url("http://www.google.com"); 449 GURL no_frame_url; 450 GURL frame_url("http://www.google.com"); 451 452 ASSERT_TRUE(MenuHasItemWithLabel( 453 page_url, GURL(), no_frame_url, std::string("Page item"))); 454 ASSERT_FALSE(MenuHasItemWithLabel( 455 page_url, GURL(), no_frame_url, std::string("Frame item"))); 456 457 ASSERT_TRUE(MenuHasItemWithLabel( 458 page_url, GURL(), frame_url, std::string("Page item"))); 459 ASSERT_TRUE(MenuHasItemWithLabel( 460 page_url, GURL(), frame_url, std::string("Frame item"))); 461 } 462