Commit 5a9eebe5 authored by Jordy Greenblatt's avatar Jordy Greenblatt Committed by Commit Bot

[CrOS MultiDevice]: Setup completion recorder

Change-Id: I81c5655e357b6211ccb32f1b82b829bb8d402f0b
Reviewed-on: https://chromium-review.googlesource.com/1088230
Commit-Queue: Jordy Greenblatt <jordynass@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#565044}
parent 82937e55
......@@ -16,6 +16,9 @@ static_library("multidevice_setup") {
"multidevice_setup_service.h",
"observer_notifier.cc",
"observer_notifier.h",
"setup_flow_completion_recorder.h",
"setup_flow_completion_recorder_impl.cc",
"setup_flow_completion_recorder_impl.h",
]
deps = [
......@@ -42,6 +45,8 @@ static_library("test_support") {
sources = [
"fake_multidevice_setup_observer.cc",
"fake_multidevice_setup_observer.h",
"fake_setup_flow_completion_recorder.cc",
"fake_setup_flow_completion_recorder.h",
]
public_deps = [
......@@ -63,6 +68,7 @@ source_set("unit_tests") {
sources = [
"multidevice_setup_service_unittest.cc",
"observer_notifier_unittest.cc",
"setup_flow_completion_recorder_impl_unittest.cc",
]
deps = [
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/services/multidevice_setup/fake_setup_flow_completion_recorder.h"
namespace chromeos {
namespace multidevice_setup {
FakeSetupFlowCompletionRecorder::FakeSetupFlowCompletionRecorder() = default;
base::Optional<base::Time>
FakeSetupFlowCompletionRecorder::GetCompletionTimestamp() {
return completion_time_;
}
void FakeSetupFlowCompletionRecorder::RecordCompletion() {
completion_time_ = current_time_;
}
} // namespace multidevice_setup
} // namespace chromeos
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_SERVICES_MULTIDEVICE_SETUP_FAKE_SETUP_FLOW_COMPLETION_RECORDER_H_
#define CHROMEOS_SERVICES_MULTIDEVICE_SETUP_FAKE_SETUP_FLOW_COMPLETION_RECORDER_H_
#include "base/optional.h"
#include "base/time/time.h"
#include "chromeos/services/multidevice_setup/setup_flow_completion_recorder.h"
namespace chromeos {
namespace multidevice_setup {
class FakeSetupFlowCompletionRecorder : public SetupFlowCompletionRecorder {
public:
FakeSetupFlowCompletionRecorder();
~FakeSetupFlowCompletionRecorder() override;
void set_current_time(base::Time current_time) {
current_time_ = current_time;
}
private:
// SetupFlowCompletionRecorder:
base::Optional<base::Time> GetCompletionTimestamp() override;
void RecordCompletion() override;
base::Optional<base::Time> completion_time_;
base::Time current_time_;
};
} // namespace multidevice_setup
} // namespace chromeos
#endif // CHROMEOS_SERVICES_MULTIDEVICE_SETUP_FAKE_SETUP_FLOW_COMPLETION_RECORDER_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_SERVICES_MULTIDEVICE_SETUP_SETUP_FLOW_COMPLETION_RECORDER_H_
#define CHROMEOS_SERVICES_MULTIDEVICE_SETUP_SETUP_FLOW_COMPLETION_RECORDER_H_
#include "base/optional.h"
#include "base/time/time.h"
namespace chromeos {
namespace multidevice_setup {
// Records time at which the logged in user completed the MultiDevice setup flow
// on this device.
class SetupFlowCompletionRecorder {
public:
virtual ~SetupFlowCompletionRecorder() = default;
// If the logged in GAIA account has completed the MultiDevice setup flow on
// this device, this returns the time at which the flow was completed.
// Otherwise it returns base::nullopt.
virtual base::Optional<base::Time> GetCompletionTimestamp() = 0;
virtual void RecordCompletion() = 0;
protected:
SetupFlowCompletionRecorder() = default;
private:
DISALLOW_COPY_AND_ASSIGN(SetupFlowCompletionRecorder);
};
} // namespace multidevice_setup
} // namespace chromeos
#endif // CHROMEOS_SERVICES_MULTIDEVICE_SETUP_SETUP_FLOW_COMPLETION_RECORDER_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/services/multidevice_setup/setup_flow_completion_recorder_impl.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "base/time/clock.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
namespace chromeos {
namespace multidevice_setup {
// static
SetupFlowCompletionRecorderImpl::Factory*
SetupFlowCompletionRecorderImpl::Factory::test_factory_ = nullptr;
// static
SetupFlowCompletionRecorderImpl::Factory*
SetupFlowCompletionRecorderImpl::Factory::Get() {
if (test_factory_)
return test_factory_;
static base::NoDestructor<SetupFlowCompletionRecorderImpl::Factory> factory;
return factory.get();
}
// static
void SetupFlowCompletionRecorderImpl::Factory::SetFactoryForTesting(
Factory* test_factory) {
test_factory_ = test_factory;
}
SetupFlowCompletionRecorderImpl::Factory::~Factory() = default;
std::unique_ptr<SetupFlowCompletionRecorder>
SetupFlowCompletionRecorderImpl::Factory::BuildInstance(
PrefService* pref_service,
base::Clock* clock) {
return base::WrapUnique(
new SetupFlowCompletionRecorderImpl(pref_service, clock));
}
// static
const char SetupFlowCompletionRecorderImpl::kSetupFlowCompletedPrefName[] =
"multidevice_setup.setup_flow_completed";
// static
void SetupFlowCompletionRecorderImpl::RegisterPrefs(
PrefRegistrySimple* registry) {
registry->RegisterInt64Pref(kSetupFlowCompletedPrefName, 0);
}
SetupFlowCompletionRecorderImpl::~SetupFlowCompletionRecorderImpl() = default;
SetupFlowCompletionRecorderImpl::SetupFlowCompletionRecorderImpl(
PrefService* pref_service,
base::Clock* clock)
: pref_service_(pref_service), clock_(clock) {}
base::Optional<base::Time>
SetupFlowCompletionRecorderImpl::GetCompletionTimestamp() {
if (pref_service_->GetInt64(kSetupFlowCompletedPrefName) > 0)
return base::Time::FromJavaTime(
pref_service_->GetInt64(kSetupFlowCompletedPrefName));
return base::nullopt;
}
void SetupFlowCompletionRecorderImpl::RecordCompletion() {
pref_service_->SetInt64(kSetupFlowCompletedPrefName,
clock_->Now().ToJavaTime());
}
} // namespace multidevice_setup
} // namespace chromeos
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_SERVICES_MULTIDEVICE_SETUP_SETUP_FLOW_COMPLETION_RECORDER_IMPL_H_
#define CHROMEOS_SERVICES_MULTIDEVICE_SETUP_SETUP_FLOW_COMPLETION_RECORDER_IMPL_H_
#include <memory>
#include "base/optional.h"
#include "base/time/time.h"
#include "chromeos/services/multidevice_setup/setup_flow_completion_recorder.h"
class PrefRegistrySimple;
class PrefService;
namespace base {
class Clock;
}
namespace chromeos {
namespace multidevice_setup {
// Concrete SetupFlowCompletionRecorder implementation.
class SetupFlowCompletionRecorderImpl : public SetupFlowCompletionRecorder {
public:
class Factory {
public:
static Factory* Get();
static void SetFactoryForTesting(Factory* test_factory);
virtual ~Factory();
virtual std::unique_ptr<SetupFlowCompletionRecorder> BuildInstance(
PrefService* pref_service,
base::Clock* clock);
private:
static Factory* test_factory_;
};
static void RegisterPrefs(PrefRegistrySimple* registry);
~SetupFlowCompletionRecorderImpl() override;
base::Optional<base::Time> GetCompletionTimestamp() override;
void RecordCompletion() override;
private:
friend class SetupFlowCompletionRecorderImplTest;
static const char kSetupFlowCompletedPrefName[];
SetupFlowCompletionRecorderImpl(PrefService* pref_service,
base::Clock* clock);
PrefService* pref_service_;
base::Clock* clock_;
DISALLOW_COPY_AND_ASSIGN(SetupFlowCompletionRecorderImpl);
};
} // namespace multidevice_setup
} // namespace chromeos
#endif // CHROMEOS_SERVICES_MULTIDEVICE_SETUP_SETUP_FLOW_COMPLETION_RECORDER_IMPL_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/services/multidevice_setup/setup_flow_completion_recorder_impl.h"
#include <memory>
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace multidevice_setup {
namespace {
const base::Time kTestTime = base::Time::FromJavaTime(1500000000000);
} // namespace
class SetupFlowCompletionRecorderImplTest : public testing::Test {
protected:
SetupFlowCompletionRecorderImplTest() = default;
~SetupFlowCompletionRecorderImplTest() override = default;
void SetUp() override {
test_pref_service_ =
std::make_unique<sync_preferences::TestingPrefServiceSyncable>();
SetupFlowCompletionRecorderImpl::RegisterPrefs(
test_pref_service_->registry());
test_clock_ = std::make_unique<base::SimpleTestClock>();
test_clock_->SetNow(kTestTime);
recorder_ = SetupFlowCompletionRecorderImpl::Factory::Get()->BuildInstance(
test_pref_service_.get(), test_clock_.get());
}
SetupFlowCompletionRecorder* recorder() { return recorder_.get(); }
private:
std::unique_ptr<sync_preferences::TestingPrefServiceSyncable>
test_pref_service_;
std::unique_ptr<base::SimpleTestClock> test_clock_;
std::unique_ptr<SetupFlowCompletionRecorder> recorder_;
DISALLOW_COPY_AND_ASSIGN(SetupFlowCompletionRecorderImplTest);
};
TEST_F(SetupFlowCompletionRecorderImplTest, RecordsCorrectTime) {
EXPECT_FALSE(recorder()->GetCompletionTimestamp());
recorder()->RecordCompletion();
EXPECT_EQ(kTestTime, recorder()->GetCompletionTimestamp());
EXPECT_EQ(kTestTime, recorder()->GetCompletionTimestamp());
}
} // namespace multidevice_setup
} // namespace chromeos
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment