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(); | |
Alexei Svitkine (slow)
2014/04/25 14:47:08
Nit: g_open_shortcut_url
| |
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]; | |
Alexei Svitkine (slow)
2014/04/25 14:47:08
Use ObjCCast() from foundation_util.h instead, whi
| |
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 NSString* url = | |
69 [NSString stringWithUTF8String:open_shortcut_url.spec().c_str()]; | |
70 [shortcutEvent setParamDescriptor: | |
71 [NSAppleEventDescriptor descriptorWithString:url] | |
72 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
| |
73 | |
74 method_invoke(controller, getUrl, shortcutEvent.get(), NULL); | |
75 } | |
76 | |
77 @end | |
78 | |
79 namespace { | |
80 | |
33 class AppControllerPlatformAppBrowserTest | 81 class AppControllerPlatformAppBrowserTest |
34 : public extensions::PlatformAppBrowserTest { | 82 : public extensions::PlatformAppBrowserTest { |
35 protected: | 83 protected: |
36 AppControllerPlatformAppBrowserTest() | 84 AppControllerPlatformAppBrowserTest() |
37 : active_browser_list_(BrowserList::GetInstance( | 85 : active_browser_list_(BrowserList::GetInstance( |
38 chrome::GetActiveDesktop())) { | 86 chrome::GetActiveDesktop())) { |
39 } | 87 } |
40 | 88 |
41 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 89 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
42 PlatformAppBrowserTest::SetUpCommandLine(command_line); | 90 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]; | 268 BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO]; |
221 EXPECT_FALSE(result); | 269 EXPECT_FALSE(result); |
222 | 270 |
223 base::RunLoop().RunUntilIdle(); | 271 base::RunLoop().RunUntilIdle(); |
224 | 272 |
225 EXPECT_EQ(1u, active_browser_list_->size()); | 273 EXPECT_EQ(1u, active_browser_list_->size()); |
226 EXPECT_TRUE(UserManagerMac::IsShowing()); | 274 EXPECT_TRUE(UserManagerMac::IsShowing()); |
227 UserManagerMac::Hide(); | 275 UserManagerMac::Hide(); |
228 } | 276 } |
229 | 277 |
278 class AppControllerOpenShortcutBrowserTest : public InProcessBrowserTest { | |
279 protected: | |
280 AppControllerOpenShortcutBrowserTest() { | |
281 } | |
282 | |
283 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
284 // In order to mimic opening shortcut during browser startup, we need to | |
285 // send the event before -applicationDidFinishLaunching is called, but | |
286 // after AppController is loaded. | |
287 // | |
288 // Since -applicationWillFinishLaunching does nothing now, we swizzle it to | |
289 // our function to send the event. We need to do this early before running | |
290 // the main message loop. | |
291 // | |
292 // NSApp does not exist yet. We need to get the AppController using | |
293 // reflection. | |
294 Class appControllerClass = NSClassFromString(@"AppController"); | |
295 Class openShortcutClass = NSClassFromString(@"TestOpenShortcutOnStartup"); | |
296 | |
297 ASSERT_TRUE(appControllerClass != nil && openShortcutClass != nil); | |
Alexei Svitkine (slow)
2014/04/25 14:47:08
Nit: Can you split these into separate ASSERT line
| |
298 | |
299 SEL targetMethod = @selector(applicationWillFinishLaunching:); | |
300 Method original = class_getInstanceMethod(appControllerClass, | |
301 targetMethod); | |
302 Method destination = class_getInstanceMethod(openShortcutClass, | |
303 targetMethod); | |
304 | |
305 ASSERT_TRUE(original != NULL && destination != NULL); | |
306 | |
307 method_exchangeImplementations(original, destination); | |
308 | |
309 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
310 open_shortcut_url = embedded_test_server()->GetURL("/simple.html"); | |
311 } | |
312 | |
313 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
314 // If the arg is empty, PrepareTestCommandLine() after this function will | |
315 // append about:blank as default url. | |
316 command_line->AppendArg(chrome::kChromeUINewTabURL); | |
317 } | |
318 }; | |
319 | |
320 IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, | |
321 OpenShortcutOnStartup) { | |
322 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
323 EXPECT_EQ(open_shortcut_url, | |
324 browser()->tab_strip_model()->GetActiveWebContents() | |
325 ->GetLastCommittedURL()); | |
326 } | |
327 | |
230 } // namespace | 328 } // namespace |
OLD | NEW |