| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ios/chrome/browser/reading_list/offline_url_utils.h" | 5 #include "ios/chrome/browser/reading_list/offline_url_utils.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/test/gtest_util.h" | 12 #include "base/test/gtest_util.h" |
| 13 #include "base/time/default_clock.h" |
| 14 #include "components/reading_list/core/reading_list_entry.h" |
| 15 #include "components/reading_list/core/reading_list_model_impl.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 14 | 18 |
| 15 // Checks the distilled URL for the page is chrome://offline/MD5/page.html; | |
| 16 TEST(OfflineURLUtilsTest, DistilledURLForPathTest) { | |
| 17 base::FilePath page_path("MD5/page.html"); | |
| 18 GURL distilled_url = | |
| 19 reading_list::OfflineURLForPath(page_path, GURL(), GURL()); | |
| 20 EXPECT_EQ("chrome://offline/MD5/page.html", distilled_url.spec()); | |
| 21 } | |
| 22 | |
| 23 // Checks the distilled URL for the page with an onlineURL is | 19 // Checks the distilled URL for the page with an onlineURL is |
| 24 // chrome://offline/MD5/page.html?entryURL=...&virtualURL=... | 20 // chrome://offline/MD5/page.html?entryURL=...&virtualURL=... |
| 25 TEST(OfflineURLUtilsTest, OfflineURLForPathWithEntryURLAndVirtualURLTest) { | 21 TEST(OfflineURLUtilsTest, OfflineURLForPathWithEntryURLAndVirtualURLTest) { |
| 26 base::FilePath page_path("MD5/page.html"); | 22 base::FilePath page_path("MD5/page.html"); |
| 27 GURL entry_url = GURL("http://foo.bar"); | 23 GURL entry_url = GURL("http://foo.bar"); |
| 28 GURL virtual_url = GURL("http://foo.bar/virtual"); | 24 GURL virtual_url = GURL("http://foo.bar/virtual"); |
| 29 GURL distilled_url = | 25 GURL distilled_url = |
| 30 reading_list::OfflineURLForPath(page_path, entry_url, virtual_url); | 26 reading_list::OfflineURLForPath(page_path, entry_url, virtual_url); |
| 31 EXPECT_EQ( | 27 EXPECT_EQ( |
| 32 "chrome://offline/MD5/page.html?" | 28 "chrome://offline/MD5/page.html?" |
| 33 "entryURL=http%3A%2F%2Ffoo.bar%2F&" | 29 "entryURL=http%3A%2F%2Ffoo.bar%2F&" |
| 34 "virtualURL=http%3A%2F%2Ffoo.bar%2Fvirtual", | 30 "virtualURL=http%3A%2F%2Ffoo.bar%2Fvirtual", |
| 35 distilled_url.spec()); | 31 distilled_url.spec()); |
| 36 } | 32 } |
| 37 | 33 |
| 38 // Checks the parsing of offline URL chrome://offline/MD5/page.html. | 34 // Checks the parsing of offline URL chrome://offline/MD5/page.html. |
| 39 // As entryURL and virtualURL are absent, they should return the offline URL. | 35 // As entryURL and virtualURL are absent, they should be invalid. |
| 40 TEST(OfflineURLUtilsTest, ParseOfflineURLTest) { | 36 TEST(OfflineURLUtilsTest, ParseOfflineURLTest) { |
| 41 GURL distilled_url("chrome://offline/MD5/page.html"); | 37 GURL distilled_url("chrome://offline/MD5/page.html"); |
| 42 GURL entry_url = reading_list::EntryURLForOfflineURL(distilled_url); | 38 GURL entry_url = reading_list::EntryURLForOfflineURL(distilled_url); |
| 43 EXPECT_EQ("chrome://offline/MD5/page.html", entry_url.spec()); | 39 EXPECT_TRUE(entry_url.is_empty()); |
| 44 GURL virtual_url = reading_list::VirtualURLForOfflineURL(distilled_url); | 40 GURL virtual_url = reading_list::VirtualURLForOfflineURL(distilled_url); |
| 45 EXPECT_EQ("chrome://offline/MD5/page.html", virtual_url.spec()); | 41 EXPECT_TRUE(virtual_url.is_empty()); |
| 46 } | 42 } |
| 47 | 43 |
| 48 // Checks the parsing of offline URL | 44 // Checks the parsing of offline URL |
| 49 // chrome://offline/MD5/page.html?entryURL=encorded%20URL | 45 // chrome://offline/MD5/page.html?entryURL=encorded%20URL |
| 50 // As entryURL is present, it should be returned correctly. | 46 // As entryURL is present, it should be returned correctly. |
| 51 // As virtualURL is absent, it should return the entryURL. | 47 // As virtualURL is absent, it should return GURL::EmptyGURL(). |
| 52 TEST(OfflineURLUtilsTest, ParseOfflineURLWithEntryURLTest) { | 48 TEST(OfflineURLUtilsTest, ParseOfflineURLWithEntryURLTest) { |
| 53 GURL offline_url( | 49 GURL offline_url( |
| 54 "chrome://offline/MD5/page.html?entryURL=http%3A%2F%2Ffoo.bar%2F"); | 50 "chrome://offline/MD5/page.html?entryURL=http%3A%2F%2Ffoo.bar%2F"); |
| 55 GURL entry_url = reading_list::EntryURLForOfflineURL(offline_url); | 51 GURL entry_url = reading_list::EntryURLForOfflineURL(offline_url); |
| 56 EXPECT_EQ("http://foo.bar/", entry_url.spec()); | 52 EXPECT_EQ("http://foo.bar/", entry_url.spec()); |
| 57 GURL virtual_url = reading_list::VirtualURLForOfflineURL(offline_url); | 53 GURL virtual_url = reading_list::VirtualURLForOfflineURL(offline_url); |
| 58 EXPECT_EQ("http://foo.bar/", virtual_url.spec()); | 54 EXPECT_TRUE(virtual_url.is_empty()); |
| 59 } | 55 } |
| 60 | 56 |
| 61 // Checks the parsing of offline URL | 57 // Checks the parsing of offline URL |
| 62 // chrome://offline/MD5/page.html?virtualURL=encorded%20URL | 58 // chrome://offline/MD5/page.html?virtualURL=encorded%20URL |
| 63 // As entryURL is absent, it should return the offline URL. | 59 // As entryURL is absent, it should return the offline URL. |
| 64 // As virtualURL is present, it should be returned correctly. | 60 // As virtualURL is present, it should be returned correctly. |
| 65 TEST(OfflineURLUtilsTest, ParseOfflineURLWithVirtualURLTest) { | 61 TEST(OfflineURLUtilsTest, ParseOfflineURLWithVirtualURLTest) { |
| 66 GURL offline_url( | 62 GURL offline_url( |
| 67 "chrome://offline/MD5/page.html?virtualURL=http%3A%2F%2Ffoo.bar%2F"); | 63 "chrome://offline/MD5/page.html?virtualURL=http%3A%2F%2Ffoo.bar%2F"); |
| 68 GURL entry_url = reading_list::EntryURLForOfflineURL(offline_url); | 64 GURL entry_url = reading_list::EntryURLForOfflineURL(offline_url); |
| 69 EXPECT_EQ(offline_url, entry_url); | 65 EXPECT_TRUE(entry_url.is_empty()); |
| 70 GURL virtual_url = reading_list::VirtualURLForOfflineURL(offline_url); | 66 GURL virtual_url = reading_list::VirtualURLForOfflineURL(offline_url); |
| 71 EXPECT_EQ("http://foo.bar/", virtual_url.spec()); | 67 EXPECT_EQ("http://foo.bar/", virtual_url.spec()); |
| 72 } | 68 } |
| 73 | 69 |
| 74 // Checks the parsing of offline URL | 70 // Checks the parsing of offline URL |
| 75 // chrome://offline/MD5/page.html?entryURL=...&virtualURL=... | 71 // chrome://offline/MD5/page.html?entryURL=...&virtualURL=... |
| 76 // As entryURL is present, it should be returned correctly. | 72 // As entryURL is present, it should be returned correctly. |
| 77 // As virtualURL is present, it should be returned correctly. | 73 // As virtualURL is present, it should be returned correctly. |
| 78 TEST(OfflineURLUtilsTest, ParseOfflineURLWithVirtualAndEntryURLTest) { | 74 TEST(OfflineURLUtilsTest, ParseOfflineURLWithVirtualAndEntryURLTest) { |
| 79 GURL offline_url( | 75 GURL offline_url( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 EXPECT_FALSE(reading_list::IsOfflineURL(GURL("chrome://offline-foobar"))); | 114 EXPECT_FALSE(reading_list::IsOfflineURL(GURL("chrome://offline-foobar"))); |
| 119 EXPECT_FALSE(reading_list::IsOfflineURL(GURL("http://offline/"))); | 115 EXPECT_FALSE(reading_list::IsOfflineURL(GURL("http://offline/"))); |
| 120 EXPECT_FALSE(reading_list::IsOfflineURL(GURL("http://chrome://offline/"))); | 116 EXPECT_FALSE(reading_list::IsOfflineURL(GURL("http://chrome://offline/"))); |
| 121 EXPECT_TRUE(reading_list::IsOfflineURL(GURL("chrome://offline"))); | 117 EXPECT_TRUE(reading_list::IsOfflineURL(GURL("chrome://offline"))); |
| 122 EXPECT_TRUE(reading_list::IsOfflineURL(GURL("chrome://offline/"))); | 118 EXPECT_TRUE(reading_list::IsOfflineURL(GURL("chrome://offline/"))); |
| 123 EXPECT_TRUE(reading_list::IsOfflineURL(GURL("chrome://offline/foobar"))); | 119 EXPECT_TRUE(reading_list::IsOfflineURL(GURL("chrome://offline/foobar"))); |
| 124 EXPECT_TRUE( | 120 EXPECT_TRUE( |
| 125 reading_list::IsOfflineURL(GURL("chrome://offline/foobar?foo=bar"))); | 121 reading_list::IsOfflineURL(GURL("chrome://offline/foobar?foo=bar"))); |
| 126 } | 122 } |
| 127 | 123 |
| 124 // Checks that the offline URLs are correctly detected by |IsOfflineURL|. |
| 125 TEST(OfflineURLUtilsTest, IsOfflineURLValid) { |
| 126 auto reading_list_model = base::MakeUnique<ReadingListModelImpl>( |
| 127 nullptr, nullptr, base::MakeUnique<base::DefaultClock>()); |
| 128 GURL entry_url("http://entry_url.com"); |
| 129 base::FilePath distilled_path("distilled/page.html"); |
| 130 GURL distilled_url("http://distilled_url.com"); |
| 131 reading_list_model->AddEntry(entry_url, "title", |
| 132 reading_list::ADDED_VIA_CURRENT_APP); |
| 133 reading_list_model->SetEntryDistilledInfo( |
| 134 entry_url, distilled_path, distilled_url, 10, base::Time::Now()); |
| 135 |
| 136 EXPECT_FALSE( |
| 137 reading_list::IsOfflineURLValid(GURL(), reading_list_model.get())); |
| 138 EXPECT_FALSE(reading_list::IsOfflineURLValid(GURL("chrome://"), |
| 139 reading_list_model.get())); |
| 140 EXPECT_FALSE(reading_list::IsOfflineURLValid(GURL("chrome://offline-foobar"), |
| 141 reading_list_model.get())); |
| 142 EXPECT_FALSE(reading_list::IsOfflineURLValid(GURL("http://offline/"), |
| 143 reading_list_model.get())); |
| 144 EXPECT_FALSE(reading_list::IsOfflineURLValid(GURL("http://chrome://offline/"), |
| 145 reading_list_model.get())); |
| 146 EXPECT_FALSE(reading_list::IsOfflineURLValid(GURL("chrome://offline"), |
| 147 reading_list_model.get())); |
| 148 EXPECT_FALSE(reading_list::IsOfflineURLValid(GURL("chrome://offline/"), |
| 149 reading_list_model.get())); |
| 150 EXPECT_FALSE(reading_list::IsOfflineURLValid(GURL("chrome://offline/foobar"), |
| 151 reading_list_model.get())); |
| 152 EXPECT_FALSE(reading_list::IsOfflineURLValid( |
| 153 GURL("chrome://offline/foobar?foo=bar"), reading_list_model.get())); |
| 154 EXPECT_TRUE(reading_list::IsOfflineURLValid( |
| 155 reading_list::OfflineURLForPath(distilled_path, entry_url, distilled_url), |
| 156 reading_list_model.get())); |
| 157 EXPECT_FALSE(reading_list::IsOfflineURLValid( |
| 158 reading_list::OfflineURLForPath(distilled_path, entry_url, entry_url), |
| 159 reading_list_model.get())); |
| 160 EXPECT_FALSE(reading_list::IsOfflineURLValid( |
| 161 reading_list::OfflineURLForPath(base::FilePath("not_distilled_path"), |
| 162 entry_url, distilled_url), |
| 163 reading_list_model.get())); |
| 164 reading_list_model->RemoveEntryByURL(entry_url); |
| 165 EXPECT_FALSE(reading_list::IsOfflineURLValid( |
| 166 reading_list::OfflineURLForPath(distilled_path, entry_url, distilled_url), |
| 167 reading_list_model.get())); |
| 168 } |
| OLD | NEW |