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

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

Issue 2798143004: Fix for URL opening code (Closed)
Patch Set: Added new files for added tests. 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 prepasre session-to-restore.
Avi (use Gerrit) 2017/06/01 19:54:31 typo: prepare
eugenebng 2017/06/07 10:42:11 Done.
417 std::string test_name =
418 testing::UnitTest::GetInstance()->current_test_info()->name();
419 std::string pre_ptefix = "PRE_";
Avi (use Gerrit) 2017/06/01 19:54:31 typo: prefix
eugenebng 2017/06/07 10:42:11 Done.
420 if (test_name.substr(0, pre_ptefix.length()) != pre_ptefix) {
Avi (use Gerrit) 2017/06/01 19:54:31 I would have thought there's a better way to test,
eugenebng 2017/06/07 10:42:11 Test launcher itself does detect pre-tests by pref
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 recieved during
Avi (use Gerrit) 2017/06/01 19:54:31 typo: received
eugenebng 2017/06/07 10:42:11 Done.
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 o pages startup setting - expect that set of pages.
Avi (use Gerrit) 2017/06/01 19:54:31 typo: of pages
eugenebng 2017/06/07 10:42:11 Done.
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) {
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 recieved by already
Avi (use Gerrit) 2017/06/01 19:54:31 typo: received
eugenebng 2017/06/07 10:42:11 Done.
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 recieved by
Avi (use Gerrit) 2017/06/01 19:54:31 typo: received
eugenebng 2017/06/07 10:42:11 Done.
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