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 return testing::AssertionSuccess(); | |
801 } | |
802 | |
771 std::string ExtensionTabsZoomTest::RunSetZoomExpectError(int tab_id, | 803 std::string ExtensionTabsZoomTest::RunSetZoomExpectError(int tab_id, |
772 double zoom_factor) { | 804 double zoom_factor) { |
773 scoped_refptr<TabsSetZoomFunction> set_zoom_function( | 805 scoped_refptr<TabsSetZoomFunction> set_zoom_function( |
774 new TabsSetZoomFunction()); | 806 new TabsSetZoomFunction()); |
775 set_zoom_function->set_extension(extension_.get()); | 807 set_zoom_function->set_extension(extension_.get()); |
776 set_zoom_function->set_has_callback(true); | 808 set_zoom_function->set_has_callback(true); |
777 | 809 |
778 return utils::RunFunctionAndReturnError( | 810 return utils::RunFunctionAndReturnError( |
779 set_zoom_function.get(), | 811 set_zoom_function.get(), |
780 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), | 812 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)); | 872 EXPECT_TRUE(RunSetZoom(tab_id, kZoomLevel)); |
841 EXPECT_EQ(kZoomLevel, | 873 EXPECT_EQ(kZoomLevel, |
842 content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents))); | 874 content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents))); |
843 | 875 |
844 // Test chrome.tabs.getZoom(). | 876 // Test chrome.tabs.getZoom(). |
845 zoom_factor = -1; | 877 zoom_factor = -1; |
846 EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor)); | 878 EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor)); |
847 EXPECT_EQ(kZoomLevel, zoom_factor); | 879 EXPECT_EQ(kZoomLevel, zoom_factor); |
848 } | 880 } |
849 | 881 |
882 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, GetDefaultZoom) { | |
883 content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL)); | |
884 content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url); | |
885 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
886 | |
887 const double kZoomFactorEpsilon = 0.00001; | |
888 | |
889 auto zoom_controller = ui_zoom::ZoomController::FromWebContents(web_contents); | |
not at google - send to devlin
2015/04/01 20:34:07
Using auto here makes it harder to read IMO, so ju
wjmaclean
2015/04/01 21:23:24
I thought this would save typing ui_zoom::ZoomCont
| |
890 double default_zoom_factor = -1.0; | |
891 EXPECT_TRUE(RunGetDefaultZoom(tab_id, &default_zoom_factor)); | |
892 EXPECT_NEAR(zoom_controller->GetDefaultZoomLevel(), | |
not at google - send to devlin
2015/04/01 20:34:07
I've been told that it's better to use EXPECT_DOUB
wjmaclean
2015/04/01 21:23:24
I tried using that initially, but it fails with ou
not at google - send to devlin
2015/04/01 22:26:11
Could you make a note of that; or perhaps better,
| |
893 content::ZoomFactorToZoomLevel(default_zoom_factor), | |
894 kZoomFactorEpsilon); | |
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 default_zoom_level += 0.5; | |
not at google - send to devlin
2015/04/01 20:34:07
I'd rather not mutate this default_zoom_level vari
wjmaclean
2015/04/01 21:23:24
Done.
| |
907 zoom_prefs->SetDefaultZoomLevelPref(default_zoom_level); | |
908 default_zoom_factor = -1.0; | |
909 EXPECT_TRUE(RunGetDefaultZoom(tab_id, &default_zoom_factor)); | |
910 EXPECT_NEAR(default_zoom_level, | |
911 content::ZoomFactorToZoomLevel(default_zoom_factor), | |
912 kZoomFactorEpsilon); | |
913 } | |
914 | |
915 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetToDefaultZoom) { | |
916 content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL)); | |
917 content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url); | |
918 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
919 | |
920 const double kZoomFactorEpsilon = 0.00001; | |
921 | |
922 auto zoom_controller = ui_zoom::ZoomController::FromWebContents(web_contents); | |
923 double default_zoom_level = zoom_controller->GetDefaultZoomLevel(); | |
924 double new_default_zoom_level = default_zoom_level + 0.42; | |
925 | |
926 content::StoragePartition* partition = | |
927 content::BrowserContext::GetStoragePartition( | |
928 web_contents->GetBrowserContext(), web_contents->GetSiteInstance()); | |
929 chrome::ChromeZoomLevelPrefs* zoom_prefs = | |
930 static_cast<chrome::ChromeZoomLevelPrefs*>( | |
931 partition->GetZoomLevelDelegate()); | |
932 | |
933 zoom_prefs->SetDefaultZoomLevelPref(new_default_zoom_level); | |
934 | |
935 double observed_zoom_factor = -1.0; | |
936 EXPECT_TRUE(RunSetZoom(tab_id, 0.0)); | |
937 EXPECT_TRUE(RunGetZoom(tab_id, &observed_zoom_factor)); | |
938 EXPECT_NEAR(new_default_zoom_level, | |
939 content::ZoomFactorToZoomLevel(observed_zoom_factor), | |
940 kZoomFactorEpsilon); | |
941 } | |
942 | |
850 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) { | 943 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) { |
851 // In this test we need two URLs that (1) represent real pages (i.e. they | 944 // 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 | 945 // 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 | 946 // (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 | 947 // 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 | 948 // requirements, allowing us to spin up a spawned http server on localhost to |
856 // get the other domain. | 949 // get the other domain. |
857 net::SpawnedTestServer http_server( | 950 net::SpawnedTestServer http_server( |
858 net::SpawnedTestServer::TYPE_HTTP, | 951 net::SpawnedTestServer::TYPE_HTTP, |
859 net::SpawnedTestServer::kLocalhost, | 952 net::SpawnedTestServer::kLocalhost, |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
994 // Test chrome.tabs.setZoom(). | 1087 // Test chrome.tabs.setZoom(). |
995 error = RunSetZoomExpectError(tab_id, 3.14159); | 1088 error = RunSetZoomExpectError(tab_id, 3.14159); |
996 EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); | 1089 EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); |
997 | 1090 |
998 // chrome.tabs.setZoomSettings(). | 1091 // chrome.tabs.setZoomSettings(). |
999 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); | 1092 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); |
1000 EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); | 1093 EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); |
1001 } | 1094 } |
1002 | 1095 |
1003 } // namespace extensions | 1096 } // namespace extensions |
OLD | NEW |