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

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

Issue 1047223003: Add integration of the new heuristics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dd-adaboost-model
Patch Set: Created 5 years, 8 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
OLDNEW
(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/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"
18
19 namespace dom_distiller {
20 namespace {
21
22 const char* kArticlePath = "/og_article.html";
23 const char* kNonArticlePath = "/non_og_article.html";
24
25 class DomDistillerDistillablePageUtilsTest : public content::ContentBrowserTest,
26 content::WebContentsObserver {
27 public:
28 void SetUpOnMainThread() override {
29 AddComponentsResources();
30 SetUpTestServer();
31 ContentBrowserTest::SetUpOnMainThread();
32 }
33
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 DUMP(embedded_test_server()->GetURL(url));
40 current_web_contents->GetController().LoadURL(
41 embedded_test_server()->GetURL(url),
42 content::Referrer(),
43 ui::PAGE_TRANSITION_TYPED,
44 std::string());
45 url_loaded_runner.Run();
46 main_frame_loaded_callback_ = base::Closure();
47 Observe(nullptr);
48 }
49
50 private:
51 void AddComponentsResources() {
52 base::FilePath pak_file;
53 base::FilePath pak_dir;
54 PathService::Get(base::DIR_MODULE, &pak_dir);
55 pak_file =
56 pak_dir.Append(FILE_PATH_LITERAL("components_tests_resources.pak"));
57 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
58 pak_file, ui::SCALE_FACTOR_NONE);
59 }
60
61 void SetUpTestServer() {
62 base::FilePath path;
63 PathService::Get(base::DIR_SOURCE_ROOT, &path);
64 path = path.AppendASCII("components/test/data/dom_distiller");
65 embedded_test_server()->ServeFilesFromDirectory(path);
66 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
67 }
68
69 void DocumentLoadedInFrame(
70 content::RenderFrameHost* render_frame_host) override {
71 if (!render_frame_host->GetParent())
72 main_frame_loaded_callback_.Run();
73 }
74
75 base::Closure main_frame_loaded_callback_;
76 };
77
78 class ResultHolder {
79 public:
80 ResultHolder(base::Closure callback) : callback_(callback) {}
81
82 void OnResult(bool result) {
83 result_ = result;
84 callback_.Run();
85 }
86
87 bool GetResult() {
88 return result_;
89 }
90
91 base::Callback<void(bool)> GetCallback() {
92 return base::Bind(&ResultHolder::OnResult, base::Unretained(this));
93 }
94
95 private:
96 base::Closure callback_;
97 bool result_;
98 };
99
100 } // namespace
101
102 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, TestIsOGArticle) {
103 LoadURL(kArticlePath);
104 base::RunLoop run_loop_;
105 ResultHolder holder(run_loop_.QuitClosure());
106 IsOpenGraphArticle(shell()->web_contents(), holder.GetCallback());
107 run_loop_.Run();
108 ASSERT_TRUE(holder.GetResult());
109 }
110
111 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest,
112 TestIsNotOGArticle) {
113 LoadURL(kNonArticlePath);
114 base::RunLoop run_loop_;
115 ResultHolder holder(run_loop_.QuitClosure());
116 IsOpenGraphArticle(shell()->web_contents(), holder.GetCallback());
117 run_loop_.Run();
118 ASSERT_FALSE(holder.GetResult());
119 }
120
121 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest,
122 TestIsDistillablePage) {
123 scoped_ptr<AdaBoostProto> proto(new AdaBoostProto);
124 proto->set_num_features(kDerivedFeaturesCount);
125 proto->set_num_stumps(1);
126
127 StumpProto* stump = proto->add_stump();
128 stump->set_feature_number(0);
129 stump->set_weight(1);
130 stump->set_split(-1);
131 scoped_ptr<DistillablePageDetector> detector(
132 new DistillablePageDetector(proto.Pass()));
133 EXPECT_DOUBLE_EQ(0.5, detector->GetThreshold());
134 // The first value of the first feature is either 0 or 1. Since the stump's
135 // split is -1, the stump weight will be applied to any set of derived
136 // features.
137 LoadURL(kArticlePath);
138 base::RunLoop run_loop_;
139 ResultHolder holder(run_loop_.QuitClosure());
140 IsDistillablePageForDetector(shell()->web_contents(), detector.get(),
141 holder.GetCallback());
142 run_loop_.Run();
143 ASSERT_TRUE(holder.GetResult());
144 }
145
146 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest,
147 TestIsNotDistillablePage) {
148 scoped_ptr<AdaBoostProto> proto(new AdaBoostProto);
149 proto->set_num_features(kDerivedFeaturesCount);
150 proto->set_num_stumps(1);
151 StumpProto* stump = proto->add_stump();
152 stump->set_feature_number(0);
153 stump->set_weight(-1);
154 stump->set_split(-1);
155 scoped_ptr<DistillablePageDetector> detector(
156 new DistillablePageDetector(proto.Pass()));
157 EXPECT_DOUBLE_EQ(-0.5, detector->GetThreshold());
158 // The first value of the first feature is either 0 or 1. Since the stump's
159 // split is -1, the stump weight will be applied to any set of derived
160 // features.
161 LoadURL(kArticlePath);
162 base::RunLoop run_loop_;
163 ResultHolder holder(run_loop_.QuitClosure());
164 IsDistillablePageForDetector(shell()->web_contents(), detector.get(),
165 holder.GetCallback());
166 run_loop_.Run();
167 ASSERT_FALSE(holder.GetResult());
168 }
169
170 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698