Commit edef1f74 authored by Hesen Zhang's avatar Hesen Zhang Committed by Commit Bot

[Upboarding]: QueryTileEntry unittest.

Bug: 1060805c
Change-Id: Id2ca8e91cd3f9626d570f68f69563d830f2ff7b9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2128940Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarHesen Zhang <hesen@chromium.org>
Commit-Queue: Hesen Zhang <hesen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755109}
parent 0e19fa7c
...@@ -116,8 +116,14 @@ if (is_android) { ...@@ -116,8 +116,14 @@ if (is_android) {
} }
} }
group("unit_tests") { source_set("unit_tests") {
testonly = true testonly = true
sources = [ "query_tile_entry_unittest.cc" ]
visibility = [ "//chrome/browser/upboarding:unit_tests" ] visibility = [ "//chrome/browser/upboarding:unit_tests" ]
deps = [ "//chrome/browser/upboarding/query_tiles/internal:unit_tests" ] deps = [
":public",
"//chrome/browser/upboarding/query_tiles/internal:unit_tests",
"//testing/gtest",
"//url",
]
} }
...@@ -32,7 +32,9 @@ bool AreTreesIdentical(const QueryTileEntry* lhs, const QueryTileEntry* rhs) { ...@@ -32,7 +32,9 @@ bool AreTreesIdentical(const QueryTileEntry* lhs, const QueryTileEntry* rhs) {
if (!lhs || !rhs || lhs->id != rhs->id || if (!lhs || !rhs || lhs->id != rhs->id ||
lhs->display_text != rhs->display_text || lhs->display_text != rhs->display_text ||
lhs->query_text != rhs->query_text || lhs->query_text != rhs->query_text ||
lhs->accessibility_text != rhs->accessibility_text) lhs->accessibility_text != rhs->accessibility_text ||
lhs->image_metadatas.size() != rhs->image_metadatas.size() ||
lhs->sub_tiles.size() != rhs->sub_tiles.size())
return false; return false;
for (const auto& it : lhs->image_metadatas) { for (const auto& it : lhs->image_metadatas) {
......
// Copyright 2020 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/upboarding/query_tiles/query_tile_entry.h"
#include "base/test/task_environment.h"
#include "chrome/browser/upboarding/query_tiles/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace upboarding {
namespace {
void ResetTestCase(QueryTileEntry& entry) {
entry.id = "test-guid-root";
entry.query_text = "test query str";
entry.display_text = "test display text";
entry.accessibility_text = "read this test display text";
entry.image_metadatas.clear();
entry.image_metadatas.emplace_back("image-test-id-1",
GURL("http://www.example.com"));
entry.image_metadatas.emplace_back("image-test-id-2",
GURL("http://www.fakeurl.com"));
auto entry1 = std::make_unique<QueryTileEntry>();
entry1->id = "test-guid-001";
auto entry2 = std::make_unique<QueryTileEntry>();
entry2->id = "test-guid-002";
auto entry3 = std::make_unique<QueryTileEntry>();
entry3->id = "test-guid-003";
entry1->sub_tiles.clear();
entry1->sub_tiles.emplace_back(std::move(entry3));
entry.sub_tiles.clear();
entry.sub_tiles.emplace_back(std::move(entry1));
entry.sub_tiles.emplace_back(std::move(entry2));
}
TEST(QueryTileEntryTest, CompareOperators) {
QueryTileEntry lhs, rhs;
ResetTestCase(lhs);
ResetTestCase(rhs);
EXPECT_EQ(lhs, rhs);
EXPECT_FALSE(lhs != rhs);
// Test any data field changed.
rhs.id = "changed";
EXPECT_NE(lhs, rhs);
ResetTestCase(rhs);
rhs.query_text = "changed";
EXPECT_NE(lhs, rhs);
ResetTestCase(rhs);
rhs.display_text = "changed";
EXPECT_NE(lhs, rhs);
ResetTestCase(rhs);
rhs.accessibility_text = "changed";
EXPECT_NE(lhs, rhs);
ResetTestCase(rhs);
// Test image metadatas changed.
rhs.image_metadatas.front().id = "changed";
EXPECT_NE(lhs, rhs);
ResetTestCase(rhs);
rhs.image_metadatas.front().url = GURL("http://www.url-changed.com");
EXPECT_NE(lhs, rhs);
ResetTestCase(rhs);
rhs.image_metadatas.pop_back();
EXPECT_NE(lhs, rhs);
ResetTestCase(rhs);
rhs.image_metadatas.emplace_back(ImageMetadata());
EXPECT_NE(lhs, rhs);
ResetTestCase(rhs);
// Test children changed.
rhs.sub_tiles.front()->id = "changed";
EXPECT_NE(lhs, rhs);
ResetTestCase(rhs);
rhs.sub_tiles.pop_back();
EXPECT_NE(lhs, rhs);
ResetTestCase(rhs);
rhs.sub_tiles.emplace_back(std::make_unique<QueryTileEntry>());
EXPECT_NE(lhs, rhs);
}
TEST(QueryTileEntryTest, CopyOperator) {
QueryTileEntry lhs;
ResetTestCase(lhs);
QueryTileEntry rhs(lhs);
EXPECT_EQ(lhs, rhs);
}
TEST(QueryTileEntryTest, AssignOperator) {
QueryTileEntry lhs;
ResetTestCase(lhs);
QueryTileEntry rhs = lhs;
EXPECT_EQ(lhs, rhs);
}
TEST(QueryTileEntryTest, MoveOperator) {
QueryTileEntry lhs;
ResetTestCase(lhs);
QueryTileEntry rhs = std::move(lhs);
EXPECT_EQ(lhs, QueryTileEntry());
QueryTileEntry expected;
ResetTestCase(expected);
EXPECT_EQ(expected, rhs);
}
} // namespace
} // namespace upboarding
...@@ -23,8 +23,8 @@ void SerializeEntry(const QueryTileEntry* entry, std::stringstream& out) { ...@@ -23,8 +23,8 @@ void SerializeEntry(const QueryTileEntry* entry, std::stringstream& out) {
<< " accessibility_text: " << entry->accessibility_text << " \n"; << " accessibility_text: " << entry->accessibility_text << " \n";
for (const auto& image : entry->image_metadatas) for (const auto& image : entry->image_metadatas)
out << "image id: " << image.id << " image url: " << image.url.spec() out << "image id: " << image.id
<< " \n"; << " image url: " << image.url.possibly_invalid_spec() << " \n";
} }
} // namespace } // namespace
...@@ -33,7 +33,7 @@ const std::string DebugString(const QueryTileEntry* root) { ...@@ -33,7 +33,7 @@ const std::string DebugString(const QueryTileEntry* root) {
if (!root) if (!root)
return std::string(); return std::string();
std::stringstream out; std::stringstream out;
out << "entries detail: \n"; out << "Entries detail: \n";
std::map<std::string, std::vector<std::string>> cache; std::map<std::string, std::vector<std::string>> cache;
std::deque<const QueryTileEntry*> queue; std::deque<const QueryTileEntry*> queue;
queue.emplace_back(root); queue.emplace_back(root);
...@@ -49,7 +49,7 @@ const std::string DebugString(const QueryTileEntry* root) { ...@@ -49,7 +49,7 @@ const std::string DebugString(const QueryTileEntry* root) {
} }
} }
} }
out << "tree table: \n"; out << "Tree table: \n";
for (auto& pair : cache) { for (auto& pair : cache) {
std::string line; std::string line;
line += pair.first + " : ["; line += pair.first + " : [";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment