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..ca90b543c020e8cab15b84d380db401261656706 |
--- /dev/null |
+++ b/chrome/browser/android/offline_pages/offline_page_utils_unittest.cc |
@@ -0,0 +1,246 @@ |
+// 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_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_page1.mhtml"); |
+const base::FilePath::CharType kTestPage2FilePath[] = |
+ FILE_PATH_LITERAL("test_page2.mhtml"); |
+// const base::FilePath::CharType kTestPage3FilePath[] = |
dewittj
2015/12/15 18:30:31
nit: remove commented out code
fgorski
2015/12/16 21:43:16
Done.
|
+// FILE_PATH_LITERAL("/offline_pages/test_org_page3.mhtml"); |
+int64 kTestPage1FileSize = 654321; |
+int64 kTestPage2FileSize = 765432; |
+ |
+} // namespace |
+ |
+class OfflinePageUtilsBasicTest : public testing::Test { |
+ public: |
+ OfflinePageUtilsBasicTest(); |
+ ~OfflinePageUtilsBasicTest() override; |
+ |
+ void SetUp() override; |
+ |
+ void FactorySetup(); |
+ void DisableOfflinePagesFeature(); |
+ void EnableOfflinePagesFeature(); |
+ void PumpLoop(); |
+ |
+ TestingProfile* profile() { return &profile_; } |
+ |
+ private: |
+ base::FilePath CreateArchiveFile(const base::FilePath& file_name); |
+ |
+ scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
+ base::ThreadTaskRunnerHandle task_runner_handle_; |
+ |
+ TestingProfile profile_; |
+}; |
+ |
+OfflinePageUtilsBasicTest::OfflinePageUtilsBasicTest() |
+ : task_runner_(new base::TestSimpleTaskRunner), |
+ task_runner_handle_(task_runner_) {} |
+ |
+OfflinePageUtilsBasicTest::~OfflinePageUtilsBasicTest() {} |
+ |
+void OfflinePageUtilsBasicTest::SetUp() { |
+ FactorySetup(); |
+ PumpLoop(); |
+} |
+ |
+void OfflinePageUtilsBasicTest::FactorySetup() { |
+ OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
+ &profile_, BuildTestOfflinePageModel); |
+} |
+ |
+void OfflinePageUtilsBasicTest::DisableOfflinePagesFeature() { |
+ base::CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kDisableOfflinePages); |
+} |
+ |
+void OfflinePageUtilsBasicTest::EnableOfflinePagesFeature() { |
+ base::CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kEnableOfflinePages); |
+} |
+ |
+void OfflinePageUtilsBasicTest::PumpLoop() { |
+ task_runner_->RunUntilIdle(); |
+} |
+ |
+// Simple test for checking if offline page has pages when the feature is |
+// disabled. |
+TEST_F(OfflinePageUtilsBasicTest, HasOfflinePagesWhenFeatureDisabled) { |
jianli
2015/12/15 23:53:34
HasOfflinePagesWhenFeatureDisabled is confusing. T
fgorski
2015/12/16 21:43:16
Done.
|
+ DisableOfflinePagesFeature(); |
+ EXPECT_FALSE(HasOfflinePages(profile())); |
+} |
+ |
+// Simple test for offline page model having none pages loaded. |
+TEST_F(OfflinePageUtilsBasicTest, HasOfflinePagesWhenStoreIsEmpty) { |
jianli
2015/12/15 23:53:34
ditto for test method name
fgorski
2015/12/16 21:43:16
Done.
|
+ EnableOfflinePagesFeature(); |
+ EXPECT_FALSE(HasOfflinePages(profile())); |
+} |
+ |
+class OfflinePageUtilsTest : public OfflinePageUtilsBasicTest { |
+ public: |
+ ~OfflinePageUtilsTest() override; |
+ |
+ void SetUp() override; |
+ |
+ OfflinePageTestStore* store() { return store_; } |
jianli
2015/12/15 23:53:34
nit: add const modifier
fgorski
2015/12/16 21:43:16
Done.
|
+ OfflinePageModel* model() { return model_; } |
jianli
2015/12/15 23:53:34
ditto
fgorski
2015/12/16 21:43:16
Done.
|
+ |
+ const base::FilePath& test_file_path_1() { return test_file_path_1_; } |
jianli
2015/12/15 23:53:34
ditto for this and two below
fgorski
2015/12/16 21:43:16
Done.
|
+ const base::FilePath& test_file_path_2() { return test_file_path_2_; } |
+ const base::FilePath& test_file_path_3() { return test_file_path_3_; } |
+ |
+ private: |
+ void CreateOfflinePages(); |
+ base::FilePath CreateArchiveFile(const base::FilePath& file_name); |
+ |
+ OfflinePageModel* model_; |
+ OfflinePageTestStore* store_; |
+ |
+ base::FilePath test_file_path_1_; |
+ base::FilePath test_file_path_2_; |
+ base::FilePath test_file_path_3_; |
+}; |
+ |
+OfflinePageUtilsTest::~OfflinePageUtilsTest() {} |
+ |
+void OfflinePageUtilsTest::SetUp() { |
+ EnableOfflinePagesFeature(); |
+ |
+ FactorySetup(); |
+ model_ = OfflinePageModelFactory::GetForBrowserContext(profile()); |
+ store_ = static_cast<OfflinePageTestStore*>(model()->GetStoreForTesting()); |
dewittj
2015/12/15 18:30:31
Any way to get this without doing a static_cast?
fgorski
2015/12/16 21:43:16
Done.
|
+ // Make sure the store contains the right offline pages before the load |
+ // happens. |
+ CreateOfflinePages(); |
+ // Load from store happens here. |
+ PumpLoop(); |
+} |
+ |
+void OfflinePageUtilsTest::CreateOfflinePages() { |
+ // Create offline copies. |
+ base::FilePath archives_dir = |
+ profile()->GetPath().Append(chrome::kOfflinePageArchviesDirname); |
+ base::CreateDirectory(archives_dir); |
+ test_file_path_1_ = CreateArchiveFile(base::FilePath(kTestPage1FilePath)); |
+ test_file_path_2_ = CreateArchiveFile(base::FilePath(kTestPage2FilePath)); |
+ test_file_path_3_ = |
+ archives_dir.Append(FILE_PATH_LITERAL("missing_file.mhtml")); |
+ |
+ // Update store metadata. |
+ store()->AddOrUpdateOfflinePage( |
+ OfflinePageItem(GURL(kTestPage1Url), kTestPage1BookmarkId, |
+ test_file_path_1(), kTestPage1FileSize), |
+ base::Callback<void(bool)>()); |
+ store()->AddOrUpdateOfflinePage( |
+ OfflinePageItem(GURL(kTestPage2Url), kTestPage2BookmarkId, |
+ test_file_path_2(), kTestPage2FileSize), |
+ base::Callback<void(bool)>()); |
+} |
+ |
+base::FilePath OfflinePageUtilsTest::CreateArchiveFile( |
+ const base::FilePath& file_name) { |
+ base::FilePath archives_dir = |
+ profile()->GetPath().Append(chrome::kOfflinePageArchviesDirname); |
+ base::FilePath file_path(archives_dir.Append(file_name)); |
+ base::File file(file_path, base::File::FLAG_OPEN_ALWAYS); |
+ return file_path; |
+} |
+ |
+// Simple test for offline page model having any pages loaded. |
+TEST_F(OfflinePageUtilsTest, HasOfflinePages) { |
+ EXPECT_TRUE(HasOfflinePages(profile())); |
+} |
+ |
+TEST_F(OfflinePageUtilsTest, MightBeOfflineURL) { |
dewittj
2015/12/15 18:30:31
Could this be in the basic tests?
fgorski
2015/12/16 21:43:16
Done.
|
+ // 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) { |
+ 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))); |
+} |
+ |
+TEST_F(OfflinePageUtilsTest, GetOnlineURLByOfflineURL) { |
+ EXPECT_EQ(GURL(kTestPage1Url), |
+ GetOnlineURLByOfflineURL( |
+ profile(), net::FilePathToFileURL(test_file_path_1()))); |
+ EXPECT_EQ(GURL(kTestPage2Url), |
+ GetOnlineURLByOfflineURL( |
+ profile(), net::FilePathToFileURL(test_file_path_2()))); |
+ EXPECT_EQ(GURL(), GetOfflineURLByOnlineURL( |
+ profile(), net::FilePathToFileURL(test_file_path_3()))); |
+} |
+ |
+TEST_F(OfflinePageUtilsTest, GetBookmarkIdByOfflineURL) { |
+ EXPECT_EQ(kTestPage1BookmarkId, |
+ GetBookmarkIdByOfflineURL( |
+ profile(), net::FilePathToFileURL(test_file_path_1()))); |
+ EXPECT_EQ(kTestPage2BookmarkId, |
+ GetBookmarkIdByOfflineURL( |
+ profile(), net::FilePathToFileURL(test_file_path_2()))); |
+ EXPECT_EQ(-1, GetBookmarkIdByOfflineURL( |
+ profile(), net::FilePathToFileURL(test_file_path_3()))); |
+} |
+ |
+TEST_F(OfflinePageUtilsTest, IsOfflinePage) { |
+ EXPECT_TRUE( |
+ IsOfflinePage(profile(), net::FilePathToFileURL(test_file_path_1()))); |
+ EXPECT_TRUE( |
+ IsOfflinePage(profile(), net::FilePathToFileURL(test_file_path_2()))); |
+ EXPECT_FALSE( |
+ IsOfflinePage(profile(), net::FilePathToFileURL(test_file_path_3()))); |
+ EXPECT_FALSE(IsOfflinePage(profile(), GURL(kTestPage1Url))); |
+ EXPECT_FALSE(IsOfflinePage(profile(), GURL(kTestPage2Url))); |
+} |
+ |
+TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { |
+ EXPECT_TRUE(HasOfflinePageForOnlineURL(profile(), GURL(kTestPage1Url))); |
+ EXPECT_TRUE(HasOfflinePageForOnlineURL(profile(), GURL(kTestPage2Url))); |
+ EXPECT_FALSE(HasOfflinePageForOnlineURL(profile(), GURL(kTestPage3Url))); |
+} |
+ |
+} // namespace android |
+} // namespace offline_pages |