Index: chrome/browser/dom_distiller/distillable_page_utils_browsertest.cc |
diff --git a/chrome/browser/dom_distiller/distillable_page_utils_browsertest.cc b/chrome/browser/dom_distiller/distillable_page_utils_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..37f410ae8e9615170202c512ea8ad40996d1b070 |
--- /dev/null |
+++ b/chrome/browser/dom_distiller/distillable_page_utils_browsertest.cc |
@@ -0,0 +1,146 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <string.h> |
+ |
+#include "base/command_line.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/tabs/tab_strip_model.h" |
+#include "chrome/common/chrome_switches.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "chrome/test/base/ui_test_utils.h" |
+#include "components/dom_distiller/content/browser/distillable_page_utils.h" |
+#include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
+#include "components/dom_distiller/core/distillable_page_detector.h" |
+#include "components/dom_distiller/core/dom_distiller_switches.h" |
+#include "components/dom_distiller/core/page_features.h" |
+#include "content/public/browser/render_frame_host.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/browser/web_contents_observer.h" |
+#include "content/public/common/isolated_world_ids.h" |
+#include "content/public/test/browser_test_utils.h" |
+#include "content/public/test/content_browser_test.h" |
+#include "content/public/test/test_utils.h" |
+#include "net/test/embedded_test_server/embedded_test_server.h" |
+ |
+namespace dom_distiller { |
+ |
+namespace { |
+ |
+const char* kSimpleArticlePath = "/dom_distiller/simple_article.html"; |
+ |
+class ResultHolder { |
+ public: |
+ explicit ResultHolder(base::Closure callback) |
+ : callback_(callback), |
+ count_(0) {} |
+ |
+ void OnResult(bool result, bool isLast) { |
+ // TODO(wychen): test isLast? |
+ LOG(ERROR) << "ReaderMode ResultHolder::OnResult " << result; |
+ switch (count_) { |
+ case 0: |
+ result1_ = result; |
+ //callback_.Run(); |
+ break; |
+ case 1: |
+ result2_ = result; |
+ callback_.Run(); |
+ break; |
+ } |
+ count_++; |
+ } |
+ |
+ int GetCount() { |
+ return count_; |
+ } |
+ |
+ bool GetResult1() { |
+ return result1_; |
+ } |
+ |
+ bool GetResult2() { |
+ return result2_; |
+ } |
+ |
+ base::Callback<void(bool, bool)> GetCallback() { |
+ return base::Bind(&ResultHolder::OnResult, base::Unretained(this)); |
+ } |
+ |
+ private: |
+ base::Closure callback_; |
+ int count_; |
+ bool result1_, result2_; |
+}; |
+ |
+} // namespace |
+ |
+class DistillablePageUtilsBrowserTest : public InProcessBrowserTest { |
+ public: |
+ void SetUpOnMainThread() override { |
+ if (!DistillerJavaScriptWorldIdIsSet()) { |
+ SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END); |
+ } |
+ InProcessBrowserTest::SetUpOnMainThread(); |
+ } |
+ |
+ void SetUpCommandLine(base::CommandLine* command_line) override { |
+ command_line->AppendSwitchASCII(switches::kReaderModeHeuristics, |
+ switches::reader_mode_heuristics::kAdaBoost); |
+ command_line->AppendSwitch(switches::kEnableDomDistiller); |
+ } |
+ |
+ void assertDistillability(bool); |
+}; |
+ |
+void DistillablePageUtilsBrowserTest::assertDistillability(bool distillable) |
+{ |
+ ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
+ |
+ content::WebContents* web_contents = |
+ browser()->tab_strip_model()->GetActiveWebContents(); |
+ const GURL& article_url = embedded_test_server()->GetURL(kSimpleArticlePath); |
+ |
+ // Set up AdaBoost. |
+ scoped_ptr<AdaBoostProto> proto(new AdaBoostProto); |
+ proto->set_num_features(kDerivedFeaturesCount); |
+ proto->set_num_stumps(1); |
+ |
+ StumpProto* stump = proto->add_stump(); |
+ stump->set_feature_number(0); |
+ int weight = distillable ? 1 : -1; |
+ stump->set_weight(weight); |
+ stump->set_split(-1); |
+ scoped_ptr<DistillablePageDetector> detector( |
+ new DistillablePageDetector(proto.Pass())); |
+ EXPECT_DOUBLE_EQ(0.5 * weight, detector->GetThreshold()); |
+ // The first value of the first feature is either 0 or 1. Since the stump's |
+ // split is -1, the stump weight will be applied to any set of derived |
+ // features. |
+ base::RunLoop run_loop_; |
+ ResultHolder holder(run_loop_.QuitClosure()); |
+ setCallback(web_contents, holder.GetCallback()); |
+ |
+ // This blocks until the navigation has completely finished. |
+ ui_test_utils::NavigateToURL(browser(), article_url); |
+ content::WaitForLoadStop(web_contents); |
+ |
+ run_loop_.Run(); |
+ |
+ EXPECT_TRUE(holder.GetCount() == 2); |
+ EXPECT_TRUE(holder.GetResult1() == distillable); |
+ EXPECT_TRUE(holder.GetResult2() == distillable); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTest, |
+ TestIsDistillablePage) { |
+ assertDistillability(true); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTest, |
+ TestIsNotDistillablePage) { |
+ assertDistillability(false); |
+} |
+ |
+} // namespace dom_distiller |