Home | History | Annotate | Download | only in extensions
      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 "chrome/browser/extensions/platform_app_browsertest_util.h"
      6 
      7 #include "apps/app_window_contents.h"
      8 #include "apps/native_app_window.h"
      9 #include "apps/shell_window_registry.h"
     10 #include "base/command_line.h"
     11 #include "base/strings/stringprintf.h"
     12 #include "chrome/browser/extensions/api/tabs/tabs_api.h"
     13 #include "chrome/browser/extensions/extension_function_test_utils.h"
     14 #include "chrome/browser/ui/apps/chrome_shell_window_delegate.h"
     15 #include "chrome/browser/ui/browser.h"
     16 #include "chrome/browser/ui/extensions/application_launch.h"
     17 #include "chrome/common/chrome_switches.h"
     18 #include "content/public/browser/notification_service.h"
     19 #include "content/public/test/test_utils.h"
     20 #include "extensions/common/switches.h"
     21 
     22 using apps::ShellWindow;
     23 using apps::ShellWindowRegistry;
     24 using content::WebContents;
     25 
     26 namespace utils = extension_function_test_utils;
     27 
     28 namespace extensions {
     29 
     30 PlatformAppBrowserTest::PlatformAppBrowserTest() {
     31   ChromeShellWindowDelegate::DisableExternalOpenForTesting();
     32 }
     33 
     34 void PlatformAppBrowserTest::SetUpCommandLine(CommandLine* command_line) {
     35   // Skips ExtensionApiTest::SetUpCommandLine.
     36   ExtensionBrowserTest::SetUpCommandLine(command_line);
     37 
     38   // Make event pages get suspended quicker.
     39   command_line->AppendSwitchASCII(::switches::kEventPageIdleTime, "1");
     40   command_line->AppendSwitchASCII(::switches::kEventPageSuspendingTime, "1");
     41 }
     42 
     43 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
     44     const char* name) {
     45   content::WindowedNotificationObserver app_loaded_observer(
     46       content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
     47       content::NotificationService::AllSources());
     48 
     49   const Extension* extension = LoadExtension(
     50       test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
     51   EXPECT_TRUE(extension);
     52 
     53   chrome::OpenApplication(chrome::AppLaunchParams(browser()->profile(),
     54                                                   extension,
     55                                                   extension_misc::LAUNCH_NONE,
     56                                                   NEW_WINDOW));
     57 
     58   app_loaded_observer.Wait();
     59 
     60   return extension;
     61 }
     62 
     63 const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
     64     const char* name) {
     65   content::WindowedNotificationObserver app_loaded_observer(
     66       content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
     67       content::NotificationService::AllSources());
     68 
     69   const Extension* extension = InstallExtension(
     70       test_data_dir_.AppendASCII("platform_apps").AppendASCII(name), 1);
     71   EXPECT_TRUE(extension);
     72 
     73   chrome::OpenApplication(chrome::AppLaunchParams(browser()->profile(),
     74                                                   extension,
     75                                                   extension_misc::LAUNCH_NONE,
     76                                                   NEW_WINDOW));
     77 
     78   app_loaded_observer.Wait();
     79 
     80   return extension;
     81 }
     82 
     83 WebContents* PlatformAppBrowserTest::GetFirstShellWindowWebContents() {
     84   ShellWindow* window = GetFirstShellWindow();
     85   if (window)
     86     return window->web_contents();
     87 
     88   return NULL;
     89 }
     90 
     91 ShellWindow* PlatformAppBrowserTest::GetFirstShellWindow() {
     92   ShellWindowRegistry* app_registry =
     93       ShellWindowRegistry::Get(browser()->profile());
     94   const ShellWindowRegistry::ShellWindowList& shell_windows =
     95       app_registry->shell_windows();
     96 
     97   ShellWindowRegistry::const_iterator iter = shell_windows.begin();
     98   if (iter != shell_windows.end())
     99     return *iter;
    100 
    101   return NULL;
    102 }
    103 
    104 size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension(
    105     const Extension* extension) {
    106   scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction();
    107   function->set_extension(extension);
    108   scoped_ptr<base::ListValue> result(utils::ToList(
    109       utils::RunFunctionAndReturnSingleResult(function.get(),
    110                                               "[]",
    111                                               browser())));
    112   return result->GetSize();
    113 }
    114 
    115 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
    116     int window_id,
    117     const Extension* extension) {
    118   scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
    119   function->set_extension(extension);
    120   utils::RunFunction(
    121           function.get(),
    122           base::StringPrintf("[%u]", window_id),
    123           browser(),
    124           utils::NONE);
    125   return function->GetResultList() != NULL;
    126 }
    127 
    128 size_t PlatformAppBrowserTest::GetShellWindowCount() {
    129   return ShellWindowRegistry::Get(browser()->profile())->
    130       shell_windows().size();
    131 }
    132 
    133 void PlatformAppBrowserTest::ClearCommandLineArgs() {
    134   CommandLine* command_line = CommandLine::ForCurrentProcess();
    135   CommandLine::StringVector args = command_line->GetArgs();
    136   CommandLine::StringVector argv = command_line->argv();
    137   for (size_t i = 0; i < args.size(); i++)
    138     argv.pop_back();
    139   command_line->InitFromArgv(argv);
    140 }
    141 
    142 void PlatformAppBrowserTest::SetCommandLineArg(const std::string& test_file) {
    143   ClearCommandLineArgs();
    144   CommandLine* command_line = CommandLine::ForCurrentProcess();
    145   base::FilePath test_doc(test_data_dir_.AppendASCII(test_file));
    146   test_doc = test_doc.NormalizePathSeparators();
    147   command_line->AppendArgPath(test_doc);
    148 }
    149 
    150 ShellWindow* PlatformAppBrowserTest::CreateShellWindow(
    151     const Extension* extension) {
    152   return CreateShellWindowFromParams(extension, ShellWindow::CreateParams());
    153 }
    154 
    155 ShellWindow* PlatformAppBrowserTest::CreateShellWindowFromParams(
    156     const Extension* extension, const ShellWindow::CreateParams& params) {
    157   ShellWindow* window = new ShellWindow(browser()->profile(),
    158                                         new ChromeShellWindowDelegate(),
    159                                         extension);
    160   window->Init(GURL(std::string()),
    161                new apps::AppWindowContents(window),
    162                params);
    163   return window;
    164 }
    165 
    166 void PlatformAppBrowserTest::CloseShellWindow(ShellWindow* window) {
    167   content::WindowedNotificationObserver destroyed_observer(
    168       content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
    169       content::NotificationService::AllSources());
    170   window->GetBaseWindow()->Close();
    171   destroyed_observer.Wait();
    172 }
    173 
    174 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForShellWindow(
    175     ShellWindow* window,
    176     const gfx::Rect& cached_bounds,
    177     const gfx::Rect& cached_screen_bounds,
    178     const gfx::Rect& current_screen_bounds,
    179     const gfx::Size& minimum_size,
    180     gfx::Rect* bounds) {
    181   window->AdjustBoundsToBeVisibleOnScreen(cached_bounds,
    182                                           cached_screen_bounds,
    183                                           current_screen_bounds,
    184                                           minimum_size,
    185                                           bounds);
    186 }
    187 
    188 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
    189     CommandLine* command_line) {
    190   PlatformAppBrowserTest::SetUpCommandLine(command_line);
    191   command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
    192 }
    193 
    194 }  // namespace extensions
    195