1 // 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 IOS_WEB_PUBLIC_TEST_WEB_TEST_H_
6 #define IOS_WEB_PUBLIC_TEST_WEB_TEST_H_
7 
8 #include <memory>
9 
10 #include "ios/web/public/test/fakes/test_browser_state.h"
11 #include "ios/web/public/test/scoped_testing_web_client.h"
12 #include "ios/web/public/test/web_task_environment.h"
13 #include "testing/platform_test.h"
14 
15 namespace web {
16 
17 class BrowserState;
18 class WebClient;
19 class WebTestRenderProcessCrashObserver;
20 
21 // A test fixture for web tests that need a minimum environment set up that
22 // mimics a web embedder.
23 class WebTest : public PlatformTest {
24  protected:
25   explicit WebTest(WebTaskEnvironment::Options options =
26                        WebTaskEnvironment::Options::DEFAULT);
27   WebTest(std::unique_ptr<web::WebClient> web_client,
28           WebTaskEnvironment::Options = WebTaskEnvironment::Options::DEFAULT);
29   ~WebTest() override;
30 
31   // Returns the WebClient that is used for testing.
32   virtual web::WebClient* GetWebClient();
33 
34   // Returns the BrowserState that is used for testing.
35   virtual BrowserState* GetBrowserState();
36 
37   // If called with |true|, prevents the test fixture from automatically failing
38   // when a render process crashes during the test.  This is useful for tests
39   // that intentionally crash the render process.  By default, the WebTest
40   // fixture will fail if a render process crashes.
41   void SetIgnoreRenderProcessCrashesDuringTesting(bool allow);
42 
43   // Sets a SharedURLLoaderFactory for |browser_state_|.
44   void SetSharedURLLoaderFactory(
45       scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory);
46 
47  private:
48   // The WebClient used in tests.
49   ScopedTestingWebClient web_client_;
50   // The threads used for testing.
51   web::WebTaskEnvironment task_environment_;
52   // The browser state used in tests.
53   TestBrowserState browser_state_;
54 
55   // Triggers test failures if a render process dies during the test.
56   std::unique_ptr<WebTestRenderProcessCrashObserver> crash_observer_;
57 };
58 
59 }  // namespace web
60 
61 #endif  // IOS_WEB_PUBLIC_TEST_WEB_TEST_H_
62