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.
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"
21 namespace dom_distiller
{
24 const char* kArticlePath
= "/og_article.html";
25 const char* kNonArticlePath
= "/non_og_article.html";
27 class DomDistillerDistillablePageUtilsTest
: public content::ContentBrowserTest
,
28 content::WebContentsObserver
{
30 void SetUpOnMainThread() override
{
31 if (!DistillerJavaScriptWorldIdIsSet()) {
32 SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END
);
34 AddComponentsResources();
36 ContentBrowserTest::SetUpOnMainThread();
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
),
47 ui::PAGE_TRANSITION_TYPED
,
49 url_loaded_runner
.Run();
50 main_frame_loaded_callback_
= base::Closure();
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"));
62 PathService::Get(base::DIR_MODULE
, &pak_dir
);
65 pak_dir
.Append(FILE_PATH_LITERAL("components_tests_resources.pak"));
66 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
67 pak_file
, ui::SCALE_FACTOR_NONE
);
70 void SetUpTestServer() {
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());
78 void DocumentLoadedInFrame(
79 content::RenderFrameHost
* render_frame_host
) override
{
80 if (!render_frame_host
->GetParent())
81 main_frame_loaded_callback_
.Run();
84 base::Closure main_frame_loaded_callback_
;
89 ResultHolder(base::Closure callback
) : callback_(callback
) {}
91 void OnResult(bool result
) {
100 base::Callback
<void(bool)> GetCallback() {
101 return base::Bind(&ResultHolder::OnResult
, base::Unretained(this));
105 base::Closure callback_
;
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());
117 ASSERT_TRUE(holder
.GetResult());
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());
127 ASSERT_FALSE(holder
.GetResult());
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);
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
146 LoadURL(kArticlePath
);
147 base::RunLoop run_loop_
;
148 ResultHolder
holder(run_loop_
.QuitClosure());
149 IsDistillablePageForDetector(shell()->web_contents(), detector
.get(),
150 holder
.GetCallback());
152 ASSERT_TRUE(holder
.GetResult());
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
170 LoadURL(kArticlePath
);
171 base::RunLoop run_loop_
;
172 ResultHolder
holder(run_loop_
.QuitClosure());
173 IsDistillablePageForDetector(shell()->web_contents(), detector
.get(),
174 holder
.GetCallback());
176 ASSERT_FALSE(holder
.GetResult());
179 } // namespace dom_distiller