Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_context_menu_browsertest.cc
blob2585602377bac72372cbf28f9688f1640cc6b041
1 // Copyright (c) 2012 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.
5 #include "base/strings/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_system.h"
10 #include "chrome/browser/extensions/extension_test_message_listener.h"
11 #include "chrome/browser/extensions/lazy_background_page_test_util.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/tab_contents/render_view_context_menu.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/common/context_menu_params.h"
18 #include "extensions/browser/test_management_policy.h"
19 #include "extensions/common/extension_set.h"
20 #include "extensions/common/switches.h"
21 #include "net/dns/mock_host_resolver.h"
22 #include "ui/base/models/menu_model.h"
24 using content::WebContents;
25 using extensions::MenuItem;
26 using ui::MenuModel;
28 namespace {
29 // This test class helps us sidestep platform-specific issues with popping up a
30 // real context menu, while still running through the actual code in
31 // RenderViewContextMenu where extension items get added and executed.
32 class TestRenderViewContextMenu : public RenderViewContextMenu {
33 public:
34 TestRenderViewContextMenu(WebContents* web_contents,
35 const content::ContextMenuParams& params)
36 : RenderViewContextMenu(web_contents, params) {}
38 virtual ~TestRenderViewContextMenu() {}
40 // Searches for an menu item with |command_id|. If it's found, the return
41 // value is true and the model and index where it appears in that model are
42 // returned in |found_model| and |found_index|. Otherwise returns false.
43 bool GetMenuModelAndItemIndex(int command_id,
44 MenuModel** found_model,
45 int *found_index) {
46 std::vector<MenuModel*> models_to_search;
47 models_to_search.push_back(&menu_model_);
49 while (!models_to_search.empty()) {
50 MenuModel* model = models_to_search.back();
51 models_to_search.pop_back();
52 for (int i = 0; i < model->GetItemCount(); i++) {
53 if (model->GetCommandIdAt(i) == command_id) {
54 *found_model = model;
55 *found_index = i;
56 return true;
57 } else if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) {
58 models_to_search.push_back(model->GetSubmenuModelAt(i));
63 return false;
66 extensions::ContextMenuMatcher& extension_items() {
67 return extension_items_;
70 protected:
71 // These two functions implement pure virtual methods of
72 // RenderViewContextMenu.
73 virtual bool GetAcceleratorForCommandId(
74 int command_id,
75 ui::Accelerator* accelerator) OVERRIDE {
76 // None of our commands have accelerators, so always return false.
77 return false;
79 virtual void PlatformInit() OVERRIDE {}
80 virtual void PlatformCancel() OVERRIDE {}
83 } // namespace
85 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest {
86 public:
87 // Helper to load an extension from context_menus/|subdirectory| in the
88 // extensions test data dir.
89 const extensions::Extension* LoadContextMenuExtension(
90 std::string subdirectory) {
91 base::FilePath extension_dir =
92 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory);
93 return LoadExtension(extension_dir);
96 // Helper to load an extension from context_menus/top_level/|subdirectory| in
97 // the extensions test data dir.
98 const extensions::Extension* LoadTopLevelContextMenuExtension(
99 std::string subdirectory) {
100 base::FilePath extension_dir =
101 test_data_dir_.AppendASCII("context_menus").AppendASCII("top_level");
102 extension_dir = extension_dir.AppendASCII(subdirectory);
103 return LoadExtension(extension_dir);
106 const extensions::Extension* LoadContextMenuExtensionIncognito(
107 std::string subdirectory) {
108 base::FilePath extension_dir =
109 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory);
110 return LoadExtensionIncognito(extension_dir);
113 TestRenderViewContextMenu* CreateMenu(Browser* browser,
114 const GURL& page_url,
115 const GURL& link_url,
116 const GURL& frame_url) {
117 WebContents* web_contents =
118 browser->tab_strip_model()->GetActiveWebContents();
119 content::ContextMenuParams params;
120 params.page_url = page_url;
121 params.link_url = link_url;
122 params.frame_url = frame_url;
123 TestRenderViewContextMenu* menu =
124 new TestRenderViewContextMenu(web_contents, params);
125 menu->Init();
126 return menu;
129 // Shortcut to return the current MenuManager.
130 extensions::MenuManager* menu_manager() {
131 return extensions::MenuManager::Get(browser()->profile());
134 // Returns a pointer to the currently loaded extension with |name|, or null
135 // if not found.
136 const extensions::Extension* GetExtensionNamed(std::string name) {
137 const extensions::ExtensionSet* extensions =
138 browser()->profile()->GetExtensionService()->extensions();
139 for (extensions::ExtensionSet::const_iterator i = extensions->begin();
140 i != extensions->end(); ++i) {
141 if ((*i)->name() == name) {
142 return i->get();
145 return NULL;
148 // This gets all the items that any extension has registered for possible
149 // inclusion in context menus.
150 MenuItem::List GetItems() {
151 MenuItem::List result;
152 std::set<std::string> extension_ids = menu_manager()->ExtensionIds();
153 std::set<std::string>::iterator i;
154 for (i = extension_ids.begin(); i != extension_ids.end(); ++i) {
155 const MenuItem::List* list = menu_manager()->MenuItems(*i);
156 result.insert(result.end(), list->begin(), list->end());
158 return result;
161 // This creates a test menu for a page with |page_url| and |link_url|, looks
162 // for an extension item with the given |label|, and returns true if the item
163 // was found.
164 bool MenuHasItemWithLabel(const GURL& page_url,
165 const GURL& link_url,
166 const GURL& frame_url,
167 const std::string& label) {
168 scoped_ptr<TestRenderViewContextMenu> menu(
169 CreateMenu(browser(), page_url, link_url, frame_url));
170 return MenuHasExtensionItemWithLabel(menu.get(), label);
173 // This creates an extension that starts |enabled| and then switches to
174 // |!enabled|.
175 void TestEnabledContextMenu(bool enabled) {
176 ExtensionTestMessageListener begin("begin", true);
177 ExtensionTestMessageListener create("create", true);
178 ExtensionTestMessageListener update("update", false);
179 ASSERT_TRUE(LoadContextMenuExtension("enabled"));
181 ASSERT_TRUE(begin.WaitUntilSatisfied());
183 if (enabled)
184 begin.Reply("start enabled");
185 else
186 begin.Reply("start disabled");
188 // Wait for the extension to tell us it's created an item.
189 ASSERT_TRUE(create.WaitUntilSatisfied());
190 create.Reply("go");
192 GURL page_url("http://www.google.com");
194 // Create and build our test context menu.
195 scoped_ptr<TestRenderViewContextMenu> menu(
196 CreateMenu(browser(), page_url, GURL(), GURL()));
198 // Look for the extension item in the menu, and make sure it's |enabled|.
199 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
200 ASSERT_EQ(enabled, menu->IsCommandIdEnabled(command_id));
202 // Update the item and make sure it is now |!enabled|.
203 ASSERT_TRUE(update.WaitUntilSatisfied());
204 ASSERT_EQ(!enabled, menu->IsCommandIdEnabled(command_id));
207 bool MenuHasExtensionItemWithLabel(TestRenderViewContextMenu* menu,
208 const std::string& label) {
209 base::string16 label16 = base::UTF8ToUTF16(label);
210 std::map<int, MenuItem::Id>::iterator i;
211 for (i = menu->extension_items().extension_item_map_.begin();
212 i != menu->extension_items().extension_item_map_.end(); ++i) {
213 const MenuItem::Id& id = i->second;
214 base::string16 tmp_label;
215 EXPECT_TRUE(GetItemLabel(menu, id, &tmp_label));
216 if (tmp_label == label16)
217 return true;
219 return false;
222 // Looks in the menu for an extension item with |id|, and if it is found and
223 // has a label, that is put in |result| and we return true. Otherwise returns
224 // false.
225 bool GetItemLabel(TestRenderViewContextMenu* menu,
226 const MenuItem::Id& id,
227 base::string16* result) {
228 int command_id = 0;
229 if (!FindCommandId(menu, id, &command_id))
230 return false;
232 MenuModel* model = NULL;
233 int index = -1;
234 if (!menu->GetMenuModelAndItemIndex(command_id, &model, &index)) {
235 return false;
237 *result = model->GetLabelAt(index);
238 return true;
241 // Given an extension menu item id, tries to find the corresponding command id
242 // in the menu.
243 bool FindCommandId(TestRenderViewContextMenu* menu,
244 const MenuItem::Id& id,
245 int* command_id) {
246 std::map<int, MenuItem::Id>::const_iterator i;
247 for (i = menu->extension_items().extension_item_map_.begin();
248 i != menu->extension_items().extension_item_map_.end(); ++i) {
249 if (i->second == id) {
250 *command_id = i->first;
251 return true;
254 return false;
258 // Tests adding a simple context menu item.
259 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Simple) {
260 ExtensionTestMessageListener listener1("created item", false);
261 ExtensionTestMessageListener listener2("onclick fired", false);
262 ASSERT_TRUE(LoadContextMenuExtension("simple"));
264 // Wait for the extension to tell us it's created an item.
265 ASSERT_TRUE(listener1.WaitUntilSatisfied());
267 GURL page_url("http://www.google.com");
269 // Create and build our test context menu.
270 scoped_ptr<TestRenderViewContextMenu> menu(
271 CreateMenu(browser(), page_url, GURL(), GURL()));
273 // Look for the extension item in the menu, and execute it.
274 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
275 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id));
276 menu->ExecuteCommand(command_id, 0);
278 // Wait for the extension's script to tell us its onclick fired.
279 ASSERT_TRUE(listener2.WaitUntilSatisfied());
282 // Tests that setting "documentUrlPatterns" for an item properly restricts
283 // those items to matching pages.
284 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Patterns) {
285 ExtensionTestMessageListener listener("created items", false);
287 ASSERT_TRUE(LoadContextMenuExtension("patterns"));
289 // Wait for the js test code to create its two items with patterns.
290 ASSERT_TRUE(listener.WaitUntilSatisfied());
292 // Check that a document url that should match the items' patterns appears.
293 GURL google_url("http://www.google.com");
294 ASSERT_TRUE(MenuHasItemWithLabel(google_url,
295 GURL(),
296 GURL(),
297 std::string("test_item1")));
298 ASSERT_TRUE(MenuHasItemWithLabel(google_url,
299 GURL(),
300 GURL(),
301 std::string("test_item2")));
303 // Now check with a non-matching url.
304 GURL test_url("http://www.test.com");
305 ASSERT_FALSE(MenuHasItemWithLabel(test_url,
306 GURL(),
307 GURL(),
308 std::string("test_item1")));
309 ASSERT_FALSE(MenuHasItemWithLabel(test_url,
310 GURL(),
311 GURL(),
312 std::string("test_item2")));
315 // Tests registering an item with a very long title that should get truncated in
316 // the actual menu displayed.
317 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, LongTitle) {
318 ExtensionTestMessageListener listener("created", false);
320 // Load the extension and wait until it's created a menu item.
321 ASSERT_TRUE(LoadContextMenuExtension("long_title"));
322 ASSERT_TRUE(listener.WaitUntilSatisfied());
324 // Make sure we have an item registered with a long title.
325 size_t limit = extensions::ContextMenuMatcher::kMaxExtensionItemTitleLength;
326 MenuItem::List items = GetItems();
327 ASSERT_EQ(1u, items.size());
328 MenuItem* item = items.at(0);
329 ASSERT_GT(item->title().size(), limit);
331 // Create a context menu, then find the item's label. It should be properly
332 // truncated.
333 GURL url("http://foo.com/");
334 scoped_ptr<TestRenderViewContextMenu> menu(
335 CreateMenu(browser(), url, GURL(), GURL()));
337 base::string16 label;
338 ASSERT_TRUE(GetItemLabel(menu.get(), item->id(), &label));
339 ASSERT_TRUE(label.size() <= limit);
342 // Flaky on Windows debug bots. http://crbug.com/251590
343 #if defined(OS_WIN)
344 #define MAYBE_TopLevel DISABLED_TopLevel
345 #else
346 #define MAYBE_TopLevel TopLevel
347 #endif
348 // Checks that Context Menus are ordered alphabetically by their name when
349 // extensions have only one single Context Menu item and by the extension name
350 // when multiples Context Menu items are created.
351 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, MAYBE_TopLevel) {
352 // We expect to see the following items in the menu:
353 // An Extension with multiple Context Menus
354 // Context Menu #1
355 // Context Menu #2
356 // Context Menu #1 - Extension #2
357 // Context Menu #2 - Extension #3
358 // Context Menu #3 - Extension #1
359 // Ze Extension with multiple Context Menus
360 // Context Menu #1
361 // Context Menu #2
363 // Load extensions and wait until it's created a single menu item.
364 ExtensionTestMessageListener listener1("created item", false);
365 ASSERT_TRUE(LoadTopLevelContextMenuExtension("single1"));
366 ASSERT_TRUE(listener1.WaitUntilSatisfied());
368 ExtensionTestMessageListener listener2("created item", false);
369 ASSERT_TRUE(LoadTopLevelContextMenuExtension("single2"));
370 ASSERT_TRUE(listener2.WaitUntilSatisfied());
372 ExtensionTestMessageListener listener3("created item", false);
373 ASSERT_TRUE(LoadTopLevelContextMenuExtension("single3"));
374 ASSERT_TRUE(listener3.WaitUntilSatisfied());
376 // Load extensions and wait until it's created two menu items.
377 ExtensionTestMessageListener listener4("created items", false);
378 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi4"));
379 ASSERT_TRUE(listener4.WaitUntilSatisfied());
381 ExtensionTestMessageListener listener5("created items", false);
382 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi5"));
383 ASSERT_TRUE(listener5.WaitUntilSatisfied());
385 GURL url("http://foo.com/");
386 scoped_ptr<TestRenderViewContextMenu> menu(
387 CreateMenu(browser(), url, GURL(), GURL()));
389 int index = 0;
390 MenuModel* model = NULL;
392 ASSERT_TRUE(menu->GetMenuModelAndItemIndex(
393 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index));
394 EXPECT_EQ(base::UTF8ToUTF16("An Extension with multiple Context Menus"),
395 model->GetLabelAt(index++));
396 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #1 - Extension #2"),
397 model->GetLabelAt(index++));
398 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #2 - Extension #3"),
399 model->GetLabelAt(index++));
400 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #3 - Extension #1"),
401 model->GetLabelAt(index++));
402 EXPECT_EQ(base::UTF8ToUTF16("Ze Extension with multiple Context Menus"),
403 model->GetLabelAt(index++));
406 // Checks that in |menu|, the item at |index| has type |expected_type| and a
407 // label of |expected_label|.
408 static void ExpectLabelAndType(const char* expected_label,
409 MenuModel::ItemType expected_type,
410 const MenuModel& menu,
411 int index) {
412 EXPECT_EQ(expected_type, menu.GetTypeAt(index));
413 EXPECT_EQ(base::UTF8ToUTF16(expected_label), menu.GetLabelAt(index));
416 // In the separators test we build a submenu with items and separators in two
417 // different ways - this is used to verify the results in both cases.
418 static void VerifyMenuForSeparatorsTest(const MenuModel& menu) {
419 // We expect to see the following items in the menu:
420 // radio1
421 // radio2
422 // --separator-- (automatically added)
423 // normal1
424 // --separator--
425 // normal2
426 // --separator--
427 // radio3
428 // radio4
429 // --separator--
430 // normal3
432 int index = 0;
433 ASSERT_EQ(11, menu.GetItemCount());
434 ExpectLabelAndType("radio1", MenuModel::TYPE_RADIO, menu, index++);
435 ExpectLabelAndType("radio2", MenuModel::TYPE_RADIO, menu, index++);
436 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
437 ExpectLabelAndType("normal1", MenuModel::TYPE_COMMAND, menu, index++);
438 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
439 ExpectLabelAndType("normal2", MenuModel::TYPE_COMMAND, menu, index++);
440 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
441 ExpectLabelAndType("radio3", MenuModel::TYPE_RADIO, menu, index++);
442 ExpectLabelAndType("radio4", MenuModel::TYPE_RADIO, menu, index++);
443 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
444 ExpectLabelAndType("normal3", MenuModel::TYPE_COMMAND, menu, index++);
447 #if defined(OS_WIN)
448 #define MAYBE_Separators DISABLED_Separators
449 #else
450 #define MAYBE_Separators Separators
451 #endif
453 // Tests a number of cases for auto-generated and explicitly added separators.
454 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Separators) {
455 // Load the extension.
456 ASSERT_TRUE(LoadContextMenuExtension("separators"));
457 const extensions::Extension* extension = GetExtensionNamed("Separators Test");
458 ASSERT_TRUE(extension != NULL);
460 // Navigate to test1.html inside the extension, which should create a bunch
461 // of items at the top-level (but they'll get pushed into an auto-generated
462 // parent).
463 ExtensionTestMessageListener listener1("test1 create finished", false);
464 ui_test_utils::NavigateToURL(browser(),
465 GURL(extension->GetResourceURL("test1.html")));
466 listener1.WaitUntilSatisfied();
468 GURL url("http://www.google.com/");
469 scoped_ptr<TestRenderViewContextMenu> menu(
470 CreateMenu(browser(), url, GURL(), GURL()));
472 // The top-level item should be an "automagic parent" with the extension's
473 // name.
474 MenuModel* model = NULL;
475 int index = 0;
476 base::string16 label;
477 ASSERT_TRUE(menu->GetMenuModelAndItemIndex(
478 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index));
479 EXPECT_EQ(base::UTF8ToUTF16(extension->name()), model->GetLabelAt(index));
480 ASSERT_EQ(MenuModel::TYPE_SUBMENU, model->GetTypeAt(index));
482 // Get the submenu and verify the items there.
483 MenuModel* submenu = model->GetSubmenuModelAt(index);
484 ASSERT_TRUE(submenu != NULL);
485 VerifyMenuForSeparatorsTest(*submenu);
487 // Now run our second test - navigate to test2.html which creates an explicit
488 // parent node and populates that with the same items as in test1.
489 ExtensionTestMessageListener listener2("test2 create finished", false);
490 ui_test_utils::NavigateToURL(browser(),
491 GURL(extension->GetResourceURL("test2.html")));
492 listener2.WaitUntilSatisfied();
493 menu.reset(CreateMenu(browser(), url, GURL(), GURL()));
494 ASSERT_TRUE(menu->GetMenuModelAndItemIndex(
495 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index));
496 EXPECT_EQ(base::UTF8ToUTF16("parent"), model->GetLabelAt(index));
497 submenu = model->GetSubmenuModelAt(index);
498 ASSERT_TRUE(submenu != NULL);
499 VerifyMenuForSeparatorsTest(*submenu);
502 // Tests that targetUrlPattern keeps items from appearing when there is no
503 // target url.
504 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, TargetURLs) {
505 ExtensionTestMessageListener listener("created items", false);
506 ASSERT_TRUE(LoadContextMenuExtension("target_urls"));
507 ASSERT_TRUE(listener.WaitUntilSatisfied());
509 GURL google_url("http://www.google.com");
510 GURL non_google_url("http://www.foo.com");
512 // No target url - the item should not appear.
513 ASSERT_FALSE(MenuHasItemWithLabel(
514 google_url, GURL(), GURL(), std::string("item1")));
516 // A matching target url - the item should appear.
517 ASSERT_TRUE(MenuHasItemWithLabel(google_url,
518 google_url,
519 GURL(),
520 std::string("item1")));
522 // A non-matching target url - the item should not appear.
523 ASSERT_FALSE(MenuHasItemWithLabel(google_url,
524 non_google_url,
525 GURL(),
526 std::string("item1")));
529 // Tests adding of context menus in incognito mode.
530 #if defined(OS_LINUX)
531 // Flakily hangs on Linux/CrOS - http://crbug.com/88317
532 #define MAYBE_IncognitoSplit DISABLED_IncognitoSplit
533 #else
534 #define MAYBE_IncognitoSplit IncognitoSplit
535 #endif
536 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, MAYBE_IncognitoSplit) {
537 ExtensionTestMessageListener created("created item regular", false);
538 ExtensionTestMessageListener created_incognito("created item incognito",
539 false);
541 ExtensionTestMessageListener onclick("onclick fired regular", false);
542 ExtensionTestMessageListener onclick_incognito("onclick fired incognito",
543 false);
545 // Open an incognito window.
546 Browser* browser_incognito = ui_test_utils::OpenURLOffTheRecord(
547 browser()->profile(), GURL("about:blank"));
549 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito"));
551 // Wait for the extension's processes to tell us they've created an item.
552 ASSERT_TRUE(created.WaitUntilSatisfied());
553 ASSERT_TRUE(created_incognito.WaitUntilSatisfied());
555 GURL page_url("http://www.google.com");
557 // Create and build our test context menu.
558 scoped_ptr<TestRenderViewContextMenu> menu(
559 CreateMenu(browser(), page_url, GURL(), GURL()));
560 scoped_ptr<TestRenderViewContextMenu> menu_incognito(
561 CreateMenu(browser_incognito, page_url, GURL(), GURL()));
563 // Look for the extension item in the menu, and execute it.
564 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
565 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id));
566 menu->ExecuteCommand(command_id, 0);
568 // Wait for the extension's script to tell us its onclick fired. Ensure
569 // that the incognito version doesn't fire until we explicitly click the
570 // incognito menu item.
571 ASSERT_TRUE(onclick.WaitUntilSatisfied());
572 EXPECT_FALSE(onclick_incognito.was_satisfied());
574 ASSERT_TRUE(menu_incognito->IsCommandIdEnabled(command_id));
575 menu_incognito->ExecuteCommand(command_id, 0);
576 ASSERT_TRUE(onclick_incognito.WaitUntilSatisfied());
579 // Tests that items with a context of frames only appear when the menu is
580 // invoked in a frame.
581 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Frames) {
582 ExtensionTestMessageListener listener("created items", false);
583 ASSERT_TRUE(LoadContextMenuExtension("frames"));
584 ASSERT_TRUE(listener.WaitUntilSatisfied());
586 GURL page_url("http://www.google.com");
587 GURL no_frame_url;
588 GURL frame_url("http://www.google.com");
590 ASSERT_TRUE(MenuHasItemWithLabel(
591 page_url, GURL(), no_frame_url, std::string("Page item")));
592 ASSERT_FALSE(MenuHasItemWithLabel(
593 page_url, GURL(), no_frame_url, std::string("Frame item")));
595 ASSERT_TRUE(MenuHasItemWithLabel(
596 page_url, GURL(), frame_url, std::string("Page item")));
597 ASSERT_TRUE(MenuHasItemWithLabel(
598 page_url, GURL(), frame_url, std::string("Frame item")));
601 // Tests enabling and disabling a context menu item.
602 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Enabled) {
603 TestEnabledContextMenu(true);
604 TestEnabledContextMenu(false);
607 class ExtensionContextMenuBrowserLazyTest :
608 public ExtensionContextMenuBrowserTest {
609 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
610 ExtensionContextMenuBrowserTest::SetUpCommandLine(command_line);
611 // Set shorter delays to prevent test timeouts.
612 command_line->AppendSwitchASCII(
613 extensions::switches::kEventPageIdleTime, "1");
614 command_line->AppendSwitchASCII(
615 extensions::switches::kEventPageSuspendingTime, "0");
619 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserLazyTest, EventPage) {
620 GURL about_blank("about:blank");
621 LazyBackgroundObserver page_complete;
622 const extensions::Extension* extension = LoadContextMenuExtension(
623 "event_page");
624 ASSERT_TRUE(extension);
625 page_complete.Wait();
627 // Test that menu items appear while the page is unloaded.
628 ASSERT_TRUE(MenuHasItemWithLabel(
629 about_blank, GURL(), GURL(), std::string("Item 1")));
630 ASSERT_TRUE(MenuHasItemWithLabel(
631 about_blank, GURL(), GURL(), std::string("Checkbox 1")));
633 // Test that checked menu items retain their checkedness.
634 LazyBackgroundObserver checkbox_checked;
635 scoped_ptr<TestRenderViewContextMenu> menu(
636 CreateMenu(browser(), about_blank, GURL(), GURL()));
637 MenuItem::Id id(false, extension->id());
638 id.string_uid = "checkbox1";
639 int command_id = -1;
640 ASSERT_TRUE(FindCommandId(menu.get(), id, &command_id));
641 EXPECT_FALSE(menu->IsCommandIdChecked(command_id));
643 // Executing the checkbox also fires the onClicked event.
644 ExtensionTestMessageListener listener("onClicked fired for checkbox1", false);
645 menu->ExecuteCommand(command_id, 0);
646 checkbox_checked.WaitUntilClosed();
648 EXPECT_TRUE(menu->IsCommandIdChecked(command_id));
649 ASSERT_TRUE(listener.WaitUntilSatisfied());
652 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,
653 IncognitoSplitContextMenuCount) {
654 ExtensionTestMessageListener created("created item regular", false);
655 ExtensionTestMessageListener created_incognito("created item incognito",
656 false);
658 // Create an incognito profile.
659 ASSERT_TRUE(browser()->profile()->GetOffTheRecordProfile());
660 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito"));
662 // Wait for the extension's processes to tell us they've created an item.
663 ASSERT_TRUE(created.WaitUntilSatisfied());
664 ASSERT_TRUE(created_incognito.WaitUntilSatisfied());
665 ASSERT_EQ(2u, GetItems().size());
667 browser()->profile()->DestroyOffTheRecordProfile();
668 ASSERT_EQ(1u, GetItems().size());