| 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_controll
er.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/test/histogram_tester.h" | |
| 9 #include "base/test/test_timeouts.h" | |
| 10 #include "chrome/browser/prefs/browser_prefs.h" | |
| 11 #include "chrome/browser/sync/profile_sync_test_util.h" | |
| 12 #include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_util.h" | |
| 13 #include "chrome/browser/ui/desktop_ios_promotion/sms_service.h" | |
| 14 #include "chrome/browser/ui/desktop_ios_promotion/sms_service_factory.h" | |
| 15 #include "chrome/test/base/testing_browser_process.h" | |
| 16 #include "chrome/test/base/testing_profile.h" | |
| 17 #include "components/browser_sync/profile_sync_service.h" | |
| 18 #include "components/pref_registry/pref_registry_syncable.h" | |
| 19 #include "components/prefs/pref_notifier_impl.h" | |
| 20 #include "components/prefs/pref_service.h" | |
| 21 #include "components/prefs/testing_pref_service.h" | |
| 22 #include "components/sync/driver/fake_sync_service.h" | |
| 23 #include "components/sync_preferences/pref_service_mock_factory.h" | |
| 24 #include "components/sync_preferences/testing_pref_service_syncable.h" | |
| 25 #include "content/public/test/test_browser_thread_bundle.h" | |
| 26 #include "testing/gmock/include/gmock/gmock.h" | |
| 27 #include "testing/gtest/include/gtest/gtest.h" | |
| 28 | |
| 29 using ::testing::_; | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 class FakeSMSService : public SMSService { | |
| 34 public: | |
| 35 FakeSMSService() : SMSService(nullptr, nullptr, nullptr) {} | |
| 36 ~FakeSMSService() override {} | |
| 37 MOCK_METHOD1(QueryPhoneNumber, void(const PhoneNumberCallback&)); | |
| 38 MOCK_METHOD2(SendSMS, | |
| 39 void(const std::string&, | |
| 40 const SMSService::PhoneNumberCallback&)); | |
| 41 | |
| 42 private: | |
| 43 DISALLOW_COPY_AND_ASSIGN(FakeSMSService); | |
| 44 }; | |
| 45 | |
| 46 std::unique_ptr<KeyedService> BuildFakeSMSService( | |
| 47 content::BrowserContext* profile) { | |
| 48 return base::MakeUnique<FakeSMSService>(); | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 class DesktopIOSPromotionControllerTest : public testing::Test { | |
| 54 public: | |
| 55 DesktopIOSPromotionControllerTest() {} | |
| 56 ~DesktopIOSPromotionControllerTest() override {} | |
| 57 | |
| 58 void SetUp() override { | |
| 59 pref_service_ = | |
| 60 base::MakeUnique<sync_preferences::TestingPrefServiceSyncable>( | |
| 61 new TestingPrefStore(), new TestingPrefStore(), | |
| 62 new TestingPrefStore(), new TestingPrefStore(), | |
| 63 new user_prefs::PrefRegistrySyncable(), new PrefNotifierImpl()); | |
| 64 chrome::RegisterUserProfilePrefs(pref_service_->registry()); | |
| 65 TestingProfile::Builder builder; | |
| 66 builder.SetPrefService(std::move(pref_service_)); | |
| 67 builder.AddTestingFactory(SMSServiceFactory::GetInstance(), | |
| 68 BuildFakeSMSService); | |
| 69 profile_ = builder.Build(); | |
| 70 local_state_ = base::MakeUnique<TestingPrefServiceSimple>(); | |
| 71 TestingBrowserProcess::GetGlobal()->SetLocalState(local_state_.get()); | |
| 72 desktop_ios_promotion::RegisterLocalPrefs(local_state_->registry()); | |
| 73 sms_service_ = static_cast<FakeSMSService*>( | |
| 74 SMSServiceFactory::GetForProfile(profile_.get())); | |
| 75 } | |
| 76 | |
| 77 void TearDown() override { | |
| 78 TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr); | |
| 79 local_state_.reset(); | |
| 80 controller_.reset(); | |
| 81 profile_.reset(); | |
| 82 } | |
| 83 | |
| 84 void InitController(desktop_ios_promotion::PromotionEntryPoint entry_point) { | |
| 85 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(sms_service_)); | |
| 86 EXPECT_CALL(*sms_service_, QueryPhoneNumber(_)); | |
| 87 controller_ = base::MakeUnique<DesktopIOSPromotionController>( | |
| 88 profile_.get(), nullptr, entry_point); | |
| 89 } | |
| 90 | |
| 91 PrefService* prefs() { return profile_->GetPrefs(); } | |
| 92 | |
| 93 protected: | |
| 94 FakeSMSService* sms_service_ = nullptr; | |
| 95 content::TestBrowserThreadBundle thread_bundle_; | |
| 96 std::unique_ptr<TestingPrefServiceSimple> local_state_; | |
| 97 std::unique_ptr<DesktopIOSPromotionController> controller_; | |
| 98 std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> pref_service_; | |
| 99 std::unique_ptr<TestingProfile> profile_; | |
| 100 | |
| 101 private: | |
| 102 DISALLOW_COPY_AND_ASSIGN(DesktopIOSPromotionControllerTest); | |
| 103 }; | |
| 104 | |
| 105 TEST_F(DesktopIOSPromotionControllerTest, ClickSendSMS) { | |
| 106 InitController( | |
| 107 desktop_ios_promotion::PromotionEntryPoint::SAVE_PASSWORD_BUBBLE); | |
| 108 EXPECT_CALL(*sms_service_, SendSMS(_, _)); | |
| 109 controller_->OnSendSMSClicked(); | |
| 110 EXPECT_EQ(desktop_ios_promotion::PromotionDismissalReason::SEND_SMS, | |
| 111 controller_->dismissal_reason()); | |
| 112 EXPECT_EQ(1, prefs()->GetInteger(prefs::kIOSPromotionSMSEntryPoint)); | |
| 113 } | |
| 114 | |
| 115 TEST_F(DesktopIOSPromotionControllerTest, PromotionShown) { | |
| 116 const char kHistogram[] = "DesktopIOSPromotion.ImpressionFromEntryPoint"; | |
| 117 base::HistogramTester histograms; | |
| 118 desktop_ios_promotion::PromotionEntryPoint entry_point = | |
| 119 desktop_ios_promotion::PromotionEntryPoint::SAVE_PASSWORD_BUBBLE; | |
| 120 InitController(entry_point); | |
| 121 | |
| 122 EXPECT_EQ( | |
| 123 0, | |
| 124 local_state_->GetInteger(prefs::kNumberSavePasswordsBubbleIOSPromoShown)); | |
| 125 EXPECT_EQ(0, prefs()->GetInteger(prefs::kIOSPromotionShownEntryPoints)); | |
| 126 controller_->OnPromotionShown(); | |
| 127 // Impressions increase. | |
| 128 EXPECT_EQ( | |
| 129 1, | |
| 130 local_state_->GetInteger(prefs::kNumberSavePasswordsBubbleIOSPromoShown)); | |
| 131 double lst_impr = prefs()->GetDouble(prefs::kIOSPromotionLastImpression); | |
| 132 // last impression time updated correctly. | |
| 133 EXPECT_LT(base::Time::Now() - base::Time::FromDoubleT(lst_impr), | |
| 134 TestTimeouts::action_timeout()); | |
| 135 // We reset the SMS entry point as the user is is still eligible and haven't | |
| 136 // seen the promotion for 7 days. | |
| 137 EXPECT_EQ(0, prefs()->GetInteger(prefs::kIOSPromotionSMSEntryPoint)); | |
| 138 // Check if the impression is logged to histograms. | |
| 139 histograms.ExpectUniqueSample(kHistogram, static_cast<int>(entry_point), 1); | |
| 140 // Check if the bit for this entry point was set in profile prefs. | |
| 141 EXPECT_EQ(1 << static_cast<int>(entry_point), | |
| 142 prefs()->GetInteger(prefs::kIOSPromotionShownEntryPoints)); | |
| 143 int shown_promotions = | |
| 144 prefs()->GetInteger(prefs::kIOSPromotionShownEntryPoints); | |
| 145 | |
| 146 controller_->OnPromotionShown(); | |
| 147 EXPECT_EQ( | |
| 148 2, | |
| 149 local_state_->GetInteger(prefs::kNumberSavePasswordsBubbleIOSPromoShown)); | |
| 150 histograms.ExpectUniqueSample(kHistogram, static_cast<int>(entry_point), 2); | |
| 151 | |
| 152 // Check different entry point. | |
| 153 entry_point = desktop_ios_promotion::PromotionEntryPoint::BOOKMARKS_BUBBLE; | |
| 154 InitController(entry_point); | |
| 155 controller_->OnPromotionShown(); | |
| 156 histograms.ExpectBucketCount(kHistogram, static_cast<int>(entry_point), 1); | |
| 157 histograms.ExpectTotalCount(kHistogram, 3); | |
| 158 | |
| 159 // Check if the bit for this entry point was set in profile prefs. | |
| 160 EXPECT_EQ(shown_promotions | (1 << static_cast<int>(entry_point)), | |
| 161 prefs()->GetInteger(prefs::kIOSPromotionShownEntryPoints)); | |
| 162 } | |
| 163 | |
| 164 TEST_F(DesktopIOSPromotionControllerTest, ClickNoThanks) { | |
| 165 InitController( | |
| 166 desktop_ios_promotion::PromotionEntryPoint::SAVE_PASSWORD_BUBBLE); | |
| 167 controller_->OnNoThanksClicked(); | |
| 168 EXPECT_EQ(desktop_ios_promotion::PromotionDismissalReason::NO_THANKS, | |
| 169 controller_->dismissal_reason()); | |
| 170 EXPECT_EQ( | |
| 171 true, | |
| 172 local_state_->GetBoolean(prefs::kSavePasswordsBubbleIOSPromoDismissed)); | |
| 173 } | |
| OLD | NEW |