[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 1 | // 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] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 5 | #import <Carbon/Carbon.h> |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 6 | #import <Cocoa/Cocoa.h> |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 7 | #import <Foundation/Foundation.h> |
| 8 | #import <Foundation/NSAppleEventDescriptor.h> |
| 9 | #import <objc/message.h> |
| 10 | #import <objc/runtime.h> |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 11 | #include <stddef.h> |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 12 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 13 | #include "base/bind.h" |
David Roger | 5179764 | 2018-03-20 13:29:16 | [diff] [blame] | 14 | #include "base/bind_helpers.h" |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 15 | #include "base/command_line.h" |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 16 | #include "base/mac/foundation_util.h" |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 17 | #include "base/mac/scoped_nsobject.h" |
thestig | af7f415 | 2014-10-31 23:19:15 | [diff] [blame] | 18 | #include "base/run_loop.h" |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 19 | #include "base/strings/sys_string_conversions.h" |
| 20 | #include "base/strings/utf_string_conversions.h" |
jam | 3f2d393 | 2017-04-26 20:28:51 | [diff] [blame] | 21 | #include "base/threading/thread_restrictions.h" |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 22 | #include "chrome/app/chrome_command_ids.h" |
| 23 | #import "chrome/browser/app_controller_mac.h" |
Dominick Ng | 6ff5105 | 2018-07-06 05:30:20 | [diff] [blame] | 24 | #include "chrome/browser/apps/platform_apps/app_browsertest_util.h" |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 25 | #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
[email protected] | dbb03fb | 2014-02-15 05:36:33 | [diff] [blame] | 26 | #include "chrome/browser/browser_process.h" |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 27 | #include "chrome/browser/history/history_service_factory.h" |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 28 | #include "chrome/browser/lifetime/application_lifetime.h" |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 29 | #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 30 | #include "chrome/browser/profiles/profile_attributes_storage.h" |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 31 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 32 | #include "chrome/browser/ui/browser.h" |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 33 | #include "chrome/browser/ui/browser_finder.h" |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 34 | #include "chrome/browser/ui/browser_list.h" |
thestig | e8082124 | 2015-09-30 23:46:08 | [diff] [blame] | 35 | #include "chrome/browser/ui/browser_navigator_params.h" |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame] | 36 | #include "chrome/browser/ui/browser_window.h" |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 37 | #include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 38 | #include "chrome/browser/ui/cocoa/history_menu_bridge.h" |
Christian Dullweber | 820ee5f | 2018-04-26 13:24:31 | [diff] [blame] | 39 | #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h" |
lgrey | 92aad3c | 2016-12-10 01:22:18 | [diff] [blame] | 40 | #include "chrome/browser/ui/cocoa/test/run_loop_testing.h" |
kristipark | a34b247a | 2018-03-09 19:05:34 | [diff] [blame] | 41 | #include "chrome/browser/ui/search/local_ntp_test_utils.h" |
[email protected] | 617ee96 | 2013-01-29 20:49:12 | [diff] [blame] | 42 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 43 | #include "chrome/browser/ui/user_manager.h" |
Hector Carmona | d6664a0 | 2019-08-01 01:58:48 | [diff] [blame] | 44 | #include "chrome/browser/ui/webui/welcome/helpers.h" |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 45 | #include "chrome/common/chrome_constants.h" |
| 46 | #include "chrome/common/chrome_switches.h" |
| 47 | #include "chrome/common/pref_names.h" |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 48 | #include "chrome/common/url_constants.h" |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 49 | #include "chrome/test/base/in_process_browser_test.h" |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 50 | #include "chrome/test/base/ui_test_utils.h" |
tapted | 574f09c | 2015-05-19 13:08:08 | [diff] [blame] | 51 | #include "components/bookmarks/browser/bookmark_model.h" |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 52 | #include "components/bookmarks/test/bookmark_test_helpers.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 53 | #include "components/prefs/pref_service.h" |
tmartino | 82e5578 | 2017-01-10 22:30:17 | [diff] [blame] | 54 | #include "content/public/browser/navigation_controller.h" |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 55 | #include "content/public/browser/web_contents.h" |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 56 | #include "content/public/test/browser_test_utils.h" |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 57 | #include "content/public/test/test_navigation_observer.h" |
hashimoto | ad3c687 | 2014-08-29 09:46:57 | [diff] [blame] | 58 | #include "extensions/browser/app_window/app_window_registry.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 59 | #include "extensions/common/extension.h" |
lfg | 910f2f9 | 2014-09-19 05:31:09 | [diff] [blame] | 60 | #include "extensions/test/extension_test_message_listener.h" |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 61 | #include "net/test/embedded_test_server/embedded_test_server.h" |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 62 | #import "ui/events/test/cocoa_test_event_utils.h" |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 63 | |
Adam Rice | ff26dcf | 2017-08-14 05:01:52 | [diff] [blame] | 64 | using base::SysUTF16ToNSString; |
| 65 | |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 66 | namespace { |
| 67 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 68 | GURL g_open_shortcut_url = GURL::EmptyGURL(); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 69 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 70 | // Returns an Apple Event that instructs the application to open |url|. |
| 71 | NSAppleEventDescriptor* 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|. |
| 86 | void 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 | |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 99 | void 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] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 106 | } // namespace |
| 107 | |
| 108 | @interface TestOpenShortcutOnStartup : NSObject |
| 109 | - (void)applicationWillFinishLaunching:(NSNotification*)notification; |
| 110 | @end |
| 111 | |
| 112 | @implementation TestOpenShortcutOnStartup |
| 113 | |
| 114 | - (void)applicationWillFinishLaunching:(NSNotification*)notification { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 115 | if (!g_open_shortcut_url.is_valid()) |
| 116 | return; |
| 117 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 118 | SendAppleEventToOpenUrlToAppController(g_open_shortcut_url); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @end |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 122 | |
| 123 | namespace { |
| 124 | |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 125 | using AppControllerBrowserTest = InProcessBrowserTest; |
| 126 | |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 127 | // 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. |
| 129 | bool IsReallyVisible(NSWindow* window) { |
| 130 | while (window) { |
| 131 | if (!window.visible) |
| 132 | return false; |
| 133 | window = [window parentWindow]; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 138 | size_t CountVisibleWindows() { |
| 139 | size_t count = 0; |
| 140 | for (NSWindow* w in [NSApp windows]) |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 141 | count = count + (IsReallyVisible(w) ? 1 : 0); |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 142 | return count; |
| 143 | } |
| 144 | |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 145 | // Returns how many visible NSWindows are expected for a given count of browser |
Sidney San MartÃn | b247d53f | 2018-05-23 02:31:21 | [diff] [blame] | 146 | // windows. |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 147 | size_t ExpectedWindowCountForBrowserCount(size_t browsers) { |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 148 | return browsers; |
| 149 | } |
| 150 | |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 151 | // Test browser shutdown with a command in the message queue. |
| 152 | IN_PROC_BROWSER_TEST_F(AppControllerBrowserTest, CommandDuringShutdown) { |
| 153 | EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 154 | EXPECT_EQ(ExpectedWindowCountForBrowserCount(1), CountVisibleWindows()); |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 155 | |
| 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-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 165 | EXPECT_EQ(ExpectedWindowCountForBrowserCount(0), CountVisibleWindows()); |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 166 | |
| 167 | base::RunLoop().RunUntilIdle(); |
| 168 | |
| 169 | EXPECT_EQ(0u, chrome::GetTotalBrowserCount()); |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 170 | EXPECT_EQ(ExpectedWindowCountForBrowserCount(0), CountVisibleWindows()); |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 171 | |
| 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] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame] | 180 | class AppControllerPlatformAppBrowserTest |
| 181 | : public extensions::PlatformAppBrowserTest { |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 182 | protected: |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 183 | AppControllerPlatformAppBrowserTest() |
scottmg | 0d8e4ab | 2016-01-28 00:34:55 | [diff] [blame] | 184 | : active_browser_list_(BrowserList::GetInstance()) {} |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 185 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 186 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame] | 187 | PlatformAppBrowserTest::SetUpCommandLine(command_line); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 188 | command_line->AppendSwitchASCII(switches::kAppId, |
| 189 | "1234"); |
| 190 | } |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 191 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 192 | const BrowserList* active_browser_list_; |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 193 | }; |
| 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. |
| 197 | IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest, |
| 198 | PlatformAppReopenWithWindows) { |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 199 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 200 | [[NSApplication sharedApplication] delegate]); |
| 201 | ASSERT_TRUE(ac); |
| 202 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 203 | NSUInteger old_window_count = [[NSApp windows] count]; |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 204 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | a25920ee | 2013-09-05 19:38:49 | [diff] [blame] | 205 | [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] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 209 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 210 | EXPECT_EQ(old_window_count, [[NSApp windows] count]); |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 211 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 214 | IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest, |
| 215 | ActivationFocusesBrowserWindow) { |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 216 | AppController* app_controller = base::mac::ObjCCast<AppController>( |
| 217 | [[NSApplication sharedApplication] delegate]); |
| 218 | ASSERT_TRUE(app_controller); |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 219 | |
| 220 | ExtensionTestMessageListener listener("Launched", false); |
| 221 | const extensions::Extension* app = |
| 222 | InstallAndLaunchPlatformApp("minimal"); |
| 223 | ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 224 | |
hashimoto | ad3c687 | 2014-08-29 09:46:57 | [diff] [blame] | 225 | NSWindow* app_window = extensions::AppWindowRegistry::Get(profile()) |
[email protected] | dbb03fb | 2014-02-15 05:36:33 | [diff] [blame] | 226 | ->GetAppWindowsForApp(app->id()) |
| 227 | .front() |
Christopher Cameron | 359c61e2 | 2018-10-18 08:06:20 | [diff] [blame] | 228 | ->GetNativeWindow() |
| 229 | .GetNativeNSWindow(); |
| 230 | NSWindow* browser_window = |
| 231 | browser()->window()->GetNativeWindow().GetNativeNSWindow(); |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 232 | |
andybons | 346c7c6 | 2015-08-13 15:18:03 | [diff] [blame] | 233 | chrome::testing::NSRunLoopRunAllPending(); |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 234 | EXPECT_LE([[NSApp orderedWindows] indexOfObject:app_window], |
| 235 | [[NSApp orderedWindows] indexOfObject:browser_window]); |
| 236 | [app_controller applicationShouldHandleReopen:NSApp |
| 237 | hasVisibleWindows:YES]; |
andybons | 346c7c6 | 2015-08-13 15:18:03 | [diff] [blame] | 238 | chrome::testing::NSRunLoopRunAllPending(); |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 239 | EXPECT_LE([[NSApp orderedWindows] indexOfObject:browser_window], |
| 240 | [[NSApp orderedWindows] indexOfObject:app_window]); |
| 241 | } |
| 242 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 243 | class AppControllerWebAppBrowserTest : public InProcessBrowserTest { |
| 244 | protected: |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 245 | AppControllerWebAppBrowserTest() |
scottmg | 0d8e4ab | 2016-01-28 00:34:55 | [diff] [blame] | 246 | : active_browser_list_(BrowserList::GetInstance()) {} |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 247 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 248 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 90ca4427 | 2012-07-18 18:15:48 | [diff] [blame] | 249 | command_line->AppendSwitchASCII(switches::kApp, GetAppURL()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | std::string GetAppURL() const { |
| 253 | return "http://example.com/"; |
| 254 | } |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 255 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 256 | const BrowserList* active_browser_list_; |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | // Test that in web app mode a reopen event opens the app URL. |
| 260 | IN_PROC_BROWSER_TEST_F(AppControllerWebAppBrowserTest, |
| 261 | WebAppReopenWithNoWindows) { |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 262 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 263 | [[NSApplication sharedApplication] delegate]); |
| 264 | ASSERT_TRUE(ac); |
| 265 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 266 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 267 | BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; |
| 268 | |
| 269 | EXPECT_FALSE(result); |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 270 | EXPECT_EQ(2u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 271 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 272 | Browser* browser = active_browser_list_->get(0); |
[email protected] | 617ee96 | 2013-01-29 20:49:12 | [diff] [blame] | 273 | GURL current_url = |
| 274 | browser->tab_strip_model()->GetActiveWebContents()->GetURL(); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 275 | EXPECT_EQ(GetAppURL(), current_url.spec()); |
| 276 | } |
| 277 | |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 278 | // Called when the ProfileManager has created a profile. |
| 279 | void 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 | |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 291 | void CreateAndWaitForSystemProfile() { |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 292 | ProfileManager::CreateCallback create_callback = |
| 293 | base::Bind(&CreateProfileCallback, |
Gabriel Charette | 87f2bef7 | 2018-05-17 20:37:53 | [diff] [blame] | 294 | base::RunLoop::QuitCurrentWhenIdleClosureDeprecated()); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 295 | g_browser_process->profile_manager()->CreateProfileAsync( |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 296 | ProfileManager::GetSystemProfilePath(), |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 297 | create_callback, |
| 298 | base::string16(), |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 299 | std::string()); |
| 300 | base::RunLoop().Run(); |
| 301 | } |
| 302 | |
| 303 | class AppControllerNewProfileManagementBrowserTest |
| 304 | : public InProcessBrowserTest { |
| 305 | protected: |
| 306 | AppControllerNewProfileManagementBrowserTest() |
scottmg | 0d8e4ab | 2016-01-28 00:34:55 | [diff] [blame] | 307 | : active_browser_list_(BrowserList::GetInstance()) {} |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 308 | |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 309 | const BrowserList* active_browser_list_; |
| 310 | }; |
| 311 | |
| 312 | // Test that for a regular last profile, a reopen event opens a browser. |
| 313 | IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, |
| 314 | RegularProfileReopenWithNoWindows) { |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 315 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 316 | [[NSApplication sharedApplication] delegate]); |
| 317 | ASSERT_TRUE(ac); |
| 318 | |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 319 | 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()); |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 324 | EXPECT_FALSE(UserManager::IsShowing()); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // Test that for a locked last profile, a reopen event opens the User Manager. |
| 328 | IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, |
| 329 | LockedProfileReopenWithNoWindows) { |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 330 | // The User Manager uses the system profile as its underlying profile. To |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 331 | // minimize flakiness due to the scheduling/descheduling of tasks on the |
| 332 | // different threads, pre-initialize the guest profile before it is needed. |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 333 | CreateAndWaitForSystemProfile(); |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 334 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 335 | [[NSApplication sharedApplication] delegate]); |
| 336 | ASSERT_TRUE(ac); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 337 | |
| 338 | // Lock the active profile. |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 339 | base::ScopedAllowBlockingForTesting allow_blocking; |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 340 | Profile* profile = [ac lastProfile]; |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 341 | 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] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 347 | |
| 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()); |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 354 | EXPECT_TRUE(UserManager::IsShowing()); |
| 355 | UserManager::Hide(); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 356 | } |
| 357 | |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 358 | // 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. |
| 362 | IN_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 | |
guidou | bbb4787a | 2016-12-15 12:34:43 | [diff] [blame] | 398 | #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] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 404 | // Test that for a guest last profile, a reopen event opens the User Manager. |
| 405 | IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, |
guidou | bbb4787a | 2016-12-15 12:34:43 | [diff] [blame] | 406 | MAYBE_GuestProfileReopenWithNoWindows) { |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 407 | // Create the system profile. Set the guest as the last used profile so the |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 408 | // app controller can use it on init. |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 409 | CreateAndWaitForSystemProfile(); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 410 | PrefService* local_state = g_browser_process->local_state(); |
| 411 | local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir); |
| 412 | |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 413 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 414 | [[NSApplication sharedApplication] delegate]); |
| 415 | ASSERT_TRUE(ac); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 416 | |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 417 | base::ScopedAllowBlockingForTesting allow_blocking; |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 418 | 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()); |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 429 | EXPECT_TRUE(UserManager::IsShowing()); |
| 430 | UserManager::Hide(); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 431 | } |
| 432 | |
guidou | bbb4787a | 2016-12-15 12:34:43 | [diff] [blame] | 433 | #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 |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 439 | IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, |
guidou | bbb4787a | 2016-12-15 12:34:43 | [diff] [blame] | 440 | MAYBE_AboutChromeForcesUserManager) { |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 441 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 442 | [[NSApplication sharedApplication] delegate]); |
| 443 | ASSERT_TRUE(ac); |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 444 | |
| 445 | // Create the guest profile, and set it as the last used profile so the |
| 446 | // app controller can use it on init. |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 447 | CreateAndWaitForSystemProfile(); |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 448 | 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 Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 454 | base::ScopedAllowBlockingForTesting allow_blocking; |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 455 | 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] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 472 | class AppControllerOpenShortcutBrowserTest : public InProcessBrowserTest { |
| 473 | protected: |
| 474 | AppControllerOpenShortcutBrowserTest() { |
Hector Carmona | 9797cc6 | 2019-08-09 20:35:35 | [diff] [blame^] | 475 | scoped_feature_list_.InitWithFeatures({welcome::kForceEnabled}, {}); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 476 | } |
| 477 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 478 | void SetUpInProcessBrowserTestFixture() override { |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 479 | // 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 Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 505 | |
| 506 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 507 | g_open_shortcut_url = embedded_test_server()->GetURL("/simple.html"); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 508 | } |
| 509 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 510 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 511 | // 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 Carmona | 1e62e35 | 2019-06-24 19:18:17 | [diff] [blame] | 515 | |
| 516 | private: |
| 517 | base::test::ScopedFeatureList scoped_feature_list_; |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, |
| 521 | OpenShortcutOnStartup) { |
tmartino | 82e5578 | 2017-01-10 22:30:17 | [diff] [blame] | 522 | // The two tabs expected are the Welcome page and the desired URL. |
| 523 | EXPECT_EQ(2, browser()->tab_strip_model()->count()); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 524 | EXPECT_EQ(g_open_shortcut_url, |
| 525 | browser()->tab_strip_model()->GetActiveWebContents() |
| 526 | ->GetLastCommittedURL()); |
| 527 | } |
| 528 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 529 | class AppControllerReplaceNTPBrowserTest : public InProcessBrowserTest { |
| 530 | protected: |
| 531 | AppControllerReplaceNTPBrowserTest() {} |
| 532 | |
| 533 | void SetUpInProcessBrowserTestFixture() override { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 534 | ASSERT_TRUE(embedded_test_server()->Start()); |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 535 | } |
| 536 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 537 | void SetUpCommandLine(base::CommandLine* command_line) override { |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 538 | // 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. |
| 545 | IN_PROC_BROWSER_TEST_F(AppControllerReplaceNTPBrowserTest, |
| 546 | ReplaceNTPAfterStartup) { |
kristipark | a34b247a | 2018-03-09 19:05:34 | [diff] [blame] | 547 | // 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 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 553 | // Ensure that there is exactly 1 tab showing, and the tab is the NTP. |
kristipark | a34b247a | 2018-03-09 19:05:34 | [diff] [blame] | 554 | GURL ntp(expected_url); |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 555 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
tmartino | 82e5578 | 2017-01-10 22:30:17 | [diff] [blame] | 556 | browser()->tab_strip_model()->GetActiveWebContents()->GetController().LoadURL( |
kristipark | a34b247a | 2018-03-09 19:05:34 | [diff] [blame] | 557 | GURL(expected_url), content::Referrer(), |
tmartino | 82e5578 | 2017-01-10 22:30:17 | [diff] [blame] | 558 | 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 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 565 | 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 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 574 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
tmartino | 82e5578 | 2017-01-10 22:30:17 | [diff] [blame] | 575 | content::TestNavigationObserver event_navigation_observer( |
| 576 | browser()->tab_strip_model()->GetActiveWebContents()); |
| 577 | event_navigation_observer.Wait(); |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 578 | |
| 579 | EXPECT_EQ(simple, |
| 580 | browser() |
| 581 | ->tab_strip_model() |
| 582 | ->GetActiveWebContents() |
| 583 | ->GetLastCommittedURL()); |
| 584 | } |
| 585 | |
Christian Dullweber | 820ee5f | 2018-04-26 13:24:31 | [diff] [blame] | 586 | // Tests that when a GURL is opened, it is not opened in incognito mode. |
| 587 | IN_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 | |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 609 | class AppControllerMainMenuBrowserTest : public InProcessBrowserTest { |
| 610 | protected: |
| 611 | AppControllerMainMenuBrowserTest() { |
| 612 | } |
| 613 | }; |
| 614 | |
| 615 | IN_PROC_BROWSER_TEST_F(AppControllerMainMenuBrowserTest, |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 616 | HistoryMenuResetAfterProfileDeletion) { |
| 617 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
tapted | 676995d | 2016-04-18 11:32:29 | [diff] [blame] | 618 | AppController* ac = |
| 619 | base::mac::ObjCCastStrict<AppController>([NSApp delegate]); |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 620 | |
| 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()), |
lwchkg | 1f62d24 | 2015-10-29 00:50:43 | [diff] [blame] | 632 | base::string16(), |
lwchkg | 1f62d24 | 2015-10-29 00:50:43 | [diff] [blame] | 633 | std::string()); |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 634 | run_loop.Run(); |
| 635 | Profile* profile2 = profile_manager->GetProfileByPath(profile2_path); |
| 636 | ASSERT_TRUE(profile2); |
| 637 | |
Sidney San MartÃn | aa3b48f | 2018-06-03 08:10:05 | [diff] [blame] | 638 | // 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)); |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 643 | // 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 Roger | 5179764 | 2018-03-20 13:29:16 | [diff] [blame] | 675 | profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), |
| 676 | base::DoNothing()); |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 677 | 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 | |
| 685 | IN_PROC_BROWSER_TEST_F(AppControllerMainMenuBrowserTest, |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 686 | BookmarksMenuIsRestoredAfterProfileSwitch) { |
| 687 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 688 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 689 | [[NSApplication sharedApplication] delegate]); |
| 690 | ASSERT_TRUE(ac); |
| 691 | |
Robert Sesek | 124f80f9 | 2018-07-24 20:31:29 | [diff] [blame] | 692 | [ac mainMenuCreated]; |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 693 | |
| 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( |
pke | 3e0d5c9 | 2016-08-08 09:07:30 | [diff] [blame] | 704 | BookmarkModelFactory::GetForBrowserContext(profile1)); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 705 | |
| 706 | // Create profile 2. |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 707 | base::ScopedAllowBlockingForTesting allow_blocking; |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 708 | base::FilePath path2 = profile_manager->GenerateNextProfileDirectoryPath(); |
Daniel Hosseinian | 8f0b748 | 2019-06-07 18:21:04 | [diff] [blame] | 709 | 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); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 713 | bookmarks::test::WaitForBookmarkModelToLoad( |
Daniel Hosseinian | 8f0b748 | 2019-06-07 18:21:04 | [diff] [blame] | 714 | BookmarkModelFactory::GetForBrowserContext(profile2_ptr)); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 715 | |
| 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 Apted | cf95689a | 2017-11-22 00:01:10 | [diff] [blame] | 721 | NSMenu* profile1_submenu = [ac bookmarkMenuBridge]->BookmarkMenu(); |
| 722 | [[profile1_submenu delegate] menuNeedsUpdate:profile1_submenu]; |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 723 | |
| 724 | // Switch to profile 2, create bookmark 2 and force the menu to build. |
Daniel Hosseinian | 8f0b748 | 2019-06-07 18:21:04 | [diff] [blame] | 725 | [ac windowChangedToProfile:profile2_ptr]; |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 726 | [ac bookmarkMenuBridge]->GetBookmarkModel()->AddURL( |
| 727 | [ac bookmarkMenuBridge]->GetBookmarkModel()->bookmark_bar_node(), |
| 728 | 0, title2, url2); |
Trent Apted | cf95689a | 2017-11-22 00:01:10 | [diff] [blame] | 729 | NSMenu* profile2_submenu = [ac bookmarkMenuBridge]->BookmarkMenu(); |
| 730 | [[profile2_submenu delegate] menuNeedsUpdate:profile2_submenu]; |
| 731 | EXPECT_NE(profile1_submenu, profile2_submenu); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 732 | |
| 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 Apted | cf95689a | 2017-11-22 00:01:10 | [diff] [blame] | 747 | |
| 748 | // Ensure a cached menu was used. |
| 749 | EXPECT_EQ(profile1_submenu, [ac bookmarkMenuBridge]->BookmarkMenu()); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 750 | } |
| 751 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 752 | } // namespace |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 753 | |
| 754 | //--------------------------AppControllerHandoffBrowserTest--------------------- |
| 755 | |
| 756 | static GURL g_handoff_url; |
| 757 | |
| 758 | @interface AppController (BrowserTest) |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 759 | - (void)new_passURLToHandoffManager:(const GURL&)handoffURL; |
| 760 | @end |
| 761 | |
| 762 | @implementation AppController (BrowserTest) |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 763 | - (void)new_passURLToHandoffManager:(const GURL&)handoffURL { |
| 764 | g_handoff_url = handoffURL; |
| 765 | } |
| 766 | @end |
| 767 | |
| 768 | namespace { |
| 769 | |
| 770 | class 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 { |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 793 | // 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 Drissman | 438827be | 2019-06-06 18:46:11 | [diff] [blame] | 795 | SEL originalMethod = @selector(passURLToHandoffManager:); |
| 796 | SEL newMethod = @selector(new_passURLToHandoffManager:); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 797 | 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. |
| 813 | IN_PROC_BROWSER_TEST_F(AppControllerHandoffBrowserTest, TestHandoffURLs) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 814 | ASSERT_TRUE(embedded_test_server()->Start()); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 815 | 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.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 824 | NavigateParams params(browser(), test_url2, ui::PAGE_TRANSITION_LINK); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 825 | params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 826 | ui_test_utils::NavigateToURL(¶ms); |
| 827 | EXPECT_EQ(g_handoff_url, test_url2); |
| 828 | |
| 829 | // Test that switching tabs updates the handoff URL. |
Edwin Joe | 6f6fc1e | 2019-02-27 20:00:37 | [diff] [blame] | 830 | browser()->tab_strip_model()->ActivateTabAt( |
| 831 | 0, {TabStripModel::GestureType::kOther}); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 832 | 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( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 841 | browser(), GURL(test_url3), WindowOpenDisposition::NEW_WINDOW, |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 842 | 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. |
scottmg | 0d8e4ab | 2016-01-28 00:34:55 | [diff] [blame] | 846 | BrowserList* active_browser_list = BrowserList::GetInstance(); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 847 | EXPECT_EQ(2u, active_browser_list->size()); |
| 848 | |
erikchen | 64a6d44 | 2015-07-10 19:38:20 | [diff] [blame] | 849 | // Close the second browser window (which only has 1 tab left). |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 850 | Browser* browser2 = active_browser_list->get(1); |
erikchen | 64a6d44 | 2015-07-10 19:38:20 | [diff] [blame] | 851 | CloseBrowserSynchronously(browser2); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 852 | 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( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 857 | browser(), GURL(test_url4), WindowOpenDisposition::OFF_THE_RECORD, |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 858 | 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( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 865 | browser3, test_url4, WindowOpenDisposition::NEW_FOREGROUND_TAB, |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 866 | 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. |
thestig | 53986dc | 2014-12-16 06:09:18 | [diff] [blame] | 870 | ui_test_utils::NavigateToURL(browser3, test_url1); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 871 | 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 |