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 <vector> |
| 6 |
| 7 #include "components/dom_distiller/core/article_distillation_update.h" |
| 8 #include "components/dom_distiller/core/test_request_view_handle.h" |
| 9 #include "components/pref_registry/testing_pref_service_syncable.h" |
| 10 #include "grit/components_resources.h" |
| 11 #include "grit/components_strings.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 |
| 16 using testing::HasSubstr; |
| 17 using testing::Not; |
| 18 |
| 19 namespace dom_distiller { |
| 20 |
| 21 class DomDistillerRequestViewTest : public testing::Test { |
| 22 protected: |
| 23 void SetUp() override { |
| 24 pref_service_.reset(new user_prefs::TestingPrefServiceSyncable()); |
| 25 DistilledPagePrefs::RegisterProfilePrefs(pref_service_->registry()); |
| 26 distilled_page_prefs_.reset(new DistilledPagePrefs(pref_service_.get())); |
| 27 } |
| 28 |
| 29 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; |
| 30 scoped_ptr<DistilledPagePrefs> distilled_page_prefs_; |
| 31 }; |
| 32 |
| 33 TEST_F(DomDistillerRequestViewTest, TestTitleEscaped) { |
| 34 const std::string no_title = |
| 35 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); |
| 36 const std::string valid_title = "valid title"; |
| 37 const std::string has_quotes = "\"" + valid_title + "\""; |
| 38 const std::string escaped_quotes = "\\\"" + valid_title + "\\\""; |
| 39 const std::string has_special_chars = "<" + valid_title + "\\"; |
| 40 const std::string escaped_special_chars = "\\u003C" + valid_title + "\\\\"; |
| 41 |
| 42 TestRequestViewHandle handle(distilled_page_prefs_.get()); |
| 43 |
| 44 // Make sure title is properly escaped from quotes. |
| 45 { |
| 46 scoped_ptr<DistilledArticleProto> article_proto( |
| 47 new DistilledArticleProto()); |
| 48 article_proto->set_title(has_quotes); |
| 49 |
| 50 handle.OnArticleReady(article_proto.get()); |
| 51 |
| 52 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(escaped_quotes)); |
| 53 EXPECT_THAT(handle.GetJavaScriptBuffer(), Not(HasSubstr(has_quotes))); |
| 54 handle.ClearJavaScriptBuffer(); |
| 55 } |
| 56 |
| 57 // Make sure title is properly escaped from special characters. |
| 58 { |
| 59 scoped_ptr<DistilledArticleProto> article_proto( |
| 60 new DistilledArticleProto()); |
| 61 article_proto->set_title(has_special_chars); |
| 62 |
| 63 handle.OnArticleReady(article_proto.get()); |
| 64 |
| 65 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(escaped_special_chars)); |
| 66 EXPECT_THAT(handle.GetJavaScriptBuffer(), |
| 67 Not(HasSubstr(has_special_chars))); |
| 68 handle.ClearJavaScriptBuffer(); |
| 69 } |
| 70 |
| 71 } |
| 72 |
| 73 TEST_F(DomDistillerRequestViewTest, TestTitleNeverEmpty) { |
| 74 const std::string no_title = |
| 75 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_TITLE); |
| 76 const std::string valid_title = "valid title"; |
| 77 |
| 78 TestRequestViewHandle handle(distilled_page_prefs_.get()); |
| 79 |
| 80 // Test that the title actually gets shown. |
| 81 { |
| 82 scoped_ptr<DistilledArticleProto> article_proto( |
| 83 new DistilledArticleProto()); |
| 84 article_proto->set_title(valid_title); |
| 85 |
| 86 handle.OnArticleReady(article_proto.get()); |
| 87 |
| 88 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(valid_title)); |
| 89 EXPECT_THAT(handle.GetJavaScriptBuffer(), Not(HasSubstr(no_title))); |
| 90 handle.ClearJavaScriptBuffer(); |
| 91 } |
| 92 |
| 93 // Test empty string title |
| 94 { |
| 95 scoped_ptr<DistilledArticleProto> article_proto( |
| 96 new DistilledArticleProto()); |
| 97 article_proto->set_title(""); |
| 98 |
| 99 handle.OnArticleReady(article_proto.get()); |
| 100 |
| 101 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(no_title)); |
| 102 EXPECT_THAT(handle.GetJavaScriptBuffer(), Not(HasSubstr(valid_title))); |
| 103 handle.ClearJavaScriptBuffer(); |
| 104 } |
| 105 |
| 106 // Test no title. |
| 107 { |
| 108 scoped_ptr<DistilledArticleProto> article_proto( |
| 109 new DistilledArticleProto()); |
| 110 |
| 111 handle.OnArticleReady(article_proto.get()); |
| 112 |
| 113 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(no_title)); |
| 114 EXPECT_THAT(handle.GetJavaScriptBuffer(), Not(HasSubstr(valid_title))); |
| 115 handle.ClearJavaScriptBuffer(); |
| 116 } |
| 117 } |
| 118 |
| 119 TEST_F(DomDistillerRequestViewTest, TestContentNeverEmpty) { |
| 120 const std::string no_content = |
| 121 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
| 122 const std::string valid_content = "valid content"; |
| 123 |
| 124 TestRequestViewHandle handle(distilled_page_prefs_.get()); |
| 125 |
| 126 // Test single page content |
| 127 { |
| 128 scoped_ptr<DistilledArticleProto> article_proto( |
| 129 new DistilledArticleProto()); |
| 130 (*(article_proto->add_pages())).set_html(valid_content); |
| 131 |
| 132 handle.OnArticleReady(article_proto.get()); |
| 133 |
| 134 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(valid_content)); |
| 135 EXPECT_THAT(handle.GetJavaScriptBuffer(), Not(HasSubstr(no_content))); |
| 136 handle.ClearJavaScriptBuffer(); |
| 137 } |
| 138 |
| 139 // Test multiple page content |
| 140 { |
| 141 scoped_ptr<DistilledArticleProto> article_proto( |
| 142 new DistilledArticleProto()); |
| 143 (*(article_proto->add_pages())).set_html(valid_content); |
| 144 (*(article_proto->add_pages())).set_html(valid_content); |
| 145 |
| 146 handle.OnArticleReady(article_proto.get()); |
| 147 |
| 148 EXPECT_THAT(handle.GetJavaScriptBuffer(), |
| 149 HasSubstr(valid_content + valid_content)); |
| 150 EXPECT_THAT(handle.GetJavaScriptBuffer(), Not(HasSubstr(no_content))); |
| 151 handle.ClearJavaScriptBuffer(); |
| 152 } |
| 153 |
| 154 // Test empty string content |
| 155 { |
| 156 scoped_ptr<DistilledArticleProto> article_proto( |
| 157 new DistilledArticleProto()); |
| 158 (*(article_proto->add_pages())).set_html(""); |
| 159 |
| 160 handle.OnArticleReady(article_proto.get()); |
| 161 |
| 162 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(no_content)); |
| 163 EXPECT_THAT(handle.GetJavaScriptBuffer(), Not(HasSubstr(valid_content))); |
| 164 handle.ClearJavaScriptBuffer(); |
| 165 } |
| 166 |
| 167 // Test page no content |
| 168 { |
| 169 scoped_ptr<DistilledArticleProto> article_proto( |
| 170 new DistilledArticleProto()); |
| 171 article_proto->add_pages(); |
| 172 |
| 173 handle.OnArticleReady(article_proto.get()); |
| 174 |
| 175 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(no_content)); |
| 176 EXPECT_THAT(handle.GetJavaScriptBuffer(), Not(HasSubstr(valid_content))); |
| 177 handle.ClearJavaScriptBuffer(); |
| 178 } |
| 179 |
| 180 // Test no page. |
| 181 { |
| 182 scoped_ptr<DistilledArticleProto> article_proto( |
| 183 new DistilledArticleProto()); |
| 184 |
| 185 handle.OnArticleReady(article_proto.get()); |
| 186 |
| 187 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(no_content)); |
| 188 EXPECT_THAT(handle.GetJavaScriptBuffer(), Not(HasSubstr(valid_content))); |
| 189 handle.ClearJavaScriptBuffer(); |
| 190 } |
| 191 |
| 192 // Test empty string page update |
| 193 { |
| 194 std::vector<scoped_refptr<ArticleDistillationUpdate::RefCountedPageProto>> |
| 195 pages; |
| 196 scoped_refptr<base::RefCountedData<DistilledPageProto>> page_proto = |
| 197 new base::RefCountedData<DistilledPageProto>(); |
| 198 page_proto->data.set_html(""); |
| 199 pages.push_back(page_proto); |
| 200 |
| 201 scoped_ptr<ArticleDistillationUpdate> article_update( |
| 202 new ArticleDistillationUpdate(pages, false, false)); |
| 203 |
| 204 handle.OnArticleUpdated(*article_update.get()); |
| 205 |
| 206 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(no_content)); |
| 207 EXPECT_THAT(handle.GetJavaScriptBuffer(), Not(HasSubstr(valid_content))); |
| 208 handle.ClearJavaScriptBuffer(); |
| 209 } |
| 210 } |
| 211 |
| 212 TEST_F(DomDistillerRequestViewTest, TestLoadingIndicator) { |
| 213 const std::string no_content = |
| 214 l10n_util::GetStringUTF8(IDS_DOM_DISTILLER_VIEWER_NO_DATA_CONTENT); |
| 215 // Showing the indicator does mean passing 'false' as the parameter. |
| 216 const std::string show_loader = "showLoadingIndicator(false);"; |
| 217 |
| 218 TestRequestViewHandle handle(distilled_page_prefs_.get()); |
| 219 handle.TakeViewerHandle(NULL); |
| 220 |
| 221 // The loading indicator should show before any content is displayed. |
| 222 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(show_loader)); |
| 223 handle.ClearJavaScriptBuffer(); |
| 224 |
| 225 std::vector<scoped_refptr<ArticleDistillationUpdate::RefCountedPageProto>> |
| 226 pages; |
| 227 scoped_refptr<base::RefCountedData<DistilledPageProto>> page_proto = |
| 228 new base::RefCountedData<DistilledPageProto>(); |
| 229 pages.push_back(page_proto); |
| 230 |
| 231 scoped_ptr<ArticleDistillationUpdate> article_update( |
| 232 new ArticleDistillationUpdate(pages, true, false)); |
| 233 |
| 234 handle.OnArticleUpdated(*article_update.get()); |
| 235 |
| 236 EXPECT_THAT(handle.GetJavaScriptBuffer(), HasSubstr(show_loader)); |
| 237 } |
| 238 |
| 239 } // namespace dom_distiller |
OLD | NEW |