Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: apps/app_shim/app_shim_interactive_uitest_mac.mm

Issue 316493002: [Mac] Add interactive App Shim test. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Don't run test on component builds. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | apps/app_shim/chrome_main_app_mode_mac.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Shims require static libraries http://crbug.com/386024.
216 #if defined(COMPONENT_BUILD)
217 #define MAYBE_Launch DISABLED_Launch
218 #else
219 #define MAYBE_Launch Launch
220 #endif
221
222 // Test that launching the shim for an app starts the app, and vice versa.
223 // These two cases are combined because the time to run the test is dominated
224 // by loading the extension and creating the shim.
225 IN_PROC_BROWSER_TEST_F(AppShimInteractiveTest, MAYBE_Launch) {
226 // Install the app.
227 const extensions::Extension* app = InstallPlatformApp("minimal");
228
229 // Use a WebAppShortcutCreator to get the path.
230 web_app::WebAppShortcutCreator shortcut_creator(
231 web_app::GetWebAppDataDirectory(profile()->GetPath(), app->id(), GURL()),
232 web_app::ShortcutInfoForExtensionAndProfile(app, profile()),
233 extensions::FileHandlersInfo());
234 base::FilePath shim_path = shortcut_creator.GetInternalShortcutPath();
235 EXPECT_FALSE(base::PathExists(shim_path));
236
237 // Create the internal app shim by simulating an app update. FilePathWatcher
238 // is used to wait for file operations on the shim to be finished before
239 // attempting to launch it. Since all of the file operations are done in the
240 // same event on the FILE thread, everything will be done by the time the
241 // watcher's callback is executed.
242 scoped_refptr<WindowedFilePathWatcher> file_watcher =
243 new WindowedFilePathWatcher(shim_path);
244 web_app::UpdateAllShortcuts(base::string16(), profile(), app);
245 file_watcher->Wait();
246 NSString* bundle_id = GetBundleID(shim_path);
247
248 // Case 1: Launch the shim, it should start the app.
249 {
250 ExtensionTestMessageListener launched_listener("Launched", false);
251 CommandLine shim_cmdline(CommandLine::NO_PROGRAM);
252 shim_cmdline.AppendSwitch(app_mode::kLaunchedForTest);
253 ProcessSerialNumber shim_psn;
254 ASSERT_TRUE(base::mac::OpenApplicationWithPath(
255 shim_path, shim_cmdline, kLSLaunchDefaults, &shim_psn));
256 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
257
258 ASSERT_TRUE(GetFirstAppWindow());
259 EXPECT_TRUE(HasAppShimHost(profile(), app->id()));
260
261 // If the window is closed, the shim should quit.
262 pid_t shim_pid;
263 EXPECT_EQ(noErr, GetProcessPID(&shim_psn, &shim_pid));
264 GetFirstAppWindow()->GetBaseWindow()->Close();
265 ASSERT_TRUE(
266 base::WaitForSingleProcess(shim_pid, TestTimeouts::action_timeout()));
267
268 EXPECT_FALSE(GetFirstAppWindow());
269 EXPECT_FALSE(HasAppShimHost(profile(), app->id()));
270 }
271
272 // Case 2: Launch the app, it should start the shim.
273 {
274 base::scoped_nsobject<WindowedNSNotificationObserver> ns_observer;
275 ns_observer.reset([[WindowedNSNotificationObserver alloc]
276 initForNotification:NSWorkspaceDidLaunchApplicationNotification
277 andBundleId:bundle_id]);
278 WindowedAppShimLaunchObserver observer(app->id());
279 LaunchPlatformApp(app);
280 [ns_observer wait];
281 observer.Wait();
282
283 EXPECT_TRUE(GetFirstAppWindow());
284 EXPECT_TRUE(HasAppShimHost(profile(), app->id()));
285
286 // Quitting the shim will eventually cause it to quit. It actually
287 // intercepts the -terminate, sends an AppShimHostMsg_QuitApp to Chrome,
288 // and returns NSTerminateLater. Chrome responds by closing all windows of
289 // the app. Once all windows are closed, Chrome closes the IPC channel,
290 // which causes the shim to actually terminate.
291 NSArray* running_shim = [NSRunningApplication
292 runningApplicationsWithBundleIdentifier:bundle_id];
293 ASSERT_EQ(1u, [running_shim count]);
294
295 ns_observer.reset([[WindowedNSNotificationObserver alloc]
296 initForNotification:NSWorkspaceDidTerminateApplicationNotification
297 andBundleId:bundle_id]);
298 [base::mac::ObjCCastStrict<NSRunningApplication>(
299 [running_shim objectAtIndex:0]) terminate];
300 [ns_observer wait];
301
302 EXPECT_FALSE(GetFirstAppWindow());
303 EXPECT_FALSE(HasAppShimHost(profile(), app->id()));
304 }
305 }
306
307 } // namespace apps
OLDNEW
« no previous file with comments | « no previous file | apps/app_shim/chrome_main_app_mode_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698