prerender_history_unittest.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2012 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/no_state_prefetch/browser/prerender_history.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include "base/values.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace prerender {
  10. namespace {
  11. bool ListEntryMatches(const base::Value::List& list,
  12. size_t index,
  13. const char* expected_url,
  14. FinalStatus expected_final_status,
  15. Origin expected_origin,
  16. const std::string& expected_end_time) {
  17. if (index >= list.size())
  18. return false;
  19. const base::Value& dict = list[index];
  20. if (!dict.is_dict())
  21. return false;
  22. if (dict.DictSize() != 4u)
  23. return false;
  24. const std::string* url = dict.FindStringPath("url");
  25. if (!url)
  26. return false;
  27. if (*url != expected_url)
  28. return false;
  29. const std::string* final_status = dict.FindStringPath("final_status");
  30. if (!final_status)
  31. return false;
  32. if (*final_status != NameFromFinalStatus(expected_final_status))
  33. return false;
  34. const std::string* origin = dict.FindStringPath("origin");
  35. if (!origin)
  36. return false;
  37. if (*origin != NameFromOrigin(expected_origin))
  38. return false;
  39. const std::string* end_time = dict.FindStringPath("end_time");
  40. if (!end_time)
  41. return false;
  42. if (*end_time != expected_end_time)
  43. return false;
  44. return true;
  45. }
  46. TEST(PrerenderHistoryTest, GetAsValue) {
  47. // Create a history with only 2 values.
  48. PrerenderHistory history(2);
  49. // Make sure an empty list exists when retrieving as value.
  50. base::Value::List entry_value = history.CopyEntriesAsValue();
  51. EXPECT_TRUE(entry_value.empty());
  52. // Base time used for all events. Each event is given a time 1 millisecond
  53. // after that of the previous one.
  54. base::Time epoch_start = base::Time::UnixEpoch();
  55. // Add a single entry and make sure that it matches up.
  56. const char* const kFirstUrl = "http://www.alpha.com/";
  57. const FinalStatus kFirstFinalStatus = FINAL_STATUS_USED;
  58. const Origin kFirstOrigin = ORIGIN_LINK_REL_PRERENDER_CROSSDOMAIN;
  59. PrerenderHistory::Entry entry_first(GURL(kFirstUrl), kFirstFinalStatus,
  60. kFirstOrigin, epoch_start);
  61. history.AddEntry(entry_first);
  62. entry_value = history.CopyEntriesAsValue();
  63. EXPECT_EQ(1u, entry_value.size());
  64. EXPECT_TRUE(ListEntryMatches(entry_value, 0u, kFirstUrl, kFirstFinalStatus,
  65. kFirstOrigin, "0"));
  66. // Add a second entry and make sure both first and second appear.
  67. const char* const kSecondUrl = "http://www.beta.com/";
  68. const FinalStatus kSecondFinalStatus = FINAL_STATUS_DUPLICATE;
  69. const Origin kSecondOrigin = ORIGIN_OMNIBOX;
  70. PrerenderHistory::Entry entry_second(GURL(kSecondUrl), kSecondFinalStatus,
  71. kSecondOrigin,
  72. epoch_start + base::Milliseconds(1));
  73. history.AddEntry(entry_second);
  74. entry_value = history.CopyEntriesAsValue();
  75. EXPECT_EQ(2u, entry_value.size());
  76. EXPECT_TRUE(ListEntryMatches(entry_value, 0u, kSecondUrl, kSecondFinalStatus,
  77. kSecondOrigin, "1"));
  78. EXPECT_TRUE(ListEntryMatches(entry_value, 1u, kFirstUrl, kFirstFinalStatus,
  79. kFirstOrigin, "0"));
  80. // Add a third entry and make sure that the first one drops off.
  81. const char* const kThirdUrl = "http://www.gamma.com/";
  82. const FinalStatus kThirdFinalStatus = FINAL_STATUS_AUTH_NEEDED;
  83. const Origin kThirdOrigin = ORIGIN_LINK_REL_PRERENDER_CROSSDOMAIN;
  84. PrerenderHistory::Entry entry_third(GURL(kThirdUrl), kThirdFinalStatus,
  85. kThirdOrigin,
  86. epoch_start + base::Milliseconds(2));
  87. history.AddEntry(entry_third);
  88. entry_value = history.CopyEntriesAsValue();
  89. EXPECT_EQ(2u, entry_value.size());
  90. EXPECT_TRUE(ListEntryMatches(entry_value, 0u, kThirdUrl, kThirdFinalStatus,
  91. kThirdOrigin, "2"));
  92. EXPECT_TRUE(ListEntryMatches(entry_value, 1u, kSecondUrl, kSecondFinalStatus,
  93. kSecondOrigin, "1"));
  94. // Make sure clearing history acts as expected.
  95. history.Clear();
  96. entry_value = history.CopyEntriesAsValue();
  97. EXPECT_TRUE(entry_value.empty());
  98. }
  99. } // namespace
  100. } // namespace prerender