[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / chrome / browser / apps / app_browsertest_util.cc
blob2290617955d73789ce30481f82e1266fb068243d
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.
5 #include "chrome/browser/apps/app_browsertest_util.h"
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"
23 using apps::AppWindow;
24 using apps::AppWindowRegistry;
25 using content::WebContents;
27 namespace {
29 const char kAppWindowTestApp[] = "app_window/generic";
31 } // namespace
33 namespace utils = extension_function_test_utils;
35 namespace extensions {
37 PlatformAppBrowserTest::PlatformAppBrowserTest() {
38 ChromeAppWindowDelegate::DisableExternalOpenForTesting();
41 void PlatformAppBrowserTest::SetUpCommandLine(CommandLine* command_line) {
42 // Skips ExtensionApiTest::SetUpCommandLine.
43 ExtensionBrowserTest::SetUpCommandLine(command_line);
45 // Make event pages get suspended quicker.
46 command_line->AppendSwitchASCII(switches::kEventPageIdleTime, "1000");
47 command_line->AppendSwitchASCII(switches::kEventPageSuspendingTime, "1000");
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();
57 AppWindowRegistry::const_iterator iter = app_windows.begin();
58 if (iter != app_windows.end())
59 return *iter;
61 return NULL;
64 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
65 const char* name) {
66 content::WindowedNotificationObserver app_loaded_observer(
67 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
68 content::NotificationService::AllSources());
70 const Extension* extension = LoadExtension(
71 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
72 EXPECT_TRUE(extension);
74 LaunchPlatformApp(extension);
76 app_loaded_observer.Wait();
78 return extension;
81 const Extension* PlatformAppBrowserTest::InstallPlatformApp(
82 const char* name) {
83 const Extension* extension = InstallExtension(
84 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name), 1);
85 EXPECT_TRUE(extension);
87 return extension;
90 const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
91 const char* name) {
92 content::WindowedNotificationObserver app_loaded_observer(
93 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
94 content::NotificationService::AllSources());
96 const Extension* extension = InstallPlatformApp(name);
98 LaunchPlatformApp(extension);
100 app_loaded_observer.Wait();
102 return extension;
105 void PlatformAppBrowserTest::LaunchPlatformApp(const Extension* extension) {
106 OpenApplication(AppLaunchParams(
107 browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW));
110 WebContents* PlatformAppBrowserTest::GetFirstAppWindowWebContents() {
111 AppWindow* window = GetFirstAppWindow();
112 if (window)
113 return window->web_contents();
115 return NULL;
118 AppWindow* PlatformAppBrowserTest::GetFirstAppWindow() {
119 return GetFirstAppWindowForBrowser(browser());
122 apps::AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForApp(
123 const std::string& app_id) {
124 AppWindowRegistry* app_registry =
125 AppWindowRegistry::Get(browser()->profile());
126 const AppWindowRegistry::AppWindowList& app_windows =
127 app_registry->GetAppWindowsForApp(app_id);
129 AppWindowRegistry::const_iterator iter = app_windows.begin();
130 if (iter != app_windows.end())
131 return *iter;
133 return NULL;
136 size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension(
137 const Extension* extension) {
138 scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction();
139 function->set_extension(extension);
140 scoped_ptr<base::ListValue> result(utils::ToList(
141 utils::RunFunctionAndReturnSingleResult(function.get(),
142 "[]",
143 browser())));
144 return result->GetSize();
147 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
148 int window_id,
149 const Extension* extension) {
150 scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
151 function->set_extension(extension);
152 utils::RunFunction(
153 function.get(),
154 base::StringPrintf("[%u]", window_id),
155 browser(),
156 utils::NONE);
157 return function->GetResultList() != NULL;
160 size_t PlatformAppBrowserTest::GetAppWindowCount() {
161 return AppWindowRegistry::Get(browser()->profile())->app_windows().size();
164 size_t PlatformAppBrowserTest::GetAppWindowCountForApp(
165 const std::string& app_id) {
166 return AppWindowRegistry::Get(browser()->profile())
167 ->GetAppWindowsForApp(app_id)
168 .size();
171 void PlatformAppBrowserTest::ClearCommandLineArgs() {
172 CommandLine* command_line = CommandLine::ForCurrentProcess();
173 CommandLine::StringVector args = command_line->GetArgs();
174 CommandLine::StringVector argv = command_line->argv();
175 for (size_t i = 0; i < args.size(); i++)
176 argv.pop_back();
177 command_line->InitFromArgv(argv);
180 void PlatformAppBrowserTest::SetCommandLineArg(const std::string& test_file) {
181 ClearCommandLineArgs();
182 CommandLine* command_line = CommandLine::ForCurrentProcess();
183 base::FilePath test_doc(test_data_dir_.AppendASCII(test_file));
184 test_doc = test_doc.NormalizePathSeparators();
185 command_line->AppendArgPath(test_doc);
188 AppWindow* PlatformAppBrowserTest::CreateAppWindow(const Extension* extension) {
189 return CreateAppWindowFromParams(extension, AppWindow::CreateParams());
192 AppWindow* PlatformAppBrowserTest::CreateAppWindowFromParams(
193 const Extension* extension,
194 const AppWindow::CreateParams& params) {
195 AppWindow* window = new AppWindow(
196 browser()->profile(), new ChromeAppWindowDelegate(), extension);
197 window->Init(
198 GURL(std::string()), new apps::AppWindowContentsImpl(window), params);
199 return window;
202 void PlatformAppBrowserTest::CloseAppWindow(AppWindow* window) {
203 content::WebContentsDestroyedWatcher destroyed_watcher(
204 window->web_contents());
205 window->GetBaseWindow()->Close();
206 destroyed_watcher.Wait();
209 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
210 AppWindow* window,
211 const gfx::Rect& cached_bounds,
212 const gfx::Rect& cached_screen_bounds,
213 const gfx::Rect& current_screen_bounds,
214 const gfx::Size& minimum_size,
215 gfx::Rect* bounds) {
216 window->AdjustBoundsToBeVisibleOnScreen(cached_bounds,
217 cached_screen_bounds,
218 current_screen_bounds,
219 minimum_size,
220 bounds);
223 apps::AppWindow* PlatformAppBrowserTest::CreateTestAppWindow(
224 const std::string& window_create_options) {
225 ExtensionTestMessageListener launched_listener("launched", true);
226 ExtensionTestMessageListener loaded_listener("window_loaded", false);
228 // Load and launch the test app.
229 const Extension* extension = LoadAndLaunchPlatformApp(kAppWindowTestApp);
230 EXPECT_TRUE(extension);
231 EXPECT_TRUE(launched_listener.WaitUntilSatisfied());
233 // Send the options for window creation.
234 launched_listener.Reply(window_create_options);
236 // Wait for the window to be opened and loaded.
237 EXPECT_TRUE(loaded_listener.WaitUntilSatisfied());
239 EXPECT_EQ(1U, GetAppWindowCount());
240 AppWindow* app_window = GetFirstAppWindow();
241 return app_window;
244 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
245 CommandLine* command_line) {
246 PlatformAppBrowserTest::SetUpCommandLine(command_line);
247 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
250 } // namespace extensions