web_view_restorable_state_inttest.mm 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 <ChromeWebView/ChromeWebView.h>
  5. #import "base/test/ios/wait_util.h"
  6. #import "ios/web/common/uikit_ui_util.h"
  7. #import "ios/web_view/test/web_view_inttest_base.h"
  8. #import "ios/web_view/test/web_view_test_util.h"
  9. #include "testing/gtest_mac.h"
  10. #if !defined(__has_feature) || !__has_feature(objc_arc)
  11. #error "This file requires ARC support."
  12. #endif
  13. using base::test::ios::WaitUntilConditionOrTimeout;
  14. using base::test::ios::kWaitForPageLoadTimeout;
  15. namespace ios_web_view {
  16. // Tests encodeRestorableStateWithCoder: and decodeRestorableStateWithCoder:
  17. // methods.
  18. typedef ios_web_view::WebViewInttestBase WebViewRestorableStateTest;
  19. TEST_F(WebViewRestorableStateTest, EncodeDecode) {
  20. // Load 2 URLs to create non-default state.
  21. ASSERT_FALSE([web_view_ lastCommittedURL]);
  22. ASSERT_FALSE([web_view_ visibleURL]);
  23. ASSERT_FALSE([web_view_ canGoBack]);
  24. ASSERT_FALSE([web_view_ canGoForward]);
  25. ASSERT_TRUE(test::LoadUrl(web_view_, [NSURL URLWithString:@"about:newtab"]));
  26. ASSERT_NSEQ(@"about:newtab", [web_view_ lastCommittedURL].absoluteString);
  27. ASSERT_NSEQ(@"about:newtab", [web_view_ visibleURL].absoluteString);
  28. ASSERT_TRUE(test::LoadUrl(web_view_, [NSURL URLWithString:@"about:blank"]));
  29. ASSERT_NSEQ(@"about:blank", [web_view_ lastCommittedURL].absoluteString);
  30. ASSERT_NSEQ(@"about:blank", [web_view_ visibleURL].absoluteString);
  31. ASSERT_TRUE([web_view_ canGoBack]);
  32. ASSERT_FALSE([web_view_ canGoForward]);
  33. // Create second web view and restore its state from the first web view.
  34. CWVWebView* restored_web_view = test::CreateWebView();
  35. test::CopyWebViewState(web_view_, restored_web_view);
  36. // The WKWebView must be present in the view hierarchy in order to prevent
  37. // WebKit optimizations which may pause internal parts of the web view
  38. // without notice. Work around this by adding the view directly.
  39. UIViewController* view_controller = [GetAnyKeyWindow() rootViewController];
  40. [view_controller.view addSubview:restored_web_view];
  41. // Wait for restore to finish.
  42. ASSERT_TRUE(WaitUntilConditionOrTimeout(kWaitForPageLoadTimeout, ^bool {
  43. return [restored_web_view lastCommittedURL] != nil;
  44. }));
  45. // Verify that the state has been restored correctly.
  46. EXPECT_NSEQ(@"about:blank",
  47. [restored_web_view lastCommittedURL].absoluteString);
  48. EXPECT_NSEQ(@"about:blank", [restored_web_view visibleURL].absoluteString);
  49. EXPECT_TRUE([web_view_ canGoBack]);
  50. EXPECT_FALSE([web_view_ canGoForward]);
  51. }
  52. } // namespace ios_web_view