OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/mac/scoped_nsobject.h" |
| 6 #import "ios/chrome/browser/ui/rtl_geometry.h" |
| 7 #import "ios/chrome/browser/ui/stack_view/card_view.h" |
| 8 #import "ios/chrome/browser/ui/stack_view/stack_card.h" |
| 9 #import "ios/chrome/browser/ui/ui_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/gtest_mac.h" |
| 12 #include "testing/platform_test.h" |
| 13 |
| 14 // Mocked-out CardView object |
| 15 @interface MockCardView : UIView |
| 16 @end |
| 17 |
| 18 @implementation MockCardView |
| 19 - (void)setIsActiveTab:(BOOL)isActiveTab { |
| 20 } |
| 21 @end |
| 22 |
| 23 // Returns mocked-out CardView objects for every request. |
| 24 @interface MockCardViewProvider : NSObject<StackCardViewProvider> |
| 25 @end |
| 26 |
| 27 @implementation MockCardViewProvider |
| 28 - (CardView*)cardViewWithFrame:(CGRect)frame forStackCard:(StackCard*)card { |
| 29 return (CardView*)[[[MockCardView alloc] initWithFrame:frame] autorelease]; |
| 30 } |
| 31 @end |
| 32 |
| 33 #pragma mark - |
| 34 |
| 35 namespace { |
| 36 |
| 37 class StackCardTest : public PlatformTest { |
| 38 protected: |
| 39 void SetUp() override { |
| 40 view_provider_.reset([[MockCardViewProvider alloc] init]); |
| 41 } |
| 42 |
| 43 base::scoped_nsobject<MockCardViewProvider> view_provider_; |
| 44 }; |
| 45 |
| 46 TEST_F(StackCardTest, LazyCreation) { |
| 47 base::scoped_nsobject<StackCard> card( |
| 48 [[StackCard alloc] initWithViewProvider:view_provider_]); |
| 49 // Set attributes before asking for the view. |
| 50 LayoutRect layout = LayoutRectMake(10, 300, 20, 55, 98); |
| 51 CGRect frame = LayoutRectGetRect(layout); |
| 52 [card setLayout:layout]; |
| 53 // Ensure the view hasn't been created yet. |
| 54 EXPECT_FALSE([card viewIsLive]); |
| 55 // Make sure that the view actually has those attributes. |
| 56 UIView* view = [card view]; |
| 57 EXPECT_FLOAT_EQ(frame.size.width, view.frame.size.width); |
| 58 EXPECT_FLOAT_EQ(frame.size.height, view.frame.size.height); |
| 59 EXPECT_FLOAT_EQ(frame.origin.x, view.frame.origin.x); |
| 60 EXPECT_FLOAT_EQ(frame.origin.y, view.frame.origin.y); |
| 61 } |
| 62 |
| 63 TEST_F(StackCardTest, LiveViewUpdating) { |
| 64 base::scoped_nsobject<StackCard> card( |
| 65 [[StackCard alloc] initWithViewProvider:view_provider_]); |
| 66 // Get the view, then set attributes. |
| 67 UIView* view = [card view]; |
| 68 LayoutRect layout = LayoutRectMake(10, 300, 20, 55, 98); |
| 69 CGRect frame = LayoutRectGetRect(layout); |
| 70 [card setLayout:layout]; |
| 71 // 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.height, view.frame.size.height); |
| 74 EXPECT_FLOAT_EQ(frame.origin.x, view.frame.origin.x); |
| 75 EXPECT_FLOAT_EQ(frame.origin.y, view.frame.origin.y); |
| 76 } |
| 77 |
| 78 TEST_F(StackCardTest, BoundsUpdatePreservesCenter) { |
| 79 base::scoped_nsobject<StackCard> card( |
| 80 [[StackCard alloc] initWithViewProvider:view_provider_]); |
| 81 LayoutRect layout = LayoutRectMake(0, 300, 0, 40, 100); |
| 82 CGRect frame = LayoutRectGetRect(layout); |
| 83 [card setLayout:layout]; |
| 84 // Changing the bounds should preserve the center (as with UIView). |
| 85 [card setSize:CGSizeMake(10, 10)]; |
| 86 CGRect newFrame = LayoutRectGetRect([card layout]); |
| 87 EXPECT_FLOAT_EQ(CGRectGetMidX(frame), CGRectGetMidX(newFrame)); |
| 88 EXPECT_FLOAT_EQ(CGRectGetMidY(frame), CGRectGetMidY(newFrame)); |
| 89 } |
| 90 |
| 91 TEST_F(StackCardTest, PixelAlignmentOfViewFrameAfterLiveUpdate) { |
| 92 base::scoped_nsobject<StackCard> card( |
| 93 [[StackCard alloc] initWithViewProvider:view_provider_]); |
| 94 // Get the view, then set attributes. |
| 95 UIView* view = [card view]; |
| 96 const LayoutRectPosition kPosition = LayoutRectPositionMake(10.3, 20.4); |
| 97 const CGSize kSize = CGSizeMake(55, 98); |
| 98 const CGFloat kBoundingWidth = 300; |
| 99 const LayoutRect kLayout = |
| 100 LayoutRectMake(kPosition.leading, kBoundingWidth, kPosition.originY, |
| 101 kSize.width, kSize.height); |
| 102 [card setLayout:kLayout]; |
| 103 EXPECT_FLOAT_EQ(kSize.width, view.frame.size.width); |
| 104 EXPECT_FLOAT_EQ(kSize.height, view.frame.size.height); |
| 105 EXPECT_FLOAT_EQ(kPosition.leading, [card layout].position.leading); |
| 106 EXPECT_FLOAT_EQ(kPosition.originY, [card layout].position.originY); |
| 107 // The view's origin should be pixel-aligned. |
| 108 const CGPoint kPixelAlignedOrigin = |
| 109 AlignRectOriginAndSizeToPixels(LayoutRectGetRect(kLayout)).origin; |
| 110 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.x, view.frame.origin.x); |
| 111 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.y, view.frame.origin.y); |
| 112 } |
| 113 |
| 114 TEST_F(StackCardTest, ViewFrameSynchronization) { |
| 115 base::scoped_nsobject<StackCard> card( |
| 116 [[StackCard alloc] initWithViewProvider:view_provider_]); |
| 117 // Get the view, then set attributes. |
| 118 UIView* view = [card view]; |
| 119 const LayoutRect kFirstLayout = LayoutRectMake(10, 300, 20, 55, 98); |
| 120 CGRect firstFrame = LayoutRectGetRect(kFirstLayout); |
| 121 [card setLayout:kFirstLayout]; |
| 122 // Make sure that the view actually has those attributes. |
| 123 EXPECT_FLOAT_EQ(firstFrame.size.width, view.frame.size.width); |
| 124 EXPECT_FLOAT_EQ(firstFrame.size.height, view.frame.size.height); |
| 125 EXPECT_FLOAT_EQ(firstFrame.origin.x, view.frame.origin.x); |
| 126 EXPECT_FLOAT_EQ(firstFrame.origin.y, view.frame.origin.y); |
| 127 [card setSynchronizeView:NO]; |
| 128 const LayoutRect kSecondLayout = LayoutRectMake(5, 300, 10, 40, 75); |
| 129 CGRect secondFrame = LayoutRectGetRect(kSecondLayout); |
| 130 [card setLayout:kSecondLayout]; |
| 131 // Card should have the new attributes... |
| 132 CGRect card_frame = LayoutRectGetRect([card layout]); |
| 133 EXPECT_FLOAT_EQ(secondFrame.size.width, card_frame.size.width); |
| 134 EXPECT_FLOAT_EQ(secondFrame.size.height, card_frame.size.height); |
| 135 EXPECT_FLOAT_EQ(secondFrame.origin.x, card_frame.origin.x); |
| 136 EXPECT_FLOAT_EQ(secondFrame.origin.y, card_frame.origin.y); |
| 137 // ... but view should still have the old attributes. |
| 138 EXPECT_FLOAT_EQ(firstFrame.size.width, view.frame.size.width); |
| 139 EXPECT_FLOAT_EQ(firstFrame.size.height, view.frame.size.height); |
| 140 EXPECT_FLOAT_EQ(firstFrame.origin.x, view.frame.origin.x); |
| 141 EXPECT_FLOAT_EQ(firstFrame.origin.y, view.frame.origin.y); |
| 142 [card setSynchronizeView:YES]; |
| 143 // View should immediately pick up the new attributes. |
| 144 EXPECT_FLOAT_EQ(secondFrame.size.width, view.frame.size.width); |
| 145 EXPECT_FLOAT_EQ(secondFrame.size.height, view.frame.size.height); |
| 146 EXPECT_FLOAT_EQ(secondFrame.origin.x, view.frame.origin.x); |
| 147 EXPECT_FLOAT_EQ(secondFrame.origin.y, view.frame.origin.y); |
| 148 } |
| 149 |
| 150 TEST_F(StackCardTest, ViewLayoutSynchronization) { |
| 151 base::scoped_nsobject<StackCard> card( |
| 152 [[StackCard alloc] initWithViewProvider:view_provider_]); |
| 153 // Get the view, then set attributes. |
| 154 UIView* view = [card view]; |
| 155 const LayoutRect kFirstLayout = LayoutRectMake(30, 300, 40, 200, 100); |
| 156 const CGRect kFirstFrame = LayoutRectGetRect(kFirstLayout); |
| 157 [card setLayout:kFirstLayout]; |
| 158 // Make sure that the view actually has those attributes. |
| 159 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.bounds.size.width); |
| 160 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.bounds.size.height); |
| 161 EXPECT_FLOAT_EQ(CGRectGetMidX(kFirstFrame), view.center.x); |
| 162 EXPECT_FLOAT_EQ(CGRectGetMidY(kFirstFrame), view.center.y); |
| 163 [card setSynchronizeView:NO]; |
| 164 const LayoutRect kSecondLayout = LayoutRectMake(20, 300, 10, 40, 50); |
| 165 const CGRect kSecondFrame = LayoutRectGetRect(kSecondLayout); |
| 166 [card setLayout:kSecondLayout]; |
| 167 // Card should have the new attributes... |
| 168 EXPECT_FLOAT_EQ(kSecondLayout.position.leading, |
| 169 [card layout].position.leading); |
| 170 EXPECT_FLOAT_EQ(kSecondLayout.position.originY, |
| 171 [card layout].position.originY); |
| 172 EXPECT_FLOAT_EQ(kSecondLayout.size.width, [card layout].size.width); |
| 173 EXPECT_FLOAT_EQ(kSecondLayout.size.height, [card layout].size.height); |
| 174 // ... but view should still have the old attributes. |
| 175 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.bounds.size.width); |
| 176 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.bounds.size.height); |
| 177 EXPECT_FLOAT_EQ(CGRectGetMidX(kFirstFrame), view.center.x); |
| 178 EXPECT_FLOAT_EQ(CGRectGetMidY(kFirstFrame), view.center.y); |
| 179 [card setSynchronizeView:YES]; |
| 180 // View should immediately pick up the new attributes. |
| 181 EXPECT_FLOAT_EQ(kSecondFrame.size.width, view.bounds.size.width); |
| 182 EXPECT_FLOAT_EQ(kSecondFrame.size.height, view.bounds.size.height); |
| 183 EXPECT_FLOAT_EQ(CGRectGetMidX(kSecondFrame), view.center.x); |
| 184 EXPECT_FLOAT_EQ(CGRectGetMidY(kSecondFrame), view.center.y); |
| 185 } |
| 186 |
| 187 TEST_F(StackCardTest, PixelAlignmentOfViewAfterSynchronization) { |
| 188 base::scoped_nsobject<StackCard> card( |
| 189 [[StackCard alloc] initWithViewProvider:view_provider_]); |
| 190 // Get the view, then set attributes. |
| 191 UIView* view = [card view]; |
| 192 const CGFloat kBoundingWidth = 300; |
| 193 const LayoutRect kFirstLayout = |
| 194 LayoutRectMake(10, kBoundingWidth, 20, 55, 98); |
| 195 const CGRect kFirstFrame = LayoutRectGetRect(kFirstLayout); |
| 196 [card setLayout:kFirstLayout]; |
| 197 // Make sure that the view actually has those attributes. |
| 198 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.frame.size.width); |
| 199 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.frame.size.height); |
| 200 EXPECT_FLOAT_EQ(kFirstFrame.origin.x, view.frame.origin.x); |
| 201 EXPECT_FLOAT_EQ(kFirstFrame.origin.y, view.frame.origin.y); |
| 202 [card setSynchronizeView:NO]; |
| 203 const LayoutRectPosition kSecondPosition = LayoutRectPositionMake(8.72, 7.73); |
| 204 const CGSize kSecondSize = CGSizeMake(40, 75); |
| 205 const LayoutRect kSecondLayout = LayoutRectMake( |
| 206 kSecondPosition.leading, kBoundingWidth, kSecondPosition.originY, |
| 207 kSecondSize.width, kSecondSize.height); |
| 208 const CGRect kSecondFrame = LayoutRectGetRect(kSecondLayout); |
| 209 [card setLayout:kSecondLayout]; |
| 210 // Card should have the new attributes... |
| 211 EXPECT_FLOAT_EQ(kSecondPosition.leading, [card layout].position.leading); |
| 212 EXPECT_FLOAT_EQ(kSecondPosition.originY, [card layout].position.originY); |
| 213 EXPECT_FLOAT_EQ(kSecondSize.width, [card layout].size.width); |
| 214 EXPECT_FLOAT_EQ(kSecondSize.height, [card layout].size.height); |
| 215 // ... but view should still have the old attributes. |
| 216 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.frame.size.width); |
| 217 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.frame.size.height); |
| 218 EXPECT_FLOAT_EQ(kFirstFrame.origin.x, view.frame.origin.x); |
| 219 EXPECT_FLOAT_EQ(kFirstFrame.origin.y, view.frame.origin.y); |
| 220 [card setSynchronizeView:YES]; |
| 221 // View should immediately pick up the new attributes, with the origin |
| 222 // correctly pixel-aligned. |
| 223 EXPECT_FLOAT_EQ(kSecondFrame.size.width, view.frame.size.width); |
| 224 EXPECT_FLOAT_EQ(kSecondFrame.size.height, view.frame.size.height); |
| 225 const CGPoint kPixelAlignedOrigin = |
| 226 AlignRectOriginAndSizeToPixels(LayoutRectGetRect(kSecondLayout)).origin; |
| 227 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.x, view.frame.origin.x); |
| 228 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.y, view.frame.origin.y); |
| 229 } |
| 230 |
| 231 } // namespace |
OLD | NEW |