Commit 4f42c285 authored by Hesen Zhang's avatar Hesen Zhang Committed by Commit Bot

[Upboarding]: Complete GetTiles flow.

Keep GetTiles as async call but return the copies of tile data.

Bug: 1066550
Change-Id: I0b2eddd7063c5f6ded269c4223cd3e67da2aedf8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2161479
Commit-Queue: Hesen Zhang <hesen@chromium.org>
Auto-Submit: Hesen Zhang <hesen@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#763534}
parent 4c2554aa
...@@ -23,37 +23,37 @@ namespace upboarding { ...@@ -23,37 +23,37 @@ namespace upboarding {
namespace { namespace {
const char kTileProviderBridgeKey[] = "tile_provider_bridge"; const char kTileProviderBridgeKey[] = "tile_provider_bridge";
ScopedJavaLocalRef<jobject> createJavaTileAndMaybeAddToList( ScopedJavaLocalRef<jobject> CreateJavaTileAndMaybeAddToList(
JNIEnv* env, JNIEnv* env,
ScopedJavaLocalRef<jobject> jlist, ScopedJavaLocalRef<jobject> jlist,
Tile* tile) { const Tile& tile) {
ScopedJavaLocalRef<jobject> jchildren = ScopedJavaLocalRef<jobject> jchildren =
Java_TileProviderBridge_createList(env); Java_TileProviderBridge_createList(env);
for (const auto& subtile : tile->sub_tiles) for (const auto& subtile : tile.sub_tiles)
createJavaTileAndMaybeAddToList(env, jchildren, subtile.get()); CreateJavaTileAndMaybeAddToList(env, jchildren, *subtile.get());
return Java_TileProviderBridge_createTileAndMaybeAddToList( return Java_TileProviderBridge_createTileAndMaybeAddToList(
env, jlist, ConvertUTF8ToJavaString(env, tile->id), env, jlist, ConvertUTF8ToJavaString(env, tile.id),
ConvertUTF8ToJavaString(env, tile->display_text), ConvertUTF8ToJavaString(env, tile.display_text),
ConvertUTF8ToJavaString(env, tile->accessibility_text), ConvertUTF8ToJavaString(env, tile.accessibility_text),
ConvertUTF8ToJavaString(env, tile->query_text), jchildren); ConvertUTF8ToJavaString(env, tile.query_text), jchildren);
} }
ScopedJavaLocalRef<jobject> createJavaTiles(JNIEnv* env, ScopedJavaLocalRef<jobject> CreateJavaTiles(JNIEnv* env,
const std::vector<Tile*>& tiles) { std::vector<Tile> tiles) {
ScopedJavaLocalRef<jobject> jlist = Java_TileProviderBridge_createList(env); ScopedJavaLocalRef<jobject> jlist = Java_TileProviderBridge_createList(env);
for (Tile* tile : tiles) for (const auto& tile : tiles)
createJavaTileAndMaybeAddToList(env, jlist, tile); CreateJavaTileAndMaybeAddToList(env, jlist, tile);
return jlist; return jlist;
} }
void RunGetTilesCallback(const JavaRef<jobject>& j_callback, void RunGetTilesCallback(const JavaRef<jobject>& j_callback,
const std::vector<Tile*>& tiles) { std::vector<Tile> tiles) {
JNIEnv* env = AttachCurrentThread(); JNIEnv* env = AttachCurrentThread();
RunObjectCallbackAndroid(j_callback, createJavaTiles(env, tiles)); RunObjectCallbackAndroid(j_callback, CreateJavaTiles(env, std::move(tiles)));
} }
void RunGeVisualsCallback(const JavaRef<jobject>& j_callback, void RunGeVisualsCallback(const JavaRef<jobject>& j_callback,
......
...@@ -52,6 +52,7 @@ source_set("unit_tests") { ...@@ -52,6 +52,7 @@ source_set("unit_tests") {
"proto_conversion_unittest.cc", "proto_conversion_unittest.cc",
"tile_fetcher_unittest.cc", "tile_fetcher_unittest.cc",
"tile_group_unittest.cc", "tile_group_unittest.cc",
"tile_manager_unittest.cc",
"tile_store_unittest.cc", "tile_store_unittest.cc",
] ]
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/guid.h" #include "base/guid.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/upboarding/query_tiles/internal/tile_manager.h" #include "chrome/browser/upboarding/query_tiles/internal/tile_manager.h"
...@@ -55,17 +57,14 @@ class TileManagerImpl : public TileManager { ...@@ -55,17 +57,14 @@ class TileManagerImpl : public TileManager {
std::move(group), std::move(callback))); std::move(group), std::move(callback)));
} }
void GetTiles(std::vector<Tile*>* tiles) override { void GetTiles(GetTilesCallback callback) override {
DCHECK(tiles); std::vector<Tile> tiles;
if (!initialized_)
return;
tiles->clear();
if (tile_group_ && ValidateGroup(tile_group_.get())) { if (tile_group_ && ValidateGroup(tile_group_.get())) {
for (const auto& tile : tile_group_->tiles) for (const auto& tile : tile_group_->tiles)
tiles->emplace_back(tile.get()); tiles.emplace_back(*tile.get());
} }
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), std::move(tiles)));
} }
void OnTileStoreInitialized( void OnTileStoreInitialized(
...@@ -110,7 +109,8 @@ class TileManagerImpl : public TileManager { ...@@ -110,7 +109,8 @@ class TileManagerImpl : public TileManager {
std::move(callback).Run(status); std::move(callback).Run(status);
} }
// Returns true if the group is not expired and the locale matches OS setting. // Returns true if the group is not expired and the locale matches OS
// setting.
bool ValidateGroup(const TileGroup* group) const { bool ValidateGroup(const TileGroup* group) const {
return clock_->Now() - group->last_updated_ts < config_->expire_duration && return clock_->Now() - group->last_updated_ts < config_->expire_duration &&
group->locale == config_->locale; group->locale == config_->locale;
......
...@@ -24,6 +24,7 @@ class TileManager { ...@@ -24,6 +24,7 @@ class TileManager {
public: public:
using TileStore = Store<TileGroup>; using TileStore = Store<TileGroup>;
using TileGroupStatusCallback = base::OnceCallback<void(TileGroupStatus)>; using TileGroupStatusCallback = base::OnceCallback<void(TileGroupStatus)>;
using GetTilesCallback = base::OnceCallback<void(std::vector<Tile>)>;
// Creates the instance. // Creates the instance.
static std::unique_ptr<TileManager> Create( static std::unique_ptr<TileManager> Create(
...@@ -36,7 +37,7 @@ class TileManager { ...@@ -36,7 +37,7 @@ class TileManager {
virtual void Init(TileGroupStatusCallback callback) = 0; virtual void Init(TileGroupStatusCallback callback) = 0;
// Returns tiles to the caller. // Returns tiles to the caller.
virtual void GetTiles(std::vector<Tile*>* tiles) = 0; virtual void GetTiles(GetTilesCallback callback) = 0;
// Save the query tiles into database. // Save the query tiles into database.
virtual void SaveTiles(std::vector<std::unique_ptr<Tile>> top_level_tiles, virtual void SaveTiles(std::vector<std::unique_ptr<Tile>> top_level_tiles,
......
...@@ -85,10 +85,17 @@ class TileManagerTest : public testing::Test { ...@@ -85,10 +85,17 @@ class TileManagerTest : public testing::Test {
// Run GetTiles call from manager_, compare the |expected| to the actual // Run GetTiles call from manager_, compare the |expected| to the actual
// returned tiles. // returned tiles.
void GetTiles(std::vector<Tile*> expected) { void GetTiles(base::RepeatingClosure closure, std::vector<Tile> expected) {
std::vector<Tile*> actual; manager()->GetTiles(base::BindOnce(
manager()->GetTiles(&actual); &TileManagerTest::OnTilesReturned, base::Unretained(this),
EXPECT_TRUE(test::AreTilesIdentical(expected, actual)); std::move(closure), std::move(expected)));
}
void OnTilesReturned(base::RepeatingClosure closure,
std::vector<Tile> expected,
std::vector<Tile> tiles) {
EXPECT_TRUE(test::AreTilesIdentical(expected, tiles));
std::move(closure).Run();
} }
protected: protected:
...@@ -105,6 +112,9 @@ class TileManagerTest : public testing::Test { ...@@ -105,6 +112,9 @@ class TileManagerTest : public testing::Test {
base::SimpleTestClock clock_; base::SimpleTestClock clock_;
}; };
// TODO(hesen): Add a test where we request tiles before the initialize
// callback from the DB.
TEST_F(TileManagerTest, InitAndLoadWithDbOperationFailed) { TEST_F(TileManagerTest, InitAndLoadWithDbOperationFailed) {
EXPECT_CALL(*tile_store(), InitAndLoad(_)) EXPECT_CALL(*tile_store(), InitAndLoad(_))
.WillOnce(Invoke([](base::OnceCallback<void( .WillOnce(Invoke([](base::OnceCallback<void(
...@@ -114,7 +124,7 @@ TEST_F(TileManagerTest, InitAndLoadWithDbOperationFailed) { ...@@ -114,7 +124,7 @@ TEST_F(TileManagerTest, InitAndLoadWithDbOperationFailed) {
base::RunLoop loop; base::RunLoop loop;
Init(loop.QuitClosure(), TileGroupStatus::kFailureDbOperation); Init(loop.QuitClosure(), TileGroupStatus::kFailureDbOperation);
GetTiles(std::vector<Tile*>() /*expect an empty result*/); GetTiles(loop.QuitClosure(), std::vector<Tile>() /*expect an empty result*/);
loop.Run(); loop.Run();
} }
...@@ -127,7 +137,7 @@ TEST_F(TileManagerTest, InitWithEmptyDb) { ...@@ -127,7 +137,7 @@ TEST_F(TileManagerTest, InitWithEmptyDb) {
base::RunLoop loop; base::RunLoop loop;
Init(loop.QuitClosure(), TileGroupStatus::kSuccess); Init(loop.QuitClosure(), TileGroupStatus::kSuccess);
GetTiles(std::vector<Tile*>() /*expect an empty result*/); GetTiles(loop.QuitClosure(), std::vector<Tile>() /*expect an empty result*/);
loop.Run(); loop.Run();
} }
...@@ -148,7 +158,7 @@ TEST_F(TileManagerTest, InitAndLoadWithLocaleNotMatch) { ...@@ -148,7 +158,7 @@ TEST_F(TileManagerTest, InitAndLoadWithLocaleNotMatch) {
base::RunLoop loop; base::RunLoop loop;
Init(loop.QuitClosure(), TileGroupStatus::kInvalidGroup); Init(loop.QuitClosure(), TileGroupStatus::kInvalidGroup);
GetTiles(std::vector<Tile*>() /*expect an empty result*/); GetTiles(loop.QuitClosure(), std::vector<Tile>() /*expect an empty result*/);
loop.Run(); loop.Run();
} }
...@@ -170,18 +180,18 @@ TEST_F(TileManagerTest, InitAndLoadWithExpiredGroup) { ...@@ -170,18 +180,18 @@ TEST_F(TileManagerTest, InitAndLoadWithExpiredGroup) {
base::RunLoop loop; base::RunLoop loop;
Init(loop.QuitClosure(), TileGroupStatus::kInvalidGroup); Init(loop.QuitClosure(), TileGroupStatus::kInvalidGroup);
GetTiles(std::vector<Tile*>() /*expect an empty result*/); GetTiles(loop.QuitClosure(), std::vector<Tile>() /*expect an empty result*/);
loop.Run(); loop.Run();
} }
TEST_F(TileManagerTest, InitAndLoadSuccess) { TEST_F(TileManagerTest, InitAndLoadSuccess) {
auto input_group = std::make_unique<TileGroup>(); auto input_group = std::make_unique<TileGroup>();
test::ResetTestGroup(input_group.get()); test::ResetTestGroup(input_group.get());
std::vector<Tile*> expected; std::vector<Tile> expected;
input_group->last_updated_ts = input_group->last_updated_ts =
clock()->Now() - base::TimeDelta::FromMinutes(5); clock()->Now() - base::TimeDelta::FromMinutes(5);
for (const auto& tile : input_group->tiles) for (const auto& tile : input_group->tiles)
expected.emplace_back(tile.get()); expected.emplace_back(*tile.get());
MockTileStore::KeysAndEntries input; MockTileStore::KeysAndEntries input;
input[input_group->id] = std::move(input_group); input[input_group->id] = std::move(input_group);
...@@ -196,7 +206,7 @@ TEST_F(TileManagerTest, InitAndLoadSuccess) { ...@@ -196,7 +206,7 @@ TEST_F(TileManagerTest, InitAndLoadSuccess) {
base::RunLoop loop; base::RunLoop loop;
Init(loop.QuitClosure(), TileGroupStatus::kSuccess); Init(loop.QuitClosure(), TileGroupStatus::kSuccess);
GetTiles(expected); GetTiles(loop.QuitClosure(), expected);
loop.Run(); loop.Run();
} }
...@@ -221,7 +231,7 @@ TEST_F(TileManagerTest, SaveTilesWhenUnintialized) { ...@@ -221,7 +231,7 @@ TEST_F(TileManagerTest, SaveTilesWhenUnintialized) {
SaveTiles(std::move(tiles_to_save), loop.QuitClosure(), SaveTiles(std::move(tiles_to_save), loop.QuitClosure(),
TileGroupStatus::kUninitialized); TileGroupStatus::kUninitialized);
GetTiles(std::vector<Tile*>() /*expect an empty result*/); GetTiles(loop.QuitClosure(), std::vector<Tile>() /*expect an empty result*/);
loop.Run(); loop.Run();
} }
...@@ -251,7 +261,7 @@ TEST_F(TileManagerTest, SaveTilesFailed) { ...@@ -251,7 +261,7 @@ TEST_F(TileManagerTest, SaveTilesFailed) {
SaveTiles(std::move(tiles_to_save), loop.QuitClosure(), SaveTiles(std::move(tiles_to_save), loop.QuitClosure(),
TileGroupStatus::kFailureDbOperation); TileGroupStatus::kFailureDbOperation);
GetTiles(std::vector<Tile*>() /*expect an empty result*/); GetTiles(loop.QuitClosure(), std::vector<Tile>() /*expect an empty result*/);
loop.Run(); loop.Run();
} }
...@@ -280,12 +290,12 @@ TEST_F(TileManagerTest, SaveTilesSuccess) { ...@@ -280,12 +290,12 @@ TEST_F(TileManagerTest, SaveTilesSuccess) {
test::ResetTestEntry(expected_tile.get()); test::ResetTestEntry(expected_tile.get());
std::vector<std::unique_ptr<Tile>> tiles_to_save; std::vector<std::unique_ptr<Tile>> tiles_to_save;
tiles_to_save.emplace_back(std::move(tile_to_save)); tiles_to_save.emplace_back(std::move(tile_to_save));
std::vector<Tile*> expected; std::vector<Tile> expected;
expected.emplace_back(expected_tile.get()); expected.emplace_back(*expected_tile.get());
SaveTiles(std::move(tiles_to_save), loop.QuitClosure(), SaveTiles(std::move(tiles_to_save), loop.QuitClosure(),
TileGroupStatus::kSuccess); TileGroupStatus::kSuccess);
GetTiles(std::move(expected)); GetTiles(loop.QuitClosure(), std::move(expected));
loop.Run(); loop.Run();
} }
...@@ -326,12 +336,12 @@ TEST_F(TileManagerTest, SaveTilesAndReplaceOldGroupSuccess) { ...@@ -326,12 +336,12 @@ TEST_F(TileManagerTest, SaveTilesAndReplaceOldGroupSuccess) {
auto expected_tile = std::make_unique<Tile>(); auto expected_tile = std::make_unique<Tile>();
test::ResetTestEntry(expected_tile.get()); test::ResetTestEntry(expected_tile.get());
std::vector<Tile*> expected; std::vector<Tile> expected;
expected.emplace_back(std::move(expected_tile.get())); expected.emplace_back(std::move(*expected_tile.get()));
SaveTiles(std::move(tiles_to_save), loop.QuitClosure(), SaveTiles(std::move(tiles_to_save), loop.QuitClosure(),
TileGroupStatus::kSuccess); TileGroupStatus::kSuccess);
GetTiles(std::move(expected)); GetTiles(loop.QuitClosure(), std::move(expected));
loop.Run(); loop.Run();
} }
......
...@@ -21,7 +21,7 @@ TileServiceImpl::TileServiceImpl(std::unique_ptr<ImageLoader> image_loader, ...@@ -21,7 +21,7 @@ TileServiceImpl::TileServiceImpl(std::unique_ptr<ImageLoader> image_loader,
TileServiceImpl::~TileServiceImpl() = default; TileServiceImpl::~TileServiceImpl() = default;
void TileServiceImpl::GetQueryTiles(GetTilesCallback callback) { void TileServiceImpl::GetQueryTiles(GetTilesCallback callback) {
NOTIMPLEMENTED(); tile_manager_->GetTiles(std::move(callback));
} }
void TileServiceImpl::GetVisuals(const std::string& tile_id, void TileServiceImpl::GetVisuals(const std::string& tile_id,
......
...@@ -161,16 +161,27 @@ bool AreTilesIdentical(const Tile& lhs, const Tile& rhs) { ...@@ -161,16 +161,27 @@ bool AreTilesIdentical(const Tile& lhs, const Tile& rhs) {
} }
bool AreTilesIdentical(std::vector<Tile*> lhs, std::vector<Tile*> rhs) { bool AreTilesIdentical(std::vector<Tile*> lhs, std::vector<Tile*> rhs) {
std::vector<Tile> lhs_copy, rhs_copy;
for (auto* tile : lhs)
lhs_copy.emplace_back(*tile);
for (auto* tile : rhs)
rhs_copy.emplace_back(*tile);
return AreTilesIdentical(std::move(lhs_copy), std::move(rhs_copy));
}
bool AreTilesIdentical(std::vector<Tile> lhs, std::vector<Tile> rhs) {
if (lhs.size() != rhs.size()) if (lhs.size() != rhs.size())
return false; return false;
auto entry_comparator = [](Tile* a, Tile* b) { return a->id < b->id; }; auto entry_comparator = [](const Tile& a, const Tile& b) {
return a.id < b.id;
};
std::sort(lhs.begin(), lhs.end(), entry_comparator); std::sort(lhs.begin(), lhs.end(), entry_comparator);
std::sort(rhs.begin(), rhs.end(), entry_comparator); std::sort(rhs.begin(), rhs.end(), entry_comparator);
for (size_t i = 0; i < lhs.size(); i++) { for (size_t i = 0; i < lhs.size(); i++) {
if (*lhs[i] != *rhs[i]) if (!AreTilesIdentical(lhs[i], rhs[i]))
return false; return false;
} }
......
...@@ -38,6 +38,9 @@ bool AreTilesIdentical(const Tile& lhs, const Tile& rhs); ...@@ -38,6 +38,9 @@ bool AreTilesIdentical(const Tile& lhs, const Tile& rhs);
// Returns true if all data in two lists of Tile are identical. // Returns true if all data in two lists of Tile are identical.
bool AreTilesIdentical(std::vector<Tile*> lhs, std::vector<Tile*> rhs); bool AreTilesIdentical(std::vector<Tile*> lhs, std::vector<Tile*> rhs);
// Returns true if all data in two lists of Tile are identical.
bool AreTilesIdentical(std::vector<Tile> lhs, std::vector<Tile> rhs);
} // namespace test } // namespace test
} // namespace upboarding } // namespace upboarding
......
...@@ -19,7 +19,8 @@ class Image; ...@@ -19,7 +19,8 @@ class Image;
namespace upboarding { namespace upboarding {
using GetTilesCallback = base::OnceCallback<void(const std::vector<Tile*>&)>; using TileList = std::vector<Tile>;
using GetTilesCallback = base::OnceCallback<void(TileList)>;
using VisualsCallback = base::OnceCallback<void(const gfx::Image&)>; using VisualsCallback = base::OnceCallback<void(const gfx::Image&)>;
// The central class on chrome client responsible for fetching, storing, // The central class on chrome client responsible for fetching, storing,
......
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