Chromium Code Reviews| 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..14cbfdd085ec1a443e5c5c23af3bddda2f249115 |
| --- /dev/null |
| +++ b/chrome/browser/android/offline_pages/offline_page_utils_unittest.cc |
| @@ -0,0 +1,226 @@ |
| +// 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/callback.h" |
| +#include "base/command_line.h" |
| +#include "base/files/file.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.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/common/chrome_constants.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "components/offline_pages/offline_page_model.h" |
| +#include "components/offline_pages/offline_page_switches.h" |
| +#include "components/offline_pages/offline_page_test_archiver.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 { |
| + |
| +const GURL kTestPage1Url("http://test.org/page1"); |
| +const GURL kTestPage2Url("http://test.org/page2"); |
| +const GURL kTestPage3Url("http://test.org/page3"); |
| +int64 kTestPage1BookmarkId = 1234; |
|
jianli
2015/12/16 23:08:08
nit: why not also adding const for this and one be
fgorski
2015/12/17 19:00:14
Done.
|
| +int64 kTestPage2BookmarkId = 5678; |
| +const int64 kTestFileSize = 876543LL; |
| + |
| +} // namespace |
| + |
| +class OfflinePageUtilsTest |
| + : public testing::Test, |
| + public OfflinePageTestArchiver::Observer, |
| + public base::SupportsWeakPtr<OfflinePageUtilsTest> { |
| + public: |
| + OfflinePageUtilsTest(); |
| + ~OfflinePageUtilsTest() override; |
| + |
| + void SetUp() override; |
| + void RunUntilIdle(); |
| + |
| + // Necessary callbacks for the offline page model. |
| + void OnSavePageDone(OfflinePageModel::SavePageResult result); |
| + void OnClearAllDone(); |
| + |
| + // OfflinePageTestArchiver::Observer implementation: |
| + void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
| + |
| + const GURL& offline_url_1() const { return offline_url_1_; } |
|
jianli
2015/12/16 23:08:08
Please comment on these URLs, like their behaviors
fgorski
2015/12/17 19:00:14
Done.
|
| + const GURL& offline_url_2() const { return offline_url_2_; } |
| + const GURL& offline_url_3() const { return offline_url_3_; } |
| + TestingProfile* profile() { return &profile_; } |
| + |
| + private: |
| + void CreateOfflinePages(); |
| + scoped_ptr<OfflinePageTestArchiver> BuildArchiver( |
| + const GURL& url, |
| + const base::FilePath& file_name); |
| + |
| + GURL offline_url_1_; |
| + GURL offline_url_2_; |
| + GURL offline_url_3_; |
| + |
| + scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| + base::ThreadTaskRunnerHandle task_runner_handle_; |
| + TestingProfile profile_; |
| +}; |
| + |
| +OfflinePageUtilsTest::OfflinePageUtilsTest() |
| + : task_runner_(new base::TestSimpleTaskRunner), |
| + task_runner_handle_(task_runner_) {} |
| + |
| +OfflinePageUtilsTest::~OfflinePageUtilsTest() {} |
| + |
| +void OfflinePageUtilsTest::SetUp() { |
| + // Enable offline pages feature. |
| + base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableOfflinePages); |
| + |
| + // Set up the factory for testing. |
| + OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| + &profile_, BuildTestOfflinePageModel); |
| + RunUntilIdle(); |
| + |
| + // Make sure the store contains the right offline pages before the load |
| + // happens. |
| + CreateOfflinePages(); |
| +} |
| + |
| +void OfflinePageUtilsTest::RunUntilIdle() { |
| + task_runner_->RunUntilIdle(); |
| +} |
| + |
| +void OfflinePageUtilsTest::OnSavePageDone( |
| + OfflinePageModel::SavePageResult result) { |
| + // Result ignored here. |
| +} |
| + |
| +void OfflinePageUtilsTest::OnClearAllDone() { |
| + // Result ignored here. |
| +} |
| + |
| +void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( |
| + const base::FilePath& file_path) { |
| + LOG(INFO) << file_path.value(); |
| + if (offline_url_1_.is_empty()) { |
| + offline_url_1_ = net::FilePathToFileURL(file_path); |
|
jianli
2015/12/16 23:08:07
I think this trick is not better than trying to ge
fgorski
2015/12/17 19:00:14
Done, but I simply decided to ask the model, which
|
| + return; |
| + } |
| + |
| + ASSERT_TRUE(offline_url_2_.is_empty()); |
| + offline_url_2_ = net::FilePathToFileURL(file_path); |
| +} |
| + |
| +void OfflinePageUtilsTest::CreateOfflinePages() { |
| + // Create page 1. |
| + scoped_ptr<OfflinePageTestArchiver> archiver( |
| + BuildArchiver(kTestPage1Url, |
| + base::FilePath(FILE_PATH_LITERAL("page1.mhtml"))) |
| + .Pass()); |
| + OfflinePageModelFactory::GetForBrowserContext(profile())->SavePage( |
| + kTestPage1Url, kTestPage1BookmarkId, archiver.Pass(), |
| + base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| + RunUntilIdle(); |
| + |
| + // Create page 2. |
| + archiver = BuildArchiver(kTestPage2Url, |
| + base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))) |
| + .Pass(); |
| + OfflinePageModelFactory::GetForBrowserContext(profile())->SavePage( |
| + kTestPage2Url, kTestPage2BookmarkId, archiver.Pass(), |
| + base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| + RunUntilIdle(); |
| + |
| + // Create path to 3rd file. |
|
jianli
2015/12/16 23:08:07
nit: Create a file that is not associated with any
fgorski
2015/12/17 19:00:14
Done.
|
| + offline_url_3_ = net::FilePathToFileURL( |
| + profile() |
| + ->GetPath() |
| + .Append(chrome::kOfflinePageArchviesDirname) |
| + .Append(FILE_PATH_LITERAL("missing_file.mhtml"))); |
| +} |
| + |
| +scoped_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( |
| + const GURL& url, |
| + const base::FilePath& file_name) { |
| + scoped_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( |
| + this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| + kTestFileSize, base::ThreadTaskRunnerHandle::Get())); |
| + archiver->set_file_name(file_name); |
| + return archiver.Pass(); |
| +} |
| + |
| +// Simple test for offline page model having any pages loaded. |
| +TEST_F(OfflinePageUtilsTest, HasOfflinePages) { |
| + EXPECT_TRUE(OfflinePageUtils::HasOfflinePages(profile())); |
| + |
| + OfflinePageModelFactory::GetForBrowserContext(profile())->ClearAll( |
| + base::Bind(&OfflinePageUtilsTest::OnClearAllDone, AsWeakPtr())); |
| + RunUntilIdle(); |
| + |
| + EXPECT_FALSE(OfflinePageUtils::HasOfflinePages(profile())); |
| +} |
| + |
| +TEST_F(OfflinePageUtilsTest, MightBeOfflineURL) { |
| + // URL is invalid. |
| + EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("/test.mhtml"))); |
| + // Scheme is not file. |
| + EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("http://test.com/"))); |
| + // Does not end with .mhtml. |
| + EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("file:///test.txt"))); |
| + // Might still be an offline page. |
| + EXPECT_TRUE(OfflinePageUtils::MightBeOfflineURL(GURL("file:///test.mhtml"))); |
| +} |
| + |
| +TEST_F(OfflinePageUtilsTest, GetOfflineURLByOnlineURL) { |
| + EXPECT_EQ(offline_url_1(), OfflinePageUtils::GetOfflineURLByOnlineURL( |
| + profile(), kTestPage1Url)); |
| + EXPECT_EQ(offline_url_2(), OfflinePageUtils::GetOfflineURLByOnlineURL( |
| + profile(), kTestPage2Url)); |
| + EXPECT_EQ(GURL(), OfflinePageUtils::GetOfflineURLByOnlineURL( |
| + profile(), GURL(kTestPage3Url))); |
| +} |
| + |
| +TEST_F(OfflinePageUtilsTest, GetOnlineURLByOfflineURL) { |
| + EXPECT_EQ(kTestPage1Url, OfflinePageUtils::GetOnlineURLByOfflineURL( |
| + profile(), offline_url_1())); |
| + EXPECT_EQ(kTestPage2Url, OfflinePageUtils::GetOnlineURLByOfflineURL( |
| + profile(), offline_url_2())); |
| + EXPECT_EQ(GURL::EmptyGURL(), OfflinePageUtils::GetOfflineURLByOnlineURL( |
| + profile(), offline_url_3())); |
| +} |
| + |
| +TEST_F(OfflinePageUtilsTest, GetBookmarkIdByOfflineURL) { |
| + EXPECT_EQ(kTestPage1BookmarkId, OfflinePageUtils::GetBookmarkIdByOfflineURL( |
| + profile(), offline_url_1())); |
| + EXPECT_EQ(kTestPage2BookmarkId, OfflinePageUtils::GetBookmarkIdByOfflineURL( |
| + profile(), offline_url_2())); |
| + EXPECT_EQ(-1, OfflinePageUtils::GetBookmarkIdByOfflineURL(profile(), |
| + offline_url_3())); |
| +} |
| + |
| +TEST_F(OfflinePageUtilsTest, IsOfflinePage) { |
| + EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_1())); |
| + EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_2())); |
| + EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_3())); |
| + EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage1Url)); |
| + EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage2Url)); |
| +} |
| + |
| +TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { |
| + EXPECT_TRUE( |
| + OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage1Url)); |
| + EXPECT_TRUE( |
| + OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage2Url)); |
| + EXPECT_FALSE( |
| + OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage3Url)); |
| +} |
| + |
| +} // namespace offline_pages |