Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Side by Side Diff: components/dom_distiller/content/distiller_page_web_contents_browsertest.cc

Issue 266073003: Add support for distilling current WebContents (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed compile on Windows and rebased to ensure mac build works too Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/memory/weak_ptr.h" 5 #include "base/memory/weak_ptr.h"
6 #include "base/path_service.h" 6 #include "base/path_service.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "components/dom_distiller/content/distiller_page_web_contents.h" 9 #include "components/dom_distiller/content/distiller_page_web_contents.h"
10 #include "components/dom_distiller/content/web_contents_main_frame_observer.h"
10 #include "components/dom_distiller/core/distiller_page.h" 11 #include "components/dom_distiller/core/distiller_page.h"
11 #include "content/public/browser/browser_context.h" 12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/web_contents_observer.h"
12 #include "content/public/test/content_browser_test.h" 16 #include "content/public/test/content_browser_test.h"
13 #include "content/shell/browser/shell.h" 17 #include "content/shell/browser/shell.h"
14 #include "grit/component_resources.h" 18 #include "grit/component_resources.h"
15 #include "net/test/embedded_test_server/embedded_test_server.h" 19 #include "net/test/embedded_test_server/embedded_test_server.h"
16 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
17 #include "ui/base/resource/resource_bundle.h" 21 #include "ui/base/resource/resource_bundle.h"
18 22
19 using content::ContentBrowserTest; 23 using content::ContentBrowserTest;
20 using testing::ContainsRegex; 24 using testing::ContainsRegex;
21 using testing::HasSubstr; 25 using testing::HasSubstr;
22 using testing::Not; 26 using testing::Not;
23 27
24 namespace dom_distiller { 28 namespace dom_distiller {
25 29
30 const char* kSimpleArticlePath = "/simple_article.html";
31
26 class DistillerPageWebContentsTest : public ContentBrowserTest { 32 class DistillerPageWebContentsTest : public ContentBrowserTest {
27 public: 33 public:
28 // ContentBrowserTest: 34 // ContentBrowserTest:
29 virtual void SetUpOnMainThread() OVERRIDE { 35 virtual void SetUpOnMainThread() OVERRIDE {
30 AddComponentsResources(); 36 AddComponentsResources();
31 SetUpTestServer(); 37 SetUpTestServer();
32 ContentBrowserTest::SetUpOnMainThread(); 38 ContentBrowserTest::SetUpOnMainThread();
33 } 39 }
34 40
35 void DistillPage(const base::Closure& quit_closure, const std::string& url) { 41 void DistillPage(const base::Closure& quit_closure, const std::string& url) {
(...skipping 22 matching lines...) Expand all
58 64
59 void SetUpTestServer() { 65 void SetUpTestServer() {
60 base::FilePath path; 66 base::FilePath path;
61 PathService::Get(base::DIR_SOURCE_ROOT, &path); 67 PathService::Get(base::DIR_SOURCE_ROOT, &path);
62 path = path.AppendASCII("components/test/data/dom_distiller"); 68 path = path.AppendASCII("components/test/data/dom_distiller");
63 embedded_test_server()->ServeFilesFromDirectory(path); 69 embedded_test_server()->ServeFilesFromDirectory(path);
64 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 70 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
65 } 71 }
66 72
67 protected: 73 protected:
74 void RunUseCurrentWebContentsTest(const std::string& url,
75 bool use_new_web_contents,
76 bool setup_main_frame_observer,
77 bool wait_for_document_loaded);
78
68 DistillerPageWebContents* distiller_page_; 79 DistillerPageWebContents* distiller_page_;
69 base::Closure quit_closure_; 80 base::Closure quit_closure_;
70 scoped_ptr<DistilledPageInfo> page_info_; 81 scoped_ptr<DistilledPageInfo> page_info_;
71 }; 82 };
72 83
84 // Use this class to be able to leak the WebContents, which is needed for when
85 // the current WebContents is used for distillation.
86 class TestDistillerPageWebContents : public DistillerPageWebContents {
87 public:
88 TestDistillerPageWebContents(
89 content::BrowserContext* browser_context,
90 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle,
91 bool use_new_web_contents)
92 : DistillerPageWebContents(browser_context,
93 optional_web_contents_handle.Pass()),
94 use_new_web_contents_(use_new_web_contents),
95 new_web_contents_created_(false) {}
96
97 virtual void CreateNewWebContents(const GURL& url) OVERRIDE {
98 EXPECT_EQ(true, use_new_web_contents_);
Yaron 2014/05/22 18:25:02 ASSERT_EQ? I think a lot's going to go south if yo
nyquist 2014/05/22 23:00:54 Done.
99 new_web_contents_created_ = true;
100 if (use_new_web_contents_) {
Yaron 2014/05/22 18:25:02 remove this
nyquist 2014/05/22 23:00:54 Done.
101 // DistillerPageWebContents::CreateNewWebContents resets the scoped_ptr to
102 // the WebContents, so intentionally leak WebContents here, since it is
103 // owned by the shell.
104 content::WebContents* web_contents = web_contents_.release();
105 web_contents->GetLastCommittedURL();
106 DistillerPageWebContents::CreateNewWebContents(url);
107 }
108 }
109
110 virtual ~TestDistillerPageWebContents() {
111 if (!use_new_web_contents_) {
112 // Intentionally leaking WebContents, since it is owned by the shell.
113 content::WebContents* web_contents = web_contents_.release();
114 web_contents->GetLastCommittedURL();
115 }
116 }
117
118 bool new_web_contents_created() { return new_web_contents_created_; }
119
120 private:
121 bool use_new_web_contents_;
122 bool new_web_contents_created_;
123 };
124
125 // Helper class to know how far in the loading process the current WebContents
126 // has come. It will call the callback either after
127 // DidCommitProvisionalLoadForFrame or DocumentLoadedInFrame is called for the
128 // main frame, based on the value of |wait_for_document_loaded|.
129 class WebContentsMainFrameHelper : public content::WebContentsObserver {
130 public:
131 WebContentsMainFrameHelper(content::WebContents* web_contents,
132 const base::Closure& callback,
133 bool wait_for_document_loaded)
134 : web_contents_(web_contents),
135 callback_(callback),
136 wait_for_document_loaded_(wait_for_document_loaded) {
137 content::WebContentsObserver::Observe(web_contents);
138 }
139
140 virtual void DidCommitProvisionalLoadForFrame(
141 int64 frame_id,
142 const base::string16& frame_unique_name,
143 bool is_main_frame,
144 const GURL& url,
145 content::PageTransition transition_type,
146 content::RenderViewHost* render_view_host) OVERRIDE {
147 if (wait_for_document_loaded_)
148 return;
149 if (is_main_frame)
150 callback_.Run();
151 }
152
153 virtual void DocumentLoadedInFrame(
154 int64 frame_id,
155 content::RenderViewHost* render_view_host) OVERRIDE {
156 if (wait_for_document_loaded_) {
157 if (web_contents_ &&
158 frame_id == web_contents_->GetMainFrame()->GetRoutingID()) {
159 callback_.Run();
160 }
161 }
162 }
163
164 private:
165 content::WebContents* web_contents_;
166 base::Closure callback_;
167 bool wait_for_document_loaded_;
168 };
169
73 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, BasicDistillationWorks) { 170 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, BasicDistillationWorks) {
74 DistillerPageWebContents distiller_page( 171 DistillerPageWebContents distiller_page(
75 shell()->web_contents()->GetBrowserContext()); 172 shell()->web_contents()->GetBrowserContext(),
173 scoped_ptr<SourcePageHandleWebContents>());
76 distiller_page_ = &distiller_page; 174 distiller_page_ = &distiller_page;
77 175
78 base::RunLoop run_loop; 176 base::RunLoop run_loop;
79 DistillPage(run_loop.QuitClosure(), "/simple_article.html"); 177 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath);
80 run_loop.Run(); 178 run_loop.Run();
81 179
82 EXPECT_EQ("Test Page Title", page_info_.get()->title); 180 EXPECT_EQ("Test Page Title", page_info_.get()->title);
83 EXPECT_THAT(page_info_.get()->html, HasSubstr("Lorem ipsum")); 181 EXPECT_THAT(page_info_.get()->html, HasSubstr("Lorem ipsum"));
84 EXPECT_THAT(page_info_.get()->html, Not(HasSubstr("questionable content"))); 182 EXPECT_THAT(page_info_.get()->html, Not(HasSubstr("questionable content")));
85 EXPECT_EQ("", page_info_.get()->next_page_url); 183 EXPECT_EQ("", page_info_.get()->next_page_url);
86 EXPECT_EQ("", page_info_.get()->prev_page_url); 184 EXPECT_EQ("", page_info_.get()->prev_page_url);
87 } 185 }
88 186
89 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, HandlesRelativeLinks) { 187 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, HandlesRelativeLinks) {
90 DistillerPageWebContents distiller_page( 188 DistillerPageWebContents distiller_page(
91 shell()->web_contents()->GetBrowserContext()); 189 shell()->web_contents()->GetBrowserContext(),
190 scoped_ptr<SourcePageHandleWebContents>());
92 distiller_page_ = &distiller_page; 191 distiller_page_ = &distiller_page;
93 192
94 base::RunLoop run_loop; 193 base::RunLoop run_loop;
95 DistillPage(run_loop.QuitClosure(), "/simple_article.html"); 194 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath);
96 run_loop.Run(); 195 run_loop.Run();
97 196
98 // A relative link should've been updated. 197 // A relative link should've been updated.
99 EXPECT_THAT(page_info_.get()->html, 198 EXPECT_THAT(page_info_.get()->html,
100 ContainsRegex("href=\"http://127.0.0.1:.*/relativelink.html\"")); 199 ContainsRegex("href=\"http://127.0.0.1:.*/relativelink.html\""));
101 EXPECT_THAT(page_info_.get()->html, 200 EXPECT_THAT(page_info_.get()->html,
102 HasSubstr("href=\"http://www.google.com/absolutelink.html\"")); 201 HasSubstr("href=\"http://www.google.com/absolutelink.html\""));
103 } 202 }
104 203
105 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, HandlesRelativeImages) { 204 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, HandlesRelativeImages) {
106 DistillerPageWebContents distiller_page( 205 DistillerPageWebContents distiller_page(
107 shell()->web_contents()->GetBrowserContext()); 206 shell()->web_contents()->GetBrowserContext(),
207 scoped_ptr<SourcePageHandleWebContents>());
108 distiller_page_ = &distiller_page; 208 distiller_page_ = &distiller_page;
109 209
110 base::RunLoop run_loop; 210 base::RunLoop run_loop;
111 DistillPage(run_loop.QuitClosure(), "/simple_article.html"); 211 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath);
112 run_loop.Run(); 212 run_loop.Run();
113 213
114 // A relative link should've been updated. 214 // A relative link should've been updated.
115 EXPECT_THAT(page_info_.get()->html, 215 EXPECT_THAT(page_info_.get()->html,
116 ContainsRegex("src=\"http://127.0.0.1:.*/relativeimage.png\"")); 216 ContainsRegex("src=\"http://127.0.0.1:.*/relativeimage.png\""));
117 EXPECT_THAT(page_info_.get()->html, 217 EXPECT_THAT(page_info_.get()->html,
118 HasSubstr("src=\"http://www.google.com/absoluteimage.png\"")); 218 HasSubstr("src=\"http://www.google.com/absoluteimage.png\""));
119 } 219 }
120 220
121 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, VisibilityDetection) { 221 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, VisibilityDetection) {
122 DistillerPageWebContents distiller_page( 222 DistillerPageWebContents distiller_page(
123 shell()->web_contents()->GetBrowserContext()); 223 shell()->web_contents()->GetBrowserContext(),
224 scoped_ptr<SourcePageHandleWebContents>());
124 distiller_page_ = &distiller_page; 225 distiller_page_ = &distiller_page;
125 226
126 // visble_style.html and invisible_style.html only differ by the visibility 227 // visble_style.html and invisible_style.html only differ by the visibility
127 // internal stylesheet. 228 // internal stylesheet.
128 229
129 { 230 {
130 base::RunLoop run_loop; 231 base::RunLoop run_loop;
131 DistillPage(run_loop.QuitClosure(), "/visible_style.html"); 232 DistillPage(run_loop.QuitClosure(), "/visible_style.html");
132 run_loop.Run(); 233 run_loop.Run();
133 EXPECT_THAT(page_info_.get()->html, HasSubstr("Lorem ipsum")); 234 EXPECT_THAT(page_info_.get()->html, HasSubstr("Lorem ipsum"));
134 } 235 }
135 236
136 { 237 {
137 base::RunLoop run_loop; 238 base::RunLoop run_loop;
138 DistillPage(run_loop.QuitClosure(), "/invisible_style.html"); 239 DistillPage(run_loop.QuitClosure(), "/invisible_style.html");
139 run_loop.Run(); 240 run_loop.Run();
140 EXPECT_THAT(page_info_.get()->html, Not(HasSubstr("Lorem ipsum"))); 241 EXPECT_THAT(page_info_.get()->html, Not(HasSubstr("Lorem ipsum")));
141 } 242 }
142 } 243 }
143 244
245 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest,
246 UsingCurrentWebContentsWrongUrl) {
247 std::string url("about:blank");
248 bool use_new_web_contents = true;
249 bool setup_main_frame_observer = true;
250 bool wait_for_document_loaded = true;
251 RunUseCurrentWebContentsTest(url,
252 use_new_web_contents,
253 setup_main_frame_observer,
254 wait_for_document_loaded);
255 }
256
257 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest,
258 UsingCurrentWebContentsNoMainFrameObserver) {
259 std::string url(kSimpleArticlePath);
260 bool use_new_web_contents = true;
261 bool setup_main_frame_observer = false;
262 bool wait_for_document_loaded = true;
263 RunUseCurrentWebContentsTest(url,
264 use_new_web_contents,
265 setup_main_frame_observer,
266 wait_for_document_loaded);
267 }
268
269 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest,
270 UsingCurrentWebContentsNotFinishedLoadingYet) {
271 std::string url(kSimpleArticlePath);
272 bool use_new_web_contents = false;
273 bool setup_main_frame_observer = true;
274 bool wait_for_document_loaded = false;
275 RunUseCurrentWebContentsTest(url,
276 use_new_web_contents,
277 setup_main_frame_observer,
278 wait_for_document_loaded);
279 }
280
281 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest,
282 UsingCurrentWebContentsReadyForDistillation) {
283 std::string url(kSimpleArticlePath);
284 bool use_new_web_contents = false;
285 bool setup_main_frame_observer = true;
286 bool wait_for_document_loaded = true;
287 RunUseCurrentWebContentsTest(url,
288 use_new_web_contents,
289 setup_main_frame_observer,
290 wait_for_document_loaded);
291 }
292
293 void DistillerPageWebContentsTest::RunUseCurrentWebContentsTest(
294 const std::string& url,
295 bool use_new_web_contents,
Yaron 2014/05/22 18:25:02 nit: expect_new_web_contents throughout as this is
nyquist 2014/05/22 23:00:54 Done.
296 bool setup_main_frame_observer,
297 bool wait_for_document_loaded) {
298 content::WebContents* current_web_contents = shell()->web_contents();
299 if (setup_main_frame_observer) {
300 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents(
301 current_web_contents);
302 }
303 base::RunLoop url_loaded_runner;
304 scoped_ptr<WebContentsMainFrameHelper> main_frame_loaded(
Yaron 2014/05/22 18:25:02 why scoped_ptr?
nyquist 2014/05/22 23:00:54 Done.
305 new WebContentsMainFrameHelper(current_web_contents,
306 url_loaded_runner.QuitClosure(),
307 wait_for_document_loaded));
308 current_web_contents->GetController().LoadURL(
309 embedded_test_server()->GetURL(url),
310 content::Referrer(),
311 content::PAGE_TRANSITION_TYPED,
312 std::string());
313 url_loaded_runner.Run();
314
315 scoped_ptr<content::WebContents> old_web_contents_sptr(current_web_contents);
316 scoped_ptr<SourcePageHandleWebContents> source_page_handle(
317 new SourcePageHandleWebContents(old_web_contents_sptr.Pass()));
318
319 TestDistillerPageWebContents distiller_page(
320 shell()->web_contents()->GetBrowserContext(),
321 source_page_handle.Pass(),
322 use_new_web_contents);
323 distiller_page_ = &distiller_page;
324
325 base::RunLoop run_loop;
326 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath);
327 run_loop.Run();
328
329 // Sanity check of distillation process.
330 EXPECT_EQ(use_new_web_contents, distiller_page.new_web_contents_created());
331 EXPECT_EQ("Test Page Title", page_info_.get()->title);
332 }
333
144 } // namespace dom_distiller 334 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698