| OLD | NEW |
| 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 <Carbon/Carbon.h> |
| 6 #import <Cocoa/Cocoa.h> | 6 #import <Cocoa/Cocoa.h> |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 #import <Foundation/NSAppleEventDescriptor.h> | 8 #import <Foundation/NSAppleEventDescriptor.h> |
| 9 #import <objc/message.h> | 9 #import <objc/message.h> |
| 10 #import <objc/runtime.h> | 10 #import <objc/runtime.h> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
| 35 #include "extensions/browser/app_window/app_window_registry.h" | 35 #include "extensions/browser/app_window/app_window_registry.h" |
| 36 #include "extensions/common/extension.h" | 36 #include "extensions/common/extension.h" |
| 37 #include "extensions/test/extension_test_message_listener.h" | 37 #include "extensions/test/extension_test_message_listener.h" |
| 38 #include "net/test/embedded_test_server/embedded_test_server.h" | 38 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 39 | 39 |
| 40 namespace { | 40 namespace { |
| 41 | 41 |
| 42 GURL g_open_shortcut_url = GURL::EmptyGURL(); | 42 GURL g_open_shortcut_url = GURL::EmptyGURL(); |
| 43 | 43 |
| 44 // Returns an Apple Event that instructs the application to open |url|. |
| 45 NSAppleEventDescriptor* AppleEventToOpenUrl(const GURL& url) { |
| 46 NSAppleEventDescriptor* shortcut_event = [[[NSAppleEventDescriptor alloc] |
| 47 initWithEventClass:kASAppleScriptSuite |
| 48 eventID:kASSubroutineEvent |
| 49 targetDescriptor:nil |
| 50 returnID:kAutoGenerateReturnID |
| 51 transactionID:kAnyTransactionID] autorelease]; |
| 52 NSString* url_string = [NSString stringWithUTF8String:url.spec().c_str()]; |
| 53 [shortcut_event setParamDescriptor:[NSAppleEventDescriptor |
| 54 descriptorWithString:url_string] |
| 55 forKeyword:keyDirectObject]; |
| 56 return shortcut_event; |
| 57 } |
| 58 |
| 59 // Instructs the NSApp's delegate to open |url|. |
| 60 void SendAppleEventToOpenUrlToAppController(const GURL& url) { |
| 61 AppController* controller = |
| 62 base::mac::ObjCCast<AppController>([NSApp delegate]); |
| 63 Method get_url = |
| 64 class_getInstanceMethod([controller class], @selector(getUrl:withReply:)); |
| 65 |
| 66 ASSERT_TRUE(get_url); |
| 67 |
| 68 NSAppleEventDescriptor* shortcut_event = |
| 69 AppleEventToOpenUrl(g_open_shortcut_url); |
| 70 |
| 71 method_invoke(controller, get_url, shortcut_event, NULL); |
| 72 } |
| 73 |
| 44 } // namespace | 74 } // namespace |
| 45 | 75 |
| 46 @interface TestOpenShortcutOnStartup : NSObject | 76 @interface TestOpenShortcutOnStartup : NSObject |
| 47 - (void)applicationWillFinishLaunching:(NSNotification*)notification; | 77 - (void)applicationWillFinishLaunching:(NSNotification*)notification; |
| 48 @end | 78 @end |
| 49 | 79 |
| 50 @implementation TestOpenShortcutOnStartup | 80 @implementation TestOpenShortcutOnStartup |
| 51 | 81 |
| 52 - (void)applicationWillFinishLaunching:(NSNotification*)notification { | 82 - (void)applicationWillFinishLaunching:(NSNotification*)notification { |
| 53 if (!g_open_shortcut_url.is_valid()) | 83 if (!g_open_shortcut_url.is_valid()) |
| 54 return; | 84 return; |
| 55 | 85 |
| 56 AppController* controller = | 86 SendAppleEventToOpenUrlToAppController(g_open_shortcut_url); |
| 57 base::mac::ObjCCast<AppController>([NSApp delegate]); | |
| 58 Method getUrl = class_getInstanceMethod([controller class], | |
| 59 @selector(getUrl:withReply:)); | |
| 60 | |
| 61 if (getUrl == nil) | |
| 62 return; | |
| 63 | |
| 64 base::scoped_nsobject<NSAppleEventDescriptor> shortcutEvent( | |
| 65 [[NSAppleEventDescriptor alloc] | |
| 66 initWithEventClass:kASAppleScriptSuite | |
| 67 eventID:kASSubroutineEvent | |
| 68 targetDescriptor:nil | |
| 69 returnID:kAutoGenerateReturnID | |
| 70 transactionID:kAnyTransactionID]); | |
| 71 NSString* url = | |
| 72 [NSString stringWithUTF8String:g_open_shortcut_url.spec().c_str()]; | |
| 73 [shortcutEvent setParamDescriptor: | |
| 74 [NSAppleEventDescriptor descriptorWithString:url] | |
| 75 forKeyword:keyDirectObject]; | |
| 76 | |
| 77 method_invoke(controller, getUrl, shortcutEvent.get(), NULL); | |
| 78 } | 87 } |
| 79 | 88 |
| 80 @end | 89 @end |
| 81 | 90 |
| 82 namespace { | 91 namespace { |
| 83 | 92 |
| 84 class AppControllerPlatformAppBrowserTest | 93 class AppControllerPlatformAppBrowserTest |
| 85 : public extensions::PlatformAppBrowserTest { | 94 : public extensions::PlatformAppBrowserTest { |
| 86 protected: | 95 protected: |
| 87 AppControllerPlatformAppBrowserTest() | 96 AppControllerPlatformAppBrowserTest() |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 }; | 332 }; |
| 324 | 333 |
| 325 IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, | 334 IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, |
| 326 OpenShortcutOnStartup) { | 335 OpenShortcutOnStartup) { |
| 327 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 336 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 328 EXPECT_EQ(g_open_shortcut_url, | 337 EXPECT_EQ(g_open_shortcut_url, |
| 329 browser()->tab_strip_model()->GetActiveWebContents() | 338 browser()->tab_strip_model()->GetActiveWebContents() |
| 330 ->GetLastCommittedURL()); | 339 ->GetLastCommittedURL()); |
| 331 } | 340 } |
| 332 | 341 |
| 342 class AppControllerReplaceNTPBrowserTest : public InProcessBrowserTest { |
| 343 protected: |
| 344 AppControllerReplaceNTPBrowserTest() {} |
| 345 }; |
| 346 |
| 347 // Tests that when a GURL is opened after startup, it replaces the NTP. |
| 348 IN_PROC_BROWSER_TEST_F(AppControllerReplaceNTPBrowserTest, |
| 349 ReplaceNTPAfterStartup) { |
| 350 // Ensure that there is exactly 1 tab showing, and the tab is the NTP. |
| 351 GURL ntp(chrome::kChromeUINewTabURL); |
| 352 ui_test_utils::NavigateToURLWithDisposition( |
| 353 browser(), |
| 354 ntp, |
| 355 CURRENT_TAB, |
| 356 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 357 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 358 EXPECT_EQ(ntp, |
| 359 browser() |
| 360 ->tab_strip_model() |
| 361 ->GetActiveWebContents() |
| 362 ->GetLastCommittedURL()); |
| 363 |
| 364 GURL simple(embedded_test_server()->GetURL("/simple.html")); |
| 365 SendAppleEventToOpenUrlToAppController(simple); |
| 366 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 367 EXPECT_EQ(simple, |
| 368 browser() |
| 369 ->tab_strip_model() |
| 370 ->GetActiveWebContents() |
| 371 ->GetLastCommittedURL()); |
| 372 } |
| 373 |
| 333 } // namespace | 374 } // namespace |
| OLD | NEW |