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/browser/extensions/extension_browsertest.h"
7 #include "chrome/browser/extensions/lazy_background_page_test_util.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
10 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/common/context_menu_params.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/process_manager.h"
17 #include "extensions/browser/test_management_policy.h"
18 #include "extensions/common/extension_set.h"
19 #include "extensions/test/extension_test_message_listener.h"
20 #include "net/dns/mock_host_resolver.h"
21 #include "ui/base/models/menu_model.h"
23 using content::WebContents
;
24 using extensions::ContextMenuMatcher
;
25 using extensions::MenuItem
;
28 class ExtensionContextMenuBrowserTest
: public ExtensionBrowserTest
{
30 // Helper to load an extension from context_menus/|subdirectory| in the
31 // extensions test data dir.
32 const extensions::Extension
* LoadContextMenuExtension(
33 std::string subdirectory
) {
34 base::FilePath extension_dir
=
35 test_data_dir_
.AppendASCII("context_menus").AppendASCII(subdirectory
);
36 return LoadExtension(extension_dir
);
39 // Helper to load an extension from context_menus/top_level/|subdirectory| in
40 // the extensions test data dir.
41 const extensions::Extension
* LoadTopLevelContextMenuExtension(
42 std::string subdirectory
) {
43 base::FilePath extension_dir
=
44 test_data_dir_
.AppendASCII("context_menus").AppendASCII("top_level");
45 extension_dir
= extension_dir
.AppendASCII(subdirectory
);
46 return LoadExtension(extension_dir
);
49 const extensions::Extension
* LoadContextMenuExtensionIncognito(
50 std::string subdirectory
) {
51 base::FilePath extension_dir
=
52 test_data_dir_
.AppendASCII("context_menus").AppendASCII(subdirectory
);
53 return LoadExtensionIncognito(extension_dir
);
56 // Returns the active WebContents.
57 WebContents
* GetWebContents() {
58 return browser()->tab_strip_model()->GetActiveWebContents();
61 // Shortcut to return the current MenuManager.
62 extensions::MenuManager
* menu_manager() {
63 return extensions::MenuManager::Get(browser()->profile());
66 // Returns a pointer to the currently loaded extension with |name|, or null
68 const extensions::Extension
* GetExtensionNamed(const std::string
& name
) {
69 const extensions::ExtensionSet
& extensions
=
70 extensions::ExtensionRegistry::Get(
71 browser()->profile())->enabled_extensions();
72 for (extensions::ExtensionSet::const_iterator i
= extensions
.begin();
73 i
!= extensions
.end(); ++i
) {
74 if ((*i
)->name() == name
) {
81 // This gets all the items that any extension has registered for possible
82 // inclusion in context menus.
83 MenuItem::List
GetItems() {
84 MenuItem::List result
;
85 std::set
<MenuItem::ExtensionKey
> extension_ids
=
86 menu_manager()->ExtensionIds();
87 std::set
<MenuItem::ExtensionKey
>::iterator i
;
88 for (i
= extension_ids
.begin(); i
!= extension_ids
.end(); ++i
) {
89 const MenuItem::List
* list
= menu_manager()->MenuItems(*i
);
90 result
.insert(result
.end(), list
->begin(), list
->end());
95 // This creates a test menu for a page with |page_url| and |link_url|, looks
96 // for an extension item with the given |label|, and returns true if the item
98 bool MenuHasItemWithLabel(const GURL
& page_url
,
100 const GURL
& frame_url
,
101 const std::string
& label
) {
102 scoped_ptr
<TestRenderViewContextMenu
> menu(
103 TestRenderViewContextMenu::Create(
104 GetWebContents(), page_url
, link_url
, frame_url
));
105 return MenuHasExtensionItemWithLabel(menu
.get(), label
);
108 // This creates an extension that starts |enabled| and then switches to
110 void TestEnabledContextMenu(bool enabled
) {
111 ExtensionTestMessageListener
begin("begin", true);
112 ExtensionTestMessageListener
create("create", true);
113 ExtensionTestMessageListener
update("update", false);
114 ASSERT_TRUE(LoadContextMenuExtension("enabled"));
116 ASSERT_TRUE(begin
.WaitUntilSatisfied());
119 begin
.Reply("start enabled");
121 begin
.Reply("start disabled");
123 // Wait for the extension to tell us it's created an item.
124 ASSERT_TRUE(create
.WaitUntilSatisfied());
127 GURL
page_url("http://www.google.com");
129 // Create and build our test context menu.
130 scoped_ptr
<TestRenderViewContextMenu
> menu(
131 TestRenderViewContextMenu::Create(
132 GetWebContents(), page_url
, GURL(), GURL()));
134 // Look for the extension item in the menu, and make sure it's |enabled|.
135 int command_id
= ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
136 ASSERT_EQ(enabled
, menu
->IsCommandIdEnabled(command_id
));
138 // Update the item and make sure it is now |!enabled|.
139 ASSERT_TRUE(update
.WaitUntilSatisfied());
140 ASSERT_EQ(!enabled
, menu
->IsCommandIdEnabled(command_id
));
143 bool MenuHasExtensionItemWithLabel(TestRenderViewContextMenu
* menu
,
144 const std::string
& label
) {
145 base::string16 label16
= base::UTF8ToUTF16(label
);
146 std::map
<int, MenuItem::Id
>::iterator i
;
147 for (i
= menu
->extension_items().extension_item_map_
.begin();
148 i
!= menu
->extension_items().extension_item_map_
.end(); ++i
) {
149 const MenuItem::Id
& id
= i
->second
;
150 base::string16 tmp_label
;
151 EXPECT_TRUE(GetItemLabel(menu
, id
, &tmp_label
));
152 if (tmp_label
== label16
)
158 // Looks in the menu for an extension item with |id|, and if it is found and
159 // has a label, that is put in |result| and we return true. Otherwise returns
161 bool GetItemLabel(TestRenderViewContextMenu
* menu
,
162 const MenuItem::Id
& id
,
163 base::string16
* result
) {
165 if (!FindCommandId(menu
, id
, &command_id
))
168 MenuModel
* model
= NULL
;
170 if (!menu
->GetMenuModelAndItemIndex(command_id
, &model
, &index
)) {
173 *result
= model
->GetLabelAt(index
);
177 // Given an extension menu item id, tries to find the corresponding command id
179 bool FindCommandId(TestRenderViewContextMenu
* menu
,
180 const MenuItem::Id
& id
,
182 std::map
<int, MenuItem::Id
>::const_iterator i
;
183 for (i
= menu
->extension_items().extension_item_map_
.begin();
184 i
!= menu
->extension_items().extension_item_map_
.end(); ++i
) {
185 if (i
->second
== id
) {
186 *command_id
= i
->first
;
194 // Tests adding a simple context menu item.
195 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
, Simple
) {
196 ExtensionTestMessageListener
listener1("created item", false);
197 ExtensionTestMessageListener
listener2("onclick fired", false);
198 ASSERT_TRUE(LoadContextMenuExtension("simple"));
200 // Wait for the extension to tell us it's created an item.
201 ASSERT_TRUE(listener1
.WaitUntilSatisfied());
203 GURL
page_url("http://www.google.com");
205 // Create and build our test context menu.
206 scoped_ptr
<TestRenderViewContextMenu
> menu(TestRenderViewContextMenu::Create(
207 GetWebContents(), page_url
, GURL(), GURL()));
209 // Look for the extension item in the menu, and execute it.
210 int command_id
= ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
211 ASSERT_TRUE(menu
->IsCommandIdEnabled(command_id
));
212 menu
->ExecuteCommand(command_id
, 0);
214 // Wait for the extension's script to tell us its onclick fired.
215 ASSERT_TRUE(listener2
.WaitUntilSatisfied());
218 // Tests that previous onclick is not fired after updating the menu's onclick,
219 // and whether setting onclick to null removes the handler.
220 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
, UpdateOnclick
) {
221 ExtensionTestMessageListener
listener_error1("onclick1-unexpected", false);
222 ExtensionTestMessageListener
listener_error2("onclick2-unexpected", false);
223 ExtensionTestMessageListener
listener_update1("update1", true);
224 ExtensionTestMessageListener
listener_update2("update2", false);
225 ExtensionTestMessageListener
listener_done("onclick2", false);
227 const extensions::Extension
* extension
=
228 LoadContextMenuExtension("onclick_null");
229 ASSERT_TRUE(extension
);
231 // Wait till item has been created and updated.
232 ASSERT_TRUE(listener_update1
.WaitUntilSatisfied());
234 GURL
page_url("http://www.google.com");
236 // Create and build our test context menu.
237 scoped_ptr
<TestRenderViewContextMenu
> menu(TestRenderViewContextMenu::Create(
238 GetWebContents(), page_url
, GURL(), GURL()));
240 // Look for the extension item in the menu, and execute it.
241 MenuItem::Id
id(false, MenuItem::ExtensionKey(extension
->id()));
242 id
.string_uid
= "id1";
244 ASSERT_TRUE(FindCommandId(menu
.get(), id
, &command_id
));
245 menu
->ExecuteCommand(command_id
, 0);
247 // Let the test proceed.
248 listener_update1
.Reply("");
250 // Wait until the second context menu has been set up.
251 ASSERT_TRUE(listener_update2
.WaitUntilSatisfied());
253 // Rebuild the context menu and click on the second extension item.
254 menu
.reset(TestRenderViewContextMenu::Create(GetWebContents(), page_url
,
256 id
.string_uid
= "id2";
257 ASSERT_TRUE(FindCommandId(menu
.get(), id
, &command_id
));
258 menu
->ExecuteCommand(command_id
, 0);
259 ASSERT_TRUE(listener_done
.WaitUntilSatisfied());
261 // Upon completion, the replaced onclick callbacks should not have fired.
262 ASSERT_FALSE(listener_error1
.was_satisfied());
263 ASSERT_FALSE(listener_error2
.was_satisfied());
266 // Tests that setting "documentUrlPatterns" for an item properly restricts
267 // those items to matching pages.
268 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
, Patterns
) {
269 ExtensionTestMessageListener
listener("created items", false);
271 ASSERT_TRUE(LoadContextMenuExtension("patterns"));
273 // Wait for the js test code to create its two items with patterns.
274 ASSERT_TRUE(listener
.WaitUntilSatisfied());
276 // Check that a document url that should match the items' patterns appears.
277 GURL
google_url("http://www.google.com");
278 ASSERT_TRUE(MenuHasItemWithLabel(google_url
,
281 std::string("test_item1")));
282 ASSERT_TRUE(MenuHasItemWithLabel(google_url
,
285 std::string("test_item2")));
287 // Now check with a non-matching url.
288 GURL
test_url("http://www.test.com");
289 ASSERT_FALSE(MenuHasItemWithLabel(test_url
,
292 std::string("test_item1")));
293 ASSERT_FALSE(MenuHasItemWithLabel(test_url
,
296 std::string("test_item2")));
299 // Tests registering an item with a very long title that should get truncated in
300 // the actual menu displayed.
301 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
, LongTitle
) {
302 ExtensionTestMessageListener
listener("created", false);
304 // Load the extension and wait until it's created a menu item.
305 ASSERT_TRUE(LoadContextMenuExtension("long_title"));
306 ASSERT_TRUE(listener
.WaitUntilSatisfied());
308 // Make sure we have an item registered with a long title.
309 size_t limit
= extensions::ContextMenuMatcher::kMaxExtensionItemTitleLength
;
310 MenuItem::List items
= GetItems();
311 ASSERT_EQ(1u, items
.size());
312 MenuItem
* item
= items
.at(0);
313 ASSERT_GT(item
->title().size(), limit
);
315 // Create a context menu, then find the item's label. It should be properly
317 GURL
url("http://foo.com/");
318 scoped_ptr
<TestRenderViewContextMenu
> menu(
319 TestRenderViewContextMenu::Create(GetWebContents(), url
, GURL(), GURL()));
321 base::string16 label
;
322 ASSERT_TRUE(GetItemLabel(menu
.get(), item
->id(), &label
));
323 ASSERT_TRUE(label
.size() <= limit
);
326 // Flaky on Windows debug bots. http://crbug.com/251590
328 #define MAYBE_TopLevel DISABLED_TopLevel
330 #define MAYBE_TopLevel TopLevel
332 // Checks that Context Menus are ordered alphabetically by their name when
333 // extensions have only one single Context Menu item and by the extension name
334 // when multiples Context Menu items are created.
335 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
, MAYBE_TopLevel
) {
336 // We expect to see the following items in the menu:
337 // An Extension with multiple Context Menus
340 // Context Menu #1 - Extension #2
341 // Context Menu #2 - Extension #3
342 // Context Menu #3 - Extension #1
343 // Ze Extension with multiple Context Menus
347 // Load extensions and wait until it's created a single menu item.
348 ExtensionTestMessageListener
listener1("created item", false);
349 ASSERT_TRUE(LoadTopLevelContextMenuExtension("single1"));
350 ASSERT_TRUE(listener1
.WaitUntilSatisfied());
352 ExtensionTestMessageListener
listener2("created item", false);
353 ASSERT_TRUE(LoadTopLevelContextMenuExtension("single2"));
354 ASSERT_TRUE(listener2
.WaitUntilSatisfied());
356 ExtensionTestMessageListener
listener3("created item", false);
357 ASSERT_TRUE(LoadTopLevelContextMenuExtension("single3"));
358 ASSERT_TRUE(listener3
.WaitUntilSatisfied());
360 // Load extensions and wait until it's created two menu items.
361 ExtensionTestMessageListener
listener4("created items", false);
362 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi4"));
363 ASSERT_TRUE(listener4
.WaitUntilSatisfied());
365 ExtensionTestMessageListener
listener5("created items", false);
366 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi5"));
367 ASSERT_TRUE(listener5
.WaitUntilSatisfied());
369 GURL
url("http://foo.com/");
370 scoped_ptr
<TestRenderViewContextMenu
> menu(
371 TestRenderViewContextMenu::Create(GetWebContents(), url
, GURL(), GURL()));
374 MenuModel
* model
= NULL
;
376 ASSERT_TRUE(menu
->GetMenuModelAndItemIndex(
377 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0),
380 EXPECT_EQ(base::UTF8ToUTF16("An Extension with multiple Context Menus"),
381 model
->GetLabelAt(index
++));
382 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #1 - Extension #2"),
383 model
->GetLabelAt(index
++));
384 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #2 - Extension #3"),
385 model
->GetLabelAt(index
++));
386 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #3 - Extension #1"),
387 model
->GetLabelAt(index
++));
388 EXPECT_EQ(base::UTF8ToUTF16("Ze Extension with multiple Context Menus"),
389 model
->GetLabelAt(index
++));
392 // Checks that in |menu|, the item at |index| has type |expected_type| and a
393 // label of |expected_label|.
394 static void ExpectLabelAndType(const char* expected_label
,
395 MenuModel::ItemType expected_type
,
396 const MenuModel
& menu
,
398 EXPECT_EQ(expected_type
, menu
.GetTypeAt(index
));
399 EXPECT_EQ(base::UTF8ToUTF16(expected_label
), menu
.GetLabelAt(index
));
402 // In the separators test we build a submenu with items and separators in two
403 // different ways - this is used to verify the results in both cases.
404 static void VerifyMenuForSeparatorsTest(const MenuModel
& menu
) {
405 // We expect to see the following items in the menu:
408 // --separator-- (automatically added)
419 ASSERT_EQ(11, menu
.GetItemCount());
420 ExpectLabelAndType("radio1", MenuModel::TYPE_RADIO
, menu
, index
++);
421 ExpectLabelAndType("radio2", MenuModel::TYPE_RADIO
, menu
, index
++);
422 EXPECT_EQ(MenuModel::TYPE_SEPARATOR
, menu
.GetTypeAt(index
++));
423 ExpectLabelAndType("normal1", MenuModel::TYPE_COMMAND
, menu
, index
++);
424 EXPECT_EQ(MenuModel::TYPE_SEPARATOR
, menu
.GetTypeAt(index
++));
425 ExpectLabelAndType("normal2", MenuModel::TYPE_COMMAND
, menu
, index
++);
426 EXPECT_EQ(MenuModel::TYPE_SEPARATOR
, menu
.GetTypeAt(index
++));
427 ExpectLabelAndType("radio3", MenuModel::TYPE_RADIO
, menu
, index
++);
428 ExpectLabelAndType("radio4", MenuModel::TYPE_RADIO
, menu
, index
++);
429 EXPECT_EQ(MenuModel::TYPE_SEPARATOR
, menu
.GetTypeAt(index
++));
430 ExpectLabelAndType("normal3", MenuModel::TYPE_COMMAND
, menu
, index
++);
434 #define MAYBE_Separators DISABLED_Separators
436 #define MAYBE_Separators Separators
439 // Tests a number of cases for auto-generated and explicitly added separators.
440 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
, Separators
) {
441 // Load the extension.
442 ASSERT_TRUE(LoadContextMenuExtension("separators"));
443 const extensions::Extension
* extension
= GetExtensionNamed("Separators Test");
444 ASSERT_TRUE(extension
!= NULL
);
446 // Navigate to test1.html inside the extension, which should create a bunch
447 // of items at the top-level (but they'll get pushed into an auto-generated
449 ExtensionTestMessageListener
listener1("test1 create finished", false);
450 ui_test_utils::NavigateToURL(browser(),
451 GURL(extension
->GetResourceURL("test1.html")));
452 listener1
.WaitUntilSatisfied();
454 GURL
url("http://www.google.com/");
455 scoped_ptr
<TestRenderViewContextMenu
> menu(
456 TestRenderViewContextMenu::Create(GetWebContents(), url
, GURL(), GURL()));
458 // The top-level item should be an "automagic parent" with the extension's
460 MenuModel
* model
= NULL
;
462 base::string16 label
;
463 ASSERT_TRUE(menu
->GetMenuModelAndItemIndex(
464 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0),
467 EXPECT_EQ(base::UTF8ToUTF16(extension
->name()), model
->GetLabelAt(index
));
468 ASSERT_EQ(MenuModel::TYPE_SUBMENU
, model
->GetTypeAt(index
));
470 // Get the submenu and verify the items there.
471 MenuModel
* submenu
= model
->GetSubmenuModelAt(index
);
472 ASSERT_TRUE(submenu
!= NULL
);
473 VerifyMenuForSeparatorsTest(*submenu
);
475 // Now run our second test - navigate to test2.html which creates an explicit
476 // parent node and populates that with the same items as in test1.
477 ExtensionTestMessageListener
listener2("test2 create finished", false);
478 ui_test_utils::NavigateToURL(browser(),
479 GURL(extension
->GetResourceURL("test2.html")));
480 listener2
.WaitUntilSatisfied();
482 TestRenderViewContextMenu::Create(GetWebContents(), url
, GURL(), GURL()));
483 ASSERT_TRUE(menu
->GetMenuModelAndItemIndex(
484 ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0),
487 EXPECT_EQ(base::UTF8ToUTF16("parent"), model
->GetLabelAt(index
));
488 submenu
= model
->GetSubmenuModelAt(index
);
489 ASSERT_TRUE(submenu
!= NULL
);
490 VerifyMenuForSeparatorsTest(*submenu
);
493 // Tests that targetUrlPattern keeps items from appearing when there is no
495 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
, TargetURLs
) {
496 ExtensionTestMessageListener
listener("created items", false);
497 ASSERT_TRUE(LoadContextMenuExtension("target_urls"));
498 ASSERT_TRUE(listener
.WaitUntilSatisfied());
500 GURL
google_url("http://www.google.com");
501 GURL
non_google_url("http://www.foo.com");
503 // No target url - the item should not appear.
504 ASSERT_FALSE(MenuHasItemWithLabel(
505 google_url
, GURL(), GURL(), std::string("item1")));
507 // A matching target url - the item should appear.
508 ASSERT_TRUE(MenuHasItemWithLabel(google_url
,
511 std::string("item1")));
513 // A non-matching target url - the item should not appear.
514 ASSERT_FALSE(MenuHasItemWithLabel(google_url
,
517 std::string("item1")));
520 // Tests adding of context menus in incognito mode.
521 #if defined(OS_LINUX)
522 // Flakily hangs on Linux/CrOS - http://crbug.com/88317
523 #define MAYBE_IncognitoSplit DISABLED_IncognitoSplit
525 #define MAYBE_IncognitoSplit IncognitoSplit
527 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
, MAYBE_IncognitoSplit
) {
528 ExtensionTestMessageListener
created("created item regular", false);
529 ExtensionTestMessageListener
created_incognito("created item incognito",
532 ExtensionTestMessageListener
onclick("onclick fired regular", false);
533 ExtensionTestMessageListener
onclick_incognito("onclick fired incognito",
536 // Open an incognito window.
537 Browser
* browser_incognito
=
538 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
540 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito"));
542 // Wait for the extension's processes to tell us they've created an item.
543 ASSERT_TRUE(created
.WaitUntilSatisfied());
544 ASSERT_TRUE(created_incognito
.WaitUntilSatisfied());
546 GURL
page_url("http://www.google.com");
548 // Create and build our test context menu.
549 scoped_ptr
<TestRenderViewContextMenu
> menu(TestRenderViewContextMenu::Create(
550 GetWebContents(), page_url
, GURL(), GURL()));
551 WebContents
* incognito_web_contents
=
552 browser_incognito
->tab_strip_model()->GetActiveWebContents();
553 scoped_ptr
<TestRenderViewContextMenu
> menu_incognito(
554 TestRenderViewContextMenu::Create(
555 incognito_web_contents
, page_url
, GURL(), GURL()));
557 // Look for the extension item in the menu, and execute it.
558 int command_id
= ContextMenuMatcher::ConvertToExtensionsCustomCommandId(0);
559 ASSERT_TRUE(menu
->IsCommandIdEnabled(command_id
));
560 menu
->ExecuteCommand(command_id
, 0);
562 // Wait for the extension's script to tell us its onclick fired. Ensure
563 // that the incognito version doesn't fire until we explicitly click the
564 // incognito menu item.
565 ASSERT_TRUE(onclick
.WaitUntilSatisfied());
566 EXPECT_FALSE(onclick_incognito
.was_satisfied());
568 ASSERT_TRUE(menu_incognito
->IsCommandIdEnabled(command_id
));
569 menu_incognito
->ExecuteCommand(command_id
, 0);
570 ASSERT_TRUE(onclick_incognito
.WaitUntilSatisfied());
573 // Tests that items with a context of frames only appear when the menu is
574 // invoked in a frame.
575 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
, Frames
) {
576 ExtensionTestMessageListener
listener("created items", false);
577 ASSERT_TRUE(LoadContextMenuExtension("frames"));
578 ASSERT_TRUE(listener
.WaitUntilSatisfied());
580 GURL
page_url("http://www.google.com");
582 GURL
frame_url("http://www.google.com");
584 ASSERT_TRUE(MenuHasItemWithLabel(
585 page_url
, GURL(), no_frame_url
, std::string("Page item")));
586 ASSERT_FALSE(MenuHasItemWithLabel(
587 page_url
, GURL(), no_frame_url
, std::string("Frame item")));
589 ASSERT_TRUE(MenuHasItemWithLabel(
590 page_url
, GURL(), frame_url
, std::string("Page item")));
591 ASSERT_TRUE(MenuHasItemWithLabel(
592 page_url
, GURL(), frame_url
, std::string("Frame item")));
595 // Tests enabling and disabling a context menu item.
596 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
, Enabled
) {
597 TestEnabledContextMenu(true);
598 TestEnabledContextMenu(false);
601 class ExtensionContextMenuBrowserLazyTest
:
602 public ExtensionContextMenuBrowserTest
{
603 void SetUpOnMainThread() override
{
604 ExtensionContextMenuBrowserTest::SetUpOnMainThread();
605 // Set shorter delays to prevent test timeouts.
606 extensions::ProcessManager::SetEventPageIdleTimeForTesting(1);
607 extensions::ProcessManager::SetEventPageSuspendingTimeForTesting(0);
611 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserLazyTest
, EventPage
) {
612 GURL
about_blank("about:blank");
613 LazyBackgroundObserver page_complete
;
614 const extensions::Extension
* extension
= LoadContextMenuExtension(
616 ASSERT_TRUE(extension
);
617 page_complete
.Wait();
619 // Test that menu items appear while the page is unloaded.
620 ASSERT_TRUE(MenuHasItemWithLabel(
621 about_blank
, GURL(), GURL(), std::string("Item 1")));
622 ASSERT_TRUE(MenuHasItemWithLabel(
623 about_blank
, GURL(), GURL(), std::string("Checkbox 1")));
625 // Test that checked menu items retain their checkedness.
626 LazyBackgroundObserver checkbox_checked
;
627 scoped_ptr
<TestRenderViewContextMenu
> menu(TestRenderViewContextMenu::Create(
628 GetWebContents(), about_blank
, GURL(), GURL()));
630 MenuItem::Id
id(false, MenuItem::ExtensionKey(extension
->id()));
631 id
.string_uid
= "checkbox1";
633 ASSERT_TRUE(FindCommandId(menu
.get(), id
, &command_id
));
634 EXPECT_FALSE(menu
->IsCommandIdChecked(command_id
));
636 // Executing the checkbox also fires the onClicked event.
637 ExtensionTestMessageListener
listener("onClicked fired for checkbox1", false);
638 menu
->ExecuteCommand(command_id
, 0);
639 checkbox_checked
.WaitUntilClosed();
641 EXPECT_TRUE(menu
->IsCommandIdChecked(command_id
));
642 ASSERT_TRUE(listener
.WaitUntilSatisfied());
645 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest
,
646 IncognitoSplitContextMenuCount
) {
647 ExtensionTestMessageListener
created("created item regular", false);
648 ExtensionTestMessageListener
created_incognito("created item incognito",
651 // Create an incognito profile.
652 ASSERT_TRUE(browser()->profile()->GetOffTheRecordProfile());
653 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito"));
655 // Wait for the extension's processes to tell us they've created an item.
656 ASSERT_TRUE(created
.WaitUntilSatisfied());
657 ASSERT_TRUE(created_incognito
.WaitUntilSatisfied());
658 ASSERT_EQ(2u, GetItems().size());
660 browser()->profile()->DestroyOffTheRecordProfile();
661 ASSERT_EQ(1u, GetItems().size());