OLD | NEW |
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 Loading... |
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_); |
| 99 new_web_contents_created_ = true; |
| 100 if (use_new_web_contents_) { |
| 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 |
| 221 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, |
| 222 UsingCurrentWebContentsWrongUrl) { |
| 223 std::string url("about:blank"); |
| 224 bool use_new_web_contents = true; |
| 225 bool setup_main_frame_observer = true; |
| 226 bool wait_for_document_loaded = true; |
| 227 RunUseCurrentWebContentsTest(url, |
| 228 use_new_web_contents, |
| 229 setup_main_frame_observer, |
| 230 wait_for_document_loaded); |
| 231 } |
| 232 |
| 233 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, |
| 234 UsingCurrentWebContentsNoMainFrameObserver) { |
| 235 std::string url(kSimpleArticlePath); |
| 236 bool use_new_web_contents = true; |
| 237 bool setup_main_frame_observer = false; |
| 238 bool wait_for_document_loaded = true; |
| 239 RunUseCurrentWebContentsTest(url, |
| 240 use_new_web_contents, |
| 241 setup_main_frame_observer, |
| 242 wait_for_document_loaded); |
| 243 } |
| 244 |
| 245 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, |
| 246 UsingCurrentWebContentsNotFinishedLoadingYet) { |
| 247 std::string url(kSimpleArticlePath); |
| 248 bool use_new_web_contents = false; |
| 249 bool setup_main_frame_observer = true; |
| 250 bool wait_for_document_loaded = false; |
| 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 UsingCurrentWebContentsReadyForDistillation) { |
| 259 std::string url(kSimpleArticlePath); |
| 260 bool use_new_web_contents = false; |
| 261 bool setup_main_frame_observer = true; |
| 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 void DistillerPageWebContentsTest::RunUseCurrentWebContentsTest( |
| 270 const std::string& url, |
| 271 bool use_new_web_contents, |
| 272 bool setup_main_frame_observer, |
| 273 bool wait_for_document_loaded) { |
| 274 content::WebContents* current_web_contents = shell()->web_contents(); |
| 275 if (setup_main_frame_observer) { |
| 276 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents( |
| 277 current_web_contents); |
| 278 } |
| 279 base::RunLoop url_loaded_runner; |
| 280 scoped_ptr<WebContentsMainFrameHelper> main_frame_loaded( |
| 281 new WebContentsMainFrameHelper(current_web_contents, |
| 282 url_loaded_runner.QuitClosure(), |
| 283 wait_for_document_loaded)); |
| 284 current_web_contents->GetController().LoadURL( |
| 285 embedded_test_server()->GetURL(url), |
| 286 content::Referrer(), |
| 287 content::PAGE_TRANSITION_TYPED, |
| 288 std::string()); |
| 289 url_loaded_runner.Run(); |
| 290 |
| 291 scoped_ptr<content::WebContents> old_web_contents_sptr(current_web_contents); |
| 292 scoped_ptr<SourcePageHandleWebContents> source_page_handle( |
| 293 new SourcePageHandleWebContents(old_web_contents_sptr.Pass())); |
| 294 |
| 295 TestDistillerPageWebContents distiller_page( |
| 296 shell()->web_contents()->GetBrowserContext(), |
| 297 source_page_handle.Pass(), |
| 298 use_new_web_contents); |
| 299 distiller_page_ = &distiller_page; |
| 300 |
| 301 base::RunLoop run_loop; |
| 302 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath); |
| 303 run_loop.Run(); |
| 304 |
| 305 // Sanity check of distillation process. |
| 306 EXPECT_EQ(use_new_web_contents, distiller_page.new_web_contents_created()); |
| 307 EXPECT_EQ("Test Page Title", page_info_.get()->title); |
| 308 } |
| 309 |
121 } // namespace dom_distiller | 310 } // namespace dom_distiller |
OLD | NEW |