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.
4
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_test_message_listener.h"
10 #include "chrome/browser/extensions/lazy_background_page_test_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.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/extension_system.h"
19 #include "extensions/browser/test_management_policy.h"
20 #include "extensions/common/extension_set.h"
21 #include "extensions/common/switches.h"
22 #include "net/dns/mock_host_resolver.h"
23 #include "ui/base/models/menu_model.h"
24
25 using content::WebContents;
26 using extensions::MenuItem;
27 using ui::MenuModel;
28
29 class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest {
30 public:
31 // Helper to load an extension from context_menus/|subdirectory| in the
32 // extensions test data dir.
LoadContextMenuExtension(std::string subdirectory)33 const extensions::Extension* LoadContextMenuExtension(
34 std::string subdirectory) {
35 base::FilePath extension_dir =
36 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory);
37 return LoadExtension(extension_dir);
38 }
39
40 // Helper to load an extension from context_menus/top_level/|subdirectory| in
41 // the extensions test data dir.
LoadTopLevelContextMenuExtension(std::string subdirectory)42 const extensions::Extension* LoadTopLevelContextMenuExtension(
43 std::string subdirectory) {
44 base::FilePath extension_dir =
45 test_data_dir_.AppendASCII("context_menus").AppendASCII("top_level");
46 extension_dir = extension_dir.AppendASCII(subdirectory);
47 return LoadExtension(extension_dir);
48 }
49
LoadContextMenuExtensionIncognito(std::string subdirectory)50 const extensions::Extension* LoadContextMenuExtensionIncognito(
51 std::string subdirectory) {
52 base::FilePath extension_dir =
53 test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory);
54 return LoadExtensionIncognito(extension_dir);
55 }
56
57 // Returns the active WebContents.
GetWebContents()58 WebContents* GetWebContents() {
59 return browser()->tab_strip_model()->GetActiveWebContents();
60 }
61
62 // Shortcut to return the current MenuManager.
menu_manager()63 extensions::MenuManager* menu_manager() {
64 return extensions::MenuManager::Get(browser()->profile());
65 }
66
67 // Returns a pointer to the currently loaded extension with |name|, or null
68 // if not found.
GetExtensionNamed(const std::string & name)69 const extensions::Extension* GetExtensionNamed(const std::string& name) {
70 const extensions::ExtensionSet* extensions =
71 browser()->profile()->GetExtensionService()->extensions();
72 for (extensions::ExtensionSet::const_iterator i = extensions->begin();
73 i != extensions->end(); ++i) {
74 if ((*i)->name() == name) {
75 return i->get();
76 }
77 }
78 return NULL;
79 }
80
81 // This gets all the items that any extension has registered for possible
82 // inclusion in context menus.
GetItems()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());
91 }
92 return result;
93 }
94
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
97 // was found.
MenuHasItemWithLabel(const GURL & page_url,const GURL & link_url,const GURL & frame_url,const std::string & label)98 bool MenuHasItemWithLabel(const GURL& page_url,
99 const GURL& link_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);
106 }
107
108 // This creates an extension that starts |enabled| and then switches to
109 // |!enabled|.
TestEnabledContextMenu(bool enabled)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"));
115
116 ASSERT_TRUE(begin.WaitUntilSatisfied());
117
118 if (enabled)
119 begin.Reply("start enabled");
120 else
121 begin.Reply("start disabled");
122
123 // Wait for the extension to tell us it's created an item.
124 ASSERT_TRUE(create.WaitUntilSatisfied());
125 create.Reply("go");
126
127 GURL page_url("http://www.google.com");
128
129 // Create and build our test context menu.
130 scoped_ptr<TestRenderViewContextMenu> menu(
131 TestRenderViewContextMenu::Create(
132 GetWebContents(), page_url, GURL(), GURL()));
133
134 // Look for the extension item in the menu, and make sure it's |enabled|.
135 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
136 ASSERT_EQ(enabled, menu->IsCommandIdEnabled(command_id));
137
138 // Update the item and make sure it is now |!enabled|.
139 ASSERT_TRUE(update.WaitUntilSatisfied());
140 ASSERT_EQ(!enabled, menu->IsCommandIdEnabled(command_id));
141 }
142
MenuHasExtensionItemWithLabel(TestRenderViewContextMenu * menu,const std::string & label)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)
153 return true;
154 }
155 return false;
156 }
157
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
160 // false.
GetItemLabel(TestRenderViewContextMenu * menu,const MenuItem::Id & id,base::string16 * result)161 bool GetItemLabel(TestRenderViewContextMenu* menu,
162 const MenuItem::Id& id,
163 base::string16* result) {
164 int command_id = 0;
165 if (!FindCommandId(menu, id, &command_id))
166 return false;
167
168 MenuModel* model = NULL;
169 int index = -1;
170 if (!menu->GetMenuModelAndItemIndex(command_id, &model, &index)) {
171 return false;
172 }
173 *result = model->GetLabelAt(index);
174 return true;
175 }
176
177 // Given an extension menu item id, tries to find the corresponding command id
178 // in the menu.
FindCommandId(TestRenderViewContextMenu * menu,const MenuItem::Id & id,int * command_id)179 bool FindCommandId(TestRenderViewContextMenu* menu,
180 const MenuItem::Id& id,
181 int* command_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;
187 return true;
188 }
189 }
190 return false;
191 }
192 };
193
194 // Tests adding a simple context menu item.
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,Simple)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"));
199
200 // Wait for the extension to tell us it's created an item.
201 ASSERT_TRUE(listener1.WaitUntilSatisfied());
202
203 GURL page_url("http://www.google.com");
204
205 // Create and build our test context menu.
206 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create(
207 GetWebContents(), page_url, GURL(), GURL()));
208
209 // Look for the extension item in the menu, and execute it.
210 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
211 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id));
212 menu->ExecuteCommand(command_id, 0);
213
214 // Wait for the extension's script to tell us its onclick fired.
215 ASSERT_TRUE(listener2.WaitUntilSatisfied());
216 }
217
218 // Tests that setting "documentUrlPatterns" for an item properly restricts
219 // those items to matching pages.
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,Patterns)220 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Patterns) {
221 ExtensionTestMessageListener listener("created items", false);
222
223 ASSERT_TRUE(LoadContextMenuExtension("patterns"));
224
225 // Wait for the js test code to create its two items with patterns.
226 ASSERT_TRUE(listener.WaitUntilSatisfied());
227
228 // Check that a document url that should match the items' patterns appears.
229 GURL google_url("http://www.google.com");
230 ASSERT_TRUE(MenuHasItemWithLabel(google_url,
231 GURL(),
232 GURL(),
233 std::string("test_item1")));
234 ASSERT_TRUE(MenuHasItemWithLabel(google_url,
235 GURL(),
236 GURL(),
237 std::string("test_item2")));
238
239 // Now check with a non-matching url.
240 GURL test_url("http://www.test.com");
241 ASSERT_FALSE(MenuHasItemWithLabel(test_url,
242 GURL(),
243 GURL(),
244 std::string("test_item1")));
245 ASSERT_FALSE(MenuHasItemWithLabel(test_url,
246 GURL(),
247 GURL(),
248 std::string("test_item2")));
249 }
250
251 // Tests registering an item with a very long title that should get truncated in
252 // the actual menu displayed.
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,LongTitle)253 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, LongTitle) {
254 ExtensionTestMessageListener listener("created", false);
255
256 // Load the extension and wait until it's created a menu item.
257 ASSERT_TRUE(LoadContextMenuExtension("long_title"));
258 ASSERT_TRUE(listener.WaitUntilSatisfied());
259
260 // Make sure we have an item registered with a long title.
261 size_t limit = extensions::ContextMenuMatcher::kMaxExtensionItemTitleLength;
262 MenuItem::List items = GetItems();
263 ASSERT_EQ(1u, items.size());
264 MenuItem* item = items.at(0);
265 ASSERT_GT(item->title().size(), limit);
266
267 // Create a context menu, then find the item's label. It should be properly
268 // truncated.
269 GURL url("http://foo.com/");
270 scoped_ptr<TestRenderViewContextMenu> menu(
271 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL()));
272
273 base::string16 label;
274 ASSERT_TRUE(GetItemLabel(menu.get(), item->id(), &label));
275 ASSERT_TRUE(label.size() <= limit);
276 }
277
278 // Flaky on Windows debug bots. http://crbug.com/251590
279 #if defined(OS_WIN)
280 #define MAYBE_TopLevel DISABLED_TopLevel
281 #else
282 #define MAYBE_TopLevel TopLevel
283 #endif
284 // Checks that Context Menus are ordered alphabetically by their name when
285 // extensions have only one single Context Menu item and by the extension name
286 // when multiples Context Menu items are created.
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,MAYBE_TopLevel)287 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, MAYBE_TopLevel) {
288 // We expect to see the following items in the menu:
289 // An Extension with multiple Context Menus
290 // Context Menu #1
291 // Context Menu #2
292 // Context Menu #1 - Extension #2
293 // Context Menu #2 - Extension #3
294 // Context Menu #3 - Extension #1
295 // Ze Extension with multiple Context Menus
296 // Context Menu #1
297 // Context Menu #2
298
299 // Load extensions and wait until it's created a single menu item.
300 ExtensionTestMessageListener listener1("created item", false);
301 ASSERT_TRUE(LoadTopLevelContextMenuExtension("single1"));
302 ASSERT_TRUE(listener1.WaitUntilSatisfied());
303
304 ExtensionTestMessageListener listener2("created item", false);
305 ASSERT_TRUE(LoadTopLevelContextMenuExtension("single2"));
306 ASSERT_TRUE(listener2.WaitUntilSatisfied());
307
308 ExtensionTestMessageListener listener3("created item", false);
309 ASSERT_TRUE(LoadTopLevelContextMenuExtension("single3"));
310 ASSERT_TRUE(listener3.WaitUntilSatisfied());
311
312 // Load extensions and wait until it's created two menu items.
313 ExtensionTestMessageListener listener4("created items", false);
314 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi4"));
315 ASSERT_TRUE(listener4.WaitUntilSatisfied());
316
317 ExtensionTestMessageListener listener5("created items", false);
318 ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi5"));
319 ASSERT_TRUE(listener5.WaitUntilSatisfied());
320
321 GURL url("http://foo.com/");
322 scoped_ptr<TestRenderViewContextMenu> menu(
323 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL()));
324
325 int index = 0;
326 MenuModel* model = NULL;
327
328 ASSERT_TRUE(menu->GetMenuModelAndItemIndex(
329 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index));
330 EXPECT_EQ(base::UTF8ToUTF16("An Extension with multiple Context Menus"),
331 model->GetLabelAt(index++));
332 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #1 - Extension #2"),
333 model->GetLabelAt(index++));
334 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #2 - Extension #3"),
335 model->GetLabelAt(index++));
336 EXPECT_EQ(base::UTF8ToUTF16("Context Menu #3 - Extension #1"),
337 model->GetLabelAt(index++));
338 EXPECT_EQ(base::UTF8ToUTF16("Ze Extension with multiple Context Menus"),
339 model->GetLabelAt(index++));
340 }
341
342 // Checks that in |menu|, the item at |index| has type |expected_type| and a
343 // label of |expected_label|.
ExpectLabelAndType(const char * expected_label,MenuModel::ItemType expected_type,const MenuModel & menu,int index)344 static void ExpectLabelAndType(const char* expected_label,
345 MenuModel::ItemType expected_type,
346 const MenuModel& menu,
347 int index) {
348 EXPECT_EQ(expected_type, menu.GetTypeAt(index));
349 EXPECT_EQ(base::UTF8ToUTF16(expected_label), menu.GetLabelAt(index));
350 }
351
352 // In the separators test we build a submenu with items and separators in two
353 // different ways - this is used to verify the results in both cases.
VerifyMenuForSeparatorsTest(const MenuModel & menu)354 static void VerifyMenuForSeparatorsTest(const MenuModel& menu) {
355 // We expect to see the following items in the menu:
356 // radio1
357 // radio2
358 // --separator-- (automatically added)
359 // normal1
360 // --separator--
361 // normal2
362 // --separator--
363 // radio3
364 // radio4
365 // --separator--
366 // normal3
367
368 int index = 0;
369 ASSERT_EQ(11, menu.GetItemCount());
370 ExpectLabelAndType("radio1", MenuModel::TYPE_RADIO, menu, index++);
371 ExpectLabelAndType("radio2", MenuModel::TYPE_RADIO, menu, index++);
372 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
373 ExpectLabelAndType("normal1", MenuModel::TYPE_COMMAND, menu, index++);
374 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
375 ExpectLabelAndType("normal2", MenuModel::TYPE_COMMAND, menu, index++);
376 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
377 ExpectLabelAndType("radio3", MenuModel::TYPE_RADIO, menu, index++);
378 ExpectLabelAndType("radio4", MenuModel::TYPE_RADIO, menu, index++);
379 EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
380 ExpectLabelAndType("normal3", MenuModel::TYPE_COMMAND, menu, index++);
381 }
382
383 #if defined(OS_WIN)
384 #define MAYBE_Separators DISABLED_Separators
385 #else
386 #define MAYBE_Separators Separators
387 #endif
388
389 // Tests a number of cases for auto-generated and explicitly added separators.
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,Separators)390 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Separators) {
391 // Load the extension.
392 ASSERT_TRUE(LoadContextMenuExtension("separators"));
393 const extensions::Extension* extension = GetExtensionNamed("Separators Test");
394 ASSERT_TRUE(extension != NULL);
395
396 // Navigate to test1.html inside the extension, which should create a bunch
397 // of items at the top-level (but they'll get pushed into an auto-generated
398 // parent).
399 ExtensionTestMessageListener listener1("test1 create finished", false);
400 ui_test_utils::NavigateToURL(browser(),
401 GURL(extension->GetResourceURL("test1.html")));
402 listener1.WaitUntilSatisfied();
403
404 GURL url("http://www.google.com/");
405 scoped_ptr<TestRenderViewContextMenu> menu(
406 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL()));
407
408 // The top-level item should be an "automagic parent" with the extension's
409 // name.
410 MenuModel* model = NULL;
411 int index = 0;
412 base::string16 label;
413 ASSERT_TRUE(menu->GetMenuModelAndItemIndex(
414 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index));
415 EXPECT_EQ(base::UTF8ToUTF16(extension->name()), model->GetLabelAt(index));
416 ASSERT_EQ(MenuModel::TYPE_SUBMENU, model->GetTypeAt(index));
417
418 // Get the submenu and verify the items there.
419 MenuModel* submenu = model->GetSubmenuModelAt(index);
420 ASSERT_TRUE(submenu != NULL);
421 VerifyMenuForSeparatorsTest(*submenu);
422
423 // Now run our second test - navigate to test2.html which creates an explicit
424 // parent node and populates that with the same items as in test1.
425 ExtensionTestMessageListener listener2("test2 create finished", false);
426 ui_test_utils::NavigateToURL(browser(),
427 GURL(extension->GetResourceURL("test2.html")));
428 listener2.WaitUntilSatisfied();
429 menu.reset(
430 TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL()));
431 ASSERT_TRUE(menu->GetMenuModelAndItemIndex(
432 IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index));
433 EXPECT_EQ(base::UTF8ToUTF16("parent"), model->GetLabelAt(index));
434 submenu = model->GetSubmenuModelAt(index);
435 ASSERT_TRUE(submenu != NULL);
436 VerifyMenuForSeparatorsTest(*submenu);
437 }
438
439 // Tests that targetUrlPattern keeps items from appearing when there is no
440 // target url.
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,TargetURLs)441 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, TargetURLs) {
442 ExtensionTestMessageListener listener("created items", false);
443 ASSERT_TRUE(LoadContextMenuExtension("target_urls"));
444 ASSERT_TRUE(listener.WaitUntilSatisfied());
445
446 GURL google_url("http://www.google.com");
447 GURL non_google_url("http://www.foo.com");
448
449 // No target url - the item should not appear.
450 ASSERT_FALSE(MenuHasItemWithLabel(
451 google_url, GURL(), GURL(), std::string("item1")));
452
453 // A matching target url - the item should appear.
454 ASSERT_TRUE(MenuHasItemWithLabel(google_url,
455 google_url,
456 GURL(),
457 std::string("item1")));
458
459 // A non-matching target url - the item should not appear.
460 ASSERT_FALSE(MenuHasItemWithLabel(google_url,
461 non_google_url,
462 GURL(),
463 std::string("item1")));
464 }
465
466 // Tests adding of context menus in incognito mode.
467 #if defined(OS_LINUX)
468 // Flakily hangs on Linux/CrOS - http://crbug.com/88317
469 #define MAYBE_IncognitoSplit DISABLED_IncognitoSplit
470 #else
471 #define MAYBE_IncognitoSplit IncognitoSplit
472 #endif
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,MAYBE_IncognitoSplit)473 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, MAYBE_IncognitoSplit) {
474 ExtensionTestMessageListener created("created item regular", false);
475 ExtensionTestMessageListener created_incognito("created item incognito",
476 false);
477
478 ExtensionTestMessageListener onclick("onclick fired regular", false);
479 ExtensionTestMessageListener onclick_incognito("onclick fired incognito",
480 false);
481
482 // Open an incognito window.
483 Browser* browser_incognito = ui_test_utils::OpenURLOffTheRecord(
484 browser()->profile(), GURL("about:blank"));
485
486 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito"));
487
488 // Wait for the extension's processes to tell us they've created an item.
489 ASSERT_TRUE(created.WaitUntilSatisfied());
490 ASSERT_TRUE(created_incognito.WaitUntilSatisfied());
491
492 GURL page_url("http://www.google.com");
493
494 // Create and build our test context menu.
495 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create(
496 GetWebContents(), page_url, GURL(), GURL()));
497 WebContents* incognito_web_contents =
498 browser_incognito->tab_strip_model()->GetActiveWebContents();
499 scoped_ptr<TestRenderViewContextMenu> menu_incognito(
500 TestRenderViewContextMenu::Create(
501 incognito_web_contents, page_url, GURL(), GURL()));
502
503 // Look for the extension item in the menu, and execute it.
504 int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
505 ASSERT_TRUE(menu->IsCommandIdEnabled(command_id));
506 menu->ExecuteCommand(command_id, 0);
507
508 // Wait for the extension's script to tell us its onclick fired. Ensure
509 // that the incognito version doesn't fire until we explicitly click the
510 // incognito menu item.
511 ASSERT_TRUE(onclick.WaitUntilSatisfied());
512 EXPECT_FALSE(onclick_incognito.was_satisfied());
513
514 ASSERT_TRUE(menu_incognito->IsCommandIdEnabled(command_id));
515 menu_incognito->ExecuteCommand(command_id, 0);
516 ASSERT_TRUE(onclick_incognito.WaitUntilSatisfied());
517 }
518
519 // Tests that items with a context of frames only appear when the menu is
520 // invoked in a frame.
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,Frames)521 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Frames) {
522 ExtensionTestMessageListener listener("created items", false);
523 ASSERT_TRUE(LoadContextMenuExtension("frames"));
524 ASSERT_TRUE(listener.WaitUntilSatisfied());
525
526 GURL page_url("http://www.google.com");
527 GURL no_frame_url;
528 GURL frame_url("http://www.google.com");
529
530 ASSERT_TRUE(MenuHasItemWithLabel(
531 page_url, GURL(), no_frame_url, std::string("Page item")));
532 ASSERT_FALSE(MenuHasItemWithLabel(
533 page_url, GURL(), no_frame_url, std::string("Frame item")));
534
535 ASSERT_TRUE(MenuHasItemWithLabel(
536 page_url, GURL(), frame_url, std::string("Page item")));
537 ASSERT_TRUE(MenuHasItemWithLabel(
538 page_url, GURL(), frame_url, std::string("Frame item")));
539 }
540
541 // Tests enabling and disabling a context menu item.
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,Enabled)542 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Enabled) {
543 TestEnabledContextMenu(true);
544 TestEnabledContextMenu(false);
545 }
546
547 class ExtensionContextMenuBrowserLazyTest :
548 public ExtensionContextMenuBrowserTest {
SetUpCommandLine(CommandLine * command_line)549 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
550 ExtensionContextMenuBrowserTest::SetUpCommandLine(command_line);
551 // Set shorter delays to prevent test timeouts.
552 command_line->AppendSwitchASCII(
553 extensions::switches::kEventPageIdleTime, "1");
554 command_line->AppendSwitchASCII(
555 extensions::switches::kEventPageSuspendingTime, "0");
556 }
557 };
558
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserLazyTest,EventPage)559 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserLazyTest, EventPage) {
560 GURL about_blank("about:blank");
561 LazyBackgroundObserver page_complete;
562 const extensions::Extension* extension = LoadContextMenuExtension(
563 "event_page");
564 ASSERT_TRUE(extension);
565 page_complete.Wait();
566
567 // Test that menu items appear while the page is unloaded.
568 ASSERT_TRUE(MenuHasItemWithLabel(
569 about_blank, GURL(), GURL(), std::string("Item 1")));
570 ASSERT_TRUE(MenuHasItemWithLabel(
571 about_blank, GURL(), GURL(), std::string("Checkbox 1")));
572
573 // Test that checked menu items retain their checkedness.
574 LazyBackgroundObserver checkbox_checked;
575 scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create(
576 GetWebContents(), about_blank, GURL(), GURL()));
577
578 MenuItem::Id id(false, MenuItem::ExtensionKey(extension->id()));
579 id.string_uid = "checkbox1";
580 int command_id = -1;
581 ASSERT_TRUE(FindCommandId(menu.get(), id, &command_id));
582 EXPECT_FALSE(menu->IsCommandIdChecked(command_id));
583
584 // Executing the checkbox also fires the onClicked event.
585 ExtensionTestMessageListener listener("onClicked fired for checkbox1", false);
586 menu->ExecuteCommand(command_id, 0);
587 checkbox_checked.WaitUntilClosed();
588
589 EXPECT_TRUE(menu->IsCommandIdChecked(command_id));
590 ASSERT_TRUE(listener.WaitUntilSatisfied());
591 }
592
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,IncognitoSplitContextMenuCount)593 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,
594 IncognitoSplitContextMenuCount) {
595 ExtensionTestMessageListener created("created item regular", false);
596 ExtensionTestMessageListener created_incognito("created item incognito",
597 false);
598
599 // Create an incognito profile.
600 ASSERT_TRUE(browser()->profile()->GetOffTheRecordProfile());
601 ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito"));
602
603 // Wait for the extension's processes to tell us they've created an item.
604 ASSERT_TRUE(created.WaitUntilSatisfied());
605 ASSERT_TRUE(created_incognito.WaitUntilSatisfied());
606 ASSERT_EQ(2u, GetItems().size());
607
608 browser()->profile()->DestroyOffTheRecordProfile();
609 ASSERT_EQ(1u, GetItems().size());
610 }
611