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

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

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

Powered by Google App Engine
This is Rietveld 408576698