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 #include "base/prefs/pref_service.h" | 5 #include "base/prefs/pref_service.h" |
6 #include "base/prefs/scoped_user_pref_update.h" | 6 #include "base/prefs/scoped_user_pref_update.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/content_settings/cookie_settings.h" | 10 #include "chrome/browser/content_settings/cookie_settings.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 #include "grit/generated_resources.h" | 44 #include "grit/generated_resources.h" |
45 #include "testing/gtest/include/gtest/gtest.h" | 45 #include "testing/gtest/include/gtest/gtest.h" |
46 #include "ui/base/l10n/l10n_util.h" | 46 #include "ui/base/l10n/l10n_util.h" |
47 | 47 |
48 using ::testing::_; | 48 using ::testing::_; |
49 using ::testing::AtLeast; | 49 using ::testing::AtLeast; |
50 using ::testing::Return; | 50 using ::testing::Return; |
51 | 51 |
52 namespace { | 52 namespace { |
53 | 53 |
54 // Explicit URLs are sign in URLs created by chrome for specific sign in access | |
55 // points. Implicit URLs are those to sign for some Google service, like gmail | |
56 // or drive. In former case, with a valid URL, we don't want to offer the | |
57 // interstitial. In all other cases we do. | |
58 | |
59 const char kImplicitURLString[] = | |
60 "https://accounts.google.com/ServiceLogin" | |
61 "?service=foo&continue=http://foo.google.com"; | |
62 | |
63 class SigninManagerMock : public FakeSigninManager { | 54 class SigninManagerMock : public FakeSigninManager { |
64 public: | 55 public: |
65 explicit SigninManagerMock(Profile* profile) : FakeSigninManager(profile) { | 56 explicit SigninManagerMock(Profile* profile) : FakeSigninManager(profile) { |
66 Initialize(profile, NULL); | 57 Initialize(profile, NULL); |
67 } | 58 } |
68 MOCK_CONST_METHOD1(IsAllowedUsername, bool(const std::string& username)); | 59 MOCK_CONST_METHOD1(IsAllowedUsername, bool(const std::string& username)); |
69 }; | 60 }; |
70 | 61 |
71 static BrowserContextKeyedService* BuildSigninManagerMock( | 62 static BrowserContextKeyedService* BuildSigninManagerMock( |
72 content::BrowserContext* profile) { | 63 content::BrowserContext* profile) { |
(...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 EXPECT_CALL(*sync_service, RemoveObserver(helper)); | 732 EXPECT_CALL(*sync_service, RemoveObserver(helper)); |
742 SetContents(NULL); | 733 SetContents(NULL); |
743 } | 734 } |
744 | 735 |
745 // I/O thread tests | 736 // I/O thread tests |
746 | 737 |
747 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThread) { | 738 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThread) { |
748 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); | 739 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); |
749 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, | 740 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, |
750 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 741 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
751 valid_gaia_url_, std::string(), &request_, io_data.get())); | 742 valid_gaia_url_, &request_, io_data.get())); |
752 } | 743 } |
753 | 744 |
754 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadIncognito) { | 745 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadIncognito) { |
755 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(true)); | 746 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(true)); |
756 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, | 747 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, |
757 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 748 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
758 valid_gaia_url_, std::string(), &request_, io_data.get())); | 749 valid_gaia_url_, &request_, io_data.get())); |
759 } | 750 } |
760 | 751 |
761 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadNoIOData) { | 752 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadNoIOData) { |
762 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, | 753 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, |
763 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 754 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
764 valid_gaia_url_, std::string(), &request_, NULL)); | 755 valid_gaia_url_, &request_, NULL)); |
765 } | 756 } |
766 | 757 |
767 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadBadURL) { | 758 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadBadURL) { |
768 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); | 759 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); |
769 EXPECT_EQ( | 760 EXPECT_EQ( |
770 OneClickSigninHelper::IGNORE_REQUEST, | 761 OneClickSigninHelper::IGNORE_REQUEST, |
771 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 762 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
772 GURL("https://foo.com/"), std::string(), &request_, io_data.get())); | 763 GURL("https://foo.com/"), &request_, io_data.get())); |
773 EXPECT_EQ(OneClickSigninHelper::IGNORE_REQUEST, | 764 EXPECT_EQ(OneClickSigninHelper::IGNORE_REQUEST, |
774 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 765 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
775 GURL("http://accounts.google.com/"), | 766 GURL("http://accounts.google.com/"), |
776 std::string(), | |
777 &request_, | 767 &request_, |
778 io_data.get())); | 768 io_data.get())); |
779 } | 769 } |
780 | 770 |
781 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadReferrer) { | |
782 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); | |
783 std::string continue_url(signin::GetPromoURL( | |
784 signin::SOURCE_START_PAGE, false).spec()); | |
785 | |
786 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, | |
787 OneClickSigninHelper::CanOfferOnIOThreadImpl( | |
788 valid_gaia_url_, continue_url, &request_, io_data.get())); | |
789 | |
790 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, | |
791 OneClickSigninHelper::CanOfferOnIOThreadImpl( | |
792 valid_gaia_url_, kImplicitURLString, &request_, io_data.get())); | |
793 | |
794 std::string bad_url_1 = continue_url; | |
795 const std::string service_name = "chromiumsync"; | |
796 bad_url_1.replace(bad_url_1.find(service_name), service_name.length(), | |
797 "foo"); | |
798 | |
799 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, | |
800 OneClickSigninHelper::CanOfferOnIOThreadImpl( | |
801 valid_gaia_url_, bad_url_1, &request_, io_data.get())); | |
802 | |
803 std::string bad_url_2 = continue_url; | |
804 const std::string source_num = "%3D0"; | |
805 bad_url_2.replace(bad_url_1.find(source_num), source_num.length(), "%3D10"); | |
806 | |
807 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, | |
808 OneClickSigninHelper::CanOfferOnIOThreadImpl( | |
809 valid_gaia_url_, bad_url_2, &request_, io_data.get())); | |
810 | |
811 std::string bad_url_3 = continue_url; | |
812 const std::string source = "source%3D0"; | |
813 bad_url_3.erase(bad_url_1.find(source), source.length()); | |
814 | |
815 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, | |
816 OneClickSigninHelper::CanOfferOnIOThreadImpl( | |
817 valid_gaia_url_, bad_url_3, &request_, io_data.get())); | |
818 } | |
819 | |
820 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadDisabled) { | 771 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadDisabled) { |
821 EnableOneClick(false); | 772 EnableOneClick(false); |
822 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); | 773 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); |
823 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, | 774 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, |
824 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 775 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
825 valid_gaia_url_, std::string(), &request_, io_data.get())); | 776 valid_gaia_url_, &request_, io_data.get())); |
826 } | 777 } |
827 | 778 |
828 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadSignedIn) { | 779 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadSignedIn) { |
829 PrefService* pref_service = profile()->GetPrefs(); | 780 PrefService* pref_service = profile()->GetPrefs(); |
830 pref_service->SetString(prefs::kGoogleServicesUsername, "user@gmail.com"); | 781 pref_service->SetString(prefs::kGoogleServicesUsername, "user@gmail.com"); |
831 | 782 |
832 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); | 783 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); |
833 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, | 784 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, |
834 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 785 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
835 valid_gaia_url_, std::string(), &request_, io_data.get())); | 786 valid_gaia_url_, &request_, io_data.get())); |
836 } | 787 } |
837 | 788 |
838 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadEmailNotAllowed) { | 789 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadEmailNotAllowed) { |
839 SetAllowedUsernamePattern("*@example.com"); | 790 SetAllowedUsernamePattern("*@example.com"); |
840 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); | 791 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); |
841 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, | 792 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, |
842 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 793 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
843 valid_gaia_url_, std::string(), &request_, io_data.get())); | 794 valid_gaia_url_, &request_, io_data.get())); |
844 } | 795 } |
845 | 796 |
846 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadEmailAlreadyUsed) { | 797 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadEmailAlreadyUsed) { |
847 ProfileInfoCache* cache = testing_profile_manager_.profile_info_cache(); | 798 ProfileInfoCache* cache = testing_profile_manager_.profile_info_cache(); |
848 const base::FilePath& user_data_dir = cache->GetUserDataDir(); | 799 const base::FilePath& user_data_dir = cache->GetUserDataDir(); |
849 cache->AddProfileToCache(user_data_dir.Append(FILE_PATH_LITERAL("user")), | 800 cache->AddProfileToCache(user_data_dir.Append(FILE_PATH_LITERAL("user")), |
850 UTF8ToUTF16("user"), | 801 UTF8ToUTF16("user"), |
851 UTF8ToUTF16("user@gmail.com"), 0, std::string()); | 802 UTF8ToUTF16("user@gmail.com"), 0, std::string()); |
852 | 803 |
853 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); | 804 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); |
854 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, | 805 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, |
855 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 806 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
856 valid_gaia_url_, std::string(), &request_, io_data.get())); | 807 valid_gaia_url_, &request_, io_data.get())); |
857 } | 808 } |
858 | 809 |
859 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadWithRejectedEmail) { | 810 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadWithRejectedEmail) { |
860 AddEmailToOneClickRejectedList("user@gmail.com"); | 811 AddEmailToOneClickRejectedList("user@gmail.com"); |
861 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); | 812 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); |
862 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, | 813 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, |
863 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 814 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
864 valid_gaia_url_, std::string(), &request_, io_data.get())); | 815 valid_gaia_url_, &request_, io_data.get())); |
865 } | 816 } |
866 | 817 |
867 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadNoSigninCookies) { | 818 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadNoSigninCookies) { |
868 AllowSigninCookies(false); | 819 AllowSigninCookies(false); |
869 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); | 820 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); |
870 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, | 821 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, |
871 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 822 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
872 valid_gaia_url_, std::string(), &request_, io_data.get())); | 823 valid_gaia_url_, &request_, io_data.get())); |
873 } | 824 } |
874 | 825 |
875 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadDisabledByPolicy) { | 826 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadDisabledByPolicy) { |
876 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); | 827 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false)); |
877 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, | 828 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, |
878 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 829 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
879 valid_gaia_url_, std::string(), &request_, io_data.get())); | 830 valid_gaia_url_, &request_, io_data.get())); |
880 | 831 |
881 // Simulate a policy disabling signin by writing kSigninAllowed directly. | 832 // Simulate a policy disabling signin by writing kSigninAllowed directly. |
882 // We should not offer to sign in the browser. | 833 // We should not offer to sign in the browser. |
883 profile()->GetTestingPrefService()->SetManagedPref( | 834 profile()->GetTestingPrefService()->SetManagedPref( |
884 prefs::kSigninAllowed, base::Value::CreateBooleanValue(false)); | 835 prefs::kSigninAllowed, base::Value::CreateBooleanValue(false)); |
885 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, | 836 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER, |
886 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 837 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
887 valid_gaia_url_, std::string(), &request_, io_data.get())); | 838 valid_gaia_url_, &request_, io_data.get())); |
888 | 839 |
889 // Reset the preference. | 840 // Reset the preference. |
890 profile()->GetTestingPrefService()->SetManagedPref( | 841 profile()->GetTestingPrefService()->SetManagedPref( |
891 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true)); | 842 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true)); |
892 | 843 |
893 // Simulate a policy disabling sync by writing kSyncManaged directly. | 844 // Simulate a policy disabling sync by writing kSyncManaged directly. |
894 // We should still offer to sign in the browser. | 845 // We should still offer to sign in the browser. |
895 profile()->GetTestingPrefService()->SetManagedPref( | 846 profile()->GetTestingPrefService()->SetManagedPref( |
896 prefs::kSyncManaged, base::Value::CreateBooleanValue(true)); | 847 prefs::kSyncManaged, base::Value::CreateBooleanValue(true)); |
897 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, | 848 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, |
898 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 849 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
899 valid_gaia_url_, std::string(), &request_, io_data.get())); | 850 valid_gaia_url_, &request_, io_data.get())); |
900 } | 851 } |
OLD | NEW |