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