diff options
author | fgorski <fgorski@chromium.org> | 2015-07-14 21:47:14 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-15 04:47:41 +0000 |
commit | a979ca5250438d924bc98060a8ba72983bc60622 (patch) | |
tree | 32826935db0da295b1ed81b065142fa4a9d10adc /components | |
parent | 568832fdf8b4a8d4076f75268602b12c82bc9f38 (diff) | |
download | chromium_src-a979ca5250438d924bc98060a8ba72983bc60622.zip chromium_src-a979ca5250438d924bc98060a8ba72983bc60622.tar.gz chromium_src-a979ca5250438d924bc98060a8ba72983bc60622.tar.bz2 |
[Offline pages] Implementation of loading of pages by the model
Added implementation of OfflinePageModel.LoadAllPages with tests.
BUG=491352
R=jianli@chromium.org
Review URL: https://codereview.chromium.org/1238723003
Cr-Commit-Position: refs/heads/master@{#338815}
Diffstat (limited to 'components')
4 files changed, 105 insertions, 9 deletions
diff --git a/components/offline_pages/offline_page_metadata_store.h b/components/offline_pages/offline_page_metadata_store.h index 6d5cc07..4e096a8 100644 --- a/components/offline_pages/offline_page_metadata_store.h +++ b/components/offline_pages/offline_page_metadata_store.h @@ -18,7 +18,7 @@ struct OfflinePageItem; // OfflinePageMetadataStore keeps metadata for the offline pages. // Ability to create multiple instances of the store as well as behavior of // asynchronous operations when the object is being destroyed, before such -// operation finishes will depend on implementation. It should be possbile to +// operation finishes will depend on implementation. It should be possible to // issue multiple asynchronous operations in parallel. class OfflinePageMetadataStore { public: diff --git a/components/offline_pages/offline_page_model.cc b/components/offline_pages/offline_page_model.cc index c0d5026..943297a 100644 --- a/components/offline_pages/offline_page_model.cc +++ b/components/offline_pages/offline_page_model.cc @@ -79,7 +79,8 @@ void OfflinePageModel::DeletePage(const GURL& url, } void OfflinePageModel::LoadAllPages(const LoadAllPagesCallback& callback) { - NOTIMPLEMENTED(); + store_->Load(base::Bind(&OfflinePageModel::OnLoadDone, + weak_ptr_factory_.GetWeakPtr(), callback)); } OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { @@ -128,6 +129,17 @@ void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, DeletePendingArchiver(archiver); } +void OfflinePageModel::OnLoadDone( + const LoadAllPagesCallback& callback, + bool success, + const std::vector<OfflinePageItem>& offline_pages) { + // TODO(fgorski): Cache the values here, if we are comfortable with that + // model. This will require extra handling of parallel loads. + LoadResult result = + success ? LoadResult::SUCCESS : LoadResult::STORE_FAILURE; + callback.Run(result, offline_pages); +} + void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, SavePageResult result) { task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); diff --git a/components/offline_pages/offline_page_model.h b/components/offline_pages/offline_page_model.h index 66dd447..fbfab50 100644 --- a/components/offline_pages/offline_page_model.h +++ b/components/offline_pages/offline_page_model.h @@ -5,6 +5,8 @@ #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ +#include <vector> + #include "base/callback.h" #include "base/macros.h" #include "base/memory/ref_counted.h" @@ -118,6 +120,9 @@ class OfflinePageModel : public KeyedService { void OnAddOfflinePageDone(OfflinePageArchiver* archiver, const SavePageCallback& callback, bool success); + void OnLoadDone(const LoadAllPagesCallback& callback, + bool success, + const std::vector<OfflinePageItem>& offline_pages); void InformSavePageDone(const SavePageCallback& callback, SavePageResult result); diff --git a/components/offline_pages/offline_page_model_unittest.cc b/components/offline_pages/offline_page_model_unittest.cc index cd933bd..1e1621b 100644 --- a/components/offline_pages/offline_page_model_unittest.cc +++ b/components/offline_pages/offline_page_model_unittest.cc @@ -16,6 +16,7 @@ #include "url/gurl.h" using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; +using LoadResult = offline_pages::OfflinePageModel::LoadResult; namespace offline_pages { @@ -31,6 +32,7 @@ class OfflinePageTestStore : public OfflinePageMetadataStore { enum class TestScenario { SUCCESSFUL, WRITE_FAILED, + FAILED, }; explicit OfflinePageTestStore( @@ -52,24 +54,37 @@ class OfflinePageTestStore : public OfflinePageMetadataStore { scoped_refptr<base::SingleThreadTaskRunner> task_runner_; TestScenario scenario_; + std::vector<OfflinePageItem> offline_pages_; + DISALLOW_COPY_AND_ASSIGN(OfflinePageTestStore); }; OfflinePageTestStore::OfflinePageTestStore( const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) - : task_runner_(task_runner), scenario_(TestScenario::SUCCESSFUL) { + : task_runner_(task_runner), + scenario_(TestScenario::SUCCESSFUL) { } OfflinePageTestStore::~OfflinePageTestStore() { } void OfflinePageTestStore::Load(const LoadCallback& callback) { + if (scenario_ != TestScenario::FAILED) { + task_runner_->PostTask( + FROM_HERE, base::Bind(callback, true, offline_pages_)); + } else { + task_runner_->PostTask( + FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>())); + } } void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page, const UpdateCallback& callback) { last_saved_page_ = offline_page; bool result = scenario_ != TestScenario::WRITE_FAILED; + if (result) { + offline_pages_.push_back(offline_page); + } task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); } @@ -152,6 +167,8 @@ class OfflinePageModelTest // OfflinePageModel callbacks. void OnSavePageDone(SavePageResult result); + void OnLoadAllPagesDone(LoadResult result, + const std::vector<OfflinePageItem>& offline_pages); scoped_ptr<OfflinePageMetadataStore> BuildStore(); scoped_ptr<OfflinePageModel> BuildModel(); @@ -159,10 +176,6 @@ class OfflinePageModelTest // Utility methods. void PumpLoop(); - OfflinePageModel::SavePageResult last_save_result() const { - return last_save_result_; - } - scoped_refptr<base::SingleThreadTaskRunner> task_runner() { return message_loop_.task_runner(); } @@ -171,15 +184,31 @@ class OfflinePageModelTest OfflinePageTestStore* GetStore(); + SavePageResult last_save_result() const { + return last_save_result_; + } + + LoadResult last_load_result() const { + return last_load_result_; + } + + const std::vector<OfflinePageItem>& last_loaded_pages() const { + return last_loaded_pages_; + } + private: base::MessageLoop message_loop_; scoped_ptr<base::RunLoop> run_loop_; scoped_ptr<OfflinePageModel> model_; - OfflinePageModel::SavePageResult last_save_result_; + SavePageResult last_save_result_; + LoadResult last_load_result_; + std::vector<OfflinePageItem> last_loaded_pages_; }; -OfflinePageModelTest::OfflinePageModelTest() { +OfflinePageModelTest::OfflinePageModelTest() + : last_save_result_(SavePageResult::CANCELLED), + last_load_result_(LoadResult::CANCELLED) { } OfflinePageModelTest::~OfflinePageModelTest() { @@ -195,6 +224,14 @@ void OfflinePageModelTest::OnSavePageDone( last_save_result_ = result; } +void OfflinePageModelTest::OnLoadAllPagesDone( + LoadResult result, + const std::vector<OfflinePageItem>& offline_pages) { + run_loop_->Quit(); + last_load_result_ = result; + last_loaded_pages_ = offline_pages; +} + scoped_ptr<OfflinePageMetadataStore> OfflinePageModelTest::BuildStore() { return scoped_ptr<OfflinePageMetadataStore>( new OfflinePageTestStore(message_loop_.task_runner())); @@ -231,6 +268,16 @@ TEST_F(OfflinePageModelTest, SavePageSuccessful) { EXPECT_EQ(base::FilePath(kTestFilePath), store->last_saved_page().file_path); EXPECT_EQ(kTestFileSize, store->last_saved_page().file_size); EXPECT_EQ(SavePageResult::SUCCESS, last_save_result()); + + model()->LoadAllPages(base::Bind(&OfflinePageModelTest::OnLoadAllPagesDone, + AsWeakPtr())); + PumpLoop(); + EXPECT_EQ(LoadResult::SUCCESS, last_load_result()); + EXPECT_EQ(1UL, last_loaded_pages().size()); + EXPECT_EQ(page_url, last_loaded_pages()[0].url); + EXPECT_EQ(kTestPageTitle, last_loaded_pages()[0].title); + EXPECT_EQ(base::FilePath(kTestFilePath), last_loaded_pages()[0].file_path); + EXPECT_EQ(kTestFileSize, last_loaded_pages()[0].file_size); } TEST_F(OfflinePageModelTest, SavePageOfflineArchiverCancelled) { @@ -355,6 +402,38 @@ TEST_F(OfflinePageModelTest, SavePageOfflineArchiverTwoPages) { EXPECT_EQ(base::FilePath(kTestFilePath), store->last_saved_page().file_path); EXPECT_EQ(kTestFileSize, store->last_saved_page().file_size); EXPECT_EQ(SavePageResult::SUCCESS, last_save_result()); + + model()->LoadAllPages(base::Bind(&OfflinePageModelTest::OnLoadAllPagesDone, + AsWeakPtr())); + PumpLoop(); + EXPECT_EQ(LoadResult::SUCCESS, last_load_result()); + EXPECT_EQ(2UL, last_loaded_pages().size()); + EXPECT_EQ(page_url2, last_loaded_pages()[0].url); + EXPECT_EQ(title2, last_loaded_pages()[0].title); + EXPECT_EQ(base::FilePath(kTestFilePath), last_loaded_pages()[0].file_path); + EXPECT_EQ(kTestFileSize, last_loaded_pages()[0].file_size); + EXPECT_EQ(page_url, last_loaded_pages()[1].url); + EXPECT_EQ(kTestPageTitle, last_loaded_pages()[1].title); + EXPECT_EQ(base::FilePath(kTestFilePath), last_loaded_pages()[1].file_path); + EXPECT_EQ(kTestFileSize, last_loaded_pages()[1].file_size); +} + +TEST_F(OfflinePageModelTest, LoadAllPagesStoreEmpty) { + model()->LoadAllPages(base::Bind(&OfflinePageModelTest::OnLoadAllPagesDone, + AsWeakPtr())); + PumpLoop(); + EXPECT_EQ(LoadResult::SUCCESS, last_load_result()); + EXPECT_EQ(0UL, last_loaded_pages().size()); +} + +TEST_F(OfflinePageModelTest, LoadAllPagesStoreFailure) { + GetStore()->set_test_scenario( + OfflinePageTestStore::TestScenario::FAILED); + model()->LoadAllPages(base::Bind(&OfflinePageModelTest::OnLoadAllPagesDone, + AsWeakPtr())); + PumpLoop(); + EXPECT_EQ(LoadResult::STORE_FAILURE, last_load_result()); + EXPECT_EQ(0UL, last_loaded_pages().size()); } } // namespace offline_pages |