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 // Tester class for the tabs.zoom* api functions. | |
597 class ExtensionTabsZoomTest : public ExtensionTabsTest { | |
598 public: | |
599 virtual void SetUpOnMainThread() OVERRIDE; | |
600 | |
601 // Runs chrome.tabs.setZoom(). | |
602 void RunSetZoom(int tab_id, double zoom_factor); | |
603 | |
604 // Runs chrome.tabs.getZoom(). | |
605 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor); | |
606 | |
607 // Runs chrome.tabs.setZoomSettings(). | |
608 void RunSetZoomSettings(int tab_id, const char* mode, const char* scope); | |
609 | |
610 // Runs chrome.tabs.getZoomSettings(). | |
611 testing::AssertionResult RunGetZoomSettings(int tab_id, | |
612 std::string* mode, | |
613 std::string* scope); | |
614 | |
615 // Runs chrome.tabs.setZoom(), expecting an error. | |
616 void RunSetZoomExpectError(int tab_id, | |
617 double zoom_factor, | |
618 std::string* error); | |
619 // Runs chrome.tabs.setZoomSettings(), expecting an error. | |
Devlin
2014/06/19 21:15:28
nit: newline between functions
wjmaclean
2014/06/20 22:01:32
Done.
| |
620 void RunSetZoomSettingsExpectError(int tab_id, | |
621 const char* mode, | |
622 const char* scope, | |
623 std::string* error); | |
624 | |
625 content::WebContents* OpenUrlAndWaitForLoad(const GURL& url); | |
626 | |
627 private: | |
628 scoped_refptr<Extension> extension_; | |
629 }; | |
630 | |
631 void ExtensionTabsZoomTest::SetUpOnMainThread() { | |
Devlin
2014/06/19 21:15:29
Call the super::SetUpOnMainThread() first.
wjmaclean
2014/06/20 22:01:33
Done.
| |
632 extension_ = utils::CreateEmptyExtension(); | |
633 } | |
634 | |
635 void ExtensionTabsZoomTest::RunSetZoom(int tab_id, double zoom_factor) { | |
Devlin
2014/06/19 21:15:28
This should probably return the bool result from u
wjmaclean
2014/06/20 22:01:32
Done.
| |
636 scoped_refptr<TabsSetZoomFunction> set_zoom_function( | |
637 new TabsSetZoomFunction()); | |
638 set_zoom_function->set_extension(extension_); | |
639 set_zoom_function->set_has_callback(true); | |
640 | |
641 utils::RunFunction(set_zoom_function.get(), | |
642 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), | |
643 browser(), | |
644 extension_function_test_utils::NONE); | |
645 } | |
646 | |
647 testing::AssertionResult ExtensionTabsZoomTest::RunGetZoom( | |
648 int tab_id, | |
649 double* zoom_factor) { | |
650 scoped_refptr<TabsGetZoomFunction> get_zoom_function( | |
651 new TabsGetZoomFunction()); | |
652 get_zoom_function->set_extension(extension_); | |
653 get_zoom_function->set_has_callback(true); | |
654 | |
655 scoped_ptr<base::Value> get_zoom_result( | |
656 utils::RunFunctionAndReturnSingleResult( | |
657 get_zoom_function.get(), | |
658 base::StringPrintf("[%u]", tab_id), | |
659 browser())); | |
660 | |
661 if (!get_zoom_result.get()) | |
662 return testing::AssertionFailure() << "no result"; | |
663 if (!get_zoom_result->GetAsDouble(zoom_factor)) | |
664 return testing::AssertionFailure() << "result was not a double"; | |
665 | |
666 return testing::AssertionSuccess(); | |
667 } | |
668 | |
669 void ExtensionTabsZoomTest::RunSetZoomSettings(int tab_id, | |
Devlin
2014/06/19 21:15:28
again, probably return a bool here.
wjmaclean
2014/06/20 22:01:33
Done.
| |
670 const char* mode, | |
671 const char* scope) { | |
672 scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( | |
673 new TabsSetZoomSettingsFunction()); | |
674 set_zoom_settings_function->set_extension(extension_); | |
675 | |
676 std:: string args; | |
Devlin
2014/06/19 21:15:28
no space after std::.
wjmaclean
2014/06/20 22:01:33
Done.
| |
677 if (scope) { | |
678 args = base::StringPrintf("[%u, {\"mode\": \"%s\", \"scope\": \"%s\"}]", | |
679 tab_id, mode, scope); | |
680 } else { | |
681 args = base::StringPrintf("[%u, {\"mode\": \"%s\"}]", tab_id, mode); | |
682 } | |
683 | |
684 utils::RunFunction(set_zoom_settings_function.get(), | |
685 args, | |
686 browser(), | |
687 extension_function_test_utils::NONE); | |
688 content::RunAllPendingInMessageLoop(); | |
Devlin
2014/06/19 21:15:28
missed this one.
wjmaclean
2014/06/20 22:01:32
Done.
| |
689 } | |
690 | |
691 testing::AssertionResult ExtensionTabsZoomTest::RunGetZoomSettings( | |
692 int tab_id, | |
693 std::string* mode, | |
694 std::string* scope) { | |
695 DCHECK(mode); | |
696 DCHECK(scope); | |
697 scoped_refptr<TabsGetZoomSettingsFunction> get_zoom_settings_function( | |
698 new TabsGetZoomSettingsFunction()); | |
699 get_zoom_settings_function->set_extension(extension_); | |
700 get_zoom_settings_function->set_has_callback(true); | |
701 | |
702 scoped_ptr<base::DictionaryValue> get_zoom_settings_result( | |
703 utils::ToDictionary(utils::RunFunctionAndReturnSingleResult( | |
704 get_zoom_settings_function.get(), | |
705 base::StringPrintf("[%u]", tab_id), | |
706 browser()))); | |
707 | |
708 if (!get_zoom_settings_result.get()) | |
709 return testing::AssertionFailure() << "no result"; | |
710 | |
711 *mode = utils::GetString(get_zoom_settings_result.get(), "mode"); | |
712 *scope = utils::GetString(get_zoom_settings_result.get(), "scope"); | |
713 | |
714 return testing::AssertionSuccess(); | |
715 } | |
716 | |
717 void ExtensionTabsZoomTest::RunSetZoomExpectError(int tab_id, | |
Devlin
2014/06/19 21:15:28
May as well have this return |error| directly.
wjmaclean
2014/06/20 22:01:33
Done.
| |
718 double zoom_factor, | |
719 std::string* error) { | |
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 *error = utils::RunFunctionAndReturnError( | |
726 set_zoom_function.get(), | |
727 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), | |
728 browser()); | |
729 } | |
730 | |
731 void ExtensionTabsZoomTest::RunSetZoomSettingsExpectError(int tab_id, | |
Devlin
2014/06/19 21:15:29
ditto.
wjmaclean
2014/06/20 22:01:33
Done.
| |
732 const char* mode, | |
733 const char* scope, | |
734 std::string* error) { | |
735 scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( | |
736 new TabsSetZoomSettingsFunction()); | |
737 set_zoom_settings_function->set_extension(extension_); | |
738 | |
739 *error = 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 double GetZoomLevel(const content::WebContents* web_contents) { | |
761 return ZoomController::FromWebContents(web_contents)->GetZoomLevel(); | |
762 } | |
763 } // namespace | |
764 | |
765 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetAndGetZoom) { | |
766 const char kNewTestTabArgs[] = "about:blank"; | |
767 content::OpenURLParams params(GURL(kNewTestTabArgs), | |
768 content::Referrer(), | |
769 NEW_FOREGROUND_TAB, | |
770 content::PAGE_TRANSITION_LINK, | |
771 false); | |
772 content::WebContents* web_contents = browser()->OpenURL(params); | |
773 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
774 | |
775 // Test default values before we set anything. | |
776 double zoom_factor = -1; | |
777 EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor)); | |
778 EXPECT_EQ(1.0, zoom_factor); | |
779 | |
780 // Test chrome.tabs.setZoom(). | |
781 const double kZoomLevel = 0.8; | |
782 RunSetZoom(tab_id, kZoomLevel); | |
783 EXPECT_EQ(kZoomLevel, | |
784 content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents))); | |
785 | |
786 // Test chrome.tabs.getZoom(). | |
787 zoom_factor = -1; | |
788 EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor)); | |
789 EXPECT_EQ(kZoomLevel, zoom_factor); | |
790 } | |
791 | |
792 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) { | |
793 const char kNewTestTabArgsA[] = "data:text/html,A"; | |
794 const char kNewTestTabArgsB[] = "data:text/html,B"; | |
795 | |
796 content::OpenURLParams params_A(GURL(kNewTestTabArgsA), content::Referrer(), | |
Devlin
2014/06/19 21:15:29
nit: one arg per line.
wjmaclean
2014/06/20 22:01:33
Done.
| |
797 NEW_FOREGROUND_TAB, | |
798 content::PAGE_TRANSITION_LINK, false); | |
799 content::OpenURLParams params_B(GURL(kNewTestTabArgsB), content::Referrer(), | |
800 NEW_FOREGROUND_TAB, | |
801 content::PAGE_TRANSITION_LINK, false); | |
802 | |
803 // Tabs A1 and A2 are navigated to the same origin, while B is navigated | |
804 // to a different one. | |
805 content::WebContents* web_contents_A1 = OpenUrlAndWaitForLoad(params_A.url); | |
806 content::WebContents* web_contents_A2 = OpenUrlAndWaitForLoad(params_A.url); | |
807 content::WebContents* web_contents_B = OpenUrlAndWaitForLoad(params_B.url); | |
808 | |
809 int tab_id_A1 = ExtensionTabUtil::GetTabId(web_contents_A1); | |
810 int tab_id_A2 = ExtensionTabUtil::GetTabId(web_contents_A2); | |
811 int tab_id_B = ExtensionTabUtil::GetTabId(web_contents_B); | |
812 | |
813 ASSERT_FLOAT_EQ( | |
814 1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); | |
815 ASSERT_FLOAT_EQ( | |
816 1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); | |
817 ASSERT_FLOAT_EQ( | |
818 1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_B))); | |
819 | |
820 // Test per-origin automatic zoom settings. | |
821 RunSetZoom(tab_id_B, 1.f); | |
822 RunSetZoom(tab_id_A2, 1.1f); | |
823 EXPECT_FLOAT_EQ( | |
824 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); | |
825 EXPECT_FLOAT_EQ( | |
826 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); | |
827 EXPECT_FLOAT_EQ(1.f, | |
828 content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_B))); | |
829 | |
830 // Test per-tab automatic zoom settings. | |
831 RunSetZoomSettings(tab_id_A1, "automatic", "per-tab"); | |
832 RunSetZoom(tab_id_A1, 1.2f); | |
833 EXPECT_FLOAT_EQ( | |
834 1.2f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); | |
835 EXPECT_FLOAT_EQ( | |
836 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); | |
837 | |
838 // Test 'manual' mode. | |
839 RunSetZoomSettings(tab_id_A1, "manual", NULL); | |
840 RunSetZoom(tab_id_A1, 1.3f); | |
841 EXPECT_FLOAT_EQ( | |
842 1.3f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); | |
843 EXPECT_FLOAT_EQ( | |
844 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); | |
845 | |
846 // Test 'disabled' mode, which will reset A1's zoom to 1.f. | |
847 RunSetZoomSettings(tab_id_A1, "disabled", NULL); | |
848 std::string error; | |
849 RunSetZoomExpectError(tab_id_A1, 1.4f, &error); | |
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 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 const char kNewTestTabArgs[] = "about:blank"; | |
861 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), | |
Devlin
2014/06/19 21:15:29
nit: one arg per line
wjmaclean
2014/06/20 22:01:33
Done.
| |
862 NEW_FOREGROUND_TAB, | |
863 content::PAGE_TRANSITION_LINK, false); | |
864 content::WebContents* web_contents = browser()->OpenURL(params); | |
865 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
866 | |
867 std::string mode; | |
868 std::string scope; | |
869 | |
870 EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope)); | |
871 EXPECT_EQ("automatic", mode); | |
872 EXPECT_EQ("per-origin", scope); | |
873 | |
874 RunSetZoomSettings(tab_id, "automatic", "per-tab"); | |
875 EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope)); | |
876 | |
877 EXPECT_EQ("automatic", mode); | |
878 EXPECT_EQ("per-tab", scope); | |
879 | |
880 std::string error; | |
881 RunSetZoomSettingsExpectError(tab_id, "manual", "per-origin", &error); | |
882 EXPECT_TRUE(MatchPattern(error, | |
883 keys::kPerOriginOnlyInAutomaticError)); | |
884 error = ""; | |
885 RunSetZoomSettingsExpectError(tab_id, "disabled", "per-origin", &error); | |
886 EXPECT_TRUE(MatchPattern(error, | |
887 keys::kPerOriginOnlyInAutomaticError)); | |
888 } | |
889 | |
890 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, CannotZoomBogusTabId) { | |
891 const char kNewTestTabArgs[] = "about:blank"; | |
892 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), | |
Devlin
2014/06/19 21:15:29
nit: one arg per line.
wjmaclean
2014/06/20 22:01:33
Done.
| |
893 NEW_FOREGROUND_TAB, | |
Devlin
2014/06/19 21:15:29
If these params are always the same in all these t
wjmaclean
2014/06/20 22:01:33
Done.
| |
894 content::PAGE_TRANSITION_LINK, false); | |
895 content::WebContents* web_contents = browser()->OpenURL(params); | |
896 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
897 | |
898 std::string error; | |
899 int bogus_id = tab_id + 1; | |
Devlin
2014/06/19 21:15:28
nit: Just in case, maybe tab_id + 100.
wjmaclean
2014/06/20 22:01:33
Done.
| |
900 RunSetZoomExpectError(bogus_id, 3.14159, &error); | |
901 EXPECT_TRUE(MatchPattern(error, keys::kTabNotFoundError)); | |
902 | |
903 error = ""; | |
904 RunSetZoomSettingsExpectError(bogus_id, "manual", "per-tab", &error); | |
905 EXPECT_TRUE(MatchPattern(error, keys::kTabNotFoundError)); | |
906 } | |
907 | |
908 IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, CannotZoomChromeURL) { | |
Devlin
2014/06/19 21:15:29
Can we combine these last two tests into one Canno
wjmaclean
2014/06/20 22:01:32
Ok, although I'm not sure I understand the benefit
| |
909 const char kNewTestTabArgs[] = "chrome://version"; | |
910 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), | |
911 NEW_FOREGROUND_TAB, | |
912 content::PAGE_TRANSITION_LINK, false); | |
913 content::WebContents* web_contents = browser()->OpenURL(params); | |
914 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
915 | |
916 std::string error; | |
917 | |
918 // Test chrome.tabs.setZoom(). | |
919 RunSetZoomExpectError(tab_id, 3.14159, &error); | |
920 EXPECT_TRUE(MatchPattern(error, keys::kCannotZoomChromePagesError)); | |
921 | |
922 // chrome.tabs.setZoomSettings(). | |
923 RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab", &error); | |
924 EXPECT_TRUE(MatchPattern(error, | |
925 keys::kCannotChangeChromePageZoomSettingsError)); | |
926 } | |
927 | |
594 } // namespace extensions | 928 } // namespace extensions |
OLD | NEW |