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/shell_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/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 "content/public/browser/notification_service.h" 18 #include "content/public/test/browser_test_utils.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, "1000"); 40 command_line->AppendSwitchASCII(switches::kEventPageSuspendingTime, "1000"); 41 } 42 43 // static 44 ShellWindow* PlatformAppBrowserTest::GetFirstShellWindowForBrowser( 45 Browser* browser) { 46 ShellWindowRegistry* app_registry = 47 ShellWindowRegistry::Get(browser->profile()); 48 const ShellWindowRegistry::ShellWindowList& shell_windows = 49 app_registry->shell_windows(); 50 51 ShellWindowRegistry::const_iterator iter = shell_windows.begin(); 52 if (iter != shell_windows.end()) 53 return *iter; 54 55 return NULL; 56 } 57 58 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp( 59 const char* name) { 60 content::WindowedNotificationObserver app_loaded_observer( 61 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, 62 content::NotificationService::AllSources()); 63 64 const Extension* extension = LoadExtension( 65 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name)); 66 EXPECT_TRUE(extension); 67 68 OpenApplication(AppLaunchParams( 69 browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW)); 70 71 app_loaded_observer.Wait(); 72 73 return extension; 74 } 75 76 const Extension* PlatformAppBrowserTest::InstallPlatformApp( 77 const char* name) { 78 const Extension* extension = InstallExtension( 79 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name), 1); 80 EXPECT_TRUE(extension); 81 82 return extension; 83 } 84 85 const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp( 86 const char* name) { 87 content::WindowedNotificationObserver app_loaded_observer( 88 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, 89 content::NotificationService::AllSources()); 90 91 const Extension* extension = InstallPlatformApp(name); 92 93 OpenApplication(AppLaunchParams( 94 browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW)); 95 96 app_loaded_observer.Wait(); 97 98 return extension; 99 } 100 101 WebContents* PlatformAppBrowserTest::GetFirstShellWindowWebContents() { 102 ShellWindow* window = GetFirstShellWindow(); 103 if (window) 104 return window->web_contents(); 105 106 return NULL; 107 } 108 109 ShellWindow* PlatformAppBrowserTest::GetFirstShellWindow() { 110 return GetFirstShellWindowForBrowser(browser()); 111 } 112 113 size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension( 114 const Extension* extension) { 115 scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction(); 116 function->set_extension(extension); 117 scoped_ptr<base::ListValue> result(utils::ToList( 118 utils::RunFunctionAndReturnSingleResult(function.get(), 119 "[]", 120 browser()))); 121 return result->GetSize(); 122 } 123 124 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension( 125 int window_id, 126 const Extension* extension) { 127 scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction(); 128 function->set_extension(extension); 129 utils::RunFunction( 130 function.get(), 131 base::StringPrintf("[%u]", window_id), 132 browser(), 133 utils::NONE); 134 return function->GetResultList() != NULL; 135 } 136 137 size_t PlatformAppBrowserTest::GetShellWindowCount() { 138 return ShellWindowRegistry::Get(browser()->profile())-> 139 shell_windows().size(); 140 } 141 142 void PlatformAppBrowserTest::ClearCommandLineArgs() { 143 CommandLine* command_line = CommandLine::ForCurrentProcess(); 144 CommandLine::StringVector args = command_line->GetArgs(); 145 CommandLine::StringVector argv = command_line->argv(); 146 for (size_t i = 0; i < args.size(); i++) 147 argv.pop_back(); 148 command_line->InitFromArgv(argv); 149 } 150 151 void PlatformAppBrowserTest::SetCommandLineArg(const std::string& test_file) { 152 ClearCommandLineArgs(); 153 CommandLine* command_line = CommandLine::ForCurrentProcess(); 154 base::FilePath test_doc(test_data_dir_.AppendASCII(test_file)); 155 test_doc = test_doc.NormalizePathSeparators(); 156 command_line->AppendArgPath(test_doc); 157 } 158 159 ShellWindow* PlatformAppBrowserTest::CreateShellWindow( 160 const Extension* extension) { 161 return CreateShellWindowFromParams(extension, ShellWindow::CreateParams()); 162 } 163 164 ShellWindow* PlatformAppBrowserTest::CreateShellWindowFromParams( 165 const Extension* extension, const ShellWindow::CreateParams& params) { 166 ShellWindow* window = new ShellWindow(browser()->profile(), 167 new ChromeShellWindowDelegate(), 168 extension); 169 window->Init(GURL(std::string()), 170 new apps::AppWindowContents(window), 171 params); 172 return window; 173 } 174 175 void PlatformAppBrowserTest::CloseShellWindow(ShellWindow* window) { 176 content::WebContentsDestroyedWatcher destroyed_watcher( 177 window->web_contents()); 178 window->GetBaseWindow()->Close(); 179 destroyed_watcher.Wait(); 180 } 181 182 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForShellWindow( 183 ShellWindow* window, 184 const gfx::Rect& cached_bounds, 185 const gfx::Rect& cached_screen_bounds, 186 const gfx::Rect& current_screen_bounds, 187 const gfx::Size& minimum_size, 188 gfx::Rect* bounds) { 189 window->AdjustBoundsToBeVisibleOnScreen(cached_bounds, 190 cached_screen_bounds, 191 current_screen_bounds, 192 minimum_size, 193 bounds); 194 } 195 196 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine( 197 CommandLine* command_line) { 198 PlatformAppBrowserTest::SetUpCommandLine(command_line); 199 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); 200 } 201 202 } // namespace extensions 203