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