scroll_view_kvo_inttest.mm 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2018 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 <Foundation/Foundation.h>
  6. #import "base/test/ios/wait_util.h"
  7. #import "ios/web_view/test/observer.h"
  8. #import "ios/web_view/test/web_view_inttest_base.h"
  9. #import "ios/web_view/test/web_view_test_util.h"
  10. #include "testing/gtest_mac.h"
  11. #if !defined(__has_feature) || !__has_feature(objc_arc)
  12. #error "This file requires ARC support."
  13. #endif
  14. namespace ios_web_view {
  15. // Tests that the KVO compliant properties of UIScrollView correctly report
  16. // changes.
  17. typedef ios_web_view::WebViewInttestBase ScrollViewKvoTest;
  18. // Tests that UIScrollView correctly reports |contentOffset| state.
  19. TEST_F(ScrollViewKvoTest, contentOffset) {
  20. Observer* offset_observer = [[Observer alloc] init];
  21. [offset_observer setObservedObject:web_view_.scrollView
  22. keyPath:@"contentOffset"];
  23. Observer* size_observer = [[Observer alloc] init];
  24. [size_observer setObservedObject:web_view_.scrollView keyPath:@"contentSize"];
  25. // A page must be loaded before changing |contentOffset|. Otherwise the change
  26. // is ignored because the underlying UIScrollView hasn't been created.
  27. [web_view_
  28. loadRequest:[NSURLRequest
  29. requestWithURL:[NSURL URLWithString:@"about:blank"]]];
  30. EXPECT_TRUE(base::test::ios::WaitUntilConditionOrTimeout(
  31. base::test::ios::kWaitForPageLoadTimeout, ^bool {
  32. return size_observer.lastValue;
  33. }));
  34. web_view_.scrollView.contentOffset = CGPointMake(10, 20);
  35. EXPECT_NSEQ([NSValue valueWithCGPoint:CGPointMake(10, 20)],
  36. offset_observer.lastValue);
  37. [web_view_.scrollView setContentOffset:CGPointMake(30, 40) animated:YES];
  38. EXPECT_TRUE(base::test::ios::WaitUntilConditionOrTimeout(
  39. base::test::ios::kWaitForUIElementTimeout, ^{
  40. return static_cast<bool>([offset_observer.lastValue
  41. isEqual:[NSValue valueWithCGPoint:CGPointMake(30, 40)]]);
  42. }));
  43. }
  44. } // namespace ios_web_view