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

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: Address comments 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
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 observed_ = true;
tapted 2014/06/05 04:08:01 since this memory read on the UIThread, it shouldn
jackhou1 2014/06/05 05:11:01 Done.
81 content::BrowserThread::PostTask(
82 content::BrowserThread::UI,
83 FROM_HERE,
84 base::Bind(&WindowedFilePathWatcher::StopRunLoop, this));
85 }
86
87 void StopRunLoop() {
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)
tapted 2014/06/05 04:08:01 nit: reference&
jackhou1 2014/06/05 05:11:01 Done.
104 : app_mode_id_(app_id) {
tapted 2014/06/05 05:20:12 observed_ needs an initializer
jackhou1 2014/06/05 06:36:04 Done.
105 apps::AppShimHandler::RegisterHandler(app_id, this);
106 }
107
108 void Wait() {
109 if (observed_)
110 return;
111
112 run_loop_.reset(new base::RunLoop);
113 run_loop_->Run();
114 }
115
116 // AppShimHandler overrides:
117 virtual void OnShimLaunch(Host* host,
118 apps::AppShimLaunchType launch_type,
119 const std::vector<base::FilePath>& files) OVERRIDE {
120 // Remove self and pass through.
tapted 2014/06/05 05:20:12 nit: ...pass through to the default handler.
jackhou1 2014/06/05 06:36:04 Done.
121 apps::AppShimHandler::RemoveHandler(app_mode_id_);
122 apps::AppShimHandler::GetForAppMode(app_mode_id_)
123 ->OnShimLaunch(host, launch_type, files);
124 observed_ = true;
125 if (run_loop_.get())
126 run_loop_->Quit();
127 }
128 virtual void OnShimClose(Host* host) OVERRIDE {}
129 virtual void OnShimFocus(Host* host,
130 apps::AppShimFocusType focus_type,
131 const std::vector<base::FilePath>& files) OVERRIDE {}
132 virtual void OnShimSetHidden(Host* host, bool hidden) OVERRIDE {}
133 virtual void OnShimQuit(Host* host) OVERRIDE {}
134
135 private:
136 std::string app_mode_id_;
137 bool observed_;
138 scoped_ptr<base::RunLoop> run_loop_;
139
140 DISALLOW_COPY_AND_ASSIGN(WindowedAppShimLaunchObserver);
141 };
142
143 NSString* GetBundleID(const base::FilePath& shim_path) {
144 base::FilePath plist_path = shim_path.Append("Contents").Append("Info.plist");
145 NSMutableDictionary* plist = [NSMutableDictionary
146 dictionaryWithContentsOfFile:base::mac::FilePathToNSString(plist_path)];
147 return [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)];
148 }
149
150 bool HasAppShimHost(Profile* profile, const std::string& app_id) {
151 return g_browser_process->platform_part()
152 ->app_shim_host_manager()
153 ->extension_app_shim_handler()
154 ->FindHost(profile, app_id);
155 }
156
157 } // namespace
158
159 // Watches for NSNotifications from the shared workspace.
160 @interface WindowedNSNotificationObserver : NSObject {
161 @private
162 base::scoped_nsobject<NSString> bundleId_;
163 BOOL notificationReceived_;
164 scoped_ptr<base::RunLoop> runLoop_;
165 }
166
167 - (id)initForNotification:(NSString*)name
168 andBundleId:(NSString*)bundleId;
169 - (void)observe:(NSNotification*)notification;
170 - (void)wait;
171 @end
172
173 @implementation WindowedNSNotificationObserver
174
175 - (id)initForNotification:(NSString*)name
176 andBundleId:(NSString*)bundleId {
177 if (self = [super init]) {
178 bundleId_.reset([[bundleId copy] retain]);
179 [[[NSWorkspace sharedWorkspace] notificationCenter]
180 addObserver:self
181 selector:@selector(observe:)
182 name:name
183 object:nil];
184 }
185 return self;
186 }
187
188 - (void)observe:(NSNotification*)notification {
189 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
190
191 NSRunningApplication* application =
192 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey];
193 if (![[application bundleIdentifier] isEqualToString:bundleId_])
194 return;
195
196 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
197 notificationReceived_ = YES;
198 if (runLoop_.get())
199 runLoop_->Quit();
200 }
201
202 - (void)wait {
203 if (notificationReceived_)
204 return;
205
206 runLoop_.reset(new base::RunLoop);
207 runLoop_->Run();
208 }
209
210 @end
211
212 namespace apps {
213
214 // Test that launching the shim for an app starts the app, and vice versa.
215 // These two cases are combined because the time to run the test is dominated
216 // by loading the extension and creating the shim.
217 IN_PROC_BROWSER_TEST_F(AppShimInteractiveTest, Launch) {
218 // Create the internal app shim.
219 const extensions::Extension* app = InstallPlatformApp("minimal");
220
221 // Use a WebAppShortcutCreator to get the path.
222 web_app::WebAppShortcutCreator shortcut_creator(
223 web_app::GetWebAppDataDirectory(profile()->GetPath(), app->id(), GURL()),
224 web_app::ShortcutInfoForExtensionAndProfile(app, profile()),
225 extensions::FileHandlersInfo());
226 base::FilePath shim_path = shortcut_creator.GetInternalShortcutPath();
227 EXPECT_FALSE(base::PathExists(shim_path));
228
229 // Simulate updating the app. FilePathWatcher is used to wait for file
230 // operations on the shim to be finished before attempting to launch it.
231 // Since all of the file operations are done in the same event on the FILE
232 // thread, everything will be done by the time the watcher's callback is
233 // executed.
234 scoped_refptr<WindowedFilePathWatcher> file_watcher =
235 new WindowedFilePathWatcher(shim_path);
236 web_app::UpdateAllShortcuts(base::string16(), profile(), app);
237 file_watcher->Wait();
238 NSString* bundle_id = GetBundleID(shim_path);
239
240 // Case 1: Launch the shim, it should start the app.
241 {
242 ExtensionTestMessageListener launched_listener("Launched", false);
243 CommandLine shim_cmdline(CommandLine::NO_PROGRAM);
244 shim_cmdline.AppendSwitch(app_mode::kLaunchedForTest);
245 ProcessSerialNumber shim_psn;
246 ASSERT_TRUE(base::mac::OpenApplicationWithPath(
247 shim_path, shim_cmdline, kLSLaunchDefaults, &shim_psn));
248 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
249
250 ASSERT_TRUE(GetFirstAppWindow());
251 EXPECT_TRUE(HasAppShimHost(profile(), app->id()));
252
253 // If the window is closed, the shim should quit.
254 pid_t shim_pid;
255 EXPECT_EQ(noErr, GetProcessPID(&shim_psn, &shim_pid));
256 GetFirstAppWindow()->GetBaseWindow()->Close();
257 ASSERT_TRUE(
258 base::WaitForSingleProcess(shim_pid, TestTimeouts::action_timeout()));
259
260 EXPECT_FALSE(GetFirstAppWindow());
261 EXPECT_FALSE(HasAppShimHost(profile(), app->id()));
262 }
263
264 // Case 2: Launch the app, it should start the shim.
265 {
266 base::scoped_nsobject<WindowedNSNotificationObserver> ns_observer;
267 ns_observer.reset([[WindowedNSNotificationObserver alloc]
268 initForNotification:NSWorkspaceDidLaunchApplicationNotification
269 andBundleId:bundle_id]);
270 WindowedAppShimLaunchObserver observer(app->id());
271 LaunchPlatformApp(app);
272 [ns_observer wait];
273 observer.Wait();
274
275 EXPECT_TRUE(GetFirstAppWindow());
276 EXPECT_TRUE(HasAppShimHost(profile(), app->id()));
277
278 // Quitting the shim will cause all windows to close, after which the shim
279 // terminates.
280 NSArray* running_shim = [NSRunningApplication
281 runningApplicationsWithBundleIdentifier:bundle_id];
282 ASSERT_EQ(1u, [running_shim count]);
283
284 ns_observer.reset([[WindowedNSNotificationObserver alloc]
285 initForNotification:NSWorkspaceDidTerminateApplicationNotification
286 andBundleId:bundle_id]);
287 [base::mac::ObjCCastStrict<NSRunningApplication>(
288 [running_shim objectAtIndex:0]) terminate];
289 [ns_observer wait];
290
291 EXPECT_FALSE(GetFirstAppWindow());
292 EXPECT_FALSE(HasAppShimHost(profile(), app->id()));
293 }
294 }
295
296 } // namespace apps
OLDNEW
« no previous file with comments | « no previous file | apps/app_shim/chrome_main_app_mode_mac.mm » ('j') | chrome/browser/web_applications/web_app_mac.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698