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/mac/scoped_nsautorelease_pool.h" | |
8 #include "base/mac/scoped_nsobject.h" | |
9 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
10 #import "ios/chrome/browser/tabs/tab.h" | 8 #import "ios/chrome/browser/tabs/tab.h" |
11 #import "ios/chrome/browser/tabs/tab_model.h" | 9 #import "ios/chrome/browser/tabs/tab_model.h" |
12 #import "ios/chrome/browser/tabs/tab_model_observer.h" | 10 #import "ios/chrome/browser/tabs/tab_model_observer.h" |
13 #include "ios/chrome/browser/ui/rtl_geometry.h" | 11 #include "ios/chrome/browser/ui/rtl_geometry.h" |
14 #import "ios/chrome/browser/ui/stack_view/card_set.h" | 12 #import "ios/chrome/browser/ui/stack_view/card_set.h" |
15 #import "ios/chrome/browser/ui/stack_view/card_stack_layout_manager.h" | 13 #import "ios/chrome/browser/ui/stack_view/card_stack_layout_manager.h" |
16 #import "ios/chrome/browser/ui/stack_view/stack_card.h" | 14 #import "ios/chrome/browser/ui/stack_view/stack_card.h" |
17 #import "ios/testing/ocmock_complex_type_helper.h" | 15 #import "ios/testing/ocmock_complex_type_helper.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "testing/gtest_mac.h" | 17 #include "testing/gtest_mac.h" |
20 #include "testing/platform_test.h" | 18 #include "testing/platform_test.h" |
21 #import "third_party/ocmock/OCMock/OCMock.h" | 19 #import "third_party/ocmock/OCMock/OCMock.h" |
22 #import "third_party/ocmock/gtest_support.h" | 20 #import "third_party/ocmock/gtest_support.h" |
23 | 21 |
| 22 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 23 #error "This file requires ARC support." |
| 24 #endif |
| 25 |
24 @interface MockTabModel : NSObject<NSFastEnumeration> { | 26 @interface MockTabModel : NSObject<NSFastEnumeration> { |
25 @private | 27 @private |
26 base::scoped_nsobject<NSMutableArray> tabs_; | 28 NSMutableArray* tabs_; |
27 id<TabModelObserver> observer_; // weak | 29 __weak id<TabModelObserver> observer_; |
28 } | 30 } |
29 | 31 |
30 // Adds a new mock tab with the given properties. | 32 // Adds a new mock tab with the given properties. |
31 - (void)addTabWithTitle:(NSString*)title location:(const GURL&)url; | 33 - (void)addTabWithTitle:(NSString*)title location:(const GURL&)url; |
32 | 34 |
33 // TabModel mocking. | 35 // TabModel mocking. |
34 - (NSUInteger)count; | 36 - (NSUInteger)count; |
35 - (BOOL)isOffTheRecord; | 37 - (BOOL)isOffTheRecord; |
36 - (Tab*)currentTab; | 38 - (Tab*)currentTab; |
37 - (NSUInteger)indexOfTab:(Tab*)tab; | 39 - (NSUInteger)indexOfTab:(Tab*)tab; |
(...skipping 11 matching lines...) Expand all Loading... |
49 | 51 |
50 - (const GURL&)url { | 52 - (const GURL&)url { |
51 return static_cast<CardSetTestTabMock_url>([self blockForSelector:_cmd])(); | 53 return static_cast<CardSetTestTabMock_url>([self blockForSelector:_cmd])(); |
52 } | 54 } |
53 @end | 55 @end |
54 | 56 |
55 @implementation MockTabModel | 57 @implementation MockTabModel |
56 | 58 |
57 - (id)init { | 59 - (id)init { |
58 if ((self = [super init])) { | 60 if ((self = [super init])) { |
59 tabs_.reset([[NSMutableArray alloc] init]); | 61 tabs_ = [[NSMutableArray alloc] init]; |
60 } | 62 } |
61 return self; | 63 return self; |
62 } | 64 } |
63 | 65 |
64 - (void)addTabWithTitle:(NSString*)title location:(const GURL&)url { | 66 - (void)addTabWithTitle:(NSString*)title location:(const GURL&)url { |
65 base::scoped_nsobject<id> tab([[CardSetTestTabMock alloc] | 67 id tab = [[CardSetTestTabMock alloc] |
66 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]); | 68 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
67 UIView* dummyView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; | 69 UIView* dummyView = [[UIView alloc] initWithFrame:CGRectZero]; |
68 static int sCounter = 0; | 70 static int sCounter = 0; |
69 NSString* sessionID = [NSString stringWithFormat:@"%d", sCounter++]; | 71 NSString* sessionID = [NSString stringWithFormat:@"%d", sCounter++]; |
70 BOOL no = NO; | 72 BOOL no = NO; |
71 [[[tab stub] andReturn:dummyView] view]; | 73 [[[tab stub] andReturn:dummyView] view]; |
72 base::scoped_nsobject<id> block([^{ | 74 id block = [^{ |
73 return (const GURL&)url; | 75 return (const GURL&)url; |
74 } copy]); | 76 } copy]; |
75 [tab onSelector:@selector(url) callBlockExpectation:block]; | 77 [tab onSelector:@selector(url) callBlockExpectation:block]; |
76 | 78 |
77 [[tab expect] retrieveSnapshot:[OCMArg any]]; | 79 [[tab expect] retrieveSnapshot:[OCMArg any]]; |
78 [[[tab stub] andReturn:nil] webController]; | 80 [[[tab stub] andReturn:nil] webController]; |
79 [[[tab stub] andReturn:nil] favicon]; | 81 [[[tab stub] andReturn:nil] favicon]; |
80 [[[tab stub] andReturnValue:OCMOCK_VALUE(no)] canGoBack]; | 82 [[[tab stub] andReturnValue:OCMOCK_VALUE(no)] canGoBack]; |
81 [[[tab stub] andReturnValue:OCMOCK_VALUE(no)] canGoForward]; | 83 [[[tab stub] andReturnValue:OCMOCK_VALUE(no)] canGoForward]; |
82 [[[tab stub] andReturn:title] title]; | 84 [[[tab stub] andReturn:title] title]; |
83 [[[tab stub] andReturn:sessionID] tabId]; | 85 [[[tab stub] andReturn:sessionID] tabId]; |
84 | 86 |
85 [tabs_ addObject:tab]; | 87 [tabs_ addObject:tab]; |
86 [observer_ tabModel:(TabModel*)self | 88 [observer_ tabModel:(TabModel*)self |
87 didInsertTab:tab | 89 didInsertTab:tab |
88 atIndex:([tabs_ count] - 1) | 90 atIndex:([tabs_ count] - 1) |
89 inForeground:YES]; | 91 inForeground:YES]; |
90 } | 92 } |
91 | 93 |
92 - (void)removeTabAtIndex:(NSUInteger)index { | 94 - (void)removeTabAtIndex:(NSUInteger)index { |
93 id tab = [tabs_ objectAtIndex:index]; | 95 id tab = [tabs_ objectAtIndex:index]; |
94 [[tab retain] autorelease]; | |
95 [tabs_ removeObjectAtIndex:index]; | 96 [tabs_ removeObjectAtIndex:index]; |
96 | 97 |
97 // A tab was removed at the given index. | 98 // A tab was removed at the given index. |
98 [observer_ tabModel:(TabModel*)self didRemoveTab:tab atIndex:index]; | 99 [observer_ tabModel:(TabModel*)self didRemoveTab:tab atIndex:index]; |
99 } | 100 } |
100 | 101 |
101 - (NSUInteger)count { | 102 - (NSUInteger)count { |
102 return [tabs_ count]; | 103 return [tabs_ count]; |
103 } | 104 } |
104 | 105 |
(...skipping 17 matching lines...) Expand all Loading... |
122 | 123 |
123 - (void)addObserver:(id<TabModelObserver>)observer { | 124 - (void)addObserver:(id<TabModelObserver>)observer { |
124 observer_ = observer; | 125 observer_ = observer; |
125 } | 126 } |
126 | 127 |
127 - (void)removeObserver:(id<TabModelObserver>)observer { | 128 - (void)removeObserver:(id<TabModelObserver>)observer { |
128 observer_ = nil; | 129 observer_ = nil; |
129 } | 130 } |
130 | 131 |
131 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState*)state | 132 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState*)state |
132 objects:(id*)stackbuf | 133 objects:(__unsafe_unretained id*)stackbuf |
133 count:(NSUInteger)len { | 134 count:(NSUInteger)len { |
134 return [tabs_ countByEnumeratingWithState:state objects:stackbuf count:len]; | 135 return [tabs_ countByEnumeratingWithState:state objects:stackbuf count:len]; |
135 } | 136 } |
136 | 137 |
137 - (id<TabModelObserver>)observer { | 138 - (id<TabModelObserver>)observer { |
138 return observer_; | 139 return observer_; |
139 } | 140 } |
140 | 141 |
141 @end | 142 @end |
142 | 143 |
(...skipping 13 matching lines...) Expand all Loading... |
156 [[[stackModelMock stub] andReturnValue:OCMOCK_VALUE(lastStartStackCardIndex)] | 157 [[[stackModelMock stub] andReturnValue:OCMOCK_VALUE(lastStartStackCardIndex)] |
157 lastStartStackCardIndex]; | 158 lastStartStackCardIndex]; |
158 NSArray* cards = @[]; | 159 NSArray* cards = @[]; |
159 [[[stackModelMock stub] andReturn:cards] cards]; | 160 [[[stackModelMock stub] andReturn:cards] cards]; |
160 return (CardStackLayoutManager*)stackModelMock; | 161 return (CardStackLayoutManager*)stackModelMock; |
161 } | 162 } |
162 | 163 |
163 class CardSetTest : public PlatformTest { | 164 class CardSetTest : public PlatformTest { |
164 protected: | 165 protected: |
165 virtual void SetUpWithTabs(int nb_tabs) { | 166 virtual void SetUpWithTabs(int nb_tabs) { |
166 tab_model_.reset([[MockTabModel alloc] init]); | 167 tab_model_ = [[MockTabModel alloc] init]; |
167 | 168 |
168 for (int i = 0; i < nb_tabs; ++i) { | 169 for (int i = 0; i < nb_tabs; ++i) { |
169 std::string url = base::StringPrintf("http://%d.example.com", i); | 170 std::string url = base::StringPrintf("http://%d.example.com", i); |
170 [tab_model_ addTabWithTitle:@"NewTab" location:GURL(url)]; | 171 [tab_model_ addTabWithTitle:@"NewTab" location:GURL(url)]; |
171 } | 172 } |
172 | 173 |
173 card_set_.reset( | 174 card_set_ = [[CardSet alloc] initWithModel:(TabModel*)tab_model_]; |
174 [[CardSet alloc] initWithModel:(TabModel*)tab_model_.get()]); | |
175 | 175 |
176 display_view_.reset( | 176 display_view_ = [[UIView alloc] |
177 [[UIView alloc] initWithFrame:CGRectMake(0, 0, kViewportDimension, | 177 initWithFrame:CGRectMake(0, 0, kViewportDimension, kViewportDimension)]; |
178 kViewportDimension)]); | |
179 // Do some initial configuration of the card set. | 178 // Do some initial configuration of the card set. |
180 [card_set_ setDisplayView:display_view_]; | 179 [card_set_ setDisplayView:display_view_]; |
181 [card_set_ setCardSize:CGSizeMake(kCardDimension, kCardDimension)]; | 180 [card_set_ setCardSize:CGSizeMake(kCardDimension, kCardDimension)]; |
182 [card_set_ setLayoutAxisPosition:(kViewportDimension / 2.0) isVertical:YES]; | 181 [card_set_ setLayoutAxisPosition:(kViewportDimension / 2.0) isVertical:YES]; |
183 [card_set_ configureLayoutParametersWithMargin:10]; | 182 [card_set_ configureLayoutParametersWithMargin:10]; |
184 } | 183 } |
185 | 184 |
186 void SetUp() override { SetUpWithTabs(2); } | 185 void SetUp() override { SetUpWithTabs(2); } |
187 | 186 |
188 base::scoped_nsobject<MockTabModel> tab_model_; | 187 MockTabModel* tab_model_; |
189 base::scoped_nsobject<UIView> display_view_; | 188 UIView* display_view_; |
190 base::scoped_nsobject<CardSet> card_set_; | 189 CardSet* card_set_; |
191 }; | 190 }; |
192 | 191 |
193 TEST_F(CardSetTest, InitialLayoutState) { | 192 TEST_F(CardSetTest, InitialLayoutState) { |
194 NSArray* cards = [card_set_ cards]; | 193 NSArray* cards = [card_set_ cards]; |
195 EXPECT_EQ([tab_model_ count], [cards count]); | 194 EXPECT_EQ([tab_model_ count], [cards count]); |
196 | 195 |
197 // Tabs should be on the trailing side. | 196 // Tabs should be on the trailing side. |
198 EXPECT_EQ(CardCloseButtonSide::TRAILING, [card_set_ closeButtonSide]); | 197 EXPECT_EQ(CardCloseButtonSide::TRAILING, [card_set_ closeButtonSide]); |
199 | 198 |
200 // At least one card should be visible after layout, and cards should have | 199 // At least one card should be visible after layout, and cards should have |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 | 362 |
364 TEST_F(CardSetTest, setTabModel) { | 363 TEST_F(CardSetTest, setTabModel) { |
365 SetUpWithTabs(0); | 364 SetUpWithTabs(0); |
366 [card_set_ setTabModel:nil]; | 365 [card_set_ setTabModel:nil]; |
367 EXPECT_TRUE([tab_model_ observer] == nil); | 366 EXPECT_TRUE([tab_model_ observer] == nil); |
368 [card_set_ setTabModel:static_cast<id>(tab_model_)]; | 367 [card_set_ setTabModel:static_cast<id>(tab_model_)]; |
369 EXPECT_NSEQ(card_set_, static_cast<id>([tab_model_ observer])); | 368 EXPECT_NSEQ(card_set_, static_cast<id>([tab_model_ observer])); |
370 } | 369 } |
371 | 370 |
372 } // namespace | 371 } // namespace |
OLD | NEW |