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..24c744ecca9e0132c945798049b7fb9d2c70566f 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/NSAppleEventDescriptor.h> |
+#import <Foundation/Foundation.h> |
+#import <objc/message.h> |
+#import <objc/runtime.h> |
#include "apps/app_window_registry.h" |
#include "base/command_line.h" |
@@ -23,10 +28,52 @@ |
#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 openShortcutUrl = GURL::EmptyGURL(); |
Alexei Svitkine (slow)
2014/04/24 18:59:28
Nit: Use c++ naming convention here.
|
+ |
+} // namespace |
+ |
+@interface TestOpenShortcutOnStartup : NSObject |
+- (void)applicationWillFinishLaunching:(NSNotification*)notification; |
+@end |
+ |
+@implementation TestOpenShortcutOnStartup |
+ |
+- (void)applicationWillFinishLaunching:(NSNotification*)notification { |
+ if (!openShortcutUrl.is_valid()) |
+ return; |
+ |
+ AppController* controller = (AppController*)[NSApp delegate]; |
+ 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]); |
+ [shortcutEvent setParamDescriptor: |
+ [NSAppleEventDescriptor descriptorWithString: |
+ [NSString stringWithUTF8String:openShortcutUrl.spec().c_str()]] |
+ forKeyword:keyDirectObject]; |
Alexei Svitkine (slow)
2014/04/24 18:59:28
Nit: Align :'s
|
+ |
+ method_invoke(controller, getUrl, shortcutEvent.get(), NULL); |
+} |
+ |
+@end |
namespace { |
@@ -227,4 +274,64 @@ 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 |
Alexei Svitkine (slow)
2014/04/24 18:59:28
Nit: "applicationDidFinishLaunching" -> "-applicat
|
+ // 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"); |
+ |
+ // We set |constructor_success_| to false on failures. |
+ if (appControllerClass == nil || openShortcutClass == nil) { |
+ constructor_success_ = false; |
+ return; |
+ } |
+ |
+ Method original = class_getInstanceMethod(appControllerClass, |
+ @selector(applicationWillFinishLaunching:)); |
Alexei Svitkine (slow)
2014/04/24 18:59:28
Nit: Extract to local SEL variable and re-use belo
|
+ Method destination = class_getInstanceMethod(openShortcutClass, |
+ @selector(applicationWillFinishLaunching:)); |
+ |
+ if (original == NULL || destination == NULL) { |
+ constructor_success_ = false; |
+ return; |
+ } |
+ |
+ method_exchangeImplementations(original, destination); |
+ |
+ constructor_success_ = |
+ embedded_test_server()->InitializeAndWaitUntilReady(); |
+ openShortcutUrl = 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); |
+ } |
+ |
+ bool constructor_success_ = true; |
Alexei Svitkine (slow)
2014/04/24 18:59:28
Instead of exposing this as a protected variable,
|
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, |
+ OpenShortcutOnStartup) { |
+ EXPECT_TRUE(constructor_success_); |
+ EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
+ EXPECT_EQ(openShortcutUrl, |
+ browser()->tab_strip_model()->GetActiveWebContents() |
+ ->GetLastCommittedURL()); |
+} |
+ |
} // namespace |