offline_url_utils_unittest.cc 3.9 KB

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