| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/signin/cross_device_promo.h" | 5 #include "chrome/browser/signin/cross_device_promo.h" |
| 6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/macros.h" |
| 7 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| 8 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 9 #include "base/test/histogram_tester.h" | 12 #include "base/test/histogram_tester.h" |
| 10 #include "chrome/browser/prefs/browser_prefs.h" | 13 #include "chrome/browser/prefs/browser_prefs.h" |
| 11 #include "chrome/browser/signin/chrome_signin_client_factory.h" | 14 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
| 12 #include "chrome/browser/signin/cross_device_promo_factory.h" | 15 #include "chrome/browser/signin/cross_device_promo_factory.h" |
| 13 #include "chrome/browser/signin/fake_gaia_cookie_manager_service.h" | 16 #include "chrome/browser/signin/fake_gaia_cookie_manager_service.h" |
| 14 #include "chrome/browser/signin/fake_signin_manager_builder.h" | 17 #include "chrome/browser/signin/fake_signin_manager_builder.h" |
| 15 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h" | 18 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h" |
| 16 #include "chrome/browser/signin/signin_manager_factory.h" | 19 #include "chrome/browser/signin/signin_manager_factory.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 28 #include "components/variations/variations_associated_data.h" | 31 #include "components/variations/variations_associated_data.h" |
| 29 #include "content/public/test/test_browser_thread_bundle.h" | 32 #include "content/public/test/test_browser_thread_bundle.h" |
| 30 #include "google_apis/gaia/gaia_urls.h" | 33 #include "google_apis/gaia/gaia_urls.h" |
| 31 #include "net/url_request/test_url_fetcher_factory.h" | 34 #include "net/url_request/test_url_fetcher_factory.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 36 |
| 34 namespace { | 37 namespace { |
| 35 | 38 |
| 36 typedef std::map<std::string, std::string> VariationsMap; | 39 typedef std::map<std::string, std::string> VariationsMap; |
| 37 | 40 |
| 38 int64 InTwoHours() { | 41 int64_t InTwoHours() { |
| 39 return (base::Time::Now() + base::TimeDelta::FromHours(2)).ToInternalValue(); | 42 return (base::Time::Now() + base::TimeDelta::FromHours(2)).ToInternalValue(); |
| 40 } | 43 } |
| 41 | 44 |
| 42 } // namespace | 45 } // namespace |
| 43 | 46 |
| 44 class CrossDevicePromoObserver : public CrossDevicePromo::Observer { | 47 class CrossDevicePromoObserver : public CrossDevicePromo::Observer { |
| 45 public: | 48 public: |
| 46 explicit CrossDevicePromoObserver(CrossDevicePromo* promo) | 49 explicit CrossDevicePromoObserver(CrossDevicePromo* promo) |
| 47 : eligible_(false), | 50 : eligible_(false), |
| 48 times_set_eligible_(0), | 51 times_set_eligible_(0), |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 EXPECT_FALSE(promo()->CheckPromoEligibilityForTesting()); | 409 EXPECT_FALSE(promo()->CheckPromoEligibilityForTesting()); |
| 407 cookie_manager_service()->set_list_accounts_stale_for_testing(true); | 410 cookie_manager_service()->set_list_accounts_stale_for_testing(true); |
| 408 cookie_manager_service()->SetListAccountsResponseOneAccount("f@bar.com", "1"); | 411 cookie_manager_service()->SetListAccountsResponseOneAccount("f@bar.com", "1"); |
| 409 std::vector<gaia::ListedAccount> accounts; | 412 std::vector<gaia::ListedAccount> accounts; |
| 410 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); | 413 EXPECT_FALSE(cookie_manager_service()->ListAccounts(&accounts)); |
| 411 base::RunLoop().RunUntilIdle(); | 414 base::RunLoop().RunUntilIdle(); |
| 412 | 415 |
| 413 // Ensure we appropriate schedule a check for device activity. | 416 // Ensure we appropriate schedule a check for device activity. |
| 414 { | 417 { |
| 415 base::HistogramTester test_missing_list_devices; | 418 base::HistogramTester test_missing_list_devices; |
| 416 int64 earliest_time_to_check_list_devices = | 419 int64_t earliest_time_to_check_list_devices = |
| 417 base::Time::Now().ToInternalValue(); | 420 base::Time::Now().ToInternalValue(); |
| 418 EXPECT_FALSE(promo()->CheckPromoEligibilityForTesting()); | 421 EXPECT_FALSE(promo()->CheckPromoEligibilityForTesting()); |
| 419 int64 latest_time_to_check_list_devices = InTwoHours(); | 422 int64_t latest_time_to_check_list_devices = InTwoHours(); |
| 420 test_missing_list_devices.ExpectUniqueSample( | 423 test_missing_list_devices.ExpectUniqueSample( |
| 421 "Signin.XDevicePromo.Eligibility", | 424 "Signin.XDevicePromo.Eligibility", |
| 422 signin_metrics::UNKNOWN_COUNT_DEVICES, 1); | 425 signin_metrics::UNKNOWN_COUNT_DEVICES, 1); |
| 423 EXPECT_TRUE( | 426 EXPECT_TRUE( |
| 424 prefs()->HasPrefPath(prefs::kCrossDevicePromoNextFetchListDevicesTime)); | 427 prefs()->HasPrefPath(prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 425 int64 when_to_check_list_devices = | 428 int64_t when_to_check_list_devices = |
| 426 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime); | 429 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime); |
| 427 EXPECT_LT(earliest_time_to_check_list_devices, when_to_check_list_devices); | 430 EXPECT_LT(earliest_time_to_check_list_devices, when_to_check_list_devices); |
| 428 EXPECT_GT(latest_time_to_check_list_devices, when_to_check_list_devices); | 431 EXPECT_GT(latest_time_to_check_list_devices, when_to_check_list_devices); |
| 429 } | 432 } |
| 430 | 433 |
| 431 // Don't reschedule the device activity check if there's one pending. | 434 // Don't reschedule the device activity check if there's one pending. |
| 432 { | 435 { |
| 433 base::HistogramTester test_unknown_devices; | 436 base::HistogramTester test_unknown_devices; |
| 434 int64 list_devices_time = InTwoHours(); | 437 int64_t list_devices_time = InTwoHours(); |
| 435 prefs()->SetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime, | 438 prefs()->SetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime, |
| 436 list_devices_time); | 439 list_devices_time); |
| 437 EXPECT_FALSE(promo()->CheckPromoEligibilityForTesting()); | 440 EXPECT_FALSE(promo()->CheckPromoEligibilityForTesting()); |
| 438 test_unknown_devices.ExpectUniqueSample( | 441 test_unknown_devices.ExpectUniqueSample( |
| 439 "Signin.XDevicePromo.Eligibility", | 442 "Signin.XDevicePromo.Eligibility", |
| 440 signin_metrics::UNKNOWN_COUNT_DEVICES, 1); | 443 signin_metrics::UNKNOWN_COUNT_DEVICES, 1); |
| 441 // The scheduled time to fetch device activity should not have changed. | 444 // The scheduled time to fetch device activity should not have changed. |
| 442 EXPECT_EQ( | 445 EXPECT_EQ( |
| 443 list_devices_time, | 446 list_devices_time, |
| 444 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); | 447 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 prefs()->SetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime, | 545 prefs()->SetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime, |
| 543 base::Time::Now().ToInternalValue()); | 546 base::Time::Now().ToInternalValue()); |
| 544 prefs()->SetInteger(prefs::kCrossDevicePromoNumDevices, 1); | 547 prefs()->SetInteger(prefs::kCrossDevicePromoNumDevices, 1); |
| 545 | 548 |
| 546 // Verify that if the device activity fetcher returns zero devices the | 549 // Verify that if the device activity fetcher returns zero devices the |
| 547 // eligibility metric will report a ZERO_DEVICES event, and will not report | 550 // eligibility metric will report a ZERO_DEVICES event, and will not report |
| 548 // the promo as eligible to be shown. | 551 // the promo as eligible to be shown. |
| 549 { | 552 { |
| 550 base::HistogramTester test_no_devices; | 553 base::HistogramTester test_no_devices; |
| 551 std::vector<DeviceActivityFetcher::DeviceActivity> devices; | 554 std::vector<DeviceActivityFetcher::DeviceActivity> devices; |
| 552 int64 in_two_hours = InTwoHours(); | 555 int64_t in_two_hours = InTwoHours(); |
| 553 promo()->OnFetchDeviceActivitySuccess(devices); | 556 promo()->OnFetchDeviceActivitySuccess(devices); |
| 554 EXPECT_LE( | 557 EXPECT_LE( |
| 555 in_two_hours, | 558 in_two_hours, |
| 556 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); | 559 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 557 EXPECT_EQ(0, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); | 560 EXPECT_EQ(0, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); |
| 558 test_no_devices.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", | 561 test_no_devices.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 559 signin_metrics::ZERO_DEVICES, 1); | 562 signin_metrics::ZERO_DEVICES, 1); |
| 560 } | 563 } |
| 561 | 564 |
| 562 // Verify that if the device activity fetcher returns one device that was | 565 // Verify that if the device activity fetcher returns one device that was |
| 563 // recently active, the promo is marked as eligible and the eligibility | 566 // recently active, the promo is marked as eligible and the eligibility |
| 564 // metric reports an ELIGIBLE event. | 567 // metric reports an ELIGIBLE event. |
| 565 { | 568 { |
| 566 CrossDevicePromoObserver observer(promo()); | 569 CrossDevicePromoObserver observer(promo()); |
| 567 EXPECT_FALSE(observer.is_eligible()); | 570 EXPECT_FALSE(observer.is_eligible()); |
| 568 base::HistogramTester test_one_device; | 571 base::HistogramTester test_one_device; |
| 569 std::vector<DeviceActivityFetcher::DeviceActivity> devices; | 572 std::vector<DeviceActivityFetcher::DeviceActivity> devices; |
| 570 base::Time device_last_active = | 573 base::Time device_last_active = |
| 571 base::Time::Now() - base::TimeDelta::FromMinutes(4); | 574 base::Time::Now() - base::TimeDelta::FromMinutes(4); |
| 572 DeviceActivityFetcher::DeviceActivity device; | 575 DeviceActivityFetcher::DeviceActivity device; |
| 573 device.last_active = device_last_active; | 576 device.last_active = device_last_active; |
| 574 device.name = "Aslan"; | 577 device.name = "Aslan"; |
| 575 devices.push_back(device); | 578 devices.push_back(device); |
| 576 | 579 |
| 577 int64 in_two_hours = InTwoHours(); | 580 int64_t in_two_hours = InTwoHours(); |
| 578 promo()->OnFetchDeviceActivitySuccess(devices); | 581 promo()->OnFetchDeviceActivitySuccess(devices); |
| 579 EXPECT_LE( | 582 EXPECT_LE( |
| 580 in_two_hours, | 583 in_two_hours, |
| 581 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); | 584 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 582 EXPECT_EQ(1, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); | 585 EXPECT_EQ(1, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); |
| 583 EXPECT_EQ(device_last_active.ToInternalValue(), | 586 EXPECT_EQ(device_last_active.ToInternalValue(), |
| 584 prefs()->GetInt64(prefs::kCrossDevicePromoLastDeviceActiveTime)); | 587 prefs()->GetInt64(prefs::kCrossDevicePromoLastDeviceActiveTime)); |
| 585 EXPECT_TRUE(prefs()->GetBoolean(prefs::kCrossDevicePromoShouldBeShown)); | 588 EXPECT_TRUE(prefs()->GetBoolean(prefs::kCrossDevicePromoShouldBeShown)); |
| 586 test_one_device.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", | 589 test_one_device.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 587 signin_metrics::ELIGIBLE, 1); | 590 signin_metrics::ELIGIBLE, 1); |
| 588 EXPECT_TRUE(observer.is_eligible()); | 591 EXPECT_TRUE(observer.is_eligible()); |
| 589 EXPECT_EQ(1, observer.times_set_eligible()); | 592 EXPECT_EQ(1, observer.times_set_eligible()); |
| 590 } | 593 } |
| 591 | 594 |
| 592 // Verify that if the device activity fetcher returns one device that was not | 595 // Verify that if the device activity fetcher returns one device that was not |
| 593 // recently active then the eligibility metric will report a NO_ACTIVE_DEVICES | 596 // recently active then the eligibility metric will report a NO_ACTIVE_DEVICES |
| 594 // event, and will not report the promo as eligible to be shown. | 597 // event, and will not report the promo as eligible to be shown. |
| 595 { | 598 { |
| 596 CrossDevicePromoObserver observer(promo()); | 599 CrossDevicePromoObserver observer(promo()); |
| 597 EXPECT_FALSE(observer.is_eligible()); | 600 EXPECT_FALSE(observer.is_eligible()); |
| 598 base::HistogramTester test_one_device; | 601 base::HistogramTester test_one_device; |
| 599 std::vector<DeviceActivityFetcher::DeviceActivity> devices; | 602 std::vector<DeviceActivityFetcher::DeviceActivity> devices; |
| 600 base::Time device_last_active = | 603 base::Time device_last_active = |
| 601 base::Time::Now() - base::TimeDelta::FromMinutes(30); | 604 base::Time::Now() - base::TimeDelta::FromMinutes(30); |
| 602 DeviceActivityFetcher::DeviceActivity device; | 605 DeviceActivityFetcher::DeviceActivity device; |
| 603 device.last_active = device_last_active; | 606 device.last_active = device_last_active; |
| 604 device.name = "Aslan"; | 607 device.name = "Aslan"; |
| 605 devices.push_back(device); | 608 devices.push_back(device); |
| 606 | 609 |
| 607 int64 in_two_hours = InTwoHours(); | 610 int64_t in_two_hours = InTwoHours(); |
| 608 promo()->OnFetchDeviceActivitySuccess(devices); | 611 promo()->OnFetchDeviceActivitySuccess(devices); |
| 609 EXPECT_LE( | 612 EXPECT_LE( |
| 610 in_two_hours, | 613 in_two_hours, |
| 611 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); | 614 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 612 EXPECT_EQ(1, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); | 615 EXPECT_EQ(1, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); |
| 613 EXPECT_EQ(device_last_active.ToInternalValue(), | 616 EXPECT_EQ(device_last_active.ToInternalValue(), |
| 614 prefs()->GetInt64(prefs::kCrossDevicePromoLastDeviceActiveTime)); | 617 prefs()->GetInt64(prefs::kCrossDevicePromoLastDeviceActiveTime)); |
| 615 EXPECT_FALSE(prefs()->GetBoolean(prefs::kCrossDevicePromoShouldBeShown)); | 618 EXPECT_FALSE(prefs()->GetBoolean(prefs::kCrossDevicePromoShouldBeShown)); |
| 616 test_one_device.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", | 619 test_one_device.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 617 signin_metrics::NO_ACTIVE_DEVICES, 1); | 620 signin_metrics::NO_ACTIVE_DEVICES, 1); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 632 base::Time::Now() - base::TimeDelta::FromMinutes(3); | 635 base::Time::Now() - base::TimeDelta::FromMinutes(3); |
| 633 DeviceActivityFetcher::DeviceActivity device1; | 636 DeviceActivityFetcher::DeviceActivity device1; |
| 634 device1.last_active = device1_last_active; | 637 device1.last_active = device1_last_active; |
| 635 device1.name = "Aslan"; | 638 device1.name = "Aslan"; |
| 636 devices.push_back(device1); | 639 devices.push_back(device1); |
| 637 DeviceActivityFetcher::DeviceActivity device2; | 640 DeviceActivityFetcher::DeviceActivity device2; |
| 638 device2.last_active = device2_last_active; | 641 device2.last_active = device2_last_active; |
| 639 device2.name = "Balrog"; | 642 device2.name = "Balrog"; |
| 640 devices.push_back(device2); | 643 devices.push_back(device2); |
| 641 | 644 |
| 642 int64 in_two_hours = InTwoHours(); | 645 int64_t in_two_hours = InTwoHours(); |
| 643 promo()->OnFetchDeviceActivitySuccess(devices); | 646 promo()->OnFetchDeviceActivitySuccess(devices); |
| 644 EXPECT_LE( | 647 EXPECT_LE( |
| 645 in_two_hours, | 648 in_two_hours, |
| 646 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); | 649 prefs()->GetInt64(prefs::kCrossDevicePromoNextFetchListDevicesTime)); |
| 647 EXPECT_EQ(2, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); | 650 EXPECT_EQ(2, prefs()->GetInteger(prefs::kCrossDevicePromoNumDevices)); |
| 648 EXPECT_EQ(device2_last_active.ToInternalValue(), | 651 EXPECT_EQ(device2_last_active.ToInternalValue(), |
| 649 prefs()->GetInt64(prefs::kCrossDevicePromoLastDeviceActiveTime)); | 652 prefs()->GetInt64(prefs::kCrossDevicePromoLastDeviceActiveTime)); |
| 650 EXPECT_TRUE(prefs()->GetBoolean(prefs::kCrossDevicePromoShouldBeShown)); | 653 EXPECT_TRUE(prefs()->GetBoolean(prefs::kCrossDevicePromoShouldBeShown)); |
| 651 test_two_devices.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", | 654 test_two_devices.ExpectUniqueSample("Signin.XDevicePromo.Eligibility", |
| 652 signin_metrics::ELIGIBLE, 1); | 655 signin_metrics::ELIGIBLE, 1); |
| 653 EXPECT_TRUE(observer.is_eligible()); | 656 EXPECT_TRUE(observer.is_eligible()); |
| 654 EXPECT_EQ(1, observer.times_set_eligible()); | 657 EXPECT_EQ(1, observer.times_set_eligible()); |
| 655 } | 658 } |
| 656 } | 659 } |
| OLD | NEW |