OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_collectio
n_utils.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #import "ios/chrome/test/base/scoped_block_swizzler.h" |
| 9 #include "testing/platform_test.h" |
| 10 #import "third_party/ocmock/OCMock/OCMock.h" |
| 11 |
| 12 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 13 #error "This file requires ARC support." |
| 14 #endif |
| 15 |
| 16 namespace content_suggestions { |
| 17 |
| 18 class ContentSuggestionsCollectionUtilsTest : public PlatformTest { |
| 19 public: |
| 20 void SetAsIPad() { |
| 21 device_type_swizzler_ = base::MakeUnique<ScopedBlockSwizzler>( |
| 22 [UIDevice class], @selector(userInterfaceIdiom), |
| 23 ^UIUserInterfaceIdiom(id self) { |
| 24 return UIUserInterfaceIdiomPad; |
| 25 }); |
| 26 } |
| 27 void SetAsIPhone() { |
| 28 device_type_swizzler_ = base::MakeUnique<ScopedBlockSwizzler>( |
| 29 [UIDevice class], @selector(userInterfaceIdiom), |
| 30 ^UIUserInterfaceIdiom(id self) { |
| 31 return UIUserInterfaceIdiomPhone; |
| 32 }); |
| 33 } |
| 34 |
| 35 private: |
| 36 std::unique_ptr<ScopedBlockSwizzler> device_type_swizzler_; |
| 37 }; |
| 38 |
| 39 TEST_F(ContentSuggestionsCollectionUtilsTest, orientationFramePortrait) { |
| 40 // Setup. |
| 41 CGRect rect1 = CGRectMake(10, 10, 0, 10); |
| 42 CGRect rect2 = CGRectMake(20, 20, 0, 20); |
| 43 const CGRect rects[2] = {rect1, rect2}; |
| 44 |
| 45 // Action. |
| 46 CGRect result = getOrientationFrame(rects, 400); |
| 47 |
| 48 // Tests. |
| 49 rect1.size.width = 380; |
| 50 EXPECT_TRUE(CGRectEqualToRect(rect1, result)); |
| 51 } |
| 52 |
| 53 TEST_F(ContentSuggestionsCollectionUtilsTest, orientationFrameLandscapeIPad) { |
| 54 // Setup. |
| 55 SetAsIPad(); |
| 56 CGRect rect1 = CGRectMake(10, 10, 0, 10); |
| 57 CGRect rect2 = CGRectMake(20, 20, 0, 20); |
| 58 const CGRect rects[2] = {rect1, rect2}; |
| 59 std::unique_ptr<ScopedBlockSwizzler> orientation_swizzler = |
| 60 base::MakeUnique<ScopedBlockSwizzler>( |
| 61 [UIApplication class], @selector(statusBarOrientation), |
| 62 ^UIInterfaceOrientation(id self) { |
| 63 return UIInterfaceOrientationLandscapeLeft; |
| 64 }); |
| 65 |
| 66 // Action. |
| 67 CGRect result = getOrientationFrame(rects, 400); |
| 68 |
| 69 // Tests. |
| 70 rect1.size.width = 380; |
| 71 EXPECT_TRUE(CGRectEqualToRect(rect1, result)); |
| 72 } |
| 73 |
| 74 TEST_F(ContentSuggestionsCollectionUtilsTest, orientationFrameLandscapeIPhone) { |
| 75 // Setup. |
| 76 SetAsIPhone(); |
| 77 CGRect rect1 = CGRectMake(10, 10, 0, 10); |
| 78 CGRect rect2 = CGRectMake(20, 20, 0, 20); |
| 79 const CGRect rects[2] = {rect1, rect2}; |
| 80 std::unique_ptr<ScopedBlockSwizzler> orientation_swizzler = |
| 81 base::MakeUnique<ScopedBlockSwizzler>( |
| 82 [UIApplication class], @selector(statusBarOrientation), |
| 83 ^UIInterfaceOrientation(id self) { |
| 84 return UIInterfaceOrientationLandscapeLeft; |
| 85 }); |
| 86 |
| 87 // Action. |
| 88 CGRect result = getOrientationFrame(rects, 400); |
| 89 |
| 90 // Tests. |
| 91 rect2.size.width = 360; |
| 92 EXPECT_TRUE(CGRectEqualToRect(rect2, result)); |
| 93 } |
| 94 |
| 95 TEST_F(ContentSuggestionsCollectionUtilsTest, centeredTilesMarginIPhone6) { |
| 96 // Setup. |
| 97 SetAsIPhone(); |
| 98 |
| 99 // Action. |
| 100 CGFloat result = centeredTilesMarginForWidth(374); |
| 101 |
| 102 // Tests. |
| 103 EXPECT_EQ(17, result); |
| 104 } |
| 105 |
| 106 TEST_F(ContentSuggestionsCollectionUtilsTest, centeredTilesMarginIPad) { |
| 107 // Setup. |
| 108 SetAsIPad(); |
| 109 |
| 110 // Action. |
| 111 CGFloat result = centeredTilesMarginForWidth(700); |
| 112 |
| 113 // Tests. |
| 114 EXPECT_EQ(168, result); |
| 115 } |
| 116 |
| 117 TEST_F(ContentSuggestionsCollectionUtilsTest, doodleFrameIPad) { |
| 118 // Setup. |
| 119 SetAsIPad(); |
| 120 |
| 121 // Action. |
| 122 CGRect result = doodleFrame(500, YES); |
| 123 |
| 124 // Test. |
| 125 EXPECT_TRUE(CGRectEqualToRect(CGRectMake(0, 82, 500, 120), result)); |
| 126 } |
| 127 |
| 128 TEST_F(ContentSuggestionsCollectionUtilsTest, doodleFrameIPhone) { |
| 129 // Setup. |
| 130 SetAsIPhone(); |
| 131 |
| 132 // Action. |
| 133 CGRect result = doodleFrame(500, YES); |
| 134 |
| 135 // Test. |
| 136 EXPECT_TRUE(CGRectEqualToRect(CGRectMake(0, 66, 500, 120), result)); |
| 137 } |
| 138 |
| 139 TEST_F(ContentSuggestionsCollectionUtilsTest, searchFieldFrameIPad) { |
| 140 // Setup. |
| 141 SetAsIPad(); |
| 142 CGFloat width = 500; |
| 143 CGFloat margin = centeredTilesMarginForWidth(width); |
| 144 |
| 145 // Action. |
| 146 CGRect result = searchFieldFrame(width, YES); |
| 147 |
| 148 // Test. |
| 149 EXPECT_TRUE( |
| 150 CGRectEqualToRect(CGRectMake(margin, 284, 500 - 2 * margin, 50), result)); |
| 151 } |
| 152 |
| 153 TEST_F(ContentSuggestionsCollectionUtilsTest, searchFieldFrameIPhone) { |
| 154 // Setup. |
| 155 SetAsIPhone(); |
| 156 CGFloat width = 500; |
| 157 CGFloat margin = centeredTilesMarginForWidth(width); |
| 158 |
| 159 // Action. |
| 160 CGRect result = searchFieldFrame(width, YES); |
| 161 |
| 162 // Test. |
| 163 EXPECT_TRUE( |
| 164 CGRectEqualToRect(CGRectMake(margin, 218, 500 - 2 * margin, 50), result)); |
| 165 } |
| 166 |
| 167 TEST_F(ContentSuggestionsCollectionUtilsTest, heightForLogoHeaderIPad) { |
| 168 // Setup. |
| 169 SetAsIPad(); |
| 170 |
| 171 // Action, tests. |
| 172 EXPECT_EQ(350, heightForLogoHeader(500, YES, YES)); |
| 173 EXPECT_EQ(406, heightForLogoHeader(500, YES, NO)); |
| 174 } |
| 175 |
| 176 TEST_F(ContentSuggestionsCollectionUtilsTest, heightForLogoHeaderIPhone) { |
| 177 // Setup. |
| 178 SetAsIPhone(); |
| 179 |
| 180 // Action, tests. |
| 181 EXPECT_EQ(284, heightForLogoHeader(500, YES, YES)); |
| 182 EXPECT_EQ(284, heightForLogoHeader(500, YES, NO)); |
| 183 } |
| 184 |
| 185 TEST_F(ContentSuggestionsCollectionUtilsTest, SizeIPhone6) { |
| 186 // Setup. |
| 187 SetAsIPhone(); |
| 188 |
| 189 // Test. |
| 190 EXPECT_EQ(4U, numberOfTilesForWidth(360)); |
| 191 } |
| 192 |
| 193 TEST_F(ContentSuggestionsCollectionUtilsTest, SizeIPhone5) { |
| 194 // Setup. |
| 195 SetAsIPhone(); |
| 196 |
| 197 // Test. |
| 198 EXPECT_EQ(3U, numberOfTilesForWidth(320)); |
| 199 } |
| 200 |
| 201 // Test for iPad portrait and iPhone landscape. |
| 202 TEST_F(ContentSuggestionsCollectionUtilsTest, SizeLarge) { |
| 203 // Test. |
| 204 EXPECT_EQ(4U, numberOfTilesForWidth(720)); |
| 205 } |
| 206 |
| 207 TEST_F(ContentSuggestionsCollectionUtilsTest, SizeIPadSplit) { |
| 208 // Setup. |
| 209 SetAsIPad(); |
| 210 |
| 211 // Test. |
| 212 EXPECT_EQ(3U, numberOfTilesForWidth(360)); |
| 213 } |
| 214 |
| 215 } // namespace content_suggestions |
OLD | NEW |