Commit 01f72a23 authored by Chris Lu's avatar Chris Lu Committed by Commit Bot

Revert "[iOS] Convert NTPHome test to EG2"

This reverts commit 5cd504f9.

Reason for revert: Failed NTPHomeTestCase tests. crbug.com/1019070

Bug: 1019070
Original change's description:
> [iOS] Convert NTPHome test to EG2
>
> OmniboxWidth and OmniboxWidthBetween are moved to the egtest.mm file
> directly to avoid having a more complicated interface.
> Suggestions() uses are replaced by -addNumberOfSuggestions:
> additionalSuggestionsURL:.
>
> Bug: 987646
> Change-Id: I58535b52919fbd1d907c8a72c611e2fbb7f63f67
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1871888
> Commit-Queue: Gauthier Ambard <gambard@chromium.org>
> Reviewed-by: Eugene But <eugenebut@chromium.org>
> Reviewed-by: Stepan Khapugin <stkhapugin@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#709521}

TBR=eugenebut@chromium.org,stkhapugin@chromium.org,gambard@chromium.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: 987646
Change-Id: If3418f1cc097867fa1ef011d5036f171c3cc3fa6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1885971Reviewed-by: default avatarChris Lu <thegreenfrog@chromium.org>
Commit-Queue: Chris Lu <thegreenfrog@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710215}
parent b59c3d73
......@@ -238,7 +238,6 @@ source_set("eg2_tests") {
testonly = true
sources = [
"content_suggestions_egtest.mm",
"ntp_home_egtest.mm",
]
deps = [
":content_suggestions_constant",
......@@ -247,14 +246,10 @@ source_set("eg2_tests") {
"//base/test:test_support",
"//components/strings",
"//ios/chrome/app/strings",
"//ios/chrome/browser/ui/toolbar/public:constants",
"//ios/chrome/test:eg_test_support+eg2",
"//ios/chrome/test/base",
"//ios/chrome/test/earl_grey:eg_test_support+eg2",
"//ios/testing/earl_grey:eg_test_support+eg2",
"//ios/third_party/earl_grey2:test_lib",
"//net:test_support",
"//ui/base",
"//ui/strings",
]
libs = [ "UIKit.framework" ]
......@@ -352,15 +347,10 @@ source_set("eg_app_support+eg2") {
"//components/keyed_service/ios",
"//components/ntp_snippets",
"//components/ntp_snippets:test_support",
"//components/search_engines",
"//ios/chrome/browser",
"//ios/chrome/browser:utils",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/ntp_snippets",
"//ios/chrome/browser/search_engines",
"//ios/chrome/browser/ui/util",
"//ios/chrome/test/app:test_support",
"//ios/chrome/test/base",
"//ios/testing/earl_grey:eg_app_support+eg2",
"//ios/third_party/earl_grey2:app_framework+link",
"//testing/gmock",
......@@ -384,18 +374,13 @@ source_set("test_support") {
"//components/keyed_service/ios",
"//components/ntp_snippets",
"//components/ntp_snippets:test_support",
"//components/search_engines",
"//ios/chrome/browser",
"//ios/chrome/browser:utils",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/ntp_snippets",
"//ios/chrome/browser/search_engines",
"//ios/chrome/browser/ui/content_suggestions",
"//ios/chrome/browser/ui/content_suggestions:content_suggestions_constant",
"//ios/chrome/browser/ui/content_suggestions:content_suggestions_ui",
"//ios/chrome/browser/ui/util",
"//ios/chrome/test/app:test_support",
"//ios/chrome/test/base",
"//ios/testing/earl_grey:earl_grey_support",
"//ios/third_party/earl_grey:earl_grey+link",
"//ios/web",
......
......@@ -5,7 +5,7 @@
#ifndef IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_APP_INTERFACE_H_
#define IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_APP_INTERFACE_H_
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
// App interface for the Content Suggestions.
@interface ContentSuggestionsAppInterface : NSObject
......@@ -32,36 +32,6 @@
// |suggestionNumber|.
+ (void)addSuggestionNumber:(NSInteger)suggestionNumber;
// Returns the short name of the default search engine.
+ (NSString*)defaultSearchEngine;
// Resets the default search engine to |defaultSearchEngine|.
// |defaultSearchEngine| should be its short name.
+ (void)resetSearchEngineTo:(NSString*)defaultSearchEngine;
// Sets the what's new promo to "Move to Dock".
+ (void)setWhatsNewPromoToMoveToDock;
// Resets the what's new promo.
+ (void)resetWhatsNewPromo;
// Returns the width the search field is supposed to have when the collection
// has |collectionWidth|.
+ (CGFloat)searchFieldWidthForCollectionWidth:(CGFloat)collectionWidth;
// Returns the collection view.
+ (UICollectionView*)collectionView;
// Returns the fake omnibox.
+ (UIView*)fakeOmnibox;
// Swizzles the callback for the search button tap action so it is recorded.
+ (void)swizzleSearchButtonLogging;
// Resets the swizzle for the search button tap, and return the whether the
// swizzled method was called.
+ (BOOL)resetSearchButtonLogging;
@end
#endif // IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_APP_INTERFACE_H_
......@@ -11,15 +11,9 @@
#include "components/ntp_snippets/content_suggestion.h"
#include "components/ntp_snippets/content_suggestions_service.h"
#include "components/ntp_snippets/mock_content_suggestions_provider.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/notification_promo.h"
#include "ios/chrome/browser/ntp_snippets/ios_chrome_content_suggestions_service_factory.h"
#include "ios/chrome/browser/ntp_snippets/ios_chrome_content_suggestions_service_factory_util.h"
#include "ios/chrome/browser/search_engines/template_url_service_factory.h"
#include "ios/chrome/browser/system_flags.h"
#include "ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_utils.h"
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_provider_test_singleton.h"
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_test_utils.h"
......@@ -32,7 +26,6 @@
#error "This file requires ARC support."
#endif
using content_suggestions::searchFieldWidth;
using ntp_snippets::AdditionalSuggestionsHelper;
using ntp_snippets::Category;
using ntp_snippets::CategoryStatus;
......@@ -132,74 +125,6 @@ ContentSuggestion CreateSuggestion(Category category,
std::move(suggestions));
}
+ (NSString*)defaultSearchEngine {
// Get the default Search Engine.
ios::ChromeBrowserState* browser_state =
chrome_test_util::GetOriginalBrowserState();
TemplateURLService* service =
ios::TemplateURLServiceFactory::GetForBrowserState(browser_state);
return base::SysUTF16ToNSString(
service->GetDefaultSearchProvider()->short_name());
}
+ (void)resetSearchEngineTo:(NSString*)defaultSearchEngine {
base::string16 defaultSearchEngineString =
base::SysNSStringToUTF16(defaultSearchEngine);
// Set the search engine back to the default in case the test fails before
// cleaning it up.
ios::ChromeBrowserState* browser_state =
chrome_test_util::GetOriginalBrowserState();
TemplateURLService* service =
ios::TemplateURLServiceFactory::GetForBrowserState(browser_state);
std::vector<TemplateURL*> urls = service->GetTemplateURLs();
for (auto iter = urls.begin(); iter != urls.end(); ++iter) {
if (defaultSearchEngineString == (*iter)->short_name()) {
service->SetUserSelectedDefaultSearchProvider(*iter);
}
}
}
+ (void)setWhatsNewPromoToMoveToDock {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:experimental_flags::WHATS_NEW_MOVE_TO_DOCK_TIP
forKey:@"WhatsNewPromoStatus"];
PrefService* local_state = GetApplicationContext()->GetLocalState();
ios::NotificationPromo::MigrateUserPrefs(local_state);
}
+ (void)resetWhatsNewPromo {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:experimental_flags::WHATS_NEW_DEFAULT
forKey:@"WhatsNewPromoStatus"];
PrefService* local_state = GetApplicationContext()->GetLocalState();
ios::NotificationPromo::MigrateUserPrefs(local_state);
}
+ (CGFloat)searchFieldWidthForCollectionWidth:(CGFloat)collectionWidth {
return content_suggestions::searchFieldWidth(collectionWidth);
}
+ (UICollectionView*)collectionView {
return ntp_home::CollectionView();
}
+ (UIView*)fakeOmnibox {
return ntp_home::FakeOmnibox();
}
+ (void)swizzleSearchButtonLogging {
[[ContentSuggestionsTestSingleton sharedInstance]
swizzleLocationBarCoordinatorSearchButton];
}
+ (BOOL)resetSearchButtonLogging {
ContentSuggestionsTestSingleton* singleton =
[ContentSuggestionsTestSingleton sharedInstance];
[singleton resetSwizzle];
return singleton.locationBarCoordinatorSearchButtonMethodCalled;
}
#pragma mark - Helper
+ (MockContentSuggestionsProvider*)provider {
......
......@@ -2,21 +2,47 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <EarlGrey/EarlGrey.h>
#import <XCTest/XCTest.h>
#include <memory>
#include "base/bind.h"
#include "base/ios/ios_util.h"
#include "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/test/scoped_command_line.h"
#include "components/reading_list/core/reading_list_model.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
#include "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_app_interface.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/chrome_switches.h"
#include "ios/chrome/browser/notification_promo.h"
#include "ios/chrome/browser/ntp_snippets/ios_chrome_content_suggestions_service_factory.h"
#include "ios/chrome/browser/ntp_snippets/ios_chrome_content_suggestions_service_factory_util.h"
#include "ios/chrome/browser/reading_list/reading_list_model_factory.h"
#include "ios/chrome/browser/search_engines/template_url_service_factory.h"
#include "ios/chrome/browser/system_flags.h"
#import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_whats_new_item.h"
#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_utils.h"
#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_controller.h"
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_constant.h"
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_provider_test_singleton.h"
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_test_utils.h"
#import "ios/chrome/browser/ui/location_bar/location_bar_coordinator.h"
#import "ios/chrome/browser/ui/settings/settings_table_view_controller.h"
#import "ios/chrome/browser/ui/toolbar/public/toolbar_constants.h"
#include "ios/chrome/browser/ui/util/ui_util.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
#include "ios/chrome/grit/ios_strings.h"
#import "ios/chrome/test/app/chrome_test_util.h"
#include "ios/chrome/test/base/scoped_block_swizzler.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h"
#import "ios/chrome/test/earl_grey/chrome_matchers.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h"
#import "ios/chrome/test/scoped_eg_synchronization_disabler.h"
#import "ios/testing/earl_grey/earl_grey_test.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "ui/base/l10n/l10n_util.h"
......@@ -25,8 +51,19 @@
#error "This file requires ARC support."
#endif
namespace {
using content_suggestions::searchFieldWidth;
using ntp_home::CollectionView;
using ntp_home::FakeOmnibox;
using ntp_home::OmniboxWidth;
using ntp_home::OmniboxWidthBetween;
using ntp_home::Suggestions;
using ntp_snippets::CategoryStatus;
using ntp_snippets::ContentSuggestionsService;
using ntp_snippets::CreateChromeContentSuggestionsService;
using ntp_snippets::KnownCategories;
using ntp_snippets::MockContentSuggestionsProvider;
namespace {
const char kPageLoadedString[] = "Page loaded!";
const char kPageURL[] = "/test-page.html";
const char kPageTitle[] = "Page title!";
......@@ -45,66 +82,27 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
std::string(kPageLoadedString) + "</body></html>");
return std::move(http_response);
}
// Returns a matcher, which is true if the view has its width equals to |width|.
id<GREYMatcher> OmniboxWidth(CGFloat width) {
GREYMatchesBlock matches = ^BOOL(UIView* view) {
return fabs(view.bounds.size.width - width) < 0.001;
};
GREYDescribeToBlock describe = ^void(id<GREYDescription> description) {
[description
appendText:[NSString stringWithFormat:@"Omnibox has correct width: %g",
width]];
};
return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
descriptionBlock:describe];
}
// Returns a matcher, which is true if the view has its width equals to |width|
// plus or minus |margin|.
id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
GREYMatchesBlock matches = ^BOOL(UIView* view) {
return view.bounds.size.width >= width - margin &&
view.bounds.size.width <= width + margin;
};
GREYDescribeToBlock describe = ^void(id<GREYDescription> description) {
[description
appendText:[NSString
stringWithFormat:
@"Omnibox has correct width: %g with margin: %g",
width, margin]];
};
return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
descriptionBlock:describe];
}
}
// Test case for the NTP home UI. More precisely, this tests the positions of
// the elements after interacting with the device.
@interface NTPHomeTestCase : ChromeTestCase
@property(nonatomic, strong) NSString* defaultSearchEngine;
// Current non-incognito browser state.
@property(nonatomic, assign, readonly) ios::ChromeBrowserState* browserState;
// Mock provider from the singleton.
@property(nonatomic, assign, readonly) MockContentSuggestionsProvider* provider;
// Article category, used by the singleton.
@property(nonatomic, assign, readonly) ntp_snippets::Category category;
@property(nonatomic, assign) base::string16 defaultSearchEngine;
@end
@implementation NTPHomeTestCase
#if defined(CHROME_EARL_GREY_1)
+ (void)setUp {
[super setUp];
[NTPHomeTestCase setUpHelper];
}
#elif defined(CHROME_EARL_GREY_2)
+ (void)setUpForTestCase {
[super setUpForTestCase];
[NTPHomeTestCase setUpHelper];
}
#endif // CHROME_EARL_GREY_2
+ (void)setUpHelper {
[super setUp];
// Clear the pasteboard in case there is a URL copied, triggering an omnibox
// suggestion.
......@@ -112,38 +110,91 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
[pasteboard setValue:@"" forPasteboardType:UIPasteboardNameGeneral];
[self closeAllTabs];
[ContentSuggestionsAppInterface setUpService];
ios::ChromeBrowserState* browserState =
chrome_test_util::GetOriginalBrowserState();
// Sets the ContentSuggestionsService associated with this browserState to a
// service with no provider registered, allowing to register fake providers
// which do not require internet connection. The previous service is deleted.
IOSChromeContentSuggestionsServiceFactory::GetInstance()->SetTestingFactory(
browserState,
base::BindRepeating(&CreateChromeContentSuggestionsService));
ContentSuggestionsService* service =
IOSChromeContentSuggestionsServiceFactory::GetForBrowserState(
browserState);
[[ContentSuggestionsTestSingleton sharedInstance]
registerArticleProvider:service];
}
+ (void)tearDown {
[self closeAllTabs];
[ContentSuggestionsAppInterface resetService];
ios::ChromeBrowserState* browserState =
chrome_test_util::GetOriginalBrowserState();
ReadingListModelFactory::GetForBrowserState(browserState)->DeleteAllEntries();
// Resets the Service associated with this browserState to a new service with
// no providers. The previous service is deleted.
IOSChromeContentSuggestionsServiceFactory::GetInstance()->SetTestingFactory(
browserState,
base::BindRepeating(&CreateChromeContentSuggestionsService));
[super tearDown];
}
- (void)setUp {
self.provider->FireCategoryStatusChanged(self.category,
CategoryStatus::AVAILABLE);
ReadingListModel* readingListModel =
ReadingListModelFactory::GetForBrowserState(self.browserState);
readingListModel->DeleteAllEntries();
[super setUp];
[ContentSuggestionsAppInterface makeSuggestionsAvailable];
self.defaultSearchEngine =
[ContentSuggestionsAppInterface defaultSearchEngine];
// Get the default Search Engine.
ios::ChromeBrowserState* browser_state =
chrome_test_util::GetOriginalBrowserState();
TemplateURLService* service =
ios::TemplateURLServiceFactory::GetForBrowserState(browser_state);
self.defaultSearchEngine = service->GetDefaultSearchProvider()->short_name();
}
- (void)tearDown {
[ContentSuggestionsAppInterface disableSuggestions];
self.provider->FireCategoryStatusChanged(
self.category, CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED);
[EarlGrey rotateDeviceToOrientation:UIDeviceOrientationPortrait
#if defined(CHROME_EARL_GREY_1)
errorOrNil:nil];
#elif defined(CHROME_EARL_GREY_2)
error:nil];
#endif
[ContentSuggestionsAppInterface resetSearchEngineTo:self.defaultSearchEngine];
// Set the search engine back to the default in case the test fails before
// cleaning it up.
ios::ChromeBrowserState* browser_state =
chrome_test_util::GetOriginalBrowserState();
TemplateURLService* service =
ios::TemplateURLServiceFactory::GetForBrowserState(browser_state);
std::vector<TemplateURL*> urls = service->GetTemplateURLs();
for (auto iter = urls.begin(); iter != urls.end(); ++iter) {
if (self.defaultSearchEngine == (*iter)->short_name()) {
service->SetUserSelectedDefaultSearchProvider(*iter);
}
}
[super tearDown];
}
#pragma mark - Properties
- (ios::ChromeBrowserState*)browserState {
return chrome_test_util::GetOriginalBrowserState();
}
- (MockContentSuggestionsProvider*)provider {
return [[ContentSuggestionsTestSingleton sharedInstance] provider];
}
- (ntp_snippets::Category)category {
return ntp_snippets::Category::FromKnownCategory(KnownCategories::ARTICLES);
}
#pragma mark - Tests
// Tests that all items are accessible on the home page.
......@@ -214,33 +265,25 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
EARL_GREY_TEST_DISABLED(@"Disabled for iPad due to device rotation bug.");
}
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
UIEdgeInsets safeArea =
[ContentSuggestionsAppInterface collectionView].safeAreaInsets;
CGFloat collectionWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(
[ContentSuggestionsAppInterface collectionView].bounds, safeArea));
UIEdgeInsets safeArea = CollectionView().safeAreaInsets;
CGFloat collectionWidth =
CGRectGetWidth(UIEdgeInsetsInsetRect(CollectionView().bounds, safeArea));
GREYAssertTrue(collectionWidth > 0, @"The collection width is nil.");
CGFloat fakeOmniboxWidth = [ContentSuggestionsAppInterface
searchFieldWidthForCollectionWidth:collectionWidth];
CGFloat fakeOmniboxWidth = searchFieldWidth(collectionWidth);
[[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()]
assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)];
[EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft
#if defined(CHROME_EARL_GREY_1)
errorOrNil:nil];
#elif defined(CHROME_EARL_GREY_2)
error:nil];
#endif
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
safeArea = [ContentSuggestionsAppInterface collectionView].safeAreaInsets;
CGFloat collectionWidthAfterRotation = CGRectGetWidth(UIEdgeInsetsInsetRect(
[ContentSuggestionsAppInterface collectionView].bounds, safeArea));
safeArea = CollectionView().safeAreaInsets;
CGFloat collectionWidthAfterRotation =
CGRectGetWidth(UIEdgeInsetsInsetRect(CollectionView().bounds, safeArea));
GREYAssertNotEqual(collectionWidth, collectionWidthAfterRotation,
@"The collection width has not changed.");
fakeOmniboxWidth = [ContentSuggestionsAppInterface
searchFieldWidthForCollectionWidth:collectionWidthAfterRotation];
fakeOmniboxWidth = searchFieldWidth(collectionWidthAfterRotation);
[[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()]
assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)];
......@@ -251,17 +294,15 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
- (void)testOmniboxWidthRotationBehindSettings {
// TODO(crbug.com/652465): Enable the test for iPad when rotation bug is
// fixed.
if ([ChromeEarlGrey isRegularXRegularSizeClass]) {
if (IsRegularXRegularSizeClass()) {
EARL_GREY_TEST_DISABLED(@"Disabled for iPad due to device rotation bug.");
}
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
UIEdgeInsets safeArea =
[ContentSuggestionsAppInterface collectionView].safeAreaInsets;
CGFloat collectionWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(
[ContentSuggestionsAppInterface collectionView].bounds, safeArea));
UIEdgeInsets safeArea = CollectionView().safeAreaInsets;
CGFloat collectionWidth =
CGRectGetWidth(UIEdgeInsetsInsetRect(CollectionView().bounds, safeArea));
GREYAssertTrue(collectionWidth > 0, @"The collection width is nil.");
CGFloat fakeOmniboxWidth = [ContentSuggestionsAppInterface
searchFieldWidthForCollectionWidth:collectionWidth];
CGFloat fakeOmniboxWidth = searchFieldWidth(collectionWidth);
[[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()]
assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)];
......@@ -269,24 +310,18 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
[ChromeEarlGreyUI openSettingsMenu];
[EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft
#if defined(CHROME_EARL_GREY_1)
errorOrNil:nil];
#elif defined(CHROME_EARL_GREY_2)
error:nil];
#endif
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
[[EarlGrey selectElementWithMatcher:chrome_test_util::SettingsDoneButton()]
performAction:grey_tap()];
safeArea = [ContentSuggestionsAppInterface collectionView].safeAreaInsets;
CGFloat collectionWidthAfterRotation = CGRectGetWidth(UIEdgeInsetsInsetRect(
[ContentSuggestionsAppInterface collectionView].bounds, safeArea));
safeArea = CollectionView().safeAreaInsets;
CGFloat collectionWidthAfterRotation =
CGRectGetWidth(UIEdgeInsetsInsetRect(CollectionView().bounds, safeArea));
GREYAssertNotEqual(collectionWidth, collectionWidthAfterRotation,
@"The collection width has not changed.");
fakeOmniboxWidth = [ContentSuggestionsAppInterface
searchFieldWidthForCollectionWidth:collectionWidthAfterRotation];
fakeOmniboxWidth = searchFieldWidth(collectionWidthAfterRotation);
[[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()]
assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)];
......@@ -297,7 +332,7 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
- (void)testOmniboxPinnedWidthRotation {
// TODO(crbug.com/652465): Enable the test for iPad when rotation bug is
// fixed.
if ([ChromeEarlGrey isRegularXRegularSizeClass]) {
if (IsRegularXRegularSizeClass()) {
EARL_GREY_TEST_DISABLED(@"Disabled for iPad due to device rotation bug.");
}
......@@ -306,8 +341,7 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
performAction:grey_swipeFastInDirection(kGREYDirectionUp)];
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
CGFloat collectionWidth =
[ContentSuggestionsAppInterface collectionView].bounds.size.width;
CGFloat collectionWidth = CollectionView().bounds.size.width;
GREYAssertTrue(collectionWidth > 0, @"The collection width is nil.");
// The fake omnibox might be slightly bigger than the screen in order to cover
......@@ -316,15 +350,10 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
assertWithMatcher:OmniboxWidthBetween(collectionWidth + 1, 2)];
[EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft
#if defined(CHROME_EARL_GREY_1)
errorOrNil:nil];
#elif defined(CHROME_EARL_GREY_2)
error:nil];
#endif
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
CGFloat collectionWidthAfterRotation =
[ContentSuggestionsAppInterface collectionView].bounds.size.width;
CGFloat collectionWidthAfterRotation = CollectionView().bounds.size.width;
GREYAssertNotEqual(collectionWidth, collectionWidthAfterRotation,
@"The collection width has not changed.");
......@@ -352,24 +381,32 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
// Tests that the promo is correctly displayed and removed once tapped.
- (void)testPromoTap {
[ContentSuggestionsAppInterface setWhatsNewPromoToMoveToDock];
// Setup the promo.
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:experimental_flags::WHATS_NEW_MOVE_TO_DOCK_TIP
forKey:@"WhatsNewPromoStatus"];
PrefService* local_state = GetApplicationContext()->GetLocalState();
ios::NotificationPromo::MigrateUserPrefs(local_state);
// Open a new tab to have the promo.
[ChromeEarlGreyUI openNewTab];
// Tap the promo.
[[EarlGrey
selectElementWithMatcher:grey_accessibilityID(
@"ContentSuggestionsWhatsNewIdentifier")]
[[EarlGrey selectElementWithMatcher:grey_accessibilityID(
[ContentSuggestionsWhatsNewItem
accessibilityIdentifier])]
performAction:grey_tap()];
// Promo dismissed.
[[EarlGrey
selectElementWithMatcher:grey_accessibilityID(
@"ContentSuggestionsWhatsNewIdentifier")]
[[EarlGrey selectElementWithMatcher:grey_accessibilityID(
[ContentSuggestionsWhatsNewItem
accessibilityIdentifier])]
assertWithMatcher:grey_not(grey_sufficientlyVisible())];
[ContentSuggestionsAppInterface resetWhatsNewPromo];
// Reset the promo.
[defaults setInteger:experimental_flags::WHATS_NEW_DEFAULT
forKey:@"WhatsNewPromoStatus"];
ios::NotificationPromo::MigrateUserPrefs(local_state);
}
// Tests that the position of the collection view is restored when navigating
......@@ -378,8 +415,10 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
[self addMostVisitedTile];
// Add suggestions to be able to scroll on iPad.
[ContentSuggestionsAppInterface addNumberOfSuggestions:15
additionalSuggestionsURL:nil];
ReadingListModelFactory::GetForBrowserState(self.browserState)
->AddEntry(GURL("http://chromium.org/"), "title",
reading_list::ADDED_VIA_CURRENT_APP);
self.provider->FireSuggestionsChanged(self.category, Suggestions());
// Scroll to have a position to restored.
[[EarlGrey selectElementWithMatcher:chrome_test_util::
......@@ -387,7 +426,7 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
performAction:grey_scrollInDirection(kGREYDirectionDown, 150)];
// Save the position before navigating.
UIView* omnibox = [ContentSuggestionsAppInterface fakeOmnibox];
UIView* omnibox = FakeOmnibox();
CGPoint previousPosition = omnibox.bounds.origin;
// Navigate and come back.
......@@ -399,7 +438,7 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
[ChromeEarlGrey goBack];
// Check that the new position is the same.
omnibox = [ContentSuggestionsAppInterface fakeOmnibox];
omnibox = FakeOmnibox();
GREYAssertEqual(previousPosition.y, omnibox.bounds.origin.y,
@"Omnibox not at the same position");
}
......@@ -411,8 +450,10 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
[self addMostVisitedTile];
// Add suggestions to be able to scroll on iPad.
[ContentSuggestionsAppInterface addNumberOfSuggestions:15
additionalSuggestionsURL:nil];
ReadingListModelFactory::GetForBrowserState(self.browserState)
->AddEntry(GURL("http://chromium.org/"), "title",
reading_list::ADDED_VIA_CURRENT_APP);
self.provider->FireSuggestionsChanged(self.category, Suggestions());
// Scroll to have a position to restored.
[[EarlGrey selectElementWithMatcher:chrome_test_util::
......@@ -420,7 +461,7 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
performAction:grey_scrollInDirection(kGREYDirectionDown, 150)];
// Save the position before navigating.
UIView* omnibox = [ContentSuggestionsAppInterface fakeOmnibox];
UIView* omnibox = FakeOmnibox();
CGPoint previousPosition = omnibox.bounds.origin;
// Tap the omnibox to focus it.
......@@ -435,7 +476,7 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
[ChromeEarlGrey goBack];
// Check that the new position is the same.
omnibox = [ContentSuggestionsAppInterface fakeOmnibox];
omnibox = FakeOmnibox();
GREYAssertEqual(previousPosition.y, omnibox.bounds.origin.y,
@"Omnibox not at the same position");
}
......@@ -444,7 +485,7 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
- (void)testTapFakeOmnibox {
// TODO(crbug.com/753098): Re-enable this test on iPad once grey_typeText
// works.
if ([ChromeEarlGrey isRegularXRegularSizeClass]) {
if (IsRegularXRegularSizeClass()) {
EARL_GREY_TEST_DISABLED(@"Test disabled on iPad.");
}
// Setup the server.
......@@ -468,20 +509,24 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
// It is important for ranking algorithm of omnibox that requests from the
// search button and real omnibox are marked appropriately.
- (void)testTapOmniboxSearchButtonLogsCorrectly {
if ([ChromeEarlGrey isRegularXRegularSizeClass]) {
if (IsRegularXRegularSizeClass()) {
// This logging only happens on iPhone, since on iPad there's no secondary
// toolbar.
return;
}
[ContentSuggestionsAppInterface swizzleSearchButtonLogging];
// Swizzle the method that needs to be called for correct logging.
__block BOOL tapped = NO;
ScopedBlockSwizzler swizzler([LocationBarCoordinator class],
@selector(focusOmniboxFromSearchButton), ^{
tapped = YES;
});
// Tap the search button.
[[EarlGrey selectElementWithMatcher:grey_accessibilityID(
kToolbarSearchButtonIdentifier)]
performAction:grey_tap()];
BOOL tapped = [ContentSuggestionsAppInterface resetSearchButtonLogging];
// Check that the page is loaded.
GREYAssertTrue(tapped,
@"The tap on the search button was not correctly logged.");
......@@ -490,9 +535,10 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
// Tests that tapping the fake omnibox moves the collection.
- (void)testTapFakeOmniboxScroll {
// Get the collection and its layout.
UICollectionView* collectionView =
[ContentSuggestionsAppInterface collectionView];
UIView* collection = CollectionView();
GREYAssertTrue([collection isKindOfClass:[UICollectionView class]],
@"The collection has not been correctly selected.");
UICollectionView* collectionView = (UICollectionView*)collection;
GREYAssertTrue(
[collectionView.delegate
conformsToProtocol:@protocol(UICollectionViewDelegateFlowLayout)],
......@@ -518,8 +564,7 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
[[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()]
assertWithMatcher:grey_not(grey_sufficientlyVisible())];
CGFloat top =
[ContentSuggestionsAppInterface collectionView].safeAreaInsets.top;
CGFloat top = CollectionView().safeAreaInsets.top;
GREYAssertTrue(offsetAfterTap.y >= origin.y + headerHeight - (60 + top),
@"The collection has not moved.");
......@@ -541,8 +586,10 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
// back to where it was.
- (void)testTapFakeOmniboxScrollScrolled {
// Get the collection and its layout.
UICollectionView* collectionView =
[ContentSuggestionsAppInterface collectionView];
UIView* collection = CollectionView();
GREYAssertTrue([collection isKindOfClass:[UICollectionView class]],
@"The collection has not been correctly selected.");
UICollectionView* collectionView = (UICollectionView*)collection;
// Scroll to have a position different from the default.
[[EarlGrey selectElementWithMatcher:chrome_test_util::
......@@ -574,7 +621,7 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
// Tests tapping the search button when the fake omnibox is scrolled.
- (void)testTapSearchButtonFakeOmniboxScrolled {
if ([ChromeEarlGrey isRegularXRegularSizeClass]) {
if (IsRegularXRegularSizeClass()) {
// This only happens on iPhone, since on iPad there's no secondary toolbar.
return;
}
......@@ -615,7 +662,7 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
{
ScopedSynchronizationDisabler disabler;
[[EarlGrey selectElementWithMatcher:tabGridMatcher]
performAction:grey_longPressWithDuration(0.05)];
performAction:[GREYActions actionForLongPressWithDuration:0.05]];
}
[[EarlGrey selectElementWithMatcher:chrome_test_util::TabGridNewTabButton()]
performAction:grey_tap()];
......@@ -628,19 +675,19 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
- (void)testFavicons {
for (NSInteger index = 0; index < 8; index++) {
[[EarlGrey selectElementWithMatcher:
grey_accessibilityID([NSString
stringWithFormat:@"%@%li",
@"contentSuggestionsMostVisitedAccessib"
@"ilityIdentifierPrefix",
index])]
assertWithMatcher:grey_sufficientlyVisible()];
[[EarlGrey
selectElementWithMatcher:
grey_accessibilityID([NSString
stringWithFormat:
@"%@%li",
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix,
index])] assertWithMatcher:grey_sufficientlyVisible()];
}
// Change the Search Engine to Yahoo!.
[ChromeEarlGreyUI openSettingsMenu];
[ChromeEarlGreyUI
tapSettingsMenuButton:grey_accessibilityID(@"Search Engine")];
tapSettingsMenuButton:grey_accessibilityID(kSettingsSearchEngineCellId)];
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Yahoo!")]
performAction:grey_tap()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::SettingsDoneButton()]
......@@ -648,19 +695,19 @@ id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
// Check again the favicons.
for (NSInteger index = 0; index < 8; index++) {
[[EarlGrey selectElementWithMatcher:
grey_accessibilityID([NSString
stringWithFormat:@"%@%li",
@"contentSuggestionsMostVisitedAccessib"
@"ilityIdentifierPrefix",
index])]
assertWithMatcher:grey_sufficientlyVisible()];
[[EarlGrey
selectElementWithMatcher:
grey_accessibilityID([NSString
stringWithFormat:
@"%@%li",
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix,
index])] assertWithMatcher:grey_sufficientlyVisible()];
}
// Change the Search Engine to Google.
[ChromeEarlGreyUI openSettingsMenu];
[ChromeEarlGreyUI
tapSettingsMenuButton:grey_accessibilityID(@"Search Engine")];
tapSettingsMenuButton:grey_accessibilityID(kSettingsSearchEngineCellId)];
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Google")]
performAction:grey_tap()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::SettingsDoneButton()]
......
......@@ -21,11 +21,6 @@ class AdditionalSuggestionsHelper;
// Shared instance of this singleton.
+ (instancetype)sharedInstance;
// Whether the @"focusOmniboxFromSearchButton" selector has been called on the
// location bar coordinator. This is the method swizzled by the methods below.
@property(nonatomic, assign, readonly)
BOOL locationBarCoordinatorSearchButtonMethodCalled;
// Resets the stored additionalSuggestions helper with |URL|.
- (void)resetAdditionalSuggestionsHelperWithURL:(const GURL&)URL;
// Returns the stored additionalSuggestionsHelper.
......@@ -37,12 +32,6 @@ class AdditionalSuggestionsHelper;
- (void)registerArticleProvider:
(ntp_snippets::ContentSuggestionsService*)service;
// Enables the swizzling of the @"focusOmniboxFromSearchButton" selector on the
// location bar coordinator.
- (void)swizzleLocationBarCoordinatorSearchButton;
// Resets the swizzling of the location bar coordinator.
- (void)resetSwizzle;
@end
#endif // IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_NTP_HOME_PROVIDER_TEST_SINGLETON_H_
......@@ -8,7 +8,6 @@
#include "components/ntp_snippets/content_suggestion.h"
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_test_utils.h"
#include "ios/chrome/test/base/scoped_block_swizzler.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -19,8 +18,6 @@
ntp_snippets::MockContentSuggestionsProvider* _provider;
std::unique_ptr<ntp_snippets::AdditionalSuggestionsHelper>
_additionalSuggestionsHelper;
std::unique_ptr<ScopedBlockSwizzler> _swizzler;
__block BOOL _tapped;
}
+ (instancetype)sharedInstance {
......@@ -56,20 +53,4 @@
service->RegisterProvider(std::move(provider));
}
- (void)swizzleLocationBarCoordinatorSearchButton {
_tapped = NO;
_swizzler = std::make_unique<ScopedBlockSwizzler>(
NSClassFromString(@"LocationBarCoordinator"),
NSSelectorFromString(@"focusOmniboxFromSearchButton"), ^{
_tapped = YES;
});
}
- (void)resetSwizzle {
_swizzler.reset();
}
- (BOOL)locationBarCoordinatorSearchButtonMethodCalled {
return _tapped;
}
@end
......@@ -20,6 +20,16 @@ UICollectionView* CollectionView();
// Returns the view corresponding to the fake omnibox. Returns nil if it is not
// in the view hierarchy.
UIView* FakeOmnibox();
// Returns a matcher, which is true if the view has its width equals to |width|.
id<GREYMatcher> OmniboxWidth(CGFloat width);
// Returns a matcher, which is true if the view has its width equals to |width|
// plus or minus |margin|.
id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin);
// Returns a vector of 10 suggestions.
std::vector<ntp_snippets::ContentSuggestion> Suggestions();
} // namespace ntp_home
namespace ntp_snippets {
......
......@@ -20,7 +20,6 @@
#endif
namespace {
// Helper method to get the Article category.
ntp_snippets::Category Category() {
return ntp_snippets::Category::FromKnownCategory(
......@@ -56,6 +55,36 @@ UIView* SubviewWithAccessibilityIdentifier(NSString* accessibilityID,
} // namespace
namespace ntp_home {
id<GREYMatcher> OmniboxWidth(CGFloat width) {
GREYMatchesBlock matches = ^BOOL(UIView* view) {
return fabs(view.bounds.size.width - width) < 0.001;
};
GREYDescribeToBlock describe = ^void(id<GREYDescription> description) {
[description
appendText:[NSString stringWithFormat:@"Omnibox has correct width: %g",
width]];
};
return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
descriptionBlock:describe];
}
id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
GREYMatchesBlock matches = ^BOOL(UIView* view) {
return view.bounds.size.width >= width - margin &&
view.bounds.size.width <= width + margin;
};
GREYDescribeToBlock describe = ^void(id<GREYDescription> description) {
[description
appendText:[NSString
stringWithFormat:
@"Omnibox has correct width: %g with margin: %g",
width, margin]];
};
return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
descriptionBlock:describe];
}
UICollectionView* CollectionView() {
return base::mac::ObjCCast<UICollectionView>(
......@@ -70,6 +99,31 @@ UIView* FakeOmnibox() {
[[UIApplication sharedApplication] keyWindow]);
}
std::vector<ntp_snippets::ContentSuggestion> Suggestions() {
std::vector<ntp_snippets::ContentSuggestion> suggestions;
suggestions.emplace_back(
Suggestion("chromium1", GURL("http://chromium.org/1")));
suggestions.emplace_back(
Suggestion("chromium2", GURL("http://chromium.org/2")));
suggestions.emplace_back(
Suggestion("chromium3", GURL("http://chromium.org/3")));
suggestions.emplace_back(
Suggestion("chromium4", GURL("http://chromium.org/4")));
suggestions.emplace_back(
Suggestion("chromium5", GURL("http://chromium.org/5")));
suggestions.emplace_back(
Suggestion("chromium6", GURL("http://chromium.org/6")));
suggestions.emplace_back(
Suggestion("chromium7", GURL("http://chromium.org/7")));
suggestions.emplace_back(
Suggestion("chromium8", GURL("http://chromium.org/8")));
suggestions.emplace_back(
Suggestion("chromium9", GURL("http://chromium.org/9")));
suggestions.emplace_back(
Suggestion("chromium10", GURL("http://chromium.org/10")));
return suggestions;
}
} // namespace ntp_home
namespace ntp_snippets {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment