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

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

Issue 2798143004: Fix for URL opening code (Closed)
Patch Set: Fixed class name usage. Created 3 years, 6 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"
26 #include "chrome/browser/history/history_service_factory.h" 27 #include "chrome/browser/history/history_service_factory.h"
28 #include "chrome/browser/prefs/session_startup_pref.h"
27 #include "chrome/browser/profiles/profile_attributes_entry.h" 29 #include "chrome/browser/profiles/profile_attributes_entry.h"
28 #include "chrome/browser/profiles/profile_attributes_storage.h" 30 #include "chrome/browser/profiles/profile_attributes_storage.h"
29 #include "chrome/browser/profiles/profile_manager.h" 31 #include "chrome/browser/profiles/profile_manager.h"
30 #include "chrome/browser/ui/browser.h" 32 #include "chrome/browser/ui/browser.h"
31 #include "chrome/browser/ui/browser_list.h" 33 #include "chrome/browser/ui/browser_list.h"
32 #include "chrome/browser/ui/browser_navigator_params.h" 34 #include "chrome/browser/ui/browser_navigator_params.h"
33 #include "chrome/browser/ui/browser_window.h" 35 #include "chrome/browser/ui/browser_window.h"
34 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" 36 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
35 #include "chrome/browser/ui/cocoa/history_menu_bridge.h" 37 #include "chrome/browser/ui/cocoa/history_menu_bridge.h"
36 #include "chrome/browser/ui/cocoa/test/run_loop_testing.h" 38 #include "chrome/browser/ui/cocoa/test/run_loop_testing.h"
37 #include "chrome/browser/ui/tabs/tab_strip_model.h" 39 #include "chrome/browser/ui/tabs/tab_strip_model.h"
38 #include "chrome/browser/ui/user_manager.h" 40 #include "chrome/browser/ui/user_manager.h"
39 #include "chrome/common/chrome_constants.h" 41 #include "chrome/common/chrome_constants.h"
40 #include "chrome/common/chrome_switches.h" 42 #include "chrome/common/chrome_switches.h"
41 #include "chrome/common/pref_names.h" 43 #include "chrome/common/pref_names.h"
42 #include "chrome/common/url_constants.h" 44 #include "chrome/common/url_constants.h"
43 #include "chrome/test/base/in_process_browser_test.h" 45 #include "chrome/test/base/in_process_browser_test.h"
44 #include "chrome/test/base/ui_test_utils.h" 46 #include "chrome/test/base/ui_test_utils.h"
45 #include "components/bookmarks/browser/bookmark_model.h" 47 #include "components/bookmarks/browser/bookmark_model.h"
46 #include "components/bookmarks/test/bookmark_test_helpers.h" 48 #include "components/bookmarks/test/bookmark_test_helpers.h"
47 #include "components/prefs/pref_service.h" 49 #include "components/prefs/pref_service.h"
48 #include "content/public/browser/navigation_controller.h" 50 #include "content/public/browser/navigation_controller.h"
51 #include "content/public/browser/notification_service.h"
49 #include "content/public/browser/web_contents.h" 52 #include "content/public/browser/web_contents.h"
50 #include "content/public/test/browser_test_utils.h" 53 #include "content/public/test/browser_test_utils.h"
54 #include "content/public/test/repeated_notification_observer.h"
51 #include "content/public/test/test_navigation_observer.h" 55 #include "content/public/test/test_navigation_observer.h"
52 #include "extensions/browser/app_window/app_window_registry.h" 56 #include "extensions/browser/app_window/app_window_registry.h"
53 #include "extensions/common/extension.h" 57 #include "extensions/common/extension.h"
54 #include "extensions/test/extension_test_message_listener.h" 58 #include "extensions/test/extension_test_message_listener.h"
55 #include "net/test/embedded_test_server/embedded_test_server.h" 59 #include "net/test/embedded_test_server/embedded_test_server.h"
56 60
57 namespace { 61 namespace {
58 62
59 GURL g_open_shortcut_url = GURL::EmptyGURL(); 63 GURL g_open_shortcut_url = GURL("https://localhost");
60 64
61 // Returns an Apple Event that instructs the application to open |url|. 65 // Returns an Apple Event that instructs the application to open |url|.
62 NSAppleEventDescriptor* AppleEventToOpenUrl(const GURL& url) { 66 NSAppleEventDescriptor* AppleEventToOpenUrl(const GURL& url) {
63 NSAppleEventDescriptor* shortcut_event = [[[NSAppleEventDescriptor alloc] 67 NSAppleEventDescriptor* shortcut_event = [[[NSAppleEventDescriptor alloc]
64 initWithEventClass:kASAppleScriptSuite 68 initWithEventClass:kASAppleScriptSuite
65 eventID:kASSubroutineEvent 69 eventID:kASSubroutineEvent
66 targetDescriptor:nil 70 targetDescriptor:nil
67 returnID:kAutoGenerateReturnID 71 returnID:kAutoGenerateReturnID
68 transactionID:kAnyTransactionID] autorelease]; 72 transactionID:kAnyTransactionID] autorelease];
69 NSString* url_string = [NSString stringWithUTF8String:url.spec().c_str()]; 73 NSString* url_string = [NSString stringWithUTF8String:url.spec().c_str()];
(...skipping 26 matching lines...) Expand all
96 100
97 } // namespace 101 } // namespace
98 102
99 @interface TestOpenShortcutOnStartup : NSObject 103 @interface TestOpenShortcutOnStartup : NSObject
100 - (void)applicationWillFinishLaunching:(NSNotification*)notification; 104 - (void)applicationWillFinishLaunching:(NSNotification*)notification;
101 @end 105 @end
102 106
103 @implementation TestOpenShortcutOnStartup 107 @implementation TestOpenShortcutOnStartup
104 108
105 - (void)applicationWillFinishLaunching:(NSNotification*)notification { 109 - (void)applicationWillFinishLaunching:(NSNotification*)notification {
106 if (!g_open_shortcut_url.is_valid())
107 return;
108
109 SendAppleEventToOpenUrlToAppController(g_open_shortcut_url); 110 SendAppleEventToOpenUrlToAppController(g_open_shortcut_url);
110 } 111 }
111 112
112 @end 113 @end
113 114
114 namespace { 115 namespace {
115 116
116 class AppControllerPlatformAppBrowserTest 117 class AppControllerPlatformAppBrowserTest
117 : public extensions::PlatformAppBrowserTest { 118 : public extensions::PlatformAppBrowserTest {
118 protected: 119 protected:
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 SEL targetMethod = @selector(applicationWillFinishLaunching:); 374 SEL targetMethod = @selector(applicationWillFinishLaunching:);
374 Method original = class_getInstanceMethod(appControllerClass, 375 Method original = class_getInstanceMethod(appControllerClass,
375 targetMethod); 376 targetMethod);
376 Method destination = class_getInstanceMethod(openShortcutClass, 377 Method destination = class_getInstanceMethod(openShortcutClass,
377 targetMethod); 378 targetMethod);
378 379
379 ASSERT_TRUE(original != NULL); 380 ASSERT_TRUE(original != NULL);
380 ASSERT_TRUE(destination != NULL); 381 ASSERT_TRUE(destination != NULL);
381 382
382 method_exchangeImplementations(original, destination); 383 method_exchangeImplementations(original, destination);
383
384 ASSERT_TRUE(embedded_test_server()->Start());
385 g_open_shortcut_url = embedded_test_server()->GetURL("/simple.html");
386 } 384 }
387 385
388 void SetUpCommandLine(base::CommandLine* command_line) override { 386 void SetUpCommandLine(base::CommandLine* command_line) override {
389 // If the arg is empty, PrepareTestCommandLine() after this function will 387 // If the arg is empty, PrepareTestCommandLine() after this function will
390 // append about:blank as default url. 388 // append about:blank as default url.
391 command_line->AppendArg(chrome::kChromeUINewTabURL); 389 command_line->AppendArg(chrome::kChromeUINewTabURL);
392 } 390 }
393 }; 391 };
394 392
395 IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest, 393 IN_PROC_BROWSER_TEST_F(AppControllerOpenShortcutBrowserTest,
396 OpenShortcutOnStartup) { 394 OpenShortcutOnStartup) {
397 // The two tabs expected are the Welcome page and the desired URL. 395 // The two tabs expected are the Welcome page and the desired URL.
398 EXPECT_EQ(2, browser()->tab_strip_model()->count()); 396 EXPECT_EQ(2, browser()->tab_strip_model()->count());
399 EXPECT_EQ(g_open_shortcut_url, 397 EXPECT_EQ(g_open_shortcut_url,
400 browser()->tab_strip_model()->GetActiveWebContents() 398 browser()->tab_strip_model()->GetActiveWebContents()
401 ->GetLastCommittedURL()); 399 ->GetLastCommittedURL());
402 } 400 }
403 401
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
404 class AppControllerReplaceNTPBrowserTest : public InProcessBrowserTest { 602 class AppControllerReplaceNTPBrowserTest : public InProcessBrowserTest {
405 protected: 603 protected:
406 AppControllerReplaceNTPBrowserTest() {} 604 AppControllerReplaceNTPBrowserTest() {}
407 605
408 void SetUpInProcessBrowserTestFixture() override { 606 void SetUpInProcessBrowserTestFixture() override {
409 ASSERT_TRUE(embedded_test_server()->Start()); 607 ASSERT_TRUE(embedded_test_server()->Start());
410 } 608 }
411 609
412 void SetUpCommandLine(base::CommandLine* command_line) override { 610 void SetUpCommandLine(base::CommandLine* command_line) override {
413 // If the arg is empty, PrepareTestCommandLine() after this function will 611 // If the arg is empty, PrepareTestCommandLine() after this function will
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 ui_test_utils::NavigateToURL(browser3, test_url1); 910 ui_test_utils::NavigateToURL(browser3, test_url1);
713 EXPECT_EQ(g_handoff_url, GURL()); 911 EXPECT_EQ(g_handoff_url, GURL());
714 912
715 // Activate the original browser window. 913 // Activate the original browser window.
716 Browser* browser1 = active_browser_list->get(0); 914 Browser* browser1 = active_browser_list->get(0);
717 browser1->window()->Show(); 915 browser1->window()->Show();
718 EXPECT_EQ(g_handoff_url, test_url2); 916 EXPECT_EQ(g_handoff_url, test_url2);
719 } 917 }
720 918
721 } // namespace 919 } // 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