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
3 // found in the LICENSE file.
5 #import "ios/web/public/web_state/crw_web_view_scroll_view_proxy.h"
7 #import <UIKit/UIKit.h>
9 #include "base/compiler_specific.h"
10 #import "base/mac/scoped_nsobject.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #import "third_party/ocmock/OCMock/OCMock.h"
13 #include "third_party/ocmock/gtest_support.h"
17 class CRWWebViewScrollViewProxyTest : public ::testing::Test {
19 void SetUp() override {
20 mockScrollView_.reset(
21 [[OCMockObject niceMockForClass:[UIScrollView class]] retain]);
22 webViewScrollViewProxy_.reset([[CRWWebViewScrollViewProxy alloc] init]);
24 base::scoped_nsobject<id> mockScrollView_;
25 base::scoped_nsobject<CRWWebViewScrollViewProxy> webViewScrollViewProxy_;
28 // Tests that the UIScrollViewDelegate is set correctly.
29 TEST_F(CRWWebViewScrollViewProxyTest, testDelegate) {
30 [static_cast<UIScrollView*>([mockScrollView_ expect])
31 setDelegate:webViewScrollViewProxy_.get()];
32 [webViewScrollViewProxy_ setScrollView:mockScrollView_];
33 EXPECT_OCMOCK_VERIFY(mockScrollView_);
36 // Tests that setting 2 scroll views consecutively, clears the delegate of the
37 // previous scroll view.
38 TEST_F(CRWWebViewScrollViewProxyTest, testMultipleScrollView) {
39 base::scoped_nsobject<UIScrollView> mockScrollView1(
40 [[UIScrollView alloc] init]);
41 base::scoped_nsobject<UIScrollView> mockScrollView2(
42 [[UIScrollView alloc] init]);
43 [webViewScrollViewProxy_ setScrollView:mockScrollView1];
44 [webViewScrollViewProxy_ setScrollView:mockScrollView2];
45 EXPECT_FALSE([mockScrollView1 delegate]);
46 EXPECT_EQ(webViewScrollViewProxy_.get(), [mockScrollView2 delegate]);
49 // Tests that when releasing a scroll view from the CRWWebViewScrollViewProxy,
50 // the UIScrollView's delegate is also cleared.
51 TEST_F(CRWWebViewScrollViewProxyTest, testDelegateClearingUp) {
52 base::scoped_nsobject<UIScrollView> mockScrollView1(
53 [[UIScrollView alloc] init]);
54 [webViewScrollViewProxy_ setScrollView:mockScrollView1];
55 EXPECT_EQ(webViewScrollViewProxy_.get(), [mockScrollView1 delegate]);
56 [webViewScrollViewProxy_ setScrollView:nil];
57 EXPECT_FALSE([mockScrollView1 delegate]);
60 // Tests that CRWWebViewScrollViewProxy returns the correct property values from
61 // the underlying UIScrollView.
62 TEST_F(CRWWebViewScrollViewProxyTest, testScrollViewPresent) {
63 [webViewScrollViewProxy_ setScrollView:mockScrollView_];
65 [[[mockScrollView_ stub] andReturnValue:OCMOCK_VALUE(yes)] isZooming];
66 EXPECT_TRUE([webViewScrollViewProxy_ isZooming]);
69 const CGPoint point = CGPointMake(10, 10);
70 [[[mockScrollView_ stub]
71 andReturnValue:[NSValue valueWithCGPoint:point]] contentOffset];
72 EXPECT_TRUE(CGPointEqualToPoint(point,
73 [webViewScrollViewProxy_ contentOffset]));
76 const UIEdgeInsets contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
77 [[[mockScrollView_ stub] andReturnValue:
78 [NSValue valueWithUIEdgeInsets:contentInset]] contentInset];
79 EXPECT_TRUE(UIEdgeInsetsEqualToEdgeInsets(contentInset,
80 [webViewScrollViewProxy_ contentInset]));
83 const UIEdgeInsets scrollIndicatorInsets = UIEdgeInsetsMake(20, 20, 20, 20);
84 [[[mockScrollView_ stub] andReturnValue:[NSValue
85 valueWithUIEdgeInsets:scrollIndicatorInsets]] scrollIndicatorInsets];
86 EXPECT_TRUE(UIEdgeInsetsEqualToEdgeInsets(scrollIndicatorInsets,
87 [webViewScrollViewProxy_ scrollIndicatorInsets]));
90 const CGSize contentSize = CGSizeMake(19, 19);
91 [[[mockScrollView_ stub] andReturnValue:
92 [NSValue valueWithCGSize:contentSize]] contentSize];
93 EXPECT_TRUE(CGSizeEqualToSize(contentSize,
94 [webViewScrollViewProxy_ contentSize]));
97 const CGRect frame = CGRectMake(2, 4, 5, 1);
98 [[[mockScrollView_ stub] andReturnValue:
99 [NSValue valueWithCGRect:frame]] frame];
100 EXPECT_TRUE(CGRectEqualToRect(frame,
101 [webViewScrollViewProxy_ frame]));
104 // Tests that CRWWebViewScrollViewProxy returns the correct property values when
105 // there is no underlying UIScrollView.
106 TEST_F(CRWWebViewScrollViewProxyTest, testScrollViewAbsent) {
107 [webViewScrollViewProxy_ setScrollView:nil];
109 EXPECT_TRUE(CGPointEqualToPoint(CGPointZero,
110 [webViewScrollViewProxy_ contentOffset]));
111 EXPECT_TRUE(UIEdgeInsetsEqualToEdgeInsets(
112 UIEdgeInsetsZero, [webViewScrollViewProxy_ contentInset]));
113 EXPECT_TRUE(UIEdgeInsetsEqualToEdgeInsets(
114 UIEdgeInsetsZero, [webViewScrollViewProxy_ scrollIndicatorInsets]));
115 EXPECT_TRUE(CGSizeEqualToSize(CGSizeZero,
116 [webViewScrollViewProxy_ contentSize]));
117 EXPECT_TRUE(CGRectEqualToRect(CGRectZero,
118 [webViewScrollViewProxy_ frame]));
120 // Make sure setting the properties is fine too.
122 const CGPoint kPoint = CGPointMake(10, 10);
123 [webViewScrollViewProxy_ setContentOffset:kPoint];
125 const UIEdgeInsets kContentInset = UIEdgeInsetsMake(10, 10, 10, 10);
126 [webViewScrollViewProxy_ setContentInset:kContentInset];
127 [webViewScrollViewProxy_ setScrollIndicatorInsets:kContentInset];
129 const CGSize kContentSize = CGSizeMake(19, 19);
130 [webViewScrollViewProxy_ setContentSize:kContentSize];
133 // Tests releasing a scroll view when none is owned by the
134 // CRWWebViewScrollViewProxy.
135 TEST_F(CRWWebViewScrollViewProxyTest, testReleasingAScrollView) {
136 [webViewScrollViewProxy_ setScrollView:nil];
139 // Tests that multiple WebViewScrollViewProxies hold onto the same underlying
141 TEST_F(CRWWebViewScrollViewProxyTest, testMultipleWebViewScrollViewProxies) {
142 [webViewScrollViewProxy_ setScrollView:mockScrollView_];
144 base::scoped_nsobject<CRWWebViewScrollViewProxy> webViewScrollViewProxy1(
145 [[CRWWebViewScrollViewProxy alloc] init]);
146 [webViewScrollViewProxy1 setScrollView:mockScrollView_];
148 base::scoped_nsobject<CRWWebViewScrollViewProxy> webViewScrollViewProxy2(
149 [[CRWWebViewScrollViewProxy alloc] init]);
150 [webViewScrollViewProxy2 setScrollView:mockScrollView_];
153 const CGPoint point = CGPointMake(10, 10);
154 [[[mockScrollView_ stub]
155 andReturnValue:[NSValue valueWithCGPoint:point]] contentOffset];
156 EXPECT_TRUE(CGPointEqualToPoint(point,
157 [webViewScrollViewProxy_ contentOffset]));
158 EXPECT_TRUE(CGPointEqualToPoint(point,
159 [webViewScrollViewProxy1 contentOffset]));
160 EXPECT_TRUE(CGPointEqualToPoint(point,
161 [webViewScrollViewProxy2 contentOffset]));