summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/history_ui_unittest.cc
blob: 58d1ddc4f7d6ddc2a60f34718eec23a452d1bd06 (plain)
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
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/history_ui.h"

#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

struct TestResult {
  std::string url;
  int64 hour_offset;  // Visit time in hours past the baseline time.
};

// Duplicates on the same day in the local timezone are removed, so set a
// baseline time in local time.
const base::Time baseline_time = base::Time::UnixEpoch().LocalMidnight();

// For each item in |results|, create a new Value representing the visit, and
// insert it into |list_value|.
void AddQueryResults(
    TestResult* test_results,
    int test_results_size,
    std::vector<BrowsingHistoryHandler::HistoryEntry>* results) {
  for (int i = 0; i < test_results_size; ++i) {
    BrowsingHistoryHandler::HistoryEntry entry;
    entry.time = baseline_time +
                 base::TimeDelta::FromHours(test_results[i].hour_offset);
    entry.url = GURL(test_results[i].url);
    entry.all_timestamps.insert(entry.time.ToInternalValue());
    results->push_back(entry);
  }
}

// Returns true if |result| matches the test data given by |correct_result|,
// otherwise returns false.
bool ResultEquals(
    const BrowsingHistoryHandler::HistoryEntry& result,
    const TestResult& correct_result) {
  base::Time correct_time =
      baseline_time + base::TimeDelta::FromHours(correct_result.hour_offset);

  return result.time == correct_time && result.url == GURL(correct_result.url);
}

}  // namespace

// Tests that the MergeDuplicateResults method correctly removes duplicate
// visits to the same URL on the same day.
TEST(HistoryUITest, MergeDuplicateResults) {
  {
    // Basic test that duplicates on the same day are removed.
    TestResult test_data[] = {
      { "http://google.com", 0 },
      { "http://google.de", 1 },
      { "http://google.com", 2 },
      { "http://google.com", 3 }  // Most recent.
    };
    std::vector<BrowsingHistoryHandler::HistoryEntry> results;
    AddQueryResults(test_data, arraysize(test_data), &results);
    BrowsingHistoryHandler::MergeDuplicateResults(&results);

    ASSERT_EQ(2U, results.size());
    EXPECT_TRUE(ResultEquals(results[0], test_data[3]));
    EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
  }

  {
    // Test that a duplicate URL on the next day is not removed.
    TestResult test_data[] = {
      { "http://google.com", 0 },
      { "http://google.com", 23 },
      { "http://google.com", 24 },  // Most recent.
    };
    std::vector<BrowsingHistoryHandler::HistoryEntry> results;
    AddQueryResults(test_data, arraysize(test_data), &results);
    BrowsingHistoryHandler::MergeDuplicateResults(&results);

    ASSERT_EQ(2U, results.size());
    EXPECT_TRUE(ResultEquals(results[0], test_data[2]));
    EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
  }

  {
    // Test multiple duplicates across multiple days.
    TestResult test_data[] = {
      // First day.
      { "http://google.de", 0 },
      { "http://google.com", 1 },
      { "http://google.de", 2 },
      { "http://google.com", 3 },

      // Second day.
      { "http://google.de", 24 },
      { "http://google.com", 25 },
      { "http://google.de", 26 },
      { "http://google.com", 27 },  // Most recent.
    };
    std::vector<BrowsingHistoryHandler::HistoryEntry> results;
    AddQueryResults(test_data, arraysize(test_data), &results);
    BrowsingHistoryHandler::MergeDuplicateResults(&results);

    ASSERT_EQ(4U, results.size());
    EXPECT_TRUE(ResultEquals(results[0], test_data[7]));
    EXPECT_TRUE(ResultEquals(results[1], test_data[6]));
    EXPECT_TRUE(ResultEquals(results[2], test_data[3]));
    EXPECT_TRUE(ResultEquals(results[3], test_data[2]));
  }

  {
    // Test that timestamps for duplicates are properly saved.
    TestResult test_data[] = {
      { "http://google.com", 0 },
      { "http://google.de", 1 },
      { "http://google.com", 2 },
      { "http://google.com", 3 }  // Most recent.
    };
    std::vector<BrowsingHistoryHandler::HistoryEntry> results;
    AddQueryResults(test_data, arraysize(test_data), &results);
    BrowsingHistoryHandler::MergeDuplicateResults(&results);

    ASSERT_EQ(2U, results.size());
    EXPECT_TRUE(ResultEquals(results[0], test_data[3]));
    EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
    EXPECT_EQ(3u, results[0].all_timestamps.size());
    EXPECT_EQ(1u, results[1].all_timestamps.size());
  }
}