OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "apps/app_lifetime_monitor_factory.h" | 8 #include "apps/app_lifetime_monitor_factory.h" |
9 #include "apps/switches.h" | 9 #include "apps/switches.h" |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/files/file_path_watcher.h" | 12 #include "base/files/file_path_watcher.h" |
13 #include "base/mac/foundation_util.h" | 13 #include "base/mac/foundation_util.h" |
14 #include "base/mac/launch_services_util.h" | 14 #include "base/mac/launch_services_util.h" |
15 #include "base/mac/mac_util.h" | 15 #include "base/mac/mac_util.h" |
16 #include "base/mac/scoped_nsobject.h" | 16 #include "base/mac/scoped_nsobject.h" |
17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
18 #include "base/process/launch.h" | 18 #include "base/process/launch.h" |
19 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
20 #include "base/test/test_timeouts.h" | 20 #include "base/test/test_timeouts.h" |
21 #include "chrome/browser/apps/app_browsertest_util.h" | 21 #include "chrome/browser/apps/app_browsertest_util.h" |
22 #include "chrome/browser/apps/app_shim/app_shim_handler_mac.h" | 22 #include "chrome/browser/apps/app_shim/app_shim_handler_mac.h" |
23 #include "chrome/browser/apps/app_shim/app_shim_host_manager_mac.h" | 23 #include "chrome/browser/apps/app_shim/app_shim_host_manager_mac.h" |
24 #include "chrome/browser/apps/app_shim/extension_app_shim_handler_mac.h" | 24 #include "chrome/browser/apps/app_shim/extension_app_shim_handler_mac.h" |
25 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
| 26 #include "chrome/browser/extensions/launch_util.h" |
26 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/ui/browser_list.h" |
| 29 #include "chrome/browser/ui/browser_window.h" |
27 #include "chrome/browser/web_applications/web_app_mac.h" | 30 #include "chrome/browser/web_applications/web_app_mac.h" |
28 #include "chrome/common/chrome_paths.h" | 31 #include "chrome/common/chrome_paths.h" |
29 #include "chrome/common/chrome_switches.h" | 32 #include "chrome/common/chrome_switches.h" |
30 #include "chrome/common/mac/app_mode_common.h" | 33 #include "chrome/common/mac/app_mode_common.h" |
31 #include "content/public/test/test_utils.h" | 34 #include "content/public/test/test_utils.h" |
32 #include "extensions/browser/app_window/native_app_window.h" | 35 #include "extensions/browser/app_window/native_app_window.h" |
| 36 #include "extensions/browser/extension_prefs.h" |
33 #include "extensions/browser/extension_registry.h" | 37 #include "extensions/browser/extension_registry.h" |
34 #include "extensions/test/extension_test_message_listener.h" | 38 #include "extensions/test/extension_test_message_listener.h" |
35 #import "ui/events/test/cocoa_test_event_utils.h" | 39 #import "ui/events/test/cocoa_test_event_utils.h" |
36 | 40 |
37 namespace { | 41 namespace { |
38 | 42 |
39 // General end-to-end test for app shims. | 43 // General end-to-end test for app shims. |
40 class AppShimInteractiveTest : public extensions::PlatformAppBrowserTest { | 44 class AppShimInteractiveTest : public extensions::PlatformAppBrowserTest { |
41 protected: | 45 protected: |
42 AppShimInteractiveTest() | 46 AppShimInteractiveTest() |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 void OnShimQuit(Host* host) override {} | 148 void OnShimQuit(Host* host) override {} |
145 | 149 |
146 private: | 150 private: |
147 std::string app_mode_id_; | 151 std::string app_mode_id_; |
148 bool observed_; | 152 bool observed_; |
149 scoped_ptr<base::RunLoop> run_loop_; | 153 scoped_ptr<base::RunLoop> run_loop_; |
150 | 154 |
151 DISALLOW_COPY_AND_ASSIGN(WindowedAppShimLaunchObserver); | 155 DISALLOW_COPY_AND_ASSIGN(WindowedAppShimLaunchObserver); |
152 }; | 156 }; |
153 | 157 |
| 158 // Watches for a hosted app browser window to open. |
| 159 class HostedAppBrowserListObserver : public chrome::BrowserListObserver { |
| 160 public: |
| 161 explicit HostedAppBrowserListObserver(const std::string& app_id) |
| 162 : app_id_(app_id), observed_add_(false), observed_removed_(false) { |
| 163 BrowserList::AddObserver(this); |
| 164 } |
| 165 |
| 166 ~HostedAppBrowserListObserver() { BrowserList::RemoveObserver(this); } |
| 167 |
| 168 void WaitUntilAdded() { |
| 169 if (observed_add_) |
| 170 return; |
| 171 |
| 172 run_loop_.reset(new base::RunLoop); |
| 173 run_loop_->Run(); |
| 174 } |
| 175 |
| 176 void WaitUntilRemoved() { |
| 177 if (observed_removed_) |
| 178 return; |
| 179 |
| 180 run_loop_.reset(new base::RunLoop); |
| 181 run_loop_->Run(); |
| 182 } |
| 183 |
| 184 // BrowserListObserver overrides: |
| 185 void OnBrowserAdded(Browser* browser) override { |
| 186 const extensions::Extension* app = |
| 187 apps::ExtensionAppShimHandler::GetAppForBrowser(browser); |
| 188 if (app && app->id() == app_id_) { |
| 189 observed_add_ = true; |
| 190 if (run_loop_.get()) |
| 191 run_loop_->Quit(); |
| 192 } |
| 193 } |
| 194 |
| 195 void OnBrowserRemoved(Browser* browser) override { |
| 196 const extensions::Extension* app = |
| 197 apps::ExtensionAppShimHandler::GetAppForBrowser(browser); |
| 198 if (app && app->id() == app_id_) { |
| 199 observed_removed_ = true; |
| 200 if (run_loop_.get()) |
| 201 run_loop_->Quit(); |
| 202 } |
| 203 } |
| 204 |
| 205 private: |
| 206 std::string app_id_; |
| 207 bool observed_add_; |
| 208 bool observed_removed_; |
| 209 scoped_ptr<base::RunLoop> run_loop_; |
| 210 |
| 211 DISALLOW_COPY_AND_ASSIGN(HostedAppBrowserListObserver); |
| 212 }; |
| 213 |
154 class AppLifetimeMonitorObserver : public apps::AppLifetimeMonitor::Observer { | 214 class AppLifetimeMonitorObserver : public apps::AppLifetimeMonitor::Observer { |
155 public: | 215 public: |
156 AppLifetimeMonitorObserver(Profile* profile) | 216 AppLifetimeMonitorObserver(Profile* profile) |
157 : profile_(profile), activated_count_(0), deactivated_count_(0) { | 217 : profile_(profile), activated_count_(0), deactivated_count_(0) { |
158 apps::AppLifetimeMonitorFactory::GetForProfile(profile_)->AddObserver(this); | 218 apps::AppLifetimeMonitorFactory::GetForProfile(profile_)->AddObserver(this); |
159 } | 219 } |
160 virtual ~AppLifetimeMonitorObserver() { | 220 virtual ~AppLifetimeMonitorObserver() { |
161 apps::AppLifetimeMonitorFactory::GetForProfile(profile_) | 221 apps::AppLifetimeMonitorFactory::GetForProfile(profile_) |
162 ->RemoveObserver(this); | 222 ->RemoveObserver(this); |
163 } | 223 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 // is used to wait for file operations on the shim to be finished before | 273 // is used to wait for file operations on the shim to be finished before |
214 // attempting to launch it. Since all of the file operations are done in the | 274 // attempting to launch it. Since all of the file operations are done in the |
215 // same event on the FILE thread, everything will be done by the time the | 275 // same event on the FILE thread, everything will be done by the time the |
216 // watcher's callback is executed. | 276 // watcher's callback is executed. |
217 scoped_refptr<WindowedFilePathWatcher> file_watcher = | 277 scoped_refptr<WindowedFilePathWatcher> file_watcher = |
218 new WindowedFilePathWatcher(shim_path); | 278 new WindowedFilePathWatcher(shim_path); |
219 web_app::UpdateAllShortcuts(base::string16(), profile, app); | 279 web_app::UpdateAllShortcuts(base::string16(), profile, app); |
220 file_watcher->Wait(); | 280 file_watcher->Wait(); |
221 } | 281 } |
222 | 282 |
| 283 Browser* GetFirstHostedAppWindow() { |
| 284 BrowserList* browsers = |
| 285 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 286 for (Browser* browser : *browsers) { |
| 287 const extensions::Extension* extension = |
| 288 apps::ExtensionAppShimHandler::GetAppForBrowser(browser); |
| 289 if (extension && extension->is_hosted_app()) |
| 290 return browser; |
| 291 } |
| 292 return nullptr; |
| 293 } |
| 294 |
223 } // namespace | 295 } // namespace |
224 | 296 |
225 // Watches for NSNotifications from the shared workspace. | 297 // Watches for NSNotifications from the shared workspace. |
226 @interface WindowedNSNotificationObserver : NSObject { | 298 @interface WindowedNSNotificationObserver : NSObject { |
227 @private | 299 @private |
228 base::scoped_nsobject<NSString> bundleId_; | 300 base::scoped_nsobject<NSString> bundleId_; |
229 BOOL notificationReceived_; | 301 BOOL notificationReceived_; |
230 scoped_ptr<base::RunLoop> runLoop_; | 302 scoped_ptr<base::RunLoop> runLoop_; |
231 } | 303 } |
232 | 304 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 runLoop_->Run(); | 345 runLoop_->Run(); |
274 } | 346 } |
275 | 347 |
276 @end | 348 @end |
277 | 349 |
278 namespace apps { | 350 namespace apps { |
279 | 351 |
280 // Shims require static libraries http://crbug.com/386024. | 352 // Shims require static libraries http://crbug.com/386024. |
281 #if defined(COMPONENT_BUILD) | 353 #if defined(COMPONENT_BUILD) |
282 #define MAYBE_Launch DISABLED_Launch | 354 #define MAYBE_Launch DISABLED_Launch |
| 355 #define MAYBE_HostedAppLaunch DISABLED_HostedAppLaunch |
283 #define MAYBE_ShowWindow DISABLED_ShowWindow | 356 #define MAYBE_ShowWindow DISABLED_ShowWindow |
284 #define MAYBE_RebuildShim DISABLED_RebuildShim | 357 #define MAYBE_RebuildShim DISABLED_RebuildShim |
285 #else | 358 #else |
286 #define MAYBE_Launch Launch | 359 #define MAYBE_Launch Launch |
| 360 #define MAYBE_HostedAppLaunch HostedAppLaunch |
287 #define MAYBE_ShowWindow ShowWindow | 361 #define MAYBE_ShowWindow ShowWindow |
288 #define MAYBE_RebuildShim RebuildShim | 362 #define MAYBE_RebuildShim RebuildShim |
289 #endif | 363 #endif |
290 | 364 |
| 365 IN_PROC_BROWSER_TEST_F(AppShimInteractiveTest, MAYBE_HostedAppLaunch) { |
| 366 const extensions::Extension* app = InstallHostedApp(); |
| 367 |
| 368 base::FilePath shim_path = GetAppShimPath(profile(), app); |
| 369 EXPECT_FALSE(base::PathExists(shim_path)); |
| 370 |
| 371 UpdateAppAndAwaitShimCreation(profile(), app, shim_path); |
| 372 ASSERT_TRUE(base::PathExists(shim_path)); |
| 373 NSString* bundle_id = GetBundleID(shim_path); |
| 374 |
| 375 // Explicitly set the launch type to open in a new window. |
| 376 extensions::SetLaunchType( |
| 377 extensions::ExtensionSystem::Get(profile())->extension_service(), |
| 378 app->id(), extensions::LAUNCH_TYPE_WINDOW); |
| 379 |
| 380 // Case 1: Launch the hosted app, it should start the shim. |
| 381 { |
| 382 base::scoped_nsobject<WindowedNSNotificationObserver> ns_observer; |
| 383 ns_observer.reset([[WindowedNSNotificationObserver alloc] |
| 384 initForNotification:NSWorkspaceDidLaunchApplicationNotification |
| 385 andBundleId:bundle_id]); |
| 386 WindowedAppShimLaunchObserver observer(app->id()); |
| 387 LaunchHostedApp(app); |
| 388 [ns_observer wait]; |
| 389 observer.Wait(); |
| 390 |
| 391 EXPECT_TRUE(HasAppShimHost(profile(), app->id())); |
| 392 EXPECT_TRUE(GetFirstHostedAppWindow()); |
| 393 |
| 394 NSArray* running_shim = [NSRunningApplication |
| 395 runningApplicationsWithBundleIdentifier:bundle_id]; |
| 396 ASSERT_EQ(1u, [running_shim count]); |
| 397 |
| 398 ns_observer.reset([[WindowedNSNotificationObserver alloc] |
| 399 initForNotification:NSWorkspaceDidTerminateApplicationNotification |
| 400 andBundleId:bundle_id]); |
| 401 [base::mac::ObjCCastStrict<NSRunningApplication>( |
| 402 [running_shim objectAtIndex:0]) terminate]; |
| 403 [ns_observer wait]; |
| 404 |
| 405 EXPECT_FALSE(GetFirstHostedAppWindow()); |
| 406 EXPECT_FALSE(HasAppShimHost(profile(), app->id())); |
| 407 } |
| 408 |
| 409 // Case 2: Launch the shim, it should start the hosted app. |
| 410 { |
| 411 HostedAppBrowserListObserver listener(app->id()); |
| 412 CommandLine shim_cmdline(CommandLine::NO_PROGRAM); |
| 413 shim_cmdline.AppendSwitch(app_mode::kLaunchedForTest); |
| 414 ProcessSerialNumber shim_psn; |
| 415 ASSERT_TRUE(base::mac::OpenApplicationWithPath( |
| 416 shim_path, shim_cmdline, kLSLaunchDefaults, &shim_psn)); |
| 417 listener.WaitUntilAdded(); |
| 418 |
| 419 ASSERT_TRUE(GetFirstHostedAppWindow()); |
| 420 EXPECT_TRUE(HasAppShimHost(profile(), app->id())); |
| 421 |
| 422 // If the window is closed, the shim should quit. |
| 423 pid_t shim_pid; |
| 424 EXPECT_EQ(noErr, GetProcessPID(&shim_psn, &shim_pid)); |
| 425 GetFirstHostedAppWindow()->window()->Close(); |
| 426 // Wait for the window to be closed. |
| 427 listener.WaitUntilRemoved(); |
| 428 ASSERT_TRUE( |
| 429 base::WaitForSingleProcess(shim_pid, TestTimeouts::action_timeout())); |
| 430 |
| 431 EXPECT_FALSE(GetFirstHostedAppWindow()); |
| 432 EXPECT_FALSE(HasAppShimHost(profile(), app->id())); |
| 433 } |
| 434 } |
| 435 |
291 // Test that launching the shim for an app starts the app, and vice versa. | 436 // Test that launching the shim for an app starts the app, and vice versa. |
292 // These two cases are combined because the time to run the test is dominated | 437 // These two cases are combined because the time to run the test is dominated |
293 // by loading the extension and creating the shim. | 438 // by loading the extension and creating the shim. |
294 IN_PROC_BROWSER_TEST_F(AppShimInteractiveTest, MAYBE_Launch) { | 439 IN_PROC_BROWSER_TEST_F(AppShimInteractiveTest, MAYBE_Launch) { |
295 const extensions::Extension* app = InstallPlatformApp("minimal"); | 440 const extensions::Extension* app = InstallPlatformApp("minimal"); |
296 | 441 |
297 base::FilePath shim_path = GetAppShimPath(profile(), app); | 442 base::FilePath shim_path = GetAppShimPath(profile(), app); |
298 EXPECT_FALSE(base::PathExists(shim_path)); | 443 EXPECT_FALSE(base::PathExists(shim_path)); |
299 | 444 |
300 UpdateAppAndAwaitShimCreation(profile(), app, shim_path); | 445 UpdateAppAndAwaitShimCreation(profile(), app, shim_path); |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 // the shim is rebuilt. | 706 // the shim is rebuilt. |
562 WindowedAppShimLaunchObserver(app->id()).Wait(); | 707 WindowedAppShimLaunchObserver(app->id()).Wait(); |
563 | 708 |
564 EXPECT_TRUE(GetFirstAppWindow()); | 709 EXPECT_TRUE(GetFirstAppWindow()); |
565 EXPECT_TRUE(HasAppShimHost(profile(), app->id())); | 710 EXPECT_TRUE(HasAppShimHost(profile(), app->id())); |
566 } | 711 } |
567 | 712 |
568 #endif // defined(ARCH_CPU_64_BITS) | 713 #endif // defined(ARCH_CPU_64_BITS) |
569 | 714 |
570 } // namespace apps | 715 } // namespace apps |
OLD | NEW |