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 #include "ios/web/public/test/test_web_thread_bundle.h"
14 #include "ios/web/public/web_client.h"
15 #import "ios/web/web_state/ui/crw_ui_web_view_web_controller.h"
16 #include "testing/platform_test.h"
18 // A subclass of WebController overridden for testing purposes. Specifically it
19 // overrides the UIWebView delegate method to intercept requests coming from
21 // TODO(jimblackler): remove use of TestWebController entirely.
22 @interface TestWebController
: CRWUIWebViewWebController
24 @
property(nonatomic
, assign
) BOOL interceptRequest
;
25 @
property(nonatomic
, assign
) BOOL requestIntercepted
;
26 @
property(nonatomic
, assign
) BOOL
27 invokeShouldStartLoadWithRequestNavigationTypeDone
;
33 // An abstract class of tests that need to create real WebControllers that can
34 // be loaded with test HTML and JavaScripts. Concrete subclasses override
35 // |CreateWebController| specifying the test WebController object.
36 class WebTestBase
: public PlatformTest
,
37 public base::MessageLoop::TaskObserver
{
39 ~WebTestBase() override
;
43 void SetUp() override
;
44 void TearDown() override
;
45 // Loads the specified HTML content into the WebController via public APIs.
46 void LoadHtml(NSString
* html
);
47 // Loads the specified HTML content into the WebController via public APIs.
48 void LoadHtml(const std::string
& html
);
49 // Loads |url| into the WebController via public APIs.
50 // Note if anyone uses this to load web pages from the live internet, the
51 // tests can be flaky / dependent on content and behavior beyond our control.
52 // Use this only when it's impossible to test with static HTML using LoadHtml.
53 void LoadURL(const GURL
& url
);
54 // Blocks until both known NSRunLoop-based and known message-loop-based
55 // background tasks have completed
56 void WaitForBackgroundTasks();
57 // Blocks until known NSRunLoop-based have completed, known message-loop-based
58 // background tasks have completed and |condition| evaluates to true.
59 void WaitForCondition(ConditionBlock condition
);
60 // Returns true if WebController message queue is empty.
61 // |WaitForBackgroundTasks| does not return until until the message queue is
63 bool MessageQueueIsEmpty() const;
64 // Evaluates JavaScript and returns result as a string.
65 NSString
* EvaluateJavaScriptAsString(NSString
* script
) const;
66 // Runs the given JavaScript and returns the result as a string. This method
67 // is a drop-in replacement for stringByEvaluatingJavaScriptFromString with
68 // the additional functionality that any JavaScript exceptions are caught and
69 // logged (not dropped silently).
70 NSString
* RunJavaScript(NSString
* script
);
71 // Returns a CRWWebController to be used in tests.
72 virtual CRWWebController
* CreateWebController() = 0;
73 // TaskObserver methods (used when waiting for background tasks).
74 void WillProcessTask(const base::PendingTask
& pending_task
) override
;
75 void DidProcessTask(const base::PendingTask
& pending_task
) override
;
77 // The threads used for testing.
78 web::TestWebThreadBundle thread_bundle_
;
79 // The web controller for testing.
80 base::scoped_nsobject
<CRWWebController
> webController_
;
81 // true if a task has been processed.
82 bool processed_a_task_
;
83 // The WebClient used in tests.
84 scoped_ptr
<web::WebClient
> client_
;
85 // The browser state used in tests.
86 TestBrowserState browser_state_
;
91 // A concrete test class that is needed to create a real
92 // CRWUIWebViewWebController.
93 class UIWebViewWebTest
: public WebTestBase
{
95 CRWWebController
* CreateWebController() override
;
96 // Invokes JS->ObjC messages directly on the web controller, registering a
97 // human interaction if userIsInteraction==YES. |commands| should be a
98 // stringified message queue.
99 void LoadCommands(NSString
* commands
,
100 const GURL
& origin_url
,
101 BOOL user_is_interacting
);
106 // A concrete test class that is needed to create a real
107 // CRWWKWebViewWebController.
108 class WKWebViewWebTest
: public WebTestBase
{
110 CRWWebController
* CreateWebController() override
;
115 #endif // IOS_WEB_TEST_WEB_TEST_H_