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/devtools/devtools_window_testing.h" | 14 #include "chrome/browser/devtools/devtools_window_testing.h" |
15 #include "chrome/browser/extensions/api/tabs/tabs_api.h" | 15 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
16 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 16 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
17 #include "chrome/browser/extensions/extension_function_test_utils.h" | 17 #include "chrome/browser/extensions/extension_function_test_utils.h" |
18 #include "chrome/browser/extensions/extension_tab_util.h" | 18 #include "chrome/browser/extensions/extension_tab_util.h" |
19 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 19 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
22 #include "chrome/browser/ui/browser_commands.h" | 22 #include "chrome/browser/ui/browser_commands.h" |
23 #include "chrome/browser/ui/browser_window.h" | 23 #include "chrome/browser/ui/browser_window.h" |
24 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 25 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" |
25 #include "chrome/test/base/in_process_browser_test.h" | 26 #include "chrome/test/base/in_process_browser_test.h" |
26 #include "chrome/test/base/ui_test_utils.h" | 27 #include "chrome/test/base/ui_test_utils.h" |
| 28 #include "content/public/browser/browser_context.h" |
| 29 #include "content/public/browser/storage_partition.h" |
27 #include "content/public/common/page_zoom.h" | 30 #include "content/public/common/page_zoom.h" |
28 #include "content/public/common/url_constants.h" | 31 #include "content/public/common/url_constants.h" |
29 #include "extensions/browser/api_test_utils.h" | 32 #include "extensions/browser/api_test_utils.h" |
30 #include "extensions/common/manifest_constants.h" | 33 #include "extensions/common/manifest_constants.h" |
31 #include "extensions/common/test_util.h" | 34 #include "extensions/common/test_util.h" |
32 #include "net/test/spawned_test_server/spawned_test_server.h" | 35 #include "net/test/spawned_test_server/spawned_test_server.h" |
33 #include "ui/gfx/geometry/rect.h" | 36 #include "ui/gfx/geometry/rect.h" |
34 | 37 |
35 namespace extensions { | 38 namespace extensions { |
36 | 39 |
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor); | 662 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor); |
660 | 663 |
661 // Runs chrome.tabs.setZoomSettings(). | 664 // Runs chrome.tabs.setZoomSettings(). |
662 bool RunSetZoomSettings(int tab_id, const char* mode, const char* scope); | 665 bool RunSetZoomSettings(int tab_id, const char* mode, const char* scope); |
663 | 666 |
664 // Runs chrome.tabs.getZoomSettings(). | 667 // Runs chrome.tabs.getZoomSettings(). |
665 testing::AssertionResult RunGetZoomSettings(int tab_id, | 668 testing::AssertionResult RunGetZoomSettings(int tab_id, |
666 std::string* mode, | 669 std::string* mode, |
667 std::string* scope); | 670 std::string* scope); |
668 | 671 |
| 672 // Runs chrome.tabs.getZoomSettings() and returns default zoom. |
| 673 testing::AssertionResult RunGetDefaultZoom(int tab_id, |
| 674 double* default_zoom_factor); |
| 675 |
669 // Runs chrome.tabs.setZoom(), expecting an error. | 676 // Runs chrome.tabs.setZoom(), expecting an error. |
670 std::string RunSetZoomExpectError(int tab_id, | 677 std::string RunSetZoomExpectError(int tab_id, |
671 double zoom_factor); | 678 double zoom_factor); |
672 | 679 |
673 // Runs chrome.tabs.setZoomSettings(), expecting an error. | 680 // Runs chrome.tabs.setZoomSettings(), expecting an error. |
674 std::string RunSetZoomSettingsExpectError(int tab_id, | 681 std::string RunSetZoomSettingsExpectError(int tab_id, |
675 const char* mode, | 682 const char* mode, |
676 const char* scope); | 683 const char* scope); |
677 | 684 |
678 content::WebContents* OpenUrlAndWaitForLoad(const GURL& url); | 685 content::WebContents* OpenUrlAndWaitForLoad(const GURL& url); |
679 | 686 |
680 private: | 687 private: |
681 scoped_refptr<Extension> extension_; | 688 scoped_refptr<Extension> extension_; |
682 }; | 689 }; |
683 | 690 |
684 void ExtensionTabsZoomTest::SetUpOnMainThread() { | 691 void ExtensionTabsZoomTest::SetUpOnMainThread() { |
685 ExtensionTabsTest::SetUpOnMainThread(); | 692 ExtensionTabsTest::SetUpOnMainThread(); |
686 extension_ = test_util::CreateEmptyExtension(); | 693 extension_ = test_util::CreateEmptyExtension(); |
687 } | 694 } |
688 | 695 |
689 bool ExtensionTabsZoomTest::RunSetZoom(int tab_id, double zoom_factor) { | 696 bool ExtensionTabsZoomTest::RunSetZoom(int tab_id, double zoom_factor) { |
690 scoped_refptr<TabsSetZoomFunction> set_zoom_function( | 697 scoped_refptr<TabsSetZoomFunction> set_zoom_function( |
691 new TabsSetZoomFunction()); | 698 new TabsSetZoomFunction()); |
692 set_zoom_function->set_extension(extension_.get()); | 699 set_zoom_function->set_extension(extension_.get()); |
693 set_zoom_function->set_has_callback(true); | 700 set_zoom_function->set_has_callback(true); |
694 | 701 |
695 return utils::RunFunction( | 702 return utils::RunFunction( |
696 set_zoom_function.get(), | 703 set_zoom_function.get(), |
697 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), | 704 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), browser(), |
698 browser(), | |
699 extension_function_test_utils::NONE); | 705 extension_function_test_utils::NONE); |
700 } | 706 } |
701 | 707 |
702 testing::AssertionResult ExtensionTabsZoomTest::RunGetZoom( | 708 testing::AssertionResult ExtensionTabsZoomTest::RunGetZoom( |
703 int tab_id, | 709 int tab_id, |
704 double* zoom_factor) { | 710 double* zoom_factor) { |
705 scoped_refptr<TabsGetZoomFunction> get_zoom_function( | 711 scoped_refptr<TabsGetZoomFunction> get_zoom_function( |
706 new TabsGetZoomFunction()); | 712 new TabsGetZoomFunction()); |
707 get_zoom_function->set_extension(extension_.get()); | 713 get_zoom_function->set_extension(extension_.get()); |
708 get_zoom_function->set_has_callback(true); | 714 get_zoom_function->set_has_callback(true); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 | 767 |
762 if (!get_zoom_settings_result) | 768 if (!get_zoom_settings_result) |
763 return testing::AssertionFailure() << "no result"; | 769 return testing::AssertionFailure() << "no result"; |
764 | 770 |
765 *mode = api_test_utils::GetString(get_zoom_settings_result.get(), "mode"); | 771 *mode = api_test_utils::GetString(get_zoom_settings_result.get(), "mode"); |
766 *scope = api_test_utils::GetString(get_zoom_settings_result.get(), "scope"); | 772 *scope = api_test_utils::GetString(get_zoom_settings_result.get(), "scope"); |
767 | 773 |
768 return testing::AssertionSuccess(); | 774 return testing::AssertionSuccess(); |
769 } | 775 } |
770 | 776 |
| 777 testing::AssertionResult ExtensionTabsZoomTest::RunGetDefaultZoom( |
| 778 int tab_id, |
| 779 double* default_zoom_factor) { |
| 780 DCHECK(default_zoom_factor); |
| 781 scoped_refptr<TabsGetZoomSettingsFunction> get_zoom_settings_function( |
| 782 new TabsGetZoomSettingsFunction()); |
| 783 get_zoom_settings_function->set_extension(extension_.get()); |
| 784 get_zoom_settings_function->set_has_callback(true); |
| 785 |
| 786 scoped_ptr<base::DictionaryValue> get_zoom_settings_result( |
| 787 utils::ToDictionary(utils::RunFunctionAndReturnSingleResult( |
| 788 get_zoom_settings_function.get(), |
| 789 base::StringPrintf("[%u]", tab_id), |
| 790 browser()))); |
| 791 |
| 792 if (!get_zoom_settings_result) |
| 793 return testing::AssertionFailure() << "no result"; |
| 794 |
| 795 if (!get_zoom_settings_result->GetDouble("default_zoom_factor", |
| 796 default_zoom_factor)) { |
| 797 return testing::AssertionFailure() |
| 798 << "default zoom factor not found in result"; |
| 799 } |
| 800 |
| 801 return testing::AssertionSuccess(); |
| 802 } |
| 803 |
771 std::string ExtensionTabsZoomTest::RunSetZoomExpectError(int tab_id, | 804 std::string ExtensionTabsZoomTest::RunSetZoomExpectError(int tab_id, |
772 double zoom_factor) { | 805 double zoom_factor) { |
773 scoped_refptr<TabsSetZoomFunction> set_zoom_function( | 806 scoped_refptr<TabsSetZoomFunction> set_zoom_function( |
774 new TabsSetZoomFunction()); | 807 new TabsSetZoomFunction()); |
775 set_zoom_function->set_extension(extension_.get()); | 808 set_zoom_function->set_extension(extension_.get()); |
776 set_zoom_function->set_has_callback(true); | 809 set_zoom_function->set_has_callback(true); |
777 | 810 |
778 return utils::RunFunctionAndReturnError( | 811 return utils::RunFunctionAndReturnError( |
779 set_zoom_function.get(), | 812 set_zoom_function.get(), |
780 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), | 813 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
840 EXPECT_TRUE(RunSetZoom(tab_id, kZoomLevel)); | 873 EXPECT_TRUE(RunSetZoom(tab_id, kZoomLevel)); |
841 EXPECT_EQ(kZoomLevel, | 874 EXPECT_EQ(kZoomLevel, |
842 content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents))); | 875 content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents))); |
843 | 876 |
844 // Test chrome.tabs.getZoom(). | 877 // Test chrome.tabs.getZoom(). |
845 zoom_factor = -1; | 878 zoom_factor = -1; |
846 EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor)); | 879 EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor)); |
847 EXPECT_EQ(kZoomLevel, zoom_factor); | 880 EXPECT_EQ(kZoomLevel, zoom_factor); |
848 } | 881 } |
849 | 882 |
| 883 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, GetDefaultZoom) { |
| 884 content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL)); |
| 885 content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url); |
| 886 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 887 |
| 888 ui_zoom::ZoomController* zoom_controller = |
| 889 ui_zoom::ZoomController::FromWebContents(web_contents); |
| 890 double default_zoom_factor = -1.0; |
| 891 EXPECT_TRUE(RunGetDefaultZoom(tab_id, &default_zoom_factor)); |
| 892 EXPECT_TRUE(content::ZoomValuesEqual( |
| 893 zoom_controller->GetDefaultZoomLevel(), |
| 894 content::ZoomFactorToZoomLevel(default_zoom_factor))); |
| 895 |
| 896 // Change the default zoom level and verify GetDefaultZoom returns the |
| 897 // correct value. |
| 898 content::StoragePartition* partition = |
| 899 content::BrowserContext::GetStoragePartition( |
| 900 web_contents->GetBrowserContext(), web_contents->GetSiteInstance()); |
| 901 chrome::ChromeZoomLevelPrefs* zoom_prefs = |
| 902 static_cast<chrome::ChromeZoomLevelPrefs*>( |
| 903 partition->GetZoomLevelDelegate()); |
| 904 |
| 905 double default_zoom_level = zoom_controller->GetDefaultZoomLevel(); |
| 906 zoom_prefs->SetDefaultZoomLevelPref(default_zoom_level + 0.5); |
| 907 default_zoom_factor = -1.0; |
| 908 EXPECT_TRUE(RunGetDefaultZoom(tab_id, &default_zoom_factor)); |
| 909 EXPECT_TRUE(content::ZoomValuesEqual( |
| 910 default_zoom_level + 0.5, |
| 911 content::ZoomFactorToZoomLevel(default_zoom_factor))); |
| 912 } |
| 913 |
| 914 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetToDefaultZoom) { |
| 915 content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL)); |
| 916 content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url); |
| 917 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 918 |
| 919 ui_zoom::ZoomController* zoom_controller = |
| 920 ui_zoom::ZoomController::FromWebContents(web_contents); |
| 921 double default_zoom_level = zoom_controller->GetDefaultZoomLevel(); |
| 922 double new_default_zoom_level = default_zoom_level + 0.42; |
| 923 |
| 924 content::StoragePartition* partition = |
| 925 content::BrowserContext::GetStoragePartition( |
| 926 web_contents->GetBrowserContext(), web_contents->GetSiteInstance()); |
| 927 chrome::ChromeZoomLevelPrefs* zoom_prefs = |
| 928 static_cast<chrome::ChromeZoomLevelPrefs*>( |
| 929 partition->GetZoomLevelDelegate()); |
| 930 |
| 931 zoom_prefs->SetDefaultZoomLevelPref(new_default_zoom_level); |
| 932 |
| 933 double observed_zoom_factor = -1.0; |
| 934 EXPECT_TRUE(RunSetZoom(tab_id, 0.0)); |
| 935 EXPECT_TRUE(RunGetZoom(tab_id, &observed_zoom_factor)); |
| 936 EXPECT_TRUE(content::ZoomValuesEqual( |
| 937 new_default_zoom_level, |
| 938 content::ZoomFactorToZoomLevel(observed_zoom_factor))); |
| 939 } |
| 940 |
850 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) { | 941 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) { |
851 // In this test we need two URLs that (1) represent real pages (i.e. they | 942 // In this test we need two URLs that (1) represent real pages (i.e. they |
852 // load without causing an error page load), (2) have different domains, and | 943 // load without causing an error page load), (2) have different domains, and |
853 // (3) are zoomable by the extension API (this last condition rules out | 944 // (3) are zoomable by the extension API (this last condition rules out |
854 // chrome:// urls). We achieve this by noting that about:blank meets these | 945 // chrome:// urls). We achieve this by noting that about:blank meets these |
855 // requirements, allowing us to spin up a spawned http server on localhost to | 946 // requirements, allowing us to spin up a spawned http server on localhost to |
856 // get the other domain. | 947 // get the other domain. |
857 net::SpawnedTestServer http_server( | 948 net::SpawnedTestServer http_server( |
858 net::SpawnedTestServer::TYPE_HTTP, | 949 net::SpawnedTestServer::TYPE_HTTP, |
859 net::SpawnedTestServer::kLocalhost, | 950 net::SpawnedTestServer::kLocalhost, |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
994 // Test chrome.tabs.setZoom(). | 1085 // Test chrome.tabs.setZoom(). |
995 error = RunSetZoomExpectError(tab_id, 3.14159); | 1086 error = RunSetZoomExpectError(tab_id, 3.14159); |
996 EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); | 1087 EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); |
997 | 1088 |
998 // chrome.tabs.setZoomSettings(). | 1089 // chrome.tabs.setZoomSettings(). |
999 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); | 1090 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); |
1000 EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); | 1091 EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); |
1001 } | 1092 } |
1002 | 1093 |
1003 } // namespace extensions | 1094 } // namespace extensions |
OLD | NEW |