send_tab_to_self_entry_unittest.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2019 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. #include "components/send_tab_to_self/send_tab_to_self_entry.h"
  5. #include <memory>
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "base/test/gtest_util.h"
  8. #include "base/test/simple_test_tick_clock.h"
  9. #include "components/send_tab_to_self/proto/send_tab_to_self.pb.h"
  10. #include "components/sync/protocol/send_tab_to_self_specifics.pb.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace send_tab_to_self {
  13. namespace {
  14. bool IsEqualForTesting(const SendTabToSelfEntry& a,
  15. const SendTabToSelfEntry& b) {
  16. return a.GetGUID() == b.GetGUID() && a.GetURL() == b.GetURL() &&
  17. a.GetTitle() == b.GetTitle() &&
  18. a.GetDeviceName() == b.GetDeviceName() &&
  19. a.GetTargetDeviceSyncCacheGuid() == b.GetTargetDeviceSyncCacheGuid() &&
  20. a.GetSharedTime() == b.GetSharedTime();
  21. }
  22. bool IsEqualForTesting(const SendTabToSelfEntry& entry,
  23. const sync_pb::SendTabToSelfSpecifics& specifics) {
  24. return (
  25. entry.GetGUID() == specifics.guid() &&
  26. entry.GetURL() == specifics.url() &&
  27. entry.GetTitle() == specifics.title() &&
  28. entry.GetDeviceName() == specifics.device_name() &&
  29. entry.GetTargetDeviceSyncCacheGuid() ==
  30. specifics.target_device_sync_cache_guid() &&
  31. specifics.shared_time_usec() ==
  32. entry.GetSharedTime().ToDeltaSinceWindowsEpoch().InMicroseconds());
  33. }
  34. TEST(SendTabToSelfEntry, CompareEntries) {
  35. const SendTabToSelfEntry e1("1", GURL("http://example.com"), "bar",
  36. base::Time::FromTimeT(10), "device1", "device2");
  37. const SendTabToSelfEntry e2("1", GURL("http://example.com"), "bar",
  38. base::Time::FromTimeT(10), "device1", "device2");
  39. EXPECT_TRUE(IsEqualForTesting(e1, e2));
  40. const SendTabToSelfEntry e3("2", GURL("http://example.org"), "bar",
  41. base::Time::FromTimeT(10), "device1", "device2");
  42. EXPECT_FALSE(IsEqualForTesting(e1, e3));
  43. }
  44. TEST(SendTabToSelfEntry, SharedTime) {
  45. SendTabToSelfEntry e("1", GURL("http://example.com"), "bar",
  46. base::Time::FromTimeT(10), "device", "device2");
  47. EXPECT_EQ("bar", e.GetTitle());
  48. // Getters return Base::Time values.
  49. EXPECT_EQ(e.GetSharedTime(), base::Time::FromTimeT(10));
  50. }
  51. // Tests that the send tab to self entry is correctly encoded to
  52. // sync_pb::SendTabToSelfSpecifics.
  53. TEST(SendTabToSelfEntry, AsProto) {
  54. SendTabToSelfEntry entry("1", GURL("http://example.com"), "bar",
  55. base::Time::FromTimeT(10), "device", "device2");
  56. SendTabToSelfLocal pb_entry(entry.AsLocalProto());
  57. EXPECT_TRUE(IsEqualForTesting(entry, pb_entry.specifics()));
  58. }
  59. // Tests that the send tab to self entry is correctly created from the required
  60. // fields
  61. TEST(SendTabToSelfEntry, FromRequiredFields) {
  62. SendTabToSelfEntry expected("1", GURL("http://example.com"), "", base::Time(),
  63. "", "target_device");
  64. std::unique_ptr<SendTabToSelfEntry> actual =
  65. SendTabToSelfEntry::FromRequiredFields("1", GURL("http://example.com"),
  66. "target_device");
  67. EXPECT_TRUE(IsEqualForTesting(expected, *actual));
  68. }
  69. // Tests that the send tab to self entry is correctly parsed from
  70. // sync_pb::SendTabToSelfSpecifics.
  71. TEST(SendTabToSelfEntry, FromProto) {
  72. std::unique_ptr<sync_pb::SendTabToSelfSpecifics> pb_entry =
  73. std::make_unique<sync_pb::SendTabToSelfSpecifics>();
  74. pb_entry->set_guid("1");
  75. pb_entry->set_url("http://example.com/");
  76. pb_entry->set_title("title");
  77. pb_entry->set_device_name("device");
  78. pb_entry->set_target_device_sync_cache_guid("device");
  79. pb_entry->set_shared_time_usec(1);
  80. std::unique_ptr<SendTabToSelfEntry> entry(
  81. SendTabToSelfEntry::FromProto(*pb_entry, base::Time::FromTimeT(10)));
  82. EXPECT_TRUE(IsEqualForTesting(*entry, *pb_entry));
  83. }
  84. // Tests that the send tab to self entry expiry works as expected
  85. TEST(SendTabToSelfEntry, IsExpired) {
  86. SendTabToSelfEntry entry("1", GURL("http://example.com"), "bar",
  87. base::Time::FromTimeT(10), "device1", "device1");
  88. EXPECT_TRUE(entry.IsExpired(base::Time::FromTimeT(11) + base::Days(10)));
  89. EXPECT_FALSE(entry.IsExpired(base::Time::FromTimeT(11)));
  90. }
  91. // Tests that the send tab to self entry rejects strings that are not utf8.
  92. TEST(SendTabToSelfEntry, InvalidStrings) {
  93. const char16_t term[1] = {u'\uFDD1'};
  94. std::string invalid_utf8;
  95. base::UTF16ToUTF8(&term[0], 1, &invalid_utf8);
  96. SendTabToSelfEntry invalid1("1", GURL("http://example.com"), invalid_utf8,
  97. base::Time::FromTimeT(10), "device", "device");
  98. EXPECT_EQ("1", invalid1.GetGUID());
  99. SendTabToSelfEntry invalid2(invalid_utf8, GURL("http://example.com"), "title",
  100. base::Time::FromTimeT(10), "device", "device");
  101. EXPECT_EQ(invalid_utf8, invalid2.GetGUID());
  102. SendTabToSelfEntry invalid3("1", GURL("http://example.com"), "title",
  103. base::Time::FromTimeT(10), invalid_utf8,
  104. "device");
  105. EXPECT_EQ("1", invalid3.GetGUID());
  106. SendTabToSelfEntry invalid4("1", GURL("http://example.com"), "title",
  107. base::Time::FromTimeT(10), "device",
  108. invalid_utf8);
  109. EXPECT_EQ("1", invalid4.GetGUID());
  110. std::unique_ptr<sync_pb::SendTabToSelfSpecifics> pb_entry =
  111. std::make_unique<sync_pb::SendTabToSelfSpecifics>();
  112. pb_entry->set_guid(invalid_utf8);
  113. pb_entry->set_url("http://example.com/");
  114. pb_entry->set_title(invalid_utf8);
  115. pb_entry->set_device_name(invalid_utf8);
  116. pb_entry->set_target_device_sync_cache_guid("device");
  117. pb_entry->set_shared_time_usec(1);
  118. std::unique_ptr<SendTabToSelfEntry> invalid_entry(
  119. SendTabToSelfEntry::FromProto(*pb_entry, base::Time::FromTimeT(10)));
  120. EXPECT_EQ(invalid_entry->GetGUID(), invalid_utf8);
  121. }
  122. // Tests that the send tab to self entry is correctly encoded to
  123. // sync_pb::SendTabToSelfSpecifics.
  124. TEST(SendTabToSelfEntry, MarkAsOpened) {
  125. SendTabToSelfEntry entry("1", GURL("http://example.com"), "bar",
  126. base::Time::FromTimeT(10), "device", "device2");
  127. EXPECT_FALSE(entry.IsOpened());
  128. entry.MarkOpened();
  129. EXPECT_TRUE(entry.IsOpened());
  130. std::unique_ptr<sync_pb::SendTabToSelfSpecifics> pb_entry =
  131. std::make_unique<sync_pb::SendTabToSelfSpecifics>();
  132. pb_entry->set_guid("1");
  133. pb_entry->set_url("http://example.com/");
  134. pb_entry->set_title("title");
  135. pb_entry->set_device_name("device");
  136. pb_entry->set_target_device_sync_cache_guid("device");
  137. pb_entry->set_shared_time_usec(1);
  138. pb_entry->set_opened(true);
  139. std::unique_ptr<SendTabToSelfEntry> entry2(
  140. SendTabToSelfEntry::FromProto(*pb_entry, base::Time::FromTimeT(10)));
  141. EXPECT_TRUE(entry2->IsOpened());
  142. }
  143. } // namespace
  144. } // namespace send_tab_to_self