This source file includes following definitions.
- AddQueryResults
- ResultEquals
- TEST
#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;
};
const base::Time baseline_time = base::Time::UnixEpoch().LocalMidnight();
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);
}
}
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);
}
}
TEST(HistoryUITest, MergeDuplicateResults) {
{
TestResult test_data[] = {
{ "http://google.com", 0 },
{ "http://google.de", 1 },
{ "http://google.com", 2 },
{ "http://google.com", 3 }
};
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]));
}
{
TestResult test_data[] = {
{ "http://google.com", 0 },
{ "http://google.com", 23 },
{ "http://google.com", 24 },
};
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]));
}
{
TestResult test_data[] = {
{ "http://google.de", 0 },
{ "http://google.com", 1 },
{ "http://google.de", 2 },
{ "http://google.com", 3 },
{ "http://google.de", 24 },
{ "http://google.com", 25 },
{ "http://google.de", 26 },
{ "http://google.com", 27 },
};
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]));
}
{
TestResult test_data[] = {
{ "http://google.com", 0 },
{ "http://google.de", 1 },
{ "http://google.com", 2 },
{ "http://google.com", 3 }
};
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());
}
}