Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: ios/chrome/browser/ui/reading_list/reading_list_collection_view_controller_unittest.mm

Issue 2914303002: [ObjC ARC] Converts ios/chrome/browser/ui/reading_list:unit_tests to ARC. (Closed)
Patch Set: Use pragma to suppress performSelector ARC warning Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/reading_list/reading_list_collection_view_control ler.h" 5 #import "ios/chrome/browser/ui/reading_list/reading_list_collection_view_control ler.h"
6 6
7 #include <unordered_set> 7 #include <unordered_set>
8 8
9 #import "base/mac/foundation_util.h" 9 #import "base/mac/foundation_util.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
12 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
13 #include "base/strings/sys_string_conversions.h" 12 #include "base/strings/sys_string_conversions.h"
14 #include "base/threading/thread_task_runner_handle.h" 13 #include "base/threading/thread_task_runner_handle.h"
15 #include "base/time/default_clock.h" 14 #include "base/time/default_clock.h"
16 #include "components/favicon/core/large_icon_service.h" 15 #include "components/favicon/core/large_icon_service.h"
17 #include "components/favicon/core/test/mock_favicon_service.h" 16 #include "components/favicon/core/test/mock_favicon_service.h"
18 #include "components/reading_list/core/reading_list_model.h" 17 #include "components/reading_list/core/reading_list_model.h"
19 #include "components/reading_list/core/reading_list_model_impl.h" 18 #include "components/reading_list/core/reading_list_model_impl.h"
20 #include "components/reading_list/core/reading_list_model_storage.h" 19 #include "components/reading_list/core/reading_list_model_storage.h"
21 #include "components/url_formatter/url_formatter.h" 20 #include "components/url_formatter/url_formatter.h"
22 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" 21 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
23 #include "ios/chrome/browser/favicon/ios_chrome_large_icon_service_factory.h" 22 #include "ios/chrome/browser/favicon/ios_chrome_large_icon_service_factory.h"
24 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" 23 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
25 #import "ios/chrome/browser/ui/reading_list/reading_list_collection_view_item.h" 24 #import "ios/chrome/browser/ui/reading_list/reading_list_collection_view_item.h"
26 #import "ios/chrome/browser/ui/reading_list/reading_list_mediator.h" 25 #import "ios/chrome/browser/ui/reading_list/reading_list_mediator.h"
27 #include "ios/web/public/test/test_web_thread_bundle.h" 26 #include "ios/web/public/test/test_web_thread_bundle.h"
28 #include "testing/gmock/include/gmock/gmock.h" 27 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
30 #include "testing/platform_test.h" 29 #include "testing/platform_test.h"
31 #import "third_party/ocmock/OCMock/OCMock.h" 30 #import "third_party/ocmock/OCMock/OCMock.h"
32 #import "third_party/ocmock/gtest_support.h" 31 #import "third_party/ocmock/gtest_support.h"
33 32
33 #if !defined(__has_feature) || !__has_feature(objc_arc)
34 #error "This file requires ARC support."
35 #endif
36
34 using favicon::PostReply; 37 using favicon::PostReply;
35 using testing::_; 38 using testing::_;
36 39
37 #pragma mark - ReadingListCollectionViewControllerTest 40 #pragma mark - ReadingListCollectionViewControllerTest
38 41
39 class ReadingListCollectionViewControllerTest : public testing::Test { 42 class ReadingListCollectionViewControllerTest : public testing::Test {
40 public: 43 public:
41 ReadingListCollectionViewControllerTest() {} 44 ReadingListCollectionViewControllerTest() {}
42 ~ReadingListCollectionViewControllerTest() override {} 45 ~ReadingListCollectionViewControllerTest() override {}
43 46
44 testing::StrictMock<favicon::MockFaviconService> mock_favicon_service_; 47 testing::StrictMock<favicon::MockFaviconService> mock_favicon_service_;
45 std::unique_ptr<ReadingListModelImpl> reading_list_model_; 48 std::unique_ptr<ReadingListModelImpl> reading_list_model_;
46 base::scoped_nsobject<ReadingListMediator> mediator_; 49 ReadingListMediator* mediator_;
47 std::unique_ptr<favicon::LargeIconService> large_icon_service_; 50 std::unique_ptr<favicon::LargeIconService> large_icon_service_;
48 51
49 base::scoped_nsobject<ReadingListCollectionViewController> 52 ReadingListCollectionViewController* reading_list_view_controller_;
50 reading_list_view_controller_;
51 id mock_delegate_; 53 id mock_delegate_;
stkhapugin 2017/06/02 09:52:33 This has gone from bare pointer to strong, is this
pkl (ping after 24h if needed) 2017/06/02 15:32:55 As discussed on IRC, mock_delegate_ should have be
52 54
53 void SetUp() override { 55 void SetUp() override {
54 testing::Test::SetUp(); 56 testing::Test::SetUp();
55 57
56 EXPECT_CALL(mock_favicon_service_, 58 EXPECT_CALL(mock_favicon_service_,
57 GetLargestRawFaviconForPageURL(_, _, _, _, _)) 59 GetLargestRawFaviconForPageURL(_, _, _, _, _))
58 .WillRepeatedly(PostReply<5>(favicon_base::FaviconRawBitmapResult())); 60 .WillRepeatedly(PostReply<5>(favicon_base::FaviconRawBitmapResult()));
59 61
60 reading_list_model_.reset(new ReadingListModelImpl( 62 reading_list_model_.reset(new ReadingListModelImpl(
61 nullptr, nullptr, base::MakeUnique<base::DefaultClock>())); 63 nullptr, nullptr, base::MakeUnique<base::DefaultClock>()));
62 mediator_.reset( 64 mediator_ =
63 [[ReadingListMediator alloc] initWithModel:reading_list_model_.get()]); 65 [[ReadingListMediator alloc] initWithModel:reading_list_model_.get()];
64 large_icon_service_.reset(new favicon::LargeIconService( 66 large_icon_service_.reset(new favicon::LargeIconService(
65 &mock_favicon_service_, base::ThreadTaskRunnerHandle::Get(), 67 &mock_favicon_service_, base::ThreadTaskRunnerHandle::Get(),
66 /*image_fetcher=*/nullptr)); 68 /*image_fetcher=*/nullptr));
67 reading_list_view_controller_.reset( 69 reading_list_view_controller_ = [[ReadingListCollectionViewController alloc]
68 [[ReadingListCollectionViewController alloc] 70 initWithDataSource:mediator_
69 initWithDataSource:mediator_ 71 largeIconService:large_icon_service_.get()
70 largeIconService:large_icon_service_.get() 72 toolbar:nil];
71 toolbar:nil]);
72 73
73 mock_delegate_ = [OCMockObject 74 mock_delegate_ = [OCMockObject
74 niceMockForProtocol:@protocol( 75 niceMockForProtocol:@protocol(
75 ReadingListCollectionViewControllerDelegate)]; 76 ReadingListCollectionViewControllerDelegate)];
76 [reading_list_view_controller_ setDelegate:mock_delegate_]; 77 [reading_list_view_controller_ setDelegate:mock_delegate_];
77 } 78 }
78 79
79 private: 80 private:
80 web::TestWebThreadBundle thread_bundle_; 81 web::TestWebThreadBundle thread_bundle_;
81 DISALLOW_COPY_AND_ASSIGN(ReadingListCollectionViewControllerTest); 82 DISALLOW_COPY_AND_ASSIGN(ReadingListCollectionViewControllerTest);
82 }; 83 };
83 84
84 // Tests that reading list items are displayed. 85 // Tests that reading list items are displayed.
85 TEST_F(ReadingListCollectionViewControllerTest, DisplaysItems) { 86 TEST_F(ReadingListCollectionViewControllerTest, DisplaysItems) {
86 // Prefill some items. 87 // Prefill some items.
87 reading_list_model_->AddEntry(GURL("https://chromium.org"), "news", 88 reading_list_model_->AddEntry(GURL("https://chromium.org"), "news",
88 reading_list::ADDED_VIA_CURRENT_APP); 89 reading_list::ADDED_VIA_CURRENT_APP);
89 reading_list_model_->AddEntry(GURL("https://mail.chromium.org"), "mail", 90 reading_list_model_->AddEntry(GURL("https://mail.chromium.org"), "mail",
90 reading_list::ADDED_VIA_CURRENT_APP); 91 reading_list::ADDED_VIA_CURRENT_APP);
91 reading_list_model_->AddEntry(GURL("https://foo.bar"), "Foo", 92 reading_list_model_->AddEntry(GURL("https://foo.bar"), "Foo",
92 reading_list::ADDED_VIA_CURRENT_APP); 93 reading_list::ADDED_VIA_CURRENT_APP);
93 reading_list_model_->SetReadStatus(GURL("https://foo.bar"), true); 94 reading_list_model_->SetReadStatus(GURL("https://foo.bar"), true);
94 95
95 // Load view. 96 // Load view.
96 [reading_list_view_controller_ view]; 97 [reading_list_view_controller_ view];
97 98
98 // There are two sections: Read and Unread. 99 // There are two sections: Read and Unread.
99 DCHECK( 100 DCHECK([reading_list_view_controller_.collectionView numberOfSections] == 2);
100 [reading_list_view_controller_.get().collectionView numberOfSections] ==
101 2);
102 // There are two unread articles. 101 // There are two unread articles.
103 DCHECK([reading_list_view_controller_.get().collectionView 102 DCHECK([reading_list_view_controller_.collectionView
104 numberOfItemsInSection:0] == 2); 103 numberOfItemsInSection:0] == 2);
105 // There is one read article. 104 // There is one read article.
106 DCHECK([reading_list_view_controller_.get().collectionView 105 DCHECK([reading_list_view_controller_.collectionView
107 numberOfItemsInSection:1] == 1); 106 numberOfItemsInSection:1] == 1);
108 } 107 }
109 108
110 // Tests that the view controller is dismissed when Done button is pressed. 109 // Tests that the view controller is dismissed when Done button is pressed.
111 TEST_F(ReadingListCollectionViewControllerTest, GetsDismissed) { 110 TEST_F(ReadingListCollectionViewControllerTest, GetsDismissed) {
112 // Load view. 111 // Load view.
113 [reading_list_view_controller_ view]; 112 [reading_list_view_controller_ view];
114 113
115 [[mock_delegate_ expect] 114 [[mock_delegate_ expect]
116 dismissReadingListCollectionViewController:reading_list_view_controller_ 115 dismissReadingListCollectionViewController:reading_list_view_controller_];
117 .get()];
118 116
119 // Simulate tap on "Done" button. 117 // Simulate tap on "Done" button.
120 UIBarButtonItem* done = 118 UIBarButtonItem* done =
121 reading_list_view_controller_.get().navigationItem.rightBarButtonItem; 119 reading_list_view_controller_.navigationItem.rightBarButtonItem;
120 #pragma clang diagnostic push
121 #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
122 // Since @selector stored in done.action is a method returning void, there is
123 // no potential for memory leak. It is OK to ignore this warning here.
122 [done.target performSelector:done.action]; 124 [done.target performSelector:done.action];
125 #pragma clang diagnostic pop
123 126
124 EXPECT_OCMOCK_VERIFY(mock_delegate_); 127 EXPECT_OCMOCK_VERIFY(mock_delegate_);
125 } 128 }
126 129
127 // Tests that when an item is selected, the article is opened with UrlLoader and 130 // Tests that when an item is selected, the article is opened with UrlLoader and
128 // the view controller is dismissed. 131 // the view controller is dismissed.
129 TEST_F(ReadingListCollectionViewControllerTest, OpensItems) { 132 TEST_F(ReadingListCollectionViewControllerTest, OpensItems) {
130 NSIndexPath* indexPath = [NSIndexPath indexPathForItem:1 inSection:0]; 133 NSIndexPath* indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
131 134
132 GURL url("https://chromium.org"); 135 GURL url("https://chromium.org");
133 GURL url2("https://chromium.org/2"); 136 GURL url2("https://chromium.org/2");
134 reading_list_model_->AddEntry(url, "chromium", 137 reading_list_model_->AddEntry(url, "chromium",
135 reading_list::ADDED_VIA_CURRENT_APP); 138 reading_list::ADDED_VIA_CURRENT_APP);
136 reading_list_model_->AddEntry(url2, "chromium - 2", 139 reading_list_model_->AddEntry(url2, "chromium - 2",
137 reading_list::ADDED_VIA_CURRENT_APP); 140 reading_list::ADDED_VIA_CURRENT_APP);
138 141
139 ReadingListCollectionViewItem* readingListItem = 142 ReadingListCollectionViewItem* readingListItem =
140 base::mac::ObjCCastStrict<ReadingListCollectionViewItem>( 143 base::mac::ObjCCastStrict<ReadingListCollectionViewItem>(
141 [[reading_list_view_controller_ collectionViewModel] 144 [[reading_list_view_controller_ collectionViewModel]
142 itemAtIndexPath:indexPath]); 145 itemAtIndexPath:indexPath]);
143 146
144 [[mock_delegate_ expect] 147 [[mock_delegate_ expect]
145 readingListCollectionViewController:reading_list_view_controller_.get() 148 readingListCollectionViewController:reading_list_view_controller_
146 openItem:readingListItem]; 149 openItem:readingListItem];
147 150
148 // Simulate touch on second cell. 151 // Simulate touch on second cell.
149 [reading_list_view_controller_ 152 [reading_list_view_controller_
150 collectionView:reading_list_view_controller_.get() 153 collectionView:reading_list_view_controller_.collectionView
151 .collectionView
152 didSelectItemAtIndexPath:indexPath]; 154 didSelectItemAtIndexPath:indexPath];
153 155
154 EXPECT_OCMOCK_VERIFY(mock_delegate_); 156 EXPECT_OCMOCK_VERIFY(mock_delegate_);
155 } 157 }
156 158
157 // Tests that the ReadingListCollectionView is creating 159 // Tests that the ReadingListCollectionView is creating
158 // ReadingListCollectionViewItem with the correct informations. 160 // ReadingListCollectionViewItem with the correct informations.
159 TEST_F(ReadingListCollectionViewControllerTest, 161 TEST_F(ReadingListCollectionViewControllerTest,
160 TestItemInitializationUndistilled) { 162 TestItemInitializationUndistilled) {
161 // Setup. 163 // Setup.
162 GURL url("https://chromium.org"); 164 GURL url("https://chromium.org");
163 std::string title("Chromium"); 165 std::string title("Chromium");
164 reading_list_model_->AddEntry(url, title, 166 reading_list_model_->AddEntry(url, title,
165 reading_list::ADDED_VIA_CURRENT_APP); 167 reading_list::ADDED_VIA_CURRENT_APP);
166 // Load view. 168 // Load view.
167 [reading_list_view_controller_ view]; 169 [reading_list_view_controller_ view];
168 DCHECK([reading_list_view_controller_.get().collectionView 170 DCHECK([reading_list_view_controller_.collectionView
169 numberOfItemsInSection:0] == 1); 171 numberOfItemsInSection:0] == 1);
170 NSIndexPath* indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; 172 NSIndexPath* indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
171 ReadingListCollectionViewItem* readingListItem = 173 ReadingListCollectionViewItem* readingListItem =
172 base::mac::ObjCCastStrict<ReadingListCollectionViewItem>( 174 base::mac::ObjCCastStrict<ReadingListCollectionViewItem>(
173 [[reading_list_view_controller_ collectionViewModel] 175 [[reading_list_view_controller_ collectionViewModel]
174 itemAtIndexPath:indexPath]); 176 itemAtIndexPath:indexPath]);
175 EXPECT_EQ(base::SysNSStringToUTF8([readingListItem title]), title); 177 EXPECT_EQ(base::SysNSStringToUTF8([readingListItem title]), title);
176 EXPECT_EQ([readingListItem url], url); 178 EXPECT_EQ([readingListItem url], url);
177 EXPECT_EQ(base::SysNSStringToUTF16([readingListItem subtitle]), 179 EXPECT_EQ(base::SysNSStringToUTF16([readingListItem subtitle]),
178 url_formatter::FormatUrl(url)); 180 url_formatter::FormatUrl(url));
(...skipping 12 matching lines...) Expand all
191 std::string title("Chromium"); 193 std::string title("Chromium");
192 GURL distilled_url("https://chromium.org/distilled"); 194 GURL distilled_url("https://chromium.org/distilled");
193 base::FilePath distilled_path("/distilled/path"); 195 base::FilePath distilled_path("/distilled/path");
194 reading_list_model_->AddEntry(url, title, 196 reading_list_model_->AddEntry(url, title,
195 reading_list::ADDED_VIA_CURRENT_APP); 197 reading_list::ADDED_VIA_CURRENT_APP);
196 int64_t size = 50; 198 int64_t size = 50;
197 reading_list_model_->SetEntryDistilledInfo(url, distilled_path, distilled_url, 199 reading_list_model_->SetEntryDistilledInfo(url, distilled_path, distilled_url,
198 size, base::Time::FromTimeT(100)); 200 size, base::Time::FromTimeT(100));
199 // Load view. 201 // Load view.
200 [reading_list_view_controller_ view]; 202 [reading_list_view_controller_ view];
201 DCHECK([reading_list_view_controller_.get().collectionView 203 DCHECK([reading_list_view_controller_.collectionView
202 numberOfItemsInSection:0] == 1); 204 numberOfItemsInSection:0] == 1);
203 NSIndexPath* indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; 205 NSIndexPath* indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
204 ReadingListCollectionViewItem* readingListItem = 206 ReadingListCollectionViewItem* readingListItem =
205 base::mac::ObjCCastStrict<ReadingListCollectionViewItem>( 207 base::mac::ObjCCastStrict<ReadingListCollectionViewItem>(
206 [[reading_list_view_controller_ collectionViewModel] 208 [[reading_list_view_controller_ collectionViewModel]
207 itemAtIndexPath:indexPath]); 209 itemAtIndexPath:indexPath]);
208 EXPECT_EQ(base::SysNSStringToUTF8([readingListItem title]), title); 210 EXPECT_EQ(base::SysNSStringToUTF8([readingListItem title]), title);
209 EXPECT_EQ([readingListItem url], url); 211 EXPECT_EQ([readingListItem url], url);
210 EXPECT_EQ(base::SysNSStringToUTF16([readingListItem subtitle]), 212 EXPECT_EQ(base::SysNSStringToUTF16([readingListItem subtitle]),
211 url_formatter::FormatUrl(url)); 213 url_formatter::FormatUrl(url));
212 EXPECT_EQ([readingListItem faviconPageURL], distilled_url); 214 EXPECT_EQ([readingListItem faviconPageURL], distilled_url);
213 EXPECT_EQ([readingListItem distillationState], 215 EXPECT_EQ([readingListItem distillationState],
214 ReadingListUIDistillationStatusSuccess); 216 ReadingListUIDistillationStatusSuccess);
215 } 217 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698