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" |
| 28 #include "extensions/common/manifest_constants.h" |
26 #include "ui/gfx/rect.h" | 29 #include "ui/gfx/rect.h" |
27 | 30 |
28 namespace extensions { | 31 namespace extensions { |
29 | 32 |
30 namespace keys = tabs_constants; | 33 namespace keys = tabs_constants; |
31 namespace utils = extension_function_test_utils; | 34 namespace utils = extension_function_test_utils; |
32 | 35 |
33 namespace { | 36 namespace { |
34 | 37 |
35 class ExtensionTabsTest : public InProcessBrowserTest { | 38 class ExtensionTabsTest : public InProcessBrowserTest { |
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 EXPECT_EQ(base::Value::TYPE_DICTIONARY, duplicate_result->GetType()); | 587 EXPECT_EQ(base::Value::TYPE_DICTIONARY, duplicate_result->GetType()); |
585 // Duplicate tab id should be different from the original tab id. | 588 // Duplicate tab id should be different from the original tab id. |
586 EXPECT_NE(tab_id, duplicate_tab_id); | 589 EXPECT_NE(tab_id, duplicate_tab_id); |
587 EXPECT_EQ(window_id, duplicate_tab_window_id); | 590 EXPECT_EQ(window_id, duplicate_tab_window_id); |
588 EXPECT_EQ(tab_index + 1, duplicate_tab_index); | 591 EXPECT_EQ(tab_index + 1, duplicate_tab_index); |
589 // The test empty extension has no permissions, therefore |duplicate_result| | 592 // The test empty extension has no permissions, therefore |duplicate_result| |
590 // should not contain url, title, and faviconUrl in the function result. | 593 // should not contain url, title, and faviconUrl in the function result. |
591 EXPECT_FALSE(utils::HasPrivacySensitiveFields(duplicate_result.get())); | 594 EXPECT_FALSE(utils::HasPrivacySensitiveFields(duplicate_result.get())); |
592 } | 595 } |
593 | 596 |
| 597 // Tester class for the tabs.zoom* api functions. |
| 598 class ExtensionTabsZoomTest : public ExtensionTabsTest { |
| 599 public: |
| 600 virtual void SetUpOnMainThread() OVERRIDE; |
| 601 |
| 602 // Runs chrome.tabs.setZoom(). |
| 603 bool RunSetZoom(int tab_id, double zoom_factor); |
| 604 |
| 605 // Runs chrome.tabs.getZoom(). |
| 606 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor); |
| 607 |
| 608 // Runs chrome.tabs.setZoomSettings(). |
| 609 bool RunSetZoomSettings(int tab_id, const char* mode, const char* scope); |
| 610 |
| 611 // Runs chrome.tabs.getZoomSettings(). |
| 612 testing::AssertionResult RunGetZoomSettings(int tab_id, |
| 613 std::string* mode, |
| 614 std::string* scope); |
| 615 |
| 616 // Runs chrome.tabs.setZoom(), expecting an error. |
| 617 std::string RunSetZoomExpectError(int tab_id, |
| 618 double zoom_factor); |
| 619 |
| 620 // Runs chrome.tabs.setZoomSettings(), expecting an error. |
| 621 std::string RunSetZoomSettingsExpectError(int tab_id, |
| 622 const char* mode, |
| 623 const char* scope); |
| 624 |
| 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) |
| 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) |
| 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 namespace { |
| 760 |
| 761 double GetZoomLevel(const content::WebContents* web_contents) { |
| 762 return ZoomController::FromWebContents(web_contents)->GetZoomLevel(); |
| 763 } |
| 764 |
| 765 content::OpenURLParams GetOpenParams(const char* url) { |
| 766 return content::OpenURLParams(GURL(url), |
| 767 content::Referrer(), |
| 768 NEW_FOREGROUND_TAB, |
| 769 content::PAGE_TRANSITION_LINK, |
| 770 false); |
| 771 } |
| 772 |
| 773 } // namespace |
| 774 |
| 775 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetAndGetZoom) { |
| 776 content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL)); |
| 777 content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url); |
| 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[] = "http://hostA/"; |
| 799 const char kNewTestTabArgsB[] = "http://hostB/"; |
| 800 |
| 801 GURL url_A(kNewTestTabArgsA); |
| 802 GURL url_B(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(url_A); |
| 807 content::WebContents* web_contents_A2 = OpenUrlAndWaitForLoad(url_A); |
| 808 content::WebContents* web_contents_B = OpenUrlAndWaitForLoad(url_B); |
| 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 content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL)); |
| 861 content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url); |
| 862 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 863 |
| 864 std::string mode; |
| 865 std::string scope; |
| 866 |
| 867 EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope)); |
| 868 EXPECT_EQ("automatic", mode); |
| 869 EXPECT_EQ("per-origin", scope); |
| 870 |
| 871 EXPECT_TRUE(RunSetZoomSettings(tab_id, "automatic", "per-tab")); |
| 872 EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope)); |
| 873 |
| 874 EXPECT_EQ("automatic", mode); |
| 875 EXPECT_EQ("per-tab", scope); |
| 876 |
| 877 std::string error = |
| 878 RunSetZoomSettingsExpectError(tab_id, "manual", "per-origin"); |
| 879 EXPECT_TRUE(MatchPattern(error, |
| 880 keys::kPerOriginOnlyInAutomaticError)); |
| 881 error = |
| 882 RunSetZoomSettingsExpectError(tab_id, "disabled", "per-origin"); |
| 883 EXPECT_TRUE(MatchPattern(error, |
| 884 keys::kPerOriginOnlyInAutomaticError)); |
| 885 } |
| 886 |
| 887 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, CannotZoomInvalidTab) { |
| 888 content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL)); |
| 889 content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url); |
| 890 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 891 |
| 892 int bogus_id = tab_id + 100; |
| 893 std::string error = RunSetZoomExpectError(bogus_id, 3.14159); |
| 894 EXPECT_TRUE(MatchPattern(error, keys::kTabNotFoundError)); |
| 895 |
| 896 error = RunSetZoomSettingsExpectError(bogus_id, "manual", "per-tab"); |
| 897 EXPECT_TRUE(MatchPattern(error, keys::kTabNotFoundError)); |
| 898 |
| 899 const char kNewTestTabArgs[] = "chrome://version"; |
| 900 params = GetOpenParams(kNewTestTabArgs); |
| 901 web_contents = browser()->OpenURL(params); |
| 902 tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 903 |
| 904 // Test chrome.tabs.setZoom(). |
| 905 error = RunSetZoomExpectError(tab_id, 3.14159); |
| 906 EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); |
| 907 |
| 908 // chrome.tabs.setZoomSettings(). |
| 909 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); |
| 910 EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); |
| 911 } |
| 912 |
594 } // namespace extensions | 913 } // namespace extensions |
OLD | NEW |