File: distillable_page_utils_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (294 lines) | stat: -rw-r--r-- 11,659 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/dom_distiller/content/browser/distillable_page_utils.h"

#include <array>
#include <cstring>
#include <memory>
#include <optional>

#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.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/core/dom_distiller_switches.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace dom_distiller {
namespace {

using ::testing::_;
using ::testing::AllOf;
using ::testing::Field;
using ::testing::InvokeWithoutArgs;
using ::testing::Not;
using ::testing::Optional;
using ::testing::Pointee;
using ::testing::SizeIs;

const char kSimpleArticlePath[] = "/dom_distiller/simple_article.html";
const char kSimpleArticleIFramePath[] =
    "/dom_distiller/simple_article_iframe.html";
const char kArticlePath[] = "/dom_distiller/og_article.html";
const char kNonArticlePath[] = "/dom_distiller/non_og_article.html";

auto kAllPaths = std::to_array<const char*>({
    kSimpleArticlePath,
    kSimpleArticleIFramePath,
    kArticlePath,
    kNonArticlePath,
});

class MockObserver : public DistillabilityObserver {
 public:
  MOCK_METHOD1(OnResult, void(const DistillabilityResult& result));
};

// Wait a bit to make sure there are no extra calls after the last expected
// call. All the expected calls happen within ~1ms on linux release build,
// so 100ms should be pretty safe to catch extra calls.
//
// If there are no extra calls, changing this doesn't change the test result.
const auto kWaitAfterLastCall = base::Milliseconds(100);

// Wait a bit if no calls are expected to make sure any unexpected calls are
// caught. Expected calls happen within 100ms after content::WaitForLoadStop()
// on linux release build, so 1s provides a safe margin.
//
// If there are no extra calls, changing this doesn't change the test result.
const auto kWaitNoExpectedCall = base::Seconds(1);

}  // namespace

template <const char Option[]>
class TestOption : public InProcessBrowserTest {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(switches::kEnableDomDistiller);
    command_line->AppendSwitchASCII(switches::kReaderModeHeuristics, Option);
    command_line->AppendSwitch(switches::kEnableDistillabilityService);
  }

  void SetUpOnMainThread() override {
    https_server_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::TYPE_HTTPS);
    https_server_->ServeFilesFromSourceDirectory(GetChromeTestDataDir());
    ASSERT_TRUE(https_server_->Start());
    web_contents_ = browser()->tab_strip_model()->GetActiveWebContents();
    AddObserver(web_contents_, &holder_);
  }

  void NavigateAndWait(const char* url, base::TimeDelta test_timeout) {
    run_loop_ = std::make_unique<base::RunLoop>();

    GURL article_url(url);
    if (base::StartsWith(url, "/", base::CompareCase::SENSITIVE)) {
      article_url = https_server()->GetURL(url);
    }

    // This blocks until the navigation has completely finished.
    EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), article_url));
    EXPECT_TRUE(content::WaitForLoadStop(web_contents_));

    if (!test_timeout.is_zero())
      QuitAfter(test_timeout);

    run_loop_->Run();
    run_loop_.reset();
  }

  void QuitSoon() { QuitAfter(kWaitAfterLastCall); }

  void QuitAfter(base::TimeDelta delta) {
    DCHECK(delta.is_positive());
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE, run_loop_->QuitClosure(), delta);
  }

  net::test_server::EmbeddedTestServer* https_server() {
    return https_server_.get();
  }

  std::unique_ptr<base::RunLoop> run_loop_;
  MockObserver holder_;
  raw_ptr<content::WebContents, AcrossTasksDanglingUntriaged> web_contents_ =
      nullptr;
  std::unique_ptr<net::test_server::EmbeddedTestServer> https_server_;
};

MATCHER(IsDistillable,
        "Result " + std::string(negation ? "isn't" : "is") + " distillable") {
  return Matches(Field(&DistillabilityResult::is_distillable, true))(arg);
}

MATCHER(IsLast, "Result " + std::string(negation ? "isn't" : "is") + " last") {
  return Matches(Field(&DistillabilityResult::is_last, true))(arg);
}

MATCHER(IsMobileFriendly,
        "Result " + std::string(negation ? "isn't" : "is") +
            " mobile friendly") {
  return Matches(Field(&DistillabilityResult::is_mobile_friendly, true))(arg);
}

using DistillablePageUtilsBrowserTestAlways =
    TestOption<switches::reader_mode_heuristics::kAlwaysTrue>;

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAlways,
                       AllRealPathsCallObserverOnceWithIsDistillable) {
  for (unsigned i = 0; i < sizeof(kAllPaths) / sizeof(kAllPaths[0]); ++i) {
    testing::InSequence dummy;
    EXPECT_CALL(holder_, OnResult(AllOf(IsDistillable(), IsLast())))
        .WillOnce(testing::InvokeWithoutArgs(this, &TestOption::QuitSoon));
    NavigateAndWait(kAllPaths[i], base::TimeDelta());
    EXPECT_THAT(GetLatestResult(web_contents_),
                Optional(AllOf(IsDistillable(), IsLast())));
  }
}

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAlways,
                       LocalUrlsDoNotCallObserver) {
  EXPECT_CALL(holder_, OnResult(_)).Times(0);
  NavigateAndWait("about:blank", kWaitNoExpectedCall);
  EXPECT_EQ(GetLatestResult(web_contents_), std::nullopt);
}

using DistillablePageUtilsBrowserTestNone =
    TestOption<switches::reader_mode_heuristics::kNone>;

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestNone, NeverCallObserver) {
  EXPECT_CALL(holder_, OnResult(_)).Times(0);
  NavigateAndWait(kSimpleArticlePath, kWaitNoExpectedCall);
  EXPECT_EQ(GetLatestResult(web_contents_), std::nullopt);
}

using DistillablePageUtilsBrowserTestOGArticle =
    TestOption<switches::reader_mode_heuristics::kOGArticle>;

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestOGArticle,
                       ArticlesCallObserverOnceWithIsDistillable) {
  EXPECT_CALL(holder_, OnResult(AllOf(IsDistillable(), IsLast())))
      .WillOnce(testing::InvokeWithoutArgs(this, &TestOption::QuitSoon));
  NavigateAndWait(kArticlePath, base::TimeDelta());
  EXPECT_THAT(GetLatestResult(web_contents_),
              Optional(AllOf(IsDistillable(), IsLast())));
}

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestOGArticle,
                       NonArticleCallsObserverOnceWithIsNotDistillable) {
  EXPECT_CALL(holder_, OnResult(AllOf(Not(IsDistillable()), IsLast())))
      .WillOnce(testing::InvokeWithoutArgs(this, &TestOption::QuitSoon));
  NavigateAndWait(kNonArticlePath, base::TimeDelta());
  EXPECT_THAT(GetLatestResult(web_contents_),
              Optional(AllOf(Not(IsDistillable()), IsLast())));
}

using DistillablePageUtilsBrowserTestAdaboost =
    TestOption<switches::reader_mode_heuristics::kAdaBoost>;

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAdaboost,
                       SimpleArticlesCallObserverTwiceWithIsDistillable) {
  auto paths = std::to_array<const char*>(
      {kSimpleArticlePath, kSimpleArticleIFramePath});
  for (unsigned i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) {
    testing::InSequence dummy;
    EXPECT_CALL(holder_, OnResult(AllOf(IsDistillable(), Not(IsLast()),
                                        Not(IsMobileFriendly()))))
        .Times(1);
    EXPECT_CALL(holder_, OnResult(AllOf(IsDistillable(), IsLast(),
                                        Not(IsMobileFriendly()))))
        .WillOnce(testing::InvokeWithoutArgs(this, &TestOption::QuitSoon));
    NavigateAndWait(paths[i], base::TimeDelta());

    EXPECT_THAT(
        GetLatestResult(web_contents_),
        Optional(AllOf(IsDistillable(), IsLast(), Not(IsMobileFriendly()))));
  }
}

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAdaboost,
                       NonArticleCallsObserverTwiceWithIsNotDistillable) {
  testing::InSequence dummy;
  EXPECT_CALL(holder_, OnResult(AllOf(Not(IsDistillable()), Not(IsLast()),
                                      Not(IsMobileFriendly()))))
      .Times(1);
  EXPECT_CALL(holder_, OnResult(AllOf(Not(IsDistillable()), IsLast(),
                                      Not(IsMobileFriendly()))))
      .WillOnce(testing::InvokeWithoutArgs(this, &TestOption::QuitSoon));
  NavigateAndWait(kNonArticlePath, base::TimeDelta());
  EXPECT_THAT(
      GetLatestResult(web_contents_),
      Optional(AllOf(Not(IsDistillable()), IsLast(), Not(IsMobileFriendly()))));
}

using DistillablePageUtilsBrowserTestAllArticles =
    TestOption<switches::reader_mode_heuristics::kAllArticles>;

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAllArticles,
                       SimpleArticlesCallObserverTwiceWithIsDistillable) {
  auto paths = std::to_array<const char*>(
      {kSimpleArticlePath, kSimpleArticleIFramePath});
  for (unsigned i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) {
    testing::InSequence dummy;
    EXPECT_CALL(holder_, OnResult(AllOf(IsDistillable(), Not(IsLast()),
                                        Not(IsMobileFriendly()))))
        .Times(1);
    EXPECT_CALL(holder_, OnResult(AllOf(IsDistillable(), IsLast(),
                                        Not(IsMobileFriendly()))))
        .WillOnce(testing::InvokeWithoutArgs(this, &TestOption::QuitSoon));
    NavigateAndWait(paths[i], base::TimeDelta());
    EXPECT_THAT(
        GetLatestResult(web_contents_),
        Optional(AllOf(IsDistillable(), IsLast(), Not(IsMobileFriendly()))));
  }
}

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAllArticles,
                       NonArticlesCallObserverTwiceWithIsNotDistillable) {
  testing::InSequence dummy;
  EXPECT_CALL(holder_, OnResult(AllOf(Not(IsDistillable()), Not(IsLast()),
                                      Not(IsMobileFriendly()))))
      .Times(1);
  EXPECT_CALL(holder_, OnResult(AllOf(Not(IsDistillable()), IsLast(),
                                      Not(IsMobileFriendly()))))
      .WillOnce(testing::InvokeWithoutArgs(this, &TestOption::QuitSoon));
  NavigateAndWait(kNonArticlePath, base::TimeDelta());
  EXPECT_THAT(
      GetLatestResult(web_contents_),
      Optional(AllOf(Not(IsDistillable()), IsLast(), Not(IsMobileFriendly()))));
}

// TODO(crbug.com/40921719): Flaky on Linux MSAN.
#if BUILDFLAG(IS_LINUX) && defined(MEMORY_SANITIZER)
#define MAYBE_ObserverNotCalledAfterRemoval \
  DISABLED_ObserverNotCalledAfterRemoval
#else
#define MAYBE_ObserverNotCalledAfterRemoval ObserverNotCalledAfterRemoval
#endif
IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAllArticles,
                       MAYBE_ObserverNotCalledAfterRemoval) {
  RemoveObserver(web_contents_, &holder_);
  EXPECT_CALL(holder_, OnResult(_)).Times(0);
  NavigateAndWait(kSimpleArticlePath, kWaitNoExpectedCall);
  EXPECT_THAT(
      GetLatestResult(web_contents_),
      Optional(AllOf(IsDistillable(), IsLast(), Not(IsMobileFriendly()))));
}

}  // namespace dom_distiller