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

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

Issue 794853004: Reland 1: "mac: Allow Chrome to hand off its active URL to other devices." (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from avi. Created 6 years 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>
(...skipping 17 matching lines...) Expand all
28 #include "chrome/browser/ui/browser_window.h" 28 #include "chrome/browser/ui/browser_window.h"
29 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" 29 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
30 #include "chrome/browser/ui/host_desktop.h" 30 #include "chrome/browser/ui/host_desktop.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h" 31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/browser/ui/user_manager.h" 32 #include "chrome/browser/ui/user_manager.h"
33 #include "chrome/common/chrome_constants.h" 33 #include "chrome/common/chrome_constants.h"
34 #include "chrome/common/chrome_switches.h" 34 #include "chrome/common/chrome_switches.h"
35 #include "chrome/common/pref_names.h" 35 #include "chrome/common/pref_names.h"
36 #include "chrome/common/url_constants.h" 36 #include "chrome/common/url_constants.h"
37 #include "chrome/test/base/in_process_browser_test.h" 37 #include "chrome/test/base/in_process_browser_test.h"
38 #include "chrome/test/base/ui_test_utils.h"
38 #include "components/bookmarks/test/bookmark_test_helpers.h" 39 #include "components/bookmarks/test/bookmark_test_helpers.h"
39 #include "components/signin/core/common/profile_management_switches.h" 40 #include "components/signin/core/common/profile_management_switches.h"
40 #include "content/public/browser/web_contents.h" 41 #include "content/public/browser/web_contents.h"
42 #include "content/public/test/browser_test_utils.h"
41 #include "content/public/test/test_navigation_observer.h" 43 #include "content/public/test/test_navigation_observer.h"
42 #include "extensions/browser/app_window/app_window_registry.h" 44 #include "extensions/browser/app_window/app_window_registry.h"
43 #include "extensions/common/extension.h" 45 #include "extensions/common/extension.h"
44 #include "extensions/test/extension_test_message_listener.h" 46 #include "extensions/test/extension_test_message_listener.h"
45 #include "net/test/embedded_test_server/embedded_test_server.h" 47 #include "net/test/embedded_test_server/embedded_test_server.h"
46 48
47 namespace { 49 namespace {
48 50
49 GURL g_open_shortcut_url = GURL::EmptyGURL(); 51 GURL g_open_shortcut_url = GURL::EmptyGURL();
50 52
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 [ac windowChangedToProfile:profile1]; 476 [ac windowChangedToProfile:profile1];
475 477
476 // Test that only bookmark 1 is shown in the restored menu. 478 // Test that only bookmark 1 is shown in the restored menu.
477 EXPECT_TRUE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle: 479 EXPECT_TRUE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle:
478 SysUTF16ToNSString(title1)]); 480 SysUTF16ToNSString(title1)]);
479 EXPECT_FALSE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle: 481 EXPECT_FALSE([[ac bookmarkMenuBridge]->BookmarkMenu() itemWithTitle:
480 SysUTF16ToNSString(title2)]); 482 SysUTF16ToNSString(title2)]);
481 } 483 }
482 484
483 } // namespace 485 } // namespace
486
487 //--------------------------AppControllerHandoffBrowserTest---------------------
488
489 static GURL g_handoff_url;
490
491 @interface AppController (BrowserTest)
492 - (BOOL)new_shouldUseHandoff;
493 - (void)new_passURLToHandoffManager:(const GURL&)handoffURL;
494 @end
495
496 @implementation AppController (BrowserTest)
497 - (BOOL)new_shouldUseHandoff {
498 return YES;
499 }
500
501 - (void)new_passURLToHandoffManager:(const GURL&)handoffURL {
502 g_handoff_url = handoffURL;
503 }
504 @end
505
506 namespace {
507
508 class AppControllerHandoffBrowserTest : public InProcessBrowserTest {
509 protected:
510 AppControllerHandoffBrowserTest() {}
511
512 // Exchanges the implementations of the two selectors on the class
513 // AppController.
514 void ExchangeSelectors(SEL originalMethod, SEL newMethod) {
515 Class appControllerClass = NSClassFromString(@"AppController");
516
517 ASSERT_TRUE(appControllerClass != nil);
518
519 Method original =
520 class_getInstanceMethod(appControllerClass, originalMethod);
521 Method destination = class_getInstanceMethod(appControllerClass, newMethod);
522
523 ASSERT_TRUE(original != NULL);
524 ASSERT_TRUE(destination != NULL);
525
526 method_exchangeImplementations(original, destination);
527 }
528
529 // Swizzle Handoff related implementations.
530 void SetUpInProcessBrowserTestFixture() override {
531 // Handoff is only available on OSX 10.10+. This swizzle makes the logic
532 // run on all OSX versions.
533 SEL originalMethod = @selector(shouldUseHandoff);
534 SEL newMethod = @selector(new_shouldUseHandoff);
535 ExchangeSelectors(originalMethod, newMethod);
536
537 // This swizzle intercepts the URL that would be sent to the Handoff
538 // Manager, and instead puts it into a variable accessible to this test.
539 originalMethod = @selector(passURLToHandoffManager:);
540 newMethod = @selector(new_passURLToHandoffManager:);
541 ExchangeSelectors(originalMethod, newMethod);
542 }
543
544 // Closes the tab, and waits for the close to finish.
545 void CloseTab(Browser* browser, int index) {
546 content::WebContentsDestroyedWatcher destroyed_watcher(
547 browser->tab_strip_model()->GetWebContentsAt(index));
548 browser->tab_strip_model()->CloseWebContentsAt(
549 index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
550 destroyed_watcher.Wait();
551 }
552 };
553
554 // Tests that as a user switches between tabs, navigates within a tab, and
555 // switches between browser windows, the correct URL is being passed to the
556 // Handoff.
557 IN_PROC_BROWSER_TEST_F(AppControllerHandoffBrowserTest, TestHandoffURLs) {
558 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
559 EXPECT_EQ(g_handoff_url, GURL(url::kAboutBlankURL));
560
561 // Test that navigating to a URL updates the handoff URL.
562 GURL test_url1 = embedded_test_server()->GetURL("/title1.html");
563 ui_test_utils::NavigateToURL(browser(), test_url1);
564 EXPECT_EQ(g_handoff_url, test_url1);
565
566 // Test that opening a new tab updates the handoff URL.
567 GURL test_url2 = embedded_test_server()->GetURL("/title2.html");
568 chrome::NavigateParams params(browser(), test_url2, ui::PAGE_TRANSITION_LINK);
569 params.disposition = NEW_FOREGROUND_TAB;
570 ui_test_utils::NavigateToURL(&params);
571 EXPECT_EQ(g_handoff_url, test_url2);
572
573 // Test that switching tabs updates the handoff URL.
574 browser()->tab_strip_model()->ActivateTabAt(0, true);
575 EXPECT_EQ(g_handoff_url, test_url1);
576
577 // Test that closing the current tab updates the handoff URL.
578 CloseTab(browser(), 0);
579 EXPECT_EQ(g_handoff_url, test_url2);
580
581 // Test that opening a new browser window updates the handoff URL.
582 GURL test_url3 = embedded_test_server()->GetURL("/title3.html");
583 ui_test_utils::NavigateToURLWithDisposition(
584 browser(), GURL(test_url3), NEW_WINDOW,
585 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
586 EXPECT_EQ(g_handoff_url, test_url3);
587
588 // Check that there are exactly 2 browsers.
589 BrowserList* active_browser_list =
590 BrowserList::GetInstance(chrome::GetActiveDesktop());
591 EXPECT_EQ(2u, active_browser_list->size());
592
593 // Close the one and only tab for the second browser window.
594 Browser* browser2 = active_browser_list->get(1);
595 CloseTab(browser2, 0);
596 base::RunLoop().RunUntilIdle();
597 EXPECT_EQ(g_handoff_url, test_url2);
598
599 // The URLs of incognito windows should not be passed to Handoff.
600 GURL test_url4 = embedded_test_server()->GetURL("/simple.html");
601 ui_test_utils::NavigateToURLWithDisposition(
602 browser(), GURL(test_url4), OFF_THE_RECORD,
603 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
604 EXPECT_EQ(g_handoff_url, GURL());
605
606 // Open a new tab in the incognito window.
607 EXPECT_EQ(2u, active_browser_list->size());
608 Browser* browser3 = active_browser_list->get(1);
609 ui_test_utils::NavigateToURLWithDisposition(
610 browser3, test_url4, NEW_FOREGROUND_TAB,
611 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
612 EXPECT_EQ(g_handoff_url, GURL());
613
614 // Navigate the current tab in the incognito window.
615 ui_test_utils::NavigateToURLWithDisposition(
616 browser3, test_url1, CURRENT_TAB,
617 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
618 EXPECT_EQ(g_handoff_url, GURL());
619
620 // Activate the original browser window.
621 Browser* browser1 = active_browser_list->get(0);
622 browser1->window()->Show();
623 EXPECT_EQ(g_handoff_url, test_url2);
624 }
625
626 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/app_controller_mac.mm ('k') | chrome/browser/ui/cocoa/handoff_active_url_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698