| 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 #import <QuartzCore/QuartzCore.h> | 5 #import <QuartzCore/QuartzCore.h> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 9 #include "base/mac/scoped_nsobject.h" | |
| 10 #import "ios/chrome/browser/tabs/tab_model.h" | 8 #import "ios/chrome/browser/tabs/tab_model.h" |
| 11 #import "ios/chrome/browser/ui/stack_view/card_set.h" | 9 #import "ios/chrome/browser/ui/stack_view/card_set.h" |
| 12 #import "ios/chrome/browser/ui/stack_view/stack_card.h" | 10 #import "ios/chrome/browser/ui/stack_view/stack_card.h" |
| 13 #import "ios/chrome/browser/ui/stack_view/stack_view_controller.h" | 11 #import "ios/chrome/browser/ui/stack_view/stack_view_controller.h" |
| 14 #import "ios/chrome/browser/ui/stack_view/stack_view_controller_private.h" | 12 #import "ios/chrome/browser/ui/stack_view/stack_view_controller_private.h" |
| 15 #include "ios/chrome/test/block_cleanup_test.h" | 13 #include "ios/chrome/test/block_cleanup_test.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "testing/gtest_mac.h" | 15 #include "testing/gtest_mac.h" |
| 18 #include "third_party/ocmock/OCMock/OCMock.h" | 16 #include "third_party/ocmock/OCMock/OCMock.h" |
| 19 | 17 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 19 #error "This file requires ARC support." |
| 20 #endif |
| 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 const CGFloat kViewportDimension = 200; | 24 const CGFloat kViewportDimension = 200; |
| 23 | 25 |
| 24 } // namespace | 26 } // namespace |
| 25 | 27 |
| 26 #pragma mark - | 28 #pragma mark - |
| 27 | 29 |
| 28 @interface MockCardSet : NSObject { | 30 @interface MockCardSet : NSObject { |
| 29 @private | 31 @private |
| 30 UIView* displayView_; | 32 UIView* displayView_; |
| 31 CGSize cardSize_; | 33 CGSize cardSize_; |
| 32 CGFloat layoutAxisPosition_; | 34 CGFloat layoutAxisPosition_; |
| 33 BOOL initialConfigurationSet_; | 35 BOOL initialConfigurationSet_; |
| 34 id observer_; | |
| 35 } | 36 } |
| 36 | 37 |
| 37 // CardSet simulation | 38 // CardSet simulation |
| 38 @property(nonatomic, retain, readwrite) UIView* displayView; | 39 @property(nonatomic, strong, readwrite) UIView* displayView; |
| 39 @property(nonatomic, assign, readwrite) CGSize cardSize; | 40 @property(nonatomic, assign, readwrite) CGSize cardSize; |
| 40 @property(nonatomic, assign, readwrite) id<CardSetObserver> observer; | 41 @property(nonatomic, weak, readwrite) id<CardSetObserver> observer; |
| 41 @property(nonatomic, assign, readwrite) BOOL keepOnlyVisibleCardViewsAlive; | 42 @property(nonatomic, assign, readwrite) BOOL keepOnlyVisibleCardViewsAlive; |
| 42 | 43 |
| 43 - (void)configureLayoutParametersWithMargin:(CGFloat)margin; | 44 - (void)configureLayoutParametersWithMargin:(CGFloat)margin; |
| 44 - (void)setLayoutAxisPosition:(CGFloat)position | 45 - (void)setLayoutAxisPosition:(CGFloat)position |
| 45 isVertical:(BOOL)layoutIsVertical; | 46 isVertical:(BOOL)layoutIsVertical; |
| 46 - (void)clearGestureRecognizerTargetAndDelegateFromCards:(id)object; | 47 - (void)clearGestureRecognizerTargetAndDelegateFromCards:(id)object; |
| 47 - (NSArray*)cards; | 48 - (NSArray*)cards; |
| 48 - (CGFloat)maximumStackLength; | 49 - (CGFloat)maximumStackLength; |
| 49 - (void)displayViewSizeWasChanged; | 50 - (void)displayViewSizeWasChanged; |
| 50 - (TabModel*)tabModel; | 51 - (TabModel*)tabModel; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 | 105 |
| 105 #pragma mark - | 106 #pragma mark - |
| 106 | 107 |
| 107 namespace { | 108 namespace { |
| 108 | 109 |
| 109 class StackViewControllerTest : public BlockCleanupTest { | 110 class StackViewControllerTest : public BlockCleanupTest { |
| 110 protected: | 111 protected: |
| 111 void SetUp() override { | 112 void SetUp() override { |
| 112 BlockCleanupTest::SetUp(); | 113 BlockCleanupTest::SetUp(); |
| 113 | 114 |
| 114 main_card_set_.reset([[MockCardSet alloc] init]); | 115 main_card_set_ = [[MockCardSet alloc] init]; |
| 115 otr_card_set_.reset([[MockCardSet alloc] init]); | 116 otr_card_set_ = [[MockCardSet alloc] init]; |
| 116 | 117 |
| 117 view_controller_.reset([[StackViewController alloc] | 118 view_controller_ = [[StackViewController alloc] |
| 118 initWithMainCardSet:(CardSet*)main_card_set_.get() | 119 initWithMainCardSet:static_cast<CardSet*>(main_card_set_) |
| 119 otrCardSet:(CardSet*)otr_card_set_.get() | 120 otrCardSet:static_cast<CardSet*>(otr_card_set_) |
| 120 activeCardSet:(CardSet*)main_card_set_.get()]); | 121 activeCardSet:static_cast<CardSet*>(main_card_set_)]; |
| 121 // Resize the view and call VC lifecycle events | 122 // Resize the view and call VC lifecycle events |
| 122 [view_controller_ view].frame = | 123 [view_controller_ view].frame = |
| 123 CGRectMake(0.0, 0.0, kViewportDimension, kViewportDimension); | 124 CGRectMake(0.0, 0.0, kViewportDimension, kViewportDimension); |
| 124 // Simulate displaying the view. | 125 // Simulate displaying the view. |
| 125 [view_controller_ viewWillAppear:NO]; | 126 [view_controller_ viewWillAppear:NO]; |
| 126 } | 127 } |
| 127 void TearDown() override { | 128 void TearDown() override { |
| 128 // The view controller uses a delayed selector call, so in the unittests | 129 // The view controller uses a delayed selector call, so in the unittests |
| 129 // that causes the controller to be retained and outlive the test. | 130 // that causes the controller to be retained and outlive the test. |
| 130 [NSObject cancelPreviousPerformRequestsWithTarget:view_controller_.get()]; | 131 [NSObject cancelPreviousPerformRequestsWithTarget:view_controller_]; |
| 131 // And there are likely animations still running. | 132 // And there are likely animations still running. |
| 132 for (UIView* view in [[view_controller_ scrollView] subviews]) { | 133 for (UIView* view in [[view_controller_ scrollView] subviews]) { |
| 133 // Remove any animations on cards themselves. | 134 // Remove any animations on cards themselves. |
| 134 for (UIView* subview in [view subviews]) { | 135 for (UIView* subview in [view subviews]) { |
| 135 [subview.layer removeAllAnimations]; | 136 [subview.layer removeAllAnimations]; |
| 136 } | 137 } |
| 137 [view.layer removeAllAnimations]; | 138 [view.layer removeAllAnimations]; |
| 138 } | 139 } |
| 139 [[[view_controller_ scrollView] layer] removeAllAnimations]; | 140 [[[view_controller_ scrollView] layer] removeAllAnimations]; |
| 140 | 141 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } else { | 177 } else { |
| 177 EXPECT_FLOAT_EQ(display_view_origin.x, content_offset); | 178 EXPECT_FLOAT_EQ(display_view_origin.x, content_offset); |
| 178 EXPECT_FLOAT_EQ(display_view_origin.y, 0); | 179 EXPECT_FLOAT_EQ(display_view_origin.y, 0); |
| 179 EXPECT_GT([[view_controller_ scrollView] contentSize].width, | 180 EXPECT_GT([[view_controller_ scrollView] contentSize].width, |
| 180 [card_set maximumStackLength]); | 181 [card_set maximumStackLength]); |
| 181 EXPECT_FLOAT_EQ(card_set.displayView.bounds.size.height, | 182 EXPECT_FLOAT_EQ(card_set.displayView.bounds.size.height, |
| 182 [[view_controller_ scrollView] contentSize].height); | 183 [[view_controller_ scrollView] contentSize].height); |
| 183 } | 184 } |
| 184 } | 185 } |
| 185 | 186 |
| 186 base::scoped_nsobject<StackViewController> view_controller_; | 187 StackViewController* view_controller_; |
| 187 base::scoped_nsobject<MockCardSet> main_card_set_; | 188 MockCardSet* main_card_set_; |
| 188 base::scoped_nsobject<MockCardSet> otr_card_set_; | 189 MockCardSet* otr_card_set_; |
| 189 }; | 190 }; |
| 190 | 191 |
| 191 TEST_F(StackViewControllerTest, BasicConfiguration) { | 192 TEST_F(StackViewControllerTest, BasicConfiguration) { |
| 192 // Ensure that the CardSet is configured and correctly laid out. | 193 // Ensure that the CardSet is configured and correctly laid out. |
| 193 EXPECT_TRUE([main_card_set_ configured]); | 194 EXPECT_TRUE([main_card_set_ configured]); |
| 194 EXPECT_TRUE([otr_card_set_ configured]); | 195 EXPECT_TRUE([otr_card_set_ configured]); |
| 195 ValidateDisplaySetupForCardSet(main_card_set_); | 196 ValidateDisplaySetupForCardSet(main_card_set_); |
| 196 ValidateDisplaySetupForCardSet(otr_card_set_); | 197 ValidateDisplaySetupForCardSet(otr_card_set_); |
| 197 EXPECT_GT([main_card_set_ cardSize].width, 0U); | 198 EXPECT_GT([main_card_set_ cardSize].width, 0U); |
| 198 EXPECT_GT([main_card_set_ cardSize].height, 0U); | 199 EXPECT_GT([main_card_set_ cardSize].height, 0U); |
| 199 | 200 |
| 200 // Incognito should always be right of (or below in landscape) the main set. | 201 // Incognito should always be right of (or below in landscape) the main set. |
| 201 EXPECT_GT([otr_card_set_ layoutAxisPosition], | 202 EXPECT_GT([otr_card_set_ layoutAxisPosition], |
| 202 [main_card_set_ layoutAxisPosition]); | 203 [main_card_set_ layoutAxisPosition]); |
| 203 } | 204 } |
| 204 | 205 |
| 205 TEST_F(StackViewControllerTest, IncognitoHandling) { | 206 TEST_F(StackViewControllerTest, IncognitoHandling) { |
| 206 EXPECT_FALSE([view_controller_ isCurrentSetIncognito]); | 207 EXPECT_FALSE([view_controller_ isCurrentSetIncognito]); |
| 207 EXPECT_EQ((CardSet*)otr_card_set_.get(), [view_controller_ inactiveCardSet]); | 208 EXPECT_EQ(static_cast<CardSet*>(otr_card_set_), |
| 209 [view_controller_ inactiveCardSet]); |
| 208 [view_controller_ setActiveCardSet:[view_controller_ inactiveCardSet]]; | 210 [view_controller_ setActiveCardSet:[view_controller_ inactiveCardSet]]; |
| 209 EXPECT_TRUE([view_controller_ isCurrentSetIncognito]); | 211 EXPECT_TRUE([view_controller_ isCurrentSetIncognito]); |
| 210 EXPECT_EQ((CardSet*)main_card_set_.get(), [view_controller_ inactiveCardSet]); | 212 EXPECT_EQ(static_cast<CardSet*>(main_card_set_), |
| 213 [view_controller_ inactiveCardSet]); |
| 211 // Incognito should always be right of (or below in landscape) the main set. | 214 // Incognito should always be right of (or below in landscape) the main set. |
| 212 EXPECT_GT([otr_card_set_ layoutAxisPosition], | 215 EXPECT_GT([otr_card_set_ layoutAxisPosition], |
| 213 [main_card_set_ layoutAxisPosition]); | 216 [main_card_set_ layoutAxisPosition]); |
| 214 } | 217 } |
| 215 | 218 |
| 216 TEST_F(StackViewControllerTest, ScrollViewContentOffsetRecentering) { | 219 TEST_F(StackViewControllerTest, ScrollViewContentOffsetRecentering) { |
| 217 BOOL is_portrait = UIInterfaceOrientationIsPortrait( | 220 BOOL is_portrait = UIInterfaceOrientationIsPortrait( |
| 218 [UIApplication sharedApplication].statusBarOrientation); | 221 [UIApplication sharedApplication].statusBarOrientation); |
| 219 CGFloat scroll_view_breadth = | 222 CGFloat scroll_view_breadth = |
| 220 is_portrait ? [[view_controller_ scrollView] contentSize].width | 223 is_portrait ? [[view_controller_ scrollView] contentSize].width |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 ValidateDisplaySetupForCardSet(otr_card_set_); | 256 ValidateDisplaySetupForCardSet(otr_card_set_); |
| 254 } | 257 } |
| 255 | 258 |
| 256 TEST_F(StackViewControllerTest, TabModelReset) { | 259 TEST_F(StackViewControllerTest, TabModelReset) { |
| 257 [view_controller_ setOtrTabModel:nil]; | 260 [view_controller_ setOtrTabModel:nil]; |
| 258 ValidateDisplaySetupForCardSet(main_card_set_); | 261 ValidateDisplaySetupForCardSet(main_card_set_); |
| 259 ValidateDisplaySetupForCardSet(otr_card_set_); | 262 ValidateDisplaySetupForCardSet(otr_card_set_); |
| 260 } | 263 } |
| 261 | 264 |
| 262 } // namespace | 265 } // namespace |
| OLD | NEW |