OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 <string.h> |
| 6 |
| 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 9 #include "chrome/test/base/in_process_browser_test.h" |
| 10 #include "chrome/test/base/ui_test_utils.h" |
| 11 #include "components/dom_distiller/content/browser/distillable_page_utils.h" |
| 12 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
| 13 #include "components/dom_distiller/core/distillable_page_detector.h" |
| 14 #include "components/dom_distiller/core/page_features.h" |
| 15 #include "content/public/browser/render_frame_host.h" |
| 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/browser/web_contents_observer.h" |
| 18 #include "content/public/common/isolated_world_ids.h" |
| 19 #include "content/public/test/browser_test_utils.h" |
| 20 #include "content/public/test/content_browser_test.h" |
| 21 #include "content/public/test/test_utils.h" |
| 22 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 23 |
| 24 namespace dom_distiller { |
| 25 |
| 26 namespace { |
| 27 |
| 28 const char* kSimpleArticlePath = "/dom_distiller/simple_article.html"; |
| 29 |
| 30 class ResultHolder { |
| 31 public: |
| 32 ResultHolder(base::Closure callback) : callback_(callback) {} |
| 33 |
| 34 void OnResult(bool result) { |
| 35 result_ = result; |
| 36 callback_.Run(); |
| 37 } |
| 38 |
| 39 bool GetResult() { |
| 40 return result_; |
| 41 } |
| 42 |
| 43 base::Callback<void(bool)> GetCallback() { |
| 44 return base::Bind(&ResultHolder::OnResult, base::Unretained(this)); |
| 45 } |
| 46 |
| 47 private: |
| 48 base::Closure callback_; |
| 49 bool result_; |
| 50 }; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 class DistillablePageUtilsBrowserTest : public InProcessBrowserTest { |
| 55 public: |
| 56 void SetUpOnMainThread() override { |
| 57 if (!DistillerJavaScriptWorldIdIsSet()) { |
| 58 SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END); |
| 59 } |
| 60 InProcessBrowserTest::SetUpOnMainThread(); |
| 61 } |
| 62 |
| 63 void assertDistillability(bool); |
| 64 }; |
| 65 |
| 66 void DistillablePageUtilsBrowserTest::assertDistillability(bool distillable) |
| 67 { |
| 68 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 69 |
| 70 content::WebContents* web_contents = |
| 71 browser()->tab_strip_model()->GetActiveWebContents(); |
| 72 const GURL& article_url = embedded_test_server()->GetURL(kSimpleArticlePath); |
| 73 |
| 74 // This blocks until the navigation has completely finished. |
| 75 ui_test_utils::NavigateToURL(browser(), article_url); |
| 76 content::WaitForLoadStop(web_contents); |
| 77 |
| 78 // Set up AdaBoost. |
| 79 scoped_ptr<AdaBoostProto> proto(new AdaBoostProto); |
| 80 proto->set_num_features(kDerivedFeaturesCount); |
| 81 proto->set_num_stumps(1); |
| 82 |
| 83 StumpProto* stump = proto->add_stump(); |
| 84 stump->set_feature_number(0); |
| 85 int weight = distillable ? 1 : -1; |
| 86 stump->set_weight(weight); |
| 87 stump->set_split(-1); |
| 88 scoped_ptr<DistillablePageDetector> detector( |
| 89 new DistillablePageDetector(proto.Pass())); |
| 90 EXPECT_DOUBLE_EQ(0.5 * weight, detector->GetThreshold()); |
| 91 // The first value of the first feature is either 0 or 1. Since the stump's |
| 92 // split is -1, the stump weight will be applied to any set of derived |
| 93 // features. |
| 94 base::RunLoop run_loop_; |
| 95 ResultHolder holder(run_loop_.QuitClosure()); |
| 96 IsDistillablePageForDetector(web_contents, detector.get(), |
| 97 holder.GetCallback()); |
| 98 run_loop_.Run(); |
| 99 ASSERT_TRUE(holder.GetResult() == distillable); |
| 100 } |
| 101 |
| 102 IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTest, |
| 103 TestIsDistillablePage) { |
| 104 assertDistillability(true); |
| 105 } |
| 106 |
| 107 IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTest, |
| 108 TestIsNotDistillablePage) { |
| 109 assertDistillability(false); |
| 110 } |
| 111 |
| 112 } // namespace dom_distiller |
OLD | NEW |