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