| Index: chrome/browser/android/offline_pages/offline_page_utils_unittest.cc
|
| diff --git a/chrome/browser/android/offline_pages/offline_page_utils_unittest.cc b/chrome/browser/android/offline_pages/offline_page_utils_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..aa602c6dd11c9b698c1798f23edc8ea74a274a47
|
| --- /dev/null
|
| +++ b/chrome/browser/android/offline_pages/offline_page_utils_unittest.cc
|
| @@ -0,0 +1,133 @@
|
| +// Copyright 2015 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/android/offline_pages/offline_page_utils.h"
|
| +
|
| +#include "base/files/file_path.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/files/scoped_temp_dir.h"
|
| +#include "base/test/test_simple_task_runner.h"
|
| +#include "base/thread_task_runner_handle.h"
|
| +#include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
|
| +#include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h"
|
| +#include "chrome/test/base/testing_profile.h"
|
| +#include "components/offline_pages/offline_page_model.h"
|
| +#include "components/offline_pages/offline_page_test_store.h"
|
| +#include "net/base/filename_util.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "url/gurl.h"
|
| +
|
| +namespace offline_pages {
|
| +namespace android {
|
| +
|
| +namespace {
|
| +const char kTestPage1Url[] = "http://test.org/page1";
|
| +const char kTestPage2Url[] = "http://test.org/page2";
|
| +const char kTestPage3Url[] = "http://test.org/page3";
|
| +int64 kTestPage1BookmarkId = 1234;
|
| +int64 kTestPage2BookmarkId = 5678;
|
| +//const base::FilePath::CharType kTestPage1FilePath[] =
|
| +// FILE_PATH_LITERAL("test_org_page1.mhtml");
|
| +//const base::FilePath::CharType kTestPage2FilePath[] =
|
| +// FILE_PATH_LITERAL("test_org_page2.mhtml");
|
| +//const base::FilePath::CharType kTestPage3FilePath[] =
|
| +// FILE_PATH_LITERAL("/offline_pages/test_org_page3.mhtml");
|
| +int64 kTestPage1FileSize = 654321;
|
| +int64 kTestPage2FileSize = 765432;
|
| +
|
| +} // namespace
|
| +
|
| +class OfflinePageUtilsTest : public testing::Test {
|
| + public:
|
| + OfflinePageUtilsTest();
|
| + ~OfflinePageUtilsTest() override;
|
| +
|
| + void SetUp() override;
|
| +
|
| + void PumpLoop();
|
| + void PreloadStore();
|
| +
|
| + TestingProfile* profile() { return &profile_; }
|
| + OfflinePageTestStore* store() { return store_; }
|
| + const base::FilePath& test_file_path_1() { return test_file_path_1_; }
|
| + const base::FilePath& test_file_path_2() { return test_file_path_2_; }
|
| +
|
| + private:
|
| + scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
|
| + base::ThreadTaskRunnerHandle task_runner_handle_;
|
| +
|
| + base::FilePath test_file_path_1_;
|
| + base::FilePath test_file_path_2_;
|
| + base::ScopedTempDir scoped_temp_dir_;
|
| + TestingProfile profile_;
|
| + OfflinePageTestStore* store_;
|
| +};
|
| +
|
| +OfflinePageUtilsTest::OfflinePageUtilsTest()
|
| + : task_runner_(new base::TestSimpleTaskRunner),
|
| + task_runner_handle_(task_runner_) {}
|
| +
|
| +OfflinePageUtilsTest::~OfflinePageUtilsTest() {}
|
| +
|
| +void OfflinePageUtilsTest::SetUp() {
|
| + EXPECT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
|
| + base::CreateTemporaryFileInDir(scoped_temp_dir_.path(),
|
| + &test_file_path_1_);
|
| + base::CreateTemporaryFileInDir(scoped_temp_dir_.path(),
|
| + &test_file_path_2_);
|
| + OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
|
| + &profile_, BuildTestOfflinePageModel);
|
| + // Make sure the store contains the right offline pages before the Load
|
| + // happens (when we PumpLoop).
|
| + store_ = static_cast<OfflinePageTestStore*>(
|
| + OfflinePageModelFactory::GetForBrowserContext(&profile_)
|
| + ->GetStoreForTesting());
|
| + PreloadStore();
|
| + PumpLoop();
|
| + PumpLoop();
|
| + PumpLoop();
|
| +}
|
| +
|
| +void OfflinePageUtilsTest::PumpLoop() {
|
| + task_runner_->RunUntilIdle();
|
| +}
|
| +
|
| +void OfflinePageUtilsTest::PreloadStore() {
|
| + std::vector<OfflinePageItem> offline_pages;
|
| + offline_pages.push_back(
|
| + OfflinePageItem(GURL(kTestPage1Url), kTestPage1BookmarkId,
|
| + test_file_path_1(), kTestPage1FileSize));
|
| + offline_pages.push_back(
|
| + OfflinePageItem(GURL(kTestPage2Url), kTestPage2BookmarkId,
|
| + test_file_path_2(), kTestPage2FileSize));
|
| + store()->SetStoreState(offline_pages);
|
| +}
|
| +
|
| +TEST_F(OfflinePageUtilsTest, HasOfflinePages) {
|
| + LOG(INFO) << "Test starting";
|
| + EXPECT_TRUE(HasOfflinePages(profile()));
|
| +}
|
| +
|
| +TEST_F(OfflinePageUtilsTest, MightBeOfflineURL) {
|
| + // URL is invalid.
|
| + EXPECT_FALSE(MightBeOfflineURL(GURL("/test.mhtml")));
|
| + // Scheme is not file.
|
| + EXPECT_FALSE(MightBeOfflineURL(GURL("http://test.com/")));
|
| + // Does not end with .mhtml.
|
| + EXPECT_FALSE(MightBeOfflineURL(GURL("file:///test.txt")));
|
| + // Might still be an offline page.
|
| + EXPECT_TRUE(MightBeOfflineURL(GURL("file:///test.mhtml")));
|
| +}
|
| +
|
| +TEST_F(OfflinePageUtilsTest, GetOfflineURLByOnlineURL) {
|
| + LOG(INFO) << "Test starting";
|
| + EXPECT_EQ(net::FilePathToFileURL(test_file_path_1()),
|
| + GetOfflineURLByOnlineURL(profile(), GURL(kTestPage1Url)));
|
| + EXPECT_EQ(net::FilePathToFileURL(test_file_path_2()),
|
| + GetOfflineURLByOnlineURL(profile(), GURL(kTestPage2Url)));
|
| + EXPECT_EQ(GURL(), GetOfflineURLByOnlineURL(profile(), GURL(kTestPage3Url)));
|
| +}
|
| +
|
| +} // namespace android
|
| +} // namespace offline_pages
|
|
|