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