OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_util.h" |
| 6 |
| 7 #include "base/i18n/rtl.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/test/scoped_feature_list.h" |
| 10 #include "chrome/browser/sync/profile_sync_test_util.h" |
| 11 #include "chrome/common/chrome_features.h" |
| 12 #include "chrome/test/base/testing_browser_process.h" |
| 13 #include "components/browser_sync/profile_sync_service.h" |
| 14 #include "components/prefs/pref_service.h" |
| 15 #include "components/sync/driver/fake_sync_service.h" |
| 16 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 class TestSyncService : public syncer::FakeSyncService { |
| 21 public: |
| 22 // FakeSyncService overrides. |
| 23 bool IsSyncAllowed() const override { return is_sync_allowed_; } |
| 24 |
| 25 void set_sync_allowed(bool sync_allowed) { is_sync_allowed_ = sync_allowed; } |
| 26 |
| 27 private: |
| 28 bool is_sync_allowed_ = true; |
| 29 DISALLOW_COPY_AND_ASSIGN(TestSyncService); |
| 30 }; |
| 31 |
| 32 } // namespace |
| 33 |
| 34 class DesktopIOSPromotionUtilTest : public testing::Test { |
| 35 public: |
| 36 DesktopIOSPromotionUtilTest() {} |
| 37 ~DesktopIOSPromotionUtilTest() override {} |
| 38 |
| 39 void SetUp() override { |
| 40 local_state_.reset(new TestingPrefServiceSimple); |
| 41 TestingBrowserProcess::GetGlobal()->SetLocalState(local_state_.get()); |
| 42 desktop_ios_promotion::RegisterLocalPrefs(local_state_->registry()); |
| 43 pref_service_ = new sync_preferences::TestingPrefServiceSyncable(); |
| 44 desktop_ios_promotion::RegisterProfilePrefs(pref_service_->registry()); |
| 45 } |
| 46 |
| 47 void TearDown() override { |
| 48 // Ensure that g_accept_requests gets set back to true after test execution. |
| 49 TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr); |
| 50 local_state_.reset(); |
| 51 } |
| 52 |
| 53 PrefService* local_state() { return local_state_.get(); } |
| 54 |
| 55 TestSyncService* sync_service() { return &fake_sync_service_; } |
| 56 sync_preferences::TestingPrefServiceSyncable* prefs() { |
| 57 return pref_service_; |
| 58 } |
| 59 |
| 60 double GetDoubleNDayOldDate(int days) { |
| 61 base::Time time_result = |
| 62 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(days); |
| 63 return time_result.ToDoubleT(); |
| 64 } |
| 65 |
| 66 protected: |
| 67 std::unique_ptr<TestingPrefServiceSimple> local_state_; |
| 68 sync_preferences::TestingPrefServiceSyncable* pref_service_; |
| 69 TestSyncService fake_sync_service_; |
| 70 |
| 71 private: |
| 72 DISALLOW_COPY_AND_ASSIGN(DesktopIOSPromotionUtilTest); |
| 73 }; |
| 74 |
| 75 TEST_F(DesktopIOSPromotionUtilTest, IsEligibleForIOSPromotionForSavePassword) { |
| 76 desktop_ios_promotion::PromotionEntryPoint entry_point = |
| 77 desktop_ios_promotion::PromotionEntryPoint::SAVE_PASSWORD_BUBBLE; |
| 78 // By default the promo is off. |
| 79 EXPECT_FALSE(desktop_ios_promotion::IsEligibleForIOSPromotion( |
| 80 prefs(), nullptr, entry_point)); |
| 81 base::test::ScopedFeatureList scoped_feature_list; |
| 82 scoped_feature_list.InitAndEnableFeature(features::kDesktopIOSPromotion); |
| 83 std::string locales[] = {"en-US", "en-CA", "en-AU", "es-US"}; |
| 84 constexpr struct { |
| 85 bool is_sync_allowed; |
| 86 int locale_index; |
| 87 bool is_dismissed; |
| 88 int show_count; |
| 89 int last_impression_days; |
| 90 int sms_entrypoint; |
| 91 bool is_user_eligible; |
| 92 bool promo_done; |
| 93 bool result; |
| 94 } kTestData[] = { |
| 95 // {sync allowed, locale, dismissed before, impression count, seen days |
| 96 // ago, bitmask with entry points seen, is user eligible, flow was |
| 97 // completed before, expected result } |
| 98 {false, 0, false, 0, 1, 0, false, false, false}, |
| 99 {false, 1, false, 0, 3, 0, true, false, false}, |
| 100 {true, 3, false, 0, 4, 0, true, false, false}, |
| 101 {true, 2, false, 0, 10, 0, true, false, false}, |
| 102 {true, 0, true, 1, 3, 0, true, false, false}, |
| 103 {true, 0, false, 3, 1, 0, true, false, false}, |
| 104 {true, 0, false, 1, 3, 2, true, false, false}, |
| 105 {true, 0, false, 0, 4, 4, true, false, false}, |
| 106 {true, 0, false, 1, 10, 0, false, false, false}, |
| 107 {true, 0, false, 0, 1, 0, true, true, false}, |
| 108 {true, 1, false, 1, 1, 0, true, false, true}, |
| 109 {true, 1, false, 0, 2, 0, true, false, true}, |
| 110 {true, 0, false, 0, 8, 2, true, false, true}, |
| 111 }; |
| 112 std::string locale = base::i18n::GetConfiguredLocale(); |
| 113 for (const auto& test_case : kTestData) { |
| 114 SCOPED_TRACE(testing::Message("#test_case = ") << (&test_case - kTestData)); |
| 115 sync_service()->set_sync_allowed(test_case.is_sync_allowed); |
| 116 local_state()->SetBoolean(prefs::kSavePasswordsBubbleIOSPromoDismissed, |
| 117 test_case.is_dismissed); |
| 118 local_state()->SetInteger(prefs::kNumberSavePasswordsBubbleIOSPromoShown, |
| 119 test_case.show_count); |
| 120 base::i18n::SetICUDefaultLocale(locales[test_case.locale_index]); |
| 121 prefs()->SetDouble(prefs::kIOSPromotionLastImpression, |
| 122 GetDoubleNDayOldDate(test_case.last_impression_days)); |
| 123 prefs()->SetInteger(prefs::kIOSPromotionSMSEntryPoint, |
| 124 test_case.sms_entrypoint); |
| 125 prefs()->SetBoolean(prefs::kIOSPromotionEligible, |
| 126 test_case.is_user_eligible); |
| 127 prefs()->SetBoolean(prefs::kIOSPromotionDone, test_case.promo_done); |
| 128 EXPECT_EQ(test_case.result, |
| 129 IsEligibleForIOSPromotion(prefs(), sync_service(), entry_point)); |
| 130 } |
| 131 base::i18n::SetICUDefaultLocale(locale); |
| 132 } |
OLD | NEW |