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

headless / test / headless_devtooled_browsertest.h [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef HEADLESS_TEST_HEADLESS_DEVTOOLED_BROWSERTEST_H_
#define HEADLESS_TEST_HEADLESS_DEVTOOLED_BROWSERTEST_H_

#include "base/memory/raw_ptr.h"
#include "base/values.h"
#include "components/devtools/simple_devtools_protocol_client/simple_devtools_protocol_client.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test_base.h"
#include "headless/public/headless_browser.h"
#include "headless/public/headless_web_contents.h"
#include "headless/test/headless_browser_test.h"

namespace headless {

// Base class for tests that require access to a DevToolsClient. Subclasses
// should override the RunDevTooledTest() method, which is called asynchronously
// when the DevToolsClient is ready.
class HeadlessDevTooledBrowserTest : public HeadlessBrowserTest,
                                     public content::WebContentsObserver {
 public:
  HeadlessDevTooledBrowserTest();
  ~HeadlessDevTooledBrowserTest() override;

 protected:
  // content::WebContentsObserver implementation:
  void RenderViewReady() override;

  // content::WebContentsObserver implementation.
  void PrimaryMainFrameRenderProcessGone(
      base::TerminationStatus status) override;

  // Implemented by tests and used to send request(s) to DevTools. Subclasses
  // need to ensure that FinishAsynchronousTest() is called after response(s)
  // are processed (e.g. in a callback).
  virtual void RunDevTooledTest() = 0;

  // These are called just before and right after calling RunAsynchronousTest()
  virtual void PreRunAsynchronousTest() {}
  virtual void PostRunAsynchronousTest() {}
  virtual void DevToolsTargetReady() {}

  // Whether to enable BeginFrameControl when creating |web_contents_|.
  virtual bool GetEnableBeginFrameControl();

  // Allows the HeadlessBrowserContext used in testing to be customized.
  virtual void CustomizeHeadlessBrowserContext(
      HeadlessBrowserContext::Builder& builder);

  // Allows the HeadlessWebContents used in testing to be customized.
  virtual void CustomizeHeadlessWebContents(
      HeadlessWebContents::Builder& builder);

  void RunTest();

  raw_ptr<HeadlessBrowserContext> browser_context_;
  raw_ptr<HeadlessWebContents> web_contents_;
  bool had_render_view_ready_ = false;
  simple_devtools_protocol_client::SimpleDevToolsProtocolClient
      devtools_client_;
  simple_devtools_protocol_client::SimpleDevToolsProtocolClient
      browser_devtools_client_;
};

#define HEADLESS_DEVTOOLED_TEST_F(TEST_FIXTURE_NAME)                     \
  IN_PROC_BROWSER_TEST_F(TEST_FIXTURE_NAME, RunAsyncTest) { RunTest(); } \
  class HeadlessDevTooledBrowserTestNeedsSemicolon##TEST_FIXTURE_NAME {}

#define HEADLESS_DEVTOOLED_TEST_P(TEST_FIXTURE_NAME)                     \
  IN_PROC_BROWSER_TEST_P(TEST_FIXTURE_NAME, RunAsyncTest) { RunTest(); } \
  class HeadlessDevTooledBrowserTestNeedsSemicolon##TEST_FIXTURE_NAME {}

#define DISABLED_HEADLESS_DEVTOOLED_TEST_F(TEST_FIXTURE_NAME)        \
  IN_PROC_BROWSER_TEST_F(TEST_FIXTURE_NAME, DISABLED_RunAsyncTest) { \
    RunTest();                                                       \
  }                                                                  \
  class HeadlessDevTooledBrowserTestNeedsSemicolon##TEST_FIXTURE_NAME {}

#define DISABLED_HEADLESS_DEVTOOLED_TEST_P(TEST_FIXTURE_NAME)        \
  IN_PROC_BROWSER_TEST_P(TEST_FIXTURE_NAME, DISABLED_RunAsyncTest) { \
    RunTest();                                                       \
  }                                                                  \
  class HeadlessDevTooledBrowserTestNeedsSemicolon##TEST_FIXTURE_NAME {}

}  // namespace headless

#endif  // HEADLESS_TEST_HEADLESS_DEVTOOLED_BROWSERTEST_H_