blob: f728b392fc6d7c32390e6e6385fa7afd7d584a33 [file] [log] [blame]
[email protected]fc44f242012-02-14 16:54:391// Copyright (c) 2012 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
[email protected]66791aff2014-04-29 09:45:385#import <Carbon/Carbon.h>
[email protected]fc44f242012-02-14 16:54:396#import <Cocoa/Cocoa.h>
[email protected]66791aff2014-04-29 09:45:387#import <Foundation/Foundation.h>
8#import <Foundation/NSAppleEventDescriptor.h>
9#import <objc/message.h>
10#import <objc/runtime.h>
avie4d7b6f2015-12-26 00:59:1811#include <stddef.h>
[email protected]fc44f242012-02-14 16:54:3912
Sebastien Marchandf1349f52019-01-25 03:16:4113#include "base/bind.h"
David Roger51797642018-03-20 13:29:1614#include "base/bind_helpers.h"
[email protected]fc44f242012-02-14 16:54:3915#include "base/command_line.h"
[email protected]66791aff2014-04-29 09:45:3816#include "base/mac/foundation_util.h"
[email protected]a8522032013-06-24 22:51:4617#include "base/mac/scoped_nsobject.h"
thestigaf7f4152014-10-31 23:19:1518#include "base/run_loop.h"
lgarron9e6dee22014-11-18 01:03:3919#include "base/strings/sys_string_conversions.h"
20#include "base/strings/utf_string_conversions.h"
jam3f2d3932017-04-26 20:28:5121#include "base/threading/thread_restrictions.h"
[email protected]fc44f242012-02-14 16:54:3922#include "chrome/app/chrome_command_ids.h"
23#import "chrome/browser/app_controller_mac.h"
Dominick Ng6ff51052018-07-06 05:30:2024#include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
lgarron9e6dee22014-11-18 01:03:3925#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]dbb03fb2014-02-15 05:36:3326#include "chrome/browser/browser_process.h"
mlerman8ae56aa2015-04-24 13:56:2727#include "chrome/browser/history/history_service_factory.h"
Trent Apted45d9ae22017-11-09 23:12:3228#include "chrome/browser/lifetime/application_lifetime.h"
lwchkg498e92492016-04-23 11:04:1229#include "chrome/browser/profiles/profile_attributes_entry.h"
30#include "chrome/browser/profiles/profile_attributes_storage.h"
[email protected]7108d912014-01-30 08:10:4531#include "chrome/browser/profiles/profile_manager.h"
[email protected]52877dbc62012-06-29 22:22:0332#include "chrome/browser/ui/browser.h"
Trent Apted45d9ae22017-11-09 23:12:3233#include "chrome/browser/ui/browser_finder.h"
[email protected]52877dbc62012-06-29 22:22:0334#include "chrome/browser/ui/browser_list.h"
thestige80821242015-09-30 23:46:0835#include "chrome/browser/ui/browser_navigator_params.h"
[email protected]ee9ccfd42013-07-23 02:31:4736#include "chrome/browser/ui/browser_window.h"
lgarron9e6dee22014-11-18 01:03:3937#include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
mlerman8ae56aa2015-04-24 13:56:2738#include "chrome/browser/ui/cocoa/history_menu_bridge.h"
Christian Dullweber820ee5f2018-04-26 13:24:3139#include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h"
lgrey92aad3c2016-12-10 01:22:1840#include "chrome/browser/ui/cocoa/test/run_loop_testing.h"
kristiparka34b247a2018-03-09 19:05:3441#include "chrome/browser/ui/search/local_ntp_test_utils.h"
[email protected]617ee962013-01-29 20:49:1242#include "chrome/browser/ui/tabs/tab_strip_model.h"
mlermane29d0032014-09-24 19:31:2643#include "chrome/browser/ui/user_manager.h"
Hector Carmonad6664a02019-08-01 01:58:4844#include "chrome/browser/ui/webui/welcome/helpers.h"
[email protected]7108d912014-01-30 08:10:4545#include "chrome/common/chrome_constants.h"
46#include "chrome/common/chrome_switches.h"
47#include "chrome/common/pref_names.h"
[email protected]66791aff2014-04-29 09:45:3848#include "chrome/common/url_constants.h"
[email protected]fc44f242012-02-14 16:54:3949#include "chrome/test/base/in_process_browser_test.h"
erikchen600f7962014-12-12 00:17:3850#include "chrome/test/base/ui_test_utils.h"
tapted574f09c2015-05-19 13:08:0851#include "components/bookmarks/browser/bookmark_model.h"
lgarron9e6dee22014-11-18 01:03:3952#include "components/bookmarks/test/bookmark_test_helpers.h"
brettwb1fc1b82016-02-02 00:19:0853#include "components/prefs/pref_service.h"
tmartino82e55782017-01-10 22:30:1754#include "content/public/browser/navigation_controller.h"
[email protected]fc44f242012-02-14 16:54:3955#include "content/public/browser/web_contents.h"
erikchen600f7962014-12-12 00:17:3856#include "content/public/test/browser_test_utils.h"
erikchen19ee3922014-10-31 19:14:2257#include "content/public/test/test_navigation_observer.h"
hashimotoad3c6872014-08-29 09:46:5758#include "extensions/browser/app_window/app_window_registry.h"
[email protected]e4452d32013-11-15 23:07:4159#include "extensions/common/extension.h"
lfg910f2f92014-09-19 05:31:0960#include "extensions/test/extension_test_message_listener.h"
[email protected]66791aff2014-04-29 09:45:3861#include "net/test/embedded_test_server/embedded_test_server.h"
Trent Apted45d9ae22017-11-09 23:12:3262#import "ui/events/test/cocoa_test_event_utils.h"
[email protected]66791aff2014-04-29 09:45:3863
Adam Riceff26dcf2017-08-14 05:01:5264using base::SysUTF16ToNSString;
65
[email protected]66791aff2014-04-29 09:45:3866namespace {
67
Scott Violet1f106b582017-07-12 15:49:5868GURL g_open_shortcut_url = GURL::EmptyGURL();
[email protected]66791aff2014-04-29 09:45:3869
erikchen19ee3922014-10-31 19:14:2270// Returns an Apple Event that instructs the application to open |url|.
71NSAppleEventDescriptor* AppleEventToOpenUrl(const GURL& url) {
72 NSAppleEventDescriptor* shortcut_event = [[[NSAppleEventDescriptor alloc]
73 initWithEventClass:kASAppleScriptSuite
74 eventID:kASSubroutineEvent
75 targetDescriptor:nil
76 returnID:kAutoGenerateReturnID
77 transactionID:kAnyTransactionID] autorelease];
78 NSString* url_string = [NSString stringWithUTF8String:url.spec().c_str()];
79 [shortcut_event setParamDescriptor:[NSAppleEventDescriptor
80 descriptorWithString:url_string]
81 forKeyword:keyDirectObject];
82 return shortcut_event;
83}
84
85// Instructs the NSApp's delegate to open |url|.
86void SendAppleEventToOpenUrlToAppController(const GURL& url) {
87 AppController* controller =
88 base::mac::ObjCCast<AppController>([NSApp delegate]);
89 Method get_url =
90 class_getInstanceMethod([controller class], @selector(getUrl:withReply:));
91
92 ASSERT_TRUE(get_url);
93
94 NSAppleEventDescriptor* shortcut_event = AppleEventToOpenUrl(url);
95
96 method_invoke(controller, get_url, shortcut_event, NULL);
97}
98
mlerman8ae56aa2015-04-24 13:56:2799void RunClosureWhenProfileInitialized(const base::Closure& closure,
100 Profile* profile,
101 Profile::CreateStatus status) {
102 if (status == Profile::CREATE_STATUS_INITIALIZED)
103 closure.Run();
104}
105
[email protected]66791aff2014-04-29 09:45:38106} // namespace
107
108@interface TestOpenShortcutOnStartup : NSObject
109- (void)applicationWillFinishLaunching:(NSNotification*)notification;
110@end
111
112@implementation TestOpenShortcutOnStartup
113
114- (void)applicationWillFinishLaunching:(NSNotification*)notification {
Scott Violet1f106b582017-07-12 15:49:58115 if (!g_open_shortcut_url.is_valid())
116 return;
117
erikchen19ee3922014-10-31 19:14:22118 SendAppleEventToOpenUrlToAppController(g_open_shortcut_url);
[email protected]66791aff2014-04-29 09:45:38119}
120
121@end
[email protected]fc44f242012-02-14 16:54:39122
123namespace {
124
Trent Apted45d9ae22017-11-09 23:12:32125using AppControllerBrowserTest = InProcessBrowserTest;
126
Elly Fong-Jones85046032018-04-19 19:15:28127// Returns whether a window's pixels are actually on the screen, which is the
128// case when it and all of its parents are marked visible.
129bool IsReallyVisible(NSWindow* window) {
130 while (window) {
131 if (!window.visible)
132 return false;
133 window = [window parentWindow];
134 }
135 return true;
136}
137
Trent Apted45d9ae22017-11-09 23:12:32138size_t CountVisibleWindows() {
139 size_t count = 0;
140 for (NSWindow* w in [NSApp windows])
Elly Fong-Jones85046032018-04-19 19:15:28141 count = count + (IsReallyVisible(w) ? 1 : 0);
Trent Apted45d9ae22017-11-09 23:12:32142 return count;
143}
144
Elly Fong-Jones85046032018-04-19 19:15:28145// Returns how many visible NSWindows are expected for a given count of browser
Sidney San Martínb247d53f2018-05-23 02:31:21146// windows.
Elly Fong-Jones85046032018-04-19 19:15:28147size_t ExpectedWindowCountForBrowserCount(size_t browsers) {
Elly Fong-Jones85046032018-04-19 19:15:28148 return browsers;
149}
150
Trent Apted45d9ae22017-11-09 23:12:32151// Test browser shutdown with a command in the message queue.
152IN_PROC_BROWSER_TEST_F(AppControllerBrowserTest, CommandDuringShutdown) {
153 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
Elly Fong-Jones85046032018-04-19 19:15:28154 EXPECT_EQ(ExpectedWindowCountForBrowserCount(1), CountVisibleWindows());
Trent Apted45d9ae22017-11-09 23:12:32155
156 chrome::AttemptExit(); // Set chrome::IsTryingToQuit and close all windows.
157
158 // Opening a new window here is fine (unload handlers can also interrupt
159 // exit). But closing the window posts an autorelease on
160 // BrowserWindowController, which calls ~Browser() and, if that was the last
161 // Browser, it invokes applicationWillTerminate: (because IsTryingToQuit is
162 // set). So, verify assumptions then process that autorelease.
163
164 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
Elly Fong-Jones85046032018-04-19 19:15:28165 EXPECT_EQ(ExpectedWindowCountForBrowserCount(0), CountVisibleWindows());
Trent Apted45d9ae22017-11-09 23:12:32166
167 base::RunLoop().RunUntilIdle();
168
169 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
Elly Fong-Jones85046032018-04-19 19:15:28170 EXPECT_EQ(ExpectedWindowCountForBrowserCount(0), CountVisibleWindows());
Trent Apted45d9ae22017-11-09 23:12:32171
172 NSEvent* cmd_n = cocoa_test_event_utils::KeyEventWithKeyCode(
173 'n', 'n', NSKeyDown, NSCommandKeyMask);
174 [[NSApp mainMenu] performSelector:@selector(performKeyEquivalent:)
175 withObject:cmd_n
176 afterDelay:0];
177 // Let the run loop get flushed, during process cleanup and try not to crash.
178}
179
[email protected]ee9ccfd42013-07-23 02:31:47180class AppControllerPlatformAppBrowserTest
181 : public extensions::PlatformAppBrowserTest {
[email protected]fc44f242012-02-14 16:54:39182 protected:
[email protected]b4207c42013-02-12 06:44:20183 AppControllerPlatformAppBrowserTest()
scottmg0d8e4ab2016-01-28 00:34:55184 : active_browser_list_(BrowserList::GetInstance()) {}
[email protected]fc44f242012-02-14 16:54:39185
avi3ef9ec9e2014-12-22 22:50:17186 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]ee9ccfd42013-07-23 02:31:47187 PlatformAppBrowserTest::SetUpCommandLine(command_line);
[email protected]fc44f242012-02-14 16:54:39188 command_line->AppendSwitchASCII(switches::kAppId,
189 "1234");
190 }
[email protected]b4207c42013-02-12 06:44:20191
[email protected]7d1a810b2013-06-26 19:51:59192 const BrowserList* active_browser_list_;
[email protected]fc44f242012-02-14 16:54:39193};
194
195// Test that if only a platform app window is open and no browser windows are
196// open then a reopen event does nothing.
197IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest,
198 PlatformAppReopenWithWindows) {
Owen Minb6e1bf292018-03-15 18:31:52199 AppController* ac = base::mac::ObjCCast<AppController>(
200 [[NSApplication sharedApplication] delegate]);
201 ASSERT_TRUE(ac);
202
[email protected]fc44f242012-02-14 16:54:39203 NSUInteger old_window_count = [[NSApp windows] count];
[email protected]7d1a810b2013-06-26 19:51:59204 EXPECT_EQ(1u, active_browser_list_->size());
[email protected]a25920ee2013-09-05 19:38:49205 [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:YES];
206 // We do not EXPECT_TRUE the result here because the method
207 // deminiaturizes windows manually rather than return YES and have
208 // AppKit do it.
[email protected]fc44f242012-02-14 16:54:39209
[email protected]fc44f242012-02-14 16:54:39210 EXPECT_EQ(old_window_count, [[NSApp windows] count]);
[email protected]7d1a810b2013-06-26 19:51:59211 EXPECT_EQ(1u, active_browser_list_->size());
[email protected]fc44f242012-02-14 16:54:39212}
213
[email protected]2e29e2232013-07-26 10:40:59214IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest,
215 ActivationFocusesBrowserWindow) {
Owen Minb6e1bf292018-03-15 18:31:52216 AppController* app_controller = base::mac::ObjCCast<AppController>(
217 [[NSApplication sharedApplication] delegate]);
218 ASSERT_TRUE(app_controller);
[email protected]2e29e2232013-07-26 10:40:59219
220 ExtensionTestMessageListener listener("Launched", false);
221 const extensions::Extension* app =
222 InstallAndLaunchPlatformApp("minimal");
223 ASSERT_TRUE(listener.WaitUntilSatisfied());
224
hashimotoad3c6872014-08-29 09:46:57225 NSWindow* app_window = extensions::AppWindowRegistry::Get(profile())
[email protected]dbb03fb2014-02-15 05:36:33226 ->GetAppWindowsForApp(app->id())
227 .front()
Christopher Cameron359c61e22018-10-18 08:06:20228 ->GetNativeWindow()
229 .GetNativeNSWindow();
230 NSWindow* browser_window =
231 browser()->window()->GetNativeWindow().GetNativeNSWindow();
[email protected]2e29e2232013-07-26 10:40:59232
andybons346c7c62015-08-13 15:18:03233 chrome::testing::NSRunLoopRunAllPending();
[email protected]2e29e2232013-07-26 10:40:59234 EXPECT_LE([[NSApp orderedWindows] indexOfObject:app_window],
235 [[NSApp orderedWindows] indexOfObject:browser_window]);
236 [app_controller applicationShouldHandleReopen:NSApp
237 hasVisibleWindows:YES];
andybons346c7c62015-08-13 15:18:03238 chrome::testing::NSRunLoopRunAllPending();
[email protected]2e29e2232013-07-26 10:40:59239 EXPECT_LE([[NSApp orderedWindows] indexOfObject:browser_window],
240 [[NSApp orderedWindows] indexOfObject:app_window]);
241}
242
[email protected]fc44f242012-02-14 16:54:39243class AppControllerWebAppBrowserTest : public InProcessBrowserTest {
244 protected:
[email protected]b4207c42013-02-12 06:44:20245 AppControllerWebAppBrowserTest()
scottmg0d8e4ab2016-01-28 00:34:55246 : active_browser_list_(BrowserList::GetInstance()) {}
[email protected]fc44f242012-02-14 16:54:39247
avi3ef9ec9e2014-12-22 22:50:17248 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]90ca44272012-07-18 18:15:48249 command_line->AppendSwitchASCII(switches::kApp, GetAppURL());
[email protected]fc44f242012-02-14 16:54:39250 }
251
252 std::string GetAppURL() const {
253 return "http://example.com/";
254 }
[email protected]b4207c42013-02-12 06:44:20255
[email protected]7d1a810b2013-06-26 19:51:59256 const BrowserList* active_browser_list_;
[email protected]fc44f242012-02-14 16:54:39257};
258
259// Test that in web app mode a reopen event opens the app URL.
260IN_PROC_BROWSER_TEST_F(AppControllerWebAppBrowserTest,
261 WebAppReopenWithNoWindows) {
Owen Minb6e1bf292018-03-15 18:31:52262 AppController* ac = base::mac::ObjCCast<AppController>(
263 [[NSApplication sharedApplication] delegate]);
264 ASSERT_TRUE(ac);
265
[email protected]7d1a810b2013-06-26 19:51:59266 EXPECT_EQ(1u, active_browser_list_->size());
[email protected]fc44f242012-02-14 16:54:39267 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
268
269 EXPECT_FALSE(result);
[email protected]7d1a810b2013-06-26 19:51:59270 EXPECT_EQ(2u, active_browser_list_->size());
[email protected]fc44f242012-02-14 16:54:39271
[email protected]7d1a810b2013-06-26 19:51:59272 Browser* browser = active_browser_list_->get(0);
[email protected]617ee962013-01-29 20:49:12273 GURL current_url =
274 browser->tab_strip_model()->GetActiveWebContents()->GetURL();
[email protected]fc44f242012-02-14 16:54:39275 EXPECT_EQ(GetAppURL(), current_url.spec());
276}
277
[email protected]7108d912014-01-30 08:10:45278// Called when the ProfileManager has created a profile.
279void CreateProfileCallback(const base::Closure& quit_closure,
280 Profile* profile,
281 Profile::CreateStatus status) {
282 EXPECT_TRUE(profile);
283 EXPECT_NE(Profile::CREATE_STATUS_LOCAL_FAIL, status);
284 EXPECT_NE(Profile::CREATE_STATUS_REMOTE_FAIL, status);
285 // This will be called multiple times. Wait until the profile is initialized
286 // fully to quit the loop.
287 if (status == Profile::CREATE_STATUS_INITIALIZED)
288 quit_closure.Run();
289}
290
mlermanb8df6e82015-01-23 19:55:34291void CreateAndWaitForSystemProfile() {
[email protected]7108d912014-01-30 08:10:45292 ProfileManager::CreateCallback create_callback =
293 base::Bind(&CreateProfileCallback,
Gabriel Charette87f2bef72018-05-17 20:37:53294 base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
[email protected]7108d912014-01-30 08:10:45295 g_browser_process->profile_manager()->CreateProfileAsync(
mlermanb8df6e82015-01-23 19:55:34296 ProfileManager::GetSystemProfilePath(),
[email protected]7108d912014-01-30 08:10:45297 create_callback,
298 base::string16(),
[email protected]7108d912014-01-30 08:10:45299 std::string());
300 base::RunLoop().Run();
301}
302
303class AppControllerNewProfileManagementBrowserTest
304 : public InProcessBrowserTest {
305 protected:
306 AppControllerNewProfileManagementBrowserTest()
scottmg0d8e4ab2016-01-28 00:34:55307 : active_browser_list_(BrowserList::GetInstance()) {}
[email protected]7108d912014-01-30 08:10:45308
[email protected]7108d912014-01-30 08:10:45309 const BrowserList* active_browser_list_;
310};
311
312// Test that for a regular last profile, a reopen event opens a browser.
313IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
314 RegularProfileReopenWithNoWindows) {
Owen Minb6e1bf292018-03-15 18:31:52315 AppController* ac = base::mac::ObjCCast<AppController>(
316 [[NSApplication sharedApplication] delegate]);
317 ASSERT_TRUE(ac);
318
[email protected]7108d912014-01-30 08:10:45319 EXPECT_EQ(1u, active_browser_list_->size());
320 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
321
322 EXPECT_FALSE(result);
323 EXPECT_EQ(2u, active_browser_list_->size());
mlermane29d0032014-09-24 19:31:26324 EXPECT_FALSE(UserManager::IsShowing());
[email protected]7108d912014-01-30 08:10:45325}
326
327// Test that for a locked last profile, a reopen event opens the User Manager.
328IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
329 LockedProfileReopenWithNoWindows) {
mlermanb8df6e82015-01-23 19:55:34330 // The User Manager uses the system profile as its underlying profile. To
[email protected]7108d912014-01-30 08:10:45331 // minimize flakiness due to the scheduling/descheduling of tasks on the
332 // different threads, pre-initialize the guest profile before it is needed.
mlermanb8df6e82015-01-23 19:55:34333 CreateAndWaitForSystemProfile();
Owen Minb6e1bf292018-03-15 18:31:52334 AppController* ac = base::mac::ObjCCast<AppController>(
335 [[NSApplication sharedApplication] delegate]);
336 ASSERT_TRUE(ac);
[email protected]7108d912014-01-30 08:10:45337
338 // Lock the active profile.
Francois Doraye6fb2d02017-10-18 21:29:13339 base::ScopedAllowBlockingForTesting allow_blocking;
[email protected]7108d912014-01-30 08:10:45340 Profile* profile = [ac lastProfile];
lwchkg9c183942016-03-13 06:29:54341 ProfileAttributesEntry* entry;
342 ASSERT_TRUE(g_browser_process->profile_manager()->
343 GetProfileAttributesStorage().
344 GetProfileAttributesWithPath(profile->GetPath(), &entry));
345 entry->SetIsSigninRequired(true);
346 EXPECT_TRUE(entry->IsSigninRequired());
[email protected]7108d912014-01-30 08:10:45347
348 EXPECT_EQ(1u, active_browser_list_->size());
349 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
350 EXPECT_FALSE(result);
351
352 base::RunLoop().RunUntilIdle();
353 EXPECT_EQ(1u, active_browser_list_->size());
mlermane29d0032014-09-24 19:31:26354 EXPECT_TRUE(UserManager::IsShowing());
355 UserManager::Hide();
[email protected]7108d912014-01-30 08:10:45356}
357
Owen Minb6e1bf292018-03-15 18:31:52358// Test that for a guest last profile, commandDispatch should open UserManager
359// if guest mode is disabled. Note that this test might be flaky under ASAN
360// due to https://crbug.com/674475. Please disable this test under ASAN
361// as the tests below if that happened.
362IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
363 OpenGuestProfileOnlyIfGuestModeIsEnabled) {
364 CreateAndWaitForSystemProfile();
365 PrefService* local_state = g_browser_process->local_state();
366 local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir);
367 local_state->SetBoolean(prefs::kBrowserGuestModeEnabled, false);
368
369 AppController* ac = base::mac::ObjCCast<AppController>(
370 [[NSApplication sharedApplication] delegate]);
371 ASSERT_TRUE(ac);
372
373 base::ScopedAllowBlockingForTesting allow_blocking;
374 Profile* profile = [ac lastProfile];
375 EXPECT_TRUE(profile->IsGuestSession());
376
377 NSMenu* menu = [ac applicationDockMenu:NSApp];
378 ASSERT_TRUE(menu);
379 NSMenuItem* item = [menu itemWithTag:IDC_NEW_WINDOW];
380 ASSERT_TRUE(item);
381 EXPECT_EQ(1u, active_browser_list_->size());
382
383 [ac commandDispatch:item];
384
385 base::RunLoop().RunUntilIdle();
386
387 EXPECT_EQ(1u, active_browser_list_->size());
388 EXPECT_TRUE(UserManager::IsShowing());
389 UserManager::Hide();
390
391 local_state->SetBoolean(prefs::kBrowserGuestModeEnabled, true);
392 [ac commandDispatch:item];
393 base::RunLoop().RunUntilIdle();
394 EXPECT_EQ(2u, active_browser_list_->size());
395 EXPECT_FALSE(UserManager::IsShowing());
396}
397
guidoubbb4787a2016-12-15 12:34:43398#if defined(ADDRESS_SANITIZER)
399// Flaky under ASAN. See https://crbug.com/674475.
400#define MAYBE_GuestProfileReopenWithNoWindows DISABLED_GuestProfileReopenWithNoWindows
401#else
402#define MAYBE_GuestProfileReopenWithNoWindows GuestProfileReopenWithNoWindows
403#endif
[email protected]7108d912014-01-30 08:10:45404// Test that for a guest last profile, a reopen event opens the User Manager.
405IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
guidoubbb4787a2016-12-15 12:34:43406 MAYBE_GuestProfileReopenWithNoWindows) {
mlermanb8df6e82015-01-23 19:55:34407 // Create the system profile. Set the guest as the last used profile so the
[email protected]7108d912014-01-30 08:10:45408 // app controller can use it on init.
mlermanb8df6e82015-01-23 19:55:34409 CreateAndWaitForSystemProfile();
[email protected]7108d912014-01-30 08:10:45410 PrefService* local_state = g_browser_process->local_state();
411 local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir);
412
Owen Minb6e1bf292018-03-15 18:31:52413 AppController* ac = base::mac::ObjCCast<AppController>(
414 [[NSApplication sharedApplication] delegate]);
415 ASSERT_TRUE(ac);
[email protected]7108d912014-01-30 08:10:45416
Francois Doraye6fb2d02017-10-18 21:29:13417 base::ScopedAllowBlockingForTesting allow_blocking;
[email protected]7108d912014-01-30 08:10:45418 Profile* profile = [ac lastProfile];
419 EXPECT_EQ(ProfileManager::GetGuestProfilePath(), profile->GetPath());
420 EXPECT_TRUE(profile->IsGuestSession());
421
422 EXPECT_EQ(1u, active_browser_list_->size());
423 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
424 EXPECT_FALSE(result);
425
426 base::RunLoop().RunUntilIdle();
427
428 EXPECT_EQ(1u, active_browser_list_->size());
mlermane29d0032014-09-24 19:31:26429 EXPECT_TRUE(UserManager::IsShowing());
430 UserManager::Hide();
[email protected]7108d912014-01-30 08:10:45431}
432
guidoubbb4787a2016-12-15 12:34:43433#if defined(ADDRESS_SANITIZER)
434// Flaky under ASAN. See https://crbug.com/674475.
435#define MAYBE_AboutChromeForcesUserManager DISABLED_AboutChromeForcesUserManager
436#else
437#define MAYBE_AboutChromeForcesUserManager AboutChromeForcesUserManager
438#endif
mlermane6e040a2014-10-31 00:53:21439IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
guidoubbb4787a2016-12-15 12:34:43440 MAYBE_AboutChromeForcesUserManager) {
Owen Minb6e1bf292018-03-15 18:31:52441 AppController* ac = base::mac::ObjCCast<AppController>(
442 [[NSApplication sharedApplication] delegate]);
443 ASSERT_TRUE(ac);
mlermane6e040a2014-10-31 00:53:21444
445 // Create the guest profile, and set it as the last used profile so the
446 // app controller can use it on init.
mlermanb8df6e82015-01-23 19:55:34447 CreateAndWaitForSystemProfile();
mlermane6e040a2014-10-31 00:53:21448 PrefService* local_state = g_browser_process->local_state();
449 local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir);
450
451 // Prohibiting guest mode forces the user manager flow for About Chrome.
452 local_state->SetBoolean(prefs::kBrowserGuestModeEnabled, false);
453
Francois Doraye6fb2d02017-10-18 21:29:13454 base::ScopedAllowBlockingForTesting allow_blocking;
mlermane6e040a2014-10-31 00:53:21455 Profile* guest_profile = [ac lastProfile];
456 EXPECT_EQ(ProfileManager::GetGuestProfilePath(), guest_profile->GetPath());
457 EXPECT_TRUE(guest_profile->IsGuestSession());
458
459 // Tell the browser to open About Chrome.
460 EXPECT_EQ(1u, active_browser_list_->size());
461 [ac orderFrontStandardAboutPanel:NSApp];
462
463 base::RunLoop().RunUntilIdle();
464
465 // No new browser is opened; the User Manager opens instead.
466 EXPECT_EQ(1u, active_browser_list_->size());
467 EXPECT_TRUE(UserManager::IsShowing());
468
469 UserManager::Hide();
470}
471
[email protected]66791aff2014-04-29 09:45:38472class AppControllerOpenShortcutBrowserTest : public InProcessBrowserTest {
473 protected:
474 AppControllerOpenShortcutBrowserTest() {
Hector Carmona9797cc62019-08-09 20:35:35475 scoped_feature_list_.InitWithFeatures({welcome::kForceEnabled}, {});
[email protected]66791aff2014-04-29 09:45:38476 }
477
Daniel Chenga542fca2014-10-21 09:51:29478 void SetUpInProcessBrowserTestFixture() override {
[email protected]66791aff2014-04-29 09:45:38479 // In order to mimic opening shortcut during browser startup, we need to
480 // send the event before -applicationDidFinishLaunching is called, but
481 // after AppController is loaded.
482 //
483 // Since -applicationWillFinishLaunching does nothing now, we swizzle it to
484 // our function to send the event. We need to do this early before running
485 // the main message loop.
486 //
487 // NSApp does not exist yet. We need to get the AppController using
488 // reflection.
489 Class appControllerClass = NSClassFromString(@"AppController");
490 Class openShortcutClass = NSClassFromString(@"TestOpenShortcutOnStartup");
491
492 ASSERT_TRUE(appControllerClass != nil);
493 ASSERT_TRUE(openShortcutClass != nil);
494
495 SEL targetMethod = @selector(applicationWillFinishLaunching:);
496 Method original = class_getInstanceMethod(appControllerClass,
497 targetMethod);
498 Method destination = class_getInstanceMethod(openShortcutClass,
499 targetMethod);
500
501 ASSERT_TRUE(original != NULL);
502 ASSERT_TRUE(destination != NULL);
503
504 method_exchangeImplementations(original, destination);
Scott Violet1f106b582017-07-12 15:49:58505
506 ASSERT_TRUE(embedded_test_server()->Start());
507 g_open_shortcut_url = embedded_test_server()->GetURL("/simple.html");
[email protected]66791aff2014-04-29 09:45:38508 }
509
avi3ef9ec9e2014-12-22 22:50:17510 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]66791aff2014-04-29 09:45:38511 // If the arg is empty, PrepareTestCommandLine() after this function will
512 // append about:blank as default url.
513 command_line->AppendArg(chrome::kChromeUINewTabURL);
514 }
Hector Carmona1e62e352019-06-24 19:18:17515
516 private:
517 base::test::ScopedFeatureList scoped_feature_list_;
[email protected]66791aff2014-04-29 09:45:38518};
519
520IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest,
521 OpenShortcutOnStartup) {
tmartino82e55782017-01-10 22:30:17522 // The two tabs expected are the Welcome page and the desired URL.
523 EXPECT_EQ(2, browser()->tab_strip_model()->count());
[email protected]66791aff2014-04-29 09:45:38524 EXPECT_EQ(g_open_shortcut_url,
525 browser()->tab_strip_model()->GetActiveWebContents()
526 ->GetLastCommittedURL());
527}
528
erikchen19ee3922014-10-31 19:14:22529class AppControllerReplaceNTPBrowserTest : public InProcessBrowserTest {
530 protected:
531 AppControllerReplaceNTPBrowserTest() {}
532
533 void SetUpInProcessBrowserTestFixture() override {
svaldeza01f7d92015-11-18 17:47:56534 ASSERT_TRUE(embedded_test_server()->Start());
erikchen19ee3922014-10-31 19:14:22535 }
536
avi3ef9ec9e2014-12-22 22:50:17537 void SetUpCommandLine(base::CommandLine* command_line) override {
erikchen19ee3922014-10-31 19:14:22538 // If the arg is empty, PrepareTestCommandLine() after this function will
539 // append about:blank as default url.
540 command_line->AppendArg(chrome::kChromeUINewTabURL);
541 }
542};
543
544// Tests that when a GURL is opened after startup, it replaces the NTP.
545IN_PROC_BROWSER_TEST_F(AppControllerReplaceNTPBrowserTest,
546 ReplaceNTPAfterStartup) {
kristiparka34b247a2018-03-09 19:05:34547 // Depending on network connectivity, the NTP URL can either be
548 // chrome://newtab/ or chrome-search://local-ntp/local-ntp.html. See
549 // local_ntp_test_utils::GetFinalNtpUrl for more details.
550 std::string expected_url =
551 local_ntp_test_utils::GetFinalNtpUrl(browser()->profile()).spec();
552
erikchen19ee3922014-10-31 19:14:22553 // Ensure that there is exactly 1 tab showing, and the tab is the NTP.
kristiparka34b247a2018-03-09 19:05:34554 GURL ntp(expected_url);
erikchen19ee3922014-10-31 19:14:22555 EXPECT_EQ(1, browser()->tab_strip_model()->count());
tmartino82e55782017-01-10 22:30:17556 browser()->tab_strip_model()->GetActiveWebContents()->GetController().LoadURL(
kristiparka34b247a2018-03-09 19:05:34557 GURL(expected_url), content::Referrer(),
tmartino82e55782017-01-10 22:30:17558 ui::PageTransition::PAGE_TRANSITION_LINK, std::string());
559
560 // Wait for one navigation on the active web contents.
561 content::TestNavigationObserver ntp_navigation_observer(
562 browser()->tab_strip_model()->GetActiveWebContents());
563 ntp_navigation_observer.Wait();
564
erikchen19ee3922014-10-31 19:14:22565 EXPECT_EQ(ntp,
566 browser()
567 ->tab_strip_model()
568 ->GetActiveWebContents()
569 ->GetLastCommittedURL());
570
571 GURL simple(embedded_test_server()->GetURL("/simple.html"));
572 SendAppleEventToOpenUrlToAppController(simple);
573
erikchen19ee3922014-10-31 19:14:22574 EXPECT_EQ(1, browser()->tab_strip_model()->count());
tmartino82e55782017-01-10 22:30:17575 content::TestNavigationObserver event_navigation_observer(
576 browser()->tab_strip_model()->GetActiveWebContents());
577 event_navigation_observer.Wait();
erikchen19ee3922014-10-31 19:14:22578
579 EXPECT_EQ(simple,
580 browser()
581 ->tab_strip_model()
582 ->GetActiveWebContents()
583 ->GetLastCommittedURL());
584}
585
Christian Dullweber820ee5f2018-04-26 13:24:31586// Tests that when a GURL is opened, it is not opened in incognito mode.
587IN_PROC_BROWSER_TEST_F(AppControllerBrowserTest, OpenInRegularBrowser) {
588 ASSERT_TRUE(embedded_test_server()->Start());
589 // Create an incognito browser.
590 Browser* incognito_browser = CreateIncognitoBrowser(browser()->profile());
591 EXPECT_EQ(incognito_browser, chrome::GetLastActiveBrowser());
592 EXPECT_EQ(1, browser()->tab_strip_model()->count());
593 EXPECT_EQ(1, incognito_browser->tab_strip_model()->count());
594 // Open a url.
595 GURL simple(embedded_test_server()->GetURL("/simple.html"));
596 SendAppleEventToOpenUrlToAppController(simple);
597 // It should be opened in the regular browser.
598 content::TestNavigationObserver event_navigation_observer(
599 browser()->tab_strip_model()->GetActiveWebContents());
600 event_navigation_observer.Wait();
601 EXPECT_EQ(2, browser()->tab_strip_model()->count());
602 EXPECT_EQ(1, incognito_browser->tab_strip_model()->count());
603 EXPECT_EQ(simple, browser()
604 ->tab_strip_model()
605 ->GetActiveWebContents()
606 ->GetLastCommittedURL());
607}
608
lgarron9e6dee22014-11-18 01:03:39609class AppControllerMainMenuBrowserTest : public InProcessBrowserTest {
610 protected:
611 AppControllerMainMenuBrowserTest() {
612 }
613};
614
615IN_PROC_BROWSER_TEST_F(AppControllerMainMenuBrowserTest,
mlerman8ae56aa2015-04-24 13:56:27616 HistoryMenuResetAfterProfileDeletion) {
617 ProfileManager* profile_manager = g_browser_process->profile_manager();
tapted676995d2016-04-18 11:32:29618 AppController* ac =
619 base::mac::ObjCCastStrict<AppController>([NSApp delegate]);
mlerman8ae56aa2015-04-24 13:56:27620
621 // Use the existing profile as profile 1.
622 Profile* profile1 = browser()->profile();
623
624 // Create profile 2.
625 base::FilePath profile2_path =
626 profile_manager->GenerateNextProfileDirectoryPath();
627 base::RunLoop run_loop;
628 profile_manager->CreateProfileAsync(
629 profile2_path,
630 base::Bind(&RunClosureWhenProfileInitialized,
631 run_loop.QuitClosure()),
lwchkg1f62d242015-10-29 00:50:43632 base::string16(),
lwchkg1f62d242015-10-29 00:50:43633 std::string());
mlerman8ae56aa2015-04-24 13:56:27634 run_loop.Run();
635 Profile* profile2 = profile_manager->GetProfileByPath(profile2_path);
636 ASSERT_TRUE(profile2);
637
Sidney San Martínaa3b48f2018-06-03 08:10:05638 // Load profile1's History Service backend so it will be assigned to the
639 // HistoryMenuBridge when windowChangedToProfile is called, or else this test
640 // will fail flaky.
641 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile(
642 profile1, ServiceAccessType::EXPLICIT_ACCESS));
mlerman8ae56aa2015-04-24 13:56:27643 // Switch the controller to profile1.
644 [ac windowChangedToProfile:profile1];
645 base::RunLoop().RunUntilIdle();
646
647 // Verify the controller's History Menu corresponds to profile1.
648 EXPECT_TRUE([ac historyMenuBridge]->service());
649 EXPECT_EQ([ac historyMenuBridge]->service(),
650 HistoryServiceFactory::GetForProfile(profile1,
651 ServiceAccessType::EXPLICIT_ACCESS));
652
653 // Load profile2's History Service backend so it will be assigned to the
654 // HistoryMenuBridge when windowChangedToProfile is called, or else this test
655 // will fail flaky.
656 ui_test_utils::WaitForHistoryToLoad(
657 HistoryServiceFactory::GetForProfile(profile2,
658 ServiceAccessType::EXPLICIT_ACCESS));
659 // Switch the controller to profile2.
660 [ac windowChangedToProfile:profile2];
661 base::RunLoop().RunUntilIdle();
662
663 // Verify the controller's History Menu has changed.
664 EXPECT_TRUE([ac historyMenuBridge]->service());
665 EXPECT_EQ([ac historyMenuBridge]->service(),
666 HistoryServiceFactory::GetForProfile(profile2,
667 ServiceAccessType::EXPLICIT_ACCESS));
668 EXPECT_NE(
669 HistoryServiceFactory::GetForProfile(profile1,
670 ServiceAccessType::EXPLICIT_ACCESS),
671 HistoryServiceFactory::GetForProfile(profile2,
672 ServiceAccessType::EXPLICIT_ACCESS));
673
674 // Delete profile2.
David Roger51797642018-03-20 13:29:16675 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
676 base::DoNothing());
mlerman8ae56aa2015-04-24 13:56:27677 base::RunLoop().RunUntilIdle();
678
679 // Verify the controller's history is back to profile1.
680 EXPECT_EQ([ac historyMenuBridge]->service(),
681 HistoryServiceFactory::GetForProfile(profile1,
682 ServiceAccessType::EXPLICIT_ACCESS));
683}
684
685IN_PROC_BROWSER_TEST_F(AppControllerMainMenuBrowserTest,
lgarron9e6dee22014-11-18 01:03:39686 BookmarksMenuIsRestoredAfterProfileSwitch) {
687 ProfileManager* profile_manager = g_browser_process->profile_manager();
Owen Minb6e1bf292018-03-15 18:31:52688 AppController* ac = base::mac::ObjCCast<AppController>(
689 [[NSApplication sharedApplication] delegate]);
690 ASSERT_TRUE(ac);
691
Robert Sesek124f80f92018-07-24 20:31:29692 [ac mainMenuCreated];
lgarron9e6dee22014-11-18 01:03:39693
694 // Constants for bookmarks that we will create later.
695 const base::string16 title1(base::ASCIIToUTF16("Dinosaur Comics"));
696 const GURL url1("http://qwantz.com//");
697
698 const base::string16 title2(base::ASCIIToUTF16("XKCD"));
699 const GURL url2("https://www.xkcd.com/");
700
701 // Use the existing profile as profile 1.
702 Profile* profile1 = browser()->profile();
703 bookmarks::test::WaitForBookmarkModelToLoad(
pke3e0d5c92016-08-08 09:07:30704 BookmarkModelFactory::GetForBrowserContext(profile1));
lgarron9e6dee22014-11-18 01:03:39705
706 // Create profile 2.
Francois Doraye6fb2d02017-10-18 21:29:13707 base::ScopedAllowBlockingForTesting allow_blocking;
lgarron9e6dee22014-11-18 01:03:39708 base::FilePath path2 = profile_manager->GenerateNextProfileDirectoryPath();
Daniel Hosseinian8f0b7482019-06-07 18:21:04709 std::unique_ptr<Profile> profile2 =
710 Profile::CreateProfile(path2, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
711 Profile* profile2_ptr = profile2.get();
712 profile_manager->RegisterTestingProfile(std::move(profile2), false, true);
lgarron9e6dee22014-11-18 01:03:39713 bookmarks::test::WaitForBookmarkModelToLoad(
Daniel Hosseinian8f0b7482019-06-07 18:21:04714 BookmarkModelFactory::GetForBrowserContext(profile2_ptr));
lgarron9e6dee22014-11-18 01:03:39715
716 // Switch to profile 1, create bookmark 1 and force the menu to build.
717 [ac windowChangedToProfile:profile1];
718 [ac bookmarkMenuBridge]->GetBookmarkModel()->AddURL(
719 [ac bookmarkMenuBridge]->GetBookmarkModel()->bookmark_bar_node(),
720 0, title1, url1);
Trent Aptedcf95689a2017-11-22 00:01:10721 NSMenu* profile1_submenu = [ac bookmarkMenuBridge]->BookmarkMenu();
722 [[profile1_submenu delegate] menuNeedsUpdate:profile1_submenu];
lgarron9e6dee22014-11-18 01:03:39723
724 // Switch to profile 2, create bookmark 2 and force the menu to build.
Daniel Hosseinian8f0b7482019-06-07 18:21:04725 [ac windowChangedToProfile:profile2_ptr];
lgarron9e6dee22014-11-18 01:03:39726 [ac bookmarkMenuBridge]->GetBookmarkModel()->AddURL(
727 [ac bookmarkMenuBridge]->GetBookmarkModel()->bookmark_bar_node(),
728 0, title2, url2);
Trent Aptedcf95689a2017-11-22 00:01:10729 NSMenu* profile2_submenu = [ac bookmarkMenuBridge]->BookmarkMenu();
730 [[profile2_submenu delegate] menuNeedsUpdate:profile2_submenu];
731 EXPECT_NE(profile1_submenu, profile2_submenu);
lgarron9e6dee22014-11-18 01:03:39732
733 // Test that only bookmark 2 is shown.
734 EXPECT_FALSE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle:
735 SysUTF16ToNSString(title1)]);
736 EXPECT_TRUE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle:
737 SysUTF16ToNSString(title2)]);
738
739 // Switch *back* to profile 1 and *don't* force the menu to build.
740 [ac windowChangedToProfile:profile1];
741
742 // Test that only bookmark 1 is shown in the restored menu.
743 EXPECT_TRUE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle:
744 SysUTF16ToNSString(title1)]);
745 EXPECT_FALSE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle:
746 SysUTF16ToNSString(title2)]);
Trent Aptedcf95689a2017-11-22 00:01:10747
748 // Ensure a cached menu was used.
749 EXPECT_EQ(profile1_submenu, [ac bookmarkMenuBridge]->BookmarkMenu());
lgarron9e6dee22014-11-18 01:03:39750}
751
[email protected]fc44f242012-02-14 16:54:39752} // namespace
erikchen600f7962014-12-12 00:17:38753
754//--------------------------AppControllerHandoffBrowserTest---------------------
755
756static GURL g_handoff_url;
757
758@interface AppController (BrowserTest)
erikchen600f7962014-12-12 00:17:38759- (void)new_passURLToHandoffManager:(const GURL&)handoffURL;
760@end
761
762@implementation AppController (BrowserTest)
erikchen600f7962014-12-12 00:17:38763- (void)new_passURLToHandoffManager:(const GURL&)handoffURL {
764 g_handoff_url = handoffURL;
765}
766@end
767
768namespace {
769
770class AppControllerHandoffBrowserTest : public InProcessBrowserTest {
771 protected:
772 AppControllerHandoffBrowserTest() {}
773
774 // Exchanges the implementations of the two selectors on the class
775 // AppController.
776 void ExchangeSelectors(SEL originalMethod, SEL newMethod) {
777 Class appControllerClass = NSClassFromString(@"AppController");
778
779 ASSERT_TRUE(appControllerClass != nil);
780
781 Method original =
782 class_getInstanceMethod(appControllerClass, originalMethod);
783 Method destination = class_getInstanceMethod(appControllerClass, newMethod);
784
785 ASSERT_TRUE(original != NULL);
786 ASSERT_TRUE(destination != NULL);
787
788 method_exchangeImplementations(original, destination);
789 }
790
791 // Swizzle Handoff related implementations.
792 void SetUpInProcessBrowserTestFixture() override {
erikchen600f7962014-12-12 00:17:38793 // This swizzle intercepts the URL that would be sent to the Handoff
794 // Manager, and instead puts it into a variable accessible to this test.
Avi Drissman438827be2019-06-06 18:46:11795 SEL originalMethod = @selector(passURLToHandoffManager:);
796 SEL newMethod = @selector(new_passURLToHandoffManager:);
erikchen600f7962014-12-12 00:17:38797 ExchangeSelectors(originalMethod, newMethod);
798 }
799
800 // Closes the tab, and waits for the close to finish.
801 void CloseTab(Browser* browser, int index) {
802 content::WebContentsDestroyedWatcher destroyed_watcher(
803 browser->tab_strip_model()->GetWebContentsAt(index));
804 browser->tab_strip_model()->CloseWebContentsAt(
805 index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
806 destroyed_watcher.Wait();
807 }
808};
809
810// Tests that as a user switches between tabs, navigates within a tab, and
811// switches between browser windows, the correct URL is being passed to the
812// Handoff.
813IN_PROC_BROWSER_TEST_F(AppControllerHandoffBrowserTest, TestHandoffURLs) {
svaldeza01f7d92015-11-18 17:47:56814 ASSERT_TRUE(embedded_test_server()->Start());
erikchen600f7962014-12-12 00:17:38815 EXPECT_EQ(g_handoff_url, GURL(url::kAboutBlankURL));
816
817 // Test that navigating to a URL updates the handoff URL.
818 GURL test_url1 = embedded_test_server()->GetURL("/title1.html");
819 ui_test_utils::NavigateToURL(browser(), test_url1);
820 EXPECT_EQ(g_handoff_url, test_url1);
821
822 // Test that opening a new tab updates the handoff URL.
823 GURL test_url2 = embedded_test_server()->GetURL("/title2.html");
cm.sanchi2522bc92017-12-04 08:04:13824 NavigateParams params(browser(), test_url2, ui::PAGE_TRANSITION_LINK);
nick3b04f322016-08-31 19:29:19825 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
erikchen600f7962014-12-12 00:17:38826 ui_test_utils::NavigateToURL(&params);
827 EXPECT_EQ(g_handoff_url, test_url2);
828
829 // Test that switching tabs updates the handoff URL.
Edwin Joe6f6fc1e2019-02-27 20:00:37830 browser()->tab_strip_model()->ActivateTabAt(
831 0, {TabStripModel::GestureType::kOther});
erikchen600f7962014-12-12 00:17:38832 EXPECT_EQ(g_handoff_url, test_url1);
833
834 // Test that closing the current tab updates the handoff URL.
835 CloseTab(browser(), 0);
836 EXPECT_EQ(g_handoff_url, test_url2);
837
838 // Test that opening a new browser window updates the handoff URL.
839 GURL test_url3 = embedded_test_server()->GetURL("/title3.html");
840 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19841 browser(), GURL(test_url3), WindowOpenDisposition::NEW_WINDOW,
erikchen600f7962014-12-12 00:17:38842 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
843 EXPECT_EQ(g_handoff_url, test_url3);
844
845 // Check that there are exactly 2 browsers.
scottmg0d8e4ab2016-01-28 00:34:55846 BrowserList* active_browser_list = BrowserList::GetInstance();
erikchen600f7962014-12-12 00:17:38847 EXPECT_EQ(2u, active_browser_list->size());
848
erikchen64a6d442015-07-10 19:38:20849 // Close the second browser window (which only has 1 tab left).
erikchen600f7962014-12-12 00:17:38850 Browser* browser2 = active_browser_list->get(1);
erikchen64a6d442015-07-10 19:38:20851 CloseBrowserSynchronously(browser2);
erikchen600f7962014-12-12 00:17:38852 EXPECT_EQ(g_handoff_url, test_url2);
853
854 // The URLs of incognito windows should not be passed to Handoff.
855 GURL test_url4 = embedded_test_server()->GetURL("/simple.html");
856 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19857 browser(), GURL(test_url4), WindowOpenDisposition::OFF_THE_RECORD,
erikchen600f7962014-12-12 00:17:38858 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
859 EXPECT_EQ(g_handoff_url, GURL());
860
861 // Open a new tab in the incognito window.
862 EXPECT_EQ(2u, active_browser_list->size());
863 Browser* browser3 = active_browser_list->get(1);
864 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19865 browser3, test_url4, WindowOpenDisposition::NEW_FOREGROUND_TAB,
erikchen600f7962014-12-12 00:17:38866 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
867 EXPECT_EQ(g_handoff_url, GURL());
868
869 // Navigate the current tab in the incognito window.
thestig53986dc2014-12-16 06:09:18870 ui_test_utils::NavigateToURL(browser3, test_url1);
erikchen600f7962014-12-12 00:17:38871 EXPECT_EQ(g_handoff_url, GURL());
872
873 // Activate the original browser window.
874 Browser* browser1 = active_browser_list->get(0);
875 browser1->window()->Show();
876 EXPECT_EQ(g_handoff_url, test_url2);
877}
878
879} // namespace