| 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 #import <UIKit/UIKit.h> |
| 9 | 9 |
| 10 #import "base/ios/block_types.h" | 10 #import "base/ios/block_types.h" |
| 11 #import "base/ios/weak_nsobject.h" |
| 11 #import "base/mac/scoped_nsobject.h" | 12 #import "base/mac/scoped_nsobject.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "ios/web/public/test/scoped_testing_web_client.h" | 14 #include "ios/web/public/test/scoped_testing_web_client.h" |
| 14 #include "ios/web/public/test/test_browser_state.h" | 15 #include "ios/web/public/test/test_browser_state.h" |
| 15 #import "ios/web/public/test/test_web_client.h" | 16 #import "ios/web/public/test/test_web_client.h" |
| 16 #include "ios/web/public/test/test_web_thread_bundle.h" | 17 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 17 #include "ios/web/public/web_client.h" | 18 #include "ios/web/public/web_client.h" |
| 18 #import "ios/web/web_state/ui/crw_web_controller.h" | 19 #import "ios/web/web_state/ui/crw_web_controller.h" |
| 19 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
| 20 | 21 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 42 ScopedTestingWebClient web_client_; | 43 ScopedTestingWebClient web_client_; |
| 43 // The threads used for testing. | 44 // The threads used for testing. |
| 44 web::TestWebThreadBundle thread_bundle_; | 45 web::TestWebThreadBundle thread_bundle_; |
| 45 // The browser state used in tests. | 46 // The browser state used in tests. |
| 46 TestBrowserState browser_state_; | 47 TestBrowserState browser_state_; |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 #pragma mark - | 50 #pragma mark - |
| 50 | 51 |
| 51 // An abstract test fixture that sets up a WebControllers that can be loaded | 52 // An abstract test fixture that sets up a WebControllers that can be loaded |
| 52 // with test HTML and JavaScripts. Concrete subclasses override | 53 // with test HTML and JavaScripts. |
| 53 // |CreateWebController| specifying the test WebController object. | |
| 54 class WebTestWithWebController : public WebTest, | 54 class WebTestWithWebController : public WebTest, |
| 55 public base::MessageLoop::TaskObserver { | 55 public base::MessageLoop::TaskObserver { |
| 56 public: | 56 public: |
| 57 ~WebTestWithWebController() override; | 57 ~WebTestWithWebController() override; |
| 58 | 58 |
| 59 protected: | 59 protected: |
| 60 WebTestWithWebController(); | 60 WebTestWithWebController(); |
| 61 void SetUp() override; | 61 void SetUp() override; |
| 62 void TearDown() override; | 62 void TearDown() override; |
| 63 // Loads the specified HTML content into the WebController via public APIs. | 63 // Loads the specified HTML content into the WebController via public APIs. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 75 // Blocks until known NSRunLoop-based have completed, known message-loop-based | 75 // Blocks until known NSRunLoop-based have completed, known message-loop-based |
| 76 // background tasks have completed and |condition| evaluates to true. | 76 // background tasks have completed and |condition| evaluates to true. |
| 77 void WaitForCondition(ConditionBlock condition); | 77 void WaitForCondition(ConditionBlock condition); |
| 78 // Evaluates JavaScript and returns result as a string. | 78 // Evaluates JavaScript and returns result as a string. |
| 79 NSString* EvaluateJavaScriptAsString(NSString* script); | 79 NSString* EvaluateJavaScriptAsString(NSString* script); |
| 80 // Runs the given JavaScript and returns the result as a string. This method | 80 // Runs the given JavaScript and returns the result as a string. This method |
| 81 // is a drop-in replacement for stringByEvaluatingJavaScriptFromString with | 81 // is a drop-in replacement for stringByEvaluatingJavaScriptFromString with |
| 82 // the additional functionality that any JavaScript exceptions are caught and | 82 // the additional functionality that any JavaScript exceptions are caught and |
| 83 // logged (not dropped silently). | 83 // logged (not dropped silently). |
| 84 NSString* RunJavaScript(NSString* script); | 84 NSString* RunJavaScript(NSString* script); |
| 85 // Creates a CRWWebController to be used in tests. | |
| 86 virtual CRWWebController* CreateWebController(); | |
| 87 // TaskObserver methods (used when waiting for background tasks). | 85 // TaskObserver methods (used when waiting for background tasks). |
| 88 void WillProcessTask(const base::PendingTask& pending_task) override; | 86 void WillProcessTask(const base::PendingTask& pending_task) override; |
| 89 void DidProcessTask(const base::PendingTask& pending_task) override; | 87 void DidProcessTask(const base::PendingTask& pending_task) override; |
| 90 | 88 |
| 91 // The web controller for testing. | 89 // The web controller for testing. |
| 92 base::scoped_nsobject<CRWWebController> webController_; | 90 base::WeakNSObject<CRWWebController> webController_; |
| 93 // true if a task has been processed. | 91 // true if a task has been processed. |
| 94 bool processed_a_task_; | 92 bool processed_a_task_; |
| 95 | 93 |
| 96 private: | 94 private: |
| 97 // LoadURL() for data URLs sometimes lock up navigation, so if the loaded page | 95 // 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 | 96 // 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. | 97 // 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. | 98 // Returns true if a reset occurred. One may want to load the page again. |
| 101 bool ResetPageIfNavigationStalled(NSString* load_check); | 99 bool ResetPageIfNavigationStalled(NSString* load_check); |
| 102 // Creates a unique HTML element to look for in | 100 // Creates a unique HTML element to look for in |
| 103 // ResetPageIfNavigationStalled(). | 101 // ResetPageIfNavigationStalled(). |
| 104 NSString* CreateLoadCheck(); | 102 NSString* CreateLoadCheck(); |
| 103 // The web state for testing. |
| 104 std::unique_ptr<WebStateImpl> web_state_impl_; |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 } // namespace web | 107 } // namespace web |
| 108 | 108 |
| 109 #endif // IOS_WEB_TEST_WEB_TEST_H_ | 109 #endif // IOS_WEB_TEST_WEB_TEST_H_ |
| OLD | NEW |