Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: chrome/browser/app_controller_mac_browsertest.mm

Issue 678423003: mac: Opening a URL should replace the NTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix the tests. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/app_controller_mac.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 14 matching lines...) Expand all
25 #include "chrome/browser/ui/tabs/tab_strip_model.h" 25 #include "chrome/browser/ui/tabs/tab_strip_model.h"
26 #include "chrome/browser/ui/user_manager.h" 26 #include "chrome/browser/ui/user_manager.h"
27 #include "chrome/common/chrome_constants.h" 27 #include "chrome/common/chrome_constants.h"
28 #include "chrome/common/chrome_switches.h" 28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
30 #include "chrome/common/url_constants.h" 30 #include "chrome/common/url_constants.h"
31 #include "chrome/test/base/in_process_browser_test.h" 31 #include "chrome/test/base/in_process_browser_test.h"
32 #include "chrome/test/base/ui_test_utils.h" 32 #include "chrome/test/base/ui_test_utils.h"
33 #include "components/signin/core/common/profile_management_switches.h" 33 #include "components/signin/core/common/profile_management_switches.h"
34 #include "content/public/browser/web_contents.h" 34 #include "content/public/browser/web_contents.h"
35 #include "content/public/test/test_navigation_observer.h"
35 #include "extensions/browser/app_window/app_window_registry.h" 36 #include "extensions/browser/app_window/app_window_registry.h"
36 #include "extensions/common/extension.h" 37 #include "extensions/common/extension.h"
37 #include "extensions/test/extension_test_message_listener.h" 38 #include "extensions/test/extension_test_message_listener.h"
38 #include "net/test/embedded_test_server/embedded_test_server.h" 39 #include "net/test/embedded_test_server/embedded_test_server.h"
39 40
40 namespace { 41 namespace {
41 42
42 GURL g_open_shortcut_url = GURL::EmptyGURL(); 43 GURL g_open_shortcut_url = GURL::EmptyGURL();
43 44
45 // Returns an Apple Event that instructs the application to open |url|.
46 NSAppleEventDescriptor* AppleEventToOpenUrl(const GURL& url) {
47 NSAppleEventDescriptor* shortcut_event = [[[NSAppleEventDescriptor alloc]
48 initWithEventClass:kASAppleScriptSuite
49 eventID:kASSubroutineEvent
50 targetDescriptor:nil
51 returnID:kAutoGenerateReturnID
52 transactionID:kAnyTransactionID] autorelease];
53 NSString* url_string = [NSString stringWithUTF8String:url.spec().c_str()];
54 [shortcut_event setParamDescriptor:[NSAppleEventDescriptor
55 descriptorWithString:url_string]
56 forKeyword:keyDirectObject];
57 return shortcut_event;
58 }
59
60 // Instructs the NSApp's delegate to open |url|.
61 void SendAppleEventToOpenUrlToAppController(const GURL& url) {
62 AppController* controller =
63 base::mac::ObjCCast<AppController>([NSApp delegate]);
64 Method get_url =
65 class_getInstanceMethod([controller class], @selector(getUrl:withReply:));
66
67 ASSERT_TRUE(get_url);
68
69 NSAppleEventDescriptor* shortcut_event = AppleEventToOpenUrl(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
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 void SetUpInProcessBrowserTestFixture() override {
347 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
348 }
349
350 void SetUpCommandLine(CommandLine* command_line) override {
351 // If the arg is empty, PrepareTestCommandLine() after this function will
352 // append about:blank as default url.
353 command_line->AppendArg(chrome::kChromeUINewTabURL);
354 }
355 };
356
357 // Tests that when a GURL is opened after startup, it replaces the NTP.
358 IN_PROC_BROWSER_TEST_F(AppControllerReplaceNTPBrowserTest,
359 ReplaceNTPAfterStartup) {
360 // Ensure that there is exactly 1 tab showing, and the tab is the NTP.
361 GURL ntp(chrome::kChromeUINewTabURL);
362 EXPECT_EQ(1, browser()->tab_strip_model()->count());
363 EXPECT_EQ(ntp,
364 browser()
365 ->tab_strip_model()
366 ->GetActiveWebContents()
367 ->GetLastCommittedURL());
368
369 GURL simple(embedded_test_server()->GetURL("/simple.html"));
370 SendAppleEventToOpenUrlToAppController(simple);
371
372 // Wait for one navigation on the active web contents.
373 EXPECT_EQ(1, browser()->tab_strip_model()->count());
374 content::TestNavigationObserver obs(
375 browser()->tab_strip_model()->GetActiveWebContents(), 1);
376 obs.Wait();
377
378 EXPECT_EQ(simple,
379 browser()
380 ->tab_strip_model()
381 ->GetActiveWebContents()
382 ->GetLastCommittedURL());
383 }
384
333 } // namespace 385 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/app_controller_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698