Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(382)

Side by Side Diff: chrome/browser/app_controller_mac_browsertest.mm

Issue 240273002: Close the ntp during startup when opening a link on Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/app_controller_mac.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Carbon/Carbon.h>
5 #import <Cocoa/Cocoa.h> 6 #import <Cocoa/Cocoa.h>
7 #import <Foundation/Foundation.h>
8 #import <Foundation/NSAppleEventDescriptor.h>
9 #import <objc/message.h>
10 #import <objc/runtime.h>
6 11
7 #include "apps/app_window_registry.h" 12 #include "apps/app_window_registry.h"
8 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/mac/foundation_util.h"
9 #include "base/mac/scoped_nsobject.h" 15 #include "base/mac/scoped_nsobject.h"
10 #include "base/prefs/pref_service.h" 16 #include "base/prefs/pref_service.h"
11 #include "chrome/app/chrome_command_ids.h" 17 #include "chrome/app/chrome_command_ids.h"
12 #import "chrome/browser/app_controller_mac.h" 18 #import "chrome/browser/app_controller_mac.h"
13 #include "chrome/browser/apps/app_browsertest_util.h" 19 #include "chrome/browser/apps/app_browsertest_util.h"
14 #include "chrome/browser/browser_process.h" 20 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/extension_test_message_listener.h" 21 #include "chrome/browser/extensions/extension_test_message_listener.h"
16 #include "chrome/browser/profiles/profile_manager.h" 22 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/ui/browser.h" 23 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_list.h" 24 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/browser_window.h" 25 #include "chrome/browser/ui/browser_window.h"
20 #import "chrome/browser/ui/cocoa/profiles/user_manager_mac.h" 26 #import "chrome/browser/ui/cocoa/profiles/user_manager_mac.h"
21 #include "chrome/browser/ui/host_desktop.h" 27 #include "chrome/browser/ui/host_desktop.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" 28 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "chrome/common/chrome_constants.h" 29 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_switches.h" 30 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h" 31 #include "chrome/common/pref_names.h"
32 #include "chrome/common/url_constants.h"
26 #include "chrome/test/base/in_process_browser_test.h" 33 #include "chrome/test/base/in_process_browser_test.h"
27 #include "chrome/test/base/ui_test_utils.h" 34 #include "chrome/test/base/ui_test_utils.h"
28 #include "content/public/browser/web_contents.h" 35 #include "content/public/browser/web_contents.h"
29 #include "extensions/common/extension.h" 36 #include "extensions/common/extension.h"
37 #include "net/test/embedded_test_server/embedded_test_server.h"
30 38
31 namespace { 39 namespace {
32 40
41 GURL g_open_shortcut_url = GURL::EmptyGURL();
Nico 2014/04/28 21:17:21 Can you pass this around instead of making this a
wjywbs 2014/04/28 21:24:01 It's used by the outer @implementation TestOpenSho
42
43 } // namespace
44
45 @interface TestOpenShortcutOnStartup : NSObject
46 - (void)applicationWillFinishLaunching:(NSNotification*)notification;
47 @end
48
49 @implementation TestOpenShortcutOnStartup
50
51 - (void)applicationWillFinishLaunching:(NSNotification*)notification {
52 if (!g_open_shortcut_url.is_valid())
53 return;
54
55 AppController* controller =
56 base::mac::ObjCCast<AppController>([NSApp delegate]);
57 Method getUrl = class_getInstanceMethod([controller class],
58 @selector(getUrl:withReply:));
59
60 if (getUrl == nil)
61 return;
62
63 base::scoped_nsobject<NSAppleEventDescriptor> shortcutEvent(
64 [[NSAppleEventDescriptor alloc]
65 initWithEventClass:kASAppleScriptSuite
66 eventID:kASSubroutineEvent
67 targetDescriptor:nil
68 returnID:kAutoGenerateReturnID
69 transactionID:kAnyTransactionID]);
70 NSString* url =
71 [NSString stringWithUTF8String:g_open_shortcut_url.spec().c_str()];
72 [shortcutEvent setParamDescriptor:
73 [NSAppleEventDescriptor descriptorWithString:url]
74 forKeyword:keyDirectObject];
75
76 method_invoke(controller, getUrl, shortcutEvent.get(), NULL);
77 }
78
79 @end
80
81 namespace {
82
33 class AppControllerPlatformAppBrowserTest 83 class AppControllerPlatformAppBrowserTest
34 : public extensions::PlatformAppBrowserTest { 84 : public extensions::PlatformAppBrowserTest {
35 protected: 85 protected:
36 AppControllerPlatformAppBrowserTest() 86 AppControllerPlatformAppBrowserTest()
37 : active_browser_list_(BrowserList::GetInstance( 87 : active_browser_list_(BrowserList::GetInstance(
38 chrome::GetActiveDesktop())) { 88 chrome::GetActiveDesktop())) {
39 } 89 }
40 90
41 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 91 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
42 PlatformAppBrowserTest::SetUpCommandLine(command_line); 92 PlatformAppBrowserTest::SetUpCommandLine(command_line);
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; 270 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
221 EXPECT_FALSE(result); 271 EXPECT_FALSE(result);
222 272
223 base::RunLoop().RunUntilIdle(); 273 base::RunLoop().RunUntilIdle();
224 274
225 EXPECT_EQ(1u, active_browser_list_->size()); 275 EXPECT_EQ(1u, active_browser_list_->size());
226 EXPECT_TRUE(UserManagerMac::IsShowing()); 276 EXPECT_TRUE(UserManagerMac::IsShowing());
227 UserManagerMac::Hide(); 277 UserManagerMac::Hide();
228 } 278 }
229 279
280 class AppControllerOpenShortcutBrowserTest : public InProcessBrowserTest {
281 protected:
282 AppControllerOpenShortcutBrowserTest() {
283 }
284
285 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
286 // In order to mimic opening shortcut during browser startup, we need to
287 // send the event before -applicationDidFinishLaunching is called, but
288 // after AppController is loaded.
289 //
290 // Since -applicationWillFinishLaunching does nothing now, we swizzle it to
291 // our function to send the event. We need to do this early before running
292 // the main message loop.
293 //
294 // NSApp does not exist yet. We need to get the AppController using
295 // reflection.
296 Class appControllerClass = NSClassFromString(@"AppController");
297 Class openShortcutClass = NSClassFromString(@"TestOpenShortcutOnStartup");
298
299 ASSERT_TRUE(appControllerClass != nil);
300 ASSERT_TRUE(openShortcutClass != nil);
301
302 SEL targetMethod = @selector(applicationWillFinishLaunching:);
303 Method original = class_getInstanceMethod(appControllerClass,
304 targetMethod);
305 Method destination = class_getInstanceMethod(openShortcutClass,
306 targetMethod);
307
308 ASSERT_TRUE(original != NULL);
309 ASSERT_TRUE(destination != NULL);
310
311 method_exchangeImplementations(original, destination);
312
313 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
314 g_open_shortcut_url = embedded_test_server()->GetURL("/simple.html");
315 }
316
317 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
318 // If the arg is empty, PrepareTestCommandLine() after this function will
319 // append about:blank as default url.
320 command_line->AppendArg(chrome::kChromeUINewTabURL);
321 }
322 };
323
324 IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest,
325 OpenShortcutOnStartup) {
326 EXPECT_EQ(1, browser()->tab_strip_model()->count());
327 EXPECT_EQ(g_open_shortcut_url,
328 browser()->tab_strip_model()->GetActiveWebContents()
329 ->GetLastCommittedURL());
330 }
331
230 } // namespace 332 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/app_controller_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698