Chromium Code Reviews| Index: components/dom_distiller/content/distiller_page_web_contents_browsertest.cc |
| diff --git a/components/dom_distiller/content/distiller_page_web_contents_browsertest.cc b/components/dom_distiller/content/distiller_page_web_contents_browsertest.cc |
| index 4c72eb739f6a72d621570d6fbb4dbb95e0ca16f5..9e52499b5684cc548d7afe81a4d265fdc48e0a39 100644 |
| --- a/components/dom_distiller/content/distiller_page_web_contents_browsertest.cc |
| +++ b/components/dom_distiller/content/distiller_page_web_contents_browsertest.cc |
| @@ -3,6 +3,8 @@ |
| // found in the LICENSE file. |
| #include "base/memory/weak_ptr.h" |
| +#include "base/base_paths.h" |
| +#include "base/path_service.h" |
| #include "base/run_loop.h" |
| #include "base/values.h" |
| #include "components/dom_distiller/content/distiller_page_web_contents.h" |
| @@ -10,28 +12,15 @@ |
| #include "content/public/browser/browser_context.h" |
| #include "content/shell/browser/shell.h" |
| #include "content/test/content_browser_test.h" |
| +#include "grit/component_resources.h" |
| #include "net/test/embedded_test_server/embedded_test_server.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| using content::ContentBrowserTest; |
| - |
| -namespace { |
| - // TODO(bengr): Once JavaScript has landed to extract article content from |
| - // a loaded page, test the interaction of that script with |
| - // DistillerPageWebContents. |
| - static const char kTitle[] = "Test Page Title"; |
| - static const char kHtml[] = |
| - "<body>T<img src='http://t.com/t.jpg' id='0'></body>"; |
| - static const char kImageUrl[] = "http://t.com/t.jpg"; |
| - |
| - static const char kScript[] = |
| - " (function () {" |
| - " var result = new Array(3);" |
| - " result[0] = \"Test Page Title\";" |
| - " result[1] = \"<body>T<img src='http://t.com/t.jpg' id='0'></body>\";" |
| - " result[2] = \"http://t.com/t.jpg\";" |
| - " return result;" |
| - " }())"; |
| -} // namespace |
| +using testing::ContainsRegex; |
| +using testing::HasSubstr; |
| +using testing::Not; |
| namespace dom_distiller { |
| @@ -39,14 +28,23 @@ class DistillerPageWebContentsTest |
| : public ContentBrowserTest, |
| public DistillerPage::Delegate { |
| public: |
| + // ContentBrowserTest: |
| + virtual void SetUpOnMainThread() OVERRIDE { |
| + AddComponentsResources(); |
| + SetUpTestServer(); |
| + ContentBrowserTest::SetUpOnMainThread(); |
| + } |
| + |
| void DistillPage(const base::Closure& quit_closure, const std::string& url) { |
| quit_closure_ = quit_closure; |
| - distiller_page_->LoadURL( |
| - embedded_test_server()->GetURL(url)); |
| + distiller_page_->LoadURL(embedded_test_server()->GetURL(url)); |
| } |
| virtual void OnLoadURLDone() OVERRIDE { |
| - distiller_page_->ExecuteJavaScript(kScript); |
| + std::string script = ResourceBundle::GetSharedInstance() |
| + .GetRawDataResource(IDR_DISTILLER_JS) |
| + .as_string(); |
|
kuan
2014/03/26 13:07:48
i think according to styling guidelines, '.' shoul
Yaron
2014/03/26 17:31:50
Done.
|
| + distiller_page_->ExecuteJavaScript(script); |
| } |
| virtual void OnExecuteJavaScriptDone(const GURL& page_url, |
| @@ -55,35 +53,101 @@ class DistillerPageWebContentsTest |
| quit_closure_.Run(); |
| } |
| + private: |
| + void AddComponentsResources() { |
| + base::FilePath pak_file; |
| + base::FilePath pak_dir; |
| + PathService::Get(base::DIR_MODULE, &pak_dir); |
| + pak_file = pak_dir.Append(FILE_PATH_LITERAL("components_resources.pak")); |
| + ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
| + pak_file, ui::SCALE_FACTOR_NONE); |
| + } |
| + |
| + void SetUpTestServer() { |
| + base::FilePath path; |
| + PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| + path = path.AppendASCII("components/test/data/dom_distiller"); |
| + embedded_test_server()->ServeFilesFromDirectory(path); |
| + ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| + } |
| + |
| protected: |
| DistillerPageWebContents* distiller_page_; |
| base::Closure quit_closure_; |
| const base::Value* value_; |
| }; |
| -IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, LoadPage) { |
| - ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| +IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, BasicDistillationWorks) { |
| base::WeakPtrFactory<DistillerPage::Delegate> weak_factory(this); |
| DistillerPageWebContents distiller_page( |
| weak_factory.GetWeakPtr(), shell()->web_contents()->GetBrowserContext()); |
| distiller_page_ = &distiller_page; |
| distiller_page.Init(); |
| + |
| base::RunLoop run_loop; |
| - DistillPage(run_loop.QuitClosure(), "/simple_page.html"); |
| + DistillPage(run_loop.QuitClosure(), "/simple_article.html"); |
| run_loop.Run(); |
| const base::ListValue* result_list = NULL; |
| ASSERT_TRUE(value_->GetAsList(&result_list)); |
| - ASSERT_EQ(3u, result_list->GetSize()); |
| + ASSERT_EQ(4u, result_list->GetSize()); |
| std::string title; |
| result_list->GetString(0, &title); |
| - ASSERT_EQ(kTitle, title); |
| + EXPECT_EQ("Test Page Title", title); |
| + std::string html; |
| + result_list->GetString(1, &html); |
| + EXPECT_THAT(html, HasSubstr("Lorem ipsum")); |
| + EXPECT_THAT(html, Not(HasSubstr("questionable content"))); |
| + std::string next_page_url; |
| + result_list->GetString(2, &next_page_url); |
| + EXPECT_EQ("", next_page_url); |
| + std::string unused; |
| + result_list->GetString(3, &unused); |
| + EXPECT_EQ("", unused); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, HandlesRelativeLinks) { |
| + base::WeakPtrFactory<DistillerPage::Delegate> weak_factory(this); |
| + DistillerPageWebContents distiller_page( |
| + weak_factory.GetWeakPtr(), shell()->web_contents()->GetBrowserContext()); |
| + distiller_page_ = &distiller_page; |
| + distiller_page.Init(); |
| + |
| + base::RunLoop run_loop; |
| + DistillPage(run_loop.QuitClosure(), "/simple_article.html"); |
| + run_loop.Run(); |
| + |
| + const base::ListValue* result_list = NULL; |
| + ASSERT_TRUE(value_->GetAsList(&result_list)); |
| + std::string html; |
| + result_list->GetString(1, &html); |
| + // A relative link should've been updated. |
| + EXPECT_THAT(html, |
| + ContainsRegex("href=\"http://127.0.0.1:.*/relativelink.html\"")); |
| + EXPECT_THAT(html, |
| + HasSubstr("href=\"http://www.google.com/absolutelink.html\"")); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, HandlesRelativeImages) { |
| + base::WeakPtrFactory<DistillerPage::Delegate> weak_factory(this); |
| + DistillerPageWebContents distiller_page( |
| + weak_factory.GetWeakPtr(), shell()->web_contents()->GetBrowserContext()); |
| + distiller_page_ = &distiller_page; |
| + distiller_page.Init(); |
| + |
| + base::RunLoop run_loop; |
| + DistillPage(run_loop.QuitClosure(), "/simple_article.html"); |
| + run_loop.Run(); |
| + |
| + const base::ListValue* result_list = NULL; |
| + ASSERT_TRUE(value_->GetAsList(&result_list)); |
| std::string html; |
| result_list->GetString(1, &html); |
| - ASSERT_EQ(kHtml, html); |
| - std::string image_url; |
| - result_list->GetString(2, &image_url); |
| - ASSERT_EQ(kImageUrl, image_url); |
| + // A relative link should've been updated. |
| + EXPECT_THAT(html, |
| + ContainsRegex("src=\"http://127.0.0.1:.*/relativeimage.png\"")); |
| + EXPECT_THAT(html, |
| + HasSubstr("src=\"http://www.google.com/absoluteimage.png\"")); |
| } |
| } // namespace dom_distiller |