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 
      7 #include "apps/app_window_contents.h"
      8 #include "apps/app_window_registry.h"
      9 #include "apps/ui/native_app_window.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/extensions/extension_test_message_listener.h"
     15 #include "chrome/browser/ui/apps/chrome_app_window_delegate.h"
     16 #include "chrome/browser/ui/browser.h"
     17 #include "chrome/browser/ui/extensions/application_launch.h"
     18 #include "content/public/browser/notification_service.h"
     19 #include "content/public/test/browser_test_utils.h"
     20 #include "content/public/test/test_utils.h"
     21 #include "extensions/common/switches.h"
     22 
     23 using apps::AppWindow;
     24 using apps::AppWindowRegistry;
     25 using content::WebContents;
     26 
     27 namespace {
     28 
     29 const char kAppWindowTestApp[] = "app_window/generic";
     30 
     31 }  // namespace
     32 
     33 namespace utils = extension_function_test_utils;
     34 
     35 namespace extensions {
     36 
     37 PlatformAppBrowserTest::PlatformAppBrowserTest() {
     38   ChromeAppWindowDelegate::DisableExternalOpenForTesting();
     39 }
     40 
     41 void PlatformAppBrowserTest::SetUpCommandLine(CommandLine* command_line) {
     42   // Skips ExtensionApiTest::SetUpCommandLine.
     43   ExtensionBrowserTest::SetUpCommandLine(command_line);
     44 
     45   // Make event pages get suspended quicker.
     46   command_line->AppendSwitchASCII(switches::kEventPageIdleTime, "1000");
     47   command_line->AppendSwitchASCII(switches::kEventPageSuspendingTime, "1000");
     48 }
     49 
     50 // static
     51 AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForBrowser(
     52     Browser* browser) {
     53   AppWindowRegistry* app_registry = AppWindowRegistry::Get(browser->profile());
     54   const AppWindowRegistry::AppWindowList& app_windows =
     55       app_registry->app_windows();
     56 
     57   AppWindowRegistry::const_iterator iter = app_windows.begin();
     58   if (iter != app_windows.end())
     59     return *iter;
     60 
     61   return NULL;
     62 }
     63 
     64 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
     65     const char* name,
     66     ExtensionTestMessageListener* listener) {
     67   DCHECK(listener);
     68   const Extension* extension = LoadExtension(
     69       test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
     70   EXPECT_TRUE(extension);
     71 
     72   LaunchPlatformApp(extension);
     73 
     74   EXPECT_TRUE(listener->WaitUntilSatisfied()) << "'" << listener->message()
     75                                               << "' message was not receieved";
     76 
     77   return extension;
     78 }
     79 
     80 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
     81     const char* name,
     82     const std::string& message) {
     83   ExtensionTestMessageListener launched_listener(message, false);
     84   const Extension* extension =
     85       LoadAndLaunchPlatformApp(name, &launched_listener);
     86 
     87   return extension;
     88 }
     89 
     90 const Extension* PlatformAppBrowserTest::InstallPlatformApp(
     91     const char* name) {
     92   const Extension* extension = InstallExtension(
     93       test_data_dir_.AppendASCII("platform_apps").AppendASCII(name), 1);
     94   EXPECT_TRUE(extension);
     95 
     96   return extension;
     97 }
     98 
     99 const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
    100     const char* name) {
    101   content::WindowedNotificationObserver app_loaded_observer(
    102       content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
    103       content::NotificationService::AllSources());
    104 
    105   const Extension* extension = InstallPlatformApp(name);
    106 
    107   LaunchPlatformApp(extension);
    108 
    109   app_loaded_observer.Wait();
    110 
    111   return extension;
    112 }
    113 
    114 void PlatformAppBrowserTest::LaunchPlatformApp(const Extension* extension) {
    115   OpenApplication(AppLaunchParams(
    116       browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW));
    117 }
    118 
    119 WebContents* PlatformAppBrowserTest::GetFirstAppWindowWebContents() {
    120   AppWindow* window = GetFirstAppWindow();
    121   if (window)
    122     return window->web_contents();
    123 
    124   return NULL;
    125 }
    126 
    127 AppWindow* PlatformAppBrowserTest::GetFirstAppWindow() {
    128   return GetFirstAppWindowForBrowser(browser());
    129 }
    130 
    131 apps::AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForApp(
    132     const std::string& app_id) {
    133   AppWindowRegistry* app_registry =
    134       AppWindowRegistry::Get(browser()->profile());
    135   const AppWindowRegistry::AppWindowList& app_windows =
    136       app_registry->GetAppWindowsForApp(app_id);
    137 
    138   AppWindowRegistry::const_iterator iter = app_windows.begin();
    139   if (iter != app_windows.end())
    140     return *iter;
    141 
    142   return NULL;
    143 }
    144 
    145 size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension(
    146     const Extension* extension) {
    147   scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction();
    148   function->set_extension(extension);
    149   scoped_ptr<base::ListValue> result(utils::ToList(
    150       utils::RunFunctionAndReturnSingleResult(function.get(),
    151                                               "[]",
    152                                               browser())));
    153   return result->GetSize();
    154 }
    155 
    156 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
    157     int window_id,
    158     const Extension* extension) {
    159   scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
    160   function->set_extension(extension);
    161   utils::RunFunction(
    162           function.get(),
    163           base::StringPrintf("[%u]", window_id),
    164           browser(),
    165           utils::NONE);
    166   return function->GetResultList() != NULL;
    167 }
    168 
    169 size_t PlatformAppBrowserTest::GetAppWindowCount() {
    170   return AppWindowRegistry::Get(browser()->profile())->app_windows().size();
    171 }
    172 
    173 size_t PlatformAppBrowserTest::GetAppWindowCountForApp(
    174     const std::string& app_id) {
    175   return AppWindowRegistry::Get(browser()->profile())
    176       ->GetAppWindowsForApp(app_id)
    177       .size();
    178 }
    179 
    180 void PlatformAppBrowserTest::ClearCommandLineArgs() {
    181   CommandLine* command_line = CommandLine::ForCurrentProcess();
    182   CommandLine::StringVector args = command_line->GetArgs();
    183   CommandLine::StringVector argv = command_line->argv();
    184   for (size_t i = 0; i < args.size(); i++)
    185     argv.pop_back();
    186   command_line->InitFromArgv(argv);
    187 }
    188 
    189 void PlatformAppBrowserTest::SetCommandLineArg(const std::string& test_file) {
    190   ClearCommandLineArgs();
    191   CommandLine* command_line = CommandLine::ForCurrentProcess();
    192   base::FilePath test_doc(test_data_dir_.AppendASCII(test_file));
    193   test_doc = test_doc.NormalizePathSeparators();
    194   command_line->AppendArgPath(test_doc);
    195 }
    196 
    197 AppWindow* PlatformAppBrowserTest::CreateAppWindow(const Extension* extension) {
    198   return CreateAppWindowFromParams(extension, AppWindow::CreateParams());
    199 }
    200 
    201 AppWindow* PlatformAppBrowserTest::CreateAppWindowFromParams(
    202     const Extension* extension,
    203     const AppWindow::CreateParams& params) {
    204   AppWindow* window = new AppWindow(
    205       browser()->profile(), new ChromeAppWindowDelegate(), extension);
    206   window->Init(
    207       GURL(std::string()), new apps::AppWindowContentsImpl(window), params);
    208   return window;
    209 }
    210 
    211 void PlatformAppBrowserTest::CloseAppWindow(AppWindow* window) {
    212   content::WebContentsDestroyedWatcher destroyed_watcher(
    213       window->web_contents());
    214   window->GetBaseWindow()->Close();
    215   destroyed_watcher.Wait();
    216 }
    217 
    218 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
    219     AppWindow* window,
    220     const gfx::Rect& cached_bounds,
    221     const gfx::Rect& cached_screen_bounds,
    222     const gfx::Rect& current_screen_bounds,
    223     const gfx::Size& minimum_size,
    224     gfx::Rect* bounds) {
    225   window->AdjustBoundsToBeVisibleOnScreen(cached_bounds,
    226                                           cached_screen_bounds,
    227                                           current_screen_bounds,
    228                                           minimum_size,
    229                                           bounds);
    230 }
    231 
    232 apps::AppWindow* PlatformAppBrowserTest::CreateTestAppWindow(
    233     const std::string& window_create_options) {
    234   ExtensionTestMessageListener launched_listener("launched", true);
    235   ExtensionTestMessageListener loaded_listener("window_loaded", false);
    236 
    237   // Load and launch the test app.
    238   const Extension* extension =
    239       LoadAndLaunchPlatformApp(kAppWindowTestApp, &launched_listener);
    240   EXPECT_TRUE(extension);
    241   EXPECT_TRUE(launched_listener.WaitUntilSatisfied());
    242 
    243   // Send the options for window creation.
    244   launched_listener.Reply(window_create_options);
    245 
    246   // Wait for the window to be opened and loaded.
    247   EXPECT_TRUE(loaded_listener.WaitUntilSatisfied());
    248 
    249   EXPECT_EQ(1U, GetAppWindowCount());
    250   AppWindow* app_window = GetFirstAppWindow();
    251   return app_window;
    252 }
    253 
    254 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
    255     CommandLine* command_line) {
    256   PlatformAppBrowserTest::SetUpCommandLine(command_line);
    257   command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
    258 }
    259 
    260 }  // namespace extensions
    261