This source file includes following definitions.
- LoadContextMenuExtension
- LoadTopLevelContextMenuExtension
- LoadContextMenuExtensionIncognito
- GetWebContents
- menu_manager
- GetExtensionNamed
- GetItems
- MenuHasItemWithLabel
- TestEnabledContextMenu
- MenuHasExtensionItemWithLabel
- GetItemLabel
- FindCommandId
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- ExpectLabelAndType
- VerifyMenuForSeparatorsTest
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- SetUpCommandLine
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/extensions/lazy_background_page_test_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/common/context_menu_params.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/test_management_policy.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/switches.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/base/models/menu_model.h"
using content::WebContents;
using extensions::MenuItem;
using ui::MenuModel;
class ExtensionContextMenuBrowserTest : public ExtensionBrowserTest {
public:
const extensions::Extension* LoadContextMenuExtension(
std::string subdirectory) {
base::FilePath extension_dir =
test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory);
return LoadExtension(extension_dir);
}
const extensions::Extension* LoadTopLevelContextMenuExtension(
std::string subdirectory) {
base::FilePath extension_dir =
test_data_dir_.AppendASCII("context_menus").AppendASCII("top_level");
extension_dir = extension_dir.AppendASCII(subdirectory);
return LoadExtension(extension_dir);
}
const extensions::Extension* LoadContextMenuExtensionIncognito(
std::string subdirectory) {
base::FilePath extension_dir =
test_data_dir_.AppendASCII("context_menus").AppendASCII(subdirectory);
return LoadExtensionIncognito(extension_dir);
}
WebContents* GetWebContents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}
extensions::MenuManager* menu_manager() {
return extensions::MenuManager::Get(browser()->profile());
}
const extensions::Extension* GetExtensionNamed(std::string name) {
const extensions::ExtensionSet* extensions =
browser()->profile()->GetExtensionService()->extensions();
for (extensions::ExtensionSet::const_iterator i = extensions->begin();
i != extensions->end(); ++i) {
if ((*i)->name() == name) {
return i->get();
}
}
return NULL;
}
MenuItem::List GetItems() {
MenuItem::List result;
std::set<MenuItem::ExtensionKey> extension_ids =
menu_manager()->ExtensionIds();
std::set<MenuItem::ExtensionKey>::iterator i;
for (i = extension_ids.begin(); i != extension_ids.end(); ++i) {
const MenuItem::List* list = menu_manager()->MenuItems(*i);
result.insert(result.end(), list->begin(), list->end());
}
return result;
}
bool MenuHasItemWithLabel(const GURL& page_url,
const GURL& link_url,
const GURL& frame_url,
const std::string& label) {
scoped_ptr<TestRenderViewContextMenu> menu(
TestRenderViewContextMenu::Create(
GetWebContents(), page_url, link_url, frame_url));
return MenuHasExtensionItemWithLabel(menu.get(), label);
}
void TestEnabledContextMenu(bool enabled) {
ExtensionTestMessageListener begin("begin", true);
ExtensionTestMessageListener create("create", true);
ExtensionTestMessageListener update("update", false);
ASSERT_TRUE(LoadContextMenuExtension("enabled"));
ASSERT_TRUE(begin.WaitUntilSatisfied());
if (enabled)
begin.Reply("start enabled");
else
begin.Reply("start disabled");
ASSERT_TRUE(create.WaitUntilSatisfied());
create.Reply("go");
GURL page_url("http://www.google.com");
scoped_ptr<TestRenderViewContextMenu> menu(
TestRenderViewContextMenu::Create(
GetWebContents(), page_url, GURL(), GURL()));
int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
ASSERT_EQ(enabled, menu->IsCommandIdEnabled(command_id));
ASSERT_TRUE(update.WaitUntilSatisfied());
ASSERT_EQ(!enabled, menu->IsCommandIdEnabled(command_id));
}
bool MenuHasExtensionItemWithLabel(TestRenderViewContextMenu* menu,
const std::string& label) {
base::string16 label16 = base::UTF8ToUTF16(label);
std::map<int, MenuItem::Id>::iterator i;
for (i = menu->extension_items().extension_item_map_.begin();
i != menu->extension_items().extension_item_map_.end(); ++i) {
const MenuItem::Id& id = i->second;
base::string16 tmp_label;
EXPECT_TRUE(GetItemLabel(menu, id, &tmp_label));
if (tmp_label == label16)
return true;
}
return false;
}
bool GetItemLabel(TestRenderViewContextMenu* menu,
const MenuItem::Id& id,
base::string16* result) {
int command_id = 0;
if (!FindCommandId(menu, id, &command_id))
return false;
MenuModel* model = NULL;
int index = -1;
if (!menu->GetMenuModelAndItemIndex(command_id, &model, &index)) {
return false;
}
*result = model->GetLabelAt(index);
return true;
}
bool FindCommandId(TestRenderViewContextMenu* menu,
const MenuItem::Id& id,
int* command_id) {
std::map<int, MenuItem::Id>::const_iterator i;
for (i = menu->extension_items().extension_item_map_.begin();
i != menu->extension_items().extension_item_map_.end(); ++i) {
if (i->second == id) {
*command_id = i->first;
return true;
}
}
return false;
}
};
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Simple) {
ExtensionTestMessageListener listener1("created item", false);
ExtensionTestMessageListener listener2("onclick fired", false);
ASSERT_TRUE(LoadContextMenuExtension("simple"));
ASSERT_TRUE(listener1.WaitUntilSatisfied());
GURL page_url("http://www.google.com");
scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create(
GetWebContents(), page_url, GURL(), GURL()));
int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
ASSERT_TRUE(menu->IsCommandIdEnabled(command_id));
menu->ExecuteCommand(command_id, 0);
ASSERT_TRUE(listener2.WaitUntilSatisfied());
}
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Patterns) {
ExtensionTestMessageListener listener("created items", false);
ASSERT_TRUE(LoadContextMenuExtension("patterns"));
ASSERT_TRUE(listener.WaitUntilSatisfied());
GURL google_url("http://www.google.com");
ASSERT_TRUE(MenuHasItemWithLabel(google_url,
GURL(),
GURL(),
std::string("test_item1")));
ASSERT_TRUE(MenuHasItemWithLabel(google_url,
GURL(),
GURL(),
std::string("test_item2")));
GURL test_url("http://www.test.com");
ASSERT_FALSE(MenuHasItemWithLabel(test_url,
GURL(),
GURL(),
std::string("test_item1")));
ASSERT_FALSE(MenuHasItemWithLabel(test_url,
GURL(),
GURL(),
std::string("test_item2")));
}
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, LongTitle) {
ExtensionTestMessageListener listener("created", false);
ASSERT_TRUE(LoadContextMenuExtension("long_title"));
ASSERT_TRUE(listener.WaitUntilSatisfied());
size_t limit = extensions::ContextMenuMatcher::kMaxExtensionItemTitleLength;
MenuItem::List items = GetItems();
ASSERT_EQ(1u, items.size());
MenuItem* item = items.at(0);
ASSERT_GT(item->title().size(), limit);
GURL url("http://foo.com/");
scoped_ptr<TestRenderViewContextMenu> menu(
TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL()));
base::string16 label;
ASSERT_TRUE(GetItemLabel(menu.get(), item->id(), &label));
ASSERT_TRUE(label.size() <= limit);
}
#if defined(OS_WIN)
#define MAYBE_TopLevel DISABLED_TopLevel
#else
#define MAYBE_TopLevel TopLevel
#endif
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, MAYBE_TopLevel) {
ExtensionTestMessageListener listener1("created item", false);
ASSERT_TRUE(LoadTopLevelContextMenuExtension("single1"));
ASSERT_TRUE(listener1.WaitUntilSatisfied());
ExtensionTestMessageListener listener2("created item", false);
ASSERT_TRUE(LoadTopLevelContextMenuExtension("single2"));
ASSERT_TRUE(listener2.WaitUntilSatisfied());
ExtensionTestMessageListener listener3("created item", false);
ASSERT_TRUE(LoadTopLevelContextMenuExtension("single3"));
ASSERT_TRUE(listener3.WaitUntilSatisfied());
ExtensionTestMessageListener listener4("created items", false);
ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi4"));
ASSERT_TRUE(listener4.WaitUntilSatisfied());
ExtensionTestMessageListener listener5("created items", false);
ASSERT_TRUE(LoadTopLevelContextMenuExtension("multi5"));
ASSERT_TRUE(listener5.WaitUntilSatisfied());
GURL url("http://foo.com/");
scoped_ptr<TestRenderViewContextMenu> menu(
TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL()));
int index = 0;
MenuModel* model = NULL;
ASSERT_TRUE(menu->GetMenuModelAndItemIndex(
IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index));
EXPECT_EQ(base::UTF8ToUTF16("An Extension with multiple Context Menus"),
model->GetLabelAt(index++));
EXPECT_EQ(base::UTF8ToUTF16("Context Menu #1 - Extension #2"),
model->GetLabelAt(index++));
EXPECT_EQ(base::UTF8ToUTF16("Context Menu #2 - Extension #3"),
model->GetLabelAt(index++));
EXPECT_EQ(base::UTF8ToUTF16("Context Menu #3 - Extension #1"),
model->GetLabelAt(index++));
EXPECT_EQ(base::UTF8ToUTF16("Ze Extension with multiple Context Menus"),
model->GetLabelAt(index++));
}
static void ExpectLabelAndType(const char* expected_label,
MenuModel::ItemType expected_type,
const MenuModel& menu,
int index) {
EXPECT_EQ(expected_type, menu.GetTypeAt(index));
EXPECT_EQ(base::UTF8ToUTF16(expected_label), menu.GetLabelAt(index));
}
static void VerifyMenuForSeparatorsTest(const MenuModel& menu) {
int index = 0;
ASSERT_EQ(11, menu.GetItemCount());
ExpectLabelAndType("radio1", MenuModel::TYPE_RADIO, menu, index++);
ExpectLabelAndType("radio2", MenuModel::TYPE_RADIO, menu, index++);
EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
ExpectLabelAndType("normal1", MenuModel::TYPE_COMMAND, menu, index++);
EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
ExpectLabelAndType("normal2", MenuModel::TYPE_COMMAND, menu, index++);
EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
ExpectLabelAndType("radio3", MenuModel::TYPE_RADIO, menu, index++);
ExpectLabelAndType("radio4", MenuModel::TYPE_RADIO, menu, index++);
EXPECT_EQ(MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(index++));
ExpectLabelAndType("normal3", MenuModel::TYPE_COMMAND, menu, index++);
}
#if defined(OS_WIN)
#define MAYBE_Separators DISABLED_Separators
#else
#define MAYBE_Separators Separators
#endif
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Separators) {
ASSERT_TRUE(LoadContextMenuExtension("separators"));
const extensions::Extension* extension = GetExtensionNamed("Separators Test");
ASSERT_TRUE(extension != NULL);
ExtensionTestMessageListener listener1("test1 create finished", false);
ui_test_utils::NavigateToURL(browser(),
GURL(extension->GetResourceURL("test1.html")));
listener1.WaitUntilSatisfied();
GURL url("http://www.google.com/");
scoped_ptr<TestRenderViewContextMenu> menu(
TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL()));
MenuModel* model = NULL;
int index = 0;
base::string16 label;
ASSERT_TRUE(menu->GetMenuModelAndItemIndex(
IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index));
EXPECT_EQ(base::UTF8ToUTF16(extension->name()), model->GetLabelAt(index));
ASSERT_EQ(MenuModel::TYPE_SUBMENU, model->GetTypeAt(index));
MenuModel* submenu = model->GetSubmenuModelAt(index);
ASSERT_TRUE(submenu != NULL);
VerifyMenuForSeparatorsTest(*submenu);
ExtensionTestMessageListener listener2("test2 create finished", false);
ui_test_utils::NavigateToURL(browser(),
GURL(extension->GetResourceURL("test2.html")));
listener2.WaitUntilSatisfied();
menu.reset(
TestRenderViewContextMenu::Create(GetWebContents(), url, GURL(), GURL()));
ASSERT_TRUE(menu->GetMenuModelAndItemIndex(
IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST, &model, &index));
EXPECT_EQ(base::UTF8ToUTF16("parent"), model->GetLabelAt(index));
submenu = model->GetSubmenuModelAt(index);
ASSERT_TRUE(submenu != NULL);
VerifyMenuForSeparatorsTest(*submenu);
}
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, TargetURLs) {
ExtensionTestMessageListener listener("created items", false);
ASSERT_TRUE(LoadContextMenuExtension("target_urls"));
ASSERT_TRUE(listener.WaitUntilSatisfied());
GURL google_url("http://www.google.com");
GURL non_google_url("http://www.foo.com");
ASSERT_FALSE(MenuHasItemWithLabel(
google_url, GURL(), GURL(), std::string("item1")));
ASSERT_TRUE(MenuHasItemWithLabel(google_url,
google_url,
GURL(),
std::string("item1")));
ASSERT_FALSE(MenuHasItemWithLabel(google_url,
non_google_url,
GURL(),
std::string("item1")));
}
#if defined(OS_LINUX)
#define MAYBE_IncognitoSplit DISABLED_IncognitoSplit
#else
#define MAYBE_IncognitoSplit IncognitoSplit
#endif
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, MAYBE_IncognitoSplit) {
ExtensionTestMessageListener created("created item regular", false);
ExtensionTestMessageListener created_incognito("created item incognito",
false);
ExtensionTestMessageListener onclick("onclick fired regular", false);
ExtensionTestMessageListener onclick_incognito("onclick fired incognito",
false);
Browser* browser_incognito = ui_test_utils::OpenURLOffTheRecord(
browser()->profile(), GURL("about:blank"));
ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito"));
ASSERT_TRUE(created.WaitUntilSatisfied());
ASSERT_TRUE(created_incognito.WaitUntilSatisfied());
GURL page_url("http://www.google.com");
scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create(
GetWebContents(), page_url, GURL(), GURL()));
WebContents* incognito_web_contents =
browser_incognito->tab_strip_model()->GetActiveWebContents();
scoped_ptr<TestRenderViewContextMenu> menu_incognito(
TestRenderViewContextMenu::Create(
incognito_web_contents, page_url, GURL(), GURL()));
int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
ASSERT_TRUE(menu->IsCommandIdEnabled(command_id));
menu->ExecuteCommand(command_id, 0);
ASSERT_TRUE(onclick.WaitUntilSatisfied());
EXPECT_FALSE(onclick_incognito.was_satisfied());
ASSERT_TRUE(menu_incognito->IsCommandIdEnabled(command_id));
menu_incognito->ExecuteCommand(command_id, 0);
ASSERT_TRUE(onclick_incognito.WaitUntilSatisfied());
}
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Frames) {
ExtensionTestMessageListener listener("created items", false);
ASSERT_TRUE(LoadContextMenuExtension("frames"));
ASSERT_TRUE(listener.WaitUntilSatisfied());
GURL page_url("http://www.google.com");
GURL no_frame_url;
GURL frame_url("http://www.google.com");
ASSERT_TRUE(MenuHasItemWithLabel(
page_url, GURL(), no_frame_url, std::string("Page item")));
ASSERT_FALSE(MenuHasItemWithLabel(
page_url, GURL(), no_frame_url, std::string("Frame item")));
ASSERT_TRUE(MenuHasItemWithLabel(
page_url, GURL(), frame_url, std::string("Page item")));
ASSERT_TRUE(MenuHasItemWithLabel(
page_url, GURL(), frame_url, std::string("Frame item")));
}
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Enabled) {
TestEnabledContextMenu(true);
TestEnabledContextMenu(false);
}
class ExtensionContextMenuBrowserLazyTest :
public ExtensionContextMenuBrowserTest {
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
ExtensionContextMenuBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(
extensions::switches::kEventPageIdleTime, "1");
command_line->AppendSwitchASCII(
extensions::switches::kEventPageSuspendingTime, "0");
}
};
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserLazyTest, EventPage) {
GURL about_blank("about:blank");
LazyBackgroundObserver page_complete;
const extensions::Extension* extension = LoadContextMenuExtension(
"event_page");
ASSERT_TRUE(extension);
page_complete.Wait();
ASSERT_TRUE(MenuHasItemWithLabel(
about_blank, GURL(), GURL(), std::string("Item 1")));
ASSERT_TRUE(MenuHasItemWithLabel(
about_blank, GURL(), GURL(), std::string("Checkbox 1")));
LazyBackgroundObserver checkbox_checked;
scoped_ptr<TestRenderViewContextMenu> menu(TestRenderViewContextMenu::Create(
GetWebContents(), about_blank, GURL(), GURL()));
MenuItem::Id id(false, MenuItem::ExtensionKey(extension->id()));
id.string_uid = "checkbox1";
int command_id = -1;
ASSERT_TRUE(FindCommandId(menu.get(), id, &command_id));
EXPECT_FALSE(menu->IsCommandIdChecked(command_id));
ExtensionTestMessageListener listener("onClicked fired for checkbox1", false);
menu->ExecuteCommand(command_id, 0);
checkbox_checked.WaitUntilClosed();
EXPECT_TRUE(menu->IsCommandIdChecked(command_id));
ASSERT_TRUE(listener.WaitUntilSatisfied());
}
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest,
IncognitoSplitContextMenuCount) {
ExtensionTestMessageListener created("created item regular", false);
ExtensionTestMessageListener created_incognito("created item incognito",
false);
ASSERT_TRUE(browser()->profile()->GetOffTheRecordProfile());
ASSERT_TRUE(LoadContextMenuExtensionIncognito("incognito"));
ASSERT_TRUE(created.WaitUntilSatisfied());
ASSERT_TRUE(created_incognito.WaitUntilSatisfied());
ASSERT_EQ(2u, GetItems().size());
browser()->profile()->DestroyOffTheRecordProfile();
ASSERT_EQ(1u, GetItems().size());
}