| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef IOS_WEB_TEST_WEB_TEST_H_ | 5 #ifndef IOS_WEB_PUBLIC_TEST_WEB_TEST_WITH_WEB_STATE_H_ |
| 6 #define IOS_WEB_TEST_WEB_TEST_H_ | 6 #define IOS_WEB_PUBLIC_TEST_WEB_TEST_WITH_WEB_STATE_H_ |
| 7 | 7 |
| 8 #import <UIKit/UIKit.h> | 8 #include "base/ios/block_types.h" |
| 9 #import "base/ios/weak_nsobject.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "ios/web/public/test/web_test.h" |
| 12 #include "url/gurl.h" |
| 9 | 13 |
| 10 #import "base/ios/block_types.h" | 14 @class CRWWebController; |
| 11 #import "base/ios/weak_nsobject.h" | |
| 12 #import "base/mac/scoped_nsobject.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "ios/web/public/test/scoped_testing_web_client.h" | |
| 15 #include "ios/web/public/test/test_browser_state.h" | |
| 16 #import "ios/web/public/test/test_web_client.h" | |
| 17 #include "ios/web/public/test/test_web_thread_bundle.h" | |
| 18 #include "ios/web/public/web_client.h" | |
| 19 #import "ios/web/web_state/ui/crw_web_controller.h" | |
| 20 #include "testing/platform_test.h" | |
| 21 | 15 |
| 22 namespace web { | 16 namespace web { |
| 23 | 17 |
| 24 // A test fixture for web tests that need a minimum environment set up that | 18 class WebState; |
| 25 // mimics a web embedder. | 19 |
| 26 class WebTest : public PlatformTest { | 20 // Base test fixture that provides WebState for testing. |
| 21 class WebTestWithWebState : public WebTest, |
| 22 public base::MessageLoop::TaskObserver { |
| 27 protected: | 23 protected: |
| 28 WebTest(); | 24 WebTestWithWebState(); |
| 29 ~WebTest() override; | 25 ~WebTestWithWebState() override; |
| 30 | 26 |
| 31 // PlatformTest methods. | 27 // WebTest overrides. |
| 32 void SetUp() override; | 28 void SetUp() override; |
| 33 void TearDown() override; | 29 void TearDown() override; |
| 34 | 30 |
| 35 // Returns the WebClient that is used for testing. | 31 // base::MessageLoop::TaskObserver overrides. |
| 36 TestWebClient* GetWebClient(); | 32 void WillProcessTask(const base::PendingTask& pending_task) override; |
| 33 void DidProcessTask(const base::PendingTask& pending_task) override; |
| 37 | 34 |
| 38 // Returns the BrowserState that is used for testing. | |
| 39 virtual BrowserState* GetBrowserState(); | |
| 40 | |
| 41 private: | |
| 42 // The WebClient used in tests. | |
| 43 ScopedTestingWebClient web_client_; | |
| 44 // The threads used for testing. | |
| 45 web::TestWebThreadBundle thread_bundle_; | |
| 46 // The browser state used in tests. | |
| 47 TestBrowserState browser_state_; | |
| 48 }; | |
| 49 | |
| 50 #pragma mark - | |
| 51 | |
| 52 // An abstract test fixture that sets up a WebControllers that can be loaded | |
| 53 // with test HTML and JavaScripts. | |
| 54 class WebTestWithWebController : public WebTest, | |
| 55 public base::MessageLoop::TaskObserver { | |
| 56 public: | |
| 57 ~WebTestWithWebController() override; | |
| 58 | |
| 59 protected: | |
| 60 WebTestWithWebController(); | |
| 61 void SetUp() override; | |
| 62 void TearDown() override; | |
| 63 // Loads the specified HTML content into the WebController via public APIs. | 35 // Loads the specified HTML content into the WebController via public APIs. |
| 64 void LoadHtml(NSString* html); | 36 void LoadHtml(NSString* html); |
| 65 // Loads the specified HTML content into the WebController via public APIs. | 37 // Loads the specified HTML content into the WebController via public APIs. |
| 66 void LoadHtml(const std::string& html); | 38 void LoadHtml(const std::string& html); |
| 67 // Loads |url| into the WebController via public APIs. | 39 // Loads |url| into the WebController via public APIs. |
| 68 // Note if anyone uses this to load web pages from the live internet, the | 40 // Note if anyone uses this to load web pages from the live internet, the |
| 69 // tests can be flaky / dependent on content and behavior beyond our control. | 41 // tests can be flaky / dependent on content and behavior beyond our control. |
| 70 // Use this only when it's impossible to test with static HTML using LoadHtml. | 42 // Use this only when it's impossible to test with static HTML using LoadHtml. |
| 71 void LoadURL(const GURL& url); | 43 void LoadURL(const GURL& url); |
| 72 // Blocks until both known NSRunLoop-based and known message-loop-based | 44 // Blocks until both known NSRunLoop-based and known message-loop-based |
| 73 // background tasks have completed | 45 // background tasks have completed |
| 74 void WaitForBackgroundTasks(); | 46 void WaitForBackgroundTasks(); |
| 75 // Blocks until known NSRunLoop-based have completed, known message-loop-based | 47 // Blocks until known NSRunLoop-based have completed, known message-loop-based |
| 76 // background tasks have completed and |condition| evaluates to true. | 48 // background tasks have completed and |condition| evaluates to true. |
| 77 void WaitForCondition(ConditionBlock condition); | 49 void WaitForCondition(ConditionBlock condition); |
| 78 // Evaluates JavaScript and returns result as a string. | 50 // Evaluates JavaScript and returns result as a string. |
| 79 // DEPRECATED. TODO(crbug.com/595761): Remove this API. | 51 // DEPRECATED. TODO(crbug.com/595761): Remove this API. |
| 80 NSString* EvaluateJavaScriptAsString(NSString* script); | 52 NSString* EvaluateJavaScriptAsString(NSString* script); |
| 81 // Synchronously executes JavaScript and returns result as id. | 53 // Synchronously executes JavaScript and returns result as id. |
| 82 id ExecuteJavaScript(NSString* script); | 54 id ExecuteJavaScript(NSString* script); |
| 83 // TaskObserver methods (used when waiting for background tasks). | |
| 84 void WillProcessTask(const base::PendingTask& pending_task) override; | |
| 85 void DidProcessTask(const base::PendingTask& pending_task) override; | |
| 86 | 55 |
| 87 // Returns web state for this web controller. | 56 // Returns web state for this web controller. |
| 88 web::WebState* web_state(); | 57 web::WebState* web_state(); |
| 89 const web::WebState* web_state() const; | 58 const web::WebState* web_state() const; |
| 90 | 59 |
| 91 // The web controller for testing. | 60 // The web controller for testing. |
| 61 // TODO(crbug.com/619076): Remove this ivar. |
| 92 base::WeakNSObject<CRWWebController> webController_; | 62 base::WeakNSObject<CRWWebController> webController_; |
| 93 // true if a task has been processed. | 63 // true if a task has been processed. |
| 94 bool processed_a_task_; | 64 bool processed_a_task_; |
| 95 | 65 |
| 96 private: | 66 private: |
| 97 // LoadURL() for data URLs sometimes lock up navigation, so if the loaded page | 67 // LoadURL() for data URLs sometimes lock up navigation, so if the loaded page |
| 98 // is not the one expected, reset the web view. In some cases, document or | 68 // is not the one expected, reset the web view. In some cases, document or |
| 99 // document.body does not exist either; also reset in those cases. | 69 // document.body does not exist either; also reset in those cases. |
| 100 // Returns true if a reset occurred. One may want to load the page again. | 70 // Returns true if a reset occurred. One may want to load the page again. |
| 101 bool ResetPageIfNavigationStalled(NSString* load_check); | 71 bool ResetPageIfNavigationStalled(NSString* load_check); |
| 102 // Creates a unique HTML element to look for in | 72 // Creates a unique HTML element to look for in |
| 103 // ResetPageIfNavigationStalled(). | 73 // ResetPageIfNavigationStalled(). |
| 104 NSString* CreateLoadCheck(); | 74 NSString* CreateLoadCheck(); |
| 105 // The web state for testing. | 75 // The web state for testing. |
| 106 std::unique_ptr<WebStateImpl> web_state_impl_; | 76 std::unique_ptr<WebState> web_state_; |
| 107 }; | 77 }; |
| 108 | 78 |
| 109 } // namespace web | 79 } // namespace web |
| 110 | 80 |
| 111 #endif // IOS_WEB_TEST_WEB_TEST_H_ | 81 #endif // IOS_WEB_PUBLIC_TEST_WEB_TEST_WITH_WEB_STATE_H_ |
| OLD | NEW |