1 // Copyright 2016 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 "components/sync_device_info/device_info_util.h"
6
7 #include "components/sync/protocol/sync.pb.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 using base::Time;
11 using base::TimeDelta;
12 using sync_pb::DeviceInfoSpecifics;
13
14 namespace syncer {
15
16 namespace {
17
18 class DeviceInfoUtilTest : public testing::Test {
19 protected:
DeviceInfoUtilTest()20 DeviceInfoUtilTest() {
21 // Test cases assume |small_| and |big_| are smaller and bigger,
22 // respectively,
23 // than both constants.
24 EXPECT_LT(small_, DeviceInfoUtil::kActiveThreshold);
25 EXPECT_LT(small_, DeviceInfoUtil::GetPulseInterval());
26 EXPECT_GT(big_, DeviceInfoUtil::kActiveThreshold);
27 EXPECT_GT(big_, DeviceInfoUtil::GetPulseInterval());
28 }
29
30 const Time now_ = Time::Now();
31 const TimeDelta small_ = TimeDelta::FromMilliseconds(1);
32 const TimeDelta big_ = TimeDelta::FromDays(1000);
33 };
34
35 } // namespace
36
TEST_F(DeviceInfoUtilTest,CalculatePulseDelaySame)37 TEST_F(DeviceInfoUtilTest, CalculatePulseDelaySame) {
38 EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
39 DeviceInfoUtil::CalculatePulseDelay(Time(), Time()));
40 EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
41 DeviceInfoUtil::CalculatePulseDelay(now_, now_));
42 EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
43 DeviceInfoUtil::CalculatePulseDelay(now_ + big_, now_ + big_));
44 }
45
TEST_F(DeviceInfoUtilTest,CalculatePulseDelayMiddle)46 TEST_F(DeviceInfoUtilTest, CalculatePulseDelayMiddle) {
47 EXPECT_EQ(DeviceInfoUtil::GetPulseInterval() - small_,
48 DeviceInfoUtil::CalculatePulseDelay(Time(), Time() + small_));
49 EXPECT_EQ(small_,
50 DeviceInfoUtil::CalculatePulseDelay(
51 Time(), Time() + DeviceInfoUtil::GetPulseInterval() - small_));
52 }
53
TEST_F(DeviceInfoUtilTest,CalculatePulseDelayStale)54 TEST_F(DeviceInfoUtilTest, CalculatePulseDelayStale) {
55 EXPECT_EQ(TimeDelta(),
56 DeviceInfoUtil::CalculatePulseDelay(
57 Time(), Time() + DeviceInfoUtil::GetPulseInterval()));
58 EXPECT_EQ(TimeDelta(),
59 DeviceInfoUtil::CalculatePulseDelay(
60 Time(), Time() + DeviceInfoUtil::GetPulseInterval() + small_));
61 EXPECT_EQ(TimeDelta(),
62 DeviceInfoUtil::CalculatePulseDelay(
63 Time(), Time() + DeviceInfoUtil::GetPulseInterval() + small_));
64 EXPECT_EQ(TimeDelta(), DeviceInfoUtil::CalculatePulseDelay(
65 now_, now_ + DeviceInfoUtil::GetPulseInterval()));
66 }
67
TEST_F(DeviceInfoUtilTest,CalculatePulseDelayFuture)68 TEST_F(DeviceInfoUtilTest, CalculatePulseDelayFuture) {
69 EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
70 DeviceInfoUtil::CalculatePulseDelay(Time() + small_, Time()));
71 EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
72 DeviceInfoUtil::CalculatePulseDelay(
73 Time() + DeviceInfoUtil::GetPulseInterval(), Time()));
74 EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
75 DeviceInfoUtil::CalculatePulseDelay(Time() + big_, Time()));
76 EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
77 DeviceInfoUtil::CalculatePulseDelay(now_ + big_, now_));
78 }
79
TEST_F(DeviceInfoUtilTest,IsActive)80 TEST_F(DeviceInfoUtilTest, IsActive) {
81 EXPECT_TRUE(DeviceInfoUtil::IsActive(Time(), Time()));
82 EXPECT_TRUE(DeviceInfoUtil::IsActive(now_, now_));
83 EXPECT_TRUE(DeviceInfoUtil::IsActive(now_, now_ + small_));
84 EXPECT_TRUE(DeviceInfoUtil::IsActive(
85 now_, now_ + DeviceInfoUtil::kActiveThreshold - small_));
86 EXPECT_FALSE(
87 DeviceInfoUtil::IsActive(now_, now_ + DeviceInfoUtil::kActiveThreshold));
88 EXPECT_FALSE(DeviceInfoUtil::IsActive(
89 now_, now_ + DeviceInfoUtil::kActiveThreshold + small_));
90 EXPECT_FALSE(DeviceInfoUtil::IsActive(
91 now_, now_ + DeviceInfoUtil::kActiveThreshold + big_));
92 EXPECT_TRUE(DeviceInfoUtil::IsActive(now_ + small_, now_));
93 EXPECT_TRUE(DeviceInfoUtil::IsActive(now_ + big_, now_));
94 }
95
TEST_F(DeviceInfoUtilTest,TagRoundTrip)96 TEST_F(DeviceInfoUtilTest, TagRoundTrip) {
97 DeviceInfoSpecifics specifics;
98 ASSERT_EQ("", DeviceInfoUtil::TagToCacheGuid(
99 DeviceInfoUtil::SpecificsToTag(specifics)));
100
101 std::string cache_guid("guid");
102 specifics.set_cache_guid(cache_guid);
103 ASSERT_EQ(cache_guid, DeviceInfoUtil::TagToCacheGuid(
104 DeviceInfoUtil::SpecificsToTag(specifics)));
105
106 specifics.set_cache_guid(DeviceInfoUtil::kClientTagPrefix);
107 ASSERT_EQ(DeviceInfoUtil::kClientTagPrefix,
108 DeviceInfoUtil::TagToCacheGuid(
109 DeviceInfoUtil::SpecificsToTag(specifics)));
110 }
111
112 } // namespace syncer
113