Home | History | Annotate | Download | only in apps
      1 // Copyright 2013 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 "chrome/browser/apps/app_browsertest_util.h"
      6 #include "chrome/browser/extensions/extension_test_message_listener.h"
      7 #include "chrome/browser/profiles/profile.h"
      8 #include "chrome/common/chrome_switches.h"
      9 #include "content/public/test/browser_test_utils.h"
     10 #include "extensions/browser/process_manager.h"
     11 
     12 class WindowControlsTest : public extensions::PlatformAppBrowserTest {
     13  protected:
     14   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     15     extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line);
     16     command_line->AppendSwitch(switches::kEnableAppWindowControls);
     17   }
     18   content::WebContents* GetWebContentsForExtensionWindow(
     19       const extensions::Extension* extension);
     20 };
     21 
     22 content::WebContents* WindowControlsTest::GetWebContentsForExtensionWindow(
     23     const extensions::Extension* extension) {
     24   extensions::ProcessManager* process_manager =
     25       extensions::ExtensionSystem::Get(profile())->process_manager();
     26 
     27   // Lookup render view host for background page.
     28   const extensions::ExtensionHost* extension_host =
     29       process_manager->GetBackgroundHostForExtension(extension->id());
     30   content::RenderViewHost* background_view_host =
     31       extension_host->render_view_host();
     32 
     33   // Go through all active views, looking for the first window of the extension
     34   const extensions::ProcessManager::ViewSet all_views =
     35       process_manager->GetAllViews();
     36   extensions::ProcessManager::ViewSet::const_iterator it = all_views.begin();
     37   for (; it != all_views.end(); ++it) {
     38     content::RenderViewHost* host = *it;
     39 
     40     // Filter out views not part of this extension
     41     if (process_manager->GetExtensionForRenderViewHost(host) == extension) {
     42       // Filter out the background page view
     43       if (host != background_view_host) {
     44         content::WebContents* web_contents =
     45             content::WebContents::FromRenderViewHost(host);
     46         return web_contents;
     47       }
     48     }
     49   }
     50   return NULL;
     51 }
     52 
     53 IN_PROC_BROWSER_TEST_F(WindowControlsTest, CloseControlWorks) {
     54   // Launch app and wait for window to show up
     55   const extensions::Extension* extension =
     56       LoadAndLaunchPlatformApp("window_controls/buttons", "window-opened");
     57 
     58   // Find WebContents of window
     59   content::WebContents* web_contents =
     60       GetWebContentsForExtensionWindow(extension);
     61   ASSERT_TRUE(web_contents != NULL);
     62 
     63   // Send a left click on the "Close" button and wait for the close action
     64   // to happen.
     65   ExtensionTestMessageListener window_closed("window-closed", false);
     66 
     67   // Send mouse click somewhere inside the [x] button
     68   const int controlOffset = 25;
     69   int x = web_contents->GetContainerBounds().size().width() - controlOffset;
     70   int y = controlOffset;
     71   content::SimulateMouseClickAt(web_contents,
     72                                 0,
     73                                 blink::WebMouseEvent::ButtonLeft,
     74                                 gfx::Point(x, y));
     75 
     76   ASSERT_TRUE(window_closed.WaitUntilSatisfied());
     77 }
     78