OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "components/reading_list/core/offline_url_utils.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "url/gurl.h" |
| 13 |
| 14 // Checks the root directory of offline pages. |
| 15 TEST(OfflineURLUtilsTest, OfflineRootDirectoryPathTest) { |
| 16 base::FilePath::StringType separator(&base::FilePath::kSeparators[0], 1); |
| 17 base::FilePath profile_path(FILE_PATH_LITERAL("profile_path")); |
| 18 base::FilePath offline_directory = |
| 19 reading_list::OfflineRootDirectoryPath(profile_path); |
| 20 // Expected value: profile_path/Offline |
| 21 std::string expected = |
| 22 base::StringPrintf("profile_path%" PRIsFP "Offline", separator.c_str()); |
| 23 EXPECT_EQ(expected, offline_directory.AsUTF8Unsafe()); |
| 24 } |
| 25 |
| 26 // Checks the offline page directory is the MD5 of the URL |
| 27 TEST(OfflineURLUtilsTest, OfflineURLDirectoryIDTest) { |
| 28 GURL url("http://www.google.com/test"); |
| 29 // MD5 of "http://www.google.com/test" |
| 30 std::string md5 = "0090071ef710946a1263c276284bb3b8"; |
| 31 std::string directory_id = reading_list::OfflineURLDirectoryID(url); |
| 32 EXPECT_EQ(md5, directory_id); |
| 33 } |
| 34 |
| 35 // Checks the offline page directory is |
| 36 // |profile_path|/Offline/OfflineURLDirectoryID; |
| 37 TEST(OfflineURLUtilsTest, OfflineURLDirectoryAbsolutePathTest) { |
| 38 base::FilePath::StringType separator(&base::FilePath::kSeparators[0], 1); |
| 39 base::FilePath profile_path(FILE_PATH_LITERAL("profile_path")); |
| 40 GURL url("http://www.google.com/test"); |
| 41 base::FilePath offline_directory = |
| 42 reading_list::OfflineURLDirectoryAbsolutePath(profile_path, url); |
| 43 // Expected value: profile_path/Offline/0090071ef710946a1263c276284bb3b8 |
| 44 std::string expected = |
| 45 base::StringPrintf("profile_path%" PRIsFP "Offline%" PRIsFP |
| 46 "0090071ef710946a1263c276284bb3b8", |
| 47 separator.c_str(), separator.c_str()); |
| 48 EXPECT_EQ(expected, offline_directory.AsUTF8Unsafe()); |
| 49 } |
| 50 |
| 51 // Checks the offline page directory is |
| 52 // |profile_path|/Offline/OfflineURLDirectoryID; |
| 53 TEST(OfflineURLUtilsTest, AbsolutePathForRelativePathTest) { |
| 54 base::FilePath::StringType separator(&base::FilePath::kSeparators[0], 1); |
| 55 base::FilePath profile_path(FILE_PATH_LITERAL("profile_path")); |
| 56 base::FilePath relative_path(FILE_PATH_LITERAL("relative")); |
| 57 relative_path = relative_path.Append(FILE_PATH_LITERAL("path")); |
| 58 base::FilePath absolute_path = |
| 59 reading_list::OfflineURLAbsolutePathFromRelativePath(profile_path, |
| 60 relative_path); |
| 61 // Expected value: profile_path/Offline/relative/path |
| 62 std::string expected = base::StringPrintf( |
| 63 "profile_path%" PRIsFP "Offline%" PRIsFP "relative%" PRIsFP "path", |
| 64 separator.c_str(), separator.c_str(), separator.c_str()); |
| 65 EXPECT_EQ(expected, absolute_path.AsUTF8Unsafe()); |
| 66 } |
| 67 |
| 68 // Checks the offline page path is OfflineURLDirectoryID/page.html; |
| 69 TEST(OfflineURLUtilsTest, OfflinePagePathTest) { |
| 70 base::FilePath::StringType separator(&base::FilePath::kSeparators[0], 1); |
| 71 GURL url("http://www.google.com/test"); |
| 72 base::FilePath offline_page = |
| 73 reading_list::OfflinePagePath(url, reading_list::OFFLINE_TYPE_HTML); |
| 74 // Expected value: 0090071ef710946a1263c276284bb3b8/page.html |
| 75 std::string expected_html = |
| 76 base::StringPrintf("0090071ef710946a1263c276284bb3b8%" PRIsFP "page.html", |
| 77 separator.c_str()); |
| 78 EXPECT_EQ(expected_html, offline_page.AsUTF8Unsafe()); |
| 79 offline_page = |
| 80 reading_list::OfflinePagePath(url, reading_list::OFFLINE_TYPE_PDF); |
| 81 // Expected value: 0090071ef710946a1263c276284bb3b8/file.pdf |
| 82 std::string expected_pdf = base::StringPrintf( |
| 83 "0090071ef710946a1263c276284bb3b8%" PRIsFP "file.pdf", separator.c_str()); |
| 84 EXPECT_EQ(expected_pdf, offline_page.AsUTF8Unsafe()); |
| 85 } |
OLD | NEW |