[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 | |
Elly Fong-Jones | 3ef60b75 | 2019-11-12 19:02:03 | [diff] [blame] | 66 | @interface AppController (ForTesting) |
| 67 | - (void)getUrl:(NSAppleEventDescriptor*)event |
| 68 | withReply:(NSAppleEventDescriptor*)reply; |
| 69 | @end |
| 70 | |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 71 | namespace { |
| 72 | |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 73 | GURL g_open_shortcut_url = GURL::EmptyGURL(); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 74 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 75 | // Returns an Apple Event that instructs the application to open |url|. |
| 76 | NSAppleEventDescriptor* AppleEventToOpenUrl(const GURL& url) { |
| 77 | NSAppleEventDescriptor* shortcut_event = [[[NSAppleEventDescriptor alloc] |
| 78 | initWithEventClass:kASAppleScriptSuite |
| 79 | eventID:kASSubroutineEvent |
| 80 | targetDescriptor:nil |
| 81 | returnID:kAutoGenerateReturnID |
| 82 | transactionID:kAnyTransactionID] autorelease]; |
| 83 | NSString* url_string = [NSString stringWithUTF8String:url.spec().c_str()]; |
| 84 | [shortcut_event setParamDescriptor:[NSAppleEventDescriptor |
| 85 | descriptorWithString:url_string] |
| 86 | forKeyword:keyDirectObject]; |
| 87 | return shortcut_event; |
| 88 | } |
| 89 | |
| 90 | // Instructs the NSApp's delegate to open |url|. |
| 91 | void SendAppleEventToOpenUrlToAppController(const GURL& url) { |
| 92 | AppController* controller = |
| 93 | base::mac::ObjCCast<AppController>([NSApp delegate]); |
Elly Fong-Jones | 3ef60b75 | 2019-11-12 19:02:03 | [diff] [blame] | 94 | [controller getUrl:AppleEventToOpenUrl(url) withReply:nullptr]; |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 95 | } |
| 96 | |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 97 | void RunClosureWhenProfileInitialized(const base::Closure& closure, |
| 98 | Profile* profile, |
| 99 | Profile::CreateStatus status) { |
| 100 | if (status == Profile::CREATE_STATUS_INITIALIZED) |
| 101 | closure.Run(); |
| 102 | } |
| 103 | |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 104 | } // namespace |
| 105 | |
| 106 | @interface TestOpenShortcutOnStartup : NSObject |
| 107 | - (void)applicationWillFinishLaunching:(NSNotification*)notification; |
| 108 | @end |
| 109 | |
| 110 | @implementation TestOpenShortcutOnStartup |
| 111 | |
| 112 | - (void)applicationWillFinishLaunching:(NSNotification*)notification { |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 113 | if (!g_open_shortcut_url.is_valid()) |
| 114 | return; |
| 115 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 116 | SendAppleEventToOpenUrlToAppController(g_open_shortcut_url); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @end |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 120 | |
| 121 | namespace { |
| 122 | |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 123 | using AppControllerBrowserTest = InProcessBrowserTest; |
| 124 | |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 125 | // Returns whether a window's pixels are actually on the screen, which is the |
| 126 | // case when it and all of its parents are marked visible. |
| 127 | bool IsReallyVisible(NSWindow* window) { |
| 128 | while (window) { |
| 129 | if (!window.visible) |
| 130 | return false; |
| 131 | window = [window parentWindow]; |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 136 | size_t CountVisibleWindows() { |
| 137 | size_t count = 0; |
| 138 | for (NSWindow* w in [NSApp windows]) |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 139 | count = count + (IsReallyVisible(w) ? 1 : 0); |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 140 | return count; |
| 141 | } |
| 142 | |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 143 | // 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] | 144 | // windows. |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 145 | size_t ExpectedWindowCountForBrowserCount(size_t browsers) { |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 146 | return browsers; |
| 147 | } |
| 148 | |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 149 | // Test browser shutdown with a command in the message queue. |
| 150 | IN_PROC_BROWSER_TEST_F(AppControllerBrowserTest, CommandDuringShutdown) { |
| 151 | EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 152 | EXPECT_EQ(ExpectedWindowCountForBrowserCount(1), CountVisibleWindows()); |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 153 | |
| 154 | chrome::AttemptExit(); // Set chrome::IsTryingToQuit and close all windows. |
| 155 | |
| 156 | // Opening a new window here is fine (unload handlers can also interrupt |
| 157 | // exit). But closing the window posts an autorelease on |
| 158 | // BrowserWindowController, which calls ~Browser() and, if that was the last |
| 159 | // Browser, it invokes applicationWillTerminate: (because IsTryingToQuit is |
| 160 | // set). So, verify assumptions then process that autorelease. |
| 161 | |
| 162 | EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 163 | EXPECT_EQ(ExpectedWindowCountForBrowserCount(0), CountVisibleWindows()); |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 164 | |
| 165 | base::RunLoop().RunUntilIdle(); |
| 166 | |
| 167 | EXPECT_EQ(0u, chrome::GetTotalBrowserCount()); |
Elly Fong-Jones | 8504603 | 2018-04-19 19:15:28 | [diff] [blame] | 168 | EXPECT_EQ(ExpectedWindowCountForBrowserCount(0), CountVisibleWindows()); |
Trent Apted | 45d9ae2 | 2017-11-09 23:12:32 | [diff] [blame] | 169 | |
| 170 | NSEvent* cmd_n = cocoa_test_event_utils::KeyEventWithKeyCode( |
| 171 | 'n', 'n', NSKeyDown, NSCommandKeyMask); |
| 172 | [[NSApp mainMenu] performSelector:@selector(performKeyEquivalent:) |
| 173 | withObject:cmd_n |
| 174 | afterDelay:0]; |
| 175 | // Let the run loop get flushed, during process cleanup and try not to crash. |
| 176 | } |
| 177 | |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame] | 178 | class AppControllerPlatformAppBrowserTest |
| 179 | : public extensions::PlatformAppBrowserTest { |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 180 | protected: |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 181 | AppControllerPlatformAppBrowserTest() |
scottmg | 0d8e4ab | 2016-01-28 00:34:55 | [diff] [blame] | 182 | : active_browser_list_(BrowserList::GetInstance()) {} |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 183 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 184 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | ee9ccfd4 | 2013-07-23 02:31:47 | [diff] [blame] | 185 | PlatformAppBrowserTest::SetUpCommandLine(command_line); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 186 | command_line->AppendSwitchASCII(switches::kAppId, |
| 187 | "1234"); |
| 188 | } |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 189 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 190 | const BrowserList* active_browser_list_; |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | // Test that if only a platform app window is open and no browser windows are |
| 194 | // open then a reopen event does nothing. |
| 195 | IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest, |
| 196 | PlatformAppReopenWithWindows) { |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 197 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 198 | [[NSApplication sharedApplication] delegate]); |
| 199 | ASSERT_TRUE(ac); |
| 200 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 201 | NSUInteger old_window_count = [[NSApp windows] count]; |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 202 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | a25920ee | 2013-09-05 19:38:49 | [diff] [blame] | 203 | [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:YES]; |
| 204 | // We do not EXPECT_TRUE the result here because the method |
| 205 | // deminiaturizes windows manually rather than return YES and have |
| 206 | // AppKit do it. |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 207 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 208 | EXPECT_EQ(old_window_count, [[NSApp windows] count]); |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 209 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 210 | } |
| 211 | |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 212 | IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest, |
| 213 | ActivationFocusesBrowserWindow) { |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 214 | AppController* app_controller = base::mac::ObjCCast<AppController>( |
| 215 | [[NSApplication sharedApplication] delegate]); |
| 216 | ASSERT_TRUE(app_controller); |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 217 | |
| 218 | ExtensionTestMessageListener listener("Launched", false); |
| 219 | const extensions::Extension* app = |
| 220 | InstallAndLaunchPlatformApp("minimal"); |
| 221 | ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 222 | |
hashimoto | ad3c687 | 2014-08-29 09:46:57 | [diff] [blame] | 223 | NSWindow* app_window = extensions::AppWindowRegistry::Get(profile()) |
[email protected] | dbb03fb | 2014-02-15 05:36:33 | [diff] [blame] | 224 | ->GetAppWindowsForApp(app->id()) |
| 225 | .front() |
Christopher Cameron | 359c61e2 | 2018-10-18 08:06:20 | [diff] [blame] | 226 | ->GetNativeWindow() |
| 227 | .GetNativeNSWindow(); |
| 228 | NSWindow* browser_window = |
| 229 | browser()->window()->GetNativeWindow().GetNativeNSWindow(); |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 230 | |
andybons | 346c7c6 | 2015-08-13 15:18:03 | [diff] [blame] | 231 | chrome::testing::NSRunLoopRunAllPending(); |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 232 | EXPECT_LE([[NSApp orderedWindows] indexOfObject:app_window], |
| 233 | [[NSApp orderedWindows] indexOfObject:browser_window]); |
| 234 | [app_controller applicationShouldHandleReopen:NSApp |
| 235 | hasVisibleWindows:YES]; |
andybons | 346c7c6 | 2015-08-13 15:18:03 | [diff] [blame] | 236 | chrome::testing::NSRunLoopRunAllPending(); |
[email protected] | 2e29e223 | 2013-07-26 10:40:59 | [diff] [blame] | 237 | EXPECT_LE([[NSApp orderedWindows] indexOfObject:browser_window], |
| 238 | [[NSApp orderedWindows] indexOfObject:app_window]); |
| 239 | } |
| 240 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 241 | class AppControllerWebAppBrowserTest : public InProcessBrowserTest { |
| 242 | protected: |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 243 | AppControllerWebAppBrowserTest() |
scottmg | 0d8e4ab | 2016-01-28 00:34:55 | [diff] [blame] | 244 | : active_browser_list_(BrowserList::GetInstance()) {} |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 245 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 246 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 90ca4427 | 2012-07-18 18:15:48 | [diff] [blame] | 247 | command_line->AppendSwitchASCII(switches::kApp, GetAppURL()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | std::string GetAppURL() const { |
| 251 | return "http://example.com/"; |
| 252 | } |
[email protected] | b4207c4 | 2013-02-12 06:44:20 | [diff] [blame] | 253 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 254 | const BrowserList* active_browser_list_; |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | // Test that in web app mode a reopen event opens the app URL. |
| 258 | IN_PROC_BROWSER_TEST_F(AppControllerWebAppBrowserTest, |
| 259 | WebAppReopenWithNoWindows) { |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 260 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 261 | [[NSApplication sharedApplication] delegate]); |
| 262 | ASSERT_TRUE(ac); |
| 263 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 264 | EXPECT_EQ(1u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 265 | BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; |
| 266 | |
| 267 | EXPECT_FALSE(result); |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 268 | EXPECT_EQ(2u, active_browser_list_->size()); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 269 | |
[email protected] | 7d1a810b | 2013-06-26 19:51:59 | [diff] [blame] | 270 | Browser* browser = active_browser_list_->get(0); |
[email protected] | 617ee96 | 2013-01-29 20:49:12 | [diff] [blame] | 271 | GURL current_url = |
| 272 | browser->tab_strip_model()->GetActiveWebContents()->GetURL(); |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 273 | EXPECT_EQ(GetAppURL(), current_url.spec()); |
| 274 | } |
| 275 | |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 276 | // Called when the ProfileManager has created a profile. |
| 277 | void CreateProfileCallback(const base::Closure& quit_closure, |
| 278 | Profile* profile, |
| 279 | Profile::CreateStatus status) { |
| 280 | EXPECT_TRUE(profile); |
| 281 | EXPECT_NE(Profile::CREATE_STATUS_LOCAL_FAIL, status); |
| 282 | EXPECT_NE(Profile::CREATE_STATUS_REMOTE_FAIL, status); |
| 283 | // This will be called multiple times. Wait until the profile is initialized |
| 284 | // fully to quit the loop. |
| 285 | if (status == Profile::CREATE_STATUS_INITIALIZED) |
| 286 | quit_closure.Run(); |
| 287 | } |
| 288 | |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 289 | void CreateAndWaitForSystemProfile() { |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 290 | ProfileManager::CreateCallback create_callback = |
| 291 | base::Bind(&CreateProfileCallback, |
Gabriel Charette | 87f2bef7 | 2018-05-17 20:37:53 | [diff] [blame] | 292 | base::RunLoop::QuitCurrentWhenIdleClosureDeprecated()); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 293 | g_browser_process->profile_manager()->CreateProfileAsync( |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 294 | ProfileManager::GetSystemProfilePath(), |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 295 | create_callback, |
| 296 | base::string16(), |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 297 | std::string()); |
| 298 | base::RunLoop().Run(); |
| 299 | } |
| 300 | |
| 301 | class AppControllerNewProfileManagementBrowserTest |
| 302 | : public InProcessBrowserTest { |
| 303 | protected: |
| 304 | AppControllerNewProfileManagementBrowserTest() |
scottmg | 0d8e4ab | 2016-01-28 00:34:55 | [diff] [blame] | 305 | : active_browser_list_(BrowserList::GetInstance()) {} |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 306 | |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 307 | const BrowserList* active_browser_list_; |
| 308 | }; |
| 309 | |
| 310 | // Test that for a regular last profile, a reopen event opens a browser. |
| 311 | IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, |
| 312 | RegularProfileReopenWithNoWindows) { |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 313 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 314 | [[NSApplication sharedApplication] delegate]); |
| 315 | ASSERT_TRUE(ac); |
| 316 | |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 317 | EXPECT_EQ(1u, active_browser_list_->size()); |
| 318 | BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; |
| 319 | |
| 320 | EXPECT_FALSE(result); |
| 321 | EXPECT_EQ(2u, active_browser_list_->size()); |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 322 | EXPECT_FALSE(UserManager::IsShowing()); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // Test that for a locked last profile, a reopen event opens the User Manager. |
| 326 | IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, |
| 327 | LockedProfileReopenWithNoWindows) { |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 328 | // The User Manager uses the system profile as its underlying profile. To |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 329 | // minimize flakiness due to the scheduling/descheduling of tasks on the |
| 330 | // different threads, pre-initialize the guest profile before it is needed. |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 331 | CreateAndWaitForSystemProfile(); |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 332 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 333 | [[NSApplication sharedApplication] delegate]); |
| 334 | ASSERT_TRUE(ac); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 335 | |
| 336 | // Lock the active profile. |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 337 | base::ScopedAllowBlockingForTesting allow_blocking; |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 338 | Profile* profile = [ac lastProfile]; |
lwchkg | 9c18394 | 2016-03-13 06:29:54 | [diff] [blame] | 339 | ProfileAttributesEntry* entry; |
| 340 | ASSERT_TRUE(g_browser_process->profile_manager()-> |
| 341 | GetProfileAttributesStorage(). |
| 342 | GetProfileAttributesWithPath(profile->GetPath(), &entry)); |
| 343 | entry->SetIsSigninRequired(true); |
| 344 | EXPECT_TRUE(entry->IsSigninRequired()); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 345 | |
| 346 | EXPECT_EQ(1u, active_browser_list_->size()); |
| 347 | BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; |
| 348 | EXPECT_FALSE(result); |
| 349 | |
| 350 | base::RunLoop().RunUntilIdle(); |
| 351 | EXPECT_EQ(1u, active_browser_list_->size()); |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 352 | EXPECT_TRUE(UserManager::IsShowing()); |
| 353 | UserManager::Hide(); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 354 | } |
| 355 | |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 356 | // Test that for a guest last profile, commandDispatch should open UserManager |
| 357 | // if guest mode is disabled. Note that this test might be flaky under ASAN |
| 358 | // due to https://crbug.com/674475. Please disable this test under ASAN |
| 359 | // as the tests below if that happened. |
| 360 | IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, |
| 361 | OpenGuestProfileOnlyIfGuestModeIsEnabled) { |
| 362 | CreateAndWaitForSystemProfile(); |
| 363 | PrefService* local_state = g_browser_process->local_state(); |
| 364 | local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir); |
| 365 | local_state->SetBoolean(prefs::kBrowserGuestModeEnabled, false); |
| 366 | |
| 367 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 368 | [[NSApplication sharedApplication] delegate]); |
| 369 | ASSERT_TRUE(ac); |
| 370 | |
| 371 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 372 | Profile* profile = [ac lastProfile]; |
| 373 | EXPECT_TRUE(profile->IsGuestSession()); |
| 374 | |
| 375 | NSMenu* menu = [ac applicationDockMenu:NSApp]; |
| 376 | ASSERT_TRUE(menu); |
| 377 | NSMenuItem* item = [menu itemWithTag:IDC_NEW_WINDOW]; |
| 378 | ASSERT_TRUE(item); |
| 379 | EXPECT_EQ(1u, active_browser_list_->size()); |
| 380 | |
| 381 | [ac commandDispatch:item]; |
| 382 | |
| 383 | base::RunLoop().RunUntilIdle(); |
| 384 | |
| 385 | EXPECT_EQ(1u, active_browser_list_->size()); |
| 386 | EXPECT_TRUE(UserManager::IsShowing()); |
| 387 | UserManager::Hide(); |
| 388 | |
| 389 | local_state->SetBoolean(prefs::kBrowserGuestModeEnabled, true); |
| 390 | [ac commandDispatch:item]; |
| 391 | base::RunLoop().RunUntilIdle(); |
| 392 | EXPECT_EQ(2u, active_browser_list_->size()); |
| 393 | EXPECT_FALSE(UserManager::IsShowing()); |
| 394 | } |
| 395 | |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 396 | // Test that for a guest last profile, a reopen event opens the User Manager. |
| 397 | IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, |
Elly Fong-Jones | 76aa43c | 2019-08-20 18:19:31 | [diff] [blame] | 398 | GuestProfileReopenWithNoWindows) { |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 399 | // 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] | 400 | // app controller can use it on init. |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 401 | CreateAndWaitForSystemProfile(); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 402 | PrefService* local_state = g_browser_process->local_state(); |
| 403 | local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir); |
| 404 | |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 405 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 406 | [[NSApplication sharedApplication] delegate]); |
| 407 | ASSERT_TRUE(ac); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 408 | |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 409 | base::ScopedAllowBlockingForTesting allow_blocking; |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 410 | Profile* profile = [ac lastProfile]; |
| 411 | EXPECT_EQ(ProfileManager::GetGuestProfilePath(), profile->GetPath()); |
| 412 | EXPECT_TRUE(profile->IsGuestSession()); |
| 413 | |
| 414 | EXPECT_EQ(1u, active_browser_list_->size()); |
| 415 | BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; |
| 416 | EXPECT_FALSE(result); |
| 417 | |
| 418 | base::RunLoop().RunUntilIdle(); |
| 419 | |
| 420 | EXPECT_EQ(1u, active_browser_list_->size()); |
mlerman | e29d003 | 2014-09-24 19:31:26 | [diff] [blame] | 421 | EXPECT_TRUE(UserManager::IsShowing()); |
| 422 | UserManager::Hide(); |
[email protected] | 7108d91 | 2014-01-30 08:10:45 | [diff] [blame] | 423 | } |
| 424 | |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 425 | IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, |
Elly Fong-Jones | eb85380 | 2019-08-21 17:09:56 | [diff] [blame] | 426 | AboutChromeForcesUserManager) { |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 427 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 428 | [[NSApplication sharedApplication] delegate]); |
| 429 | ASSERT_TRUE(ac); |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 430 | |
| 431 | // Create the guest profile, and set it as the last used profile so the |
| 432 | // app controller can use it on init. |
mlerman | b8df6e8 | 2015-01-23 19:55:34 | [diff] [blame] | 433 | CreateAndWaitForSystemProfile(); |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 434 | PrefService* local_state = g_browser_process->local_state(); |
| 435 | local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir); |
| 436 | |
| 437 | // Prohibiting guest mode forces the user manager flow for About Chrome. |
| 438 | local_state->SetBoolean(prefs::kBrowserGuestModeEnabled, false); |
| 439 | |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 440 | base::ScopedAllowBlockingForTesting allow_blocking; |
mlerman | e6e040a | 2014-10-31 00:53:21 | [diff] [blame] | 441 | Profile* guest_profile = [ac lastProfile]; |
| 442 | EXPECT_EQ(ProfileManager::GetGuestProfilePath(), guest_profile->GetPath()); |
| 443 | EXPECT_TRUE(guest_profile->IsGuestSession()); |
| 444 | |
| 445 | // Tell the browser to open About Chrome. |
| 446 | EXPECT_EQ(1u, active_browser_list_->size()); |
| 447 | [ac orderFrontStandardAboutPanel:NSApp]; |
| 448 | |
| 449 | base::RunLoop().RunUntilIdle(); |
| 450 | |
| 451 | // No new browser is opened; the User Manager opens instead. |
| 452 | EXPECT_EQ(1u, active_browser_list_->size()); |
| 453 | EXPECT_TRUE(UserManager::IsShowing()); |
| 454 | |
| 455 | UserManager::Hide(); |
| 456 | } |
| 457 | |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 458 | class AppControllerOpenShortcutBrowserTest : public InProcessBrowserTest { |
| 459 | protected: |
| 460 | AppControllerOpenShortcutBrowserTest() { |
Hector Carmona | 9797cc6 | 2019-08-09 20:35:35 | [diff] [blame] | 461 | scoped_feature_list_.InitWithFeatures({welcome::kForceEnabled}, {}); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 462 | } |
| 463 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 464 | void SetUpInProcessBrowserTestFixture() override { |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 465 | // In order to mimic opening shortcut during browser startup, we need to |
| 466 | // send the event before -applicationDidFinishLaunching is called, but |
| 467 | // after AppController is loaded. |
| 468 | // |
| 469 | // Since -applicationWillFinishLaunching does nothing now, we swizzle it to |
| 470 | // our function to send the event. We need to do this early before running |
| 471 | // the main message loop. |
| 472 | // |
| 473 | // NSApp does not exist yet. We need to get the AppController using |
| 474 | // reflection. |
| 475 | Class appControllerClass = NSClassFromString(@"AppController"); |
| 476 | Class openShortcutClass = NSClassFromString(@"TestOpenShortcutOnStartup"); |
| 477 | |
| 478 | ASSERT_TRUE(appControllerClass != nil); |
| 479 | ASSERT_TRUE(openShortcutClass != nil); |
| 480 | |
| 481 | SEL targetMethod = @selector(applicationWillFinishLaunching:); |
| 482 | Method original = class_getInstanceMethod(appControllerClass, |
| 483 | targetMethod); |
| 484 | Method destination = class_getInstanceMethod(openShortcutClass, |
| 485 | targetMethod); |
| 486 | |
| 487 | ASSERT_TRUE(original != NULL); |
| 488 | ASSERT_TRUE(destination != NULL); |
| 489 | |
| 490 | method_exchangeImplementations(original, destination); |
Scott Violet | 1f106b58 | 2017-07-12 15:49:58 | [diff] [blame] | 491 | |
| 492 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 493 | g_open_shortcut_url = embedded_test_server()->GetURL("/simple.html"); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 494 | } |
| 495 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 496 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 497 | // If the arg is empty, PrepareTestCommandLine() after this function will |
| 498 | // append about:blank as default url. |
| 499 | command_line->AppendArg(chrome::kChromeUINewTabURL); |
| 500 | } |
Hector Carmona | 1e62e35 | 2019-06-24 19:18:17 | [diff] [blame] | 501 | |
| 502 | private: |
| 503 | base::test::ScopedFeatureList scoped_feature_list_; |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 504 | }; |
| 505 | |
| 506 | IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, |
| 507 | OpenShortcutOnStartup) { |
tmartino | 82e5578 | 2017-01-10 22:30:17 | [diff] [blame] | 508 | // The two tabs expected are the Welcome page and the desired URL. |
| 509 | EXPECT_EQ(2, browser()->tab_strip_model()->count()); |
[email protected] | 66791aff | 2014-04-29 09:45:38 | [diff] [blame] | 510 | EXPECT_EQ(g_open_shortcut_url, |
| 511 | browser()->tab_strip_model()->GetActiveWebContents() |
| 512 | ->GetLastCommittedURL()); |
| 513 | } |
| 514 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 515 | class AppControllerReplaceNTPBrowserTest : public InProcessBrowserTest { |
| 516 | protected: |
| 517 | AppControllerReplaceNTPBrowserTest() {} |
| 518 | |
| 519 | void SetUpInProcessBrowserTestFixture() override { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 520 | ASSERT_TRUE(embedded_test_server()->Start()); |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 521 | } |
| 522 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 523 | void SetUpCommandLine(base::CommandLine* command_line) override { |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 524 | // If the arg is empty, PrepareTestCommandLine() after this function will |
| 525 | // append about:blank as default url. |
| 526 | command_line->AppendArg(chrome::kChromeUINewTabURL); |
| 527 | } |
| 528 | }; |
| 529 | |
| 530 | // Tests that when a GURL is opened after startup, it replaces the NTP. |
| 531 | IN_PROC_BROWSER_TEST_F(AppControllerReplaceNTPBrowserTest, |
| 532 | ReplaceNTPAfterStartup) { |
kristipark | a34b247a | 2018-03-09 19:05:34 | [diff] [blame] | 533 | // Depending on network connectivity, the NTP URL can either be |
| 534 | // chrome://newtab/ or chrome-search://local-ntp/local-ntp.html. See |
| 535 | // local_ntp_test_utils::GetFinalNtpUrl for more details. |
| 536 | std::string expected_url = |
| 537 | local_ntp_test_utils::GetFinalNtpUrl(browser()->profile()).spec(); |
| 538 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 539 | // Ensure that there is exactly 1 tab showing, and the tab is the NTP. |
kristipark | a34b247a | 2018-03-09 19:05:34 | [diff] [blame] | 540 | GURL ntp(expected_url); |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 541 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
tmartino | 82e5578 | 2017-01-10 22:30:17 | [diff] [blame] | 542 | browser()->tab_strip_model()->GetActiveWebContents()->GetController().LoadURL( |
kristipark | a34b247a | 2018-03-09 19:05:34 | [diff] [blame] | 543 | GURL(expected_url), content::Referrer(), |
tmartino | 82e5578 | 2017-01-10 22:30:17 | [diff] [blame] | 544 | ui::PageTransition::PAGE_TRANSITION_LINK, std::string()); |
| 545 | |
| 546 | // Wait for one navigation on the active web contents. |
| 547 | content::TestNavigationObserver ntp_navigation_observer( |
| 548 | browser()->tab_strip_model()->GetActiveWebContents()); |
| 549 | ntp_navigation_observer.Wait(); |
| 550 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 551 | EXPECT_EQ(ntp, |
| 552 | browser() |
| 553 | ->tab_strip_model() |
| 554 | ->GetActiveWebContents() |
| 555 | ->GetLastCommittedURL()); |
| 556 | |
| 557 | GURL simple(embedded_test_server()->GetURL("/simple.html")); |
| 558 | SendAppleEventToOpenUrlToAppController(simple); |
| 559 | |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 560 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
tmartino | 82e5578 | 2017-01-10 22:30:17 | [diff] [blame] | 561 | content::TestNavigationObserver event_navigation_observer( |
| 562 | browser()->tab_strip_model()->GetActiveWebContents()); |
| 563 | event_navigation_observer.Wait(); |
erikchen | 19ee392 | 2014-10-31 19:14:22 | [diff] [blame] | 564 | |
| 565 | EXPECT_EQ(simple, |
| 566 | browser() |
| 567 | ->tab_strip_model() |
| 568 | ->GetActiveWebContents() |
| 569 | ->GetLastCommittedURL()); |
| 570 | } |
| 571 | |
Christian Dullweber | 820ee5f | 2018-04-26 13:24:31 | [diff] [blame] | 572 | // Tests that when a GURL is opened, it is not opened in incognito mode. |
| 573 | IN_PROC_BROWSER_TEST_F(AppControllerBrowserTest, OpenInRegularBrowser) { |
| 574 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 575 | // Create an incognito browser. |
| 576 | Browser* incognito_browser = CreateIncognitoBrowser(browser()->profile()); |
| 577 | EXPECT_EQ(incognito_browser, chrome::GetLastActiveBrowser()); |
| 578 | EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 579 | EXPECT_EQ(1, incognito_browser->tab_strip_model()->count()); |
| 580 | // Open a url. |
| 581 | GURL simple(embedded_test_server()->GetURL("/simple.html")); |
| 582 | SendAppleEventToOpenUrlToAppController(simple); |
| 583 | // It should be opened in the regular browser. |
| 584 | content::TestNavigationObserver event_navigation_observer( |
| 585 | browser()->tab_strip_model()->GetActiveWebContents()); |
| 586 | event_navigation_observer.Wait(); |
| 587 | EXPECT_EQ(2, browser()->tab_strip_model()->count()); |
| 588 | EXPECT_EQ(1, incognito_browser->tab_strip_model()->count()); |
| 589 | EXPECT_EQ(simple, browser() |
| 590 | ->tab_strip_model() |
| 591 | ->GetActiveWebContents() |
| 592 | ->GetLastCommittedURL()); |
| 593 | } |
| 594 | |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 595 | class AppControllerMainMenuBrowserTest : public InProcessBrowserTest { |
| 596 | protected: |
| 597 | AppControllerMainMenuBrowserTest() { |
| 598 | } |
| 599 | }; |
| 600 | |
| 601 | IN_PROC_BROWSER_TEST_F(AppControllerMainMenuBrowserTest, |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 602 | HistoryMenuResetAfterProfileDeletion) { |
| 603 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
tapted | 676995d | 2016-04-18 11:32:29 | [diff] [blame] | 604 | AppController* ac = |
| 605 | base::mac::ObjCCastStrict<AppController>([NSApp delegate]); |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 606 | |
| 607 | // Use the existing profile as profile 1. |
| 608 | Profile* profile1 = browser()->profile(); |
| 609 | |
| 610 | // Create profile 2. |
| 611 | base::FilePath profile2_path = |
| 612 | profile_manager->GenerateNextProfileDirectoryPath(); |
| 613 | base::RunLoop run_loop; |
| 614 | profile_manager->CreateProfileAsync( |
| 615 | profile2_path, |
| 616 | base::Bind(&RunClosureWhenProfileInitialized, |
| 617 | run_loop.QuitClosure()), |
lwchkg | 1f62d24 | 2015-10-29 00:50:43 | [diff] [blame] | 618 | base::string16(), |
lwchkg | 1f62d24 | 2015-10-29 00:50:43 | [diff] [blame] | 619 | std::string()); |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 620 | run_loop.Run(); |
| 621 | Profile* profile2 = profile_manager->GetProfileByPath(profile2_path); |
| 622 | ASSERT_TRUE(profile2); |
| 623 | |
Sidney San MartÃn | aa3b48f | 2018-06-03 08:10:05 | [diff] [blame] | 624 | // Load profile1's History Service backend so it will be assigned to the |
| 625 | // HistoryMenuBridge when windowChangedToProfile is called, or else this test |
| 626 | // will fail flaky. |
| 627 | ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile( |
| 628 | profile1, ServiceAccessType::EXPLICIT_ACCESS)); |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 629 | // Switch the controller to profile1. |
| 630 | [ac windowChangedToProfile:profile1]; |
| 631 | base::RunLoop().RunUntilIdle(); |
| 632 | |
| 633 | // Verify the controller's History Menu corresponds to profile1. |
| 634 | EXPECT_TRUE([ac historyMenuBridge]->service()); |
| 635 | EXPECT_EQ([ac historyMenuBridge]->service(), |
| 636 | HistoryServiceFactory::GetForProfile(profile1, |
| 637 | ServiceAccessType::EXPLICIT_ACCESS)); |
| 638 | |
| 639 | // Load profile2's History Service backend so it will be assigned to the |
| 640 | // HistoryMenuBridge when windowChangedToProfile is called, or else this test |
| 641 | // will fail flaky. |
| 642 | ui_test_utils::WaitForHistoryToLoad( |
| 643 | HistoryServiceFactory::GetForProfile(profile2, |
| 644 | ServiceAccessType::EXPLICIT_ACCESS)); |
| 645 | // Switch the controller to profile2. |
| 646 | [ac windowChangedToProfile:profile2]; |
| 647 | base::RunLoop().RunUntilIdle(); |
| 648 | |
| 649 | // Verify the controller's History Menu has changed. |
| 650 | EXPECT_TRUE([ac historyMenuBridge]->service()); |
| 651 | EXPECT_EQ([ac historyMenuBridge]->service(), |
| 652 | HistoryServiceFactory::GetForProfile(profile2, |
| 653 | ServiceAccessType::EXPLICIT_ACCESS)); |
| 654 | EXPECT_NE( |
| 655 | HistoryServiceFactory::GetForProfile(profile1, |
| 656 | ServiceAccessType::EXPLICIT_ACCESS), |
| 657 | HistoryServiceFactory::GetForProfile(profile2, |
| 658 | ServiceAccessType::EXPLICIT_ACCESS)); |
| 659 | |
| 660 | // Delete profile2. |
David Roger | 5179764 | 2018-03-20 13:29:16 | [diff] [blame] | 661 | profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), |
| 662 | base::DoNothing()); |
mlerman | 8ae56aa | 2015-04-24 13:56:27 | [diff] [blame] | 663 | base::RunLoop().RunUntilIdle(); |
| 664 | |
| 665 | // Verify the controller's history is back to profile1. |
| 666 | EXPECT_EQ([ac historyMenuBridge]->service(), |
| 667 | HistoryServiceFactory::GetForProfile(profile1, |
| 668 | ServiceAccessType::EXPLICIT_ACCESS)); |
| 669 | } |
| 670 | |
| 671 | IN_PROC_BROWSER_TEST_F(AppControllerMainMenuBrowserTest, |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 672 | BookmarksMenuIsRestoredAfterProfileSwitch) { |
| 673 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
Owen Min | b6e1bf29 | 2018-03-15 18:31:52 | [diff] [blame] | 674 | AppController* ac = base::mac::ObjCCast<AppController>( |
| 675 | [[NSApplication sharedApplication] delegate]); |
| 676 | ASSERT_TRUE(ac); |
| 677 | |
Robert Sesek | 124f80f9 | 2018-07-24 20:31:29 | [diff] [blame] | 678 | [ac mainMenuCreated]; |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 679 | |
| 680 | // Constants for bookmarks that we will create later. |
| 681 | const base::string16 title1(base::ASCIIToUTF16("Dinosaur Comics")); |
| 682 | const GURL url1("http://qwantz.com//"); |
| 683 | |
| 684 | const base::string16 title2(base::ASCIIToUTF16("XKCD")); |
| 685 | const GURL url2("https://www.xkcd.com/"); |
| 686 | |
| 687 | // Use the existing profile as profile 1. |
| 688 | Profile* profile1 = browser()->profile(); |
| 689 | bookmarks::test::WaitForBookmarkModelToLoad( |
pke | 3e0d5c9 | 2016-08-08 09:07:30 | [diff] [blame] | 690 | BookmarkModelFactory::GetForBrowserContext(profile1)); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 691 | |
| 692 | // Create profile 2. |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 693 | base::ScopedAllowBlockingForTesting allow_blocking; |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 694 | base::FilePath path2 = profile_manager->GenerateNextProfileDirectoryPath(); |
Daniel Hosseinian | 8f0b748 | 2019-06-07 18:21:04 | [diff] [blame] | 695 | std::unique_ptr<Profile> profile2 = |
| 696 | Profile::CreateProfile(path2, NULL, Profile::CREATE_MODE_SYNCHRONOUS); |
| 697 | Profile* profile2_ptr = profile2.get(); |
| 698 | profile_manager->RegisterTestingProfile(std::move(profile2), false, true); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 699 | bookmarks::test::WaitForBookmarkModelToLoad( |
Daniel Hosseinian | 8f0b748 | 2019-06-07 18:21:04 | [diff] [blame] | 700 | BookmarkModelFactory::GetForBrowserContext(profile2_ptr)); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 701 | |
| 702 | // Switch to profile 1, create bookmark 1 and force the menu to build. |
| 703 | [ac windowChangedToProfile:profile1]; |
| 704 | [ac bookmarkMenuBridge]->GetBookmarkModel()->AddURL( |
| 705 | [ac bookmarkMenuBridge]->GetBookmarkModel()->bookmark_bar_node(), |
| 706 | 0, title1, url1); |
Trent Apted | cf95689a | 2017-11-22 00:01:10 | [diff] [blame] | 707 | NSMenu* profile1_submenu = [ac bookmarkMenuBridge]->BookmarkMenu(); |
| 708 | [[profile1_submenu delegate] menuNeedsUpdate:profile1_submenu]; |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 709 | |
| 710 | // Switch to profile 2, create bookmark 2 and force the menu to build. |
Daniel Hosseinian | 8f0b748 | 2019-06-07 18:21:04 | [diff] [blame] | 711 | [ac windowChangedToProfile:profile2_ptr]; |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 712 | [ac bookmarkMenuBridge]->GetBookmarkModel()->AddURL( |
| 713 | [ac bookmarkMenuBridge]->GetBookmarkModel()->bookmark_bar_node(), |
| 714 | 0, title2, url2); |
Trent Apted | cf95689a | 2017-11-22 00:01:10 | [diff] [blame] | 715 | NSMenu* profile2_submenu = [ac bookmarkMenuBridge]->BookmarkMenu(); |
| 716 | [[profile2_submenu delegate] menuNeedsUpdate:profile2_submenu]; |
| 717 | EXPECT_NE(profile1_submenu, profile2_submenu); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 718 | |
| 719 | // Test that only bookmark 2 is shown. |
| 720 | EXPECT_FALSE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle: |
| 721 | SysUTF16ToNSString(title1)]); |
| 722 | EXPECT_TRUE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle: |
| 723 | SysUTF16ToNSString(title2)]); |
| 724 | |
| 725 | // Switch *back* to profile 1 and *don't* force the menu to build. |
| 726 | [ac windowChangedToProfile:profile1]; |
| 727 | |
| 728 | // Test that only bookmark 1 is shown in the restored menu. |
| 729 | EXPECT_TRUE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle: |
| 730 | SysUTF16ToNSString(title1)]); |
| 731 | EXPECT_FALSE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle: |
| 732 | SysUTF16ToNSString(title2)]); |
Trent Apted | cf95689a | 2017-11-22 00:01:10 | [diff] [blame] | 733 | |
| 734 | // Ensure a cached menu was used. |
| 735 | EXPECT_EQ(profile1_submenu, [ac bookmarkMenuBridge]->BookmarkMenu()); |
lgarron | 9e6dee2 | 2014-11-18 01:03:39 | [diff] [blame] | 736 | } |
| 737 | |
[email protected] | fc44f24 | 2012-02-14 16:54:39 | [diff] [blame] | 738 | } // namespace |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 739 | |
| 740 | //--------------------------AppControllerHandoffBrowserTest--------------------- |
| 741 | |
| 742 | static GURL g_handoff_url; |
| 743 | |
| 744 | @interface AppController (BrowserTest) |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 745 | - (void)new_passURLToHandoffManager:(const GURL&)handoffURL; |
| 746 | @end |
| 747 | |
| 748 | @implementation AppController (BrowserTest) |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 749 | - (void)new_passURLToHandoffManager:(const GURL&)handoffURL { |
| 750 | g_handoff_url = handoffURL; |
| 751 | } |
| 752 | @end |
| 753 | |
| 754 | namespace { |
| 755 | |
| 756 | class AppControllerHandoffBrowserTest : public InProcessBrowserTest { |
| 757 | protected: |
| 758 | AppControllerHandoffBrowserTest() {} |
| 759 | |
| 760 | // Exchanges the implementations of the two selectors on the class |
| 761 | // AppController. |
| 762 | void ExchangeSelectors(SEL originalMethod, SEL newMethod) { |
| 763 | Class appControllerClass = NSClassFromString(@"AppController"); |
| 764 | |
| 765 | ASSERT_TRUE(appControllerClass != nil); |
| 766 | |
| 767 | Method original = |
| 768 | class_getInstanceMethod(appControllerClass, originalMethod); |
| 769 | Method destination = class_getInstanceMethod(appControllerClass, newMethod); |
| 770 | |
| 771 | ASSERT_TRUE(original != NULL); |
| 772 | ASSERT_TRUE(destination != NULL); |
| 773 | |
| 774 | method_exchangeImplementations(original, destination); |
| 775 | } |
| 776 | |
| 777 | // Swizzle Handoff related implementations. |
| 778 | void SetUpInProcessBrowserTestFixture() override { |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 779 | // This swizzle intercepts the URL that would be sent to the Handoff |
| 780 | // Manager, and instead puts it into a variable accessible to this test. |
Avi Drissman | 438827be | 2019-06-06 18:46:11 | [diff] [blame] | 781 | SEL originalMethod = @selector(passURLToHandoffManager:); |
| 782 | SEL newMethod = @selector(new_passURLToHandoffManager:); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 783 | ExchangeSelectors(originalMethod, newMethod); |
| 784 | } |
| 785 | |
| 786 | // Closes the tab, and waits for the close to finish. |
| 787 | void CloseTab(Browser* browser, int index) { |
| 788 | content::WebContentsDestroyedWatcher destroyed_watcher( |
| 789 | browser->tab_strip_model()->GetWebContentsAt(index)); |
| 790 | browser->tab_strip_model()->CloseWebContentsAt( |
| 791 | index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB); |
| 792 | destroyed_watcher.Wait(); |
| 793 | } |
| 794 | }; |
| 795 | |
| 796 | // Tests that as a user switches between tabs, navigates within a tab, and |
| 797 | // switches between browser windows, the correct URL is being passed to the |
| 798 | // Handoff. |
| 799 | IN_PROC_BROWSER_TEST_F(AppControllerHandoffBrowserTest, TestHandoffURLs) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 800 | ASSERT_TRUE(embedded_test_server()->Start()); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 801 | EXPECT_EQ(g_handoff_url, GURL(url::kAboutBlankURL)); |
| 802 | |
| 803 | // Test that navigating to a URL updates the handoff URL. |
| 804 | GURL test_url1 = embedded_test_server()->GetURL("/title1.html"); |
| 805 | ui_test_utils::NavigateToURL(browser(), test_url1); |
| 806 | EXPECT_EQ(g_handoff_url, test_url1); |
| 807 | |
| 808 | // Test that opening a new tab updates the handoff URL. |
| 809 | GURL test_url2 = embedded_test_server()->GetURL("/title2.html"); |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 810 | NavigateParams params(browser(), test_url2, ui::PAGE_TRANSITION_LINK); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 811 | params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 812 | ui_test_utils::NavigateToURL(¶ms); |
| 813 | EXPECT_EQ(g_handoff_url, test_url2); |
| 814 | |
| 815 | // Test that switching tabs updates the handoff URL. |
Edwin Joe | 6f6fc1e | 2019-02-27 20:00:37 | [diff] [blame] | 816 | browser()->tab_strip_model()->ActivateTabAt( |
| 817 | 0, {TabStripModel::GestureType::kOther}); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 818 | EXPECT_EQ(g_handoff_url, test_url1); |
| 819 | |
| 820 | // Test that closing the current tab updates the handoff URL. |
| 821 | CloseTab(browser(), 0); |
| 822 | EXPECT_EQ(g_handoff_url, test_url2); |
| 823 | |
| 824 | // Test that opening a new browser window updates the handoff URL. |
| 825 | GURL test_url3 = embedded_test_server()->GetURL("/title3.html"); |
| 826 | ui_test_utils::NavigateToURLWithDisposition( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 827 | browser(), GURL(test_url3), WindowOpenDisposition::NEW_WINDOW, |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 828 | ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); |
| 829 | EXPECT_EQ(g_handoff_url, test_url3); |
| 830 | |
| 831 | // Check that there are exactly 2 browsers. |
scottmg | 0d8e4ab | 2016-01-28 00:34:55 | [diff] [blame] | 832 | BrowserList* active_browser_list = BrowserList::GetInstance(); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 833 | EXPECT_EQ(2u, active_browser_list->size()); |
| 834 | |
erikchen | 64a6d44 | 2015-07-10 19:38:20 | [diff] [blame] | 835 | // Close the second browser window (which only has 1 tab left). |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 836 | Browser* browser2 = active_browser_list->get(1); |
erikchen | 64a6d44 | 2015-07-10 19:38:20 | [diff] [blame] | 837 | CloseBrowserSynchronously(browser2); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 838 | EXPECT_EQ(g_handoff_url, test_url2); |
| 839 | |
| 840 | // The URLs of incognito windows should not be passed to Handoff. |
| 841 | GURL test_url4 = embedded_test_server()->GetURL("/simple.html"); |
| 842 | ui_test_utils::NavigateToURLWithDisposition( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 843 | browser(), GURL(test_url4), WindowOpenDisposition::OFF_THE_RECORD, |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 844 | ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); |
| 845 | EXPECT_EQ(g_handoff_url, GURL()); |
| 846 | |
| 847 | // Open a new tab in the incognito window. |
| 848 | EXPECT_EQ(2u, active_browser_list->size()); |
| 849 | Browser* browser3 = active_browser_list->get(1); |
| 850 | ui_test_utils::NavigateToURLWithDisposition( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 851 | browser3, test_url4, WindowOpenDisposition::NEW_FOREGROUND_TAB, |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 852 | ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 853 | EXPECT_EQ(g_handoff_url, GURL()); |
| 854 | |
| 855 | // Navigate the current tab in the incognito window. |
thestig | 53986dc | 2014-12-16 06:09:18 | [diff] [blame] | 856 | ui_test_utils::NavigateToURL(browser3, test_url1); |
erikchen | 600f796 | 2014-12-12 00:17:38 | [diff] [blame] | 857 | EXPECT_EQ(g_handoff_url, GURL()); |
| 858 | |
| 859 | // Activate the original browser window. |
| 860 | Browser* browser1 = active_browser_list->get(0); |
| 861 | browser1->window()->Show(); |
| 862 | EXPECT_EQ(g_handoff_url, test_url2); |
| 863 | } |
| 864 | |
| 865 | } // namespace |