blob: 3db973b02e5490ca3c4a7107a56b9a2a168ee461 [file] [log] [blame]
skyostilfe116162016-02-26 20:53:331// Copyright 2016 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#ifndef HEADLESS_TEST_HEADLESS_BROWSER_TEST_H_
6#define HEADLESS_TEST_HEADLESS_BROWSER_TEST_H_
7
skyostilab258d6d2016-04-06 16:32:578#include <memory>
eseckler9ef54762016-06-03 17:28:389#include <string>
10
skyostilfe116162016-02-26 20:53:3311#include "content/public/test/browser_test_base.h"
altimineaffa8e02016-11-08 23:56:3112#include "headless/public/devtools/domains/network.h"
13#include "headless/public/devtools/domains/page.h"
skyostilf52fa5b2016-03-15 11:38:4514#include "headless/public/headless_browser.h"
eseckler9ef54762016-06-03 17:28:3815#include "headless/public/headless_web_contents.h"
skyostilfe116162016-02-26 20:53:3316
skyostilab258d6d2016-04-06 16:32:5717namespace base {
18class RunLoop;
19}
20
skyostilfe116162016-02-26 20:53:3321namespace headless {
skyostila65d5e612016-06-01 18:04:2122namespace runtime {
23class EvaluateResult;
24}
eseckler9ef54762016-06-03 17:28:3825class HeadlessDevToolsClient;
skyostilfe116162016-02-26 20:53:3326
skyostil25c2c0122016-06-09 12:54:1227// A utility class for asynchronously observing load events.
28class LoadObserver : public page::Observer, public network::Observer {
29 public:
30 LoadObserver(HeadlessDevToolsClient* devtools_client, base::Closure callback);
31 ~LoadObserver() override;
32
33 // page::Observer implementation:
34 void OnLoadEventFired(const page::LoadEventFiredParams& params) override;
35
36 // network::Observer implementation:
37 void OnResponseReceived(
38 const network::ResponseReceivedParams& params) override;
39
40 bool navigation_succeeded() const { return navigation_succeeded_; }
41
42 private:
43 base::Closure callback_;
44 HeadlessDevToolsClient* devtools_client_; // Not owned.
45
46 bool navigation_succeeded_;
47
48 DISALLOW_COPY_AND_ASSIGN(LoadObserver);
49};
50
skyostilfe116162016-02-26 20:53:3351// Base class for tests which require a full instance of the headless browser.
52class HeadlessBrowserTest : public content::BrowserTestBase {
skyostilab258d6d2016-04-06 16:32:5753 public:
54 // Notify that an asynchronous test is now complete and the test runner should
55 // exit.
56 void FinishAsynchronousTest();
57
skyostilfe116162016-02-26 20:53:3358 protected:
59 HeadlessBrowserTest();
60 ~HeadlessBrowserTest() override;
61
62 // BrowserTestBase:
63 void RunTestOnMainThreadLoop() override;
64 void SetUpOnMainThread() override;
65 void TearDownOnMainThread() override;
66
skyostilab258d6d2016-04-06 16:32:5767 // Run an asynchronous test in a nested run loop. The caller should call
68 // FinishAsynchronousTest() to notify that the test should finish.
69 void RunAsynchronousTest();
70
71 // Synchronously waits for a tab to finish loading.
72 bool WaitForLoad(HeadlessWebContents* web_contents);
skyostilf52fa5b2016-03-15 11:38:4573
skyostila65d5e612016-06-01 18:04:2174 // Synchronously evaluates a script and returns the result.
75 std::unique_ptr<runtime::EvaluateResult> EvaluateScript(
76 HeadlessWebContents* web_contents,
77 const std::string& script);
78
skyostilfe116162016-02-26 20:53:3379 protected:
80 // Returns the browser for the test.
81 HeadlessBrowser* browser() const;
82
eseckler9ff09982016-11-15 09:18:2583 // Returns the options used by the browser. Modify with caution, since some
84 // options only take effect if they were set before browser creation.
85 HeadlessBrowser::Options* options() const;
86
skyostilfe116162016-02-26 20:53:3387 private:
skyostilab258d6d2016-04-06 16:32:5788 std::unique_ptr<base::RunLoop> run_loop_;
89
skyostilfe116162016-02-26 20:53:3390 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowserTest);
91};
92
eseckler9ef54762016-06-03 17:28:3893#define HEADLESS_ASYNC_DEVTOOLED_TEST_F(TEST_FIXTURE_NAME) \
94 IN_PROC_BROWSER_TEST_F(TEST_FIXTURE_NAME, RunAsyncTest) { RunTest(); } \
95 class AsyncHeadlessBrowserTestNeedsSemicolon##TEST_FIXTURE_NAME {}
96
97// Base class for tests that require access to a DevToolsClient. Subclasses
98// should override the RunDevTooledTest() method, which is called asynchronously
99// when the DevToolsClient is ready.
100class HeadlessAsyncDevTooledBrowserTest : public HeadlessBrowserTest,
101 public HeadlessWebContents::Observer {
102 public:
103 HeadlessAsyncDevTooledBrowserTest();
104 ~HeadlessAsyncDevTooledBrowserTest() override;
105
106 // HeadlessWebContentsObserver implementation:
107 void DevToolsTargetReady() override;
alexclarke5777c83e2016-11-18 17:42:16108 void RenderProcessExited(base::TerminationStatus status,
109 int exit_code) override;
eseckler9ef54762016-06-03 17:28:38110
111 // Implemented by tests and used to send request(s) to DevTools. Subclasses
112 // need to ensure that FinishAsynchronousTest() is called after response(s)
113 // are processed (e.g. in a callback).
114 virtual void RunDevTooledTest() = 0;
115
116 protected:
117 void RunTest();
118
altiminbf875c92016-08-04 17:09:07119 HeadlessBrowserContext* browser_context_; // Not owned.
eseckler9ef54762016-06-03 17:28:38120 HeadlessWebContents* web_contents_;
121 std::unique_ptr<HeadlessDevToolsClient> devtools_client_;
alexclarke5777c83e2016-11-18 17:42:16122 bool render_process_exited_;
eseckler9ef54762016-06-03 17:28:38123};
124
skyostilfe116162016-02-26 20:53:33125} // namespace headless
126
127#endif // HEADLESS_TEST_HEADLESS_BROWSER_TEST_H_