OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/path_service.h" | |
7 #include "base/run_loop.h" | |
8 #include "components/dom_distiller/content/browser/distillable_page_utils.h" | |
9 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" | |
10 #include "components/dom_distiller/core/distillable_page_detector.h" | |
11 #include "components/dom_distiller/core/page_features.h" | |
12 #include "content/public/browser/browser_context.h" | |
13 #include "content/public/browser/render_frame_host.h" | |
14 #include "content/public/browser/web_contents_observer.h" | |
15 #include "content/public/common/isolated_world_ids.h" | |
16 #include "content/public/test/content_browser_test.h" | |
17 #include "content/shell/browser/shell.h" | |
18 #include "net/test/embedded_test_server/embedded_test_server.h" | |
19 #include "ui/base/resource/resource_bundle.h" | |
20 | |
21 namespace dom_distiller { | |
22 namespace { | |
23 | |
24 const char* kArticlePath = "/og_article.html"; | |
25 const char* kNonArticlePath = "/non_og_article.html"; | |
26 | |
27 class DomDistillerDistillablePageUtilsTest : public content::ContentBrowserTest, | |
28 content::WebContentsObserver { | |
29 public: | |
30 void SetUpOnMainThread() override { | |
31 if (!DistillerJavaScriptWorldIdIsSet()) { | |
32 SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END); | |
33 } | |
34 AddComponentsResources(); | |
35 SetUpTestServer(); | |
36 ContentBrowserTest::SetUpOnMainThread(); | |
37 } | |
38 | |
39 void LoadURL(const std::string& url) { | |
40 content::WebContents* current_web_contents = shell()->web_contents(); | |
41 Observe(current_web_contents); | |
42 base::RunLoop url_loaded_runner; | |
43 main_frame_loaded_callback_ = url_loaded_runner.QuitClosure(); | |
44 current_web_contents->GetController().LoadURL( | |
45 embedded_test_server()->GetURL(url), | |
46 content::Referrer(), | |
47 ui::PAGE_TRANSITION_TYPED, | |
48 std::string()); | |
49 url_loaded_runner.Run(); | |
50 main_frame_loaded_callback_ = base::Closure(); | |
51 Observe(nullptr); | |
52 } | |
53 | |
54 private: | |
55 void AddComponentsResources() { | |
56 base::FilePath pak_file; | |
57 base::FilePath pak_dir; | |
58 #if defined(OS_ANDROID) | |
59 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir)); | |
60 pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks")); | |
61 #else | |
62 PathService::Get(base::DIR_MODULE, &pak_dir); | |
63 #endif // OS_ANDROID | |
64 pak_file = | |
65 pak_dir.Append(FILE_PATH_LITERAL("components_tests_resources.pak")); | |
66 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( | |
67 pak_file, ui::SCALE_FACTOR_NONE); | |
68 } | |
69 | |
70 void SetUpTestServer() { | |
71 base::FilePath path; | |
72 PathService::Get(base::DIR_SOURCE_ROOT, &path); | |
73 path = path.AppendASCII("components/test/data/dom_distiller"); | |
74 embedded_test_server()->ServeFilesFromDirectory(path); | |
75 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
76 } | |
77 | |
78 void DocumentLoadedInFrame( | |
79 content::RenderFrameHost* render_frame_host) override { | |
80 if (!render_frame_host->GetParent()) | |
81 main_frame_loaded_callback_.Run(); | |
82 } | |
83 | |
84 base::Closure main_frame_loaded_callback_; | |
85 }; | |
86 | |
87 class ResultHolder { | |
88 public: | |
89 ResultHolder(base::Closure callback) : callback_(callback) {} | |
90 | |
91 void OnResult(bool result) { | |
92 result_ = result; | |
93 callback_.Run(); | |
94 } | |
95 | |
96 bool GetResult() { | |
97 return result_; | |
98 } | |
99 | |
100 base::Callback<void(bool)> GetCallback() { | |
101 return base::Bind(&ResultHolder::OnResult, base::Unretained(this)); | |
102 } | |
103 | |
104 private: | |
105 base::Closure callback_; | |
106 bool result_; | |
107 }; | |
108 | |
109 } // namespace | |
110 | |
111 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, TestIsOGArticle) { | |
112 LoadURL(kArticlePath); | |
113 base::RunLoop run_loop_; | |
114 ResultHolder holder(run_loop_.QuitClosure()); | |
115 IsOpenGraphArticle(shell()->web_contents(), holder.GetCallback()); | |
116 run_loop_.Run(); | |
117 ASSERT_TRUE(holder.GetResult()); | |
118 } | |
119 | |
120 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, | |
121 TestIsNotOGArticle) { | |
122 LoadURL(kNonArticlePath); | |
123 base::RunLoop run_loop_; | |
124 ResultHolder holder(run_loop_.QuitClosure()); | |
125 IsOpenGraphArticle(shell()->web_contents(), holder.GetCallback()); | |
126 run_loop_.Run(); | |
127 ASSERT_FALSE(holder.GetResult()); | |
128 } | |
129 | |
130 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, | |
131 TestIsDistillablePage) { | |
132 scoped_ptr<AdaBoostProto> proto(new AdaBoostProto); | |
133 proto->set_num_features(kDerivedFeaturesCount); | |
134 proto->set_num_stumps(1); | |
135 | |
136 StumpProto* stump = proto->add_stump(); | |
137 stump->set_feature_number(0); | |
138 stump->set_weight(1); | |
139 stump->set_split(-1); | |
140 scoped_ptr<DistillablePageDetector> detector( | |
141 new DistillablePageDetector(proto.Pass())); | |
142 EXPECT_DOUBLE_EQ(0.5, detector->GetThreshold()); | |
143 // The first value of the first feature is either 0 or 1. Since the stump's | |
144 // split is -1, the stump weight will be applied to any set of derived | |
145 // features. | |
146 LoadURL(kArticlePath); | |
147 base::RunLoop run_loop_; | |
148 ResultHolder holder(run_loop_.QuitClosure()); | |
149 IsDistillablePageForDetector(shell()->web_contents(), detector.get(), | |
150 holder.GetCallback()); | |
151 run_loop_.Run(); | |
152 ASSERT_TRUE(holder.GetResult()); | |
153 } | |
154 | |
155 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, | |
156 TestIsNotDistillablePage) { | |
157 scoped_ptr<AdaBoostProto> proto(new AdaBoostProto); | |
158 proto->set_num_features(kDerivedFeaturesCount); | |
159 proto->set_num_stumps(1); | |
160 StumpProto* stump = proto->add_stump(); | |
161 stump->set_feature_number(0); | |
162 stump->set_weight(-1); | |
163 stump->set_split(-1); | |
164 scoped_ptr<DistillablePageDetector> detector( | |
165 new DistillablePageDetector(proto.Pass())); | |
166 EXPECT_DOUBLE_EQ(-0.5, detector->GetThreshold()); | |
167 // The first value of the first feature is either 0 or 1. Since the stump's | |
168 // split is -1, the stump weight will be applied to any set of derived | |
169 // features. | |
170 LoadURL(kArticlePath); | |
171 base::RunLoop run_loop_; | |
172 ResultHolder holder(run_loop_.QuitClosure()); | |
173 IsDistillablePageForDetector(shell()->web_contents(), detector.get(), | |
174 holder.GetCallback()); | |
175 run_loop_.Run(); | |
176 ASSERT_FALSE(holder.GetResult()); | |
177 } | |
178 | |
179 } // namespace dom_distiller | |
OLD | NEW |