Chromium Code Reviews| Index: ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_utils_unittest.mm |
| diff --git a/ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_utils_unittest.mm b/ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_utils_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5ebf3570d4ff3d42fed4af46b7de7134dc7a9bdc |
| --- /dev/null |
| +++ b/ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_utils_unittest.mm |
| @@ -0,0 +1,174 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_utils.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "ios/chrome/browser/ui/ui_util.h" |
| +#import "ios/chrome/test/base/scoped_block_swizzler.h" |
| +#include "testing/platform_test.h" |
| +#import "third_party/ocmock/OCMock/OCMock.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +namespace content_suggestions { |
| + |
| +class ContentSuggestionsCollectionUtilsTest : public PlatformTest { |
| + public: |
| + void SetAsIPad() { |
| + device_type_swizzler_ = base::MakeUnique<ScopedBlockSwizzler>( |
| + [UIDevice class], @selector(userInterfaceIdiom), |
| + ^UIUserInterfaceIdiom(id self) { |
| + return UIUserInterfaceIdiomPad; |
| + }); |
| + } |
| + void SetAsIPhone() { |
| + device_type_swizzler_ = base::MakeUnique<ScopedBlockSwizzler>( |
| + [UIDevice class], @selector(userInterfaceIdiom), |
| + ^UIUserInterfaceIdiom(id self) { |
| + return UIUserInterfaceIdiomPhone; |
| + }); |
| + } |
| + |
| + private: |
| + std::unique_ptr<ScopedBlockSwizzler> device_type_swizzler_; |
| +}; |
| + |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, orientationFramePortrait) { |
| + // Setup. |
| + CGRect rect1 = CGRectMake(10, 10, 0, 10); |
| + CGRect rect2 = CGRectMake(20, 20, 0, 20); |
| + const CGRect rects[2] = {rect1, rect2}; |
| + |
| + // Action. |
| + CGRect result = getOrientationFrame(rects, 400); |
| + |
| + // Tests. |
| + rect1.size.width = 380; |
| + EXPECT_TRUE(CGRectEqualToRect(rect1, result)); |
| +} |
| + |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, orientationFrameLandscape) { |
| + // Setup. |
| + CGRect rect1 = CGRectMake(10, 10, 0, 10); |
| + CGRect rect2 = CGRectMake(20, 20, 0, 20); |
| + const CGRect rects[2] = {rect1, rect2}; |
| + std::unique_ptr<ScopedBlockSwizzler> orientation_swizzler = |
| + base::MakeUnique<ScopedBlockSwizzler>( |
| + [UIApplication class], @selector(statusBarOrientation), |
| + ^UIInterfaceOrientation(id self) { |
| + return UIInterfaceOrientationLandscapeLeft; |
| + }); |
| + |
| + // Action. |
| + CGRect result = getOrientationFrame(rects, 400); |
| + |
| + // Tests. |
| + rect1.size.width = 380; |
| + rect2.size.width = 360; |
| + if (IsIPadIdiom()) { |
|
marq (ping after 24h)
2017/05/10 11:17:26
Here (and elsewhere) -- for each of the tests with
gambard
2017/05/10 11:48:46
Done.
|
| + EXPECT_TRUE(CGRectEqualToRect(rect1, result)); |
| + } else { |
| + EXPECT_TRUE(CGRectEqualToRect(rect2, result)); |
| + } |
| +} |
| + |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, centeredTilesMarginIPhone6) { |
| + // Setup. |
| + SetAsIPhone(); |
| + |
| + // Action. |
| + CGFloat result = centeredTilesMarginForWidth(374); |
| + |
| + // Tests. |
| + EXPECT_EQ(17, result); |
| +} |
| + |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, centeredTilesMarginIPad) { |
| + // Setup. |
| + SetAsIPad(); |
| + |
| + // Action. |
| + CGFloat result = centeredTilesMarginForWidth(700); |
| + |
| + // Tests. |
| + EXPECT_EQ(168, result); |
| +} |
| + |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, doodleFrame) { |
| + // Setup. |
| + CGFloat yValue = 66; |
| + if (IsIPadIdiom()) |
| + yValue = 82; |
| + |
| + // Action. |
| + CGRect result = doodleFrame(500, YES); |
| + |
| + // Test. |
| + EXPECT_TRUE(CGRectEqualToRect(CGRectMake(0, yValue, 500, 120), result)); |
| +} |
| + |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, searchFieldFrame) { |
| + // Setup. |
| + CGFloat width = 500; |
| + CGFloat margin = centeredTilesMarginForWidth(width); |
| + CGFloat offset = 0; |
| + if (IsIPadIdiom()) |
| + offset = 66; |
| + |
| + // Action. |
| + CGRect result = searchFieldFrame(width, YES); |
| + |
| + // Test. |
| + EXPECT_TRUE(CGRectEqualToRect( |
| + CGRectMake(margin, 218 + offset, 500 - 2 * margin, 50), result)); |
| +} |
| + |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, heightForLogoHeader) { |
| + // Setup. |
| + CGFloat yValue = 284; |
| + CGFloat offset = 0; |
| + if (IsIPadIdiom()) { |
| + yValue = 350; |
| + offset = 56; |
| + } |
| + |
| + // Action, tests. |
| + EXPECT_EQ(yValue, heightForLogoHeader(500, YES, YES)); |
| + EXPECT_EQ(yValue + offset, heightForLogoHeader(500, YES, NO)); |
| +} |
| + |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, SizeIPhone6) { |
| + // Setup. |
| + SetAsIPhone(); |
| + |
| + // Test. |
| + EXPECT_EQ(4U, numberOfTilesForWidth(360)); |
| +} |
| + |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, SizeIPhone5) { |
| + // Setup. |
| + SetAsIPhone(); |
| + |
| + // Test. |
| + EXPECT_EQ(3U, numberOfTilesForWidth(320)); |
| +} |
| + |
| +// Test for iPad portrait and iPhone landscape. |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, SizeLarge) { |
| + // Test. |
| + EXPECT_EQ(4U, numberOfTilesForWidth(720)); |
| +} |
| + |
| +TEST_F(ContentSuggestionsCollectionUtilsTest, SizeIPadSplit) { |
| + // Setup. |
| + SetAsIPad(); |
| + |
| + // Test. |
| + EXPECT_EQ(3U, numberOfTilesForWidth(360)); |
| +} |
| + |
| +} // namespace content_suggestions |