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