OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/test/test_simple_task_runner.h" |
| 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 13 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h
" |
| 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "components/offline_pages/offline_page_model.h" |
| 16 #include "components/offline_pages/offline_page_test_store.h" |
| 17 #include "net/base/filename_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "url/gurl.h" |
| 20 |
| 21 namespace offline_pages { |
| 22 namespace android { |
| 23 |
| 24 namespace { |
| 25 const char kTestPage1Url[] = "http://test.org/page1"; |
| 26 const char kTestPage2Url[] = "http://test.org/page2"; |
| 27 const char kTestPage3Url[] = "http://test.org/page3"; |
| 28 int64 kTestPage1BookmarkId = 1234; |
| 29 int64 kTestPage2BookmarkId = 5678; |
| 30 //const base::FilePath::CharType kTestPage1FilePath[] = |
| 31 // FILE_PATH_LITERAL("test_org_page1.mhtml"); |
| 32 //const base::FilePath::CharType kTestPage2FilePath[] = |
| 33 // FILE_PATH_LITERAL("test_org_page2.mhtml"); |
| 34 //const base::FilePath::CharType kTestPage3FilePath[] = |
| 35 // FILE_PATH_LITERAL("/offline_pages/test_org_page3.mhtml"); |
| 36 int64 kTestPage1FileSize = 654321; |
| 37 int64 kTestPage2FileSize = 765432; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 class OfflinePageUtilsTest : public testing::Test { |
| 42 public: |
| 43 OfflinePageUtilsTest(); |
| 44 ~OfflinePageUtilsTest() override; |
| 45 |
| 46 void SetUp() override; |
| 47 |
| 48 void PumpLoop(); |
| 49 void PreloadStore(); |
| 50 |
| 51 TestingProfile* profile() { return &profile_; } |
| 52 OfflinePageTestStore* store() { return store_; } |
| 53 const base::FilePath& test_file_path_1() { return test_file_path_1_; } |
| 54 const base::FilePath& test_file_path_2() { return test_file_path_2_; } |
| 55 |
| 56 private: |
| 57 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 58 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 59 |
| 60 base::FilePath test_file_path_1_; |
| 61 base::FilePath test_file_path_2_; |
| 62 base::ScopedTempDir scoped_temp_dir_; |
| 63 TestingProfile profile_; |
| 64 OfflinePageTestStore* store_; |
| 65 }; |
| 66 |
| 67 OfflinePageUtilsTest::OfflinePageUtilsTest() |
| 68 : task_runner_(new base::TestSimpleTaskRunner), |
| 69 task_runner_handle_(task_runner_) {} |
| 70 |
| 71 OfflinePageUtilsTest::~OfflinePageUtilsTest() {} |
| 72 |
| 73 void OfflinePageUtilsTest::SetUp() { |
| 74 EXPECT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
| 75 base::CreateTemporaryFileInDir(scoped_temp_dir_.path(), |
| 76 &test_file_path_1_); |
| 77 base::CreateTemporaryFileInDir(scoped_temp_dir_.path(), |
| 78 &test_file_path_2_); |
| 79 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| 80 &profile_, BuildTestOfflinePageModel); |
| 81 // Make sure the store contains the right offline pages before the Load |
| 82 // happens (when we PumpLoop). |
| 83 store_ = static_cast<OfflinePageTestStore*>( |
| 84 OfflinePageModelFactory::GetForBrowserContext(&profile_) |
| 85 ->GetStoreForTesting()); |
| 86 PreloadStore(); |
| 87 PumpLoop(); |
| 88 PumpLoop(); |
| 89 PumpLoop(); |
| 90 } |
| 91 |
| 92 void OfflinePageUtilsTest::PumpLoop() { |
| 93 task_runner_->RunUntilIdle(); |
| 94 } |
| 95 |
| 96 void OfflinePageUtilsTest::PreloadStore() { |
| 97 std::vector<OfflinePageItem> offline_pages; |
| 98 offline_pages.push_back( |
| 99 OfflinePageItem(GURL(kTestPage1Url), kTestPage1BookmarkId, |
| 100 test_file_path_1(), kTestPage1FileSize)); |
| 101 offline_pages.push_back( |
| 102 OfflinePageItem(GURL(kTestPage2Url), kTestPage2BookmarkId, |
| 103 test_file_path_2(), kTestPage2FileSize)); |
| 104 store()->SetStoreState(offline_pages); |
| 105 } |
| 106 |
| 107 TEST_F(OfflinePageUtilsTest, HasOfflinePages) { |
| 108 LOG(INFO) << "Test starting"; |
| 109 EXPECT_TRUE(HasOfflinePages(profile())); |
| 110 } |
| 111 |
| 112 TEST_F(OfflinePageUtilsTest, MightBeOfflineURL) { |
| 113 // URL is invalid. |
| 114 EXPECT_FALSE(MightBeOfflineURL(GURL("/test.mhtml"))); |
| 115 // Scheme is not file. |
| 116 EXPECT_FALSE(MightBeOfflineURL(GURL("http://test.com/"))); |
| 117 // Does not end with .mhtml. |
| 118 EXPECT_FALSE(MightBeOfflineURL(GURL("file:///test.txt"))); |
| 119 // Might still be an offline page. |
| 120 EXPECT_TRUE(MightBeOfflineURL(GURL("file:///test.mhtml"))); |
| 121 } |
| 122 |
| 123 TEST_F(OfflinePageUtilsTest, GetOfflineURLByOnlineURL) { |
| 124 LOG(INFO) << "Test starting"; |
| 125 EXPECT_EQ(net::FilePathToFileURL(test_file_path_1()), |
| 126 GetOfflineURLByOnlineURL(profile(), GURL(kTestPage1Url))); |
| 127 EXPECT_EQ(net::FilePathToFileURL(test_file_path_2()), |
| 128 GetOfflineURLByOnlineURL(profile(), GURL(kTestPage2Url))); |
| 129 EXPECT_EQ(GURL(), GetOfflineURLByOnlineURL(profile(), GURL(kTestPage3Url))); |
| 130 } |
| 131 |
| 132 } // namespace android |
| 133 } // namespace offline_pages |
OLD | NEW |