Skip to content

Commit

Permalink
[ios] Init MostVisited StackView if tiles are made available later
Browse files Browse the repository at this point in the history
If viewDidLoad is called before the Most Visited tiles are made
available, then the MostVisited StackView needs to be constructed
and added to the Content Suggestions later. This change also separates
the Most Visited tiles and Shortcuts a11y id groupings since they will
be considered separate modules.

(cherry picked from commit abb04fd)

Fixed: 1316991
Change-Id: Id446bd3e6a06e8701ef1541124f53a16f26728c1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3591281
Reviewed-by: Adam Arcaro <adamta@google.com>
Commit-Queue: Chris Lu <thegreenfrog@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#993801}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3594574
Cr-Commit-Position: refs/branch-heads/5005@{#50}
Cr-Branched-From: 5b4d945-refs/heads/main@{#992738}
  • Loading branch information
Chris Lu authored and Chromium LUCI CQ committed Apr 20, 2022
1 parent 4296904 commit a8d4f95
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ - (void)configureCell:(ContentSuggestionsParentCell*)cell {
[whatsNewView.heightAnchor constraintEqualToConstant:size.height]
]];
}
NSUInteger index = 0;
if (self.mostVisitedItems) {
UIStackView* stackView = [[UIStackView alloc] init];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.alignment = UIStackViewAlignmentTop;
stackView.distribution = UIStackViewDistributionFillEqually;
stackView.spacing = horizontalSpacing;
NSUInteger index = 0;
for (ContentSuggestionsMostVisitedItem* item in self.mostVisitedItems) {
ContentSuggestionsMostVisitedTileView* view =
[[ContentSuggestionsMostVisitedTileView alloc]
Expand Down Expand Up @@ -151,15 +151,15 @@ - (void)configureCell:(ContentSuggestionsParentCell*)cell {
stackView.alignment = UIStackViewAlignmentTop;
stackView.distribution = UIStackViewDistributionFillEqually;
stackView.spacing = horizontalSpacing;
NSUInteger index = 0;
for (ContentSuggestionsMostVisitedActionItem* item in self.shortcutsItems) {
ContentSuggestionsShortcutTileView* view =
[[ContentSuggestionsShortcutTileView alloc]
initWithConfiguration:item];
view.accessibilityIdentifier = [NSString
stringWithFormat:
@"%@%li",
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix,
index];
kContentSuggestionsShortcutsAccessibilityIdentifierPrefix, index];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self.tapTarget
action:@selector(contentSuggestionsElementTapped:)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ extern NSString* const kContentSuggestionsCollectionIdentifier;
// Represents the Learn More button in the content suggestions.
extern NSString* const kContentSuggestionsLearnMoreIdentifier;

// Represents the most visited tile of the content suggestions.
// Represents the most visited tiles of the content suggestions.
extern NSString* const
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix;

// Represents the shortcuts of the content suggestions.
extern NSString* const
kContentSuggestionsShortcutsAccessibilityIdentifierPrefix;

// The bottom margin below the Most Visited section.
extern const CGFloat kMostVisitedBottomMargin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
NSString* const kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix =
@"contentSuggestionsMostVisitedAccessibilityIdentifierPrefix";

NSString* const kContentSuggestionsShortcutsAccessibilityIdentifierPrefix =
@"contentSuggestionsShortcutsAccessibilityIdentifierPrefix";

const CGFloat kMostVisitedBottomMargin = 13;
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ - (void)reloadAllData {
[self.consumer
showReturnToRecentTabTileWithConfig:self.returnToRecentTabItem];
}
[self.consumer setMostVisitedTilesWithConfigs:self.mostVisitedItems];
if ([self.mostVisitedItems count]) {
[self.consumer setMostVisitedTilesWithConfigs:self.mostVisitedItems];
}
if (!ShouldHideShortcutsForStartSurface()) {
[self.consumer setShortcutTilesWithConfigs:self.actionButtonItems];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,12 @@ - (void)viewDidLoad {
[self.whatsNewView.heightAnchor constraintEqualToConstant:size.height]
]];
}
NSUInteger index = 0;
if (self.mostVisitedViews) {
self.mostVisitedStackView = [[UIStackView alloc] init];
self.mostVisitedStackView.axis = UILayoutConstraintAxisHorizontal;
self.mostVisitedStackView.alignment = UIStackViewAlignmentTop;
self.mostVisitedStackView.distribution = UIStackViewDistributionFillEqually;
self.mostVisitedStackView.spacing = horizontalSpacing;
for (ContentSuggestionsMostVisitedTileView* view in self.mostVisitedViews) {
view.accessibilityIdentifier = [NSString
stringWithFormat:
@"%@%li",
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix,
index];
view.menuProvider = self.menuProvider;
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(contentSuggestionsElementTapped:)];
[view addGestureRecognizer:tapRecognizer];
tapRecognizer.enabled = YES;
[self.mostVisitedTapRecognizers addObject:tapRecognizer];
[self.mostVisitedStackView addArrangedSubview:view];
index++;
}
[self addUIElement:self.mostVisitedStackView
withCustomBottomSpacing:kMostVisitedBottomMargin];
CGFloat width =
Expand All @@ -162,19 +145,20 @@ - (void)viewDidLoad {
[self.mostVisitedStackView.heightAnchor
constraintEqualToConstant:size.height]
]];
[self populateMostVisitedModule];
}
if (self.shortcutsViews) {
self.shortcutsStackView = [[UIStackView alloc] init];
self.shortcutsStackView.axis = UILayoutConstraintAxisHorizontal;
self.shortcutsStackView.alignment = UIStackViewAlignmentTop;
self.shortcutsStackView.distribution = UIStackViewDistributionFillEqually;
self.shortcutsStackView.spacing = horizontalSpacing;
NSUInteger index = 0;
for (ContentSuggestionsShortcutTileView* view in self.shortcutsViews) {
view.accessibilityIdentifier = [NSString
stringWithFormat:
@"%@%li",
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix,
index];
kContentSuggestionsShortcutsAccessibilityIdentifierPrefix, index];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(contentSuggestionsElementTapped:)];
Expand Down Expand Up @@ -278,41 +262,28 @@ - (void)hideWhatsNewView {

- (void)setMostVisitedTilesWithConfigs:
(NSArray<ContentSuggestionsMostVisitedItem*>*)configs {
if (!self.mostVisitedViews) {
self.mostVisitedViews = [NSMutableArray array];
}
BOOL refreshingTiles = NO;
if ([self.mostVisitedViews count]) {
refreshingTiles = YES;
for (ContentSuggestionsMostVisitedTileView* view in self.mostVisitedViews) {
[view removeFromSuperview];
}
[self.mostVisitedViews removeAllObjects];
[self.mostVisitedTapRecognizers removeAllObjects];
} else {
self.mostVisitedViews = [NSMutableArray array];
}
NSInteger index = 0;
for (ContentSuggestionsMostVisitedItem* item in configs) {
ContentSuggestionsMostVisitedTileView* view =
[[ContentSuggestionsMostVisitedTileView alloc]
initWithConfiguration:item];
view.menuProvider = self.menuProvider;
view.accessibilityIdentifier = [NSString
stringWithFormat:
@"%@%li",
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix, index];
[self.mostVisitedViews addObject:view];
if (refreshingTiles) {
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(contentSuggestionsElementTapped:)];
[view addGestureRecognizer:tapRecognizer];
tapRecognizer.enabled = YES;
[self.mostVisitedTapRecognizers addObject:tapRecognizer];
view.accessibilityIdentifier = [NSString
stringWithFormat:
@"%@%li",
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix,
index];
[self.mostVisitedStackView addArrangedSubview:view];
index++;
}
}
[self populateMostVisitedModule];
}

- (void)setShortcutTilesWithConfigs:
Expand Down Expand Up @@ -447,4 +418,50 @@ - (void)addUIElement:(UIView*)view withCustomBottomSpacing:(CGFloat)spacing {
}
}

// Add the elements in |mostVisitedViews| into |verticalStackView|, constructing
// |verticalStackView| beforehand if it has not been yet.
- (void)populateMostVisitedModule {
// If viewDidLoad has been called before the first valid Most Visited Tiles
// are available, construct |mostVisitedStackView|.
if (self.verticalStackView && !self.mostVisitedStackView) {
self.mostVisitedStackView = [[UIStackView alloc] init];
self.mostVisitedStackView.axis = UILayoutConstraintAxisHorizontal;
self.mostVisitedStackView.alignment = UIStackViewAlignmentTop;
self.mostVisitedStackView.distribution = UIStackViewDistributionFillEqually;
self.mostVisitedStackView.spacing =
ContentSuggestionsTilesHorizontalSpacing(self.traitCollection);
// Find correct insertion position in the stack.
int insertionIndex = 0;
if (self.returnToRecentTabTile) {
insertionIndex++;
}
if (self.whatsNewView) {
insertionIndex++;
}
[self.verticalStackView insertArrangedSubview:self.mostVisitedStackView
atIndex:insertionIndex];
[self.verticalStackView setCustomSpacing:kMostVisitedBottomMargin
afterView:self.mostVisitedStackView];
CGFloat width =
MostVisitedTilesContentHorizontalSpace(self.traitCollection);
CGSize size =
MostVisitedCellSize(self.traitCollection.preferredContentSizeCategory);
[NSLayoutConstraint activateConstraints:@[
[self.mostVisitedStackView.widthAnchor constraintEqualToConstant:width],
[self.mostVisitedStackView.heightAnchor
constraintEqualToConstant:size.height]
]];
}
for (ContentSuggestionsMostVisitedTileView* view in self.mostVisitedViews) {
view.menuProvider = self.menuProvider;
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(contentSuggestionsElementTapped:)];
[view addGestureRecognizer:tapRecognizer];
tapRecognizer.enabled = YES;
[self.mostVisitedTapRecognizers addObject:tapRecognizer];
[self.mostVisitedStackView addArrangedSubview:view];
}
}

@end
33 changes: 30 additions & 3 deletions ios/chrome/browser/ui/content_suggestions/ntp_home_egtest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ - (void)testOpeningNewTab {
}

- (void)testFavicons {
for (NSInteger index = 0; index < 8; index++) {
for (NSInteger index = 0; index < 4; index++) {
[[EarlGrey
selectElementWithMatcher:
grey_accessibilityID([NSString
Expand All @@ -691,6 +691,15 @@ - (void)testFavicons {
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix,
index])] assertWithMatcher:grey_sufficientlyVisible()];
}
for (NSInteger index = 0; index < 4; index++) {
[[EarlGrey
selectElementWithMatcher:
grey_accessibilityID([NSString
stringWithFormat:
@"%@%li",
kContentSuggestionsShortcutsAccessibilityIdentifierPrefix,
index])] assertWithMatcher:grey_sufficientlyVisible()];
}

// Change the Search Engine to Yahoo!.
[ChromeEarlGreyUI openSettingsMenu];
Expand All @@ -705,7 +714,7 @@ - (void)testFavicons {
performAction:grey_tap()];

// Check again the favicons.
for (NSInteger index = 0; index < 8; index++) {
for (NSInteger index = 0; index < 4; index++) {
[[EarlGrey
selectElementWithMatcher:
grey_accessibilityID([NSString
Expand All @@ -714,6 +723,15 @@ - (void)testFavicons {
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix,
index])] assertWithMatcher:grey_sufficientlyVisible()];
}
for (NSInteger index = 0; index < 4; index++) {
[[EarlGrey
selectElementWithMatcher:
grey_accessibilityID([NSString
stringWithFormat:
@"%@%li",
kContentSuggestionsShortcutsAccessibilityIdentifierPrefix,
index])] assertWithMatcher:grey_sufficientlyVisible()];
}

// Change the Search Engine to Google.
[ChromeEarlGreyUI openSettingsMenu];
Expand Down Expand Up @@ -742,7 +760,7 @@ - (void)testMinimumHeight {

// Ensures that tiles are still all visible with feed turned off after
// scrolling.
for (NSInteger index = 0; index < 8; index++) {
for (NSInteger index = 0; index < 4; index++) {
[[EarlGrey
selectElementWithMatcher:
grey_accessibilityID([NSString
Expand All @@ -751,6 +769,15 @@ - (void)testMinimumHeight {
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix,
index])] assertWithMatcher:grey_sufficientlyVisible()];
}
for (NSInteger index = 0; index < 4; index++) {
[[EarlGrey
selectElementWithMatcher:
grey_accessibilityID([NSString
stringWithFormat:
@"%@%li",
kContentSuggestionsShortcutsAccessibilityIdentifierPrefix,
index])] assertWithMatcher:grey_sufficientlyVisible()];
}
// Ensures that fake omnibox visibility is correct.
// On iPads, fake omnibox disappears and becomes real omnibox. On other
// devices, fake omnibox persists and sticks to top.
Expand Down

0 comments on commit a8d4f95

Please sign in to comment.