1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <Foundation/NSAppleEventDescriptor.h>
#import <objc/message.h>
#import <objc/runtime.h>
#include "base/command_line.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/prefs/pref_service.h"
#include "chrome/app/chrome_command_ids.h"
#import "chrome/browser/app_controller_mac.h"
#include "chrome/browser/apps/app_browsertest_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/user_manager.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/signin/core/common/profile_management_switches.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/common/extension.h"
#include "extensions/test/extension_test_message_listener.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
namespace {
GURL g_open_shortcut_url = GURL::EmptyGURL();
} // namespace
@interface TestOpenShortcutOnStartup : NSObject
- (void)applicationWillFinishLaunching:(NSNotification*)notification;
@end
@implementation TestOpenShortcutOnStartup
- (void)applicationWillFinishLaunching:(NSNotification*)notification {
if (!g_open_shortcut_url.is_valid())
return;
AppController* controller =
base::mac::ObjCCast<AppController>([NSApp delegate]);
Method getUrl = class_getInstanceMethod([controller class],
@selector(getUrl:withReply:));
if (getUrl == nil)
return;
base::scoped_nsobject<NSAppleEventDescriptor> shortcutEvent(
[[NSAppleEventDescriptor alloc]
initWithEventClass:kASAppleScriptSuite
eventID:kASSubroutineEvent
targetDescriptor:nil
returnID:kAutoGenerateReturnID
transactionID:kAnyTransactionID]);
NSString* url =
[NSString stringWithUTF8String:g_open_shortcut_url.spec().c_str()];
[shortcutEvent setParamDescriptor:
[NSAppleEventDescriptor descriptorWithString:url]
forKeyword:keyDirectObject];
method_invoke(controller, getUrl, shortcutEvent.get(), NULL);
}
@end
namespace {
class AppControllerPlatformAppBrowserTest
: public extensions::PlatformAppBrowserTest {
protected:
AppControllerPlatformAppBrowserTest()
: active_browser_list_(BrowserList::GetInstance(
chrome::GetActiveDesktop())) {
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
PlatformAppBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(switches::kAppId,
"1234");
}
const BrowserList* active_browser_list_;
};
// Test that if only a platform app window is open and no browser windows are
// open then a reopen event does nothing.
IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest,
PlatformAppReopenWithWindows) {
base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
NSUInteger old_window_count = [[NSApp windows] count];
EXPECT_EQ(1u, active_browser_list_->size());
[ac applicationShouldHandleReopen:NSApp hasVisibleWindows:YES];
// We do not EXPECT_TRUE the result here because the method
// deminiaturizes windows manually rather than return YES and have
// AppKit do it.
EXPECT_EQ(old_window_count, [[NSApp windows] count]);
EXPECT_EQ(1u, active_browser_list_->size());
}
IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest,
ActivationFocusesBrowserWindow) {
base::scoped_nsobject<AppController> app_controller(
[[AppController alloc] init]);
ExtensionTestMessageListener listener("Launched", false);
const extensions::Extension* app =
InstallAndLaunchPlatformApp("minimal");
ASSERT_TRUE(listener.WaitUntilSatisfied());
NSWindow* app_window = extensions::AppWindowRegistry::Get(profile())
->GetAppWindowsForApp(app->id())
.front()
->GetNativeWindow();
NSWindow* browser_window = browser()->window()->GetNativeWindow();
EXPECT_LE([[NSApp orderedWindows] indexOfObject:app_window],
[[NSApp orderedWindows] indexOfObject:browser_window]);
[app_controller applicationShouldHandleReopen:NSApp
hasVisibleWindows:YES];
EXPECT_LE([[NSApp orderedWindows] indexOfObject:browser_window],
[[NSApp orderedWindows] indexOfObject:app_window]);
}
class AppControllerWebAppBrowserTest : public InProcessBrowserTest {
protected:
AppControllerWebAppBrowserTest()
: active_browser_list_(BrowserList::GetInstance(
chrome::GetActiveDesktop())) {
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
command_line->AppendSwitchASCII(switches::kApp, GetAppURL());
}
std::string GetAppURL() const {
return "http://example.com/";
}
const BrowserList* active_browser_list_;
};
// Test that in web app mode a reopen event opens the app URL.
IN_PROC_BROWSER_TEST_F(AppControllerWebAppBrowserTest,
WebAppReopenWithNoWindows) {
base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
EXPECT_EQ(1u, active_browser_list_->size());
BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
EXPECT_FALSE(result);
EXPECT_EQ(2u, active_browser_list_->size());
Browser* browser = active_browser_list_->get(0);
GURL current_url =
browser->tab_strip_model()->GetActiveWebContents()->GetURL();
EXPECT_EQ(GetAppURL(), current_url.spec());
}
// Called when the ProfileManager has created a profile.
void CreateProfileCallback(const base::Closure& quit_closure,
Profile* profile,
Profile::CreateStatus status) {
EXPECT_TRUE(profile);
EXPECT_NE(Profile::CREATE_STATUS_LOCAL_FAIL, status);
EXPECT_NE(Profile::CREATE_STATUS_REMOTE_FAIL, status);
// This will be called multiple times. Wait until the profile is initialized
// fully to quit the loop.
if (status == Profile::CREATE_STATUS_INITIALIZED)
quit_closure.Run();
}
void CreateAndWaitForGuestProfile() {
ProfileManager::CreateCallback create_callback =
base::Bind(&CreateProfileCallback,
base::MessageLoop::current()->QuitClosure());
g_browser_process->profile_manager()->CreateProfileAsync(
ProfileManager::GetGuestProfilePath(),
create_callback,
base::string16(),
base::string16(),
std::string());
base::RunLoop().Run();
}
class AppControllerNewProfileManagementBrowserTest
: public InProcessBrowserTest {
protected:
AppControllerNewProfileManagementBrowserTest()
: active_browser_list_(BrowserList::GetInstance(
chrome::GetActiveDesktop())) {
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
switches::EnableNewProfileManagementForTesting(command_line);
}
const BrowserList* active_browser_list_;
};
// Test that for a regular last profile, a reopen event opens a browser.
IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
RegularProfileReopenWithNoWindows) {
base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
EXPECT_EQ(1u, active_browser_list_->size());
BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
EXPECT_FALSE(result);
EXPECT_EQ(2u, active_browser_list_->size());
EXPECT_FALSE(UserManager::IsShowing());
}
// Test that for a locked last profile, a reopen event opens the User Manager.
IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
LockedProfileReopenWithNoWindows) {
// The User Manager uses the guest profile as its underlying profile. To
// minimize flakiness due to the scheduling/descheduling of tasks on the
// different threads, pre-initialize the guest profile before it is needed.
CreateAndWaitForGuestProfile();
base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
// Lock the active profile.
Profile* profile = [ac lastProfile];
ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
cache.SetProfileSigninRequiredAtIndex(profile_index, true);
EXPECT_TRUE(cache.ProfileIsSigninRequiredAtIndex(profile_index));
EXPECT_EQ(1u, active_browser_list_->size());
BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
EXPECT_FALSE(result);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, active_browser_list_->size());
EXPECT_TRUE(UserManager::IsShowing());
UserManager::Hide();
}
// Test that for a guest last profile, a reopen event opens the User Manager.
IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
GuestProfileReopenWithNoWindows) {
// Create the guest profile, and set it as the last used profile so the
// app controller can use it on init.
CreateAndWaitForGuestProfile();
PrefService* local_state = g_browser_process->local_state();
local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir);
base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
Profile* profile = [ac lastProfile];
EXPECT_EQ(ProfileManager::GetGuestProfilePath(), profile->GetPath());
EXPECT_TRUE(profile->IsGuestSession());
EXPECT_EQ(1u, active_browser_list_->size());
BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
EXPECT_FALSE(result);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, active_browser_list_->size());
EXPECT_TRUE(UserManager::IsShowing());
UserManager::Hide();
}
class AppControllerOpenShortcutBrowserTest : public InProcessBrowserTest {
protected:
AppControllerOpenShortcutBrowserTest() {
}
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
// In order to mimic opening shortcut during browser startup, we need to
// send the event before -applicationDidFinishLaunching is called, but
// after AppController is loaded.
//
// Since -applicationWillFinishLaunching does nothing now, we swizzle it to
// our function to send the event. We need to do this early before running
// the main message loop.
//
// NSApp does not exist yet. We need to get the AppController using
// reflection.
Class appControllerClass = NSClassFromString(@"AppController");
Class openShortcutClass = NSClassFromString(@"TestOpenShortcutOnStartup");
ASSERT_TRUE(appControllerClass != nil);
ASSERT_TRUE(openShortcutClass != nil);
SEL targetMethod = @selector(applicationWillFinishLaunching:);
Method original = class_getInstanceMethod(appControllerClass,
targetMethod);
Method destination = class_getInstanceMethod(openShortcutClass,
targetMethod);
ASSERT_TRUE(original != NULL);
ASSERT_TRUE(destination != NULL);
method_exchangeImplementations(original, destination);
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
g_open_shortcut_url = embedded_test_server()->GetURL("/simple.html");
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
// If the arg is empty, PrepareTestCommandLine() after this function will
// append about:blank as default url.
command_line->AppendArg(chrome::kChromeUINewTabURL);
}
};
IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest,
OpenShortcutOnStartup) {
EXPECT_EQ(1, browser()->tab_strip_model()->count());
EXPECT_EQ(g_open_shortcut_url,
browser()->tab_strip_model()->GetActiveWebContents()
->GetLastCommittedURL());
}
} // namespace
|