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 ZoomExtensionAPITestHelper { | |
not at google - send to devlin
2014/04/11 15:04:25
a more convenient/common pattern is to have
class
paulmeyer
2014/04/11 19:09:49
Done.
| |
596 public: | |
597 explicit ZoomExtensionAPITestHelper(Browser* browser) | |
598 : browser_(browser), | |
599 extension_(utils::CreateEmptyExtension()) {} | |
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 void 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 | |
not at google - send to devlin
2014/04/11 15:04:25
ASSERT_TRUE(get_zoom_result.get());
though an eve
paulmeyer
2014/04/11 19:09:49
Done.
| |
625 get_zoom_result->GetAsDouble(zoom_factor); | |
626 } | |
627 | |
628 // Runs chrome.tabs.setZoomSettings(). | |
629 void RunSetZoomSettings(int tab_id, | |
630 const char* mode, | |
631 const char* scope) { | |
632 scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( | |
633 new TabsSetZoomSettingsFunction()); | |
634 set_zoom_settings_function->set_extension(extension_); | |
635 | |
636 if (scope) { | |
not at google - send to devlin
2014/04/11 15:04:25
instead, can you do like
std::string args;
if (sc
paulmeyer
2014/04/11 19:09:49
Done.
| |
637 utils::RunFunction(set_zoom_settings_function.get(), | |
638 base::StringPrintf("[%u, {\"mode\": \"%s\", " | |
639 "\"scope\": \"%s\"}]", | |
640 tab_id, mode, scope), | |
641 browser_, extension_function_test_utils::NONE); | |
642 } else { | |
643 utils::RunFunction(set_zoom_settings_function.get(), | |
644 base::StringPrintf("[%u, {\"mode\": \"%s\"}]", | |
645 tab_id, mode), | |
646 browser_, extension_function_test_utils::NONE); | |
647 } | |
648 } | |
649 | |
650 // Runs chrome.tabs.getZoomSettings(). | |
651 void RunGetZoomSettings(int tab_id, std::string* mode, std::string* scope) { | |
652 scoped_refptr<TabsGetZoomSettingsFunction> get_zoom_settings_function( | |
653 new TabsGetZoomSettingsFunction()); | |
654 get_zoom_settings_function->set_extension(extension_); | |
655 get_zoom_settings_function->set_has_callback(true); | |
656 | |
657 scoped_ptr<base::DictionaryValue> get_zoom_settings_result( | |
658 utils::ToDictionary(utils::RunFunctionAndReturnSingleResult( | |
659 get_zoom_settings_function.get(), | |
660 base::StringPrintf("[%u]", tab_id), | |
661 browser_))); | |
662 | |
663 *mode = utils::GetString(get_zoom_settings_result.get(), "mode"); | |
664 *scope = utils::GetString(get_zoom_settings_result.get(), "scope"); | |
665 } | |
666 | |
667 // Runs chrome.tabs.setZoom(), expecting an error. | |
668 void RunSetZoomExpectError(int tab_id, | |
669 double zoom_factor, | |
670 std::string* error) { | |
671 scoped_refptr<TabsSetZoomFunction> set_zoom_function( | |
672 new TabsSetZoomFunction()); | |
673 set_zoom_function->set_extension(extension_); | |
674 set_zoom_function->set_has_callback(true); | |
675 | |
676 *error = utils::RunFunctionAndReturnError( | |
677 set_zoom_function.get(), | |
678 base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), | |
679 browser_); | |
680 } | |
681 | |
682 // Runs chrome.tabs.setZoomSettings(), expecting an error. | |
683 void RunSetZoomSettingsExpectError(int tab_id, | |
not at google - send to devlin
2014/04/11 15:04:25
ideally you'd just have a single general function
paulmeyer
2014/04/11 19:09:49
Yes, the structure doesn't allow for that.
On 201
| |
684 const char* mode, | |
685 const char* scope, | |
686 std::string* error) { | |
687 scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( | |
688 new TabsSetZoomSettingsFunction()); | |
689 set_zoom_settings_function->set_extension(extension_); | |
690 | |
691 *error = utils::RunFunctionAndReturnError( | |
692 set_zoom_settings_function.get(), | |
693 base::StringPrintf("[%u, {\"mode\": \"%s\", " | |
694 "\"scope\": \"%s\"}]", | |
695 tab_id, mode, scope), | |
696 browser_); | |
697 } | |
698 | |
699 private: | |
700 Browser* browser_; | |
701 scoped_refptr<Extension> extension_; | |
702 }; | |
703 | |
704 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, SetAndGetZoom) { | |
705 ZoomExtensionAPITestHelper helper(browser()); | |
706 static const char kNewTestTabArgs[] = "about:blank"; | |
not at google - send to devlin
2014/04/11 15:04:25
there isn't any advantage having this as "static".
paulmeyer
2014/04/11 19:09:49
Done.
| |
707 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), | |
708 NEW_FOREGROUND_TAB, | |
709 content::PAGE_TRANSITION_LINK, false); | |
710 content::WebContents* web_contents = browser()->OpenURL(params); | |
711 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
712 | |
713 // Test chrome.tabs.setZoom(). | |
714 helper.RunSetZoom(tab_id, 0.8); | |
715 EXPECT_EQ(0.8, content::ZoomLevelToZoomFactor(web_contents->GetZoomLevel())); | |
716 | |
717 // Test chrome.tabs.getZoom(). | |
718 double zoom_factor = -1; | |
719 helper.RunGetZoom(tab_id, &zoom_factor); | |
720 EXPECT_EQ(0.8, zoom_factor); | |
721 } | |
722 | |
723 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, ZoomSettings) { | |
724 ZoomExtensionAPITestHelper helper(browser()); | |
725 | |
726 static const char kNewTestTabArgsA[] = "data:text/html,A"; | |
727 static const char kNewTestTabArgsB[] = "data:text/html,B"; | |
728 | |
729 content::OpenURLParams params_A(GURL(kNewTestTabArgsA), content::Referrer(), | |
730 NEW_FOREGROUND_TAB, | |
731 content::PAGE_TRANSITION_LINK, false); | |
732 content::OpenURLParams params_B(GURL(kNewTestTabArgsB), content::Referrer(), | |
733 NEW_FOREGROUND_TAB, | |
734 content::PAGE_TRANSITION_LINK, false); | |
735 | |
736 // Tabs A1 and A2 are navigated to the same origin, while B is navigated | |
737 // to a different one. | |
738 content::WebContents* web_contents_A1 = browser()->OpenURL(params_A); | |
739 content::WebContents* web_contents_A2 = browser()->OpenURL(params_A); | |
740 content::WebContents* web_contents_B = browser()->OpenURL(params_B); | |
741 int tab_id_A1 = ExtensionTabUtil::GetTabId(web_contents_A1); | |
742 int tab_id_A2 = ExtensionTabUtil::GetTabId(web_contents_A2); | |
743 int tab_id_B = ExtensionTabUtil::GetTabId(web_contents_B); | |
744 | |
745 // Test per-origin automatic zoom settings. | |
746 helper.RunSetZoom(tab_id_B, 1); | |
747 helper.RunSetZoom(tab_id_A2, 1.1); | |
748 EXPECT_EQ(1.1, | |
749 content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); | |
750 EXPECT_EQ(1.1, | |
751 content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); | |
752 EXPECT_EQ(1, | |
753 content::ZoomLevelToZoomFactor(web_contents_B->GetZoomLevel())); | |
754 | |
755 // Test per-tab automatic zoom settings. | |
756 helper.RunSetZoomSettings(tab_id_A1, "automatic", "per-tab"); | |
757 helper.RunSetZoom(tab_id_A1, 1.2); | |
758 EXPECT_EQ(1.2, | |
759 content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); | |
760 EXPECT_EQ(1.1, | |
761 content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); | |
762 | |
763 // Test 'manual' mode. | |
764 helper.RunSetZoomSettings(tab_id_A1, "manual", NULL); | |
765 helper.RunSetZoom(tab_id_A1, 1.3); | |
766 EXPECT_EQ(1.3, | |
767 content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); | |
768 EXPECT_EQ(1.1, | |
769 content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); | |
770 | |
771 // Test 'disabled' mode. | |
772 helper.RunSetZoomSettings(tab_id_A1, "disabled", NULL); | |
773 helper.RunSetZoom(tab_id_A2, 1.4); | |
774 EXPECT_EQ(1, | |
775 content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); | |
776 EXPECT_EQ(1.4, | |
777 content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); | |
778 } | |
779 | |
780 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetZoomSettings) { | |
781 ZoomExtensionAPITestHelper helper(browser()); | |
782 static const char kNewTestTabArgs[] = "about:blank"; | |
783 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), | |
784 NEW_FOREGROUND_TAB, | |
785 content::PAGE_TRANSITION_LINK, false); | |
786 content::WebContents* web_contents = browser()->OpenURL(params); | |
787 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
788 | |
789 helper.RunSetZoomSettings(tab_id, "automatic", "per-tab"); | |
790 std::string mode; | |
791 std::string scope; | |
792 helper.RunGetZoomSettings(tab_id, &mode, &scope); | |
793 | |
794 EXPECT_EQ("automatic", mode); | |
795 EXPECT_EQ("per-tab", scope); | |
796 } | |
797 | |
798 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, CannotZoomChromeURL) { | |
799 ZoomExtensionAPITestHelper helper(browser()); | |
800 static const char kNewTestTabArgs[] = "chrome://version"; | |
801 content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), | |
802 NEW_FOREGROUND_TAB, | |
803 content::PAGE_TRANSITION_LINK, false); | |
804 content::WebContents* web_contents = browser()->OpenURL(params); | |
805 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
806 | |
807 std::string error; | |
808 | |
809 // Test chrome.tabs.setZoom(). | |
810 helper.RunSetZoomExpectError(tab_id, 3.14159, &error); | |
811 EXPECT_TRUE(MatchPattern(error, keys::kCannotZoomChromePagesError)); | |
812 | |
813 // chrome.tabs.setZoomSettings(). | |
814 helper.RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab", &error); | |
815 EXPECT_TRUE(MatchPattern(error, | |
816 keys::kCannotChangeChromePageZoomSettingsError)); | |
817 } | |
818 | |
593 } // namespace extensions | 819 } // namespace extensions |
OLD | NEW |