| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/content_suggestions/content_suggestions_article_i
tem.h" | 5 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i
tem.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #import "third_party/ocmock/OCMock/OCMock.h" |
| 9 #import "third_party/ocmock/gtest_support.h" |
| 8 #include "url/gurl.h" | 10 #include "url/gurl.h" |
| 9 | 11 |
| 10 #if !defined(__has_feature) || !__has_feature(objc_arc) | 12 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 11 #error "This file requires ARC support." | 13 #error "This file requires ARC support." |
| 12 #endif | 14 #endif |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 // Tests that configureCell: set all the fields of the cell. | 18 // Tests that configureCell: set all the fields of the cell except the image and |
| 17 TEST(ContentSuggestionsArticleItemTest, CellIsConfigured) { | 19 // fetches the image through the delegate. |
| 20 TEST(ContentSuggestionsArticleItemTest, CellIsConfiguredWithoutImage) { |
| 21 // Setup. |
| 22 NSString* title = @"testTitle"; |
| 23 NSString* subtitle = @"testSubtitle"; |
| 24 GURL url = GURL("http://chromium.org"); |
| 25 id delegateMock = |
| 26 OCMProtocolMock(@protocol(ContentSuggestionsArticleItemDelegate)); |
| 27 ContentSuggestionsArticleItem* item = |
| 28 [[ContentSuggestionsArticleItem alloc] initWithType:0 |
| 29 title:title |
| 30 subtitle:subtitle |
| 31 delegate:delegateMock |
| 32 url:url]; |
| 33 OCMExpect([delegateMock loadImageForArticleItem:item]); |
| 34 ContentSuggestionsArticleCell* cell = [[[item cellClass] alloc] init]; |
| 35 ASSERT_EQ([ContentSuggestionsArticleCell class], [cell class]); |
| 36 ASSERT_EQ(url, item.articleURL); |
| 37 ASSERT_EQ(nil, item.image); |
| 38 |
| 39 // Action. |
| 40 [item configureCell:cell]; |
| 41 |
| 42 // Tests. |
| 43 EXPECT_EQ(nil, cell.imageView.image); |
| 44 EXPECT_EQ(title, cell.titleLabel.text); |
| 45 EXPECT_EQ(subtitle, cell.subtitleLabel.text); |
| 46 EXPECT_OCMOCK_VERIFY(delegateMock); |
| 47 } |
| 48 |
| 49 // Tests that configureCell: set all the fields of the cell with an image and |
| 50 // does not call the delegate. |
| 51 TEST(ContentSuggestionsArticleItemTest, CellIsConfiguredWithImage) { |
| 52 // Setup. |
| 18 NSString* title = @"testTitle"; | 53 NSString* title = @"testTitle"; |
| 19 NSString* subtitle = @"testSubtitle"; | 54 NSString* subtitle = @"testSubtitle"; |
| 20 UIImage* image = [[UIImage alloc] init]; | 55 UIImage* image = [[UIImage alloc] init]; |
| 21 GURL url = GURL("http://chromium.org"); | 56 GURL url = GURL("http://chromium.org"); |
| 57 id delegateMock = |
| 58 OCMStrictProtocolMock(@protocol(ContentSuggestionsArticleItemDelegate)); |
| 22 ContentSuggestionsArticleItem* item = | 59 ContentSuggestionsArticleItem* item = |
| 23 [[ContentSuggestionsArticleItem alloc] initWithType:0 | 60 [[ContentSuggestionsArticleItem alloc] initWithType:0 |
| 24 title:title | 61 title:title |
| 25 subtitle:subtitle | 62 subtitle:subtitle |
| 26 image:image | 63 delegate:delegateMock |
| 27 url:url]; | 64 url:url]; |
| 65 item.image = image; |
| 28 ContentSuggestionsArticleCell* cell = [[[item cellClass] alloc] init]; | 66 ContentSuggestionsArticleCell* cell = [[[item cellClass] alloc] init]; |
| 29 EXPECT_EQ([ContentSuggestionsArticleCell class], [cell class]); | |
| 30 EXPECT_EQ(url, item.articleURL); | |
| 31 | 67 |
| 68 // Action. |
| 32 [item configureCell:cell]; | 69 [item configureCell:cell]; |
| 70 |
| 71 // Tests. |
| 72 EXPECT_EQ(image, cell.imageView.image); |
| 33 EXPECT_EQ(title, cell.titleLabel.text); | 73 EXPECT_EQ(title, cell.titleLabel.text); |
| 34 EXPECT_EQ(subtitle, cell.subtitleLabel.text); | 74 EXPECT_EQ(subtitle, cell.subtitleLabel.text); |
| 35 EXPECT_EQ(image, cell.imageView.image); | 75 EXPECT_EQ(image, cell.imageView.image); |
| 36 } | 76 } |
| 37 | 77 |
| 78 // Tests that configureCell: does not call the delegate is |imageBeingFetched| |
| 79 // is YES even if there is no image. |
| 80 TEST(ContentSuggestionsArticleItemTest, DontFetchImageIsImageIsBeingFetched) { |
| 81 // Setup. |
| 82 NSString* title = @"testTitle"; |
| 83 NSString* subtitle = @"testSubtitle"; |
| 84 GURL url = GURL("http://chromium.org"); |
| 85 id delegateMock = |
| 86 OCMStrictProtocolMock(@protocol(ContentSuggestionsArticleItemDelegate)); |
| 87 ContentSuggestionsArticleItem* item = |
| 88 [[ContentSuggestionsArticleItem alloc] initWithType:0 |
| 89 title:title |
| 90 subtitle:subtitle |
| 91 delegate:delegateMock |
| 92 url:url]; |
| 93 item.imageBeingFetched = YES; |
| 94 ContentSuggestionsArticleCell* cell = [[[item cellClass] alloc] init]; |
| 95 ASSERT_EQ(nil, item.image); |
| 96 |
| 97 // Action. |
| 98 [item configureCell:cell]; |
| 99 |
| 100 // Tests. |
| 101 EXPECT_EQ(nil, cell.imageView.image); |
| 102 EXPECT_EQ(title, cell.titleLabel.text); |
| 103 EXPECT_EQ(subtitle, cell.subtitleLabel.text); |
| 104 EXPECT_OCMOCK_VERIFY(delegateMock); |
| 105 } |
| 106 |
| 38 } // namespace | 107 } // namespace |
| OLD | NEW |