Commit 9e7372c7 authored by Shakti Sahu's avatar Shakti Sahu Committed by Commit Bot

Video Tutorials : Added TutorialManager

This CL adds TutorialManager class to the video tutorials. Also
cleaned up a few methods on the storage layer.

Bug: 1130260
Change-Id: Ida331aefc538daddccd30175cd5652152cf4ab4d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2420779
Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809192}
parent e8c969b6
...@@ -16,6 +16,9 @@ source_set("internal") { ...@@ -16,6 +16,9 @@ source_set("internal") {
"store.h", "store.h",
"tutorial_group.cc", "tutorial_group.cc",
"tutorial_group.h", "tutorial_group.h",
"tutorial_manager.h",
"tutorial_manager_impl.cc",
"tutorial_manager_impl.h",
"tutorial_service_impl.cc", "tutorial_service_impl.cc",
"tutorial_service_impl.h", "tutorial_service_impl.h",
"tutorial_store.cc", "tutorial_store.cc",
...@@ -88,6 +91,7 @@ source_set("unit_tests") { ...@@ -88,6 +91,7 @@ source_set("unit_tests") {
"config_unittest.cc", "config_unittest.cc",
"proto_conversions_unittest.cc", "proto_conversions_unittest.cc",
"tutorial_group_unittest.cc", "tutorial_group_unittest.cc",
"tutorial_manager_impl_unittest.cc",
"tutorial_store_unittest.cc", "tutorial_store_unittest.cc",
] ]
...@@ -100,6 +104,7 @@ source_set("unit_tests") { ...@@ -100,6 +104,7 @@ source_set("unit_tests") {
"//chrome/browser/video_tutorials/test:test_lib", "//chrome/browser/video_tutorials/test:test_lib",
"//components/leveldb_proto", "//components/leveldb_proto",
"//components/leveldb_proto:test_support", "//components/leveldb_proto:test_support",
"//components/prefs:test_support",
"//testing/gmock", "//testing/gmock",
"//testing/gtest", "//testing/gtest",
] ]
......
...@@ -16,13 +16,13 @@ constexpr char kDefaultBaseURL[] = "https://chromeupboarding-pa.googleapis.com"; ...@@ -16,13 +16,13 @@ constexpr char kDefaultBaseURL[] = "https://chromeupboarding-pa.googleapis.com";
// Default URL string for GetTutorials RPC. // Default URL string for GetTutorials RPC.
constexpr char kDefaultGetTutorialsPath[] = "/v1/videotutorials"; constexpr char kDefaultGetTutorialsPath[] = "/v1/videotutorials";
// Hindi is the default language. // Hindi is the default locale.
constexpr char kDefaultPreferredLanguage[] = "hi"; constexpr char kDefaultPreferredLocale[] = "hi";
// Finch parameter key for base server URL to retrieve the tutorials. // Finch parameter key for base server URL to retrieve the tutorials.
constexpr char kBaseURLKey[] = "base_url"; constexpr char kBaseURLKey[] = "base_url";
constexpr char kPrefferedLanguageKey[] = "default_lang"; constexpr char kPreferredLocaleConfigKey[] = "default_locale";
namespace { namespace {
const GURL BuildGetTutorialsEndpoint(const GURL& base_url, const char* path) { const GURL BuildGetTutorialsEndpoint(const GURL& base_url, const char* path) {
...@@ -43,11 +43,12 @@ GURL Config::GetTutorialsServerURL() { ...@@ -43,11 +43,12 @@ GURL Config::GetTutorialsServerURL() {
} }
// static // static
std::string Config::GetDefaultPreferredLanguage() { std::string Config::GetDefaultPreferredLocale() {
std::string default_lang_from_finch = base::GetFieldTrialParamValueByFeature( std::string default_locale_from_finch =
features::kVideoTutorials, kPrefferedLanguageKey); base::GetFieldTrialParamValueByFeature(features::kVideoTutorials,
return default_lang_from_finch.empty() ? kDefaultPreferredLanguage kPreferredLocaleConfigKey);
: default_lang_from_finch; return default_locale_from_finch.empty() ? kDefaultPreferredLocale
: default_locale_from_finch;
} }
} // namespace video_tutorials } // namespace video_tutorials
...@@ -21,19 +21,19 @@ extern const char kDefaultBaseURL[]; ...@@ -21,19 +21,19 @@ extern const char kDefaultBaseURL[];
// Finch parameter key for base server URL to retrieve the tutorials. // Finch parameter key for base server URL to retrieve the tutorials.
extern const char kBaseURLKey[]; extern const char kBaseURLKey[];
// Finch parameter key for the default preferred language. // Finch parameter key for the default preferred locale.
extern const char kPrefferedLanguageKey[]; extern const char kPreferredLocaleConfigKey[];
// Default preferred language setting before users pick. // Default preferred locale setting before users pick.
extern const char kDefaultPreferredLanguage[]; extern const char kDefaultPreferredLocale[];
class Config { class Config {
public: public:
// Get video tutorials metadata server URL. // Get video tutorials metadata server URL.
static GURL GetTutorialsServerURL(); static GURL GetTutorialsServerURL();
// Get the default language before users choice. // Get the default locale before users choice.
static std::string GetDefaultPreferredLanguage(); static std::string GetDefaultPreferredLocale();
}; };
} // namespace video_tutorials } // namespace video_tutorials
......
...@@ -16,13 +16,13 @@ namespace video_tutorials { ...@@ -16,13 +16,13 @@ namespace video_tutorials {
TEST(VideoTutorialsConfigTest, FinchConfigEnabled) { TEST(VideoTutorialsConfigTest, FinchConfigEnabled) {
base::test::ScopedFeatureList feature_list; base::test::ScopedFeatureList feature_list;
std::map<std::string, std::string> params = { std::map<std::string, std::string> params = {
{kBaseURLKey, "https://test.com"}, {kPrefferedLanguageKey, "en"}}; {kBaseURLKey, "https://test.com"}, {kPreferredLocaleConfigKey, "en"}};
feature_list.InitAndEnableFeatureWithParameters(features::kVideoTutorials, feature_list.InitAndEnableFeatureWithParameters(features::kVideoTutorials,
params); params);
EXPECT_EQ(Config::GetTutorialsServerURL().spec(), EXPECT_EQ(Config::GetTutorialsServerURL().spec(),
"https://test.com/v1/videotutorials"); "https://test.com/v1/videotutorials");
EXPECT_EQ(Config::GetDefaultPreferredLanguage(), "en"); EXPECT_EQ(Config::GetDefaultPreferredLocale(), "en");
} }
TEST(VideoTutorialsConfigTest, ConfigDefaultParams) { TEST(VideoTutorialsConfigTest, ConfigDefaultParams) {
...@@ -30,7 +30,7 @@ TEST(VideoTutorialsConfigTest, ConfigDefaultParams) { ...@@ -30,7 +30,7 @@ TEST(VideoTutorialsConfigTest, ConfigDefaultParams) {
feature_list.InitAndEnableFeature(features::kVideoTutorials); feature_list.InitAndEnableFeature(features::kVideoTutorials);
EXPECT_EQ(Config::GetTutorialsServerURL().spec(), EXPECT_EQ(Config::GetTutorialsServerURL().spec(),
"https://chromeupboarding-pa.googleapis.com/v1/videotutorials"); "https://chromeupboarding-pa.googleapis.com/v1/videotutorials");
EXPECT_EQ(Config::GetDefaultPreferredLanguage(), "hi"); EXPECT_EQ(Config::GetDefaultPreferredLocale(), "hi");
} }
} // namespace video_tutorials } // namespace video_tutorials
...@@ -25,7 +25,6 @@ class Store { ...@@ -25,7 +25,6 @@ class Store {
using LoadKeysCallback = base::OnceCallback<void(bool, Keys)>; using LoadKeysCallback = base::OnceCallback<void(bool, Keys)>;
using LoadEntriesCallback = base::OnceCallback<void(bool, Entries)>; using LoadEntriesCallback = base::OnceCallback<void(bool, Entries)>;
using UpdateCallback = base::OnceCallback<void(bool)>; using UpdateCallback = base::OnceCallback<void(bool)>;
using DeleteCallback = base::OnceCallback<void(bool)>;
// Initialize the db and load keys into memory. // Initialize the db and load keys into memory.
virtual void InitAndLoadKeys(LoadKeysCallback callback) = 0; virtual void InitAndLoadKeys(LoadKeysCallback callback) = 0;
...@@ -34,14 +33,12 @@ class Store { ...@@ -34,14 +33,12 @@ class Store {
virtual void LoadEntries(const std::vector<std::string>& keys, virtual void LoadEntries(const std::vector<std::string>& keys,
LoadEntriesCallback callback) = 0; LoadEntriesCallback callback) = 0;
// Add a new entry or update an existing entry. // Batch update call with ability to update and remove multiple
virtual void Update(const std::string& key, // entries.
const T& entry, virtual void UpdateAll(
UpdateCallback callback) = 0; const std::vector<std::pair<std::string, T>>& key_entry_pairs,
const std::vector<std::string>& keys_to_delete,
// Delete entries from database. UpdateCallback callback) = 0;
virtual void Delete(const std::vector<std::string>& keys,
DeleteCallback callback) = 0;
Store() = default; Store() = default;
virtual ~Store() = default; virtual ~Store() = default;
......
// 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.
#ifndef CHROME_BROWSER_VIDEO_TUTORIALS_INTERNAL_TUTORIAL_MANAGER_H_
#define CHROME_BROWSER_VIDEO_TUTORIALS_INTERNAL_TUTORIAL_MANAGER_H_
#include "base/callback.h"
#include "chrome/browser/video_tutorials/internal/tutorial_group.h"
namespace video_tutorials {
// Responsible for serving video tutorials and coordinating access with the
// network fetcher and the storage layer.
class TutorialManager {
public:
using SuccessCallback = base::OnceCallback<void(bool)>;
using GetTutorialsCallback = base::OnceCallback<void(std::vector<Tutorial>)>;
// Initialization method that reads the database and loads video tutorials
// into in-memory.
virtual void Init(SuccessCallback callback) = 0;
// Loads video tutorials. Must be called again if the locale was changed by
// the user.
virtual void GetTutorials(GetTutorialsCallback callback) = 0;
// Returns a list of locales for which video tutorials are available.
virtual const std::vector<std::string>& GetSupportedLocales() = 0;
// Returns the preferred locale for the video tutorials.
virtual std::string GetPreferredLocale() = 0;
// Sets the user preferred locale for watching the video tutorials. This
// doesn't update the cached tutorials. GetTutorials must be called for the
// new data to be reflected.
virtual void SetPreferredLocale(const std::string& locale) = 0;
// Saves a fresh set of video tutorials into database. Called after a network
// fetch.
virtual void SaveGroups(std::vector<std::unique_ptr<TutorialGroup>> groups,
SuccessCallback callback) = 0;
virtual ~TutorialManager() = default;
TutorialManager(TutorialManager& other) = delete;
TutorialManager& operator=(TutorialManager& other) = delete;
protected:
TutorialManager() = default;
};
} // namespace video_tutorials
#endif // CHROME_BROWSER_VIDEO_TUTORIALS_INTERNAL_TUTORIAL_MANAGER_H_
// 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/video_tutorials/internal/tutorial_manager_impl.h"
#include <set>
#include "base/bind.h"
#include "base/stl_util.h"
#include "chrome/browser/video_tutorials/internal/config.h"
#include "chrome/browser/video_tutorials/prefs.h"
#include "components/prefs/pref_service.h"
namespace video_tutorials {
TutorialManagerImpl::TutorialManagerImpl(std::unique_ptr<TutorialStore> store,
PrefService* prefs)
: store_(std::move(store)), prefs_(prefs) {}
TutorialManagerImpl::~TutorialManagerImpl() = default;
void TutorialManagerImpl::Init(SuccessCallback callback) {
store_->InitAndLoadKeys(base::BindOnce(&TutorialManagerImpl::OnInitCompleted,
weak_ptr_factory_.GetWeakPtr(),
std::move(callback)));
}
void TutorialManagerImpl::GetTutorials(GetTutorialsCallback callback) {
// Find the data from cache.
std::string locale = GetPreferredLocale();
if (tutorial_group_ && tutorial_group_->locale == locale) {
std::move(callback).Run(tutorial_group_->tutorials);
return;
}
// The data doesn't exist in the cache. Probably the locale has changed. Load
// it from the DB.
store_->LoadEntries(
{locale},
base::BindOnce(&TutorialManagerImpl::OnTutorialsLoaded,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
const std::vector<std::string>& TutorialManagerImpl::GetSupportedLocales() {
return supported_locales_;
}
std::string TutorialManagerImpl::GetPreferredLocale() {
return prefs_->HasPrefPath(kPreferredLocaleKey)
? prefs_->GetString(kPreferredLocaleKey)
: Config::GetDefaultPreferredLocale();
}
void TutorialManagerImpl::SetPreferredLocale(const std::string& locale) {
prefs_->SetString(kPreferredLocaleKey, locale);
}
void TutorialManagerImpl::OnInitCompleted(
SuccessCallback callback,
bool success,
std::unique_ptr<std::vector<std::string>> supported_locales) {
if (!success) {
std::move(callback).Run(success);
return;
}
if (supported_locales)
supported_locales_ = *supported_locales.get();
std::move(callback).Run(true);
}
void TutorialManagerImpl::OnTutorialsLoaded(
GetTutorialsCallback callback,
bool success,
std::vector<std::unique_ptr<TutorialGroup>> loaded_group) {
if (!success || loaded_group.empty()) {
std::move(callback).Run(std::vector<Tutorial>());
return;
}
// We are loading tutorials only for the preferred locale.
DCHECK(loaded_group.size() == 1u);
tutorial_group_ = std::move(loaded_group.front());
std::move(callback).Run(tutorial_group_->tutorials);
}
void TutorialManagerImpl::SaveGroups(
std::vector<std::unique_ptr<TutorialGroup>> groups,
SuccessCallback callback) {
std::vector<std::string> new_locales;
std::vector<std::pair<std::string, TutorialGroup>> key_entry_pairs;
for (auto& group : groups) {
new_locales.emplace_back(group->locale);
key_entry_pairs.emplace_back(std::make_pair(group->locale, *group));
}
// Remove the locales that don't exist in the new data.
std::vector<std::string> keys_to_delete;
for (auto& old_locale : supported_locales_) {
if (std::find(new_locales.begin(), new_locales.end(), old_locale) ==
new_locales.end()) {
keys_to_delete.emplace_back(old_locale);
}
}
store_->UpdateAll(key_entry_pairs, keys_to_delete, std::move(callback));
}
} // namespace video_tutorials
// 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.
#ifndef CHROME_BROWSER_VIDEO_TUTORIALS_INTERNAL_TUTORIAL_MANAGER_IMPL_H_
#define CHROME_BROWSER_VIDEO_TUTORIALS_INTERNAL_TUTORIAL_MANAGER_IMPL_H_
#include "chrome/browser/video_tutorials/internal/tutorial_manager.h"
#include <memory>
#include <string>
#include <vector>
#include "base/memory/weak_ptr.h"
#include "chrome/browser/video_tutorials/internal/store.h"
class PrefService;
namespace video_tutorials {
class TutorialManagerImpl : public TutorialManager {
public:
using TutorialStore = Store<TutorialGroup>;
TutorialManagerImpl(std::unique_ptr<TutorialStore> store, PrefService* prefs);
~TutorialManagerImpl() override;
private:
// TutorialManager implementation.
void Init(SuccessCallback callback) override;
void GetTutorials(GetTutorialsCallback callback) override;
const std::vector<std::string>& GetSupportedLocales() override;
std::string GetPreferredLocale() override;
void SetPreferredLocale(const std::string& locale) override;
void SaveGroups(std::vector<std::unique_ptr<TutorialGroup>> groups,
SuccessCallback callback) override;
void OnInitCompleted(
SuccessCallback callback,
bool success,
std::unique_ptr<std::vector<std::string>> supported_locales);
void OnTutorialsLoaded(
GetTutorialsCallback callback,
bool success,
std::vector<std::unique_ptr<TutorialGroup>> loaded_group);
std::unique_ptr<TutorialStore> store_;
PrefService* prefs_;
// List of locales for which we have tutorials.
std::vector<std::string> supported_locales_;
// We only keep the tutorials for the preferred locale.
std::unique_ptr<TutorialGroup> tutorial_group_;
base::WeakPtrFactory<TutorialManagerImpl> weak_ptr_factory_{this};
};
} // namespace video_tutorials
#endif // CHROME_BROWSER_VIDEO_TUTORIALS_INTERNAL_TUTORIAL_MANAGER_IMPL_H_
// 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/video_tutorials/internal/tutorial_manager_impl.h"
#include <map>
#include <utility>
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::Invoke;
using ::testing::StrictMock;
namespace video_tutorials {
namespace {
std::vector<TutorialGroup> CreateSampleGroups(
const std::vector<std::string>& locales) {
std::vector<TutorialGroup> groups;
for (const auto& locale : locales) {
TutorialGroup group;
group.locale = locale;
group.tutorials.emplace_back(Tutorial());
group.tutorials.emplace_back(Tutorial());
groups.emplace_back(group);
}
return groups;
}
std::vector<std::unique_ptr<TutorialGroup>> CreateSampleFetchData(
const std::vector<std::string>& locales) {
std::vector<std::unique_ptr<TutorialGroup>> groups;
for (const auto& group : CreateSampleGroups(locales)) {
groups.emplace_back(std::make_unique<TutorialGroup>(group));
}
return groups;
}
class TestStore : public Store<TutorialGroup> {
public:
TestStore() = default;
~TestStore() override = default;
void InitAndLoadKeys(LoadKeysCallback callback) override {
std::vector<std::string> keys;
for (const TutorialGroup& group : groups_)
keys.emplace_back(group.locale);
std::move(callback).Run(true,
std::make_unique<std::vector<std::string>>(keys));
}
void LoadEntries(const std::vector<std::string>& keys,
LoadEntriesCallback callback) override {
std::vector<std::unique_ptr<TutorialGroup>> entries;
for (const TutorialGroup& group : groups_) {
if (group.locale != locale_)
continue;
entries.emplace_back(std::make_unique<TutorialGroup>(group));
}
std::move(callback).Run(true, std::move(entries));
}
void Initialize(const std::string& locale,
const std::vector<TutorialGroup>& groups) {
locale_ = locale;
groups_ = groups;
}
void UpdateAll(
const std::vector<std::pair<std::string, TutorialGroup>>& key_entry_pairs,
const std::vector<std::string>& keys_to_delete,
UpdateCallback callback) override {
groups_.clear();
for (auto pair : key_entry_pairs)
groups_.emplace_back(pair.second);
std::move(callback).Run(true);
}
private:
std::string locale_;
std::vector<TutorialGroup> groups_;
};
class TutorialManagerTest : public testing::Test {
public:
TutorialManagerTest() = default;
~TutorialManagerTest() override = default;
TutorialManagerTest(const TutorialManagerTest& other) = delete;
TutorialManagerTest& operator=(const TutorialManagerTest& other) = delete;
void SetUp() override {
auto tutorial_store = std::make_unique<StrictMock<TestStore>>();
tutorial_store_ = tutorial_store.get();
manager_ = std::make_unique<TutorialManagerImpl>(std::move(tutorial_store),
&prefs_);
}
// Run GetTutorials call from manager_, compare the |expected| to the actual
// returned tutorials.
void GetTutorials(std::vector<Tutorial> expected) {
base::RunLoop loop;
manager()->GetTutorials(base::BindOnce(
&TutorialManagerTest::OnGetTutorials, base::Unretained(this),
loop.QuitClosure(), std::move(expected)));
loop.Run();
}
void OnGetTutorials(base::RepeatingClosure closure,
std::vector<Tutorial> expected,
std::vector<Tutorial> tutorials) {
EXPECT_TRUE(expected.size() == tutorials.size());
std::move(closure).Run();
}
void Init() {
base::RunLoop loop;
manager()->Init(base::BindOnce(&TutorialManagerTest::OnComplete,
base::Unretained(this), loop.QuitClosure()));
loop.Run();
}
void OnComplete(base::RepeatingClosure closure, bool success) {
std::move(closure).Run();
}
void SaveGroups(std::vector<std::unique_ptr<TutorialGroup>> groups) {
base::RunLoop loop;
manager()->SaveGroups(
std::move(groups),
base::BindOnce(&TutorialManagerTest::OnComplete, base::Unretained(this),
loop.QuitClosure()));
loop.Run();
}
protected:
TutorialManager* manager() { return manager_.get(); }
TestStore* tutorial_store() { return tutorial_store_; }
private:
base::test::TaskEnvironment task_environment_;
TestingPrefServiceSimple prefs_;
std::unique_ptr<TutorialManager> manager_;
TestStore* tutorial_store_;
};
TEST_F(TutorialManagerTest, InitAndGetTutorials) {
auto groups = CreateSampleGroups({"hi", "kn"});
tutorial_store()->Initialize("hi", groups);
Init();
auto locales = manager()->GetSupportedLocales();
EXPECT_EQ(locales.size(), 2u);
GetTutorials(groups[0].tutorials);
}
TEST_F(TutorialManagerTest, SaveNewData) {
auto groups = CreateSampleGroups({"hi", "kn"});
tutorial_store()->Initialize("hi", groups);
Init();
auto locales = manager()->GetSupportedLocales();
EXPECT_EQ(locales.size(), 2u);
GetTutorials(groups[0].tutorials);
auto new_groups = CreateSampleFetchData({"hi", "tl", "ar"});
SaveGroups(std::move(new_groups));
}
} // namespace
} // namespace video_tutorials
...@@ -46,22 +46,20 @@ void TutorialStore::LoadEntries(const std::vector<std::string>& keys, ...@@ -46,22 +46,20 @@ void TutorialStore::LoadEntries(const std::vector<std::string>& keys,
weak_ptr_factory_.GetWeakPtr(), std::move(callback))); weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} }
void TutorialStore::Update(const std::string& key, void TutorialStore::UpdateAll(
const TutorialGroup& group, const std::vector<std::pair<std::string, TutorialGroup>>& key_entry_pairs,
UpdateCallback callback) { const std::vector<std::string>& keys_to_delete,
UpdateCallback callback) {
auto entries_to_save = std::make_unique<KeyEntryVector>(); auto entries_to_save = std::make_unique<KeyEntryVector>();
auto entry_to_save = group; for (auto& pair : key_entry_pairs)
entries_to_save->emplace_back(key, std::move(entry_to_save)); entries_to_save->emplace_back(pair.first, pair.second);
db_->UpdateEntries(std::move(entries_to_save),
std::make_unique<KeyVector>() /*keys_to_remove*/,
std::move(callback));
}
void TutorialStore::Delete(const std::vector<std::string>& keys, auto keys_to_remove = std::make_unique<std::vector<std::string>>();
DeleteCallback callback) { for (auto key : keys_to_delete)
auto keys_to_delete = std::make_unique<KeyVector>(keys); keys_to_remove->emplace_back(key);
db_->UpdateEntries(std::make_unique<KeyEntryVector>() /*entries_to_save*/,
std::move(keys_to_delete), std::move(callback)); db_->UpdateEntries(std::move(entries_to_save), std::move(keys_to_remove),
std::move(callback));
} }
void TutorialStore::OnDbInitialized(LoadKeysCallback callback, void TutorialStore::OnDbInitialized(LoadKeysCallback callback,
......
...@@ -50,11 +50,10 @@ class TutorialStore : public Store<TutorialGroup> { ...@@ -50,11 +50,10 @@ class TutorialStore : public Store<TutorialGroup> {
void InitAndLoadKeys(LoadKeysCallback callback) override; void InitAndLoadKeys(LoadKeysCallback callback) override;
void LoadEntries(const std::vector<std::string>& keys, void LoadEntries(const std::vector<std::string>& keys,
LoadEntriesCallback callback) override; LoadEntriesCallback callback) override;
void Update(const std::string& key, void UpdateAll(
const TutorialGroup& group, const std::vector<std::pair<std::string, TutorialGroup>>& key_entry_pairs,
UpdateCallback callback) override; const std::vector<std::string>& keys_to_delete,
void Delete(const std::vector<std::string>& keys, UpdateCallback callback) override;
DeleteCallback callback) override;
// Called when db is initialized. // Called when db is initialized.
void OnDbInitialized(LoadKeysCallback callback, void OnDbInitialized(LoadKeysCallback callback,
......
...@@ -182,8 +182,12 @@ TEST_F(TutorialStoreTest, AddAndUpdateDataSuccess) { ...@@ -182,8 +182,12 @@ TEST_F(TutorialStoreTest, AddAndUpdateDataSuccess) {
// Add a group successfully. // Add a group successfully.
TutorialGroup test_group; TutorialGroup test_group;
test::BuildTestGroup(&test_group); test::BuildTestGroup(&test_group);
store()->Update(test_group.locale, test_group, std::vector<std::pair<std::string, TutorialGroup>> entries_to_save;
base::BindOnce([](bool success) { EXPECT_TRUE(success); })); entries_to_save.emplace_back(std::make_pair(test_group.locale, test_group));
std::vector<std::string> keys_to_delete;
store()->UpdateAll(
entries_to_save, keys_to_delete,
base::BindOnce([](bool success) { EXPECT_TRUE(success); }));
db()->UpdateCallback(true); db()->UpdateCallback(true);
auto expected = std::make_unique<KeysAndEntries>(); auto expected = std::make_unique<KeysAndEntries>();
...@@ -204,8 +208,11 @@ TEST_F(TutorialStoreTest, Delete) { ...@@ -204,8 +208,11 @@ TEST_F(TutorialStoreTest, Delete) {
EXPECT_EQ(loaded_keys().size(), 1u); EXPECT_EQ(loaded_keys().size(), 1u);
EXPECT_EQ(loaded_keys().front(), locale); EXPECT_EQ(loaded_keys().front(), locale);
std::vector<std::string> keys{locale}; std::vector<std::string> keys{locale};
store()->Delete(std::move(keys), std::vector<std::pair<std::string, TutorialGroup>> entries_to_save;
base::BindOnce([](bool success) { EXPECT_TRUE(success); }));
store()->UpdateAll(
entries_to_save, std::move(keys),
base::BindOnce([](bool success) { EXPECT_TRUE(success); }));
db()->UpdateCallback(true); db()->UpdateCallback(true);
// No entry is expected in db. // No entry is expected in db.
auto expected = std::make_unique<KeysAndEntries>(); auto expected = std::make_unique<KeysAndEntries>();
......
...@@ -9,12 +9,12 @@ ...@@ -9,12 +9,12 @@
namespace video_tutorials { namespace video_tutorials {
constexpr char kPreferredLanguageKey[] = "video_tutorials.perferred_language"; constexpr char kPreferredLocaleKey[] = "video_tutorials.perferred_locale";
constexpr char kLastUpdatedTimeKey[] = "video_tutorials.last_updated_time"; constexpr char kLastUpdatedTimeKey[] = "video_tutorials.last_updated_time";
void RegisterPrefs(PrefRegistrySimple* registry) { void RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterStringPref(kPreferredLanguageKey, std::string()); registry->RegisterStringPref(kPreferredLocaleKey, std::string());
registry->RegisterTimePref(kLastUpdatedTimeKey, base::Time()); registry->RegisterTimePref(kLastUpdatedTimeKey, base::Time());
} }
......
...@@ -13,8 +13,8 @@ class PrefRegistrySimple; ...@@ -13,8 +13,8 @@ class PrefRegistrySimple;
namespace video_tutorials { namespace video_tutorials {
// Key for the language that user picked in video tutorials service. // Key for the locale that user picked in video tutorials service.
extern const char kPreferredLanguageKey[]; extern const char kPreferredLocaleKey[];
// Key to record a timestamp when the last update of video tutorials metadata // Key to record a timestamp when the last update of video tutorials metadata
// happened. // happened.
......
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