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/run_loop.h" | 7 #include "base/run_loop.h" |
7 #include "base/values.h" | 8 #include "base/values.h" |
8 #include "components/dom_distiller/content/distiller_page_web_contents.h" | 9 #include "components/dom_distiller/content/distiller_page_web_contents.h" |
9 #include "components/dom_distiller/core/distiller_page.h" | 10 #include "components/dom_distiller/core/distiller_page.h" |
10 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
11 #include "content/shell/browser/shell.h" | 12 #include "content/shell/browser/shell.h" |
12 #include "content/test/content_browser_test.h" | 13 #include "content/test/content_browser_test.h" |
14 #include "grit/component_resources.h" | |
13 #include "net/test/embedded_test_server/embedded_test_server.h" | 15 #include "net/test/embedded_test_server/embedded_test_server.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "ui/base/resource/resource_bundle.h" | |
14 | 18 |
15 using content::ContentBrowserTest; | 19 using content::ContentBrowserTest; |
16 | 20 using testing::ContainsRegex; |
17 namespace { | 21 using testing::HasSubstr; |
18 // TODO(bengr): Once JavaScript has landed to extract article content from | 22 using testing::Not; |
19 // a loaded page, test the interaction of that script with | |
20 // DistillerPageWebContents. | |
21 static const char kTitle[] = "Test Page Title"; | |
22 static const char kHtml[] = | |
23 "<body>T<img src='http://t.com/t.jpg' id='0'></body>"; | |
24 static const char kImageUrl[] = "http://t.com/t.jpg"; | |
25 | |
26 static const char kScript[] = | |
27 " (function () {" | |
28 " var result = new Array(3);" | |
29 " result[0] = \"Test Page Title\";" | |
30 " result[1] = \"<body>T<img src='http://t.com/t.jpg' id='0'></body>\";" | |
31 " result[2] = \"http://t.com/t.jpg\";" | |
32 " return result;" | |
33 " }())"; | |
34 } // namespace | |
35 | 23 |
36 namespace dom_distiller { | 24 namespace dom_distiller { |
37 | 25 |
38 class DistillerPageWebContentsTest | 26 class DistillerPageWebContentsTest |
39 : public ContentBrowserTest, | 27 : public ContentBrowserTest, |
40 public DistillerPage::Delegate { | 28 public DistillerPage::Delegate { |
41 public: | 29 public: |
30 // ContentBrowserTest: | |
31 virtual void SetUpOnMainThread() OVERRIDE { | |
32 AddComponentsResources(); | |
33 SetUpTestServer(); | |
34 ContentBrowserTest::SetUpOnMainThread(); | |
35 } | |
36 | |
42 void DistillPage(const base::Closure& quit_closure, const std::string& url) { | 37 void DistillPage(const base::Closure& quit_closure, const std::string& url) { |
43 quit_closure_ = quit_closure; | 38 quit_closure_ = quit_closure; |
44 distiller_page_->LoadURL( | 39 distiller_page_->LoadURL(embedded_test_server()->GetURL(url)); |
45 embedded_test_server()->GetURL(url)); | |
46 } | 40 } |
47 | 41 |
48 virtual void OnLoadURLDone() OVERRIDE { | 42 virtual void OnLoadURLDone() OVERRIDE { |
49 distiller_page_->ExecuteJavaScript(kScript); | 43 std::string script = ResourceBundle::GetSharedInstance(). |
44 GetRawDataResource(IDR_DISTILLER_JS).as_string(); | |
45 distiller_page_->ExecuteJavaScript(script); | |
50 } | 46 } |
51 | 47 |
52 virtual void OnExecuteJavaScriptDone(const GURL& page_url, | 48 virtual void OnExecuteJavaScriptDone(const GURL& page_url, |
53 const base::Value* value) OVERRIDE { | 49 const base::Value* value) OVERRIDE { |
54 value_ = value->DeepCopy(); | 50 value_ = value->DeepCopy(); |
55 quit_closure_.Run(); | 51 quit_closure_.Run(); |
56 } | 52 } |
57 | 53 |
54 private: | |
55 void AddComponentsResources() { | |
56 base::FilePath pak_file; | |
57 base::FilePath pak_dir; | |
58 PathService::Get(base::DIR_MODULE, &pak_dir); | |
59 pak_file = pak_dir.Append(FILE_PATH_LITERAL("components_resources.pak")); | |
60 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( | |
61 pak_file, ui::SCALE_FACTOR_NONE); | |
62 } | |
63 | |
64 void SetUpTestServer() { | |
65 base::FilePath path; | |
66 PathService::Get(base::DIR_SOURCE_ROOT, &path); | |
67 path = path.AppendASCII("components/test/data/dom_distiller"); | |
68 embedded_test_server()->ServeFilesFromDirectory(path); | |
69 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
70 } | |
71 | |
58 protected: | 72 protected: |
59 DistillerPageWebContents* distiller_page_; | 73 DistillerPageWebContents* distiller_page_; |
60 base::Closure quit_closure_; | 74 base::Closure quit_closure_; |
61 const base::Value* value_; | 75 const base::Value* value_; |
62 }; | 76 }; |
63 | 77 |
64 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, LoadPage) { | 78 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, BasicDistillationWorks) { |
65 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
66 base::WeakPtrFactory<DistillerPage::Delegate> weak_factory(this); | 79 base::WeakPtrFactory<DistillerPage::Delegate> weak_factory(this); |
67 DistillerPageWebContents distiller_page( | 80 DistillerPageWebContents distiller_page( |
68 weak_factory.GetWeakPtr(), shell()->web_contents()->GetBrowserContext()); | 81 weak_factory.GetWeakPtr(), shell()->web_contents()->GetBrowserContext()); |
69 distiller_page_ = &distiller_page; | 82 distiller_page_ = &distiller_page; |
70 distiller_page.Init(); | 83 distiller_page.Init(); |
84 | |
71 base::RunLoop run_loop; | 85 base::RunLoop run_loop; |
72 DistillPage(run_loop.QuitClosure(), "/simple_page.html"); | 86 DistillPage(run_loop.QuitClosure(), "/simple_article.html"); |
73 run_loop.Run(); | 87 run_loop.Run(); |
74 | 88 |
75 const base::ListValue* result_list = NULL; | 89 const base::ListValue* result_list = NULL; |
76 ASSERT_TRUE(value_->GetAsList(&result_list)); | 90 ASSERT_TRUE(value_->GetAsList(&result_list)); |
77 ASSERT_EQ(3u, result_list->GetSize()); | 91 ASSERT_EQ(4u, result_list->GetSize()); |
78 std::string title; | 92 std::string title; |
79 result_list->GetString(0, &title); | 93 result_list->GetString(0, &title); |
80 ASSERT_EQ(kTitle, title); | 94 EXPECT_EQ("Test Page Title", title); |
81 std::string html; | 95 std::string html; |
82 result_list->GetString(1, &html); | 96 result_list->GetString(1, &html); |
83 ASSERT_EQ(kHtml, html); | 97 EXPECT_THAT(html, HasSubstr("Lorem ipsum")); |
84 std::string image_url; | 98 EXPECT_THAT(html, Not(HasSubstr("questionable content"))); |
85 result_list->GetString(2, &image_url); | 99 std::string next_page_url; |
86 ASSERT_EQ(kImageUrl, image_url); | 100 result_list->GetString(2, &next_page_url); |
101 EXPECT_EQ("", next_page_url); | |
102 std::string unused; | |
103 result_list->GetString(3, &unused); | |
104 EXPECT_EQ("", unused); | |
105 } | |
106 | |
107 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, HandlesRelativeLinks) { | |
108 base::WeakPtrFactory<DistillerPage::Delegate> weak_factory(this); | |
109 DistillerPageWebContents distiller_page( | |
110 weak_factory.GetWeakPtr(), shell()->web_contents()->GetBrowserContext()); | |
111 distiller_page_ = &distiller_page; | |
112 distiller_page.Init(); | |
113 | |
114 base::RunLoop run_loop; | |
115 DistillPage(run_loop.QuitClosure(), "/simple_article.html"); | |
116 run_loop.Run(); | |
117 | |
118 const base::ListValue* result_list = NULL; | |
119 ASSERT_TRUE(value_->GetAsList(&result_list)); | |
120 std::string html; | |
121 result_list->GetString(1, &html); | |
122 // A relative link should've been updated. | |
123 EXPECT_THAT(html, | |
124 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.
| |
125 EXPECT_THAT(html, | |
126 HasSubstr("href=\"http://www.google.com/absolutelink.html\"")); | |
127 } | |
128 | |
129 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, HandlesRelativeImages) { | |
130 base::WeakPtrFactory<DistillerPage::Delegate> weak_factory(this); | |
131 DistillerPageWebContents distiller_page( | |
132 weak_factory.GetWeakPtr(), shell()->web_contents()->GetBrowserContext()); | |
133 distiller_page_ = &distiller_page; | |
134 distiller_page.Init(); | |
135 | |
136 base::RunLoop run_loop; | |
137 DistillPage(run_loop.QuitClosure(), "/simple_article.html"); | |
138 run_loop.Run(); | |
139 | |
140 const base::ListValue* result_list = NULL; | |
141 ASSERT_TRUE(value_->GetAsList(&result_list)); | |
142 std::string html; | |
143 result_list->GetString(1, &html); | |
144 // A relative link should've been updated. | |
145 EXPECT_THAT(html, | |
146 ContainsRegex("src=\"http://127.0.0.1:.*/relativeimage.png\"")); | |
147 EXPECT_THAT(html, | |
148 HasSubstr("src=\"http://www.google.com/absoluteimage.png\"")); | |
87 } | 149 } |
88 | 150 |
89 } // namespace dom_distiller | 151 } // namespace dom_distiller |
OLD | NEW |