OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import <Cocoa/Cocoa.h> | |
6 #include <vector> | |
7 | |
8 #include "apps/app_shim/app_shim_handler_mac.h" | |
9 #include "apps/app_shim/app_shim_host_manager_mac.h" | |
10 #include "apps/app_shim/extension_app_shim_handler_mac.h" | |
11 #include "apps/switches.h" | |
12 #include "apps/ui/native_app_window.h" | |
13 #include "base/auto_reset.h" | |
14 #include "base/callback.h" | |
15 #include "base/files/file_path_watcher.h" | |
16 #include "base/mac/foundation_util.h" | |
17 #include "base/mac/launch_services_util.h" | |
18 #include "base/mac/scoped_nsobject.h" | |
19 #include "base/path_service.h" | |
20 #include "base/process/launch.h" | |
21 #include "base/strings/sys_string_conversions.h" | |
22 #include "base/test/test_timeouts.h" | |
23 #include "chrome/browser/apps/app_browsertest_util.h" | |
24 #include "chrome/browser/browser_process.h" | |
25 #include "chrome/browser/extensions/extension_test_message_listener.h" | |
26 #include "chrome/browser/profiles/profile.h" | |
27 #include "chrome/browser/web_applications/web_app_mac.h" | |
28 #include "chrome/common/chrome_paths.h" | |
29 #include "chrome/common/chrome_switches.h" | |
30 #include "chrome/common/mac/app_mode_common.h" | |
31 #include "content/public/test/test_utils.h" | |
32 #include "extensions/browser/extension_registry.h" | |
33 #import "ui/events/test/cocoa_test_event_utils.h" | |
34 | |
35 namespace { | |
36 | |
37 // General end-to-end test for app shims. | |
38 class AppShimInteractiveTest : public extensions::PlatformAppBrowserTest { | |
39 protected: | |
40 AppShimInteractiveTest() | |
41 : auto_reset_(&g_app_shims_allow_update_and_launch_in_tests, true) {} | |
42 | |
43 private: | |
44 // Temporarily enable app shims. | |
45 base::AutoReset<bool> auto_reset_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(AppShimInteractiveTest); | |
48 }; | |
49 | |
50 // Watches for changes to a file. This is designed to be used from the the UI | |
51 // thread. | |
52 class WindowedFilePathWatcher | |
53 : public base::RefCountedThreadSafe<WindowedFilePathWatcher> { | |
54 public: | |
55 WindowedFilePathWatcher(const base::FilePath& path) : observed_(false) { | |
56 content::BrowserThread::PostTask( | |
57 content::BrowserThread::FILE, | |
58 FROM_HERE, | |
59 base::Bind(&WindowedFilePathWatcher::Watch, this, path)); | |
60 } | |
61 | |
62 void Wait() { | |
63 if (observed_) | |
64 return; | |
65 | |
66 run_loop_.reset(new base::RunLoop); | |
67 run_loop_->Run(); | |
68 } | |
69 | |
70 protected: | |
71 friend class base::RefCountedThreadSafe<WindowedFilePathWatcher>; | |
72 virtual ~WindowedFilePathWatcher() {} | |
73 | |
74 void Watch(const base::FilePath& path) { | |
75 watcher_.Watch( | |
76 path, false, base::Bind(&WindowedFilePathWatcher::Observe, this)); | |
77 } | |
78 | |
79 void Observe(const base::FilePath& path, bool error) { | |
80 content::BrowserThread::PostTask( | |
81 content::BrowserThread::UI, | |
82 FROM_HERE, | |
83 base::Bind(&WindowedFilePathWatcher::StopRunLoop, this)); | |
84 } | |
85 | |
86 void StopRunLoop() { | |
87 observed_ = true; | |
88 if (run_loop_.get()) | |
89 run_loop_->Quit(); | |
90 } | |
91 | |
92 private: | |
93 base::FilePathWatcher watcher_; | |
94 bool observed_; | |
95 scoped_ptr<base::RunLoop> run_loop_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(WindowedFilePathWatcher); | |
98 }; | |
99 | |
100 // Watches for an app shim to connect. | |
101 class WindowedAppShimLaunchObserver : public apps::AppShimHandler { | |
102 public: | |
103 WindowedAppShimLaunchObserver(const std::string& app_id) | |
104 : app_mode_id_(app_id), | |
105 observed_(false) { | |
106 apps::AppShimHandler::RegisterHandler(app_id, this); | |
107 } | |
108 | |
109 void Wait() { | |
110 if (observed_) | |
111 return; | |
112 | |
113 run_loop_.reset(new base::RunLoop); | |
114 run_loop_->Run(); | |
115 } | |
116 | |
117 // AppShimHandler overrides: | |
118 virtual void OnShimLaunch(Host* host, | |
119 apps::AppShimLaunchType launch_type, | |
120 const std::vector<base::FilePath>& files) OVERRIDE { | |
121 // Remove self and pass through to the default handler. | |
122 apps::AppShimHandler::RemoveHandler(app_mode_id_); | |
123 apps::AppShimHandler::GetForAppMode(app_mode_id_) | |
124 ->OnShimLaunch(host, launch_type, files); | |
125 observed_ = true; | |
126 if (run_loop_.get()) | |
127 run_loop_->Quit(); | |
128 } | |
129 virtual void OnShimClose(Host* host) OVERRIDE {} | |
130 virtual void OnShimFocus(Host* host, | |
131 apps::AppShimFocusType focus_type, | |
132 const std::vector<base::FilePath>& files) OVERRIDE {} | |
133 virtual void OnShimSetHidden(Host* host, bool hidden) OVERRIDE {} | |
134 virtual void OnShimQuit(Host* host) OVERRIDE {} | |
135 | |
136 private: | |
137 std::string app_mode_id_; | |
138 bool observed_; | |
139 scoped_ptr<base::RunLoop> run_loop_; | |
140 | |
141 DISALLOW_COPY_AND_ASSIGN(WindowedAppShimLaunchObserver); | |
142 }; | |
143 | |
144 NSString* GetBundleID(const base::FilePath& shim_path) { | |
145 base::FilePath plist_path = shim_path.Append("Contents").Append("Info.plist"); | |
146 NSMutableDictionary* plist = [NSMutableDictionary | |
147 dictionaryWithContentsOfFile:base::mac::FilePathToNSString(plist_path)]; | |
148 return [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]; | |
149 } | |
150 | |
151 bool HasAppShimHost(Profile* profile, const std::string& app_id) { | |
152 return g_browser_process->platform_part() | |
153 ->app_shim_host_manager() | |
154 ->extension_app_shim_handler() | |
155 ->FindHost(profile, app_id); | |
156 } | |
157 | |
158 } // namespace | |
159 | |
160 // Watches for NSNotifications from the shared workspace. | |
161 @interface WindowedNSNotificationObserver : NSObject { | |
162 @private | |
163 base::scoped_nsobject<NSString> bundleId_; | |
164 BOOL notificationReceived_; | |
165 scoped_ptr<base::RunLoop> runLoop_; | |
166 } | |
167 | |
168 - (id)initForNotification:(NSString*)name | |
169 andBundleId:(NSString*)bundleId; | |
170 - (void)observe:(NSNotification*)notification; | |
171 - (void)wait; | |
172 @end | |
173 | |
174 @implementation WindowedNSNotificationObserver | |
175 | |
176 - (id)initForNotification:(NSString*)name | |
177 andBundleId:(NSString*)bundleId { | |
178 if (self = [super init]) { | |
179 bundleId_.reset([[bundleId copy] retain]); | |
180 [[[NSWorkspace sharedWorkspace] notificationCenter] | |
181 addObserver:self | |
182 selector:@selector(observe:) | |
183 name:name | |
184 object:nil]; | |
185 } | |
186 return self; | |
187 } | |
188 | |
189 - (void)observe:(NSNotification*)notification { | |
190 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
191 | |
192 NSRunningApplication* application = | |
193 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; | |
194 if (![[application bundleIdentifier] isEqualToString:bundleId_]) | |
195 return; | |
196 | |
197 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; | |
198 notificationReceived_ = YES; | |
199 if (runLoop_.get()) | |
200 runLoop_->Quit(); | |
201 } | |
202 | |
203 - (void)wait { | |
204 if (notificationReceived_) | |
205 return; | |
206 | |
207 runLoop_.reset(new base::RunLoop); | |
208 runLoop_->Run(); | |
209 } | |
210 | |
211 @end | |
212 | |
213 namespace apps { | |
214 | |
215 // Test that launching the shim for an app starts the app, and vice versa. | |
216 // These two cases are combined because the time to run the test is dominated | |
217 // by loading the extension and creating the shim. | |
218 IN_PROC_BROWSER_TEST_F(AppShimInteractiveTest, Launch) { | |
219 // Install the app. | |
220 const extensions::Extension* app = InstallPlatformApp("minimal"); | |
221 | |
222 // Use a WebAppShortcutCreator to get the path. | |
223 web_app::WebAppShortcutCreator shortcut_creator( | |
224 web_app::GetWebAppDataDirectory(profile()->GetPath(), app->id(), GURL()), | |
225 web_app::ShortcutInfoForExtensionAndProfile(app, profile()), | |
226 extensions::FileHandlersInfo()); | |
227 base::FilePath shim_path = shortcut_creator.GetInternalShortcutPath(); | |
228 EXPECT_FALSE(base::PathExists(shim_path)); | |
229 | |
230 // Create the internal app shim by simulating an app update. FilePathWatcher | |
231 // is used to wait for file operations on the shim to be finished before | |
232 // attempting to launch it. Since all of the file operations are done in the | |
233 // same event on the FILE thread, everything will be done by the time the | |
234 // watcher's callback is executed. | |
235 scoped_refptr<WindowedFilePathWatcher> file_watcher = | |
236 new WindowedFilePathWatcher(shim_path); | |
237 web_app::UpdateAllShortcuts(base::string16(), profile(), app); | |
238 file_watcher->Wait(); | |
239 NSString* bundle_id = GetBundleID(shim_path); | |
240 | |
241 // Case 1: Launch the shim, it should start the app. | |
242 { | |
243 ExtensionTestMessageListener launched_listener("Launched", false); | |
244 CommandLine shim_cmdline(CommandLine::NO_PROGRAM); | |
245 shim_cmdline.AppendSwitch(app_mode::kLaunchedForTest); | |
246 ProcessSerialNumber shim_psn; | |
247 ASSERT_TRUE(base::mac::OpenApplicationWithPath( | |
248 shim_path, shim_cmdline, kLSLaunchDefaults, &shim_psn)); | |
249 ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); | |
250 | |
251 ASSERT_TRUE(GetFirstAppWindow()); | |
252 EXPECT_TRUE(HasAppShimHost(profile(), app->id())); | |
253 | |
254 // If the window is closed, the shim should quit. | |
255 pid_t shim_pid; | |
256 EXPECT_EQ(noErr, GetProcessPID(&shim_psn, &shim_pid)); | |
257 GetFirstAppWindow()->GetBaseWindow()->Close(); | |
258 ASSERT_TRUE( | |
259 base::WaitForSingleProcess(shim_pid, TestTimeouts::action_timeout())); | |
260 | |
261 EXPECT_FALSE(GetFirstAppWindow()); | |
262 EXPECT_FALSE(HasAppShimHost(profile(), app->id())); | |
263 } | |
264 | |
265 // Case 2: Launch the app, it should start the shim. | |
266 { | |
267 base::scoped_nsobject<WindowedNSNotificationObserver> ns_observer; | |
268 ns_observer.reset([[WindowedNSNotificationObserver alloc] | |
269 initForNotification:NSWorkspaceDidLaunchApplicationNotification | |
270 andBundleId:bundle_id]); | |
271 WindowedAppShimLaunchObserver observer(app->id()); | |
272 LaunchPlatformApp(app); | |
273 [ns_observer wait]; | |
274 observer.Wait(); | |
275 | |
276 EXPECT_TRUE(GetFirstAppWindow()); | |
277 EXPECT_TRUE(HasAppShimHost(profile(), app->id())); | |
278 | |
279 // Quitting the shim will eventually cause it to quit. It actually | |
280 // intercepts the -terminate, sends an AppShimHostMsg_QuitApp to Chrome, | |
281 // and returns NSTerminateLater. Chrome responds by closing all windows of | |
282 // the app. Once all windows are closed, Chrome closes the IPC channel, | |
283 // which causes the shim to actually terminate. | |
284 NSArray* running_shim = [NSRunningApplication | |
285 runningApplicationsWithBundleIdentifier:bundle_id]; | |
286 ASSERT_EQ(1u, [running_shim count]); | |
287 | |
288 ns_observer.reset([[WindowedNSNotificationObserver alloc] | |
289 initForNotification:NSWorkspaceDidTerminateApplicationNotification | |
290 andBundleId:bundle_id]); | |
291 [base::mac::ObjCCastStrict<NSRunningApplication>( | |
292 [running_shim objectAtIndex:0]) terminate]; | |
293 [ns_observer wait]; | |
294 | |
295 EXPECT_FALSE(GetFirstAppWindow()); | |
296 EXPECT_FALSE(HasAppShimHost(profile(), app->id())); | |
297 } | |
298 } | |
299 | |
300 } // namespace apps | |
OLD | NEW |