OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/mac/scoped_nsobject.h" |
| 6 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar.h" |
| 7 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar_item.h" |
| 8 #include "ios/chrome/browser/ui/ui_util.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "testing/gtest_mac.h" |
| 11 #include "testing/platform_test.h" |
| 12 #import "third_party/ocmock/OCMock/OCMock.h" |
| 13 #import "third_party/ocmock/gtest_support.h" |
| 14 |
| 15 @interface NewTabPageBar (Testing) |
| 16 - (void)buttonDidTap:(UIButton*)button; |
| 17 @end |
| 18 |
| 19 namespace { |
| 20 |
| 21 class NewTabPageBarTest : public PlatformTest { |
| 22 protected: |
| 23 void SetUp() override { |
| 24 CGRect frame = CGRectMake(0, 0, 320, 44); |
| 25 bar_.reset([[NewTabPageBar alloc] initWithFrame:frame]); |
| 26 }; |
| 27 base::scoped_nsobject<NewTabPageBar> bar_; |
| 28 }; |
| 29 |
| 30 TEST_F(NewTabPageBarTest, SetItems) { |
| 31 NewTabPageBarItem* firstItem = [NewTabPageBarItem |
| 32 newTabPageBarItemWithTitle:@"First" |
| 33 identifier:1 |
| 34 image:[UIImage imageNamed:@"ntp_bookmarks"]]; |
| 35 // Tests that identifier test function can return both true and false. |
| 36 EXPECT_TRUE(firstItem.identifier == 1U); |
| 37 |
| 38 NewTabPageBarItem* secondItem = [NewTabPageBarItem |
| 39 newTabPageBarItemWithTitle:@"Second" |
| 40 identifier:2 |
| 41 image:[UIImage imageNamed:@"ntp_bookmarks"]]; |
| 42 NewTabPageBarItem* thirdItem = [NewTabPageBarItem |
| 43 newTabPageBarItemWithTitle:@"Third" |
| 44 identifier:3 |
| 45 image:[UIImage imageNamed:@"ntp_bookmarks"]]; |
| 46 |
| 47 [bar_ setItems:[NSArray arrayWithObject:firstItem]]; |
| 48 EXPECT_EQ(bar_.get().buttons.count, 1U); |
| 49 [bar_ setItems:[NSArray arrayWithObjects:firstItem, secondItem, nil]]; |
| 50 EXPECT_EQ(bar_.get().buttons.count, 2U); |
| 51 [bar_ setItems:[NSArray |
| 52 arrayWithObjects:firstItem, secondItem, thirdItem, nil]]; |
| 53 EXPECT_EQ(bar_.get().buttons.count, 3U); |
| 54 [bar_ setItems:[NSArray arrayWithObject:firstItem]]; |
| 55 EXPECT_EQ(bar_.get().buttons.count, 1U); |
| 56 } |
| 57 |
| 58 TEST_F(NewTabPageBarTest, SetSelectedIndex_iPadOnly) { |
| 59 // Selected index isn't meaningful on iPhone. |
| 60 if (!IsIPadIdiom()) { |
| 61 return; |
| 62 } |
| 63 |
| 64 NewTabPageBarItem* firstItem = [NewTabPageBarItem |
| 65 newTabPageBarItemWithTitle:@"First" |
| 66 identifier:1 |
| 67 image:[UIImage imageNamed:@"ntp_bookmarks"]]; |
| 68 NewTabPageBarItem* secondItem = [NewTabPageBarItem |
| 69 newTabPageBarItemWithTitle:@"Second" |
| 70 identifier:2 |
| 71 image:[UIImage imageNamed:@"ntp_bookmarks"]]; |
| 72 |
| 73 NewTabPageBarItem* thirdItem = [NewTabPageBarItem |
| 74 newTabPageBarItemWithTitle:@"Third" |
| 75 identifier:3 |
| 76 image:[UIImage imageNamed:@"ntp_bookmarks"]]; |
| 77 |
| 78 [bar_ setItems:[NSArray |
| 79 arrayWithObjects:firstItem, secondItem, thirdItem, nil]]; |
| 80 |
| 81 UIButton* button = [[bar_ buttons] objectAtIndex:0]; |
| 82 [button sendActionsForControlEvents:UIControlEventTouchDown]; |
| 83 EXPECT_TRUE([[[bar_ buttons] objectAtIndex:0] isSelected]); |
| 84 |
| 85 id secondItemDelegate = |
| 86 [OCMockObject mockForProtocol:@protocol(NewTabPageBarDelegate)]; |
| 87 [[secondItemDelegate expect] newTabBarItemDidChange:secondItem |
| 88 changePanel:YES]; |
| 89 [bar_ setDelegate:secondItemDelegate]; |
| 90 button = [[bar_ buttons] objectAtIndex:1]; |
| 91 [button sendActionsForControlEvents:UIControlEventTouchDown]; |
| 92 EXPECT_TRUE([[[bar_ buttons] objectAtIndex:1] isSelected]); |
| 93 EXPECT_OCMOCK_VERIFY(secondItemDelegate); |
| 94 |
| 95 id thirdItemDelegate = |
| 96 [OCMockObject mockForProtocol:@protocol(NewTabPageBarDelegate)]; |
| 97 [[thirdItemDelegate expect] newTabBarItemDidChange:thirdItem changePanel:YES]; |
| 98 [bar_ setDelegate:thirdItemDelegate]; |
| 99 button = [[bar_ buttons] objectAtIndex:2]; |
| 100 [button sendActionsForControlEvents:UIControlEventTouchDown]; |
| 101 EXPECT_TRUE([[[bar_ buttons] objectAtIndex:2] isSelected]); |
| 102 EXPECT_OCMOCK_VERIFY(thirdItemDelegate); |
| 103 |
| 104 // Reselecting the same item should not cause the method to be called again. |
| 105 id uncalledDelegate = |
| 106 [OCMockObject niceMockForProtocol:@protocol(NewTabPageBarDelegate)]; |
| 107 [[uncalledDelegate reject] newTabBarItemDidChange:OCMOCK_ANY changePanel:YES]; |
| 108 [bar_ setDelegate:uncalledDelegate]; |
| 109 [bar_ setSelectedIndex:2]; |
| 110 EXPECT_TRUE([[[bar_ buttons] objectAtIndex:2] isSelected]); |
| 111 EXPECT_OCMOCK_VERIFY(uncalledDelegate); |
| 112 } |
| 113 |
| 114 } // anonymous namespace |
OLD | NEW |