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..2cf9258cbf6cf5592d67aa40f325250a6df837d1 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,7 @@ |
// found in the LICENSE file. |
#include "base/memory/weak_ptr.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 +11,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 "testing/gmock/include/gmock/gmock.h" |
+#include "ui/base/resource/resource_bundle.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 +27,22 @@ 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(); |
+ distiller_page_->ExecuteJavaScript(script); |
} |
virtual void OnExecuteJavaScriptDone(const GURL& page_url, |
@@ -55,35 +51,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\"")); |
cjhopman
2014/03/27 00:22:39
Can we make the ".*" a bit stricter (like [0-9]*)?
Yaron
2014/04/02 17:11:59
Done.
|
+ 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 |