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/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" |
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 open_shortcut_url = 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 (!open_shortcut_url.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:open_shortcut_url.spec().c_st r()]] | |
wjywbs
2014/04/24 21:26:46
I aligned the :'s, but this line is >80 chars. Wha
Alexei Svitkine (slow)
2014/04/24 21:39:24
Can you make a local variable for [NSString string
| |
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 ASSERT_TRUE(appControllerClass != nil && openShortcutClass != nil); | |
297 | |
298 SEL targetMethod = @selector(applicationWillFinishLaunching:); | |
299 Method original = class_getInstanceMethod(appControllerClass, | |
300 targetMethod); | |
301 Method destination = class_getInstanceMethod(openShortcutClass, | |
302 targetMethod); | |
303 | |
304 ASSERT_TRUE(original != NULL && destination != NULL); | |
305 | |
306 method_exchangeImplementations(original, destination); | |
307 | |
308 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
309 open_shortcut_url = embedded_test_server()->GetURL("/simple.html"); | |
310 } | |
311 | |
312 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
313 // If the arg is empty, PrepareTestCommandLine() after this function will | |
314 // append about:blank as default url. | |
315 command_line->AppendArg(chrome::kChromeUINewTabURL); | |
316 } | |
317 }; | |
318 | |
319 IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, | |
320 OpenShortcutOnStartup) { | |
321 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
322 EXPECT_EQ(open_shortcut_url, | |
323 browser()->tab_strip_model()->GetActiveWebContents() | |
324 ->GetLastCommittedURL()); | |
325 } | |
326 | |
230 } // namespace | 327 } // namespace |
OLD | NEW |