web_view_test_util.mm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2017 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.
  4. #import "ios/web_view/test/web_view_test_util.h"
  5. #import "ios/web_view/public/cwv_web_view.h"
  6. #import "ios/web_view/public/cwv_web_view_configuration.h"
  7. #import "base/test/ios/wait_util.h"
  8. #if !defined(__has_feature) || !__has_feature(objc_arc)
  9. #error "This file requires ARC support."
  10. #endif
  11. using base::test::ios::WaitUntilConditionOrTimeout;
  12. using base::test::ios::kWaitForPageLoadTimeout;
  13. using base::test::ios::kWaitForJSCompletionTimeout;
  14. using base::test::ios::kWaitForUIElementTimeout;
  15. namespace ios_web_view {
  16. namespace test {
  17. CWVWebView* CreateWebView() {
  18. return [[CWVWebView alloc]
  19. initWithFrame:UIScreen.mainScreen.bounds
  20. configuration:[CWVWebViewConfiguration defaultConfiguration]];
  21. }
  22. bool LoadUrl(CWVWebView* web_view, NSURL* url) {
  23. [web_view loadRequest:[NSURLRequest requestWithURL:url]];
  24. return WaitForWebViewLoadCompletionOrTimeout(web_view);
  25. }
  26. bool TapWebViewElementWithId(CWVWebView* web_view, NSString* element_id) {
  27. NSString* script = [NSString
  28. stringWithFormat:@"(function() {"
  29. " var element = document.getElementById('%@');"
  30. " if (element) {"
  31. " element.click();"
  32. " return true;"
  33. " }"
  34. " return false;"
  35. "})();",
  36. element_id];
  37. return [EvaluateJavaScript(web_view, script) boolValue];
  38. }
  39. id EvaluateJavaScript(CWVWebView* web_view, NSString* script, bool* success) {
  40. __block bool callback_called = false;
  41. __block id evaluation_result = nil;
  42. __block bool evaluation_success = false;
  43. [web_view evaluateJavaScript:script
  44. completion:^(id local_result, BOOL local_success) {
  45. callback_called = true;
  46. evaluation_result = [local_result copy];
  47. evaluation_success = local_success;
  48. }];
  49. bool completed = WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
  50. return callback_called;
  51. });
  52. if (success) {
  53. *success = evaluation_success;
  54. }
  55. return completed ? evaluation_result : nil;
  56. }
  57. bool WaitForWebViewContainingTextOrTimeout(CWVWebView* web_view,
  58. NSString* text) {
  59. // Wait for load to stop because a new load may have just started.
  60. if (!WaitForWebViewLoadCompletionOrTimeout(web_view)) {
  61. return false;
  62. }
  63. return WaitUntilConditionOrTimeout(kWaitForUIElementTimeout, ^{
  64. id body = ios_web_view::test::EvaluateJavaScript(
  65. web_view, @"document.body ? document.body.textContent : null");
  66. return [body isKindOfClass:[NSString class]] && [body containsString:text];
  67. });
  68. }
  69. bool WaitForWebViewLoadCompletionOrTimeout(CWVWebView* web_view) {
  70. return WaitUntilConditionOrTimeout(kWaitForPageLoadTimeout, ^{
  71. return !web_view.loading;
  72. });
  73. }
  74. void CopyWebViewState(CWVWebView* source_web_view,
  75. CWVWebView* destination_web_view) {
  76. NSKeyedArchiver* archiver =
  77. [[NSKeyedArchiver alloc] initRequiringSecureCoding:NO];
  78. [source_web_view encodeRestorableStateWithCoder:archiver];
  79. NSKeyedUnarchiver* unarchiver =
  80. [[NSKeyedUnarchiver alloc] initForReadingFromData:[archiver encodedData]
  81. error:nil];
  82. unarchiver.requiresSecureCoding = NO;
  83. [destination_web_view decodeRestorableStateWithCoder:unarchiver];
  84. }
  85. } // namespace test
  86. } // namespace ios_web_view