OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/signin/cross_device_promo.h" |
| 6 |
| 7 #include "base/metrics/field_trial.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/test/histogram_tester.h" |
| 10 #include "chrome/browser/prefs/browser_prefs.h" |
| 11 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 12 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
| 13 #include "chrome/browser/signin/cross_device_promo_factory.h" |
| 14 #include "chrome/browser/signin/fake_gaia_cookie_manager_service.h" |
| 15 #include "chrome/browser/signin/fake_signin_manager.h" |
| 16 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h" |
| 17 #include "chrome/browser/signin/signin_manager_factory.h" |
| 18 #include "chrome/browser/signin/test_signin_client_builder.h" |
| 19 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/test/base/testing_browser_process.h" |
| 21 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 22 #include "chrome/test/base/testing_profile.h" |
| 23 #include "chrome/test/base/testing_profile_manager.h" |
| 24 #include "components/signin/core/browser/signin_manager.h" |
| 25 #include "components/signin/core/browser/signin_metrics.h" |
| 26 #include "components/signin/core/browser/test_signin_client.h" |
| 27 #include "components/variations/entropy_provider.h" |
| 28 #include "components/variations/variations_associated_data.h" |
| 29 #include "content/public/test/test_browser_thread_bundle.h" |
| 30 #include "google_apis/gaia/gaia_urls.h" |
| 31 #include "net/url_request/test_url_fetcher_factory.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" |
| 33 |
| 34 namespace { |
| 35 |
| 36 int64 InOneHour() { |
| 37 return (base::Time::Now() + base::TimeDelta::FromHours(1)).ToInternalValue(); |
| 38 } |
| 39 |
| 40 } |
| 41 |
| 42 class CrossDevicePromoObserver : public CrossDevicePromo::Observer { |
| 43 public: |
| 44 explicit CrossDevicePromoObserver(CrossDevicePromo* promo) : |
| 45 active_(false), set_active_(0), set_inactive_(0), promo_(promo) { |
| 46 promo->AddObserver(this); |
| 47 } |
| 48 |
| 49 ~CrossDevicePromoObserver() { |
| 50 promo_->RemoveObserver(this); |
| 51 } |
| 52 |
| 53 void OnPromoActivationChanged(bool active) override { |
| 54 active_ = active; |
| 55 active ? set_active_++ : set_inactive_++; |
| 56 } |
| 57 |
| 58 bool IsActive() { return active_; } |
| 59 int TimesBecameActive() { return set_active_; } |
| 60 int TimesBecameInactive() { return set_inactive_; } |
| 61 |
| 62 private: |
| 63 bool active_; |
| 64 int set_active_; |
| 65 int set_inactive_; |
| 66 CrossDevicePromo* promo_; |
| 67 }; |
| 68 |
| 69 class CrossDevicePromoTest : public ::testing::Test { |
| 70 public: |
| 71 CrossDevicePromoTest(); |
| 72 void SetUp() override; |
| 73 |
| 74 void ResetFieldTrialList(); |
| 75 |
| 76 CrossDevicePromo* promo() { return cross_device_promo_; } |
| 77 TestingProfile* profile() { return profile_; } |
| 78 FakeSigninManagerForTesting* signin_manager() { return signin_manager_; } |
| 79 base::HistogramTester* histogram_tester() { return &histogram_tester_; } |
| 80 TestingPrefServiceSyncable* prefs() { return pref_service_; } |
| 81 FakeGaiaCookieManagerService* cookie_manager_service() { |
| 82 return cookie_manager_service_; |
| 83 } |
| 84 net::FakeURLFetcherFactory* fetcher_factory() { |
| 85 return &fake_url_fetcher_factory_; |
| 86 } |
| 87 |
| 88 void InitPromoVariation() { |
| 89 std::map<std::string, std::string> variations_params; |
| 90 variations_params["HoursBetweenSyncDeviceChecks"] = "1"; |
| 91 variations_params["DaysToVerifySingleUserProfile"] = "0"; |
| 92 variations_params["MinutesBetweenBrowsingSessions"] = "0"; |
| 93 variations_params["MinutesMaxContextSwitchDuration"] = "10"; |
| 94 variations_params["RPCThrottle"] = "0"; |
| 95 EXPECT_TRUE(variations::AssociateVariationParams( |
| 96 "CrossDevicePromo", "A", variations_params)); |
| 97 base::FieldTrialList::CreateFieldTrial("CrossDevicePromo", "A"); |
| 98 } |
| 99 |
| 100 private: |
| 101 content::TestBrowserThreadBundle bundle_; |
| 102 CrossDevicePromo* cross_device_promo_; |
| 103 TestingProfile* profile_; |
| 104 FakeSigninManagerForTesting* signin_manager_; |
| 105 FakeGaiaCookieManagerService* cookie_manager_service_; |
| 106 TestingPrefServiceSyncable* pref_service_; |
| 107 scoped_ptr<TestingProfileManager> testing_profile_manager_; |
| 108 base::HistogramTester histogram_tester_; |
| 109 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 110 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(CrossDevicePromoTest); |
| 113 }; |
| 114 |
| 115 void CrossDevicePromoTest::SetUp() { |
| 116 testing_profile_manager_.reset( |
| 117 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 118 ASSERT_TRUE(testing_profile_manager_.get()->SetUp()); |
| 119 |
| 120 TestingProfile::TestingFactories factories; |
| 121 factories.push_back(std::make_pair(ChromeSigninClientFactory::GetInstance(), |
| 122 signin::BuildTestSigninClient)); |
| 123 factories.push_back(std::make_pair( |
| 124 GaiaCookieManagerServiceFactory::GetInstance(), |
| 125 FakeGaiaCookieManagerService::Build)); |
| 126 factories.push_back(std::make_pair(SigninManagerFactory::GetInstance(), |
| 127 FakeSigninManagerBase::Build)); |
| 128 |
| 129 pref_service_ = new TestingPrefServiceSyncable(); |
| 130 chrome::RegisterUserProfilePrefs(pref_service_->registry()); |
| 131 |
| 132 profile_ = testing_profile_manager_.get()->CreateTestingProfile( |
| 133 "name", make_scoped_ptr<PrefServiceSyncable>(pref_service_), |
| 134 base::UTF8ToUTF16("name"), 0, std::string(), factories); |
| 135 |
| 136 cookie_manager_service_ = |
| 137 static_cast<FakeGaiaCookieManagerService*>( |
| 138 GaiaCookieManagerServiceFactory::GetForProfile(profile())); |
| 139 cookie_manager_service_->Init(&fake_url_fetcher_factory_); |
| 140 |
| 141 signin_manager_ = |
| 142 static_cast<FakeSigninManagerForTesting*>( |
| 143 SigninManagerFactory::GetForProfile(profile())); |
| 144 cross_device_promo_ = CrossDevicePromoFactory::GetForProfile(profile()); |
| 145 } |
| 146 |
| 147 CrossDevicePromoTest::CrossDevicePromoTest() |
| 148 : fake_url_fetcher_factory_(NULL) { |
| 149 ResetFieldTrialList(); |
| 150 } |
| 151 |
| 152 void CrossDevicePromoTest::ResetFieldTrialList() { |
| 153 // Destroy the existing FieldTrialList before creating a new one to avoid |
| 154 // a DCHECK. |
| 155 field_trial_list_.reset(); |
| 156 field_trial_list_.reset(new base::FieldTrialList( |
| 157 new metrics::SHA1EntropyProvider("foo"))); |
| 158 variations::testing::ClearAllVariationParams(); |
| 159 } |
| 160 |
| 161 TEST_F(CrossDevicePromoTest, Uninitialized) { |
| 162 ASSERT_TRUE(promo()); |
| 163 histogram_tester()->ExpectUniqueSample( |
| 164 "Signin.XDevicePromo.Initialized", 0, 1); |
| 165 |
| 166 promo()->CheckPromoEligibility(); |
| 167 histogram_tester()->ExpectUniqueSample( |
| 168 "Signin.XDevicePromo.Initialized", 0, 2); |
| 169 histogram_tester()->ExpectTotalCount("Signin.XDevicePromo.Eligibility", 0); |
| 170 ASSERT_FALSE(prefs()->GetBoolean(prefs::kCrossDevicePromoOptedOut)); |
| 171 } |
| 172 |
| 173 TEST_F(CrossDevicePromoTest, UnitializedOptedOut) { |
| 174 CrossDevicePromoObserver observer(promo()); |
| 175 |
| 176 promo()->OptOut(); |
| 177 // Opting out doesn't de-activate a never-active promo. |
| 178 ASSERT_EQ(0, observer.TimesBecameInactive()); |
| 179 ASSERT_TRUE(prefs()->GetBoolean(prefs::kCrossDevicePromoOptedOut)); |
| 180 |
| 181 // Never initialize a promo that is opted out. |
| 182 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 183 histogram_tester()->ExpectUniqueSample( |
| 184 "Signin.XDevicePromo.Initialized", 0, 2); |
| 185 histogram_tester()->ExpectTotalCount("Signin.XDevicePromo.Eligibility", 0); |
| 186 } |
| 187 |
| 188 TEST_F(CrossDevicePromoTest, PartiallyInitialized) { |
| 189 histogram_tester()->ExpectUniqueSample( |
| 190 "Signin.XDevicePromo.Initialized", 0, 1); |
| 191 |
| 192 std::map<std::string, std::string> variations_params; |
| 193 variations_params["HoursBetweenSyncDeviceChecks"] = "1"; |
| 194 variations_params["DaysToVerifySingleUserProfile"] = "1"; |
| 195 ASSERT_TRUE(variations::AssociateVariationParams( |
| 196 "CrossDevicePromo", "A", variations_params)); |
| 197 base::FieldTrialList::CreateFieldTrial("CrossDevicePromo", "A"); |
| 198 |
| 199 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 200 histogram_tester()->ExpectUniqueSample( |
| 201 "Signin.XDevicePromo.Initialized", 0, 2); |
| 202 ASSERT_FALSE(histogram_tester()->GetHistogramSamplesSinceCreation( |
| 203 "Signin.XDevicePromo.Eligibility")); |
| 204 } |
| 205 |
| 206 TEST_F(CrossDevicePromoTest, FullyInitialized) { |
| 207 histogram_tester()->ExpectUniqueSample( |
| 208 "Signin.XDevicePromo.Initialized", 0, 1); |
| 209 |
| 210 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 211 histogram_tester()->ExpectUniqueSample( |
| 212 "Signin.XDevicePromo.Initialized", 0, 2); |
| 213 |
| 214 InitPromoVariation(); |
| 215 signin_manager()->SignIn("12345", "foo@bar.com", "password"); |
| 216 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 217 histogram_tester()->ExpectBucketCount( |
| 218 "Signin.XDevicePromo.Initialized", 1, 1); |
| 219 histogram_tester()->ExpectBucketCount( |
| 220 "Signin.XDevicePromo.Initialized", 0, 2); |
| 221 |
| 222 histogram_tester()->ExpectUniqueSample( |
| 223 "Signin.XDevicePromo.Eligibility", signin_metrics::SIGNED_IN, 1); |
| 224 } |
| 225 |
| 226 TEST_F(CrossDevicePromoTest, InitializedOptOut) { |
| 227 // In a normal browser, the variations get set before the CrossDevicePromo is |
| 228 // created. Here, we need to force another Init() by calling |
| 229 // CheckPromoEligibility(). |
| 230 InitPromoVariation(); |
| 231 signin_manager()->SignIn("12345", "foo@bar.com", "password"); |
| 232 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 233 |
| 234 histogram_tester()->ExpectBucketCount( |
| 235 "Signin.XDevicePromo.Initialized", 1, 1); |
| 236 histogram_tester()->ExpectUniqueSample( |
| 237 "Signin.XDevicePromo.Eligibility", signin_metrics::SIGNED_IN, 1); |
| 238 |
| 239 // After opting out the initialized state remains; eligibility changes. |
| 240 promo()->OptOut(); |
| 241 promo()->CheckPromoEligibility(); |
| 242 histogram_tester()->ExpectBucketCount( |
| 243 "Signin.XDevicePromo.Initialized", 1, 1); |
| 244 histogram_tester()->ExpectBucketCount( |
| 245 "Signin.XDevicePromo.Eligibility", signin_metrics::OPTED_OUT, 1); |
| 246 } |
| 247 |
| 248 TEST_F(CrossDevicePromoTest, SignedInAndOut) { |
| 249 InitPromoVariation(); |
| 250 |
| 251 { |
| 252 base::HistogramTester test_signed_in; |
| 253 signin_manager()->SignIn("12345", "foo@bar.com", "password"); |
| 254 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 255 test_signed_in.ExpectUniqueSample( |
| 256 "Signin.XDevicePromo.Eligibility", signin_metrics::SIGNED_IN, 1); |
| 257 } |
| 258 |
| 259 { |
| 260 base::HistogramTester test_signed_out; |
| 261 signin_manager()->SignOut(signin_metrics::SIGNOUT_TEST); |
| 262 promo()->CheckPromoEligibility(); |
| 263 test_signed_out.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 264 signin_metrics::NOT_SINGLE_GAIA_ACCOUNT, 1); |
| 265 } |
| 266 } |
| 267 |
| 268 TEST_F(CrossDevicePromoTest, TrackAccountsInCookie) { |
| 269 InitPromoVariation(); |
| 270 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 271 |
| 272 ASSERT_FALSE(prefs()->HasPrefPath( |
| 273 prefs::kCrossDevicePromoObservedSingleAccountCookie)); |
| 274 std::vector<std::pair<std::string, bool>> accounts; |
| 275 |
| 276 // Setting a single cookie sets the time. |
| 277 base::Time before_setting_cookies = base::Time::Now(); |
| 278 cookie_manager_service()->set_list_accounts_fetched_once_for_testing(false); |
| 279 cookie_manager_service()->SetListAccountsResponseOneAccount("foo@bar.com"); |
| 280 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); |
| 281 base::RunLoop().RunUntilIdle(); |
| 282 |
| 283 base::Time after_setting_cookies = base::Time::Now(); |
| 284 ASSERT_TRUE(prefs()->HasPrefPath( |
| 285 prefs::kCrossDevicePromoObservedSingleAccountCookie)); |
| 286 ASSERT_LT(before_setting_cookies.ToInternalValue(), prefs()->GetInt64( |
| 287 prefs::kCrossDevicePromoObservedSingleAccountCookie)); |
| 288 ASSERT_GT(after_setting_cookies.ToInternalValue(), prefs()->GetInt64( |
| 289 prefs::kCrossDevicePromoObservedSingleAccountCookie)); |
| 290 |
| 291 // A single cookie a second time doesn't change the time. |
| 292 cookie_manager_service()->set_list_accounts_fetched_once_for_testing(false); |
| 293 cookie_manager_service()->SetListAccountsResponseOneAccount("foo@bar.com"); |
| 294 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); |
| 295 base::RunLoop().RunUntilIdle(); |
| 296 |
| 297 ASSERT_TRUE(prefs()->HasPrefPath( |
| 298 prefs::kCrossDevicePromoObservedSingleAccountCookie)); |
| 299 ASSERT_GT(after_setting_cookies.ToInternalValue(), prefs()->GetInt64( |
| 300 prefs::kCrossDevicePromoObservedSingleAccountCookie)); |
| 301 |
| 302 // Setting accounts with an auth error doesn't change the time. |
| 303 cookie_manager_service()->set_list_accounts_fetched_once_for_testing(false); |
| 304 cookie_manager_service()->SetListAccountsResponseWebLoginRequired(); |
| 305 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); |
| 306 base::RunLoop().RunUntilIdle(); |
| 307 |
| 308 ASSERT_TRUE(prefs()->HasPrefPath( |
| 309 prefs::kCrossDevicePromoObservedSingleAccountCookie)); |
| 310 ASSERT_LT(before_setting_cookies.ToInternalValue(), prefs()->GetInt64( |
| 311 prefs::kCrossDevicePromoObservedSingleAccountCookie)); |
| 312 ASSERT_GT(after_setting_cookies.ToInternalValue(), prefs()->GetInt64( |
| 313 prefs::kCrossDevicePromoObservedSingleAccountCookie)); |
| 314 |
| 315 // Seeing zero accounts clears the pref. |
| 316 cookie_manager_service()->set_list_accounts_fetched_once_for_testing(false); |
| 317 cookie_manager_service()->SetListAccountsResponseNoAccounts(); |
| 318 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); |
| 319 base::RunLoop().RunUntilIdle(); |
| 320 |
| 321 ASSERT_FALSE(prefs()->HasPrefPath( |
| 322 prefs::kCrossDevicePromoObservedSingleAccountCookie)); |
| 323 } |
| 324 |
| 325 TEST_F(CrossDevicePromoTest, SingleAccountEligibility) { |
| 326 InitPromoVariation(); |
| 327 |
| 328 { |
| 329 base::HistogramTester test_single_account; |
| 330 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 331 test_single_account.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 332 signin_metrics::NOT_SINGLE_GAIA_ACCOUNT, 1); |
| 333 } |
| 334 |
| 335 // Notice a single account. |
| 336 { |
| 337 base::HistogramTester test_single_account; |
| 338 std::vector<std::pair<std::string, bool>> accounts; |
| 339 cookie_manager_service()->set_list_accounts_fetched_once_for_testing(false); |
| 340 cookie_manager_service()->SetListAccountsResponseOneAccount("foo@bar.com"); |
| 341 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); |
| 342 base::RunLoop().RunUntilIdle(); |
| 343 |
| 344 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 345 test_single_account.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 346 signin_metrics::UNKNOWN_COUNT_DEVICES, 1); |
| 347 } |
| 348 |
| 349 // Set a single account that hasn't been around for "long enough". |
| 350 { |
| 351 base::HistogramTester test_single_account; |
| 352 prefs()->SetInt64(prefs::kCrossDevicePromoObservedSingleAccountCookie, |
| 353 InOneHour()); |
| 354 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 355 test_single_account.ExpectBucketCount("Signin.XDevicePromo.Eligibility", |
| 356 signin_metrics::NOT_SINGLE_GAIA_ACCOUNT, 1); |
| 357 } |
| 358 } |
| 359 |
| 360 TEST_F(CrossDevicePromoTest, NumDevicesEligibility) { |
| 361 // Start with a variation, signed in, and one account in the cookie jar. |
| 362 InitPromoVariation(); |
| 363 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 364 cookie_manager_service()->set_list_accounts_fetched_once_for_testing(false); |
| 365 cookie_manager_service()->SetListAccountsResponseOneAccount("foo@bar.com"); |
| 366 std::vector<std::pair<std::string, bool>> accounts; |
| 367 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); |
| 368 base::RunLoop().RunUntilIdle(); |
| 369 |
| 370 // Ensure we appropriate schedule a check for listing devices. |
| 371 { |
| 372 base::HistogramTester test_missing_list_devices; |
| 373 int64 earliest_time_to_check_list_devices = base::Time::Now(). |
| 374 ToInternalValue(); |
| 375 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 376 int64 latest_time_to_check_list_devices = InOneHour(); |
| 377 test_missing_list_devices.ExpectUniqueSample( |
| 378 "Signin.XDevicePromo.Eligibility", |
| 379 signin_metrics::UNKNOWN_COUNT_DEVICES, 1); |
| 380 EXPECT_TRUE(prefs()->HasPrefPath( |
| 381 prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 382 int64 when_to_check_list_devices = |
| 383 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime); |
| 384 EXPECT_LT(earliest_time_to_check_list_devices, when_to_check_list_devices); |
| 385 EXPECT_GT(latest_time_to_check_list_devices, when_to_check_list_devices); |
| 386 } |
| 387 |
| 388 // Don't reschedule the list devices check if there's one pending. |
| 389 { |
| 390 base::HistogramTester test_unknown_devices; |
| 391 int64 list_devices_time = InOneHour(); |
| 392 prefs()->SetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime, |
| 393 list_devices_time); |
| 394 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 395 test_unknown_devices.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 396 signin_metrics::UNKNOWN_COUNT_DEVICES, 1); |
| 397 // The scheduled time to call ListDevices should not have changed. |
| 398 ASSERT_EQ(list_devices_time, prefs()->GetInt64( |
| 399 prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 400 } |
| 401 |
| 402 // Execute the list devices check if it's time. |
| 403 { |
| 404 base::HistogramTester test_unknown_devices; |
| 405 prefs()->SetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime, |
| 406 base::Time::Now().ToInternalValue()); |
| 407 // The DeviceActivityFetcher will return an error to the promo service. |
| 408 fetcher_factory()->SetFakeResponse( |
| 409 GaiaUrls::GetInstance()->oauth2_iframe_url(), |
| 410 "not json", net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 411 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 412 base::RunLoop().RunUntilIdle(); |
| 413 test_unknown_devices.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 414 signin_metrics::ERROR_FETCHING_DEVICE_ACTIVITY, 1); |
| 415 } |
| 416 } |
| 417 |
| 418 TEST_F(CrossDevicePromoTest, ThrottleDeviceActivityCall) { |
| 419 // Start with a variation (fully throttled), signed in, one account in cookie. |
| 420 std::map<std::string, std::string> variations_params; |
| 421 variations_params["HoursBetweenSyncDeviceChecks"] = "1"; |
| 422 variations_params["DaysToVerifySingleUserProfile"] = "0"; |
| 423 variations_params["MinutesBetweenBrowsingSessions"] = "0"; |
| 424 variations_params["MinutesMaxContextSwitchDuration"] = "10"; |
| 425 variations_params["RPCThrottle"] = "100"; |
| 426 EXPECT_TRUE(variations::AssociateVariationParams( |
| 427 "CrossDevicePromo", "A", variations_params)); |
| 428 base::FieldTrialList::CreateFieldTrial("CrossDevicePromo", "A"); |
| 429 |
| 430 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 431 cookie_manager_service()->set_list_accounts_fetched_once_for_testing(false); |
| 432 cookie_manager_service()->SetListAccountsResponseOneAccount("foo@bar.com"); |
| 433 std::vector<std::pair<std::string, bool>> accounts; |
| 434 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); |
| 435 base::RunLoop().RunUntilIdle(); |
| 436 |
| 437 // Ensure Device Activity Fetch gets throttled. |
| 438 { |
| 439 base::HistogramTester test_throttle_rpc; |
| 440 prefs()->SetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime, |
| 441 base::Time::Now().ToInternalValue()); |
| 442 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 443 test_throttle_rpc.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 444 signin_metrics::THROTTLED_FETCHING_DEVICE_ACTIVITY, 1); |
| 445 } |
| 446 } |
| 447 |
| 448 TEST_F(CrossDevicePromoTest, NumDevicesKnown) { |
| 449 // Start with a variation, signed in, and one account, sync devices in 1 hour. |
| 450 InitPromoVariation(); |
| 451 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 452 cookie_manager_service()->set_list_accounts_fetched_once_for_testing(false); |
| 453 cookie_manager_service()->SetListAccountsResponseOneAccount("foo@bar.com"); |
| 454 std::vector<std::pair<std::string, bool>> accounts; |
| 455 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); |
| 456 base::RunLoop().RunUntilIdle(); |
| 457 prefs()->SetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime, |
| 458 InOneHour()); |
| 459 |
| 460 // If there is no device present. |
| 461 { |
| 462 base::HistogramTester test_no_devices; |
| 463 prefs()->SetInteger(prefs::kCrossDevicePromoNumDevices, 0); |
| 464 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 465 test_no_devices.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 466 signin_metrics::ZERO_DEVICES, 1); |
| 467 } |
| 468 |
| 469 // If there is one device present. |
| 470 { |
| 471 prefs()->SetInteger(prefs::kCrossDevicePromoNumDevices, 1); |
| 472 EXPECT_TRUE(promo()->CheckPromoEligibility()); |
| 473 } |
| 474 } |
| 475 |
| 476 TEST_F(CrossDevicePromoTest, FetchDeviceResults) { |
| 477 // Start with a variation, signed in, and one account, sync devices in 1 hour. |
| 478 InitPromoVariation(); |
| 479 EXPECT_FALSE(promo()->CheckPromoEligibility()); |
| 480 cookie_manager_service()->set_list_accounts_fetched_once_for_testing(false); |
| 481 cookie_manager_service()->SetListAccountsResponseOneAccount("foo@bar.com"); |
| 482 std::vector<std::pair<std::string, bool>> accounts; |
| 483 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); |
| 484 base::RunLoop().RunUntilIdle(); |
| 485 prefs()->SetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime, |
| 486 base::Time::Now().ToInternalValue()); |
| 487 prefs()->SetInteger(prefs::kCrossDevicePromoNumDevices, 1); |
| 488 |
| 489 // If there is no device found. |
| 490 { |
| 491 base::HistogramTester test_no_devices; |
| 492 std::vector<DeviceActivityFetcher::DeviceActivity> devices; |
| 493 int64 in_one_hour = InOneHour(); |
| 494 promo()->OnFetchDeviceActivitySuccess(devices); |
| 495 EXPECT_LE(in_one_hour, |
| 496 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 497 EXPECT_EQ(0, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); |
| 498 test_no_devices.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 499 signin_metrics::ZERO_DEVICES, 1); |
| 500 } |
| 501 |
| 502 // If there is one device found. It was recently active. |
| 503 { |
| 504 CrossDevicePromoObserver observer(promo()); |
| 505 ASSERT_FALSE(observer.IsActive()); |
| 506 base::HistogramTester test_one_device; |
| 507 std::vector<DeviceActivityFetcher::DeviceActivity> devices; |
| 508 base::Time device_last_active = |
| 509 base::Time::Now() - base::TimeDelta::FromMinutes(4); |
| 510 DeviceActivityFetcher::DeviceActivity device; |
| 511 device.last_active = device_last_active; |
| 512 device.name = "Aslan"; |
| 513 devices.push_back(device); |
| 514 |
| 515 int64 in_one_hour = InOneHour(); |
| 516 promo()->OnFetchDeviceActivitySuccess(devices); |
| 517 EXPECT_LE(in_one_hour, |
| 518 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 519 EXPECT_EQ(1, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); |
| 520 EXPECT_EQ(device_last_active.ToInternalValue(), |
| 521 prefs()->GetInt64(prefs::kCrossDevicePromoLastDeviceActiveTime)); |
| 522 EXPECT_TRUE(prefs()->GetBoolean(prefs::kCrossDevicePromoActive)); |
| 523 test_one_device.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 524 signin_metrics::ELIGIBLE, 1); |
| 525 EXPECT_TRUE(observer.IsActive()); |
| 526 EXPECT_EQ(1, observer.TimesBecameActive()); |
| 527 } |
| 528 |
| 529 // If there is one device found. It was not recently active. |
| 530 { |
| 531 CrossDevicePromoObserver observer(promo()); |
| 532 ASSERT_FALSE(observer.IsActive()); |
| 533 base::HistogramTester test_one_device; |
| 534 std::vector<DeviceActivityFetcher::DeviceActivity> devices; |
| 535 base::Time device_last_active = |
| 536 base::Time::Now() - base::TimeDelta::FromMinutes(30); |
| 537 DeviceActivityFetcher::DeviceActivity device; |
| 538 device.last_active = device_last_active; |
| 539 device.name = "Aslan"; |
| 540 devices.push_back(device); |
| 541 |
| 542 int64 in_one_hour = InOneHour(); |
| 543 promo()->OnFetchDeviceActivitySuccess(devices); |
| 544 EXPECT_LE(in_one_hour, |
| 545 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 546 EXPECT_EQ(1, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); |
| 547 EXPECT_EQ(device_last_active.ToInternalValue(), |
| 548 prefs()->GetInt64(prefs::kCrossDevicePromoLastDeviceActiveTime)); |
| 549 EXPECT_FALSE(prefs()->GetBoolean(prefs::kCrossDevicePromoActive)); |
| 550 test_one_device.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 551 signin_metrics::NO_ACTIVE_DEVICES, 1); |
| 552 EXPECT_FALSE(observer.IsActive()); |
| 553 } |
| 554 |
| 555 // If there are two devices found, one recently. |
| 556 { |
| 557 CrossDevicePromoObserver observer(promo()); |
| 558 ASSERT_FALSE(observer.IsActive()); |
| 559 base::HistogramTester test_two_devices; |
| 560 std::vector<DeviceActivityFetcher::DeviceActivity> devices; |
| 561 base::Time device1_last_active = |
| 562 base::Time::Now() - base::TimeDelta::FromMinutes(30); |
| 563 base::Time device2_last_active = |
| 564 base::Time::Now() - base::TimeDelta::FromMinutes(3); |
| 565 DeviceActivityFetcher::DeviceActivity device1; |
| 566 device1.last_active = device1_last_active; |
| 567 device1.name = "Aslan"; |
| 568 devices.push_back(device1); |
| 569 DeviceActivityFetcher::DeviceActivity device2; |
| 570 device2.last_active = device2_last_active; |
| 571 device2.name = "Balrog"; |
| 572 devices.push_back(device2); |
| 573 |
| 574 int64 in_one_hour = InOneHour(); |
| 575 promo()->OnFetchDeviceActivitySuccess(devices); |
| 576 EXPECT_LE(in_one_hour, |
| 577 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 578 EXPECT_EQ(2, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); |
| 579 EXPECT_EQ(device2_last_active.ToInternalValue(), |
| 580 prefs()->GetInt64(prefs::kCrossDevicePromoLastDeviceActiveTime)); |
| 581 EXPECT_TRUE(prefs()->GetBoolean(prefs::kCrossDevicePromoActive)); |
| 582 test_two_devices.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 583 signin_metrics::ELIGIBLE, 1); |
| 584 EXPECT_TRUE(observer.IsActive()); |
| 585 EXPECT_EQ(1, observer.TimesBecameActive()); |
| 586 } |
| 587 } |
OLD | NEW |