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#ifndef CHROME_BROWSER_APPS_APP_BROWSERTEST_UTIL_H_
6#define CHROME_BROWSER_APPS_APP_BROWSERTEST_UTIL_H_
7
8#include "chrome/browser/extensions/extension_apitest.h"
9#include "extensions/browser/app_window/app_window.h"
10
11namespace base {
12class CommandLine;
13}
14
15namespace content {
16class WebContents;
17}
18
19class Browser;
20class ExtensionTestMessageListener;
21
22namespace extensions {
23class Extension;
24
25class PlatformAppBrowserTest : public ExtensionApiTest {
26 public:
27  PlatformAppBrowserTest();
28
29  virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE;
30
31  // Gets the first app window that is found for a given browser.
32  static AppWindow* GetFirstAppWindowForBrowser(Browser* browser);
33
34 protected:
35  // Runs the app named |name| out of the platform_apps subdirectory. Waits
36  // for the provided listener to be satisifed.
37  const Extension* LoadAndLaunchPlatformApp(
38      const char* name,
39      ExtensionTestMessageListener* listener);
40
41  // Runs the app named |name| out of the platform_apps subdirectory. Waits
42  // until the given message is received from the app.
43  const Extension* LoadAndLaunchPlatformApp(const char* name,
44                                            const std::string& message);
45
46  // Installs the app named |name| out of the platform_apps subdirectory.
47  const Extension* InstallPlatformApp(const char* name);
48
49  // Installs and runs the app named |name| out of the platform_apps
50  // subdirectory. Waits until it is launched.
51  const Extension* InstallAndLaunchPlatformApp(const char* name);
52
53  // Launch the given platform app.
54  virtual void LaunchPlatformApp(const Extension* extension);
55
56  // Gets the WebContents associated with the first app window that is found
57  // (most tests only deal with one platform app window, so this is good
58  // enough).
59  content::WebContents* GetFirstAppWindowWebContents();
60
61  // Gets the first app window that is found (most tests only deal with one
62  // platform app window, so this is good enough).
63  AppWindow* GetFirstAppWindow();
64
65  // Gets the first app window for an app.
66  AppWindow* GetFirstAppWindowForApp(const std::string& app_id);
67
68  // Runs chrome.windows.getAll for the given extension and returns the number
69  // of windows that the function returns.
70  size_t RunGetWindowsFunctionForExtension(const Extension* extension);
71
72  // Runs chrome.windows.get(|window_id|) for the the given extension and
73  // returns whether or not a window was found.
74  bool RunGetWindowFunctionForExtension(int window_id,
75                                        const Extension* extension);
76
77  // Returns the number of app windows.
78  size_t GetAppWindowCount();
79
80  // Returns the number of app windows for a specific app.
81  size_t GetAppWindowCountForApp(const std::string& app_id);
82
83  // The command line already has an argument on it - about:blank, which
84  // is set by InProcessBrowserTest::PrepareTestCommandLine. For platform app
85  // launch tests we need to clear this.
86  void ClearCommandLineArgs();
87
88  // Sets up the command line for running platform apps.
89  void SetCommandLineArg(const std::string& test_file);
90
91  // Creates an empty app window for |extension|.
92  AppWindow* CreateAppWindow(const Extension* extension);
93
94  AppWindow* CreateAppWindowFromParams(const Extension* extension,
95                                       const AppWindow::CreateParams& params);
96
97  // Closes |window| and waits until it's gone.
98  void CloseAppWindow(AppWindow* window);
99
100  // Call AdjustBoundsToBeVisibleOnScreen of |window|.
101  void CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
102      AppWindow* window,
103      const gfx::Rect& cached_bounds,
104      const gfx::Rect& cached_screen_bounds,
105      const gfx::Rect& current_screen_bounds,
106      const gfx::Size& minimum_size,
107      gfx::Rect* bounds);
108
109  // Load a simple test app and create a window. The window must be closed by
110  // the caller in order to terminate the test - use CloseAppWindow().
111  // |window_create_options| are the options that will be passed to
112  // chrome.app.window.create() in the test app.
113  AppWindow* CreateTestAppWindow(const std::string& window_create_options);
114
115 private:
116  DISALLOW_COPY_AND_ASSIGN(PlatformAppBrowserTest);
117};
118
119class ExperimentalPlatformAppBrowserTest : public PlatformAppBrowserTest {
120 public:
121  virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE;
122};
123
124}  // namespace extensions
125
126#endif  // CHROME_BROWSER_APPS_APP_BROWSERTEST_UTIL_H_
127