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_PLATFORM_APPS_APP_BROWSERTEST_UTIL_H_
6 #define CHROME_BROWSER_APPS_PLATFORM_APPS_APP_BROWSERTEST_UTIL_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 #include <string>
12 
13 #include "base/macros.h"
14 #include "chrome/browser/extensions/extension_apitest.h"
15 #include "extensions/browser/app_window/app_window.h"
16 
17 #if defined(OS_CHROMEOS)
18 #include "components/media_router/browser/test/mock_media_router.h"
19 #endif
20 
21 namespace base {
22 class CommandLine;
23 }
24 
25 namespace content {
26 class BrowserContext;
27 class WebContents;
28 }  // namespace content
29 
30 class Browser;
31 class ExtensionTestMessageListener;
32 
33 namespace extensions {
34 class Extension;
35 
36 class PlatformAppBrowserTest : public ExtensionApiTest {
37  public:
38   PlatformAppBrowserTest();
39   ~PlatformAppBrowserTest() override;
40 
41   void SetUpCommandLine(base::CommandLine* command_line) override;
42   void SetUpOnMainThread() override;
43   void TearDownOnMainThread() override;
44 
45   // Gets the first app window that is found for a given browser.
46   static AppWindow* GetFirstAppWindowForBrowser(Browser* browser);
47 
48  protected:
49   // Runs the app named |name| out of the platform_apps subdirectory. Waits
50   // for the provided listener to be satisifed.
51   const Extension* LoadAndLaunchPlatformApp(
52       const char* name,
53       ExtensionTestMessageListener* listener);
54 
55   // Runs the app named |name| out of the platform_apps subdirectory. Waits
56   // until the given message is received from the app.
57   const Extension* LoadAndLaunchPlatformApp(const char* name,
58                                             const std::string& message);
59 
60   // Installs the app named |name| out of the platform_apps subdirectory.
61   const Extension* InstallPlatformApp(const char* name);
62 
63   // Installs the sample hosted app.
64   const Extension* InstallHostedApp();
65 
66   // Installs and runs the app named |name| out of the platform_apps
67   // subdirectory. Waits until it is launched.
68   const Extension* InstallAndLaunchPlatformApp(const char* name);
69 
70   // Launch the given platform app.
71   virtual void LaunchPlatformApp(const Extension* extension);
72 
73   // Launch the given hosted app.
74   virtual void LaunchHostedApp(const Extension* extension);
75 
76   // Gets the WebContents associated with the first app window that is found
77   // (most tests only deal with one platform app window, so this is good
78   // enough).
79   content::WebContents* GetFirstAppWindowWebContents();
80 
81   // Gets the first app window that is found (most tests only deal with one
82   // platform app window, so this is good enough).
83   AppWindow* GetFirstAppWindow();
84 
85   // Gets the first app window for an app.
86   AppWindow* GetFirstAppWindowForApp(const std::string& app_id);
87 
88   // Runs chrome.windows.getAll for the given extension and returns the number
89   // of windows that the function returns.
90   size_t RunGetWindowsFunctionForExtension(const Extension* extension);
91 
92   // Runs chrome.windows.get(|window_id|) for the the given extension and
93   // returns whether or not a window was found.
94   bool RunGetWindowFunctionForExtension(int window_id,
95                                         const Extension* extension);
96 
97   // Returns the number of app windows.
98   size_t GetAppWindowCount();
99 
100   // Returns the number of app windows for a specific app.
101   size_t GetAppWindowCountForApp(const std::string& app_id);
102 
103   // Creates an empty app window for |extension|.
104   AppWindow* CreateAppWindow(content::BrowserContext* context,
105                              const Extension* extension);
106 
107   AppWindow* CreateAppWindowFromParams(content::BrowserContext* context,
108                                        const Extension* extension,
109                                        const AppWindow::CreateParams& params);
110 
111   // Closes |window| and waits until it's gone.
112   void CloseAppWindow(AppWindow* window);
113 
114   // Call AdjustBoundsToBeVisibleOnScreen of |window|.
115   void CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
116       AppWindow* window,
117       const gfx::Rect& cached_bounds,
118       const gfx::Rect& cached_screen_bounds,
119       const gfx::Rect& current_screen_bounds,
120       const gfx::Size& minimum_size,
121       gfx::Rect* bounds);
122 
123   // Load a simple test app and create a window. The window must be closed by
124   // the caller in order to terminate the test - use CloseAppWindow().
125   // |window_create_options| are the options that will be passed to
126   // chrome.app.window.create() in the test app.
127   AppWindow* CreateTestAppWindow(const std::string& window_create_options);
128 
129   // Returns the native app window associated with |window|.
130   NativeAppWindow* GetNativeAppWindowForAppWindow(AppWindow* window);
131 
132  private:
133 #if defined(OS_CHROMEOS)
134   std::unique_ptr<media_router::MockMediaRouter> media_router_;
135 #endif
136 
137   DISALLOW_COPY_AND_ASSIGN(PlatformAppBrowserTest);
138 };
139 
140 class ExperimentalPlatformAppBrowserTest : public PlatformAppBrowserTest {
141  public:
142   void SetUpCommandLine(base::CommandLine* command_line) override;
143 };
144 
145 }  // namespace extensions
146 
147 #endif  // CHROME_BROWSER_APPS_PLATFORM_APPS_APP_BROWSERTEST_UTIL_H_
148