OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 #include "base/mac/scoped_nsobject.h" | |
6 #import "ios/chrome/browser/ui/rtl_geometry.h" | 5 #import "ios/chrome/browser/ui/rtl_geometry.h" |
7 #import "ios/chrome/browser/ui/stack_view/card_view.h" | 6 #import "ios/chrome/browser/ui/stack_view/card_view.h" |
8 #import "ios/chrome/browser/ui/stack_view/stack_card.h" | 7 #import "ios/chrome/browser/ui/stack_view/stack_card.h" |
9 #import "ios/chrome/browser/ui/ui_util.h" | 8 #import "ios/chrome/browser/ui/ui_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "testing/gtest_mac.h" | 10 #include "testing/gtest_mac.h" |
12 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
13 | 12 |
13 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
14 #error "This file requires ARC support." | |
15 #endif | |
16 | |
14 // Mocked-out CardView object | 17 // Mocked-out CardView object |
15 @interface MockCardView : UIView | 18 @interface MockCardView : UIView |
16 @end | 19 @end |
17 | 20 |
18 @implementation MockCardView | 21 @implementation MockCardView |
19 - (void)setIsActiveTab:(BOOL)isActiveTab { | 22 - (void)setIsActiveTab:(BOOL)isActiveTab { |
20 } | 23 } |
21 @end | 24 @end |
22 | 25 |
23 // Returns mocked-out CardView objects for every request. | 26 // Returns mocked-out CardView objects for every request. |
24 @interface MockCardViewProvider : NSObject<StackCardViewProvider> | 27 @interface MockCardViewProvider : NSObject<StackCardViewProvider> |
25 @end | 28 @end |
26 | 29 |
27 @implementation MockCardViewProvider | 30 @implementation MockCardViewProvider |
28 - (CardView*)cardViewWithFrame:(CGRect)frame forStackCard:(StackCard*)card { | 31 - (CardView*)cardViewWithFrame:(CGRect)frame forStackCard:(StackCard*)card { |
29 return (CardView*)[[[MockCardView alloc] initWithFrame:frame] autorelease]; | 32 return (CardView*)[[MockCardView alloc] initWithFrame:frame]; |
sdefresne
2017/02/09 13:47:49
static_cast
lody
2017/02/09 16:00:03
Done.
| |
30 } | 33 } |
31 @end | 34 @end |
32 | 35 |
33 #pragma mark - | 36 #pragma mark - |
34 | 37 |
35 namespace { | 38 namespace { |
36 | 39 |
37 class StackCardTest : public PlatformTest { | 40 class StackCardTest : public PlatformTest { |
38 protected: | 41 protected: |
39 void SetUp() override { | 42 void SetUp() override { |
40 view_provider_.reset([[MockCardViewProvider alloc] init]); | 43 view_provider_ = [[MockCardViewProvider alloc] init]; |
41 } | 44 } |
42 | 45 |
sdefresne
2017/02/09 13:47:49
PlatformTest drain it's autoreleasepool in ShutDow
lody
2017/02/09 16:00:03
Done.
| |
43 base::scoped_nsobject<MockCardViewProvider> view_provider_; | 46 MockCardViewProvider* view_provider_; |
44 }; | 47 }; |
45 | 48 |
46 TEST_F(StackCardTest, LazyCreation) { | 49 TEST_F(StackCardTest, LazyCreation) { |
47 base::scoped_nsobject<StackCard> card( | 50 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
48 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
49 // Set attributes before asking for the view. | 51 // Set attributes before asking for the view. |
50 LayoutRect layout = LayoutRectMake(10, 300, 20, 55, 98); | 52 LayoutRect layout = LayoutRectMake(10, 300, 20, 55, 98); |
51 CGRect frame = LayoutRectGetRect(layout); | 53 CGRect frame = LayoutRectGetRect(layout); |
52 [card setLayout:layout]; | 54 [card setLayout:layout]; |
53 // Ensure the view hasn't been created yet. | 55 // Ensure the view hasn't been created yet. |
54 EXPECT_FALSE([card viewIsLive]); | 56 EXPECT_FALSE([card viewIsLive]); |
55 // Make sure that the view actually has those attributes. | 57 // Make sure that the view actually has those attributes. |
56 UIView* view = [card view]; | 58 UIView* view = [card view]; |
57 EXPECT_FLOAT_EQ(frame.size.width, view.frame.size.width); | 59 EXPECT_FLOAT_EQ(frame.size.width, view.frame.size.width); |
58 EXPECT_FLOAT_EQ(frame.size.height, view.frame.size.height); | 60 EXPECT_FLOAT_EQ(frame.size.height, view.frame.size.height); |
59 EXPECT_FLOAT_EQ(frame.origin.x, view.frame.origin.x); | 61 EXPECT_FLOAT_EQ(frame.origin.x, view.frame.origin.x); |
60 EXPECT_FLOAT_EQ(frame.origin.y, view.frame.origin.y); | 62 EXPECT_FLOAT_EQ(frame.origin.y, view.frame.origin.y); |
61 } | 63 } |
62 | 64 |
63 TEST_F(StackCardTest, LiveViewUpdating) { | 65 TEST_F(StackCardTest, LiveViewUpdating) { |
64 base::scoped_nsobject<StackCard> card( | 66 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
65 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
66 // Get the view, then set attributes. | 67 // Get the view, then set attributes. |
67 UIView* view = [card view]; | 68 UIView* view = [card view]; |
68 LayoutRect layout = LayoutRectMake(10, 300, 20, 55, 98); | 69 LayoutRect layout = LayoutRectMake(10, 300, 20, 55, 98); |
69 CGRect frame = LayoutRectGetRect(layout); | 70 CGRect frame = LayoutRectGetRect(layout); |
70 [card setLayout:layout]; | 71 [card setLayout:layout]; |
71 // Make sure that the view actually has those attributes. | 72 // Make sure that the view actually has those attributes. |
72 EXPECT_FLOAT_EQ(frame.size.width, view.frame.size.width); | 73 EXPECT_FLOAT_EQ(frame.size.width, view.frame.size.width); |
73 EXPECT_FLOAT_EQ(frame.size.height, view.frame.size.height); | 74 EXPECT_FLOAT_EQ(frame.size.height, view.frame.size.height); |
74 EXPECT_FLOAT_EQ(frame.origin.x, view.frame.origin.x); | 75 EXPECT_FLOAT_EQ(frame.origin.x, view.frame.origin.x); |
75 EXPECT_FLOAT_EQ(frame.origin.y, view.frame.origin.y); | 76 EXPECT_FLOAT_EQ(frame.origin.y, view.frame.origin.y); |
76 } | 77 } |
77 | 78 |
78 TEST_F(StackCardTest, BoundsUpdatePreservesCenter) { | 79 TEST_F(StackCardTest, BoundsUpdatePreservesCenter) { |
79 base::scoped_nsobject<StackCard> card( | 80 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
80 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
81 LayoutRect layout = LayoutRectMake(0, 300, 0, 40, 100); | 81 LayoutRect layout = LayoutRectMake(0, 300, 0, 40, 100); |
82 CGRect frame = LayoutRectGetRect(layout); | 82 CGRect frame = LayoutRectGetRect(layout); |
83 [card setLayout:layout]; | 83 [card setLayout:layout]; |
84 // Changing the bounds should preserve the center (as with UIView). | 84 // Changing the bounds should preserve the center (as with UIView). |
85 [card setSize:CGSizeMake(10, 10)]; | 85 [card setSize:CGSizeMake(10, 10)]; |
86 CGRect newFrame = LayoutRectGetRect([card layout]); | 86 CGRect newFrame = LayoutRectGetRect([card layout]); |
87 EXPECT_FLOAT_EQ(CGRectGetMidX(frame), CGRectGetMidX(newFrame)); | 87 EXPECT_FLOAT_EQ(CGRectGetMidX(frame), CGRectGetMidX(newFrame)); |
88 EXPECT_FLOAT_EQ(CGRectGetMidY(frame), CGRectGetMidY(newFrame)); | 88 EXPECT_FLOAT_EQ(CGRectGetMidY(frame), CGRectGetMidY(newFrame)); |
89 } | 89 } |
90 | 90 |
91 TEST_F(StackCardTest, PixelAlignmentOfViewFrameAfterLiveUpdate) { | 91 TEST_F(StackCardTest, PixelAlignmentOfViewFrameAfterLiveUpdate) { |
92 base::scoped_nsobject<StackCard> card( | 92 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
93 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
94 // Get the view, then set attributes. | 93 // Get the view, then set attributes. |
95 UIView* view = [card view]; | 94 UIView* view = [card view]; |
96 const LayoutRectPosition kPosition = LayoutRectPositionMake(10.3, 20.4); | 95 const LayoutRectPosition kPosition = LayoutRectPositionMake(10.3, 20.4); |
97 const CGSize kSize = CGSizeMake(55, 98); | 96 const CGSize kSize = CGSizeMake(55, 98); |
98 const CGFloat kBoundingWidth = 300; | 97 const CGFloat kBoundingWidth = 300; |
99 const LayoutRect kLayout = | 98 const LayoutRect kLayout = |
100 LayoutRectMake(kPosition.leading, kBoundingWidth, kPosition.originY, | 99 LayoutRectMake(kPosition.leading, kBoundingWidth, kPosition.originY, |
101 kSize.width, kSize.height); | 100 kSize.width, kSize.height); |
102 [card setLayout:kLayout]; | 101 [card setLayout:kLayout]; |
103 EXPECT_FLOAT_EQ(kSize.width, view.frame.size.width); | 102 EXPECT_FLOAT_EQ(kSize.width, view.frame.size.width); |
104 EXPECT_FLOAT_EQ(kSize.height, view.frame.size.height); | 103 EXPECT_FLOAT_EQ(kSize.height, view.frame.size.height); |
105 EXPECT_FLOAT_EQ(kPosition.leading, [card layout].position.leading); | 104 EXPECT_FLOAT_EQ(kPosition.leading, [card layout].position.leading); |
106 EXPECT_FLOAT_EQ(kPosition.originY, [card layout].position.originY); | 105 EXPECT_FLOAT_EQ(kPosition.originY, [card layout].position.originY); |
107 // The view's origin should be pixel-aligned. | 106 // The view's origin should be pixel-aligned. |
108 const CGPoint kPixelAlignedOrigin = | 107 const CGPoint kPixelAlignedOrigin = |
109 AlignRectOriginAndSizeToPixels(LayoutRectGetRect(kLayout)).origin; | 108 AlignRectOriginAndSizeToPixels(LayoutRectGetRect(kLayout)).origin; |
110 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.x, view.frame.origin.x); | 109 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.x, view.frame.origin.x); |
111 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.y, view.frame.origin.y); | 110 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.y, view.frame.origin.y); |
112 } | 111 } |
113 | 112 |
114 TEST_F(StackCardTest, ViewFrameSynchronization) { | 113 TEST_F(StackCardTest, ViewFrameSynchronization) { |
115 base::scoped_nsobject<StackCard> card( | 114 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
116 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
117 // Get the view, then set attributes. | 115 // Get the view, then set attributes. |
118 UIView* view = [card view]; | 116 UIView* view = [card view]; |
119 const LayoutRect kFirstLayout = LayoutRectMake(10, 300, 20, 55, 98); | 117 const LayoutRect kFirstLayout = LayoutRectMake(10, 300, 20, 55, 98); |
120 CGRect firstFrame = LayoutRectGetRect(kFirstLayout); | 118 CGRect firstFrame = LayoutRectGetRect(kFirstLayout); |
121 [card setLayout:kFirstLayout]; | 119 [card setLayout:kFirstLayout]; |
122 // Make sure that the view actually has those attributes. | 120 // Make sure that the view actually has those attributes. |
123 EXPECT_FLOAT_EQ(firstFrame.size.width, view.frame.size.width); | 121 EXPECT_FLOAT_EQ(firstFrame.size.width, view.frame.size.width); |
124 EXPECT_FLOAT_EQ(firstFrame.size.height, view.frame.size.height); | 122 EXPECT_FLOAT_EQ(firstFrame.size.height, view.frame.size.height); |
125 EXPECT_FLOAT_EQ(firstFrame.origin.x, view.frame.origin.x); | 123 EXPECT_FLOAT_EQ(firstFrame.origin.x, view.frame.origin.x); |
126 EXPECT_FLOAT_EQ(firstFrame.origin.y, view.frame.origin.y); | 124 EXPECT_FLOAT_EQ(firstFrame.origin.y, view.frame.origin.y); |
(...skipping 14 matching lines...) Expand all Loading... | |
141 EXPECT_FLOAT_EQ(firstFrame.origin.y, view.frame.origin.y); | 139 EXPECT_FLOAT_EQ(firstFrame.origin.y, view.frame.origin.y); |
142 [card setSynchronizeView:YES]; | 140 [card setSynchronizeView:YES]; |
143 // View should immediately pick up the new attributes. | 141 // View should immediately pick up the new attributes. |
144 EXPECT_FLOAT_EQ(secondFrame.size.width, view.frame.size.width); | 142 EXPECT_FLOAT_EQ(secondFrame.size.width, view.frame.size.width); |
145 EXPECT_FLOAT_EQ(secondFrame.size.height, view.frame.size.height); | 143 EXPECT_FLOAT_EQ(secondFrame.size.height, view.frame.size.height); |
146 EXPECT_FLOAT_EQ(secondFrame.origin.x, view.frame.origin.x); | 144 EXPECT_FLOAT_EQ(secondFrame.origin.x, view.frame.origin.x); |
147 EXPECT_FLOAT_EQ(secondFrame.origin.y, view.frame.origin.y); | 145 EXPECT_FLOAT_EQ(secondFrame.origin.y, view.frame.origin.y); |
148 } | 146 } |
149 | 147 |
150 TEST_F(StackCardTest, ViewLayoutSynchronization) { | 148 TEST_F(StackCardTest, ViewLayoutSynchronization) { |
151 base::scoped_nsobject<StackCard> card( | 149 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
152 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
153 // Get the view, then set attributes. | 150 // Get the view, then set attributes. |
154 UIView* view = [card view]; | 151 UIView* view = [card view]; |
155 const LayoutRect kFirstLayout = LayoutRectMake(30, 300, 40, 200, 100); | 152 const LayoutRect kFirstLayout = LayoutRectMake(30, 300, 40, 200, 100); |
156 const CGRect kFirstFrame = LayoutRectGetRect(kFirstLayout); | 153 const CGRect kFirstFrame = LayoutRectGetRect(kFirstLayout); |
157 [card setLayout:kFirstLayout]; | 154 [card setLayout:kFirstLayout]; |
158 // Make sure that the view actually has those attributes. | 155 // Make sure that the view actually has those attributes. |
159 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.bounds.size.width); | 156 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.bounds.size.width); |
160 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.bounds.size.height); | 157 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.bounds.size.height); |
161 EXPECT_FLOAT_EQ(CGRectGetMidX(kFirstFrame), view.center.x); | 158 EXPECT_FLOAT_EQ(CGRectGetMidX(kFirstFrame), view.center.x); |
162 EXPECT_FLOAT_EQ(CGRectGetMidY(kFirstFrame), view.center.y); | 159 EXPECT_FLOAT_EQ(CGRectGetMidY(kFirstFrame), view.center.y); |
(...skipping 15 matching lines...) Expand all Loading... | |
178 EXPECT_FLOAT_EQ(CGRectGetMidY(kFirstFrame), view.center.y); | 175 EXPECT_FLOAT_EQ(CGRectGetMidY(kFirstFrame), view.center.y); |
179 [card setSynchronizeView:YES]; | 176 [card setSynchronizeView:YES]; |
180 // View should immediately pick up the new attributes. | 177 // View should immediately pick up the new attributes. |
181 EXPECT_FLOAT_EQ(kSecondFrame.size.width, view.bounds.size.width); | 178 EXPECT_FLOAT_EQ(kSecondFrame.size.width, view.bounds.size.width); |
182 EXPECT_FLOAT_EQ(kSecondFrame.size.height, view.bounds.size.height); | 179 EXPECT_FLOAT_EQ(kSecondFrame.size.height, view.bounds.size.height); |
183 EXPECT_FLOAT_EQ(CGRectGetMidX(kSecondFrame), view.center.x); | 180 EXPECT_FLOAT_EQ(CGRectGetMidX(kSecondFrame), view.center.x); |
184 EXPECT_FLOAT_EQ(CGRectGetMidY(kSecondFrame), view.center.y); | 181 EXPECT_FLOAT_EQ(CGRectGetMidY(kSecondFrame), view.center.y); |
185 } | 182 } |
186 | 183 |
187 TEST_F(StackCardTest, PixelAlignmentOfViewAfterSynchronization) { | 184 TEST_F(StackCardTest, PixelAlignmentOfViewAfterSynchronization) { |
188 base::scoped_nsobject<StackCard> card( | 185 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
189 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
190 // Get the view, then set attributes. | 186 // Get the view, then set attributes. |
191 UIView* view = [card view]; | 187 UIView* view = [card view]; |
192 const CGFloat kBoundingWidth = 300; | 188 const CGFloat kBoundingWidth = 300; |
193 const LayoutRect kFirstLayout = | 189 const LayoutRect kFirstLayout = |
194 LayoutRectMake(10, kBoundingWidth, 20, 55, 98); | 190 LayoutRectMake(10, kBoundingWidth, 20, 55, 98); |
195 const CGRect kFirstFrame = LayoutRectGetRect(kFirstLayout); | 191 const CGRect kFirstFrame = LayoutRectGetRect(kFirstLayout); |
196 [card setLayout:kFirstLayout]; | 192 [card setLayout:kFirstLayout]; |
197 // Make sure that the view actually has those attributes. | 193 // Make sure that the view actually has those attributes. |
198 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.frame.size.width); | 194 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.frame.size.width); |
199 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.frame.size.height); | 195 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.frame.size.height); |
(...skipping 22 matching lines...) Expand all Loading... | |
222 // correctly pixel-aligned. | 218 // correctly pixel-aligned. |
223 EXPECT_FLOAT_EQ(kSecondFrame.size.width, view.frame.size.width); | 219 EXPECT_FLOAT_EQ(kSecondFrame.size.width, view.frame.size.width); |
224 EXPECT_FLOAT_EQ(kSecondFrame.size.height, view.frame.size.height); | 220 EXPECT_FLOAT_EQ(kSecondFrame.size.height, view.frame.size.height); |
225 const CGPoint kPixelAlignedOrigin = | 221 const CGPoint kPixelAlignedOrigin = |
226 AlignRectOriginAndSizeToPixels(LayoutRectGetRect(kSecondLayout)).origin; | 222 AlignRectOriginAndSizeToPixels(LayoutRectGetRect(kSecondLayout)).origin; |
227 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.x, view.frame.origin.x); | 223 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.x, view.frame.origin.x); |
228 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.y, view.frame.origin.y); | 224 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.y, view.frame.origin.y); |
229 } | 225 } |
230 | 226 |
231 } // namespace | 227 } // namespace |
OLD | NEW |