Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / web / test / web_test.h
blob01aec2a70a4c7b90148c1500c500c59875c28db8
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
21 // core.js.
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;
30 @end
32 namespace web {
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 {
37 protected:
38 WebTest();
39 ~WebTest() override;
41 // PlatformTest methods.
42 void SetUp() override;
43 void TearDown() override;
45 // Returns the WebClient that is used for testing.
46 TestWebClient* GetWebClient() { return &client_; }
48 // Returns the BrowserState that is used for testing.
49 BrowserState* GetBrowserState() { return &browser_state_; }
51 private:
52 // The WebClient used in tests.
53 TestWebClient client_;
54 // The threads used for testing.
55 web::TestWebThreadBundle thread_bundle_;
56 // The browser state used in tests.
57 TestBrowserState browser_state_;
60 #pragma mark -
62 // An abstract test fixture that sets up a WebControllers that can be loaded
63 // with test HTML and JavaScripts. Concrete subclasses override
64 // |CreateWebController| specifying the test WebController object.
65 class WebTestWithWebController : public WebTest,
66 public base::MessageLoop::TaskObserver {
67 public:
68 ~WebTestWithWebController() override;
70 protected:
71 WebTestWithWebController();
72 void SetUp() override;
73 void TearDown() override;
74 // Loads the specified HTML content into the WebController via public APIs.
75 void LoadHtml(NSString* html);
76 // Loads the specified HTML content into the WebController via public APIs.
77 void LoadHtml(const std::string& html);
78 // Loads |url| into the WebController via public APIs.
79 // Note if anyone uses this to load web pages from the live internet, the
80 // tests can be flaky / dependent on content and behavior beyond our control.
81 // Use this only when it's impossible to test with static HTML using LoadHtml.
82 void LoadURL(const GURL& url);
83 // Blocks until both known NSRunLoop-based and known message-loop-based
84 // background tasks have completed
85 void WaitForBackgroundTasks();
86 // Blocks until known NSRunLoop-based have completed, known message-loop-based
87 // background tasks have completed and |condition| evaluates to true.
88 void WaitForCondition(ConditionBlock condition);
89 // Returns true if WebController message queue is empty.
90 // |WaitForBackgroundTasks| does not return until until the message queue is
91 // empty.
92 bool MessageQueueIsEmpty() const;
93 // Evaluates JavaScript and returns result as a string.
94 NSString* EvaluateJavaScriptAsString(NSString* script) const;
95 // Runs the given JavaScript and returns the result as a string. This method
96 // is a drop-in replacement for stringByEvaluatingJavaScriptFromString with
97 // the additional functionality that any JavaScript exceptions are caught and
98 // logged (not dropped silently).
99 NSString* RunJavaScript(NSString* script);
100 // Returns a CRWWebController to be used in tests.
101 virtual CRWWebController* CreateWebController() = 0;
102 // TaskObserver methods (used when waiting for background tasks).
103 void WillProcessTask(const base::PendingTask& pending_task) override;
104 void DidProcessTask(const base::PendingTask& pending_task) override;
106 // The web controller for testing.
107 base::scoped_nsobject<CRWWebController> webController_;
108 // true if a task has been processed.
109 bool processed_a_task_;
112 #pragma mark -
114 // A test fixtures thats creates a CRWUIWebViewWebController for testing.
115 class WebTestWithUIWebViewWebController : public WebTestWithWebController {
116 protected:
117 // WebTestWithWebController methods.
118 CRWWebController* CreateWebController() override;
120 // Invokes JS->ObjC messages directly on the web controller, registering a
121 // human interaction if userIsInteraction==YES. |commands| should be a
122 // stringified message queue.
123 void LoadCommands(NSString* commands,
124 const GURL& origin_url,
125 BOOL user_is_interacting);
128 #pragma mark -
130 // A test fixtures thats creates a CRWWKWebViewWebController for testing.
131 class WebTestWithWKWebViewWebController : public WebTestWithWebController {
132 protected:
133 // WebTestWithWebController methods.
134 CRWWebController* CreateWebController() override;
137 } // namespace web
139 #endif // IOS_WEB_TEST_WEB_TEST_H_