| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_TEST_WEB_TEST_H_ |
| 6 #define IOS_WEB_TEST_WEB_TEST_H_ | 6 #define IOS_WEB_TEST_WEB_TEST_H_ |
| 7 | 7 |
| 8 #import <UIKit/UIKit.h> | 8 // TODO(crbug.com/619076): Remove this file once downsteam clients stop using it |
| 9 | 9 |
| 10 #import "base/ios/block_types.h" | 10 #import "ios/web/public/test/web_test_with_web_state.h" |
| 11 #import "base/ios/weak_nsobject.h" | 11 #import "ios/web/test/web_test_with_web_controller.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 | |
| 22 namespace web { | |
| 23 | |
| 24 // A test fixture for web tests that need a minimum environment set up that | |
| 25 // mimics a web embedder. | |
| 26 class WebTest : public PlatformTest { | |
| 27 protected: | |
| 28 WebTest(); | |
| 29 ~WebTest() override; | |
| 30 | |
| 31 // PlatformTest methods. | |
| 32 void SetUp() override; | |
| 33 void TearDown() override; | |
| 34 | |
| 35 // Returns the WebClient that is used for testing. | |
| 36 TestWebClient* GetWebClient(); | |
| 37 | |
| 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. | |
| 64 void LoadHtml(NSString* html); | |
| 65 // Loads the specified HTML content into the WebController via public APIs. | |
| 66 void LoadHtml(const std::string& html); | |
| 67 // Loads |url| into the WebController via public APIs. | |
| 68 // 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. | |
| 70 // Use this only when it's impossible to test with static HTML using LoadHtml. | |
| 71 void LoadURL(const GURL& url); | |
| 72 // Blocks until both known NSRunLoop-based and known message-loop-based | |
| 73 // background tasks have completed | |
| 74 void WaitForBackgroundTasks(); | |
| 75 // Blocks until known NSRunLoop-based have completed, known message-loop-based | |
| 76 // background tasks have completed and |condition| evaluates to true. | |
| 77 void WaitForCondition(ConditionBlock condition); | |
| 78 // Evaluates JavaScript and returns result as a string. | |
| 79 // DEPRECATED. TODO(crbug.com/595761): Remove this API. | |
| 80 NSString* EvaluateJavaScriptAsString(NSString* script); | |
| 81 // Synchronously executes JavaScript and returns result as id. | |
| 82 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 | |
| 87 // Returns web state for this web controller. | |
| 88 web::WebState* web_state(); | |
| 89 const web::WebState* web_state() const; | |
| 90 | |
| 91 // The web controller for testing. | |
| 92 base::WeakNSObject<CRWWebController> webController_; | |
| 93 // true if a task has been processed. | |
| 94 bool processed_a_task_; | |
| 95 | |
| 96 private: | |
| 97 // 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 | |
| 99 // 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. | |
| 101 bool ResetPageIfNavigationStalled(NSString* load_check); | |
| 102 // Creates a unique HTML element to look for in | |
| 103 // ResetPageIfNavigationStalled(). | |
| 104 NSString* CreateLoadCheck(); | |
| 105 // The web state for testing. | |
| 106 std::unique_ptr<WebStateImpl> web_state_impl_; | |
| 107 }; | |
| 108 | |
| 109 } // namespace web | |
| 110 | 12 |
| 111 #endif // IOS_WEB_TEST_WEB_TEST_H_ | 13 #endif // IOS_WEB_TEST_WEB_TEST_H_ |
| OLD | NEW |