| 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 static_cast<CardView*>([[MockCardView alloc] initWithFrame:frame]); |
| 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 StackCardTest() { view_provider_ = [[MockCardViewProvider alloc] init]; } |
| 40 view_provider_.reset([[MockCardViewProvider alloc] init]); | |
| 41 } | |
| 42 | 43 |
| 43 base::scoped_nsobject<MockCardViewProvider> view_provider_; | 44 MockCardViewProvider* view_provider_; |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 TEST_F(StackCardTest, LazyCreation) { | 47 TEST_F(StackCardTest, LazyCreation) { |
| 47 base::scoped_nsobject<StackCard> card( | 48 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
| 48 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
| 49 // Set attributes before asking for the view. | 49 // Set attributes before asking for the view. |
| 50 LayoutRect layout = LayoutRectMake(10, 300, 20, 55, 98); | 50 LayoutRect layout = LayoutRectMake(10, 300, 20, 55, 98); |
| 51 CGRect frame = LayoutRectGetRect(layout); | 51 CGRect frame = LayoutRectGetRect(layout); |
| 52 [card setLayout:layout]; | 52 [card setLayout:layout]; |
| 53 // Ensure the view hasn't been created yet. | 53 // Ensure the view hasn't been created yet. |
| 54 EXPECT_FALSE([card viewIsLive]); | 54 EXPECT_FALSE([card viewIsLive]); |
| 55 // Make sure that the view actually has those attributes. | 55 // Make sure that the view actually has those attributes. |
| 56 UIView* view = [card view]; | 56 UIView* view = [card view]; |
| 57 EXPECT_FLOAT_EQ(frame.size.width, view.frame.size.width); | 57 EXPECT_FLOAT_EQ(frame.size.width, view.frame.size.width); |
| 58 EXPECT_FLOAT_EQ(frame.size.height, view.frame.size.height); | 58 EXPECT_FLOAT_EQ(frame.size.height, view.frame.size.height); |
| 59 EXPECT_FLOAT_EQ(frame.origin.x, view.frame.origin.x); | 59 EXPECT_FLOAT_EQ(frame.origin.x, view.frame.origin.x); |
| 60 EXPECT_FLOAT_EQ(frame.origin.y, view.frame.origin.y); | 60 EXPECT_FLOAT_EQ(frame.origin.y, view.frame.origin.y); |
| 61 } | 61 } |
| 62 | 62 |
| 63 TEST_F(StackCardTest, LiveViewUpdating) { | 63 TEST_F(StackCardTest, LiveViewUpdating) { |
| 64 base::scoped_nsobject<StackCard> card( | 64 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
| 65 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
| 66 // Get the view, then set attributes. | 65 // Get the view, then set attributes. |
| 67 UIView* view = [card view]; | 66 UIView* view = [card view]; |
| 68 LayoutRect layout = LayoutRectMake(10, 300, 20, 55, 98); | 67 LayoutRect layout = LayoutRectMake(10, 300, 20, 55, 98); |
| 69 CGRect frame = LayoutRectGetRect(layout); | 68 CGRect frame = LayoutRectGetRect(layout); |
| 70 [card setLayout:layout]; | 69 [card setLayout:layout]; |
| 71 // Make sure that the view actually has those attributes. | 70 // Make sure that the view actually has those attributes. |
| 72 EXPECT_FLOAT_EQ(frame.size.width, view.frame.size.width); | 71 EXPECT_FLOAT_EQ(frame.size.width, view.frame.size.width); |
| 73 EXPECT_FLOAT_EQ(frame.size.height, view.frame.size.height); | 72 EXPECT_FLOAT_EQ(frame.size.height, view.frame.size.height); |
| 74 EXPECT_FLOAT_EQ(frame.origin.x, view.frame.origin.x); | 73 EXPECT_FLOAT_EQ(frame.origin.x, view.frame.origin.x); |
| 75 EXPECT_FLOAT_EQ(frame.origin.y, view.frame.origin.y); | 74 EXPECT_FLOAT_EQ(frame.origin.y, view.frame.origin.y); |
| 76 } | 75 } |
| 77 | 76 |
| 78 TEST_F(StackCardTest, BoundsUpdatePreservesCenter) { | 77 TEST_F(StackCardTest, BoundsUpdatePreservesCenter) { |
| 79 base::scoped_nsobject<StackCard> card( | 78 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
| 80 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
| 81 LayoutRect layout = LayoutRectMake(0, 300, 0, 40, 100); | 79 LayoutRect layout = LayoutRectMake(0, 300, 0, 40, 100); |
| 82 CGRect frame = LayoutRectGetRect(layout); | 80 CGRect frame = LayoutRectGetRect(layout); |
| 83 [card setLayout:layout]; | 81 [card setLayout:layout]; |
| 84 // Changing the bounds should preserve the center (as with UIView). | 82 // Changing the bounds should preserve the center (as with UIView). |
| 85 [card setSize:CGSizeMake(10, 10)]; | 83 [card setSize:CGSizeMake(10, 10)]; |
| 86 CGRect newFrame = LayoutRectGetRect([card layout]); | 84 CGRect newFrame = LayoutRectGetRect([card layout]); |
| 87 EXPECT_FLOAT_EQ(CGRectGetMidX(frame), CGRectGetMidX(newFrame)); | 85 EXPECT_FLOAT_EQ(CGRectGetMidX(frame), CGRectGetMidX(newFrame)); |
| 88 EXPECT_FLOAT_EQ(CGRectGetMidY(frame), CGRectGetMidY(newFrame)); | 86 EXPECT_FLOAT_EQ(CGRectGetMidY(frame), CGRectGetMidY(newFrame)); |
| 89 } | 87 } |
| 90 | 88 |
| 91 TEST_F(StackCardTest, PixelAlignmentOfViewFrameAfterLiveUpdate) { | 89 TEST_F(StackCardTest, PixelAlignmentOfViewFrameAfterLiveUpdate) { |
| 92 base::scoped_nsobject<StackCard> card( | 90 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
| 93 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
| 94 // Get the view, then set attributes. | 91 // Get the view, then set attributes. |
| 95 UIView* view = [card view]; | 92 UIView* view = [card view]; |
| 96 const LayoutRectPosition kPosition = LayoutRectPositionMake(10.3, 20.4); | 93 const LayoutRectPosition kPosition = LayoutRectPositionMake(10.3, 20.4); |
| 97 const CGSize kSize = CGSizeMake(55, 98); | 94 const CGSize kSize = CGSizeMake(55, 98); |
| 98 const CGFloat kBoundingWidth = 300; | 95 const CGFloat kBoundingWidth = 300; |
| 99 const LayoutRect kLayout = | 96 const LayoutRect kLayout = |
| 100 LayoutRectMake(kPosition.leading, kBoundingWidth, kPosition.originY, | 97 LayoutRectMake(kPosition.leading, kBoundingWidth, kPosition.originY, |
| 101 kSize.width, kSize.height); | 98 kSize.width, kSize.height); |
| 102 [card setLayout:kLayout]; | 99 [card setLayout:kLayout]; |
| 103 EXPECT_FLOAT_EQ(kSize.width, view.frame.size.width); | 100 EXPECT_FLOAT_EQ(kSize.width, view.frame.size.width); |
| 104 EXPECT_FLOAT_EQ(kSize.height, view.frame.size.height); | 101 EXPECT_FLOAT_EQ(kSize.height, view.frame.size.height); |
| 105 EXPECT_FLOAT_EQ(kPosition.leading, [card layout].position.leading); | 102 EXPECT_FLOAT_EQ(kPosition.leading, [card layout].position.leading); |
| 106 EXPECT_FLOAT_EQ(kPosition.originY, [card layout].position.originY); | 103 EXPECT_FLOAT_EQ(kPosition.originY, [card layout].position.originY); |
| 107 // The view's origin should be pixel-aligned. | 104 // The view's origin should be pixel-aligned. |
| 108 const CGPoint kPixelAlignedOrigin = | 105 const CGPoint kPixelAlignedOrigin = |
| 109 AlignRectOriginAndSizeToPixels(LayoutRectGetRect(kLayout)).origin; | 106 AlignRectOriginAndSizeToPixels(LayoutRectGetRect(kLayout)).origin; |
| 110 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.x, view.frame.origin.x); | 107 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.x, view.frame.origin.x); |
| 111 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.y, view.frame.origin.y); | 108 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.y, view.frame.origin.y); |
| 112 } | 109 } |
| 113 | 110 |
| 114 TEST_F(StackCardTest, ViewFrameSynchronization) { | 111 TEST_F(StackCardTest, ViewFrameSynchronization) { |
| 115 base::scoped_nsobject<StackCard> card( | 112 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
| 116 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
| 117 // Get the view, then set attributes. | 113 // Get the view, then set attributes. |
| 118 UIView* view = [card view]; | 114 UIView* view = [card view]; |
| 119 const LayoutRect kFirstLayout = LayoutRectMake(10, 300, 20, 55, 98); | 115 const LayoutRect kFirstLayout = LayoutRectMake(10, 300, 20, 55, 98); |
| 120 CGRect firstFrame = LayoutRectGetRect(kFirstLayout); | 116 CGRect firstFrame = LayoutRectGetRect(kFirstLayout); |
| 121 [card setLayout:kFirstLayout]; | 117 [card setLayout:kFirstLayout]; |
| 122 // Make sure that the view actually has those attributes. | 118 // Make sure that the view actually has those attributes. |
| 123 EXPECT_FLOAT_EQ(firstFrame.size.width, view.frame.size.width); | 119 EXPECT_FLOAT_EQ(firstFrame.size.width, view.frame.size.width); |
| 124 EXPECT_FLOAT_EQ(firstFrame.size.height, view.frame.size.height); | 120 EXPECT_FLOAT_EQ(firstFrame.size.height, view.frame.size.height); |
| 125 EXPECT_FLOAT_EQ(firstFrame.origin.x, view.frame.origin.x); | 121 EXPECT_FLOAT_EQ(firstFrame.origin.x, view.frame.origin.x); |
| 126 EXPECT_FLOAT_EQ(firstFrame.origin.y, view.frame.origin.y); | 122 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); | 137 EXPECT_FLOAT_EQ(firstFrame.origin.y, view.frame.origin.y); |
| 142 [card setSynchronizeView:YES]; | 138 [card setSynchronizeView:YES]; |
| 143 // View should immediately pick up the new attributes. | 139 // View should immediately pick up the new attributes. |
| 144 EXPECT_FLOAT_EQ(secondFrame.size.width, view.frame.size.width); | 140 EXPECT_FLOAT_EQ(secondFrame.size.width, view.frame.size.width); |
| 145 EXPECT_FLOAT_EQ(secondFrame.size.height, view.frame.size.height); | 141 EXPECT_FLOAT_EQ(secondFrame.size.height, view.frame.size.height); |
| 146 EXPECT_FLOAT_EQ(secondFrame.origin.x, view.frame.origin.x); | 142 EXPECT_FLOAT_EQ(secondFrame.origin.x, view.frame.origin.x); |
| 147 EXPECT_FLOAT_EQ(secondFrame.origin.y, view.frame.origin.y); | 143 EXPECT_FLOAT_EQ(secondFrame.origin.y, view.frame.origin.y); |
| 148 } | 144 } |
| 149 | 145 |
| 150 TEST_F(StackCardTest, ViewLayoutSynchronization) { | 146 TEST_F(StackCardTest, ViewLayoutSynchronization) { |
| 151 base::scoped_nsobject<StackCard> card( | 147 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
| 152 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
| 153 // Get the view, then set attributes. | 148 // Get the view, then set attributes. |
| 154 UIView* view = [card view]; | 149 UIView* view = [card view]; |
| 155 const LayoutRect kFirstLayout = LayoutRectMake(30, 300, 40, 200, 100); | 150 const LayoutRect kFirstLayout = LayoutRectMake(30, 300, 40, 200, 100); |
| 156 const CGRect kFirstFrame = LayoutRectGetRect(kFirstLayout); | 151 const CGRect kFirstFrame = LayoutRectGetRect(kFirstLayout); |
| 157 [card setLayout:kFirstLayout]; | 152 [card setLayout:kFirstLayout]; |
| 158 // Make sure that the view actually has those attributes. | 153 // Make sure that the view actually has those attributes. |
| 159 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.bounds.size.width); | 154 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.bounds.size.width); |
| 160 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.bounds.size.height); | 155 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.bounds.size.height); |
| 161 EXPECT_FLOAT_EQ(CGRectGetMidX(kFirstFrame), view.center.x); | 156 EXPECT_FLOAT_EQ(CGRectGetMidX(kFirstFrame), view.center.x); |
| 162 EXPECT_FLOAT_EQ(CGRectGetMidY(kFirstFrame), view.center.y); | 157 EXPECT_FLOAT_EQ(CGRectGetMidY(kFirstFrame), view.center.y); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 178 EXPECT_FLOAT_EQ(CGRectGetMidY(kFirstFrame), view.center.y); | 173 EXPECT_FLOAT_EQ(CGRectGetMidY(kFirstFrame), view.center.y); |
| 179 [card setSynchronizeView:YES]; | 174 [card setSynchronizeView:YES]; |
| 180 // View should immediately pick up the new attributes. | 175 // View should immediately pick up the new attributes. |
| 181 EXPECT_FLOAT_EQ(kSecondFrame.size.width, view.bounds.size.width); | 176 EXPECT_FLOAT_EQ(kSecondFrame.size.width, view.bounds.size.width); |
| 182 EXPECT_FLOAT_EQ(kSecondFrame.size.height, view.bounds.size.height); | 177 EXPECT_FLOAT_EQ(kSecondFrame.size.height, view.bounds.size.height); |
| 183 EXPECT_FLOAT_EQ(CGRectGetMidX(kSecondFrame), view.center.x); | 178 EXPECT_FLOAT_EQ(CGRectGetMidX(kSecondFrame), view.center.x); |
| 184 EXPECT_FLOAT_EQ(CGRectGetMidY(kSecondFrame), view.center.y); | 179 EXPECT_FLOAT_EQ(CGRectGetMidY(kSecondFrame), view.center.y); |
| 185 } | 180 } |
| 186 | 181 |
| 187 TEST_F(StackCardTest, PixelAlignmentOfViewAfterSynchronization) { | 182 TEST_F(StackCardTest, PixelAlignmentOfViewAfterSynchronization) { |
| 188 base::scoped_nsobject<StackCard> card( | 183 StackCard* card = [[StackCard alloc] initWithViewProvider:view_provider_]; |
| 189 [[StackCard alloc] initWithViewProvider:view_provider_]); | |
| 190 // Get the view, then set attributes. | 184 // Get the view, then set attributes. |
| 191 UIView* view = [card view]; | 185 UIView* view = [card view]; |
| 192 const CGFloat kBoundingWidth = 300; | 186 const CGFloat kBoundingWidth = 300; |
| 193 const LayoutRect kFirstLayout = | 187 const LayoutRect kFirstLayout = |
| 194 LayoutRectMake(10, kBoundingWidth, 20, 55, 98); | 188 LayoutRectMake(10, kBoundingWidth, 20, 55, 98); |
| 195 const CGRect kFirstFrame = LayoutRectGetRect(kFirstLayout); | 189 const CGRect kFirstFrame = LayoutRectGetRect(kFirstLayout); |
| 196 [card setLayout:kFirstLayout]; | 190 [card setLayout:kFirstLayout]; |
| 197 // Make sure that the view actually has those attributes. | 191 // Make sure that the view actually has those attributes. |
| 198 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.frame.size.width); | 192 EXPECT_FLOAT_EQ(kFirstFrame.size.width, view.frame.size.width); |
| 199 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.frame.size.height); | 193 EXPECT_FLOAT_EQ(kFirstFrame.size.height, view.frame.size.height); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 222 // correctly pixel-aligned. | 216 // correctly pixel-aligned. |
| 223 EXPECT_FLOAT_EQ(kSecondFrame.size.width, view.frame.size.width); | 217 EXPECT_FLOAT_EQ(kSecondFrame.size.width, view.frame.size.width); |
| 224 EXPECT_FLOAT_EQ(kSecondFrame.size.height, view.frame.size.height); | 218 EXPECT_FLOAT_EQ(kSecondFrame.size.height, view.frame.size.height); |
| 225 const CGPoint kPixelAlignedOrigin = | 219 const CGPoint kPixelAlignedOrigin = |
| 226 AlignRectOriginAndSizeToPixels(LayoutRectGetRect(kSecondLayout)).origin; | 220 AlignRectOriginAndSizeToPixels(LayoutRectGetRect(kSecondLayout)).origin; |
| 227 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.x, view.frame.origin.x); | 221 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.x, view.frame.origin.x); |
| 228 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.y, view.frame.origin.y); | 222 EXPECT_FLOAT_EQ(kPixelAlignedOrigin.y, view.frame.origin.y); |
| 229 } | 223 } |
| 230 | 224 |
| 231 } // namespace | 225 } // namespace |
| OLD | NEW |