| 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/cells/content_suggestions_ite
m.h" | 5 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_ite
m.h" |
| 6 | 6 |
| 7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #import "third_party/ocmock/OCMock/OCMock.h" | 9 #import "third_party/ocmock/OCMock/OCMock.h" |
| 10 #import "third_party/ocmock/gtest_support.h" | 10 #import "third_party/ocmock/gtest_support.h" |
| 11 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 12 | 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) | 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." | 14 #error "This file requires ARC support." |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // Tests that configureCell: sets all the fields of the cell except the image | 19 // Tests that configureCell: sets all the fields of the cell except the image |
| 20 // and fetches the image through the delegate. | 20 // and fetches the image through the delegate. |
| 21 TEST(ContentSuggestionsItemTest, CellIsConfiguredWithoutImage) { | 21 TEST(ContentSuggestionsItemTest, CellIsConfiguredWithoutImage) { |
| 22 // Setup. | 22 // Setup. |
| 23 NSString* title = @"testTitle"; | 23 NSString* title = @"testTitle"; |
| 24 NSString* subtitle = @"testSubtitle"; | 24 NSString* subtitle = @"testSubtitle"; |
| 25 GURL url = GURL("http://chromium.org"); | 25 GURL url = GURL("http://chromium.org"); |
| 26 NSString* publisher = @"publisherName"; | 26 NSString* publisher = @"publisherName"; |
| 27 base::Time publishTime = base::Time::Now(); | 27 base::Time publishTime = base::Time::Now(); |
| 28 id delegateMock = OCMProtocolMock(@protocol(ContentSuggestionsItemDelegate)); | 28 id delegateMock = OCMProtocolMock(@protocol(SuggestedContentDelegate)); |
| 29 ContentSuggestionsItem* item = | 29 ContentSuggestionsItem* item = |
| 30 [[ContentSuggestionsItem alloc] initWithType:0 | 30 [[ContentSuggestionsItem alloc] initWithType:0 |
| 31 title:title | 31 title:title |
| 32 subtitle:subtitle | 32 subtitle:subtitle |
| 33 delegate:delegateMock | |
| 34 url:url]; | 33 url:url]; |
| 34 item.delegate = delegateMock; |
| 35 item.hasImage = YES; | 35 item.hasImage = YES; |
| 36 item.publisher = publisher; | 36 item.publisher = publisher; |
| 37 item.publishDate = publishTime; | 37 item.publishDate = publishTime; |
| 38 item.availableOffline = YES; | 38 item.availableOffline = YES; |
| 39 OCMExpect([delegateMock loadImageForSuggestionItem:item]); | 39 OCMExpect([delegateMock loadImageForSuggestedItem:item]); |
| 40 ContentSuggestionsCell* cell = [[[item cellClass] alloc] init]; | 40 ContentSuggestionsCell* cell = [[[item cellClass] alloc] init]; |
| 41 ASSERT_EQ([ContentSuggestionsCell class], [cell class]); | 41 ASSERT_EQ([ContentSuggestionsCell class], [cell class]); |
| 42 ASSERT_EQ(url, item.URL); | 42 ASSERT_EQ(url, item.URL); |
| 43 ASSERT_EQ(nil, item.image); | 43 ASSERT_EQ(nil, item.image); |
| 44 id cellMock = OCMPartialMock(cell); | 44 id cellMock = OCMPartialMock(cell); |
| 45 OCMExpect([cellMock setContentImage:item.image]); | 45 OCMExpect([cellMock setContentImage:item.image]); |
| 46 OCMExpect([cellMock setSubtitleText:subtitle]); | 46 OCMExpect([cellMock setSubtitleText:subtitle]); |
| 47 OCMExpect([cellMock setAdditionalInformationWithPublisherName:publisher | 47 OCMExpect([cellMock setAdditionalInformationWithPublisherName:publisher |
| 48 date:publishTime | 48 date:publishTime |
| 49 offlineAvailability:YES]); | 49 offlineAvailability:YES]); |
| 50 | 50 |
| 51 // Action. | 51 // Action. |
| 52 [item configureCell:cell]; | 52 [item configureCell:cell]; |
| 53 | 53 |
| 54 // Tests. | 54 // Tests. |
| 55 EXPECT_OCMOCK_VERIFY(cellMock); | 55 EXPECT_OCMOCK_VERIFY(cellMock); |
| 56 EXPECT_EQ(title, cell.titleLabel.text); | 56 EXPECT_EQ(title, cell.titleLabel.text); |
| 57 EXPECT_OCMOCK_VERIFY(delegateMock); | 57 EXPECT_OCMOCK_VERIFY(delegateMock); |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Tests that configureCell: does not call the delegate if it fetched the image | 60 // Tests that configureCell: does not call the delegate if it fetched the image |
| 61 // once. | 61 // once. |
| 62 TEST(ContentSuggestionsItemTest, DontFetchImageIsImageIsBeingFetched) { | 62 TEST(ContentSuggestionsItemTest, DontFetchImageIsImageIsBeingFetched) { |
| 63 // Setup. | 63 // Setup. |
| 64 NSString* title = @"testTitle"; | 64 NSString* title = @"testTitle"; |
| 65 NSString* subtitle = @"testSubtitle"; | 65 NSString* subtitle = @"testSubtitle"; |
| 66 GURL url = GURL("http://chromium.org"); | 66 GURL url = GURL("http://chromium.org"); |
| 67 id niceDelegateMock = | 67 id niceDelegateMock = OCMProtocolMock(@protocol(SuggestedContentDelegate)); |
| 68 OCMProtocolMock(@protocol(ContentSuggestionsItemDelegate)); | |
| 69 ContentSuggestionsItem* item = | 68 ContentSuggestionsItem* item = |
| 70 [[ContentSuggestionsItem alloc] initWithType:0 | 69 [[ContentSuggestionsItem alloc] initWithType:0 |
| 71 title:title | 70 title:title |
| 72 subtitle:subtitle | 71 subtitle:subtitle |
| 73 delegate:niceDelegateMock | |
| 74 url:url]; | 72 url:url]; |
| 73 item.delegate = niceDelegateMock; |
| 75 item.hasImage = YES; | 74 item.hasImage = YES; |
| 76 item.image = [[UIImage alloc] init]; | 75 item.image = [[UIImage alloc] init]; |
| 77 | 76 |
| 78 OCMExpect([niceDelegateMock loadImageForSuggestionItem:item]); | 77 OCMExpect([niceDelegateMock loadImageForSuggestedItem:item]); |
| 79 ContentSuggestionsCell* cell = [[[item cellClass] alloc] init]; | 78 ContentSuggestionsCell* cell = [[[item cellClass] alloc] init]; |
| 80 ASSERT_NE(nil, item.image); | 79 ASSERT_NE(nil, item.image); |
| 81 [item configureCell:cell]; | 80 [item configureCell:cell]; |
| 82 ASSERT_OCMOCK_VERIFY(niceDelegateMock); | 81 ASSERT_OCMOCK_VERIFY(niceDelegateMock); |
| 83 | 82 |
| 84 id strictDelegateMock = | 83 id strictDelegateMock = |
| 85 OCMStrictProtocolMock(@protocol(ContentSuggestionsItemDelegate)); | 84 OCMStrictProtocolMock(@protocol(SuggestedContentDelegate)); |
| 86 item.delegate = strictDelegateMock; | 85 item.delegate = strictDelegateMock; |
| 87 id cellMock = OCMPartialMock(cell); | 86 id cellMock = OCMPartialMock(cell); |
| 88 OCMExpect([cellMock setContentImage:item.image]); | 87 OCMExpect([cellMock setContentImage:item.image]); |
| 89 | 88 |
| 90 // Action. | 89 // Action. |
| 91 [item configureCell:cell]; | 90 [item configureCell:cell]; |
| 92 | 91 |
| 93 // Tests. | 92 // Tests. |
| 94 EXPECT_OCMOCK_VERIFY(cellMock); | 93 EXPECT_OCMOCK_VERIFY(cellMock); |
| 95 EXPECT_EQ(title, cell.titleLabel.text); | 94 EXPECT_EQ(title, cell.titleLabel.text); |
| 96 } | 95 } |
| 97 | 96 |
| 98 // Tests that the delegate is not called when |hasImage| is set to NO. If the | 97 // Tests that the delegate is not called when |hasImage| is set to NO. If the |
| 99 // delegate is called an exception is raised. | 98 // delegate is called an exception is raised. |
| 100 TEST(ContentSuggestionsItemTest, NoDelegateCallWhenHasNotImage) { | 99 TEST(ContentSuggestionsItemTest, NoDelegateCallWhenHasNotImage) { |
| 101 // Setup. | 100 // Setup. |
| 102 NSString* title = @"testTitle"; | 101 NSString* title = @"testTitle"; |
| 103 NSString* subtitle = @"testSubtitle"; | 102 NSString* subtitle = @"testSubtitle"; |
| 104 GURL url = GURL("http://chromium.org"); | 103 GURL url = GURL("http://chromium.org"); |
| 105 // Strict mock. Raise exception if the load method is called. | 104 // Strict mock. Raise exception if the load method is called. |
| 106 id delegateMock = | 105 id delegateMock = OCMStrictProtocolMock(@protocol(SuggestedContentDelegate)); |
| 107 OCMStrictProtocolMock(@protocol(ContentSuggestionsItemDelegate)); | |
| 108 ContentSuggestionsItem* item = | 106 ContentSuggestionsItem* item = |
| 109 [[ContentSuggestionsItem alloc] initWithType:0 | 107 [[ContentSuggestionsItem alloc] initWithType:0 |
| 110 title:title | 108 title:title |
| 111 subtitle:subtitle | 109 subtitle:subtitle |
| 112 delegate:delegateMock | |
| 113 url:url]; | 110 url:url]; |
| 111 item.delegate = delegateMock; |
| 114 item.hasImage = NO; | 112 item.hasImage = NO; |
| 115 ContentSuggestionsCell* cell = [[[item cellClass] alloc] init]; | 113 ContentSuggestionsCell* cell = [[[item cellClass] alloc] init]; |
| 116 | 114 |
| 117 // Action. | 115 // Action. |
| 118 [item configureCell:cell]; | 116 [item configureCell:cell]; |
| 119 } | 117 } |
| 120 | 118 |
| 121 } // namespace | 119 } // namespace |
| OLD | NEW |