OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <string.h> | 5 #include <string.h> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/guid.h" | |
8 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" | 9 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" |
9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
14 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
16 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" | 17 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" |
17 #include "components/dom_distiller/core/distiller.h" | 18 #include "components/dom_distiller/core/distiller.h" |
18 #include "components/dom_distiller/core/dom_distiller_service.h" | 19 #include "components/dom_distiller/core/dom_distiller_service.h" |
19 #include "components/dom_distiller/core/dom_distiller_store.h" | 20 #include "components/dom_distiller/core/dom_distiller_store.h" |
20 #include "components/dom_distiller/core/dom_distiller_test_util.h" | 21 #include "components/dom_distiller/core/dom_distiller_test_util.h" |
21 #include "components/dom_distiller/core/fake_db.h" | 22 #include "components/dom_distiller/core/fake_db.h" |
22 #include "components/dom_distiller/core/fake_distiller.h" | 23 #include "components/dom_distiller/core/fake_distiller.h" |
23 #include "components/dom_distiller/core/task_tracker.h" | 24 #include "components/dom_distiller/core/task_tracker.h" |
25 #include "components/dom_distiller/core/url_constants.h" | |
shashi
2014/02/27 04:29:08
nit: is this import needed?
shashi
2014/02/27 04:29:08
nit: is this import needed?
nyquist
2014/02/27 19:50:19
Done.
| |
26 #include "components/dom_distiller/core/url_utils.h" | |
24 #include "content/public/browser/render_view_host.h" | 27 #include "content/public/browser/render_view_host.h" |
25 #include "content/public/browser/url_data_source.h" | 28 #include "content/public/browser/url_data_source.h" |
26 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
27 #include "content/public/browser/web_contents_observer.h" | 30 #include "content/public/browser/web_contents_observer.h" |
31 #include "net/base/escape.h" | |
shashi
2014/02/27 04:29:08
nit: is this import needed?
nyquist
2014/02/27 19:50:19
Done.
| |
32 #include "net/base/url_util.h" | |
shashi
2014/02/27 04:29:08
nit: is this import needed?
nyquist
2014/02/27 19:50:19
Done.
| |
28 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
29 | 34 |
30 namespace dom_distiller { | 35 namespace dom_distiller { |
31 | 36 |
32 using test::FakeDB; | 37 using test::FakeDB; |
33 using test::FakeDistiller; | 38 using test::FakeDistiller; |
34 using test::MockDistillerFactory; | 39 using test::MockDistillerFactory; |
35 using test::util::CreateStoreWithFakeDB; | 40 using test::util::CreateStoreWithFakeDB; |
36 | 41 |
37 namespace { | 42 namespace { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 if (expect_distillation_) { | 136 if (expect_distillation_) { |
132 // There will only be destillation of an article if the database contains | 137 // There will only be destillation of an article if the database contains |
133 // the article. | 138 // the article. |
134 FakeDistiller* distiller = new FakeDistiller(true); | 139 FakeDistiller* distiller = new FakeDistiller(true); |
135 EXPECT_CALL(*factory, CreateDistillerImpl()) | 140 EXPECT_CALL(*factory, CreateDistillerImpl()) |
136 .WillOnce(testing::Return(distiller)); | 141 .WillOnce(testing::Return(distiller)); |
137 } | 142 } |
138 return service; | 143 return service; |
139 } | 144 } |
140 | 145 |
141 void ViewSingleDistilledPage(); | 146 void ViewSingleDistilledPage(const GURL& url); |
142 | 147 |
143 // Database entries. | 148 // Database entries. |
144 static FakeDB::EntryMap* database_model_; | 149 static FakeDB::EntryMap* database_model_; |
145 static bool expect_distillation_; | 150 static bool expect_distillation_; |
146 }; | 151 }; |
147 | 152 |
148 FakeDB::EntryMap* DomDistillerViewerSourceBrowserTest::database_model_; | 153 FakeDB::EntryMap* DomDistillerViewerSourceBrowserTest::database_model_; |
149 bool DomDistillerViewerSourceBrowserTest::expect_distillation_ = false; | 154 bool DomDistillerViewerSourceBrowserTest::expect_distillation_ = false; |
150 | 155 |
151 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings | 156 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings |
152 // are enabled when the article exists in the database. | 157 // are enabled when the article exists in the database. |
153 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, | 158 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, |
154 NoWebUIBindingsArticleExists) { | 159 NoWebUIBindingsArticleExists) { |
155 // Ensure there is one item in the database, which will trigger distillation. | 160 // Ensure there is one item in the database, which will trigger distillation. |
156 ArticleEntry entry = CreateEntry("DISTILLED", "http://example.com/1"); | 161 const ArticleEntry entry = CreateEntry("DISTILLED", "http://example.com/1"); |
157 AddEntry(entry, database_model_); | 162 AddEntry(entry, database_model_); |
158 expect_distillation_ = true; | 163 expect_distillation_ = true; |
159 ViewSingleDistilledPage(); | 164 const GURL url = url_utils::GetDistillerViewUrlFromEntryId( |
165 chrome::kDomDistillerScheme, entry.entry_id()); | |
166 ViewSingleDistilledPage(url); | |
160 } | 167 } |
161 | 168 |
162 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings | 169 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings |
163 // are enabled when the article is not found. | 170 // are enabled when the article is not found. |
164 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, | 171 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, |
165 NoWebUIBindingsArticleNotFound) { | 172 NoWebUIBindingsArticleNotFound) { |
166 // The article does not exist, so assume no distillation will happen. | 173 // The article does not exist, so assume no distillation will happen. |
167 expect_distillation_ = false; | 174 expect_distillation_ = false; |
168 ViewSingleDistilledPage(); | 175 const GURL url(std::string(chrome::kDomDistillerScheme) + "://" + |
176 base::GenerateGUID() + "/"); | |
177 ViewSingleDistilledPage(url); | |
169 } | 178 } |
170 | 179 |
171 void DomDistillerViewerSourceBrowserTest::ViewSingleDistilledPage() { | 180 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings |
181 // are enabled when requesting to view an arbitrary URL. | |
182 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, | |
183 NoWebUIBindingsViewUrl) { | |
184 // We should expect distillation for any valid URL. | |
185 expect_distillation_ = true; | |
186 GURL view_url("http://www.example.com/1"); | |
187 const GURL url = url_utils::GetDistillerViewUrlFromUrl( | |
188 chrome::kDomDistillerScheme, view_url); | |
189 ViewSingleDistilledPage(url); | |
190 } | |
191 | |
192 void DomDistillerViewerSourceBrowserTest::ViewSingleDistilledPage( | |
193 const GURL& url) { | |
172 // Create the service. | 194 // Create the service. |
173 DomDistillerContextKeyedService* service = | 195 DomDistillerContextKeyedService* service = |
174 static_cast<DomDistillerContextKeyedService*>( | 196 static_cast<DomDistillerContextKeyedService*>( |
175 dom_distiller::DomDistillerServiceFactory::GetInstance() | 197 dom_distiller::DomDistillerServiceFactory::GetInstance() |
176 ->SetTestingFactoryAndUse(browser()->profile(), &Build)); | 198 ->SetTestingFactoryAndUse(browser()->profile(), &Build)); |
177 | 199 |
178 // Ensure the source is registered. | 200 // Ensure the source is registered. |
179 // TODO(nyquist): Remove when the source is always registered on startup. | 201 // TODO(nyquist): Remove when the source is always registered on startup. |
180 DomDistillerViewerSource* source = | 202 DomDistillerViewerSource* source = |
181 new DomDistillerViewerSource(service, chrome::kDomDistillerScheme); | 203 new DomDistillerViewerSource(service, chrome::kDomDistillerScheme); |
182 content::URLDataSource::Add(browser()->profile(), source); | 204 content::URLDataSource::Add(browser()->profile(), source); |
183 | 205 |
184 // Setup observer to inspect the RenderViewHost after committed navigation. | 206 // Setup observer to inspect the RenderViewHost after committed navigation. |
185 content::WebContents* contents = | 207 content::WebContents* contents = |
186 browser()->tab_strip_model()->GetActiveWebContents(); | 208 browser()->tab_strip_model()->GetActiveWebContents(); |
187 LoadSuccessObserver observer(contents); | 209 LoadSuccessObserver observer(contents); |
188 | 210 |
189 // Navigate to a URL which the source should respond to. | 211 // Navigate to a URL which the source should respond to. |
190 std::string url_without_scheme = "://distilled"; | |
191 GURL url(chrome::kDomDistillerScheme + url_without_scheme); | |
192 ui_test_utils::NavigateToURL(browser(), url); | 212 ui_test_utils::NavigateToURL(browser(), url); |
193 | 213 |
194 // A navigation should have succeeded to the correct URL. | 214 // A navigation should have succeeded to the correct URL. |
195 ASSERT_FALSE(observer.load_failed()); | 215 ASSERT_FALSE(observer.load_failed()); |
196 ASSERT_TRUE(observer.finished_load()); | 216 ASSERT_TRUE(observer.finished_load()); |
197 ASSERT_EQ(url, observer.validated_url()); | 217 ASSERT_EQ(url, observer.validated_url()); |
198 // Ensure no bindings. | 218 // Ensure no bindings. |
199 const content::RenderViewHost* render_view_host = observer.render_view_host(); | 219 const content::RenderViewHost* render_view_host = observer.render_view_host(); |
200 ASSERT_EQ(0, render_view_host->GetEnabledBindings()); | 220 ASSERT_EQ(0, render_view_host->GetEnabledBindings()); |
201 // The MIME-type should always be text/html for the distilled articles. | 221 // The MIME-type should always be text/html for the distilled articles. |
(...skipping 26 matching lines...) Expand all Loading... | |
228 ASSERT_TRUE(observer.finished_load()); | 248 ASSERT_TRUE(observer.finished_load()); |
229 ASSERT_EQ(url, observer.validated_url()); | 249 ASSERT_EQ(url, observer.validated_url()); |
230 // Ensure no bindings. | 250 // Ensure no bindings. |
231 const content::RenderViewHost* render_view_host = observer.render_view_host(); | 251 const content::RenderViewHost* render_view_host = observer.render_view_host(); |
232 ASSERT_EQ(0, render_view_host->GetEnabledBindings()); | 252 ASSERT_EQ(0, render_view_host->GetEnabledBindings()); |
233 // The MIME-type should always be text/css for the CSS resources. | 253 // The MIME-type should always be text/css for the CSS resources. |
234 EXPECT_EQ("text/css", observer.web_contents()->GetContentsMimeType()); | 254 EXPECT_EQ("text/css", observer.web_contents()->GetContentsMimeType()); |
235 } | 255 } |
236 | 256 |
237 } // namespace dom_distiller | 257 } // namespace dom_distiller |
OLD | NEW |