File: power_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (83 lines) | stat: -rw-r--r-- 3,172 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/power_bookmarks/common/power.h"

#include "base/time/time.h"
#include "base/uuid.h"
#include "components/sync/protocol/power_bookmark_specifics.pb.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace power_bookmarks {

namespace {
sync_pb::PowerBookmarkSpecifics CreatePowerBookmarkSpecifics() {
  sync_pb::PowerBookmarkSpecifics specifics;
  specifics.set_guid(base::Uuid::GenerateRandomV4().AsLowercaseString());
  specifics.set_url("http://google.com/");
  specifics.set_power_type(sync_pb::PowerBookmarkSpecifics::POWER_TYPE_MOCK);
  specifics.set_creation_time_usec(
      base::Time::Now().ToDeltaSinceWindowsEpoch().InMicroseconds());
  specifics.set_update_time_usec(
      base::Time::Now().ToDeltaSinceWindowsEpoch().InMicroseconds());

  return specifics;
}
}  // namespace

TEST(PowerTest, CreateFromSpecifics) {
  sync_pb::PowerBookmarkSpecifics specifics = CreatePowerBookmarkSpecifics();
  Power power(specifics);

  EXPECT_EQ(power.guid(), base::Uuid::ParseLowercase(specifics.guid()));
  EXPECT_EQ(power.url().spec(), specifics.url());
  EXPECT_EQ(power.power_type(), specifics.power_type());
  EXPECT_EQ(power.time_added(),
            base::Time::FromDeltaSinceWindowsEpoch(
                base::Microseconds(specifics.creation_time_usec())));
  EXPECT_EQ(power.time_modified(),
            base::Time::FromDeltaSinceWindowsEpoch(
                base::Microseconds(specifics.update_time_usec())));
}

TEST(PowerTest, ToAndFromSpecifics) {
  sync_pb::PowerBookmarkSpecifics specifics = CreatePowerBookmarkSpecifics();
  Power power(specifics);
  sync_pb::PowerBookmarkSpecifics new_specifics;
  power.ToPowerBookmarkSpecifics(&new_specifics);

  EXPECT_EQ(specifics.guid(), new_specifics.guid());
  EXPECT_EQ(specifics.url(), new_specifics.url());
  EXPECT_EQ(specifics.power_type(), new_specifics.power_type());
  EXPECT_EQ(specifics.creation_time_usec(), new_specifics.creation_time_usec());
  EXPECT_EQ(specifics.update_time_usec(), new_specifics.update_time_usec());
}

TEST(PowerTest, ClonePower) {
  sync_pb::PowerBookmarkSpecifics specifics = CreatePowerBookmarkSpecifics();
  Power power(specifics);
  std::unique_ptr<Power> clone = power.Clone();
  EXPECT_EQ(power.guid(), clone->guid());
  EXPECT_EQ(power.url(), clone->url());
  EXPECT_EQ(power.time_added(), clone->time_added());
  EXPECT_EQ(power.time_modified(), clone->time_modified());
  EXPECT_EQ(power.power_entity()->SerializeAsString(),
            clone->power_entity()->SerializeAsString());
}

TEST(PowerTest, MergePower) {
  sync_pb::PowerBookmarkSpecifics specifics = CreatePowerBookmarkSpecifics();
  Power power(specifics);
  Power other(specifics);
  base::Time now = base::Time::Now();
  power.set_time_added(now);
  power.set_time_modified(now);
  other.set_time_added(now + base::Seconds(1));
  other.set_time_modified(now + base::Seconds(1));
  power.Merge(other);
  EXPECT_EQ(power.time_added(), now);
  EXPECT_EQ(power.time_modified(), other.time_modified());
}

}  // namespace power_bookmarks