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 "ios/chrome/browser/ui/history/tab_history_popup_controller.h" | 5 #import "ios/chrome/browser/ui/history/tab_history_popup_controller.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
11 #include "components/sessions/core/session_types.h" | 11 #include "components/sessions/core/session_types.h" |
12 #import "ios/chrome/browser/ui/history/tab_history_view_controller.h" | 12 #import "ios/chrome/browser/ui/history/tab_history_view_controller.h" |
13 #include "ios/chrome/browser/ui/ui_util.h" | 13 #include "ios/chrome/browser/ui/ui_util.h" |
| 14 #import "ios/web/navigation/crw_session_entry.h" |
14 #include "ios/web/public/navigation_item.h" | 15 #include "ios/web/public/navigation_item.h" |
15 #include "ios/web/public/referrer.h" | 16 #include "ios/web/public/referrer.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "testing/platform_test.h" | 18 #include "testing/platform_test.h" |
18 #import "third_party/ocmock/OCMock/OCMock.h" | 19 #import "third_party/ocmock/OCMock/OCMock.h" |
19 #include "ui/gfx/ios/uikit_util.h" | 20 #include "ui/gfx/ios/uikit_util.h" |
20 | 21 |
21 @interface TabHistoryPopupController (Testing) | 22 @interface TabHistoryPopupController (Testing) |
22 + (CGFloat)popupWidthForItems:(const web::NavigationItemList)items; | 23 - (CGFloat)calculatePopupWidth:(NSArray*)entries; |
23 @property(nonatomic, retain) | 24 @property(nonatomic, retain) |
24 TabHistoryViewController* tabHistoryTableViewController; | 25 TabHistoryViewController* tabHistoryTableViewController; |
25 @end | 26 @end |
26 | 27 |
27 namespace { | 28 namespace { |
28 static const CGFloat kTabHistoryMinWidth = 250.0; | 29 static const CGFloat kTabHistoryMinWidth = 250.0; |
29 static const CGFloat kTabHistoryMaxWidthLandscapePhone = 350.0; | 30 static const CGFloat kTabHistoryMaxWidthLandscapePhone = 350.0; |
30 | 31 |
31 class TabHistoryPopupControllerTest : public PlatformTest { | 32 class TabHistoryPopupControllerTest : public PlatformTest { |
32 protected: | 33 protected: |
33 void SetUp() override { | 34 void SetUp() override { |
34 web::Referrer referrer(GURL("http://www.example.com"), | |
35 web::ReferrerPolicyDefault); | |
36 items_.push_back(web::NavigationItem::Create()); | |
37 items_.back()->SetURL(GURL("http://www.example.com/0")); | |
38 items_.back()->SetReferrer(referrer); | |
39 items_.push_back(web::NavigationItem::Create()); | |
40 items_.back()->SetURL(GURL("http://www.example.com/1")); | |
41 items_.back()->SetReferrer(referrer); | |
42 items_.push_back(web::NavigationItem::Create()); | |
43 items_.back()->SetURL(GURL("http://www.example.com/0")); | |
44 items_.back()->SetReferrer(referrer); | |
45 | |
46 parent_.reset([[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]); | 35 parent_.reset([[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]); |
47 popup_.reset([[TabHistoryPopupController alloc] | 36 popup_.reset([[TabHistoryPopupController alloc] |
48 initWithOrigin:CGPointZero | 37 initWithOrigin:CGPointZero |
49 parentView:parent_ | 38 parentView:parent_ |
50 items:web::CreateRawNavigationItemList(items_)]); | 39 entries:testEntriesArray()]); |
51 } | 40 } |
52 | 41 void TearDown() override { |
53 web::ScopedNavigationItemList items_; | 42 parent_.reset(); |
| 43 popup_.reset(); |
| 44 } |
| 45 NSArray* testEntriesArray() { |
| 46 web::Referrer referrer(GURL("http://www.example.com"), |
| 47 web::ReferrerPolicyDefault); |
| 48 std::unique_ptr<web::NavigationItem> item0 = web::NavigationItem::Create(); |
| 49 item0->SetURL(GURL("http://www.example.com/0")); |
| 50 item0->SetReferrer(referrer); |
| 51 CRWSessionEntry* entry0 = |
| 52 [[CRWSessionEntry alloc] initWithNavigationItem:std::move(item0)]; |
| 53 std::unique_ptr<web::NavigationItem> item1 = web::NavigationItem::Create(); |
| 54 item1->SetURL(GURL("http://www.example.com/1")); |
| 55 item1->SetReferrer(referrer); |
| 56 CRWSessionEntry* entry1 = |
| 57 [[CRWSessionEntry alloc] initWithNavigationItem:std::move(item1)]; |
| 58 std::unique_ptr<web::NavigationItem> item2 = web::NavigationItem::Create(); |
| 59 item2->SetURL(GURL("http://www.example.com/2")); |
| 60 item2->SetReferrer(referrer); |
| 61 CRWSessionEntry* entry2 = |
| 62 [[CRWSessionEntry alloc] initWithNavigationItem:std::move(item2)]; |
| 63 return [NSArray arrayWithObjects:entry0, entry1, entry2, nil]; |
| 64 } |
54 base::scoped_nsobject<UIView> parent_; | 65 base::scoped_nsobject<UIView> parent_; |
55 base::scoped_nsobject<TabHistoryPopupController> popup_; | 66 base::scoped_nsobject<TabHistoryPopupController> popup_; |
56 }; | 67 }; |
57 | 68 |
58 TEST_F(TabHistoryPopupControllerTest, TestTableSize) { | 69 TEST_F(TabHistoryPopupControllerTest, TestTableSize) { |
59 NSInteger number_of_rows = 0; | 70 NSInteger number_of_rows = 0; |
60 | 71 |
61 UICollectionView* collectionView = | 72 UICollectionView* collectionView = |
62 [[popup_ tabHistoryTableViewController] collectionView]; | 73 [[popup_ tabHistoryTableViewController] collectionView]; |
63 | 74 |
64 NSInteger number_of_sections = [collectionView numberOfSections]; | 75 NSInteger number_of_sections = [collectionView numberOfSections]; |
65 for (NSInteger section = 0; section < number_of_sections; ++section) { | 76 for (NSInteger section = 0; section < number_of_sections; ++section) { |
66 number_of_rows += [collectionView numberOfItemsInSection:section]; | 77 number_of_rows += [collectionView numberOfItemsInSection:section]; |
67 } | 78 } |
68 | 79 |
69 EXPECT_EQ(3, number_of_rows); | 80 EXPECT_EQ(3, number_of_rows); |
70 } | 81 } |
71 | 82 |
72 TEST_F(TabHistoryPopupControllerTest, TestCalculatePopupWidth) { | 83 TEST_F(TabHistoryPopupControllerTest, TestCalculatePopupWidth) { |
| 84 web::Referrer referrer(GURL("http://www.example.com"), |
| 85 web::ReferrerPolicyDefault); |
| 86 std::unique_ptr<web::NavigationItem> itemShort = |
| 87 web::NavigationItem::Create(); |
| 88 itemShort->SetURL(GURL("http://foo.com/")); |
| 89 itemShort->SetReferrer(referrer); |
| 90 CRWSessionEntry* entryShort = |
| 91 [[CRWSessionEntry alloc] initWithNavigationItem:std::move(itemShort)]; |
| 92 std::unique_ptr<web::NavigationItem> itemMedium = |
| 93 web::NavigationItem::Create(); |
| 94 itemMedium->SetURL(GURL("http://www.example.com/mediumurl")); |
| 95 itemMedium->SetReferrer(referrer); |
| 96 CRWSessionEntry* entryMedium = |
| 97 [[CRWSessionEntry alloc] initWithNavigationItem:std::move(itemMedium)]; |
| 98 std::string longURL = |
| 99 "http://www.example.com/this/is/areally/long/url/that/" |
| 100 "is/larger/than/the/maximum/table/width/so/its/text/will/get/cut/off/and/" |
| 101 "the/max/width/is/used/"; |
| 102 std::unique_ptr<web::NavigationItem> itemLong = web::NavigationItem::Create(); |
| 103 itemLong->SetURL(GURL(longURL)); |
| 104 itemLong->SetReferrer(referrer); |
| 105 CRWSessionEntry* entryLong = |
| 106 [[CRWSessionEntry alloc] initWithNavigationItem:std::move(itemLong)]; |
| 107 |
73 CGFloat minWidth = kTabHistoryMinWidth; | 108 CGFloat minWidth = kTabHistoryMinWidth; |
74 CGFloat maxWidth = kTabHistoryMinWidth; | 109 CGFloat maxWidth = kTabHistoryMinWidth; |
75 if (!IsIPadIdiom()) { | 110 if (!IsIPadIdiom()) { |
76 UIInterfaceOrientation orientation = | 111 UIInterfaceOrientation orientation = |
77 [[UIApplication sharedApplication] statusBarOrientation]; | 112 [[UIApplication sharedApplication] statusBarOrientation]; |
78 if (!UIInterfaceOrientationIsPortrait(orientation)) | 113 if (!UIInterfaceOrientationIsPortrait(orientation)) |
79 maxWidth = kTabHistoryMaxWidthLandscapePhone; | 114 maxWidth = kTabHistoryMaxWidthLandscapePhone; |
80 } else { | 115 } else { |
81 maxWidth = ui::AlignValueToUpperPixel( | 116 maxWidth = ui::AlignValueToUpperPixel( |
82 [UIApplication sharedApplication].keyWindow.frame.size.width * .85); | 117 [UIApplication sharedApplication].keyWindow.frame.size.width * .85); |
83 } | 118 } |
84 | 119 |
85 // Add an item with a short URL and verify that the minimum width is returned. | 120 CGFloat width = |
86 web::ScopedNavigationItemList items; | 121 [popup_ calculatePopupWidth:[NSArray arrayWithObjects:entryShort, nil]]; |
87 web::Referrer referrer(GURL("http://www.example.com"), | |
88 web::ReferrerPolicyDefault); | |
89 items.push_back(web::NavigationItem::Create()); | |
90 items.back()->SetURL(GURL("http://foo.com/")); | |
91 items.back()->SetReferrer(referrer); | |
92 web::NavigationItemList raw_items = web::CreateRawNavigationItemList(items); | |
93 CGFloat width = [TabHistoryPopupController popupWidthForItems:raw_items]; | |
94 EXPECT_EQ(minWidth, width); | 122 EXPECT_EQ(minWidth, width); |
95 | 123 |
96 // Add an item with a medium URL and verify that the returned width is between | 124 width = |
97 // the minimum and maximum. | 125 [popup_ calculatePopupWidth:[NSArray arrayWithObjects:entryShort, |
98 items.push_back(web::NavigationItem::Create()); | 126 entryMedium, nil]]; |
99 items.back()->SetURL(GURL("http://www.example.com/mediumurl")); | |
100 items.back()->SetReferrer(referrer); | |
101 raw_items.push_back(items.back().get()); | |
102 width = [TabHistoryPopupController popupWidthForItems:raw_items]; | |
103 EXPECT_GE(width, minWidth); | 127 EXPECT_GE(width, minWidth); |
104 EXPECT_LE(width, maxWidth); | 128 EXPECT_LE(width, maxWidth); |
105 | 129 |
106 // Add an item with a long URL and verify that the maximum width is returned. | 130 width = [popup_ |
107 std::string long_url = | 131 calculatePopupWidth:[NSArray arrayWithObjects:entryShort, entryLong, |
108 "http://www.example.com/this/is/areally/long/url/that/" | 132 entryMedium, nil]]; |
109 "is/larger/than/the/maximum/table/width/so/its/text/will/get/cut/off/and/" | |
110 "the/max/width/is/used/"; | |
111 items.push_back(web::NavigationItem::Create()); | |
112 items.back()->SetURL(GURL(long_url)); | |
113 items.back()->SetReferrer(referrer); | |
114 raw_items.push_back(items.back().get()); | |
115 width = [TabHistoryPopupController popupWidthForItems:raw_items]; | |
116 EXPECT_EQ(maxWidth, width); | 133 EXPECT_EQ(maxWidth, width); |
117 } | 134 } |
118 | 135 |
119 } // namespace | 136 } // namespace |
OLD | NEW |