Chromium Code Reviews| 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 "content/public/browser/browser_context.h" | |
| 11 #include "content/public/browser/render_frame_host.h" | |
| 12 #include "content/public/browser/web_contents_observer.h" | |
| 13 #include "content/public/test/content_browser_test.h" | |
| 14 #include "content/shell/browser/shell.h" | |
| 15 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 16 #include "ui/base/resource/resource_bundle.h" | |
| 17 | |
| 18 namespace dom_distiller { | |
| 19 namespace { | |
| 20 | |
| 21 const char* kArticlePath = "/og_article.html"; | |
| 22 const char* kNonArticlePath = "/non_og_article.html"; | |
| 23 | |
| 24 // This needs to match the number of derived features from | |
| 25 // dom_distiller::CalculateDerivedFeatures. | |
|
nyquist
2015/04/01 18:12:20
Nit: Would it be helpful to add a link to this con
cjhopman
2015/04/01 20:59:29
I just moved this into that header, it makes sense
| |
| 26 int kNumDetectorFeatures = 29; | |
| 27 | |
| 28 class DomDistillerDistillablePageUtilsTest : public content::ContentBrowserTest, | |
| 29 content::WebContentsObserver { | |
| 30 public: | |
| 31 void SetUpOnMainThread() override { | |
| 32 AddComponentsResources(); | |
| 33 SetUpTestServer(); | |
| 34 ContentBrowserTest::SetUpOnMainThread(); | |
| 35 } | |
| 36 | |
| 37 void LoadURL(const std::string& url) { | |
| 38 content::WebContents* current_web_contents = shell()->web_contents(); | |
| 39 Observe(current_web_contents); | |
| 40 base::RunLoop url_loaded_runner; | |
| 41 main_frame_loaded_callback_ = url_loaded_runner.QuitClosure(); | |
| 42 DUMP(embedded_test_server()->GetURL(url)); | |
| 43 current_web_contents->GetController().LoadURL( | |
| 44 embedded_test_server()->GetURL(url), | |
| 45 content::Referrer(), | |
| 46 ui::PAGE_TRANSITION_TYPED, | |
| 47 std::string()); | |
| 48 url_loaded_runner.Run(); | |
| 49 main_frame_loaded_callback_ = base::Closure(); | |
| 50 Observe(nullptr); | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 void AddComponentsResources() { | |
| 55 base::FilePath pak_file; | |
| 56 base::FilePath pak_dir; | |
| 57 PathService::Get(base::DIR_MODULE, &pak_dir); | |
| 58 pak_file = | |
| 59 pak_dir.Append(FILE_PATH_LITERAL("components_tests_resources.pak")); | |
| 60 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( | |
| 61 pak_file, ui::SCALE_FACTOR_NONE); | |
| 62 } | |
| 63 | |
| 64 void SetUpTestServer() { | |
| 65 base::FilePath path; | |
| 66 PathService::Get(base::DIR_SOURCE_ROOT, &path); | |
| 67 path = path.AppendASCII("components/test/data/dom_distiller"); | |
| 68 embedded_test_server()->ServeFilesFromDirectory(path); | |
| 69 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 70 } | |
| 71 | |
| 72 void DocumentLoadedInFrame( | |
| 73 content::RenderFrameHost* render_frame_host) override { | |
| 74 if (!render_frame_host->GetParent()) | |
| 75 main_frame_loaded_callback_.Run(); | |
| 76 } | |
| 77 | |
| 78 base::Closure main_frame_loaded_callback_; | |
| 79 }; | |
| 80 | |
| 81 class ResultHolder { | |
| 82 public: | |
| 83 ResultHolder(base::Closure callback) : callback_(callback) {} | |
| 84 | |
| 85 void OnResult(bool result) { | |
| 86 result_ = result; | |
| 87 callback_.Run(); | |
| 88 } | |
| 89 | |
| 90 bool GetResult() { | |
| 91 return result_; | |
| 92 } | |
| 93 | |
| 94 base::Callback<void(bool)> GetCallback() { | |
| 95 return base::Bind(&ResultHolder::OnResult, base::Unretained(this)); | |
| 96 } | |
| 97 | |
| 98 private: | |
| 99 base::Closure callback_; | |
| 100 bool result_; | |
| 101 }; | |
| 102 | |
| 103 } // namespace | |
| 104 | |
| 105 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, TestIsOGArticle) { | |
| 106 LoadURL(kArticlePath); | |
| 107 base::RunLoop run_loop_; | |
| 108 ResultHolder holder(run_loop_.QuitClosure()); | |
| 109 IsOpenGraphArticle(shell()->web_contents(), holder.GetCallback()); | |
| 110 run_loop_.Run(); | |
| 111 ASSERT_TRUE(holder.GetResult()); | |
| 112 } | |
| 113 | |
| 114 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, | |
| 115 TestIsNotOGArticle) { | |
| 116 LoadURL(kNonArticlePath); | |
| 117 base::RunLoop run_loop_; | |
| 118 ResultHolder holder(run_loop_.QuitClosure()); | |
| 119 IsOpenGraphArticle(shell()->web_contents(), holder.GetCallback()); | |
| 120 run_loop_.Run(); | |
| 121 ASSERT_FALSE(holder.GetResult()); | |
| 122 } | |
| 123 | |
| 124 IN_PROC_BROWSER_TEST_F(DomDistillerDistillablePageUtilsTest, | |
| 125 TestIsDistillablePage) { | |
| 126 scoped_ptr<AdaBoostProto> proto(new AdaBoostProto); | |
| 127 proto->set_num_features(kNumDetectorFeatures); | |
| 128 proto->set_num_stumps(1); | |
| 129 // The first value of the first feature is either 0 or 1. This model should | |
|
nyquist
2015/04/01 18:12:19
Yes, it's true that the first value is false/true
cjhopman
2015/04/01 20:59:29
This might be better.
nyquist
2015/04/01 22:30:24
Yes. Thanks!
| |
| 130 // accept any set of derived features. | |
| 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 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(kNumDetectorFeatures); | |
| 150 proto->set_num_stumps(1); | |
| 151 // The first value of the first feature is either 0 or 1. This model should | |
| 152 // reject (because of the negative weight) any set of derived features. | |
| 153 StumpProto* stump = proto->add_stump(); | |
| 154 stump->set_feature_number(0); | |
| 155 stump->set_weight(-1); | |
| 156 stump->set_split(-1); | |
| 157 scoped_ptr<DistillablePageDetector> detector( | |
| 158 new DistillablePageDetector(proto.Pass())); | |
| 159 LoadURL(kArticlePath); | |
| 160 base::RunLoop run_loop_; | |
| 161 ResultHolder holder(run_loop_.QuitClosure()); | |
| 162 IsDistillablePageForDetector(shell()->web_contents(), detector.get(), | |
| 163 holder.GetCallback()); | |
| 164 run_loop_.Run(); | |
| 165 ASSERT_FALSE(holder.GetResult()); | |
| 166 } | |
| 167 | |
| 168 } // namespace dom_distiller | |
| OLD | NEW |