Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / dom_distiller / content / browser / distillable_page_utils_browsertest.cc
blob7723e67be4ba7562186e4a9ee7b123dc39a25629
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.
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"
21 namespace dom_distiller {
22 namespace {
24 const char* kArticlePath = "/og_article.html";
25 const char* kNonArticlePath = "/non_og_article.html";
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);
34 AddComponentsResources();
35 SetUpTestServer();
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),
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);
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);
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());
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_;
87 class ResultHolder {
88 public:
89 ResultHolder(base::Closure callback) : callback_(callback) {}
91 void OnResult(bool result) {
92 result_ = result;
93 callback_.Run();
96 bool GetResult() {
97 return result_;
100 base::Callback<void(bool)> GetCallback() {
101 return base::Bind(&ResultHolder::OnResult, base::Unretained(this));
104 private:
105 base::Closure callback_;
106 bool result_;
109 } // namespace
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());
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());
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
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());
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());
179 } // namespace dom_distiller