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

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

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

Powered by Google App Engine
This is Rietveld 408576698