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 "base/thread_task_runner_handle.h" |
| 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 11 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "chrome/test/base/ui_test_utils.h" |
| 14 #include "components/dom_distiller/content/browser/distillable_page_utils.h" |
| 15 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
| 16 #include "components/dom_distiller/core/distillable_page_detector.h" |
| 17 #include "components/dom_distiller/core/dom_distiller_switches.h" |
| 18 #include "components/dom_distiller/core/page_features.h" |
| 19 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/browser/web_contents_observer.h" |
| 22 #include "content/public/common/isolated_world_ids.h" |
| 23 #include "content/public/test/browser_test_utils.h" |
| 24 #include "content/public/test/content_browser_test.h" |
| 25 #include "content/public/test/test_utils.h" |
| 26 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 27 #include "testing/gmock/include/gmock/gmock.h" |
| 28 |
| 29 namespace dom_distiller { |
| 30 |
| 31 using ::testing::_; |
| 32 using namespace switches::reader_mode_heuristics; |
| 33 |
| 34 namespace { |
| 35 |
| 36 const char* kSimpleArticlePath = "/dom_distiller/simple_article.html"; |
| 37 const char* kArticlePath = "/dom_distiller/og_article.html"; |
| 38 const char* kNonArticlePath = "/dom_distiller/non_og_article.html"; |
| 39 |
| 40 class Holder { |
| 41 public: |
| 42 virtual ~Holder() {}; |
| 43 virtual void OnResult(bool, bool) = 0; |
| 44 }; |
| 45 |
| 46 class MockCallback : public Holder { |
| 47 public: |
| 48 MOCK_METHOD2(OnResult, void(bool, bool)); |
| 49 |
| 50 base::Callback<void(bool, bool)> GetCallback() { |
| 51 return base::Bind(&MockCallback::OnResult, base::Unretained(this)); |
| 52 } |
| 53 }; |
| 54 |
| 55 } // namespace |
| 56 |
| 57 template<const char Option[]> |
| 58 class DistillablePageUtilsBrowserTestOption : public InProcessBrowserTest { |
| 59 public: |
| 60 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 61 command_line->AppendSwitch(switches::kEnableDomDistiller); |
| 62 command_line->AppendSwitchASCII(switches::kReaderModeHeuristics, |
| 63 Option); |
| 64 } |
| 65 |
| 66 void SetUpOnMainThread() override { |
| 67 InProcessBrowserTest::SetUpOnMainThread(); |
| 68 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 69 web_contents_ = |
| 70 browser()->tab_strip_model()->GetActiveWebContents(); |
| 71 setCallback(web_contents_, holder_.GetCallback()); |
| 72 } |
| 73 |
| 74 void NavigateAndWait(const char* url) { |
| 75 const GURL& article_url = embedded_test_server()->GetURL(url); |
| 76 |
| 77 // This blocks until the navigation has completely finished. |
| 78 ui_test_utils::NavigateToURL(browser(), article_url); |
| 79 content::WaitForLoadStop(web_contents_); |
| 80 |
| 81 // Wait a bit for the message. |
| 82 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 83 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
| 84 base::TimeDelta::FromMilliseconds(100)); |
| 85 content::RunMessageLoop(); |
| 86 } |
| 87 |
| 88 MockCallback holder_; |
| 89 content::WebContents* web_contents_; |
| 90 }; |
| 91 |
| 92 |
| 93 typedef DistillablePageUtilsBrowserTestOption<kAlwaysTrue> |
| 94 DistillablePageUtilsBrowserTestAlways; |
| 95 |
| 96 IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAlways, |
| 97 TestCallback) { |
| 98 // Run twice to make sure the callback object is still alive. |
| 99 for (unsigned i = 0; i < 2; i++) { |
| 100 testing::InSequence dummy; |
| 101 EXPECT_CALL(holder_, OnResult(true, true)).Times(1); |
| 102 NavigateAndWait(kSimpleArticlePath); |
| 103 } |
| 104 } |
| 105 |
| 106 |
| 107 typedef DistillablePageUtilsBrowserTestOption<kNone> |
| 108 DistillablePageUtilsBrowserTestNone; |
| 109 |
| 110 IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestNone, |
| 111 TestCallback) { |
| 112 EXPECT_CALL(holder_, OnResult(_, _)).Times(0); |
| 113 NavigateAndWait(kSimpleArticlePath); |
| 114 } |
| 115 |
| 116 |
| 117 typedef DistillablePageUtilsBrowserTestOption<kOGArticle> |
| 118 DistillablePageUtilsBrowserTestOG; |
| 119 |
| 120 IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestOG, |
| 121 TestCallback) { |
| 122 { |
| 123 testing::InSequence dummy; |
| 124 EXPECT_CALL(holder_, OnResult(true, true)).Times(1); |
| 125 NavigateAndWait(kArticlePath); |
| 126 } |
| 127 { |
| 128 testing::InSequence dummy; |
| 129 EXPECT_CALL(holder_, OnResult(false, true)).Times(1); |
| 130 NavigateAndWait(kNonArticlePath); |
| 131 } |
| 132 } |
| 133 |
| 134 |
| 135 typedef DistillablePageUtilsBrowserTestOption<kAdaBoost> |
| 136 DistillablePageUtilsBrowserTestAdaboost; |
| 137 |
| 138 IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAdaboost, |
| 139 TestCallback) { |
| 140 { |
| 141 testing::InSequence dummy; |
| 142 EXPECT_CALL(holder_, OnResult(true, false)).Times(1); |
| 143 EXPECT_CALL(holder_, OnResult(true, true)).Times(1); |
| 144 NavigateAndWait(kSimpleArticlePath); |
| 145 } |
| 146 { |
| 147 testing::InSequence dummy; |
| 148 EXPECT_CALL(holder_, OnResult(false, false)).Times(1); |
| 149 EXPECT_CALL(holder_, OnResult(false, true)).Times(1); |
| 150 NavigateAndWait(kNonArticlePath); |
| 151 } |
| 152 } |
| 153 |
| 154 } // namespace dom_distiller |
OLD | NEW |