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

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

Issue 2964163002: Revert of Fix for URL opening code (Closed)
Patch Set: Created 3 years, 5 months 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
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>
11 #include <stddef.h> 11 #include <stddef.h>
12 12
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/mac/foundation_util.h" 14 #include "base/mac/foundation_util.h"
15 #include "base/mac/scoped_nsobject.h" 15 #include "base/mac/scoped_nsobject.h"
16 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
17 #include "base/run_loop.h" 17 #include "base/run_loop.h"
18 #include "base/strings/sys_string_conversions.h" 18 #include "base/strings/sys_string_conversions.h"
19 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
20 #include "base/threading/thread_restrictions.h" 20 #include "base/threading/thread_restrictions.h"
21 #include "chrome/app/chrome_command_ids.h" 21 #include "chrome/app/chrome_command_ids.h"
22 #import "chrome/browser/app_controller_mac.h" 22 #import "chrome/browser/app_controller_mac.h"
23 #include "chrome/browser/apps/app_browsertest_util.h" 23 #include "chrome/browser/apps/app_browsertest_util.h"
24 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 24 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
25 #include "chrome/browser/browser_process.h" 25 #include "chrome/browser/browser_process.h"
26 #include "chrome/browser/chrome_notification_types.h"
27 #include "chrome/browser/history/history_service_factory.h" 26 #include "chrome/browser/history/history_service_factory.h"
28 #include "chrome/browser/prefs/session_startup_pref.h"
29 #include "chrome/browser/profiles/profile_attributes_entry.h" 27 #include "chrome/browser/profiles/profile_attributes_entry.h"
30 #include "chrome/browser/profiles/profile_attributes_storage.h" 28 #include "chrome/browser/profiles/profile_attributes_storage.h"
31 #include "chrome/browser/profiles/profile_manager.h" 29 #include "chrome/browser/profiles/profile_manager.h"
32 #include "chrome/browser/ui/browser.h" 30 #include "chrome/browser/ui/browser.h"
33 #include "chrome/browser/ui/browser_list.h" 31 #include "chrome/browser/ui/browser_list.h"
34 #include "chrome/browser/ui/browser_navigator_params.h" 32 #include "chrome/browser/ui/browser_navigator_params.h"
35 #include "chrome/browser/ui/browser_window.h" 33 #include "chrome/browser/ui/browser_window.h"
36 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" 34 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
37 #include "chrome/browser/ui/cocoa/history_menu_bridge.h" 35 #include "chrome/browser/ui/cocoa/history_menu_bridge.h"
38 #include "chrome/browser/ui/cocoa/test/run_loop_testing.h" 36 #include "chrome/browser/ui/cocoa/test/run_loop_testing.h"
39 #include "chrome/browser/ui/tabs/tab_strip_model.h" 37 #include "chrome/browser/ui/tabs/tab_strip_model.h"
40 #include "chrome/browser/ui/user_manager.h" 38 #include "chrome/browser/ui/user_manager.h"
41 #include "chrome/common/chrome_constants.h" 39 #include "chrome/common/chrome_constants.h"
42 #include "chrome/common/chrome_switches.h" 40 #include "chrome/common/chrome_switches.h"
43 #include "chrome/common/pref_names.h" 41 #include "chrome/common/pref_names.h"
44 #include "chrome/common/url_constants.h" 42 #include "chrome/common/url_constants.h"
45 #include "chrome/test/base/in_process_browser_test.h" 43 #include "chrome/test/base/in_process_browser_test.h"
46 #include "chrome/test/base/ui_test_utils.h" 44 #include "chrome/test/base/ui_test_utils.h"
47 #include "components/bookmarks/browser/bookmark_model.h" 45 #include "components/bookmarks/browser/bookmark_model.h"
48 #include "components/bookmarks/test/bookmark_test_helpers.h" 46 #include "components/bookmarks/test/bookmark_test_helpers.h"
49 #include "components/prefs/pref_service.h" 47 #include "components/prefs/pref_service.h"
50 #include "content/public/browser/navigation_controller.h" 48 #include "content/public/browser/navigation_controller.h"
51 #include "content/public/browser/notification_service.h"
52 #include "content/public/browser/web_contents.h" 49 #include "content/public/browser/web_contents.h"
53 #include "content/public/test/browser_test_utils.h" 50 #include "content/public/test/browser_test_utils.h"
54 #include "content/public/test/repeated_notification_observer.h"
55 #include "content/public/test/test_navigation_observer.h" 51 #include "content/public/test/test_navigation_observer.h"
56 #include "extensions/browser/app_window/app_window_registry.h" 52 #include "extensions/browser/app_window/app_window_registry.h"
57 #include "extensions/common/extension.h" 53 #include "extensions/common/extension.h"
58 #include "extensions/test/extension_test_message_listener.h" 54 #include "extensions/test/extension_test_message_listener.h"
59 #include "net/test/embedded_test_server/embedded_test_server.h" 55 #include "net/test/embedded_test_server/embedded_test_server.h"
60 56
61 namespace { 57 namespace {
62 58
63 GURL g_open_shortcut_url = GURL("https://localhost"); 59 GURL g_open_shortcut_url = GURL::EmptyGURL();
64 60
65 // Returns an Apple Event that instructs the application to open |url|. 61 // Returns an Apple Event that instructs the application to open |url|.
66 NSAppleEventDescriptor* AppleEventToOpenUrl(const GURL& url) { 62 NSAppleEventDescriptor* AppleEventToOpenUrl(const GURL& url) {
67 NSAppleEventDescriptor* shortcut_event = [[[NSAppleEventDescriptor alloc] 63 NSAppleEventDescriptor* shortcut_event = [[[NSAppleEventDescriptor alloc]
68 initWithEventClass:kASAppleScriptSuite 64 initWithEventClass:kASAppleScriptSuite
69 eventID:kASSubroutineEvent 65 eventID:kASSubroutineEvent
70 targetDescriptor:nil 66 targetDescriptor:nil
71 returnID:kAutoGenerateReturnID 67 returnID:kAutoGenerateReturnID
72 transactionID:kAnyTransactionID] autorelease]; 68 transactionID:kAnyTransactionID] autorelease];
73 NSString* url_string = [NSString stringWithUTF8String:url.spec().c_str()]; 69 NSString* url_string = [NSString stringWithUTF8String:url.spec().c_str()];
(...skipping 26 matching lines...) Expand all
100 96
101 } // namespace 97 } // namespace
102 98
103 @interface TestOpenShortcutOnStartup : NSObject 99 @interface TestOpenShortcutOnStartup : NSObject
104 - (void)applicationWillFinishLaunching:(NSNotification*)notification; 100 - (void)applicationWillFinishLaunching:(NSNotification*)notification;
105 @end 101 @end
106 102
107 @implementation TestOpenShortcutOnStartup 103 @implementation TestOpenShortcutOnStartup
108 104
109 - (void)applicationWillFinishLaunching:(NSNotification*)notification { 105 - (void)applicationWillFinishLaunching:(NSNotification*)notification {
106 if (!g_open_shortcut_url.is_valid())
107 return;
108
110 SendAppleEventToOpenUrlToAppController(g_open_shortcut_url); 109 SendAppleEventToOpenUrlToAppController(g_open_shortcut_url);
111 } 110 }
112 111
113 @end 112 @end
114 113
115 namespace { 114 namespace {
116 115
117 class AppControllerPlatformAppBrowserTest 116 class AppControllerPlatformAppBrowserTest
118 : public extensions::PlatformAppBrowserTest { 117 : public extensions::PlatformAppBrowserTest {
119 protected: 118 protected:
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 SEL targetMethod = @selector(applicationWillFinishLaunching:); 373 SEL targetMethod = @selector(applicationWillFinishLaunching:);
375 Method original = class_getInstanceMethod(appControllerClass, 374 Method original = class_getInstanceMethod(appControllerClass,
376 targetMethod); 375 targetMethod);
377 Method destination = class_getInstanceMethod(openShortcutClass, 376 Method destination = class_getInstanceMethod(openShortcutClass,
378 targetMethod); 377 targetMethod);
379 378
380 ASSERT_TRUE(original != NULL); 379 ASSERT_TRUE(original != NULL);
381 ASSERT_TRUE(destination != NULL); 380 ASSERT_TRUE(destination != NULL);
382 381
383 method_exchangeImplementations(original, destination); 382 method_exchangeImplementations(original, destination);
383
384 ASSERT_TRUE(embedded_test_server()->Start());
385 g_open_shortcut_url = embedded_test_server()->GetURL("/simple.html");
384 } 386 }
385 387
386 void SetUpCommandLine(base::CommandLine* command_line) override { 388 void SetUpCommandLine(base::CommandLine* command_line) override {
387 // If the arg is empty, PrepareTestCommandLine() after this function will 389 // If the arg is empty, PrepareTestCommandLine() after this function will
388 // append about:blank as default url. 390 // append about:blank as default url.
389 command_line->AppendArg(chrome::kChromeUINewTabURL); 391 command_line->AppendArg(chrome::kChromeUINewTabURL);
390 } 392 }
391 }; 393 };
392 394
393 IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, 395 IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest,
394 OpenShortcutOnStartup) { 396 OpenShortcutOnStartup) {
395 // The two tabs expected are the Welcome page and the desired URL. 397 // The two tabs expected are the Welcome page and the desired URL.
396 EXPECT_EQ(2, browser()->tab_strip_model()->count()); 398 EXPECT_EQ(2, browser()->tab_strip_model()->count());
397 EXPECT_EQ(g_open_shortcut_url, 399 EXPECT_EQ(g_open_shortcut_url,
398 browser()->tab_strip_model()->GetActiveWebContents() 400 browser()->tab_strip_model()->GetActiveWebContents()
399 ->GetLastCommittedURL()); 401 ->GetLastCommittedURL());
400 } 402 }
401 403
402 const char kSessionURL[] = "https://example.com/session.html";
403 const char kPresetURL[] = "https://example.com/preset.html";
404 const char kUrlToOpen[] = "https://example.com/shortcut.html";
405
406 class AppControllerOpenShortcutOnStartupBrowserTest
407 : public AppControllerOpenShortcutBrowserTest,
408 public testing::WithParamInterface<SessionStartupPref::Type> {
409 public:
410 AppControllerOpenShortcutOnStartupBrowserTest()
411 : session_startup_pref_(GetParam()) {}
412 virtual ~AppControllerOpenShortcutOnStartupBrowserTest() {}
413
414 void SetUpInProcessBrowserTestFixture() override {
415 // Don't open URL via AppControllerOpenShortcutBrowserTest in PRE test,
416 // PRE test should only prepare session-to-restore.
417 if (!base::StartsWith(
418 testing::UnitTest::GetInstance()->current_test_info()->name(),
419 "PRE_", base::CompareCase::SENSITIVE)) {
420 AppControllerOpenShortcutBrowserTest::SetUpInProcessBrowserTestFixture();
421 }
422 }
423
424 void SetUpOnMainThread() override {
425 ASSERT_TRUE(embedded_test_server()->Start());
426 SessionStartupPref pref(GetParam());
427 pref.urls.push_back(GURL(kPresetURL));
428 SessionStartupPref::SetStartupPref(browser()->profile(), pref);
429 InProcessBrowserTest::SetUpOnMainThread();
430 }
431
432 protected:
433 void SetUpCommandLine(base::CommandLine* command_line) override {
434 set_open_about_blank_on_browser_launch(false);
435 // Skip AppControllerOpenShortcutBrowserTest::SetUpCommandLine, which adds
436 // startup URL.
437 InProcessBrowserTest::SetUpCommandLine(command_line);
438 }
439
440 SessionStartupPref::Type GetSessionStartupPref() {
441 return session_startup_pref_;
442 }
443
444 private:
445 SessionStartupPref::Type session_startup_pref_;
446
447 DISALLOW_COPY_AND_ASSIGN(AppControllerOpenShortcutOnStartupBrowserTest);
448 };
449
450 IN_PROC_BROWSER_TEST_P(AppControllerOpenShortcutOnStartupBrowserTest,
451 PRE_OpenURL) {
452 // Prepare a session to restore in main test body.
453 ui_test_utils::NavigateToURL(browser(), GURL(kSessionURL));
454 ASSERT_EQ(1, browser()->tab_strip_model()->count());
455 }
456
457 // This test checks that when AppleEvent on opening URL is received during
458 // browser starup, that URL is opened and located properly alongside restored
459 // session or startup URLs.
460 // Emulates opening local file or URL from other application when chromium
461 // application is not started.
462 IN_PROC_BROWSER_TEST_P(AppControllerOpenShortcutOnStartupBrowserTest, OpenURL) {
463 TabStripModel* tab_strip = browser()->tab_strip_model();
464 if (GetSessionStartupPref() == SessionStartupPref::DEFAULT) {
465 // Open NTP startup setting - tab opened via shortcut should replace NTP.
466 EXPECT_EQ(1, tab_strip->count());
467 } else if (GetSessionStartupPref() == SessionStartupPref::LAST) {
468 // Restore sesion on startup - expect tab of that session.
469 EXPECT_EQ(2, tab_strip->count());
470 EXPECT_EQ(kSessionURL, tab_strip->GetWebContentsAt(0)->GetURL().spec());
471 } else if (GetSessionStartupPref() == SessionStartupPref::URLS) {
472 // Open specific set of pages startup setting - expect that set of pages.
473 EXPECT_EQ(2, tab_strip->count());
474 EXPECT_EQ(kPresetURL, tab_strip->GetWebContentsAt(0)->GetURL().spec());
475 } else {
476 NOTREACHED() << "Unknown session startup pref, not covered by test.";
477 }
478 // Tab opened via shortcut should be the last tab and shold be active.
479 EXPECT_EQ(
480 g_open_shortcut_url.spec(),
481 tab_strip->GetWebContentsAt(tab_strip->count() - 1)->GetURL().spec());
482 EXPECT_EQ(tab_strip->count() - 1, tab_strip->active_index());
483 }
484
485 INSTANTIATE_TEST_CASE_P(AppControllerOpenShortcutOnStartupPrefsAny,
486 AppControllerOpenShortcutOnStartupBrowserTest,
487 testing::Values(SessionStartupPref::DEFAULT,
488 SessionStartupPref::LAST,
489 SessionStartupPref::URLS));
490
491 class AppControllerOpenShortcutInBrowserTest
492 : public InProcessBrowserTest,
493 public testing::WithParamInterface<SessionStartupPref::Type> {
494 public:
495 AppControllerOpenShortcutInBrowserTest()
496 : session_startup_pref_(GetParam()) {}
497
498 protected:
499 SessionStartupPref::Type GetSessionStartupPref() {
500 return session_startup_pref_;
501 }
502
503 void SetUpOnMainThread() override {
504 SessionStartupPref pref(session_startup_pref_);
505 pref.urls.push_back(GURL(kPresetURL));
506 SessionStartupPref::SetStartupPref(browser()->profile(), pref);
507 InProcessBrowserTest::SetUpOnMainThread();
508 }
509
510 void SetUpCommandLine(base::CommandLine* command_line) override {
511 set_open_about_blank_on_browser_launch(false);
512 }
513
514 private:
515 SessionStartupPref::Type session_startup_pref_;
516
517 DISALLOW_COPY_AND_ASSIGN(AppControllerOpenShortcutInBrowserTest);
518 };
519
520 IN_PROC_BROWSER_TEST_P(AppControllerOpenShortcutInBrowserTest,
521 PRE_OpenShortcutInBrowserWithWindow) {
522 // Prepare a session to restore in main test body.
523 ui_test_utils::NavigateToURL(browser(), GURL(kSessionURL));
524 ASSERT_EQ(1, browser()->tab_strip_model()->count());
525 }
526
527 // This test checks that when AppleEvent on opening URL is received by already
528 // started browser with window, that URL opens in a tab next to existing tabs.
529 // This needs to be TEST_P (with SessionStartupPref::Type) parameter to
530 // ensure that opening URL logic will not by mistake restore sesiion or open
531 // startup URLs.
532 IN_PROC_BROWSER_TEST_P(AppControllerOpenShortcutInBrowserTest,
533 OpenShortcutInBrowserWithWindow) {
534 ui_test_utils::NavigateToURL(browser(), GURL(kSessionURL));
535 ASSERT_EQ(1, browser()->tab_strip_model()->count());
536 SendAppleEventToOpenUrlToAppController(GURL(kUrlToOpen));
537 content::TestNavigationObserver event_navigation_observer(
538 browser()->tab_strip_model()->GetActiveWebContents());
539 event_navigation_observer.Wait();
540
541 ASSERT_EQ(2, browser()->tab_strip_model()->count());
542 TabStripModel* tab_strip = browser()->tab_strip_model();
543 EXPECT_EQ(kUrlToOpen, tab_strip->GetWebContentsAt(1)->GetURL().spec());
544 EXPECT_EQ(1, tab_strip->active_index());
545 }
546
547 IN_PROC_BROWSER_TEST_P(AppControllerOpenShortcutInBrowserTest,
548 PRE_OpenShortcutInBrowserWithoutWindow) {
549 // Prepare a session to restore in main test body.
550 ui_test_utils::NavigateToURL(browser(), GURL(kSessionURL));
551 ASSERT_EQ(1, browser()->tab_strip_model()->count());
552 }
553
554 // This test checks that when AppleEvent on opening URL is received by
555 // chromium application withot windows, that URL is opened, alongside with
556 // restored session if corresponding setting is enabled.
557 IN_PROC_BROWSER_TEST_P(AppControllerOpenShortcutInBrowserTest,
558 OpenShortcutInBrowserWithoutWindow) {
559 ui_test_utils::NavigateToURL(browser(), GURL(kSessionURL));
560 content::RepeatedNotificationObserver close_observer(
561 chrome::NOTIFICATION_BROWSER_CLOSED, 1);
562 browser()->window()->Close();
563 close_observer.Wait();
564
565 content::RepeatedNotificationObserver open_observer(
566 chrome::NOTIFICATION_TAB_ADDED, 1);
567 SendAppleEventToOpenUrlToAppController(GURL(kUrlToOpen));
568
569 open_observer.Wait();
570
571 Browser* browser = BrowserList::GetInstance()->GetLastActive();
572 TabStripModel* tab_strip = browser->tab_strip_model();
573
574 if (GetSessionStartupPref() == SessionStartupPref::DEFAULT) {
575 // Open NTP startup setting - tab opened via shortcut should replace NTP.
576 ASSERT_EQ(1, tab_strip->count());
577 } else if (GetSessionStartupPref() == SessionStartupPref::LAST) {
578 // Restore sesion on startup - expect tab of that session.
579 ASSERT_EQ(2, tab_strip->count());
580 EXPECT_EQ(kSessionURL, tab_strip->GetWebContentsAt(0)->GetURL().spec());
581 } else if (GetSessionStartupPref() == SessionStartupPref::URLS) {
582 // Open specific set of pages startup setting - when Chromium application
583 // is already started, do NOT expect these pages to be opened.
584 ASSERT_EQ(1, tab_strip->count());
585 } else {
586 NOTREACHED() << "Unknown session startup pref, not covered by test.";
587 }
588
589 // Tab opened via shortcut should be the last tab and shold be active.
590 EXPECT_EQ(
591 kUrlToOpen,
592 tab_strip->GetWebContentsAt(tab_strip->count() - 1)->GetURL().spec());
593 EXPECT_EQ(tab_strip->count() - 1, tab_strip->active_index());
594 }
595
596 INSTANTIATE_TEST_CASE_P(AppControllerOpenShortcutInBrowserPrefsAny,
597 AppControllerOpenShortcutInBrowserTest,
598 testing::Values(SessionStartupPref::DEFAULT,
599 SessionStartupPref::LAST,
600 SessionStartupPref::URLS));
601
602 class AppControllerReplaceNTPBrowserTest : public InProcessBrowserTest { 404 class AppControllerReplaceNTPBrowserTest : public InProcessBrowserTest {
603 protected: 405 protected:
604 AppControllerReplaceNTPBrowserTest() {} 406 AppControllerReplaceNTPBrowserTest() {}
605 407
606 void SetUpInProcessBrowserTestFixture() override { 408 void SetUpInProcessBrowserTestFixture() override {
607 ASSERT_TRUE(embedded_test_server()->Start()); 409 ASSERT_TRUE(embedded_test_server()->Start());
608 } 410 }
609 411
610 void SetUpCommandLine(base::CommandLine* command_line) override { 412 void SetUpCommandLine(base::CommandLine* command_line) override {
611 // If the arg is empty, PrepareTestCommandLine() after this function will 413 // If the arg is empty, PrepareTestCommandLine() after this function will
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 ui_test_utils::NavigateToURL(browser3, test_url1); 712 ui_test_utils::NavigateToURL(browser3, test_url1);
911 EXPECT_EQ(g_handoff_url, GURL()); 713 EXPECT_EQ(g_handoff_url, GURL());
912 714
913 // Activate the original browser window. 715 // Activate the original browser window.
914 Browser* browser1 = active_browser_list->get(0); 716 Browser* browser1 = active_browser_list->get(0);
915 browser1->window()->Show(); 717 browser1->window()->Show();
916 EXPECT_EQ(g_handoff_url, test_url2); 718 EXPECT_EQ(g_handoff_url, test_url2);
917 } 719 }
918 720
919 } // namespace 721 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/app_controller_mac.mm ('k') | chrome/browser/lifetime/browser_close_manager_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698