1 // Copyright 2018 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 "chromeos/services/multidevice_setup/host_device_timestamp_manager_impl.h"
6 
7 #include <memory>
8 
9 #include "base/test/simple_test_clock.h"
10 #include "base/time/time.h"
11 #include "chromeos/components/multidevice/remote_device_test_util.h"
12 #include "chromeos/services/multidevice_setup/fake_host_status_provider.h"
13 #include "components/sync_preferences/testing_pref_service_syncable.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace chromeos {
17 
18 namespace multidevice_setup {
19 
20 namespace {
21 const base::Time kTestTime = base::Time::FromJavaTime(1500000000000);
22 const base::Time kLaterTime =
23     kTestTime + base::TimeDelta::FromMilliseconds(123456789);
24 }  // namespace
25 
26 class HostDeviceTimestampManagerImplTest : public testing::Test {
27  protected:
28   HostDeviceTimestampManagerImplTest() = default;
29   ~HostDeviceTimestampManagerImplTest() override = default;
30 
SetUp()31   void SetUp() override {
32     test_pref_service_ =
33         std::make_unique<sync_preferences::TestingPrefServiceSyncable>();
34     HostDeviceTimestampManagerImpl::RegisterPrefs(
35         test_pref_service_->registry());
36 
37     fake_host_status_provider_ = std::make_unique<FakeHostStatusProvider>();
38     test_clock_ = std::make_unique<base::SimpleTestClock>();
39     SetNow(kTestTime);
40 
41     manager_ = HostDeviceTimestampManagerImpl::Factory::Create(
42         fake_host_status_provider_.get(), test_pref_service_.get(),
43         test_clock_.get());
44   }
45 
46   // If the status corresponds to a set host, we set a dummy default device
47   // because the device info is irrelevant.
SetHostStatus(mojom::HostStatus host_status)48   void SetHostStatus(mojom::HostStatus host_status) {
49     if (host_status == mojom::HostStatus::kNoEligibleHosts ||
50         host_status == mojom::HostStatus::kEligibleHostExistsButNoHostSet) {
51       fake_host_status_provider_->SetHostWithStatus(
52           host_status, base::nullopt /* host_device */);
53       return;
54     }
55     fake_host_status_provider_->SetHostWithStatus(
56         host_status, multidevice::RemoteDeviceRefBuilder()
57                          .SetPublicKey("fake-phone-key")
58                          .SetName("Fake Phone Name")
59                          .Build());
60   }
61 
SetNow(const base::Time now)62   void SetNow(const base::Time now) { test_clock_->SetNow(now); }
63 
manager()64   HostDeviceTimestampManager* manager() { return manager_.get(); }
65 
66  private:
67   std::unique_ptr<sync_preferences::TestingPrefServiceSyncable>
68       test_pref_service_;
69   std::unique_ptr<FakeHostStatusProvider> fake_host_status_provider_;
70   std::unique_ptr<base::SimpleTestClock> test_clock_;
71 
72   std::unique_ptr<HostDeviceTimestampManager> manager_;
73 
74   DISALLOW_COPY_AND_ASSIGN(HostDeviceTimestampManagerImplTest);
75 };
76 
TEST_F(HostDeviceTimestampManagerImplTest,RecordsWhetherHostWasSetFromThisChromebook)77 TEST_F(HostDeviceTimestampManagerImplTest,
78        RecordsWhetherHostWasSetFromThisChromebook) {
79   EXPECT_FALSE(manager()->WasHostSetFromThisChromebook());
80   // Discover potential host.
81   SetHostStatus(mojom::HostStatus::kEligibleHostExistsButNoHostSet);
82 
83   // Set up host.
84   SetHostStatus(
85       mojom::HostStatus::kHostSetLocallyButWaitingForBackendConfirmation);
86   EXPECT_TRUE(manager()->WasHostSetFromThisChromebook());
87 
88   // Verify host.
89   SetHostStatus(mojom::HostStatus::kHostVerified);
90   EXPECT_TRUE(manager()->WasHostSetFromThisChromebook());
91 
92   // Forget the host.
93   SetHostStatus(mojom::HostStatus::kEligibleHostExistsButNoHostSet);
94   EXPECT_FALSE(manager()->WasHostSetFromThisChromebook());
95 
96   // Set verified host from a different Chromebook
97   SetHostStatus(mojom::HostStatus::kHostVerified);
98   EXPECT_FALSE(manager()->WasHostSetFromThisChromebook());
99 }
100 
TEST_F(HostDeviceTimestampManagerImplTest,RecordsCorrectCompletionTime)101 TEST_F(HostDeviceTimestampManagerImplTest, RecordsCorrectCompletionTime) {
102   EXPECT_FALSE(manager()->GetLatestSetupFlowCompletionTimestamp());
103   SetHostStatus(
104       mojom::HostStatus::kHostSetLocallyButWaitingForBackendConfirmation);
105   EXPECT_EQ(kTestTime, manager()->GetLatestSetupFlowCompletionTimestamp());
106   // Forget the host.
107   SetHostStatus(mojom::HostStatus::kEligibleHostExistsButNoHostSet);
108   // Set up later and check that the new time replaces the old.
109   SetNow(kLaterTime);
110   SetHostStatus(
111       mojom::HostStatus::kHostSetLocallyButWaitingForBackendConfirmation);
112   EXPECT_EQ(kLaterTime, manager()->GetLatestSetupFlowCompletionTimestamp());
113 }
114 
TEST_F(HostDeviceTimestampManagerImplTest,RecordsCorrectVerificationTime)115 TEST_F(HostDeviceTimestampManagerImplTest, RecordsCorrectVerificationTime) {
116   EXPECT_FALSE(manager()->GetLatestVerificationTimestamp());
117   // Update with verified host.
118   SetHostStatus(mojom::HostStatus::kHostVerified);
119   EXPECT_EQ(kTestTime, manager()->GetLatestVerificationTimestamp());
120   // Forget the host.
121   SetHostStatus(mojom::HostStatus::kEligibleHostExistsButNoHostSet);
122   // Set up later and check that the new time replaces the old.
123   SetNow(kLaterTime);
124   SetHostStatus(mojom::HostStatus::kHostVerified);
125   EXPECT_EQ(kLaterTime, manager()->GetLatestVerificationTimestamp());
126 }
127 
TEST_F(HostDeviceTimestampManagerImplTest,StoresCompletionsAndVerificationSimultaneously)128 TEST_F(HostDeviceTimestampManagerImplTest,
129        StoresCompletionsAndVerificationSimultaneously) {
130   SetHostStatus(
131       mojom::HostStatus::kHostSetLocallyButWaitingForBackendConfirmation);
132   SetNow(kLaterTime);
133   SetHostStatus(mojom::HostStatus::kHostVerified);
134   EXPECT_EQ(kTestTime, manager()->GetLatestSetupFlowCompletionTimestamp());
135   EXPECT_EQ(kLaterTime, manager()->GetLatestVerificationTimestamp());
136 }
137 }  // namespace multidevice_setup
138 
139 }  // namespace chromeos
140