chromium/chrome/browser/dom_distiller/distillable_page_utils_browsertest.cc

// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

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

#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/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 {

_;
AllOf;
Field;
InvokeWithoutArgs;
Not;
Optional;
Pointee;
SizeIs;

const char kSimpleArticlePath[] =;
const char kSimpleArticleIFramePath[] =;
const char kArticlePath[] =;
const char kNonArticlePath[] =;

const char* kAllPaths[] =;

class MockObserver : public DistillabilityObserver {};

// 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 =;

// 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 =;

}  // namespace

template <const char Option[]>
class TestOption : public InProcessBrowserTest {};

MATCHER(IsDistillable,
        "Result " + std::string(negation ? "isn't" : "is") + " distillable") {}

MATCHER(IsLast, "Result " + std::string(negation ? "isn't" : "is") + " last") {}

MATCHER(IsMobileFriendly,
        "Result " + std::string(negation ? "isn't" : "is") +
            " mobile friendly") {}

DistillablePageUtilsBrowserTestAlways;

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAlways,
                       AllRealPathsCallObserverOnceWithIsDistillable) {}

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAlways,
                       LocalUrlsDoNotCallObserver) {}

DistillablePageUtilsBrowserTestNone;

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestNone, NeverCallObserver) {}

DistillablePageUtilsBrowserTestOGArticle;

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestOGArticle,
                       ArticlesCallObserverOnceWithIsDistillable) {}

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestOGArticle,
                       NonArticleCallsObserverOnceWithIsNotDistillable) {}

DistillablePageUtilsBrowserTestAdaboost;

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAdaboost,
                       SimpleArticlesCallObserverTwiceWithIsDistillable) {}

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAdaboost,
                       NonArticleCallsObserverTwiceWithIsNotDistillable) {}

DistillablePageUtilsBrowserTestAllArticles;

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAllArticles,
                       SimpleArticlesCallObserverTwiceWithIsDistillable) {}

IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAllArticles,
                       NonArticlesCallObserverTwiceWithIsNotDistillable) {}

// TODO(crbug.com/40921719): Flaky on Linux MSAN.
#if BUILDFLAG(IS_LINUX) && defined(MEMORY_SANITIZER)
#define MAYBE_ObserverNotCalledAfterRemoval
#else
#define MAYBE_ObserverNotCalledAfterRemoval
#endif
IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAllArticles,
                       MAYBE_ObserverNotCalledAfterRemoval) {}

}  // namespace dom_distiller