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/distillable_page_utils.h"
9 #include "components/dom_distiller/core/distillable_page_detector.h"
10 #include "components/dom_distiller/core/page_features.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "content/public/test/content_browser_test.h"
15 #include "content/shell/browser/shell.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h"
17 #include "ui/base/resource/resource_bundle.h"
19 namespace dom_distiller
{
22 const char* kArticlePath
= "/og_article.html";
23 const char* kNonArticlePath
= "/non_og_article.html";
25 class DomDistillerDistillablePageUtilsTest
: public content::ContentBrowserTest
,
26 content::WebContentsObserver
{
28 void SetUpOnMainThread() override
{
29 AddComponentsResources();
31 ContentBrowserTest::SetUpOnMainThread();
34 void LoadURL(const std::string
& url
) {
35 content::WebContents
* current_web_contents
= shell()->web_contents();
36 Observe(current_web_contents
);
37 base::RunLoop url_loaded_runner
;
38 main_frame_loaded_callback_
= url_loaded_runner
.QuitClosure();
39 current_web_contents
->GetController().LoadURL(
40 embedded_test_server()->GetURL(url
),
42 ui::PAGE_TRANSITION_TYPED
,
44 url_loaded_runner
.Run();
45 main_frame_loaded_callback_
= base::Closure();
50 void AddComponentsResources() {
51 base::FilePath pak_file
;
52 base::FilePath pak_dir
;
53 #if defined(OS_ANDROID)
54 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA
, &pak_dir
));
55 pak_dir
= pak_dir
.Append(FILE_PATH_LITERAL("paks"));
57 PathService::Get(base::DIR_MODULE
, &pak_dir
);
60 pak_dir
.Append(FILE_PATH_LITERAL("components_tests_resources.pak"));
61 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
62 pak_file
, ui::SCALE_FACTOR_NONE
);
65 void SetUpTestServer() {
67 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
68 path
= path
.AppendASCII("components/test/data/dom_distiller");
69 embedded_test_server()->ServeFilesFromDirectory(path
);
70 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
73 void DocumentLoadedInFrame(
74 content::RenderFrameHost
* render_frame_host
) override
{
75 if (!render_frame_host
->GetParent())
76 main_frame_loaded_callback_
.Run();
79 base::Closure main_frame_loaded_callback_
;
84 ResultHolder(base::Closure callback
) : callback_(callback
) {}
86 void OnResult(bool result
) {
95 base::Callback
<void(bool)> GetCallback() {
96 return base::Bind(&ResultHolder::OnResult
, base::Unretained(this));
100 base::Closure callback_
;
106 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest
, TestIsOGArticle
) {
107 LoadURL(kArticlePath
);
108 base::RunLoop run_loop_
;
109 ResultHolder
holder(run_loop_
.QuitClosure());
110 IsOpenGraphArticle(shell()->web_contents(), holder
.GetCallback());
112 ASSERT_TRUE(holder
.GetResult());
115 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest
,
116 TestIsNotOGArticle
) {
117 LoadURL(kNonArticlePath
);
118 base::RunLoop run_loop_
;
119 ResultHolder
holder(run_loop_
.QuitClosure());
120 IsOpenGraphArticle(shell()->web_contents(), holder
.GetCallback());
122 ASSERT_FALSE(holder
.GetResult());
125 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest
,
126 TestIsDistillablePage
) {
127 scoped_ptr
<AdaBoostProto
> proto(new AdaBoostProto
);
128 proto
->set_num_features(kDerivedFeaturesCount
);
129 proto
->set_num_stumps(1);
131 StumpProto
* stump
= proto
->add_stump();
132 stump
->set_feature_number(0);
133 stump
->set_weight(1);
134 stump
->set_split(-1);
135 scoped_ptr
<DistillablePageDetector
> detector(
136 new DistillablePageDetector(proto
.Pass()));
137 EXPECT_DOUBLE_EQ(0.5, detector
->GetThreshold());
138 // The first value of the first feature is either 0 or 1. Since the stump's
139 // split is -1, the stump weight will be applied to any set of derived
141 LoadURL(kArticlePath
);
142 base::RunLoop run_loop_
;
143 ResultHolder
holder(run_loop_
.QuitClosure());
144 IsDistillablePageForDetector(shell()->web_contents(), detector
.get(),
145 holder
.GetCallback());
147 ASSERT_TRUE(holder
.GetResult());
150 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest
,
151 TestIsNotDistillablePage
) {
152 scoped_ptr
<AdaBoostProto
> proto(new AdaBoostProto
);
153 proto
->set_num_features(kDerivedFeaturesCount
);
154 proto
->set_num_stumps(1);
155 StumpProto
* stump
= proto
->add_stump();
156 stump
->set_feature_number(0);
157 stump
->set_weight(-1);
158 stump
->set_split(-1);
159 scoped_ptr
<DistillablePageDetector
> detector(
160 new DistillablePageDetector(proto
.Pass()));
161 EXPECT_DOUBLE_EQ(-0.5, detector
->GetThreshold());
162 // The first value of the first feature is either 0 or 1. Since the stump's
163 // split is -1, the stump weight will be applied to any set of derived
165 LoadURL(kArticlePath
);
166 base::RunLoop run_loop_
;
167 ResultHolder
holder(run_loop_
.QuitClosure());
168 IsDistillablePageForDetector(shell()->web_contents(), detector
.get(),
169 holder
.GetCallback());
171 ASSERT_FALSE(holder
.GetResult());
174 } // namespace dom_distiller