Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 | 5 |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/extensions/api/tabs/tabs_api.h" | 14 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
| 15 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 15 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 16 #include "chrome/browser/extensions/extension_function_test_utils.h" | 16 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 17 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
| 18 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 18 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_commands.h" | 21 #include "chrome/browser/ui/browser_commands.h" |
| 22 #include "chrome/browser/ui/browser_window.h" | 22 #include "chrome/browser/ui/browser_window.h" |
| 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 23 #include "chrome/test/base/in_process_browser_test.h" | 24 #include "chrome/test/base/in_process_browser_test.h" |
| 24 #include "chrome/test/base/ui_test_utils.h" | 25 #include "chrome/test/base/ui_test_utils.h" |
| 26 #include "content/public/common/page_zoom.h" | |
| 25 #include "content/public/common/url_constants.h" | 27 #include "content/public/common/url_constants.h" |
| 26 #include "ui/gfx/rect.h" | 28 #include "ui/gfx/rect.h" |
| 27 | 29 |
| 28 namespace extensions { | 30 namespace extensions { |
| 29 | 31 |
| 30 namespace keys = tabs_constants; | 32 namespace keys = tabs_constants; |
| 31 namespace utils = extension_function_test_utils; | 33 namespace utils = extension_function_test_utils; |
| 32 | 34 |
| 33 namespace { | 35 namespace { |
| 34 | 36 |
| (...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 584 EXPECT_EQ(base::Value::TYPE_DICTIONARY, duplicate_result->GetType()); | 586 EXPECT_EQ(base::Value::TYPE_DICTIONARY, duplicate_result->GetType()); |
| 585 // Duplicate tab id should be different from the original tab id. | 587 // Duplicate tab id should be different from the original tab id. |
| 586 EXPECT_NE(tab_id, duplicate_tab_id); | 588 EXPECT_NE(tab_id, duplicate_tab_id); |
| 587 EXPECT_EQ(window_id, duplicate_tab_window_id); | 589 EXPECT_EQ(window_id, duplicate_tab_window_id); |
| 588 EXPECT_EQ(tab_index + 1, duplicate_tab_index); | 590 EXPECT_EQ(tab_index + 1, duplicate_tab_index); |
| 589 // The test empty extension has no permissions, therefore |duplicate_result| | 591 // The test empty extension has no permissions, therefore |duplicate_result| |
| 590 // should not contain url, title, and faviconUrl in the function result. | 592 // should not contain url, title, and faviconUrl in the function result. |
| 591 EXPECT_FALSE(utils::HasPrivacySensitiveFields(duplicate_result.get())); | 593 EXPECT_FALSE(utils::HasPrivacySensitiveFields(duplicate_result.get())); |
| 592 } | 594 } |
| 593 | 595 |
| 596 // Tester class for the tabs.zoom* api functions. | |
| 597 class ExtensionTabsZoomTest : public ExtensionTabsTest { | |
| 598 public: | |
| 599 virtual void SetUpOnMainThread() OVERRIDE; | |
| 600 | |
| 601 // Runs chrome.tabs.setZoom(). | |
| 602 bool RunSetZoom(int tab_id, double zoom_factor); | |
| 603 | |
| 604 // Runs chrome.tabs.getZoom(). | |
| 605 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor); | |
| 606 | |
| 607 // Runs chrome.tabs.setZoomSettings(). | |
| 608 bool RunSetZoomSettings(int tab_id, const char* mode, const char* scope); | |
| 609 | |
| 610 // Runs chrome.tabs.getZoomSettings(). | |
| 611 testing::AssertionResult RunGetZoomSettings(int tab_id, | |
| 612 std::string* mode, | |
| 613 std::string* scope); | |
| 614 | |
| 615 // Runs chrome.tabs.setZoom(), expecting an error. | |
| 616 std::string RunSetZoomExpectError(int tab_id, | |
| 617 double zoom_factor); | |
| 618 | |
| 619 // Runs chrome.tabs.setZoomSettings(), expecting an error. | |
| 620 std::string RunSetZoomSettingsExpectError(int tab_id, | |
| 621 const char* mode, | |
| 622 const char* scope); | |
| 623 | |
| 624 content::OpenURLParams GetOpenParams(const char* url) const; | |
| 625 content::WebContents* OpenUrlAndWaitForLoad(const GURL& url); | |
| 626 | |
| 627 private: | |
| 628 scoped_refptr<Extension> extension_; | |
| 629 }; | |
| 630 | |
| 631 void ExtensionTabsZoomTest::SetUpOnMainThread() { | |
| 632 ExtensionTabsTest::SetUpOnMainThread(); | |
| 633 extension_ = utils::CreateEmptyExtension(); | |
| 634 } | |
| 635 | |
| 636 bool ExtensionTabsZoomTest::RunSetZoom(int tab_id, double zoom_factor) { | |
| 637 scoped_refptr<TabsSetZoomFunction> set_zoom_function( | |
| 638 new TabsSetZoomFunction()); | |
| 639 set_zoom_function->set_extension(extension_); | |
| 640 set_zoom_function->set_has_callback(true); | |
| 641 | |
| 642 return utils::RunFunction( | |
| 643 set_zoom_function.get(), | |
| 644 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), | |
| 645 browser(), | |
| 646 extension_function_test_utils::NONE); | |
| 647 } | |
| 648 | |
| 649 testing::AssertionResult ExtensionTabsZoomTest::RunGetZoom( | |
| 650 int tab_id, | |
| 651 double* zoom_factor) { | |
| 652 scoped_refptr<TabsGetZoomFunction> get_zoom_function( | |
| 653 new TabsGetZoomFunction()); | |
| 654 get_zoom_function->set_extension(extension_); | |
| 655 get_zoom_function->set_has_callback(true); | |
| 656 | |
| 657 scoped_ptr<base::Value> get_zoom_result( | |
| 658 utils::RunFunctionAndReturnSingleResult( | |
| 659 get_zoom_function.get(), | |
| 660 base::StringPrintf("[%u]", tab_id), | |
| 661 browser())); | |
| 662 | |
| 663 if (!get_zoom_result.get()) | |
| 664 return testing::AssertionFailure() << "no result"; | |
| 665 if (!get_zoom_result->GetAsDouble(zoom_factor)) | |
| 666 return testing::AssertionFailure() << "result was not a double"; | |
| 667 | |
| 668 return testing::AssertionSuccess(); | |
| 669 } | |
| 670 | |
| 671 bool ExtensionTabsZoomTest::RunSetZoomSettings(int tab_id, | |
| 672 const char* mode, | |
| 673 const char* scope) { | |
| 674 scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( | |
| 675 new TabsSetZoomSettingsFunction()); | |
| 676 set_zoom_settings_function->set_extension(extension_); | |
| 677 | |
| 678 std::string args; | |
| 679 if (scope) { | |
| 680 args = base::StringPrintf("[%u, {\"mode\": \"%s\", \"scope\": \"%s\"}]", | |
| 681 tab_id, mode, scope); | |
| 682 } else { | |
| 683 args = base::StringPrintf("[%u, {\"mode\": \"%s\"}]", tab_id, mode); | |
| 684 } | |
| 685 | |
| 686 return utils::RunFunction(set_zoom_settings_function.get(), | |
| 687 args, | |
| 688 browser(), | |
| 689 extension_function_test_utils::NONE); | |
| 690 } | |
| 691 | |
| 692 testing::AssertionResult ExtensionTabsZoomTest::RunGetZoomSettings( | |
| 693 int tab_id, | |
| 694 std::string* mode, | |
| 695 std::string* scope) { | |
| 696 DCHECK(mode); | |
| 697 DCHECK(scope); | |
| 698 scoped_refptr<TabsGetZoomSettingsFunction> get_zoom_settings_function( | |
| 699 new TabsGetZoomSettingsFunction()); | |
| 700 get_zoom_settings_function->set_extension(extension_); | |
| 701 get_zoom_settings_function->set_has_callback(true); | |
| 702 | |
| 703 scoped_ptr<base::DictionaryValue> get_zoom_settings_result( | |
| 704 utils::ToDictionary(utils::RunFunctionAndReturnSingleResult( | |
| 705 get_zoom_settings_function.get(), | |
| 706 base::StringPrintf("[%u]", tab_id), | |
| 707 browser()))); | |
| 708 | |
| 709 if (!get_zoom_settings_result.get()) | |
| 710 return testing::AssertionFailure() << "no result"; | |
| 711 | |
| 712 *mode = utils::GetString(get_zoom_settings_result.get(), "mode"); | |
| 713 *scope = utils::GetString(get_zoom_settings_result.get(), "scope"); | |
| 714 | |
| 715 return testing::AssertionSuccess(); | |
| 716 } | |
| 717 | |
| 718 std::string ExtensionTabsZoomTest::RunSetZoomExpectError(int tab_id, | |
| 719 double zoom_factor) { | |
| 720 scoped_refptr<TabsSetZoomFunction> set_zoom_function( | |
| 721 new TabsSetZoomFunction()); | |
| 722 set_zoom_function->set_extension(extension_); | |
| 723 set_zoom_function->set_has_callback(true); | |
| 724 | |
| 725 return utils::RunFunctionAndReturnError( | |
| 726 set_zoom_function.get(), | |
| 727 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), | |
| 728 browser()); | |
| 729 } | |
| 730 | |
| 731 std::string ExtensionTabsZoomTest::RunSetZoomSettingsExpectError( | |
| 732 int tab_id, | |
| 733 const char* mode, | |
| 734 const char* scope) { | |
| 735 scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( | |
| 736 new TabsSetZoomSettingsFunction()); | |
| 737 set_zoom_settings_function->set_extension(extension_); | |
| 738 | |
| 739 return utils::RunFunctionAndReturnError(set_zoom_settings_function.get(), | |
| 740 base::StringPrintf( | |
| 741 "[%u, {\"mode\": \"%s\", " | |
| 742 "\"scope\": \"%s\"}]", | |
| 743 tab_id, | |
| 744 mode, | |
| 745 scope), | |
| 746 browser()); | |
| 747 } | |
| 748 | |
| 749 content::WebContents* ExtensionTabsZoomTest::OpenUrlAndWaitForLoad( | |
| 750 const GURL& url) { | |
| 751 ui_test_utils::NavigateToURLWithDisposition( | |
| 752 browser(), | |
| 753 url, | |
| 754 NEW_FOREGROUND_TAB, | |
| 755 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 756 return browser()->tab_strip_model()->GetActiveWebContents(); | |
| 757 } | |
| 758 | |
| 759 content::OpenURLParams ExtensionTabsZoomTest::GetOpenParams( | |
|
Devlin
2014/06/23 22:56:08
nit: this could actually go in the anonymous names
wjmaclean
2014/06/24 15:38:16
Done.
| |
| 760 const char* url) const { | |
| 761 return content::OpenURLParams(GURL(url), | |
| 762 content::Referrer(), | |
| 763 NEW_FOREGROUND_TAB, | |
| 764 content::PAGE_TRANSITION_LINK, | |
| 765 false); | |
| 766 } | |
| 767 | |
| 768 namespace { | |
| 769 double GetZoomLevel(const content::WebContents* web_contents) { | |
| 770 return ZoomController::FromWebContents(web_contents)->GetZoomLevel(); | |
| 771 } | |
| 772 } // namespace | |
| 773 | |
| 774 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetAndGetZoom) { | |
| 775 const char kNewTestTabArgs[] = "about:blank"; | |
|
Devlin
2014/06/23 22:56:08
nit: turns out we actually have a kAboutBlankURL i
wjmaclean
2014/06/24 15:38:16
Done.
| |
| 776 content::OpenURLParams params(GetOpenParams(kNewTestTabArgs)); | |
| 777 content::WebContents* web_contents = browser()->OpenURL(params); | |
|
Devlin
2014/06/23 22:56:08
Sorry for not bringing this up sooner, but it just
wjmaclean
2014/06/24 15:38:16
Done. I assume it's OK if I use OpenUrlAndWaitForL
| |
| 778 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
| 779 | |
| 780 // Test default values before we set anything. | |
| 781 double zoom_factor = -1; | |
| 782 EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor)); | |
| 783 EXPECT_EQ(1.0, zoom_factor); | |
| 784 | |
| 785 // Test chrome.tabs.setZoom(). | |
| 786 const double kZoomLevel = 0.8; | |
| 787 EXPECT_TRUE(RunSetZoom(tab_id, kZoomLevel)); | |
| 788 EXPECT_EQ(kZoomLevel, | |
| 789 content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents))); | |
| 790 | |
| 791 // Test chrome.tabs.getZoom(). | |
| 792 zoom_factor = -1; | |
| 793 EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor)); | |
| 794 EXPECT_EQ(kZoomLevel, zoom_factor); | |
| 795 } | |
| 796 | |
| 797 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) { | |
| 798 const char kNewTestTabArgsA[] = "data:text/html,A"; | |
| 799 const char kNewTestTabArgsB[] = "data:text/html,B"; | |
| 800 | |
| 801 content::OpenURLParams params_A(GetOpenParams(kNewTestTabArgsA)); | |
| 802 content::OpenURLParams params_B(GetOpenParams(kNewTestTabArgsB)); | |
| 803 | |
| 804 // Tabs A1 and A2 are navigated to the same origin, while B is navigated | |
| 805 // to a different one. | |
| 806 content::WebContents* web_contents_A1 = OpenUrlAndWaitForLoad(params_A.url); | |
| 807 content::WebContents* web_contents_A2 = OpenUrlAndWaitForLoad(params_A.url); | |
| 808 content::WebContents* web_contents_B = OpenUrlAndWaitForLoad(params_B.url); | |
| 809 | |
| 810 int tab_id_A1 = ExtensionTabUtil::GetTabId(web_contents_A1); | |
| 811 int tab_id_A2 = ExtensionTabUtil::GetTabId(web_contents_A2); | |
| 812 int tab_id_B = ExtensionTabUtil::GetTabId(web_contents_B); | |
| 813 | |
| 814 ASSERT_FLOAT_EQ( | |
| 815 1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); | |
| 816 ASSERT_FLOAT_EQ( | |
| 817 1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); | |
| 818 ASSERT_FLOAT_EQ( | |
| 819 1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_B))); | |
| 820 | |
| 821 // Test per-origin automatic zoom settings. | |
| 822 EXPECT_TRUE(RunSetZoom(tab_id_B, 1.f)); | |
| 823 EXPECT_TRUE(RunSetZoom(tab_id_A2, 1.1f)); | |
| 824 EXPECT_FLOAT_EQ( | |
| 825 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); | |
| 826 EXPECT_FLOAT_EQ( | |
| 827 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); | |
| 828 EXPECT_FLOAT_EQ(1.f, | |
| 829 content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_B))); | |
| 830 | |
| 831 // Test per-tab automatic zoom settings. | |
| 832 EXPECT_TRUE(RunSetZoomSettings(tab_id_A1, "automatic", "per-tab")); | |
| 833 EXPECT_TRUE(RunSetZoom(tab_id_A1, 1.2f)); | |
| 834 EXPECT_FLOAT_EQ( | |
| 835 1.2f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); | |
| 836 EXPECT_FLOAT_EQ( | |
| 837 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); | |
| 838 | |
| 839 // Test 'manual' mode. | |
| 840 EXPECT_TRUE(RunSetZoomSettings(tab_id_A1, "manual", NULL)); | |
| 841 EXPECT_TRUE(RunSetZoom(tab_id_A1, 1.3f)); | |
| 842 EXPECT_FLOAT_EQ( | |
| 843 1.3f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); | |
| 844 EXPECT_FLOAT_EQ( | |
| 845 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); | |
| 846 | |
| 847 // Test 'disabled' mode, which will reset A1's zoom to 1.f. | |
| 848 EXPECT_TRUE(RunSetZoomSettings(tab_id_A1, "disabled", NULL)); | |
| 849 std::string error = RunSetZoomExpectError(tab_id_A1, 1.4f); | |
| 850 EXPECT_TRUE(MatchPattern(error, keys::kCannotZoomDisabledTabError)); | |
| 851 EXPECT_FLOAT_EQ( | |
| 852 1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); | |
| 853 // We should still be able to zoom A2 though. | |
| 854 EXPECT_TRUE(RunSetZoom(tab_id_A2, 1.4f)); | |
| 855 EXPECT_FLOAT_EQ( | |
| 856 1.4f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); | |
| 857 } | |
| 858 | |
| 859 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, GetZoomSettings) { | |
| 860 const char kNewTestTabArgs[] = "about:blank"; | |
| 861 content::OpenURLParams params(GetOpenParams(kNewTestTabArgs)); | |
| 862 content::WebContents* web_contents = browser()->OpenURL(params); | |
| 863 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
| 864 | |
| 865 std::string mode; | |
| 866 std::string scope; | |
| 867 | |
| 868 EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope)); | |
| 869 EXPECT_EQ("automatic", mode); | |
| 870 EXPECT_EQ("per-origin", scope); | |
| 871 | |
| 872 EXPECT_TRUE(RunSetZoomSettings(tab_id, "automatic", "per-tab")); | |
| 873 EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope)); | |
| 874 | |
| 875 EXPECT_EQ("automatic", mode); | |
| 876 EXPECT_EQ("per-tab", scope); | |
| 877 | |
| 878 std::string error = | |
| 879 RunSetZoomSettingsExpectError(tab_id, "manual", "per-origin"); | |
| 880 EXPECT_TRUE(MatchPattern(error, | |
| 881 keys::kPerOriginOnlyInAutomaticError)); | |
| 882 error = | |
| 883 RunSetZoomSettingsExpectError(tab_id, "disabled", "per-origin"); | |
| 884 EXPECT_TRUE(MatchPattern(error, | |
| 885 keys::kPerOriginOnlyInAutomaticError)); | |
| 886 } | |
| 887 | |
| 888 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, CannotZoomInvalidTab) { | |
| 889 { | |
| 890 const char kNewTestTabArgs[] = "about:blank"; | |
| 891 content::OpenURLParams params(GetOpenParams(kNewTestTabArgs)); | |
| 892 content::WebContents* web_contents = browser()->OpenURL(params); | |
| 893 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
| 894 | |
| 895 int bogus_id = tab_id + 100; | |
| 896 std::string error = RunSetZoomExpectError(bogus_id, 3.14159); | |
| 897 EXPECT_TRUE(MatchPattern(error, keys::kTabNotFoundError)); | |
| 898 | |
| 899 error = RunSetZoomSettingsExpectError(bogus_id, "manual", "per-tab"); | |
| 900 EXPECT_TRUE(MatchPattern(error, keys::kTabNotFoundError)); | |
| 901 } | |
|
Devlin
2014/06/23 22:56:07
nit: If there's no reason for the extra scoping, g
wjmaclean
2014/06/24 15:38:16
Done.
| |
| 902 { | |
| 903 const char kNewTestTabArgs[] = "chrome://version"; | |
| 904 content::OpenURLParams params(GetOpenParams(kNewTestTabArgs)); | |
| 905 content::WebContents* web_contents = browser()->OpenURL(params); | |
| 906 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
| 907 | |
| 908 // Test chrome.tabs.setZoom(). | |
| 909 std::string error = RunSetZoomExpectError(tab_id, 3.14159); | |
| 910 fprintf(stderr, "wjm: error=[%s]\n", error.c_str()); | |
|
Devlin
2014/06/23 22:56:08
Remove
wjmaclean
2014/06/24 15:38:16
Done.
| |
| 911 EXPECT_TRUE(MatchPattern(error, keys::kCannotZoomChromePagesError)); | |
| 912 | |
| 913 // chrome.tabs.setZoomSettings(). | |
| 914 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); | |
| 915 EXPECT_TRUE(MatchPattern(error, | |
| 916 keys::kCannotChangeChromePageZoomSettingsError)); | |
| 917 } | |
| 918 } | |
| 919 | |
| 594 } // namespace extensions | 920 } // namespace extensions |
| OLD | NEW |