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 "base/command_line.h" |
| 8 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 10 #include "chrome/common/chrome_switches.h" |
| 11 #include "chrome/test/base/in_process_browser_test.h" |
| 12 #include "chrome/test/base/ui_test_utils.h" |
| 13 #include "components/dom_distiller/content/browser/distillable_page_utils.h" |
| 14 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
| 15 #include "components/dom_distiller/core/distillable_page_detector.h" |
| 16 #include "components/dom_distiller/core/dom_distiller_switches.h" |
| 17 #include "components/dom_distiller/core/page_features.h" |
| 18 #include "content/public/browser/render_frame_host.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/browser/web_contents_observer.h" |
| 21 #include "content/public/common/isolated_world_ids.h" |
| 22 #include "content/public/test/browser_test_utils.h" |
| 23 #include "content/public/test/content_browser_test.h" |
| 24 #include "content/public/test/test_utils.h" |
| 25 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 26 |
| 27 namespace dom_distiller { |
| 28 |
| 29 namespace { |
| 30 |
| 31 const char* kSimpleArticlePath = "/dom_distiller/simple_article.html"; |
| 32 |
| 33 class ResultHolder { |
| 34 public: |
| 35 explicit ResultHolder(base::Closure callback) |
| 36 : callback_(callback), |
| 37 count_(0) {} |
| 38 |
| 39 void OnResult(bool result, bool isLast) { |
| 40 // TODO(wychen): test isLast? |
| 41 LOG(ERROR) << "ReaderMode ResultHolder::OnResult " << result; |
| 42 switch (count_) { |
| 43 case 0: |
| 44 result1_ = result; |
| 45 //callback_.Run(); |
| 46 break; |
| 47 case 1: |
| 48 result2_ = result; |
| 49 callback_.Run(); |
| 50 break; |
| 51 } |
| 52 count_++; |
| 53 } |
| 54 |
| 55 int GetCount() { |
| 56 return count_; |
| 57 } |
| 58 |
| 59 bool GetResult1() { |
| 60 return result1_; |
| 61 } |
| 62 |
| 63 bool GetResult2() { |
| 64 return result2_; |
| 65 } |
| 66 |
| 67 base::Callback<void(bool, bool)> GetCallback() { |
| 68 return base::Bind(&ResultHolder::OnResult, base::Unretained(this)); |
| 69 } |
| 70 |
| 71 private: |
| 72 base::Closure callback_; |
| 73 int count_; |
| 74 bool result1_, result2_; |
| 75 }; |
| 76 |
| 77 } // namespace |
| 78 |
| 79 class DistillablePageUtilsBrowserTest : public InProcessBrowserTest { |
| 80 public: |
| 81 void SetUpOnMainThread() override { |
| 82 if (!DistillerJavaScriptWorldIdIsSet()) { |
| 83 SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END); |
| 84 } |
| 85 InProcessBrowserTest::SetUpOnMainThread(); |
| 86 } |
| 87 |
| 88 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 89 command_line->AppendSwitchASCII(switches::kReaderModeHeuristics, |
| 90 switches::reader_mode_heuristics::kAdaBoost); |
| 91 command_line->AppendSwitch(switches::kEnableDomDistiller); |
| 92 } |
| 93 |
| 94 void assertDistillability(bool); |
| 95 }; |
| 96 |
| 97 void DistillablePageUtilsBrowserTest::assertDistillability(bool distillable) |
| 98 { |
| 99 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 100 |
| 101 content::WebContents* web_contents = |
| 102 browser()->tab_strip_model()->GetActiveWebContents(); |
| 103 const GURL& article_url = embedded_test_server()->GetURL(kSimpleArticlePath); |
| 104 |
| 105 // Set up AdaBoost. |
| 106 scoped_ptr<AdaBoostProto> proto(new AdaBoostProto); |
| 107 proto->set_num_features(kDerivedFeaturesCount); |
| 108 proto->set_num_stumps(1); |
| 109 |
| 110 StumpProto* stump = proto->add_stump(); |
| 111 stump->set_feature_number(0); |
| 112 int weight = distillable ? 1 : -1; |
| 113 stump->set_weight(weight); |
| 114 stump->set_split(-1); |
| 115 scoped_ptr<DistillablePageDetector> detector( |
| 116 new DistillablePageDetector(proto.Pass())); |
| 117 EXPECT_DOUBLE_EQ(0.5 * weight, detector->GetThreshold()); |
| 118 // The first value of the first feature is either 0 or 1. Since the stump's |
| 119 // split is -1, the stump weight will be applied to any set of derived |
| 120 // features. |
| 121 base::RunLoop run_loop_; |
| 122 ResultHolder holder(run_loop_.QuitClosure()); |
| 123 setCallback(web_contents, holder.GetCallback()); |
| 124 |
| 125 // This blocks until the navigation has completely finished. |
| 126 ui_test_utils::NavigateToURL(browser(), article_url); |
| 127 content::WaitForLoadStop(web_contents); |
| 128 |
| 129 run_loop_.Run(); |
| 130 |
| 131 EXPECT_TRUE(holder.GetCount() == 2); |
| 132 EXPECT_TRUE(holder.GetResult1() == distillable); |
| 133 EXPECT_TRUE(holder.GetResult2() == distillable); |
| 134 } |
| 135 |
| 136 IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTest, |
| 137 TestIsDistillablePage) { |
| 138 assertDistillability(true); |
| 139 } |
| 140 |
| 141 IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTest, |
| 142 TestIsNotDistillablePage) { |
| 143 assertDistillability(false); |
| 144 } |
| 145 |
| 146 } // namespace dom_distiller |
OLD | NEW |