| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "components/dom_distiller/content/browser/distillable_page_utils.h" | 8 #include "components/dom_distiller/content/browser/distillable_page_utils.h" |
| 9 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" | 9 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
| 10 #include "components/dom_distiller/core/distillable_page_detector.h" | 10 #include "components/dom_distiller/core/distillable_page_detector.h" |
| 11 #include "components/dom_distiller/core/page_features.h" | 11 #include "components/dom_distiller/core/page_features.h" |
| 12 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/render_frame_host.h" | 13 #include "content/public/browser/render_frame_host.h" |
| 14 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
| 15 #include "content/public/common/isolated_world_ids.h" | 15 #include "content/public/common/isolated_world_ids.h" |
| 16 #include "content/public/test/content_browser_test.h" | 16 #include "content/public/test/content_browser_test.h" |
| 17 #include "content/shell/browser/shell.h" | 17 #include "content/shell/browser/shell.h" |
| 18 #include "net/test/embedded_test_server/embedded_test_server.h" | 18 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 20 | 20 |
| 21 namespace dom_distiller { | 21 namespace dom_distiller { |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 //const char* kSimpleArticlePath = "/dom_distiller/simple_article.html"; |
| 24 const char* kArticlePath = "/og_article.html"; | 25 const char* kArticlePath = "/og_article.html"; |
| 25 const char* kNonArticlePath = "/non_og_article.html"; | 26 //const char* kNonArticlePath = "/non_og_article.html"; |
| 26 | 27 |
| 27 class DomDistillerDistillablePageUtilsTest : public content::ContentBrowserTest, | 28 class DomDistillerDistillablePageUtilsTest : public content::ContentBrowserTest, |
| 28 content::WebContentsObserver { | 29 content::WebContentsObserver { |
| 29 public: | 30 public: |
| 30 void SetUpOnMainThread() override { | 31 void SetUpOnMainThread() override { |
| 31 if (!DistillerJavaScriptWorldIdIsSet()) { | 32 if (!DistillerJavaScriptWorldIdIsSet()) { |
| 32 SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END); | 33 SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END); |
| 33 } | 34 } |
| 34 AddComponentsResources(); | 35 AddComponentsResources(); |
| 35 SetUpTestServer(); | 36 SetUpTestServer(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 main_frame_loaded_callback_.Run(); | 82 main_frame_loaded_callback_.Run(); |
| 82 } | 83 } |
| 83 | 84 |
| 84 base::Closure main_frame_loaded_callback_; | 85 base::Closure main_frame_loaded_callback_; |
| 85 }; | 86 }; |
| 86 | 87 |
| 87 class ResultHolder { | 88 class ResultHolder { |
| 88 public: | 89 public: |
| 89 ResultHolder(base::Closure callback) : callback_(callback) {} | 90 ResultHolder(base::Closure callback) : callback_(callback) {} |
| 90 | 91 |
| 91 void OnResult(bool result) { | 92 void OnResult(bool result, bool a) { |
| 92 result_ = result; | 93 result_ = result; |
| 93 callback_.Run(); | 94 callback_.Run(); |
| 94 } | 95 } |
| 95 | 96 |
| 96 bool GetResult() { | 97 bool GetResult() { |
| 97 return result_; | 98 return result_; |
| 98 } | 99 } |
| 99 | 100 |
| 100 base::Callback<void(bool)> GetCallback() { | 101 base::Callback<void(bool, bool)> GetCallback() { |
| 101 return base::Bind(&ResultHolder::OnResult, base::Unretained(this)); | 102 return base::Bind(&ResultHolder::OnResult, base::Unretained(this)); |
| 102 } | 103 } |
| 103 | 104 |
| 104 private: | 105 private: |
| 105 base::Closure callback_; | 106 base::Closure callback_; |
| 106 bool result_; | 107 bool result_; |
| 107 }; | 108 }; |
| 108 | 109 |
| 109 } // namespace | 110 } // namespace |
| 110 | 111 /* |
| 111 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, TestIsOGArticle) { | 112 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, TestIsOGArticle) { |
| 112 LoadURL(kArticlePath); | 113 LoadURL(kArticlePath); |
| 113 base::RunLoop run_loop_; | 114 base::RunLoop run_loop_; |
| 114 ResultHolder holder(run_loop_.QuitClosure()); | 115 ResultHolder holder(run_loop_.QuitClosure()); |
| 115 IsOpenGraphArticle(shell()->web_contents(), holder.GetCallback()); | 116 IsOpenGraphArticle(shell()->web_contents(), holder.GetCallback()); |
| 116 run_loop_.Run(); | 117 run_loop_.Run(); |
| 117 ASSERT_TRUE(holder.GetResult()); | 118 ASSERT_TRUE(holder.GetResult()); |
| 118 } | 119 } |
| 119 | 120 |
| 120 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, | 121 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, |
| 121 TestIsNotOGArticle) { | 122 TestIsNotOGArticle) { |
| 122 LoadURL(kNonArticlePath); | 123 LoadURL(kNonArticlePath); |
| 123 base::RunLoop run_loop_; | 124 base::RunLoop run_loop_; |
| 124 ResultHolder holder(run_loop_.QuitClosure()); | 125 ResultHolder holder(run_loop_.QuitClosure()); |
| 125 IsOpenGraphArticle(shell()->web_contents(), holder.GetCallback()); | 126 IsOpenGraphArticle(shell()->web_contents(), holder.GetCallback()); |
| 126 run_loop_.Run(); | 127 run_loop_.Run(); |
| 127 ASSERT_FALSE(holder.GetResult()); | 128 ASSERT_FALSE(holder.GetResult()); |
| 128 } | 129 } |
| 129 | 130 */ |
| 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 | 131 } // namespace dom_distiller |
| OLD | NEW |