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 <Cocoa/Cocoa.h> | 6 #import <Cocoa/Cocoa.h> |
| 7 #import <Foundation/NSAppleEventDescriptor.h> |
| 8 #import <Foundation/Foundation.h> |
| 9 #import <objc/runtime.h> |
| 10 #import <objc/message.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" |
9 #include "base/mac/scoped_nsobject.h" | 14 #include "base/mac/scoped_nsobject.h" |
10 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
11 #include "chrome/app/chrome_command_ids.h" | 16 #include "chrome/app/chrome_command_ids.h" |
12 #import "chrome/browser/app_controller_mac.h" | 17 #import "chrome/browser/app_controller_mac.h" |
13 #include "chrome/browser/apps/app_browsertest_util.h" | 18 #include "chrome/browser/apps/app_browsertest_util.h" |
14 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/extensions/extension_test_message_listener.h" | 20 #include "chrome/browser/extensions/extension_test_message_listener.h" |
16 #include "chrome/browser/profiles/profile_manager.h" | 21 #include "chrome/browser/profiles/profile_manager.h" |
17 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/browser_list.h" | 23 #include "chrome/browser/ui/browser_list.h" |
19 #include "chrome/browser/ui/browser_window.h" | 24 #include "chrome/browser/ui/browser_window.h" |
20 #import "chrome/browser/ui/cocoa/profiles/user_manager_mac.h" | 25 #import "chrome/browser/ui/cocoa/profiles/user_manager_mac.h" |
21 #include "chrome/browser/ui/host_desktop.h" | 26 #include "chrome/browser/ui/host_desktop.h" |
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 27 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
23 #include "chrome/common/chrome_constants.h" | 28 #include "chrome/common/chrome_constants.h" |
24 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
25 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
| 31 #include "chrome/common/url_constants.h" |
26 #include "chrome/test/base/in_process_browser_test.h" | 32 #include "chrome/test/base/in_process_browser_test.h" |
27 #include "chrome/test/base/ui_test_utils.h" | 33 #include "chrome/test/base/ui_test_utils.h" |
28 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
29 #include "extensions/common/extension.h" | 35 #include "extensions/common/extension.h" |
| 36 #include "net/test/embedded_test_server/embedded_test_server.h" |
30 | 37 |
31 namespace { | 38 namespace { |
32 | 39 |
| 40 GURL openShortcutUrl = GURL::EmptyGURL(); |
| 41 |
| 42 } // namespace |
| 43 |
| 44 @interface TestOpenShortcutOnStartup : NSObject |
| 45 - (void)applicationWillFinishLaunching:(NSNotification*)notification; |
| 46 @end |
| 47 |
| 48 @implementation TestOpenShortcutOnStartup |
| 49 |
| 50 - (void)applicationWillFinishLaunching:(NSNotification*)notification { |
| 51 if (!openShortcutUrl.is_valid()) |
| 52 return; |
| 53 |
| 54 AppController* controller = (AppController*)[NSApp delegate]; |
| 55 Method getUrl = class_getInstanceMethod([controller class], |
| 56 @selector(getUrl:withReply:)); |
| 57 |
| 58 if (getUrl == nil) |
| 59 return; |
| 60 |
| 61 base::scoped_nsobject<NSAppleEventDescriptor> shortcutEvent( |
| 62 [[NSAppleEventDescriptor alloc] |
| 63 initWithEventClass:kASAppleScriptSuite |
| 64 eventID:kASSubroutineEvent |
| 65 targetDescriptor:nil |
| 66 returnID:kAutoGenerateReturnID |
| 67 transactionID:kAnyTransactionID]); |
| 68 [shortcutEvent setParamDescriptor: |
| 69 [NSAppleEventDescriptor descriptorWithString: |
| 70 [NSString stringWithUTF8String:openShortcutUrl.spec().c_str()]] |
| 71 forKeyword:keyDirectObject]; |
| 72 |
| 73 method_invoke(controller, getUrl, shortcutEvent.get(), NULL); |
| 74 } |
| 75 |
| 76 @end |
| 77 |
| 78 namespace { |
| 79 |
33 class AppControllerPlatformAppBrowserTest | 80 class AppControllerPlatformAppBrowserTest |
34 : public extensions::PlatformAppBrowserTest { | 81 : public extensions::PlatformAppBrowserTest { |
35 protected: | 82 protected: |
36 AppControllerPlatformAppBrowserTest() | 83 AppControllerPlatformAppBrowserTest() |
37 : active_browser_list_(BrowserList::GetInstance( | 84 : active_browser_list_(BrowserList::GetInstance( |
38 chrome::GetActiveDesktop())) { | 85 chrome::GetActiveDesktop())) { |
39 } | 86 } |
40 | 87 |
41 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 88 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
42 PlatformAppBrowserTest::SetUpCommandLine(command_line); | 89 PlatformAppBrowserTest::SetUpCommandLine(command_line); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; | 267 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; |
221 EXPECT_FALSE(result); | 268 EXPECT_FALSE(result); |
222 | 269 |
223 base::RunLoop().RunUntilIdle(); | 270 base::RunLoop().RunUntilIdle(); |
224 | 271 |
225 EXPECT_EQ(1u, active_browser_list_->size()); | 272 EXPECT_EQ(1u, active_browser_list_->size()); |
226 EXPECT_TRUE(UserManagerMac::IsShowing()); | 273 EXPECT_TRUE(UserManagerMac::IsShowing()); |
227 UserManagerMac::Hide(); | 274 UserManagerMac::Hide(); |
228 } | 275 } |
229 | 276 |
| 277 class AppControllerOpenShortcutBrowserTest : public InProcessBrowserTest { |
| 278 protected: |
| 279 AppControllerOpenShortcutBrowserTest() { |
| 280 } |
| 281 |
| 282 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 283 // In order to mimic opening shortcut during browser startup, we need to |
| 284 // send the event before applicationDidFinishLaunching is called, but |
| 285 // after AppController is loaded. |
| 286 // |
| 287 // Since applicationWillFinishLaunching does nothing now, we swizzle it to |
| 288 // our function to send the event. We need to do this early before running |
| 289 // the main message loop. |
| 290 // |
| 291 // NSApp does not exist yet. We need to get the AppController using |
| 292 // reflection. |
| 293 Class appControllerClass = NSClassFromString(@"AppController"); |
| 294 Class openShortcutClass = NSClassFromString(@"TestOpenShortcutOnStartup"); |
| 295 |
| 296 // We set |constructor_success_| to false on failures. |
| 297 if (appControllerClass == nil || openShortcutClass == nil) { |
| 298 constructor_success_ = false; |
| 299 return; |
| 300 } |
| 301 |
| 302 Method original = class_getInstanceMethod(appControllerClass, |
| 303 @selector(applicationWillFinishLaunching:)); |
| 304 Method destination = class_getInstanceMethod(openShortcutClass, |
| 305 @selector(applicationWillFinishLaunching:)); |
| 306 |
| 307 if (original == NULL || destination == NULL) { |
| 308 constructor_success_ = false; |
| 309 return; |
| 310 } |
| 311 |
| 312 method_exchangeImplementations(original, destination); |
| 313 |
| 314 constructor_success_ = |
| 315 embedded_test_server()->InitializeAndWaitUntilReady(); |
| 316 openShortcutUrl = embedded_test_server()->GetURL("/simple.html"); |
| 317 } |
| 318 |
| 319 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 320 // If the arg is empty, PrepareTestCommandLine() after this function will |
| 321 // append about:blank as default url. |
| 322 command_line->AppendArg(chrome::kChromeUINewTabURL); |
| 323 } |
| 324 |
| 325 bool constructor_success_ = true; |
| 326 }; |
| 327 |
| 328 IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, |
| 329 OpenShortcutOnStartup) { |
| 330 EXPECT_TRUE(constructor_success_); |
| 331 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 332 EXPECT_EQ(openShortcutUrl, |
| 333 browser()->tab_strip_model()->GetActiveWebContents() |
| 334 ->GetLastCommittedURL()); |
| 335 } |
| 336 |
230 } // namespace | 337 } // namespace |
OLD | NEW |