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 // Provides helper functions for testing the zoom extension API. |
| 597 class ExtensionTabsZoomTest : public ExtensionTabsTest { |
| 598 public: |
| 599 void Init() { |
| 600 extension_ = utils::CreateEmptyExtension(); |
| 601 } |
| 602 |
| 603 // Runs chrome.tabs.setZoom(). |
| 604 void RunSetZoom(int tab_id, double zoom_factor) { |
| 605 scoped_refptr<TabsSetZoomFunction> set_zoom_function( |
| 606 new TabsSetZoomFunction()); |
| 607 set_zoom_function->set_extension(extension_); |
| 608 set_zoom_function->set_has_callback(true); |
| 609 |
| 610 utils::RunFunction(set_zoom_function.get(), |
| 611 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), |
| 612 browser(), extension_function_test_utils::NONE); |
| 613 content::RunAllPendingInMessageLoop(); |
| 614 } |
| 615 |
| 616 // Runs chrome.tabs.getZoom(). |
| 617 bool RunGetZoom(int tab_id, double* zoom_factor) { |
| 618 scoped_refptr<TabsGetZoomFunction> get_zoom_function( |
| 619 new TabsGetZoomFunction()); |
| 620 get_zoom_function->set_extension(extension_); |
| 621 get_zoom_function->set_has_callback(true); |
| 622 |
| 623 scoped_ptr<base::Value> get_zoom_result( |
| 624 utils::RunFunctionAndReturnSingleResult( |
| 625 get_zoom_function.get(), base::StringPrintf("[%u]", tab_id), |
| 626 browser())); |
| 627 |
| 628 if (!get_zoom_result.get()) |
| 629 return testing::AssertionFailure() << "no result"; |
| 630 if (!get_zoom_result->GetAsDouble(zoom_factor)) |
| 631 return testing::AssertionFailure() << "result was not a double"; |
| 632 |
| 633 return true; |
| 634 } |
| 635 |
| 636 // Runs chrome.tabs.setZoomSettings(). |
| 637 void RunSetZoomSettings(int tab_id, |
| 638 const char* mode, |
| 639 const char* scope) { |
| 640 scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( |
| 641 new TabsSetZoomSettingsFunction()); |
| 642 set_zoom_settings_function->set_extension(extension_); |
| 643 |
| 644 std:: string args; |
| 645 if (scope) { |
| 646 args = base::StringPrintf("[%u, {\"mode\": \"%s\", \"scope\": \"%s\"}]", |
| 647 tab_id, mode, scope); |
| 648 } else { |
| 649 args = base::StringPrintf("[%u, {\"mode\": \"%s\"}]", tab_id, mode); |
| 650 } |
| 651 |
| 652 utils::RunFunction(set_zoom_settings_function.get(), args, |
| 653 browser(), extension_function_test_utils::NONE); |
| 654 content::RunAllPendingInMessageLoop(); |
| 655 } |
| 656 |
| 657 // Runs chrome.tabs.getZoomSettings(). |
| 658 bool RunGetZoomSettings(int tab_id, std::string* mode, std::string* scope) { |
| 659 scoped_refptr<TabsGetZoomSettingsFunction> get_zoom_settings_function( |
| 660 new TabsGetZoomSettingsFunction()); |
| 661 get_zoom_settings_function->set_extension(extension_); |
| 662 get_zoom_settings_function->set_has_callback(true); |
| 663 |
| 664 scoped_ptr<base::DictionaryValue> get_zoom_settings_result( |
| 665 utils::ToDictionary(utils::RunFunctionAndReturnSingleResult( |
| 666 get_zoom_settings_function.get(), |
| 667 base::StringPrintf("[%u]", tab_id), |
| 668 browser()))); |
| 669 |
| 670 if (!get_zoom_settings_result.get()) |
| 671 return testing::AssertionFailure() << "no result"; |
| 672 |
| 673 *mode = utils::GetString(get_zoom_settings_result.get(), "mode"); |
| 674 *scope = utils::GetString(get_zoom_settings_result.get(), "scope"); |
| 675 |
| 676 return true; |
| 677 } |
| 678 |
| 679 // Runs chrome.tabs.setZoom(), expecting an error. |
| 680 void RunSetZoomExpectError(int tab_id, |
| 681 double zoom_factor, |
| 682 std::string* error) { |
| 683 scoped_refptr<TabsSetZoomFunction> set_zoom_function( |
| 684 new TabsSetZoomFunction()); |
| 685 set_zoom_function->set_extension(extension_); |
| 686 set_zoom_function->set_has_callback(true); |
| 687 |
| 688 *error = utils::RunFunctionAndReturnError( |
| 689 set_zoom_function.get(), |
| 690 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), |
| 691 browser()); |
| 692 } |
| 693 |
| 694 // Runs chrome.tabs.setZoomSettings(), expecting an error. |
| 695 void RunSetZoomSettingsExpectError(int tab_id, |
| 696 const char* mode, |
| 697 const char* scope, |
| 698 std::string* error) { |
| 699 scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( |
| 700 new TabsSetZoomSettingsFunction()); |
| 701 set_zoom_settings_function->set_extension(extension_); |
| 702 |
| 703 *error = utils::RunFunctionAndReturnError( |
| 704 set_zoom_settings_function.get(), |
| 705 base::StringPrintf("[%u, {\"mode\": \"%s\", " |
| 706 "\"scope\": \"%s\"}]", |
| 707 tab_id, mode, scope), |
| 708 browser()); |
| 709 } |
| 710 |
| 711 content::WebContents* OpenUrlAndWaitForLoad(const GURL& url) { |
| 712 ui_test_utils::NavigateToURLWithDisposition( |
| 713 browser(), url, NEW_FOREGROUND_TAB, |
| 714 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 715 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 716 } |
| 717 |
| 718 private: |
| 719 scoped_refptr<Extension> extension_; |
| 720 }; |
| 721 |
| 722 namespace { |
| 723 double GetZoomLevel(const content::WebContents* web_contents) { |
| 724 return ZoomController::FromWebContents(web_contents)->GetZoomLevel(); |
| 725 } |
| 726 } // namespace |
| 727 |
| 728 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetAndGetZoom) { |
| 729 Init(); |
| 730 const char kNewTestTabArgs[] = "about:blank"; |
| 731 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), |
| 732 NEW_FOREGROUND_TAB, |
| 733 content::PAGE_TRANSITION_LINK, false); |
| 734 content::WebContents* web_contents = browser()->OpenURL(params); |
| 735 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 736 |
| 737 // Test chrome.tabs.setZoom(). |
| 738 RunSetZoom(tab_id, 0.8); |
| 739 EXPECT_EQ(0.8, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents))); |
| 740 |
| 741 // Test chrome.tabs.getZoom(). |
| 742 double zoom_factor = -1; |
| 743 EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor)); |
| 744 EXPECT_EQ(0.8, zoom_factor); |
| 745 } |
| 746 |
| 747 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) { |
| 748 Init(); |
| 749 |
| 750 const char kNewTestTabArgsA[] = "data:text/html,A"; |
| 751 const char kNewTestTabArgsB[] = "data:text/html,B"; |
| 752 |
| 753 content::OpenURLParams params_A(GURL(kNewTestTabArgsA), content::Referrer(), |
| 754 NEW_FOREGROUND_TAB, |
| 755 content::PAGE_TRANSITION_LINK, false); |
| 756 content::OpenURLParams params_B(GURL(kNewTestTabArgsB), content::Referrer(), |
| 757 NEW_FOREGROUND_TAB, |
| 758 content::PAGE_TRANSITION_LINK, false); |
| 759 |
| 760 // Tabs A1 and A2 are navigated to the same origin, while B is navigated |
| 761 // to a different one. |
| 762 content::WebContents* web_contents_A1 = OpenUrlAndWaitForLoad(params_A.url); |
| 763 content::WebContents* web_contents_A2 = OpenUrlAndWaitForLoad(params_A.url); |
| 764 content::WebContents* web_contents_B = OpenUrlAndWaitForLoad(params_B.url); |
| 765 |
| 766 int tab_id_A1 = ExtensionTabUtil::GetTabId(web_contents_A1); |
| 767 int tab_id_A2 = ExtensionTabUtil::GetTabId(web_contents_A2); |
| 768 int tab_id_B = ExtensionTabUtil::GetTabId(web_contents_B); |
| 769 |
| 770 // Test per-origin automatic zoom settings. |
| 771 RunSetZoom(tab_id_B, 1.f); |
| 772 RunSetZoom(tab_id_A2, 1.1f); |
| 773 EXPECT_FLOAT_EQ( |
| 774 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); |
| 775 EXPECT_FLOAT_EQ( |
| 776 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); |
| 777 EXPECT_FLOAT_EQ(1.f, |
| 778 content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_B))); |
| 779 |
| 780 // Test per-tab automatic zoom settings. |
| 781 RunSetZoomSettings(tab_id_A1, "automatic", "per-tab"); |
| 782 RunSetZoom(tab_id_A1, 1.2f); |
| 783 EXPECT_FLOAT_EQ( |
| 784 1.2f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); |
| 785 EXPECT_FLOAT_EQ( |
| 786 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); |
| 787 |
| 788 // Test 'manual' mode. |
| 789 RunSetZoomSettings(tab_id_A1, "manual", NULL); |
| 790 RunSetZoom(tab_id_A1, 1.3f); |
| 791 EXPECT_FLOAT_EQ( |
| 792 1.3f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); |
| 793 EXPECT_FLOAT_EQ( |
| 794 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); |
| 795 |
| 796 // Test 'disabled' mode, which will reset A1's zoom to 1.f. |
| 797 RunSetZoomSettings(tab_id_A1, "disabled", NULL); |
| 798 RunSetZoom(tab_id_A2, 1.4f); |
| 799 EXPECT_FLOAT_EQ( |
| 800 1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); |
| 801 EXPECT_FLOAT_EQ( |
| 802 1.4f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); |
| 803 } |
| 804 |
| 805 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, GetZoomSettings) { |
| 806 Init(); |
| 807 const char kNewTestTabArgs[] = "about:blank"; |
| 808 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), |
| 809 NEW_FOREGROUND_TAB, |
| 810 content::PAGE_TRANSITION_LINK, false); |
| 811 content::WebContents* web_contents = browser()->OpenURL(params); |
| 812 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 813 |
| 814 std::string mode; |
| 815 std::string scope; |
| 816 |
| 817 EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope)); |
| 818 EXPECT_EQ("automatic", mode); |
| 819 EXPECT_EQ("per-origin", scope); |
| 820 |
| 821 RunSetZoomSettings(tab_id, "automatic", "per-tab"); |
| 822 EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope)); |
| 823 |
| 824 EXPECT_EQ("automatic", mode); |
| 825 EXPECT_EQ("per-tab", scope); |
| 826 } |
| 827 |
| 828 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, CannotZoomChromeURL) { |
| 829 Init(); |
| 830 const char kNewTestTabArgs[] = "chrome://version"; |
| 831 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), |
| 832 NEW_FOREGROUND_TAB, |
| 833 content::PAGE_TRANSITION_LINK, false); |
| 834 content::WebContents* web_contents = browser()->OpenURL(params); |
| 835 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 836 |
| 837 std::string error; |
| 838 |
| 839 // Test chrome.tabs.setZoom(). |
| 840 RunSetZoomExpectError(tab_id, 3.14159, &error); |
| 841 EXPECT_TRUE(MatchPattern(error, keys::kCannotZoomChromePagesError)); |
| 842 |
| 843 // chrome.tabs.setZoomSettings(). |
| 844 RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab", &error); |
| 845 EXPECT_TRUE(MatchPattern(error, |
| 846 keys::kCannotChangeChromePageZoomSettingsError)); |
| 847 } |
| 848 |
594 } // namespace extensions | 849 } // namespace extensions |
OLD | NEW |