Skip to content

Commit

Permalink
Add EG test for SCContentSuggestions
Browse files Browse the repository at this point in the history
This CL adds Earl Grey tests testing swipe to dismiss, tap and long press on
suggested content.

BUG=704493

Change-Id: Ibd3d9e8030fcaf4069a2cf632791bbb0387a7301
Reviewed-on: https://chromium-review.googlesource.com/529248
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: Louis Romero <lpromero@chromium.org>
Cr-Commit-Position: refs/heads/master@{#478984}
  • Loading branch information
gambard authored and Commit Bot committed Jun 13, 2017
1 parent 23b68f6 commit 2be5085
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ios/showcase/content_suggestions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ source_set("eg_tests") {
"sc_content_suggestions_egtest.mm",
]
deps = [
":content_suggestions",
"//components/strings",
"//ios/chrome/app/strings",
"//ios/chrome/test/earl_grey:test_support",
"//ios/showcase/test",
"//ios/third_party/earl_grey",
"//ui/base",
]
configs += [ "//build/config/compiler:enable_arc" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
@interface SCContentSuggestionsDataSource
: NSObject<ContentSuggestionsDataSource>

// Title of the first suggested article.
+ (NSString*)titleFirstSuggestion;

// Title of the Reading List item.
+ (NSString*)titleReadingListItem;

@end

#endif // IOS_SHOWCASE_CONTENT_SUGGESTIONS_SC_CONTENT_SUGGESTIONS_DATA_SOURCE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ @implementation SCContentSuggestionsDataSource
@synthesize readingListSection = _readingListSection;
@synthesize articleSection = _articleSection;

#pragma mark - Public

+ (NSString*)titleFirstSuggestion {
return @"Title of the first suggestions";
}

+ (NSString*)titleReadingListItem {
return @"Reading List item: No subtitle, no image";
}

#pragma mark - ContentSuggestionsDataSource

- (NSArray<ContentSuggestionsSectionInformation*>*)sectionsInfo {
Expand All @@ -72,7 +82,7 @@ @implementation SCContentSuggestionsDataSource
}
case ContentSuggestionsSectionReadingList: {
SCContentSuggestionsItem* suggestion = [self article];
suggestion.title = @"Reading List item: No subtitle, no image";
suggestion.title = [[self class] titleReadingListItem];
suggestion.subtitle = nil;
suggestion.hasImage = NO;
suggestion.suggestionIdentifier.sectionInfo = self.readingListSection;
Expand All @@ -81,7 +91,7 @@ @implementation SCContentSuggestionsDataSource
case ContentSuggestionsSectionArticles: {
NSMutableArray<CSCollectionViewItem*>* items = [NSMutableArray array];
SCContentSuggestionsItem* suggestion = [self article];
suggestion.title = @"Title of the first suggestions";
suggestion.title = [[self class] titleFirstSuggestion];
suggestion.suggestionIdentifier.IDInSection = std::to_string(1);
[items addObject:suggestion];
for (int i = 2; i < 6; i++) {
Expand Down Expand Up @@ -147,6 +157,8 @@ - (ContentSuggestionsSectionInformation*)readingListSection {
initWithSectionID:ContentSuggestionsSectionReadingList];
_readingListSection.title =
l10n_util::GetNSString(IDS_NTP_READING_LIST_SUGGESTIONS_SECTION_HEADER);
_readingListSection.emptyText =
l10n_util::GetNSString(IDS_NTP_READING_LIST_SUGGESTIONS_SECTION_EMPTY);
_readingListSection.footerTitle =
l10n_util::GetNSString(IDS_IOS_CONTENT_SUGGESTIONS_FOOTER_TITLE);
}
Expand Down
130 changes: 130 additions & 0 deletions ios/showcase/content_suggestions/sc_content_suggestions_egtest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,42 @@
#include "components/strings/grit/components_strings.h"
#include "ios/chrome/grit/ios_strings.h"
#import "ios/chrome/test/earl_grey/chrome_matchers.h"
#import "ios/showcase/content_suggestions/sc_content_suggestions_data_source.h"
#import "ios/showcase/test/showcase_eg_utils.h"
#import "ios/showcase/test/showcase_test_case.h"
#include "ui/base/l10n/l10n_util_mac.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif

namespace {
id<GREYMatcher> TextBeginsWith(NSString* text) {
MatchesBlock matches = ^BOOL(id element) {
return [[element text] hasPrefix:text];
};
DescribeToBlock describe = ^void(id<GREYDescription> description) {
[description
appendText:[NSString stringWithFormat:@"beginsWithText('%@')", text]];
};
id<GREYMatcher> matcher =
[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
descriptionBlock:describe];
return grey_allOf(grey_anyOf(grey_kindOfClass([UILabel class]),
grey_kindOfClass([UITextField class]),
grey_kindOfClass([UITextView class]), nil),
matcher, nil);
}

NSString* ReadingListEmptySection() {
return [NSString
stringWithFormat:@"%@, %@",
l10n_util::GetNSString(IDS_NTP_TITLE_NO_SUGGESTIONS),
l10n_util::GetNSString(
IDS_NTP_READING_LIST_SUGGESTIONS_SECTION_EMPTY)];
}
} // namespace

// Tests for the suggestions view controller.
@interface SCContentSuggestionsTestCase : ShowcaseTestCase
@end
Expand All @@ -34,4 +63,105 @@ - (void)testLaunch {
showcase_utils::Close();
}

// Tests the opening of a suggestion item by tapping on it.
- (void)testOpenItem {
showcase_utils::Open(@"ContentSuggestionsViewController");
[[[EarlGrey selectElementWithMatcher:grey_accessibilityID(
[SCContentSuggestionsDataSource
titleFirstSuggestion])]
usingSearchAction:grey_scrollInDirection(kGREYDirectionUp, 50)
onElementWithMatcher:grey_kindOfClass([UICollectionView class])]
performAction:grey_tap()];

[[EarlGrey
selectElementWithMatcher:
grey_allOf(grey_text([@"openPageForItem:"
stringByAppendingString:[SCContentSuggestionsDataSource
titleFirstSuggestion]]),
grey_sufficientlyVisible(), nil)]
assertWithMatcher:grey_sufficientlyVisible()];
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(
@"protocol_alerter_done")]
performAction:grey_tap()];

showcase_utils::Close();
}

// Tests dismissing an item with swipe-to-dismiss.
- (void)testSwipeToDismiss {
showcase_utils::Open(@"ContentSuggestionsViewController");
[[[EarlGrey selectElementWithMatcher:grey_accessibilityID(
[SCContentSuggestionsDataSource
titleFirstSuggestion])]
usingSearchAction:grey_scrollInDirection(kGREYDirectionUp, 50)
onElementWithMatcher:grey_kindOfClass([UICollectionView class])]
performAction:grey_swipeFastInDirection(kGREYDirectionLeft)];

[[EarlGrey
selectElementWithMatcher:grey_allOf(grey_text(@"dismissContextMenu"),
grey_sufficientlyVisible(), nil)]
assertWithMatcher:grey_sufficientlyVisible()];
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(
@"protocol_alerter_done")]
performAction:grey_tap()];

[[EarlGrey selectElementWithMatcher:grey_accessibilityID(
[SCContentSuggestionsDataSource
titleFirstSuggestion])]
assertWithMatcher:grey_nil()];

showcase_utils::Close();
}

// Tests that long pressing an item starts a context menu.
- (void)testLongPressItem {
showcase_utils::Open(@"ContentSuggestionsViewController");
[[[EarlGrey selectElementWithMatcher:grey_accessibilityID(
[SCContentSuggestionsDataSource
titleFirstSuggestion])]
usingSearchAction:grey_scrollInDirection(kGREYDirectionUp, 50)
onElementWithMatcher:grey_kindOfClass([UICollectionView class])]
performAction:grey_longPress()];

[[EarlGrey
selectElementWithMatcher:grey_allOf(TextBeginsWith(
@"displayContextMenuForArticle:"),
grey_sufficientlyVisible(), nil)]
assertWithMatcher:grey_sufficientlyVisible()];
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(
@"protocol_alerter_done")]
performAction:grey_tap()];

showcase_utils::Close();
}

// Tests that swipe-to-dismiss on empty item does nothing.
- (void)testNoSwipeToDismissEmptyItem {
showcase_utils::Open(@"ContentSuggestionsViewController");
[[[EarlGrey selectElementWithMatcher:grey_accessibilityID(
[SCContentSuggestionsDataSource
titleReadingListItem])]
usingSearchAction:grey_scrollInDirection(kGREYDirectionUp, 50)
onElementWithMatcher:grey_kindOfClass([UICollectionView class])]
performAction:grey_swipeFastInDirection(kGREYDirectionLeft)];

[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(
@"protocol_alerter_done")]
performAction:grey_tap()];

[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(
ReadingListEmptySection())]
performAction:grey_swipeFastInDirection(kGREYDirectionLeft)];

// Check that it is not dismissed.
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(
@"protocol_alerter_done")]
assertWithMatcher:grey_nil()];
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(
ReadingListEmptySection())]
assertWithMatcher:grey_sufficientlyVisible()];

showcase_utils::Close();
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ - (void)configureCell:(ContentSuggestionsCell*)cell {
[cell setAdditionalInformationWithPublisherName:self.publisher
date:self.publicationDate
offlineAvailability:self.availableOffline];
cell.isAccessibilityElement = YES;
cell.accessibilityLabel = self.title;
cell.accessibilityIdentifier = self.title;
}

- (NSString*)description {
return self.title;
}

@end

0 comments on commit 2be5085

Please sign in to comment.