Index: chrome/browser/app_controller_mac_browsertest.mm |
diff --git a/chrome/browser/app_controller_mac_browsertest.mm b/chrome/browser/app_controller_mac_browsertest.mm |
index 61400016edca21f617cb66d2f241155eaea5c767..5c81b42e3895c26bf95f373ed7f95063b8f18aa5 100644 |
--- a/chrome/browser/app_controller_mac_browsertest.mm |
+++ b/chrome/browser/app_controller_mac_browsertest.mm |
@@ -2,7 +2,12 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#import <Carbon/Carbon.h> |
#import <Cocoa/Cocoa.h> |
+#import <Foundation/Foundation.h> |
+#import <Foundation/NSAppleEventDescriptor.h> |
+#import <objc/message.h> |
+#import <objc/runtime.h> |
#include "apps/app_window_registry.h" |
#include "base/command_line.h" |
@@ -23,10 +28,53 @@ |
#include "chrome/common/chrome_constants.h" |
#include "chrome/common/chrome_switches.h" |
#include "chrome/common/pref_names.h" |
+#include "chrome/common/url_constants.h" |
#include "chrome/test/base/in_process_browser_test.h" |
#include "chrome/test/base/ui_test_utils.h" |
#include "content/public/browser/web_contents.h" |
#include "extensions/common/extension.h" |
+#include "net/test/embedded_test_server/embedded_test_server.h" |
+ |
+namespace { |
+ |
+GURL open_shortcut_url = GURL::EmptyGURL(); |
Alexei Svitkine (slow)
2014/04/25 14:47:08
Nit: g_open_shortcut_url
|
+ |
+} // namespace |
+ |
+@interface TestOpenShortcutOnStartup : NSObject |
+- (void)applicationWillFinishLaunching:(NSNotification*)notification; |
+@end |
+ |
+@implementation TestOpenShortcutOnStartup |
+ |
+- (void)applicationWillFinishLaunching:(NSNotification*)notification { |
+ if (!open_shortcut_url.is_valid()) |
+ return; |
+ |
+ AppController* controller = (AppController*)[NSApp delegate]; |
Alexei Svitkine (slow)
2014/04/25 14:47:08
Use ObjCCast() from foundation_util.h instead, whi
|
+ Method getUrl = class_getInstanceMethod([controller class], |
+ @selector(getUrl:withReply:)); |
+ |
+ if (getUrl == nil) |
+ return; |
+ |
+ base::scoped_nsobject<NSAppleEventDescriptor> shortcutEvent( |
+ [[NSAppleEventDescriptor alloc] |
+ initWithEventClass:kASAppleScriptSuite |
+ eventID:kASSubroutineEvent |
+ targetDescriptor:nil |
+ returnID:kAutoGenerateReturnID |
+ transactionID:kAnyTransactionID]); |
+ NSString* url = |
+ [NSString stringWithUTF8String:open_shortcut_url.spec().c_str()]; |
+ [shortcutEvent setParamDescriptor: |
+ [NSAppleEventDescriptor descriptorWithString:url] |
+ forKeyword:keyDirectObject]; |
Alexei Svitkine (slow)
2014/04/25 14:47:08
Nit: Sorry, maybe my previous comment was confusin
Alexei Svitkine (slow)
2014/04/25 14:47:54
*within a single call, rather
|
+ |
+ method_invoke(controller, getUrl, shortcutEvent.get(), NULL); |
+} |
+ |
+@end |
namespace { |
@@ -227,4 +275,54 @@ IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, |
UserManagerMac::Hide(); |
} |
+class AppControllerOpenShortcutBrowserTest : public InProcessBrowserTest { |
+ protected: |
+ AppControllerOpenShortcutBrowserTest() { |
+ } |
+ |
+ virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
+ // In order to mimic opening shortcut during browser startup, we need to |
+ // send the event before -applicationDidFinishLaunching is called, but |
+ // after AppController is loaded. |
+ // |
+ // Since -applicationWillFinishLaunching does nothing now, we swizzle it to |
+ // our function to send the event. We need to do this early before running |
+ // the main message loop. |
+ // |
+ // NSApp does not exist yet. We need to get the AppController using |
+ // reflection. |
+ Class appControllerClass = NSClassFromString(@"AppController"); |
+ Class openShortcutClass = NSClassFromString(@"TestOpenShortcutOnStartup"); |
+ |
+ ASSERT_TRUE(appControllerClass != nil && openShortcutClass != nil); |
Alexei Svitkine (slow)
2014/04/25 14:47:08
Nit: Can you split these into separate ASSERT line
|
+ |
+ SEL targetMethod = @selector(applicationWillFinishLaunching:); |
+ Method original = class_getInstanceMethod(appControllerClass, |
+ targetMethod); |
+ Method destination = class_getInstanceMethod(openShortcutClass, |
+ targetMethod); |
+ |
+ ASSERT_TRUE(original != NULL && destination != NULL); |
+ |
+ method_exchangeImplementations(original, destination); |
+ |
+ ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
+ open_shortcut_url = embedded_test_server()->GetURL("/simple.html"); |
+ } |
+ |
+ virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
+ // If the arg is empty, PrepareTestCommandLine() after this function will |
+ // append about:blank as default url. |
+ command_line->AppendArg(chrome::kChromeUINewTabURL); |
+ } |
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, |
+ OpenShortcutOnStartup) { |
+ EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
+ EXPECT_EQ(open_shortcut_url, |
+ browser()->tab_strip_model()->GetActiveWebContents() |
+ ->GetLastCommittedURL()); |
+} |
+ |
} // namespace |