1 // Copyright 2014 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.
5 #ifndef IOS_WEB_TEST_WEB_TEST_H_
6 #define IOS_WEB_TEST_WEB_TEST_H_
8 #import <UIKit/UIKit.h>
10 #import "base/mac/scoped_nsobject.h"
11 #include "base/message_loop/message_loop.h"
12 #include "ios/web/public/test/test_browser_state.h"
13 #import "ios/web/public/test/test_web_client.h"
14 #include "ios/web/public/test/test_web_thread_bundle.h"
15 #include "ios/web/public/web_client.h"
16 #import "ios/web/web_state/ui/crw_ui_web_view_web_controller.h"
17 #include "testing/platform_test.h"
19 // A subclass of WebController overridden for testing purposes. Specifically it
20 // overrides the UIWebView delegate method to intercept requests coming from
22 // TODO(jimblackler): remove use of TestWebController entirely.
23 @interface TestWebController
: CRWUIWebViewWebController
25 @
property(nonatomic
, assign
) BOOL interceptRequest
;
26 @
property(nonatomic
, assign
) BOOL requestIntercepted
;
27 @
property(nonatomic
, assign
) BOOL
28 invokeShouldStartLoadWithRequestNavigationTypeDone
;
34 // A test fixture for web tests that need a minimum environment set up that
35 // mimics a web embedder.
36 class WebTest
: public PlatformTest
{
41 // PlatformTest methods.
42 void SetUp() override
;
43 void TearDown() override
;
45 // Returns the BrowserState that is used for testing.
46 BrowserState
* GetBrowserState() { return &browser_state_
; }
49 // The WebClient used in tests.
50 TestWebClient client_
;
51 // The threads used for testing.
52 web::TestWebThreadBundle thread_bundle_
;
53 // The browser state used in tests.
54 TestBrowserState browser_state_
;
59 // An abstract test fixture that sets up a WebControllers that can be loaded
60 // with test HTML and JavaScripts. Concrete subclasses override
61 // |CreateWebController| specifying the test WebController object.
62 // DEPRECATED. Please use either WebTest or WebTestWithWebController instead.
63 // TODO(shreyasv): Remove this after all clients have stopped using it.
65 class WebTestBase
: public WebTest
, public base::MessageLoop::TaskObserver
{
67 ~WebTestBase() override
;
71 void SetUp() override
;
72 void TearDown() override
;
73 // Loads the specified HTML content into the WebController via public APIs.
74 void LoadHtml(NSString
* html
);
75 // Loads the specified HTML content into the WebController via public APIs.
76 void LoadHtml(const std::string
& html
);
77 // Loads |url| into the WebController via public APIs.
78 // Note if anyone uses this to load web pages from the live internet, the
79 // tests can be flaky / dependent on content and behavior beyond our control.
80 // Use this only when it's impossible to test with static HTML using LoadHtml.
81 void LoadURL(const GURL
& url
);
82 // Blocks until both known NSRunLoop-based and known message-loop-based
83 // background tasks have completed
84 void WaitForBackgroundTasks();
85 // Blocks until known NSRunLoop-based have completed, known message-loop-based
86 // background tasks have completed and |condition| evaluates to true.
87 void WaitForCondition(ConditionBlock condition
);
88 // Returns true if WebController message queue is empty.
89 // |WaitForBackgroundTasks| does not return until until the message queue is
91 bool MessageQueueIsEmpty() const;
92 // Evaluates JavaScript and returns result as a string.
93 NSString
* EvaluateJavaScriptAsString(NSString
* script
) const;
94 // Runs the given JavaScript and returns the result as a string. This method
95 // is a drop-in replacement for stringByEvaluatingJavaScriptFromString with
96 // the additional functionality that any JavaScript exceptions are caught and
97 // logged (not dropped silently).
98 NSString
* RunJavaScript(NSString
* script
);
99 // Returns a CRWWebController to be used in tests.
100 virtual CRWWebController
* CreateWebController() = 0;
101 // TaskObserver methods (used when waiting for background tasks).
102 void WillProcessTask(const base::PendingTask
& pending_task
) override
;
103 void DidProcessTask(const base::PendingTask
& pending_task
) override
;
105 // The web controller for testing.
106 base::scoped_nsobject
<CRWWebController
> webController_
;
107 // true if a task has been processed.
108 bool processed_a_task_
;
113 // A test fixture that sets up a WebControllers that can be loaded with test
114 // HTML and JavaScripts. Concrete subclasses override |CreateWebController|
115 // specifying the test WebController object.
116 typedef WebTestBase WebTestWithWebController
;
120 // A test fixtures thats creates a CRWUIWebViewWebController for testing.
121 // DEPRECATED. Please use WebTestWithUIWebViewWebController instead.
122 // TODO(shreyasv): Remove this once all clients have moved over.
124 class UIWebViewWebTest
: public WebTestWithWebController
{
126 // WebTestWithWebController methods.
127 CRWWebController
* CreateWebController() override
;
129 // Invokes JS->ObjC messages directly on the web controller, registering a
130 // human interaction if userIsInteraction==YES. |commands| should be a
131 // stringified message queue.
132 void LoadCommands(NSString
* commands
,
133 const GURL
& origin_url
,
134 BOOL user_is_interacting
);
139 // A test fixtures thats creates a CRWUIWebViewWebController for testing.
140 typedef UIWebViewWebTest WebTestWithUIWebViewWebController
;
144 // A test fixtures thats creates a CRWWKWebViewWebController for testing.
145 // DEPRECATED. Please use WebTestWithWKWebViewWebController instead.
146 // TODO(shreyasv): Remove this once all clients have moved over.
148 class WKWebViewWebTest
: public WebTestWithWebController
{
150 // WebTestWithWebController methods.
151 CRWWebController
* CreateWebController() override
;
156 // A test fixtures thats creates a CRWWKWebViewWebController for testing.
157 typedef WKWebViewWebTest WebTestWithWKWebViewWebController
;
161 #endif // IOS_WEB_TEST_WEB_TEST_H_