File: send_tab_to_self_entry_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (181 lines) | stat: -rw-r--r-- 7,058 bytes parent folder | download | duplicates (6)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2019 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/send_tab_to_self/send_tab_to_self_entry.h"

#include <array>
#include <memory>

#include "base/strings/utf_string_conversions.h"
#include "base/test/gtest_util.h"
#include "base/test/simple_test_tick_clock.h"
#include "components/send_tab_to_self/proto/send_tab_to_self.pb.h"
#include "components/sync/protocol/send_tab_to_self_specifics.pb.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace send_tab_to_self {

namespace {

bool IsEqualForTesting(const SendTabToSelfEntry& a,
                       const SendTabToSelfEntry& b) {
  return a.GetGUID() == b.GetGUID() && a.GetURL() == b.GetURL() &&
         a.GetTitle() == b.GetTitle() &&
         a.GetDeviceName() == b.GetDeviceName() &&
         a.GetTargetDeviceSyncCacheGuid() == b.GetTargetDeviceSyncCacheGuid() &&
         a.GetSharedTime() == b.GetSharedTime();
}

bool IsEqualForTesting(const SendTabToSelfEntry& entry,
                       const sync_pb::SendTabToSelfSpecifics& specifics) {
  return (
      entry.GetGUID() == specifics.guid() &&
      entry.GetURL() == specifics.url() &&
      entry.GetTitle() == specifics.title() &&
      entry.GetDeviceName() == specifics.device_name() &&
      entry.GetTargetDeviceSyncCacheGuid() ==
          specifics.target_device_sync_cache_guid() &&
      specifics.shared_time_usec() ==
          entry.GetSharedTime().ToDeltaSinceWindowsEpoch().InMicroseconds());
}

TEST(SendTabToSelfEntry, CompareEntries) {
  const SendTabToSelfEntry e1("1", GURL("http://example.com"), "bar",
                              base::Time::FromTimeT(10), "device1", "device2");
  const SendTabToSelfEntry e2("1", GURL("http://example.com"), "bar",
                              base::Time::FromTimeT(10), "device1", "device2");

  EXPECT_TRUE(IsEqualForTesting(e1, e2));
  const SendTabToSelfEntry e3("2", GURL("http://example.org"), "bar",
                              base::Time::FromTimeT(10), "device1", "device2");

  EXPECT_FALSE(IsEqualForTesting(e1, e3));
}

TEST(SendTabToSelfEntry, SharedTime) {
  SendTabToSelfEntry e("1", GURL("http://example.com"), "bar",
                       base::Time::FromTimeT(10), "device", "device2");
  EXPECT_EQ("bar", e.GetTitle());
  // Getters return Base::Time values.
  EXPECT_EQ(e.GetSharedTime(), base::Time::FromTimeT(10));
}

// Tests that the send tab to self entry is correctly encoded to
// sync_pb::SendTabToSelfSpecifics.
TEST(SendTabToSelfEntry, AsProto) {
  SendTabToSelfEntry entry("1", GURL("http://example.com"), "bar",
                           base::Time::FromTimeT(10), "device", "device2");
  SendTabToSelfLocal pb_entry(entry.AsLocalProto());
  EXPECT_TRUE(IsEqualForTesting(entry, pb_entry.specifics()));
}

// Tests that the send tab to self entry is correctly created from the required
// fields
TEST(SendTabToSelfEntry, FromRequiredFields) {
  SendTabToSelfEntry expected("1", GURL("http://example.com"), "", base::Time(),
                              "", "target_device");
  std::unique_ptr<SendTabToSelfEntry> actual =
      SendTabToSelfEntry::FromRequiredFields("1", GURL("http://example.com"),
                                             "target_device");
  EXPECT_TRUE(IsEqualForTesting(expected, *actual));
}

// Tests that the send tab to self entry is correctly parsed from
// sync_pb::SendTabToSelfSpecifics.
TEST(SendTabToSelfEntry, FromProto) {
  std::unique_ptr<sync_pb::SendTabToSelfSpecifics> pb_entry =
      std::make_unique<sync_pb::SendTabToSelfSpecifics>();
  pb_entry->set_guid("1");
  pb_entry->set_url("http://example.com/");
  pb_entry->set_title("title");
  pb_entry->set_device_name("device");
  pb_entry->set_target_device_sync_cache_guid("device");
  pb_entry->set_shared_time_usec(1);

  std::unique_ptr<SendTabToSelfEntry> entry(
      SendTabToSelfEntry::FromProto(*pb_entry, base::Time::FromTimeT(10)));

  EXPECT_TRUE(IsEqualForTesting(*entry, *pb_entry));
}

// Tests that the send tab to self entry expiry works as expected
TEST(SendTabToSelfEntry, IsExpired) {
  SendTabToSelfEntry entry("1", GURL("http://example.com"), "bar",
                           base::Time::FromTimeT(10), "device1", "device1");

  EXPECT_TRUE(entry.IsExpired(base::Time::FromTimeT(11) + base::Days(10)));
  EXPECT_FALSE(entry.IsExpired(base::Time::FromTimeT(11)));
}

// Tests that the send tab to self entry rejects strings that are not utf8.
TEST(SendTabToSelfEntry, InvalidStrings) {
  const std::array<char16_t, 1> term = {u'\uFDD1'};
  std::string invalid_utf8;
  base::UTF16ToUTF8(&term[0], 1, &invalid_utf8);

  SendTabToSelfEntry invalid1("1", GURL("http://example.com"), invalid_utf8,
                              base::Time::FromTimeT(10), "device", "device");

  EXPECT_EQ("1", invalid1.GetGUID());

  SendTabToSelfEntry invalid2(invalid_utf8, GURL("http://example.com"), "title",
                              base::Time::FromTimeT(10), "device", "device");

  EXPECT_EQ(invalid_utf8, invalid2.GetGUID());

  SendTabToSelfEntry invalid3("1", GURL("http://example.com"), "title",
                              base::Time::FromTimeT(10), invalid_utf8,
                              "device");

  EXPECT_EQ("1", invalid3.GetGUID());

  SendTabToSelfEntry invalid4("1", GURL("http://example.com"), "title",
                              base::Time::FromTimeT(10), "device",
                              invalid_utf8);

  EXPECT_EQ("1", invalid4.GetGUID());

  std::unique_ptr<sync_pb::SendTabToSelfSpecifics> pb_entry =
      std::make_unique<sync_pb::SendTabToSelfSpecifics>();
  pb_entry->set_guid(invalid_utf8);
  pb_entry->set_url("http://example.com/");
  pb_entry->set_title(invalid_utf8);
  pb_entry->set_device_name(invalid_utf8);
  pb_entry->set_target_device_sync_cache_guid("device");
  pb_entry->set_shared_time_usec(1);

  std::unique_ptr<SendTabToSelfEntry> invalid_entry(
      SendTabToSelfEntry::FromProto(*pb_entry, base::Time::FromTimeT(10)));

  EXPECT_EQ(invalid_entry->GetGUID(), invalid_utf8);
}

// Tests that the send tab to self entry is correctly encoded to
// sync_pb::SendTabToSelfSpecifics.
TEST(SendTabToSelfEntry, MarkAsOpened) {
  SendTabToSelfEntry entry("1", GURL("http://example.com"), "bar",
                           base::Time::FromTimeT(10), "device", "device2");
  EXPECT_FALSE(entry.IsOpened());
  entry.MarkOpened();
  EXPECT_TRUE(entry.IsOpened());

  std::unique_ptr<sync_pb::SendTabToSelfSpecifics> pb_entry =
      std::make_unique<sync_pb::SendTabToSelfSpecifics>();
  pb_entry->set_guid("1");
  pb_entry->set_url("http://example.com/");
  pb_entry->set_title("title");
  pb_entry->set_device_name("device");
  pb_entry->set_target_device_sync_cache_guid("device");
  pb_entry->set_shared_time_usec(1);
  pb_entry->set_opened(true);

  std::unique_ptr<SendTabToSelfEntry> entry2(
      SendTabToSelfEntry::FromProto(*pb_entry, base::Time::FromTimeT(10)));

  EXPECT_TRUE(entry2->IsOpened());
}

}  // namespace

}  // namespace send_tab_to_self