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/test/base/in_process_browser_test.h" | 23 #include "chrome/test/base/in_process_browser_test.h" |
24 #include "chrome/test/base/ui_test_utils.h" | 24 #include "chrome/test/base/ui_test_utils.h" |
| 25 #include "content/public/common/page_zoom.h" |
25 #include "ui/gfx/rect.h" | 26 #include "ui/gfx/rect.h" |
26 | 27 |
27 namespace extensions { | 28 namespace extensions { |
28 | 29 |
29 namespace keys = tabs_constants; | 30 namespace keys = tabs_constants; |
30 namespace utils = extension_function_test_utils; | 31 namespace utils = extension_function_test_utils; |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 class ExtensionTabsTest : public InProcessBrowserTest { | 35 class ExtensionTabsTest : public InProcessBrowserTest { |
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 EXPECT_EQ(base::Value::TYPE_DICTIONARY, duplicate_result->GetType()); | 584 EXPECT_EQ(base::Value::TYPE_DICTIONARY, duplicate_result->GetType()); |
584 // Duplicate tab id should be different from the original tab id. | 585 // Duplicate tab id should be different from the original tab id. |
585 EXPECT_NE(tab_id, duplicate_tab_id); | 586 EXPECT_NE(tab_id, duplicate_tab_id); |
586 EXPECT_EQ(window_id, duplicate_tab_window_id); | 587 EXPECT_EQ(window_id, duplicate_tab_window_id); |
587 EXPECT_EQ(tab_index + 1, duplicate_tab_index); | 588 EXPECT_EQ(tab_index + 1, duplicate_tab_index); |
588 // The test empty extension has no permissions, therefore |duplicate_result| | 589 // The test empty extension has no permissions, therefore |duplicate_result| |
589 // should not contain url, title, and faviconUrl in the function result. | 590 // should not contain url, title, and faviconUrl in the function result. |
590 EXPECT_FALSE(utils::HasPrivacySensitiveFields(duplicate_result.get())); | 591 EXPECT_FALSE(utils::HasPrivacySensitiveFields(duplicate_result.get())); |
591 } | 592 } |
592 | 593 |
| 594 // Provides helper functions for testing the zoom extension API. |
| 595 class ExtensionTabsZoomTest : public ExtensionTabsTest { |
| 596 public: |
| 597 void Init() { |
| 598 extension_ = utils::CreateEmptyExtension(); |
| 599 } |
| 600 |
| 601 // Runs chrome.tabs.setZoom(). |
| 602 void RunSetZoom(int tab_id, double zoom_factor) { |
| 603 scoped_refptr<TabsSetZoomFunction> set_zoom_function( |
| 604 new TabsSetZoomFunction()); |
| 605 set_zoom_function->set_extension(extension_); |
| 606 set_zoom_function->set_has_callback(true); |
| 607 |
| 608 utils::RunFunction(set_zoom_function.get(), |
| 609 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), |
| 610 browser(), extension_function_test_utils::NONE); |
| 611 } |
| 612 |
| 613 // Runs chrome.tabs.getZoom(). |
| 614 bool RunGetZoom(int tab_id, double* zoom_factor) { |
| 615 scoped_refptr<TabsGetZoomFunction> get_zoom_function( |
| 616 new TabsGetZoomFunction()); |
| 617 get_zoom_function->set_extension(extension_); |
| 618 get_zoom_function->set_has_callback(true); |
| 619 |
| 620 scoped_ptr<base::Value> get_zoom_result( |
| 621 utils::RunFunctionAndReturnSingleResult( |
| 622 get_zoom_function.get(), base::StringPrintf("[%u]", tab_id), |
| 623 browser())); |
| 624 |
| 625 if (!get_zoom_result.get()) |
| 626 return testing::AssertionFailure() << "no result"; |
| 627 if (!get_zoom_result->GetAsDouble(zoom_factor)) |
| 628 return testing::AssertionFailure() << "result was not a double"; |
| 629 |
| 630 return true; |
| 631 } |
| 632 |
| 633 // Runs chrome.tabs.setZoomSettings(). |
| 634 void RunSetZoomSettings(int tab_id, |
| 635 const char* mode, |
| 636 const char* scope) { |
| 637 scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( |
| 638 new TabsSetZoomSettingsFunction()); |
| 639 set_zoom_settings_function->set_extension(extension_); |
| 640 |
| 641 std:: string args; |
| 642 if (scope) { |
| 643 args = base::StringPrintf("[%u, {\"mode\": \"%s\", \"scope\": \"%s\"}]", |
| 644 tab_id, mode, scope); |
| 645 } else { |
| 646 args = base::StringPrintf("[%u, {\"mode\": \"%s\"}]", tab_id, mode); |
| 647 } |
| 648 |
| 649 utils::RunFunction(set_zoom_settings_function.get(), args, |
| 650 browser(), extension_function_test_utils::NONE); |
| 651 } |
| 652 |
| 653 // Runs chrome.tabs.getZoomSettings(). |
| 654 bool RunGetZoomSettings(int tab_id, std::string* mode, std::string* scope) { |
| 655 scoped_refptr<TabsGetZoomSettingsFunction> get_zoom_settings_function( |
| 656 new TabsGetZoomSettingsFunction()); |
| 657 get_zoom_settings_function->set_extension(extension_); |
| 658 get_zoom_settings_function->set_has_callback(true); |
| 659 |
| 660 scoped_ptr<base::DictionaryValue> get_zoom_settings_result( |
| 661 utils::ToDictionary(utils::RunFunctionAndReturnSingleResult( |
| 662 get_zoom_settings_function.get(), |
| 663 base::StringPrintf("[%u]", tab_id), |
| 664 browser()))); |
| 665 |
| 666 if (!get_zoom_settings_result.get()) |
| 667 return testing::AssertionFailure() << "no result"; |
| 668 |
| 669 *mode = utils::GetString(get_zoom_settings_result.get(), "mode"); |
| 670 *scope = utils::GetString(get_zoom_settings_result.get(), "scope"); |
| 671 |
| 672 return true; |
| 673 } |
| 674 |
| 675 // Runs chrome.tabs.setZoom(), expecting an error. |
| 676 void RunSetZoomExpectError(int tab_id, |
| 677 double zoom_factor, |
| 678 std::string* error) { |
| 679 scoped_refptr<TabsSetZoomFunction> set_zoom_function( |
| 680 new TabsSetZoomFunction()); |
| 681 set_zoom_function->set_extension(extension_); |
| 682 set_zoom_function->set_has_callback(true); |
| 683 |
| 684 *error = utils::RunFunctionAndReturnError( |
| 685 set_zoom_function.get(), |
| 686 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), |
| 687 browser()); |
| 688 } |
| 689 |
| 690 // Runs chrome.tabs.setZoomSettings(), expecting an error. |
| 691 void RunSetZoomSettingsExpectError(int tab_id, |
| 692 const char* mode, |
| 693 const char* scope, |
| 694 std::string* error) { |
| 695 scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( |
| 696 new TabsSetZoomSettingsFunction()); |
| 697 set_zoom_settings_function->set_extension(extension_); |
| 698 |
| 699 *error = utils::RunFunctionAndReturnError( |
| 700 set_zoom_settings_function.get(), |
| 701 base::StringPrintf("[%u, {\"mode\": \"%s\", " |
| 702 "\"scope\": \"%s\"}]", |
| 703 tab_id, mode, scope), |
| 704 browser()); |
| 705 } |
| 706 |
| 707 private: |
| 708 scoped_refptr<Extension> extension_; |
| 709 }; |
| 710 |
| 711 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetAndGetZoom) { |
| 712 Init(); |
| 713 const char kNewTestTabArgs[] = "about:blank"; |
| 714 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), |
| 715 NEW_FOREGROUND_TAB, |
| 716 content::PAGE_TRANSITION_LINK, false); |
| 717 content::WebContents* web_contents = browser()->OpenURL(params); |
| 718 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 719 |
| 720 // Test chrome.tabs.setZoom(). |
| 721 RunSetZoom(tab_id, 0.8); |
| 722 EXPECT_EQ(0.8, content::ZoomLevelToZoomFactor(web_contents->GetZoomLevel())); |
| 723 |
| 724 // Test chrome.tabs.getZoom(). |
| 725 double zoom_factor = -1; |
| 726 EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor)); |
| 727 EXPECT_EQ(0.8, zoom_factor); |
| 728 } |
| 729 |
| 730 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) { |
| 731 Init(); |
| 732 |
| 733 const char kNewTestTabArgsA[] = "data:text/html,A"; |
| 734 const char kNewTestTabArgsB[] = "data:text/html,B"; |
| 735 |
| 736 content::OpenURLParams params_A(GURL(kNewTestTabArgsA), content::Referrer(), |
| 737 NEW_FOREGROUND_TAB, |
| 738 content::PAGE_TRANSITION_LINK, false); |
| 739 content::OpenURLParams params_B(GURL(kNewTestTabArgsB), content::Referrer(), |
| 740 NEW_FOREGROUND_TAB, |
| 741 content::PAGE_TRANSITION_LINK, false); |
| 742 |
| 743 // Tabs A1 and A2 are navigated to the same origin, while B is navigated |
| 744 // to a different one. |
| 745 content::WebContents* web_contents_A1 = browser()->OpenURL(params_A); |
| 746 content::WebContents* web_contents_A2 = browser()->OpenURL(params_A); |
| 747 content::WebContents* web_contents_B = browser()->OpenURL(params_B); |
| 748 int tab_id_A1 = ExtensionTabUtil::GetTabId(web_contents_A1); |
| 749 int tab_id_A2 = ExtensionTabUtil::GetTabId(web_contents_A2); |
| 750 int tab_id_B = ExtensionTabUtil::GetTabId(web_contents_B); |
| 751 |
| 752 // Test per-origin automatic zoom settings. |
| 753 RunSetZoom(tab_id_B, 1); |
| 754 RunSetZoom(tab_id_A2, 1.1); |
| 755 EXPECT_EQ(1.1, |
| 756 content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); |
| 757 EXPECT_EQ(1.1, |
| 758 content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); |
| 759 EXPECT_EQ(1, |
| 760 content::ZoomLevelToZoomFactor(web_contents_B->GetZoomLevel())); |
| 761 |
| 762 // Test per-tab automatic zoom settings. |
| 763 RunSetZoomSettings(tab_id_A1, "automatic", "per-tab"); |
| 764 RunSetZoom(tab_id_A1, 1.2); |
| 765 EXPECT_EQ(1.2, |
| 766 content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); |
| 767 EXPECT_EQ(1.1, |
| 768 content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); |
| 769 |
| 770 // Test 'manual' mode. |
| 771 RunSetZoomSettings(tab_id_A1, "manual", NULL); |
| 772 RunSetZoom(tab_id_A1, 1.3); |
| 773 EXPECT_EQ(1.3, |
| 774 content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); |
| 775 EXPECT_EQ(1.1, |
| 776 content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); |
| 777 |
| 778 // Test 'disabled' mode. |
| 779 RunSetZoomSettings(tab_id_A1, "disabled", NULL); |
| 780 RunSetZoom(tab_id_A2, 1.4); |
| 781 EXPECT_EQ(1, |
| 782 content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); |
| 783 EXPECT_EQ(1.4, |
| 784 content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); |
| 785 } |
| 786 |
| 787 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, GetZoomSettings) { |
| 788 Init(); |
| 789 const char kNewTestTabArgs[] = "about:blank"; |
| 790 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), |
| 791 NEW_FOREGROUND_TAB, |
| 792 content::PAGE_TRANSITION_LINK, false); |
| 793 content::WebContents* web_contents = browser()->OpenURL(params); |
| 794 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 795 |
| 796 RunSetZoomSettings(tab_id, "automatic", "per-tab"); |
| 797 std::string mode; |
| 798 std::string scope; |
| 799 EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope)); |
| 800 |
| 801 EXPECT_EQ("automatic", mode); |
| 802 EXPECT_EQ("per-tab", scope); |
| 803 } |
| 804 |
| 805 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, CannotZoomChromeURL) { |
| 806 Init(); |
| 807 const char kNewTestTabArgs[] = "chrome://version"; |
| 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 error; |
| 815 |
| 816 // Test chrome.tabs.setZoom(). |
| 817 RunSetZoomExpectError(tab_id, 3.14159, &error); |
| 818 EXPECT_TRUE(MatchPattern(error, keys::kCannotZoomChromePagesError)); |
| 819 |
| 820 // chrome.tabs.setZoomSettings(). |
| 821 RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab", &error); |
| 822 EXPECT_TRUE(MatchPattern(error, |
| 823 keys::kCannotChangeChromePageZoomSettingsError)); |
| 824 } |
| 825 |
593 } // namespace extensions | 826 } // namespace extensions |
OLD | NEW |