Home | History | Annotate | Download | only in panels
      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/command_line.h"
      6 #include "base/path_service.h"
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "chrome/app/chrome_command_ids.h"
      9 #include "chrome/browser/chrome_notification_types.h"
     10 #include "chrome/browser/extensions/extension_browsertest.h"
     11 #include "chrome/browser/profiles/profile.h"
     12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
     13 #include "chrome/browser/ui/browser.h"
     14 #include "chrome/browser/ui/panels/native_panel.h"
     15 #include "chrome/browser/ui/panels/panel.h"
     16 #include "chrome/browser/ui/panels/panel_constants.h"
     17 #include "chrome/browser/ui/panels/panel_manager.h"
     18 #include "chrome/browser/web_applications/web_app.h"
     19 #include "chrome/common/chrome_paths.h"
     20 #include "chrome/common/chrome_switches.h"
     21 #include "chrome/test/base/ui_test_utils.h"
     22 #include "content/public/browser/web_contents.h"
     23 #include "content/public/test/test_utils.h"
     24 #include "extensions/common/extension.h"
     25 #include "extensions/test/extension_test_message_listener.h"
     26 #include "testing/gtest/include/gtest/gtest.h"
     27 
     28 #if defined(OS_MACOSX)
     29 #include "base/mac/scoped_nsautorelease_pool.h"
     30 #endif
     31 
     32 using extensions::Extension;
     33 
     34 class PanelExtensionBrowserTest : public ExtensionBrowserTest {
     35  protected:
     36   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     37     ExtensionBrowserTest::SetUpCommandLine(command_line);
     38     command_line->AppendSwitch(switches::kEnablePanels);
     39     PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
     40     test_data_dir_ = test_data_dir_.AppendASCII("panels");
     41   }
     42 
     43   Panel* CreatePanelFromExtension(const Extension* extension) const {
     44 #if defined(OS_MACOSX)
     45     // Opening panels on a Mac causes NSWindowController of the Panel window
     46     // to be autoreleased. We need a pool drained after it's done so the test
     47     // can close correctly. The NSWindowController of the Panel window controls
     48     // lifetime of the Panel object so we want to release it as soon as
     49     // possible. In real Chrome, this is done by message pump.
     50     // On non-Mac platform, this is an empty class.
     51     base::mac::ScopedNSAutoreleasePool autorelease_pool;
     52 #endif
     53 
     54     Panel* panel = PanelManager::GetInstance()->CreatePanel(
     55         web_app::GenerateApplicationNameFromExtensionId(extension->id()),
     56         browser()->profile(),
     57         GURL(),
     58         gfx::Rect(),
     59         PanelManager::CREATE_AS_DETACHED);
     60     panel->ShowInactive();
     61     return panel;
     62   }
     63 
     64   void WaitForAppIconAvailable(Panel* panel) const {
     65     content::WindowedNotificationObserver signal(
     66         chrome::NOTIFICATION_PANEL_APP_ICON_LOADED,
     67         content::Source<Panel>(panel));
     68     if (!panel->app_icon().IsEmpty())
     69       return;
     70     signal.Wait();
     71     EXPECT_FALSE(panel->app_icon().IsEmpty());
     72   }
     73 
     74   static NativePanelTesting* CreateNativePanelTesting(Panel* panel) {
     75     return panel->native_panel()->CreateNativePanelTesting();
     76   }
     77 };
     78 
     79 // TODO(jschuh): Hanging plugin tests. crbug.com/244653
     80 #if !defined(OS_WIN) && !defined(ARCH_CPU_X86_64)
     81 IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest, PanelAppIcon) {
     82   const Extension* extension =
     83       LoadExtension(test_data_dir_.AppendASCII("test_extension"));
     84   Panel* panel = CreatePanelFromExtension(extension);
     85 
     86   // Wait for the app icon gets fully loaded.
     87   WaitForAppIconAvailable(panel);
     88 
     89   // First verify on the panel level.
     90   gfx::ImageSkia app_icon = panel->app_icon().AsImageSkia();
     91   EXPECT_EQ(panel::kPanelAppIconSize, app_icon.width());
     92   EXPECT_EQ(panel::kPanelAppIconSize, app_icon.height());
     93 
     94   // Then verify on the native panel level.
     95 #if !defined(OS_WIN) || !defined(USE_AURA)
     96   scoped_ptr<NativePanelTesting> native_panel_testing(
     97       CreateNativePanelTesting(panel));
     98   EXPECT_TRUE(native_panel_testing->VerifyAppIcon());
     99 #endif
    100 
    101   panel->Close();
    102 }
    103 #endif
    104 
    105 IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest,
    106                        ClosePanelBeforeIconLoadingCompleted) {
    107   const Extension* extension =
    108       LoadExtension(test_data_dir_.AppendASCII("test_extension"));
    109   Panel* panel = CreatePanelFromExtension(extension);
    110 
    111   // Close tha panel without waiting for the app icon loaded.
    112   panel->Close();
    113 }
    114 
    115 // Non-abstract RenderViewContextMenu class for testing context menus in Panels.
    116 class PanelContextMenu : public RenderViewContextMenu {
    117  public:
    118   PanelContextMenu(content::RenderFrameHost* render_frame_host,
    119                    const content::ContextMenuParams& params)
    120       : RenderViewContextMenu(render_frame_host, params) {}
    121 
    122   bool HasCommandWithId(int command_id) {
    123     return menu_model_.GetIndexOfCommandId(command_id) != -1;
    124   }
    125 
    126  protected:
    127   // RenderViewContextMenu implementation.
    128   virtual bool GetAcceleratorForCommandId(
    129       int command_id,
    130       ui::Accelerator* accelerator) OVERRIDE {
    131     return false;
    132   }
    133 };
    134 
    135 IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest, BasicContextMenu) {
    136   ExtensionTestMessageListener listener("panel loaded", false);
    137   LoadExtension(test_data_dir_.AppendASCII("basic"));
    138   ASSERT_TRUE(listener.WaitUntilSatisfied());
    139 
    140   // There should only be one panel.
    141   PanelManager* panel_manager = PanelManager::GetInstance();
    142   EXPECT_EQ(1, panel_manager->num_panels());
    143   Panel* panel = panel_manager->panels().front();
    144   content::WebContents* web_contents = panel->GetWebContents();
    145   ASSERT_TRUE(web_contents);
    146 
    147   // Verify basic menu contents. The basic extension does not add any
    148   // context menu items so the panel's menu should include only the
    149   // developer tools.
    150   {
    151     content::ContextMenuParams params;
    152     params.page_url = web_contents->GetURL();
    153     // Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
    154     EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
    155 
    156     scoped_ptr<PanelContextMenu> menu(
    157         new PanelContextMenu(web_contents->GetMainFrame(), params));
    158     menu->Init();
    159 
    160     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
    161     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
    162     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE));
    163     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
    164     EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK));
    165     EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
    166     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
    167   }
    168 
    169   // Verify expected menu contents for editable item.
    170   {
    171     content::ContextMenuParams params;
    172     params.is_editable = true;
    173     params.page_url = web_contents->GetURL();
    174     // Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
    175     EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
    176 
    177     scoped_ptr<PanelContextMenu> menu(
    178         new PanelContextMenu(web_contents->GetMainFrame(), params));
    179     menu->Init();
    180 
    181     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
    182     EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
    183     EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE));
    184     EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
    185     EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK));
    186     EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
    187     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
    188   }
    189 
    190   // Verify expected menu contents for text selection.
    191   {
    192     content::ContextMenuParams params;
    193     params.page_url = web_contents->GetURL();
    194     params.selection_text = base::ASCIIToUTF16("Select me");
    195     // Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
    196     EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
    197 
    198     scoped_ptr<PanelContextMenu> menu(
    199         new PanelContextMenu(web_contents->GetMainFrame(), params));
    200     menu->Init();
    201 
    202     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
    203     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
    204     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE));
    205     EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
    206     EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK));
    207     EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
    208     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
    209   }
    210 
    211   // Verify expected menu contexts for a link.
    212   {
    213     content::ContextMenuParams params;
    214     params.page_url = web_contents->GetURL();
    215     params.unfiltered_link_url = GURL("http://google.com/");
    216     // Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
    217     EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
    218 
    219     scoped_ptr<PanelContextMenu> menu(
    220         new PanelContextMenu(web_contents->GetMainFrame(), params));
    221     menu->Init();
    222 
    223     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
    224     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
    225     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE));
    226     EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
    227     EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK));
    228     EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
    229     EXPECT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
    230   }
    231 }
    232 
    233 IN_PROC_BROWSER_TEST_F(PanelExtensionBrowserTest, CustomContextMenu) {
    234   ExtensionTestMessageListener listener("created item", false);
    235   LoadExtension(test_data_dir_.AppendASCII("context_menu"));
    236   ASSERT_TRUE(listener.WaitUntilSatisfied());
    237 
    238   // Load a second extension that also creates a custom context menu item.
    239   ExtensionTestMessageListener bogey_listener("created bogey item", false);
    240   LoadExtension(test_data_dir_.AppendASCII("context_menu2"));
    241   ASSERT_TRUE(bogey_listener.WaitUntilSatisfied());
    242 
    243   // There should only be one panel.
    244   PanelManager* panel_manager = PanelManager::GetInstance();
    245   EXPECT_EQ(1, panel_manager->num_panels());
    246   Panel* panel = panel_manager->panels().front();
    247   content::WebContents* web_contents = panel->GetWebContents();
    248   ASSERT_TRUE(web_contents);
    249 
    250   content::ContextMenuParams params;
    251   params.page_url = web_contents->GetURL();
    252 
    253   // Ensure context menu isn't swallowed by WebContentsDelegate (the panel).
    254   EXPECT_FALSE(web_contents->GetDelegate()->HandleContextMenu(params));
    255 
    256   // Verify menu contents contains the custom item added by their own extension.
    257   scoped_ptr<PanelContextMenu> menu;
    258   menu.reset(new PanelContextMenu(web_contents->GetMainFrame(), params));
    259   menu->Init();
    260   EXPECT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
    261   EXPECT_FALSE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1));
    262   EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
    263   EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO));
    264   EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_PASTE));
    265   EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY));
    266   EXPECT_FALSE(menu->HasCommandWithId(IDC_BACK));
    267   EXPECT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
    268   EXPECT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
    269 
    270   // Execute the extension's custom menu item and wait for the extension's
    271   // script to tell us its onclick fired.
    272   ExtensionTestMessageListener onclick_listener("clicked", false);
    273   int command_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
    274   ASSERT_TRUE(menu->IsCommandIdEnabled(command_id));
    275   menu->ExecuteCommand(command_id, 0);
    276   EXPECT_TRUE(onclick_listener.WaitUntilSatisfied());
    277 }
    278