Commit 5cd504f9 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[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: default avatarEugene But <eugenebut@chromium.org>
Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709521}
parent 44af6716
...@@ -238,6 +238,7 @@ source_set("eg2_tests") { ...@@ -238,6 +238,7 @@ source_set("eg2_tests") {
testonly = true testonly = true
sources = [ sources = [
"content_suggestions_egtest.mm", "content_suggestions_egtest.mm",
"ntp_home_egtest.mm",
] ]
deps = [ deps = [
":content_suggestions_constant", ":content_suggestions_constant",
...@@ -246,10 +247,14 @@ source_set("eg2_tests") { ...@@ -246,10 +247,14 @@ source_set("eg2_tests") {
"//base/test:test_support", "//base/test:test_support",
"//components/strings", "//components/strings",
"//ios/chrome/app/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/chrome/test/earl_grey:eg_test_support+eg2",
"//ios/testing/earl_grey:eg_test_support+eg2", "//ios/testing/earl_grey:eg_test_support+eg2",
"//ios/third_party/earl_grey2:test_lib", "//ios/third_party/earl_grey2:test_lib",
"//net:test_support", "//net:test_support",
"//ui/base",
"//ui/strings", "//ui/strings",
] ]
libs = [ "UIKit.framework" ] libs = [ "UIKit.framework" ]
...@@ -347,10 +352,15 @@ source_set("eg_app_support+eg2") { ...@@ -347,10 +352,15 @@ source_set("eg_app_support+eg2") {
"//components/keyed_service/ios", "//components/keyed_service/ios",
"//components/ntp_snippets", "//components/ntp_snippets",
"//components/ntp_snippets:test_support", "//components/ntp_snippets:test_support",
"//components/search_engines",
"//ios/chrome/browser",
"//ios/chrome/browser:utils",
"//ios/chrome/browser/browser_state", "//ios/chrome/browser/browser_state",
"//ios/chrome/browser/ntp_snippets", "//ios/chrome/browser/ntp_snippets",
"//ios/chrome/browser/search_engines",
"//ios/chrome/browser/ui/util", "//ios/chrome/browser/ui/util",
"//ios/chrome/test/app:test_support", "//ios/chrome/test/app:test_support",
"//ios/chrome/test/base",
"//ios/testing/earl_grey:eg_app_support+eg2", "//ios/testing/earl_grey:eg_app_support+eg2",
"//ios/third_party/earl_grey2:app_framework+link", "//ios/third_party/earl_grey2:app_framework+link",
"//testing/gmock", "//testing/gmock",
...@@ -374,13 +384,18 @@ source_set("test_support") { ...@@ -374,13 +384,18 @@ source_set("test_support") {
"//components/keyed_service/ios", "//components/keyed_service/ios",
"//components/ntp_snippets", "//components/ntp_snippets",
"//components/ntp_snippets:test_support", "//components/ntp_snippets:test_support",
"//components/search_engines",
"//ios/chrome/browser",
"//ios/chrome/browser:utils",
"//ios/chrome/browser/browser_state", "//ios/chrome/browser/browser_state",
"//ios/chrome/browser/ntp_snippets", "//ios/chrome/browser/ntp_snippets",
"//ios/chrome/browser/search_engines",
"//ios/chrome/browser/ui/content_suggestions", "//ios/chrome/browser/ui/content_suggestions",
"//ios/chrome/browser/ui/content_suggestions:content_suggestions_constant", "//ios/chrome/browser/ui/content_suggestions:content_suggestions_constant",
"//ios/chrome/browser/ui/content_suggestions:content_suggestions_ui", "//ios/chrome/browser/ui/content_suggestions:content_suggestions_ui",
"//ios/chrome/browser/ui/util", "//ios/chrome/browser/ui/util",
"//ios/chrome/test/app:test_support", "//ios/chrome/test/app:test_support",
"//ios/chrome/test/base",
"//ios/testing/earl_grey:earl_grey_support", "//ios/testing/earl_grey:earl_grey_support",
"//ios/third_party/earl_grey:earl_grey+link", "//ios/third_party/earl_grey:earl_grey+link",
"//ios/web", "//ios/web",
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#ifndef IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_APP_INTERFACE_H_ #ifndef IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_APP_INTERFACE_H_
#define IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_APP_INTERFACE_H_ #define IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_APP_INTERFACE_H_
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h>
// App interface for the Content Suggestions. // App interface for the Content Suggestions.
@interface ContentSuggestionsAppInterface : NSObject @interface ContentSuggestionsAppInterface : NSObject
...@@ -32,6 +32,36 @@ ...@@ -32,6 +32,36 @@
// |suggestionNumber|. // |suggestionNumber|.
+ (void)addSuggestionNumber:(NSInteger)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 @end
#endif // IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_APP_INTERFACE_H_ #endif // IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_APP_INTERFACE_H_
...@@ -11,9 +11,15 @@ ...@@ -11,9 +11,15 @@
#include "components/ntp_snippets/content_suggestion.h" #include "components/ntp_snippets/content_suggestion.h"
#include "components/ntp_snippets/content_suggestions_service.h" #include "components/ntp_snippets/content_suggestions_service.h"
#include "components/ntp_snippets/mock_content_suggestions_provider.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/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.h"
#include "ios/chrome/browser/ntp_snippets/ios_chrome_content_suggestions_service_factory_util.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" #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_provider_test_singleton.h"
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_test_utils.h" #import "ios/chrome/browser/ui/content_suggestions/ntp_home_test_utils.h"
...@@ -26,6 +32,7 @@ ...@@ -26,6 +32,7 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
using content_suggestions::searchFieldWidth;
using ntp_snippets::AdditionalSuggestionsHelper; using ntp_snippets::AdditionalSuggestionsHelper;
using ntp_snippets::Category; using ntp_snippets::Category;
using ntp_snippets::CategoryStatus; using ntp_snippets::CategoryStatus;
...@@ -125,6 +132,74 @@ ContentSuggestion CreateSuggestion(Category category, ...@@ -125,6 +132,74 @@ ContentSuggestion CreateSuggestion(Category category,
std::move(suggestions)); 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 #pragma mark - Helper
+ (MockContentSuggestionsProvider*)provider { + (MockContentSuggestionsProvider*)provider {
......
...@@ -2,47 +2,21 @@ ...@@ -2,47 +2,21 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#import <EarlGrey/EarlGrey.h>
#import <XCTest/XCTest.h>
#include <memory>
#include "base/bind.h" #include "base/bind.h"
#include "base/ios/ios_util.h" #include "base/ios/ios_util.h"
#include "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.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" #include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/application_context.h" #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_app_interface.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_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" #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" #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.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.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_matchers.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h" #import "ios/chrome/test/earl_grey/chrome_test_case.h"
#import "ios/chrome/test/scoped_eg_synchronization_disabler.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_request.h"
#include "net/test/embedded_test_server/http_response.h" #include "net/test/embedded_test_server/http_response.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
...@@ -51,19 +25,8 @@ ...@@ -51,19 +25,8 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
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 { namespace {
const char kPageLoadedString[] = "Page loaded!"; const char kPageLoadedString[] = "Page loaded!";
const char kPageURL[] = "/test-page.html"; const char kPageURL[] = "/test-page.html";
const char kPageTitle[] = "Page title!"; const char kPageTitle[] = "Page title!";
...@@ -82,27 +45,66 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -82,27 +45,66 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
std::string(kPageLoadedString) + "</body></html>"); std::string(kPageLoadedString) + "</body></html>");
return std::move(http_response); 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 // Test case for the NTP home UI. More precisely, this tests the positions of
// the elements after interacting with the device. // the elements after interacting with the device.
@interface NTPHomeTestCase : ChromeTestCase @interface NTPHomeTestCase : ChromeTestCase
// Current non-incognito browser state. @property(nonatomic, strong) NSString* defaultSearchEngine;
@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 @end
@implementation NTPHomeTestCase @implementation NTPHomeTestCase
#if defined(CHROME_EARL_GREY_1)
+ (void)setUp { + (void)setUp {
[super 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 // Clear the pasteboard in case there is a URL copied, triggering an omnibox
// suggestion. // suggestion.
...@@ -110,91 +112,38 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -110,91 +112,38 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
[pasteboard setValue:@"" forPasteboardType:UIPasteboardNameGeneral]; [pasteboard setValue:@"" forPasteboardType:UIPasteboardNameGeneral];
[self closeAllTabs]; [self closeAllTabs];
ios::ChromeBrowserState* browserState = [ContentSuggestionsAppInterface setUpService];
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 { + (void)tearDown {
[self closeAllTabs]; [self closeAllTabs];
ios::ChromeBrowserState* browserState = [ContentSuggestionsAppInterface resetService];
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]; [super tearDown];
} }
- (void)setUp { - (void)setUp {
self.provider->FireCategoryStatusChanged(self.category,
CategoryStatus::AVAILABLE);
ReadingListModel* readingListModel =
ReadingListModelFactory::GetForBrowserState(self.browserState);
readingListModel->DeleteAllEntries();
[super setUp]; [super setUp];
[ContentSuggestionsAppInterface makeSuggestionsAvailable];
// Get the default Search Engine. self.defaultSearchEngine =
ios::ChromeBrowserState* browser_state = [ContentSuggestionsAppInterface defaultSearchEngine];
chrome_test_util::GetOriginalBrowserState();
TemplateURLService* service =
ios::TemplateURLServiceFactory::GetForBrowserState(browser_state);
self.defaultSearchEngine = service->GetDefaultSearchProvider()->short_name();
} }
- (void)tearDown { - (void)tearDown {
self.provider->FireCategoryStatusChanged( [ContentSuggestionsAppInterface disableSuggestions];
self.category, CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED);
[EarlGrey rotateDeviceToOrientation:UIDeviceOrientationPortrait [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationPortrait
#if defined(CHROME_EARL_GREY_1)
errorOrNil:nil]; errorOrNil:nil];
#elif defined(CHROME_EARL_GREY_2)
error:nil];
#endif
// Set the search engine back to the default in case the test fails before [ContentSuggestionsAppInterface resetSearchEngineTo:self.defaultSearchEngine];
// 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]; [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 #pragma mark - Tests
// Tests that all items are accessible on the home page. // Tests that all items are accessible on the home page.
...@@ -265,25 +214,33 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -265,25 +214,33 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
EARL_GREY_TEST_DISABLED(@"Disabled for iPad due to device rotation bug."); EARL_GREY_TEST_DISABLED(@"Disabled for iPad due to device rotation bug.");
} }
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; [[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
UIEdgeInsets safeArea = CollectionView().safeAreaInsets; UIEdgeInsets safeArea =
CGFloat collectionWidth = [ContentSuggestionsAppInterface collectionView].safeAreaInsets;
CGRectGetWidth(UIEdgeInsetsInsetRect(CollectionView().bounds, safeArea)); CGFloat collectionWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(
[ContentSuggestionsAppInterface collectionView].bounds, safeArea));
GREYAssertTrue(collectionWidth > 0, @"The collection width is nil."); GREYAssertTrue(collectionWidth > 0, @"The collection width is nil.");
CGFloat fakeOmniboxWidth = searchFieldWidth(collectionWidth); CGFloat fakeOmniboxWidth = [ContentSuggestionsAppInterface
searchFieldWidthForCollectionWidth:collectionWidth];
[[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()] [[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()]
assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)]; assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)];
[EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft
#if defined(CHROME_EARL_GREY_1)
errorOrNil:nil]; errorOrNil:nil];
#elif defined(CHROME_EARL_GREY_2)
error:nil];
#endif
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; [[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
safeArea = CollectionView().safeAreaInsets; safeArea = [ContentSuggestionsAppInterface collectionView].safeAreaInsets;
CGFloat collectionWidthAfterRotation = CGFloat collectionWidthAfterRotation = CGRectGetWidth(UIEdgeInsetsInsetRect(
CGRectGetWidth(UIEdgeInsetsInsetRect(CollectionView().bounds, safeArea)); [ContentSuggestionsAppInterface collectionView].bounds, safeArea));
GREYAssertNotEqual(collectionWidth, collectionWidthAfterRotation, GREYAssertNotEqual(collectionWidth, collectionWidthAfterRotation,
@"The collection width has not changed."); @"The collection width has not changed.");
fakeOmniboxWidth = searchFieldWidth(collectionWidthAfterRotation); fakeOmniboxWidth = [ContentSuggestionsAppInterface
searchFieldWidthForCollectionWidth:collectionWidthAfterRotation];
[[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()] [[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()]
assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)]; assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)];
...@@ -294,15 +251,17 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -294,15 +251,17 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
- (void)testOmniboxWidthRotationBehindSettings { - (void)testOmniboxWidthRotationBehindSettings {
// TODO(crbug.com/652465): Enable the test for iPad when rotation bug is // TODO(crbug.com/652465): Enable the test for iPad when rotation bug is
// fixed. // fixed.
if (IsRegularXRegularSizeClass()) { if ([ChromeEarlGrey isRegularXRegularSizeClass]) {
EARL_GREY_TEST_DISABLED(@"Disabled for iPad due to device rotation bug."); EARL_GREY_TEST_DISABLED(@"Disabled for iPad due to device rotation bug.");
} }
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; [[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
UIEdgeInsets safeArea = CollectionView().safeAreaInsets; UIEdgeInsets safeArea =
CGFloat collectionWidth = [ContentSuggestionsAppInterface collectionView].safeAreaInsets;
CGRectGetWidth(UIEdgeInsetsInsetRect(CollectionView().bounds, safeArea)); CGFloat collectionWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(
[ContentSuggestionsAppInterface collectionView].bounds, safeArea));
GREYAssertTrue(collectionWidth > 0, @"The collection width is nil."); GREYAssertTrue(collectionWidth > 0, @"The collection width is nil.");
CGFloat fakeOmniboxWidth = searchFieldWidth(collectionWidth); CGFloat fakeOmniboxWidth = [ContentSuggestionsAppInterface
searchFieldWidthForCollectionWidth:collectionWidth];
[[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()] [[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()]
assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)]; assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)];
...@@ -310,18 +269,24 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -310,18 +269,24 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
[ChromeEarlGreyUI openSettingsMenu]; [ChromeEarlGreyUI openSettingsMenu];
[EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft
#if defined(CHROME_EARL_GREY_1)
errorOrNil:nil]; errorOrNil:nil];
#elif defined(CHROME_EARL_GREY_2)
error:nil];
#endif
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; [[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
[[EarlGrey selectElementWithMatcher:chrome_test_util::SettingsDoneButton()] [[EarlGrey selectElementWithMatcher:chrome_test_util::SettingsDoneButton()]
performAction:grey_tap()]; performAction:grey_tap()];
safeArea = CollectionView().safeAreaInsets; safeArea = [ContentSuggestionsAppInterface collectionView].safeAreaInsets;
CGFloat collectionWidthAfterRotation = CGFloat collectionWidthAfterRotation = CGRectGetWidth(UIEdgeInsetsInsetRect(
CGRectGetWidth(UIEdgeInsetsInsetRect(CollectionView().bounds, safeArea)); [ContentSuggestionsAppInterface collectionView].bounds, safeArea));
GREYAssertNotEqual(collectionWidth, collectionWidthAfterRotation, GREYAssertNotEqual(collectionWidth, collectionWidthAfterRotation,
@"The collection width has not changed."); @"The collection width has not changed.");
fakeOmniboxWidth = searchFieldWidth(collectionWidthAfterRotation); fakeOmniboxWidth = [ContentSuggestionsAppInterface
searchFieldWidthForCollectionWidth:collectionWidthAfterRotation];
[[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()] [[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()]
assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)]; assertWithMatcher:OmniboxWidth(fakeOmniboxWidth)];
...@@ -332,7 +297,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -332,7 +297,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
- (void)testOmniboxPinnedWidthRotation { - (void)testOmniboxPinnedWidthRotation {
// TODO(crbug.com/652465): Enable the test for iPad when rotation bug is // TODO(crbug.com/652465): Enable the test for iPad when rotation bug is
// fixed. // fixed.
if (IsRegularXRegularSizeClass()) { if ([ChromeEarlGrey isRegularXRegularSizeClass]) {
EARL_GREY_TEST_DISABLED(@"Disabled for iPad due to device rotation bug."); EARL_GREY_TEST_DISABLED(@"Disabled for iPad due to device rotation bug.");
} }
...@@ -341,7 +306,8 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -341,7 +306,8 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
performAction:grey_swipeFastInDirection(kGREYDirectionUp)]; performAction:grey_swipeFastInDirection(kGREYDirectionUp)];
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; [[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
CGFloat collectionWidth = CollectionView().bounds.size.width; CGFloat collectionWidth =
[ContentSuggestionsAppInterface collectionView].bounds.size.width;
GREYAssertTrue(collectionWidth > 0, @"The collection width is nil."); GREYAssertTrue(collectionWidth > 0, @"The collection width is nil.");
// The fake omnibox might be slightly bigger than the screen in order to cover // The fake omnibox might be slightly bigger than the screen in order to cover
...@@ -350,10 +316,15 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -350,10 +316,15 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
assertWithMatcher:OmniboxWidthBetween(collectionWidth + 1, 2)]; assertWithMatcher:OmniboxWidthBetween(collectionWidth + 1, 2)];
[EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft
#if defined(CHROME_EARL_GREY_1)
errorOrNil:nil]; errorOrNil:nil];
#elif defined(CHROME_EARL_GREY_2)
error:nil];
#endif
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; [[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
CGFloat collectionWidthAfterRotation = CollectionView().bounds.size.width; CGFloat collectionWidthAfterRotation =
[ContentSuggestionsAppInterface collectionView].bounds.size.width;
GREYAssertNotEqual(collectionWidth, collectionWidthAfterRotation, GREYAssertNotEqual(collectionWidth, collectionWidthAfterRotation,
@"The collection width has not changed."); @"The collection width has not changed.");
...@@ -381,32 +352,24 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -381,32 +352,24 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
// Tests that the promo is correctly displayed and removed once tapped. // Tests that the promo is correctly displayed and removed once tapped.
- (void)testPromoTap { - (void)testPromoTap {
// Setup the promo. [ContentSuggestionsAppInterface 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);
// Open a new tab to have the promo. // Open a new tab to have the promo.
[ChromeEarlGreyUI openNewTab]; [ChromeEarlGreyUI openNewTab];
// Tap the promo. // Tap the promo.
[[EarlGrey selectElementWithMatcher:grey_accessibilityID( [[EarlGrey
[ContentSuggestionsWhatsNewItem selectElementWithMatcher:grey_accessibilityID(
accessibilityIdentifier])] @"ContentSuggestionsWhatsNewIdentifier")]
performAction:grey_tap()]; performAction:grey_tap()];
// Promo dismissed. // Promo dismissed.
[[EarlGrey selectElementWithMatcher:grey_accessibilityID( [[EarlGrey
[ContentSuggestionsWhatsNewItem selectElementWithMatcher:grey_accessibilityID(
accessibilityIdentifier])] @"ContentSuggestionsWhatsNewIdentifier")]
assertWithMatcher:grey_not(grey_sufficientlyVisible())]; assertWithMatcher:grey_not(grey_sufficientlyVisible())];
// Reset the promo. [ContentSuggestionsAppInterface resetWhatsNewPromo];
[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 // Tests that the position of the collection view is restored when navigating
...@@ -415,10 +378,8 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -415,10 +378,8 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
[self addMostVisitedTile]; [self addMostVisitedTile];
// Add suggestions to be able to scroll on iPad. // Add suggestions to be able to scroll on iPad.
ReadingListModelFactory::GetForBrowserState(self.browserState) [ContentSuggestionsAppInterface addNumberOfSuggestions:15
->AddEntry(GURL("http://chromium.org/"), "title", additionalSuggestionsURL:nil];
reading_list::ADDED_VIA_CURRENT_APP);
self.provider->FireSuggestionsChanged(self.category, Suggestions());
// Scroll to have a position to restored. // Scroll to have a position to restored.
[[EarlGrey selectElementWithMatcher:chrome_test_util:: [[EarlGrey selectElementWithMatcher:chrome_test_util::
...@@ -426,7 +387,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -426,7 +387,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
performAction:grey_scrollInDirection(kGREYDirectionDown, 150)]; performAction:grey_scrollInDirection(kGREYDirectionDown, 150)];
// Save the position before navigating. // Save the position before navigating.
UIView* omnibox = FakeOmnibox(); UIView* omnibox = [ContentSuggestionsAppInterface fakeOmnibox];
CGPoint previousPosition = omnibox.bounds.origin; CGPoint previousPosition = omnibox.bounds.origin;
// Navigate and come back. // Navigate and come back.
...@@ -438,7 +399,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -438,7 +399,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
[ChromeEarlGrey goBack]; [ChromeEarlGrey goBack];
// Check that the new position is the same. // Check that the new position is the same.
omnibox = FakeOmnibox(); omnibox = [ContentSuggestionsAppInterface fakeOmnibox];
GREYAssertEqual(previousPosition.y, omnibox.bounds.origin.y, GREYAssertEqual(previousPosition.y, omnibox.bounds.origin.y,
@"Omnibox not at the same position"); @"Omnibox not at the same position");
} }
...@@ -450,10 +411,8 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -450,10 +411,8 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
[self addMostVisitedTile]; [self addMostVisitedTile];
// Add suggestions to be able to scroll on iPad. // Add suggestions to be able to scroll on iPad.
ReadingListModelFactory::GetForBrowserState(self.browserState) [ContentSuggestionsAppInterface addNumberOfSuggestions:15
->AddEntry(GURL("http://chromium.org/"), "title", additionalSuggestionsURL:nil];
reading_list::ADDED_VIA_CURRENT_APP);
self.provider->FireSuggestionsChanged(self.category, Suggestions());
// Scroll to have a position to restored. // Scroll to have a position to restored.
[[EarlGrey selectElementWithMatcher:chrome_test_util:: [[EarlGrey selectElementWithMatcher:chrome_test_util::
...@@ -461,7 +420,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -461,7 +420,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
performAction:grey_scrollInDirection(kGREYDirectionDown, 150)]; performAction:grey_scrollInDirection(kGREYDirectionDown, 150)];
// Save the position before navigating. // Save the position before navigating.
UIView* omnibox = FakeOmnibox(); UIView* omnibox = [ContentSuggestionsAppInterface fakeOmnibox];
CGPoint previousPosition = omnibox.bounds.origin; CGPoint previousPosition = omnibox.bounds.origin;
// Tap the omnibox to focus it. // Tap the omnibox to focus it.
...@@ -476,7 +435,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -476,7 +435,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
[ChromeEarlGrey goBack]; [ChromeEarlGrey goBack];
// Check that the new position is the same. // Check that the new position is the same.
omnibox = FakeOmnibox(); omnibox = [ContentSuggestionsAppInterface fakeOmnibox];
GREYAssertEqual(previousPosition.y, omnibox.bounds.origin.y, GREYAssertEqual(previousPosition.y, omnibox.bounds.origin.y,
@"Omnibox not at the same position"); @"Omnibox not at the same position");
} }
...@@ -485,7 +444,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -485,7 +444,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
- (void)testTapFakeOmnibox { - (void)testTapFakeOmnibox {
// TODO(crbug.com/753098): Re-enable this test on iPad once grey_typeText // TODO(crbug.com/753098): Re-enable this test on iPad once grey_typeText
// works. // works.
if (IsRegularXRegularSizeClass()) { if ([ChromeEarlGrey isRegularXRegularSizeClass]) {
EARL_GREY_TEST_DISABLED(@"Test disabled on iPad."); EARL_GREY_TEST_DISABLED(@"Test disabled on iPad.");
} }
// Setup the server. // Setup the server.
...@@ -509,24 +468,20 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -509,24 +468,20 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
// It is important for ranking algorithm of omnibox that requests from the // It is important for ranking algorithm of omnibox that requests from the
// search button and real omnibox are marked appropriately. // search button and real omnibox are marked appropriately.
- (void)testTapOmniboxSearchButtonLogsCorrectly { - (void)testTapOmniboxSearchButtonLogsCorrectly {
if (IsRegularXRegularSizeClass()) { if ([ChromeEarlGrey isRegularXRegularSizeClass]) {
// This logging only happens on iPhone, since on iPad there's no secondary // This logging only happens on iPhone, since on iPad there's no secondary
// toolbar. // toolbar.
return; 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. // Tap the search button.
[[EarlGrey selectElementWithMatcher:grey_accessibilityID( [[EarlGrey selectElementWithMatcher:grey_accessibilityID(
kToolbarSearchButtonIdentifier)] kToolbarSearchButtonIdentifier)]
performAction:grey_tap()]; performAction:grey_tap()];
BOOL tapped = [ContentSuggestionsAppInterface resetSearchButtonLogging];
// Check that the page is loaded. // Check that the page is loaded.
GREYAssertTrue(tapped, GREYAssertTrue(tapped,
@"The tap on the search button was not correctly logged."); @"The tap on the search button was not correctly logged.");
...@@ -535,10 +490,9 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -535,10 +490,9 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
// Tests that tapping the fake omnibox moves the collection. // Tests that tapping the fake omnibox moves the collection.
- (void)testTapFakeOmniboxScroll { - (void)testTapFakeOmniboxScroll {
// Get the collection and its layout. // Get the collection and its layout.
UIView* collection = CollectionView(); UICollectionView* collectionView =
GREYAssertTrue([collection isKindOfClass:[UICollectionView class]], [ContentSuggestionsAppInterface collectionView];
@"The collection has not been correctly selected.");
UICollectionView* collectionView = (UICollectionView*)collection;
GREYAssertTrue( GREYAssertTrue(
[collectionView.delegate [collectionView.delegate
conformsToProtocol:@protocol(UICollectionViewDelegateFlowLayout)], conformsToProtocol:@protocol(UICollectionViewDelegateFlowLayout)],
...@@ -564,7 +518,8 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -564,7 +518,8 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
[[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()] [[EarlGrey selectElementWithMatcher:chrome_test_util::FakeOmnibox()]
assertWithMatcher:grey_not(grey_sufficientlyVisible())]; assertWithMatcher:grey_not(grey_sufficientlyVisible())];
CGFloat top = CollectionView().safeAreaInsets.top; CGFloat top =
[ContentSuggestionsAppInterface collectionView].safeAreaInsets.top;
GREYAssertTrue(offsetAfterTap.y >= origin.y + headerHeight - (60 + top), GREYAssertTrue(offsetAfterTap.y >= origin.y + headerHeight - (60 + top),
@"The collection has not moved."); @"The collection has not moved.");
...@@ -586,10 +541,8 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -586,10 +541,8 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
// back to where it was. // back to where it was.
- (void)testTapFakeOmniboxScrollScrolled { - (void)testTapFakeOmniboxScrollScrolled {
// Get the collection and its layout. // Get the collection and its layout.
UIView* collection = CollectionView(); UICollectionView* collectionView =
GREYAssertTrue([collection isKindOfClass:[UICollectionView class]], [ContentSuggestionsAppInterface collectionView];
@"The collection has not been correctly selected.");
UICollectionView* collectionView = (UICollectionView*)collection;
// Scroll to have a position different from the default. // Scroll to have a position different from the default.
[[EarlGrey selectElementWithMatcher:chrome_test_util:: [[EarlGrey selectElementWithMatcher:chrome_test_util::
...@@ -621,7 +574,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -621,7 +574,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
// Tests tapping the search button when the fake omnibox is scrolled. // Tests tapping the search button when the fake omnibox is scrolled.
- (void)testTapSearchButtonFakeOmniboxScrolled { - (void)testTapSearchButtonFakeOmniboxScrolled {
if (IsRegularXRegularSizeClass()) { if ([ChromeEarlGrey isRegularXRegularSizeClass]) {
// This only happens on iPhone, since on iPad there's no secondary toolbar. // This only happens on iPhone, since on iPad there's no secondary toolbar.
return; return;
} }
...@@ -662,7 +615,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -662,7 +615,7 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
{ {
ScopedSynchronizationDisabler disabler; ScopedSynchronizationDisabler disabler;
[[EarlGrey selectElementWithMatcher:tabGridMatcher] [[EarlGrey selectElementWithMatcher:tabGridMatcher]
performAction:[GREYActions actionForLongPressWithDuration:0.05]]; performAction:grey_longPressWithDuration(0.05)];
} }
[[EarlGrey selectElementWithMatcher:chrome_test_util::TabGridNewTabButton()] [[EarlGrey selectElementWithMatcher:chrome_test_util::TabGridNewTabButton()]
performAction:grey_tap()]; performAction:grey_tap()];
...@@ -675,19 +628,19 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -675,19 +628,19 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
- (void)testFavicons { - (void)testFavicons {
for (NSInteger index = 0; index < 8; index++) { for (NSInteger index = 0; index < 8; index++) {
[[EarlGrey [[EarlGrey selectElementWithMatcher:
selectElementWithMatcher: grey_accessibilityID([NSString
grey_accessibilityID([NSString stringWithFormat:@"%@%li",
stringWithFormat: @"contentSuggestionsMostVisitedAccessib"
@"%@%li", @"ilityIdentifierPrefix",
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix, index])]
index])] assertWithMatcher:grey_sufficientlyVisible()]; assertWithMatcher:grey_sufficientlyVisible()];
} }
// Change the Search Engine to Yahoo!. // Change the Search Engine to Yahoo!.
[ChromeEarlGreyUI openSettingsMenu]; [ChromeEarlGreyUI openSettingsMenu];
[ChromeEarlGreyUI [ChromeEarlGreyUI
tapSettingsMenuButton:grey_accessibilityID(kSettingsSearchEngineCellId)]; tapSettingsMenuButton:grey_accessibilityID(@"Search Engine")];
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Yahoo!")] [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Yahoo!")]
performAction:grey_tap()]; performAction:grey_tap()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::SettingsDoneButton()] [[EarlGrey selectElementWithMatcher:chrome_test_util::SettingsDoneButton()]
...@@ -695,19 +648,19 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -695,19 +648,19 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
// Check again the favicons. // Check again the favicons.
for (NSInteger index = 0; index < 8; index++) { for (NSInteger index = 0; index < 8; index++) {
[[EarlGrey [[EarlGrey selectElementWithMatcher:
selectElementWithMatcher: grey_accessibilityID([NSString
grey_accessibilityID([NSString stringWithFormat:@"%@%li",
stringWithFormat: @"contentSuggestionsMostVisitedAccessib"
@"%@%li", @"ilityIdentifierPrefix",
kContentSuggestionsMostVisitedAccessibilityIdentifierPrefix, index])]
index])] assertWithMatcher:grey_sufficientlyVisible()]; assertWithMatcher:grey_sufficientlyVisible()];
} }
// Change the Search Engine to Google. // Change the Search Engine to Google.
[ChromeEarlGreyUI openSettingsMenu]; [ChromeEarlGreyUI openSettingsMenu];
[ChromeEarlGreyUI [ChromeEarlGreyUI
tapSettingsMenuButton:grey_accessibilityID(kSettingsSearchEngineCellId)]; tapSettingsMenuButton:grey_accessibilityID(@"Search Engine")];
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Google")] [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Google")]
performAction:grey_tap()]; performAction:grey_tap()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::SettingsDoneButton()] [[EarlGrey selectElementWithMatcher:chrome_test_util::SettingsDoneButton()]
......
...@@ -21,6 +21,11 @@ class AdditionalSuggestionsHelper; ...@@ -21,6 +21,11 @@ class AdditionalSuggestionsHelper;
// Shared instance of this singleton. // Shared instance of this singleton.
+ (instancetype)sharedInstance; + (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|. // Resets the stored additionalSuggestions helper with |URL|.
- (void)resetAdditionalSuggestionsHelperWithURL:(const GURL&)URL; - (void)resetAdditionalSuggestionsHelperWithURL:(const GURL&)URL;
// Returns the stored additionalSuggestionsHelper. // Returns the stored additionalSuggestionsHelper.
...@@ -32,6 +37,12 @@ class AdditionalSuggestionsHelper; ...@@ -32,6 +37,12 @@ class AdditionalSuggestionsHelper;
- (void)registerArticleProvider: - (void)registerArticleProvider:
(ntp_snippets::ContentSuggestionsService*)service; (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 @end
#endif // IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_NTP_HOME_PROVIDER_TEST_SINGLETON_H_ #endif // IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_NTP_HOME_PROVIDER_TEST_SINGLETON_H_
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "components/ntp_snippets/content_suggestion.h" #include "components/ntp_snippets/content_suggestion.h"
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_test_utils.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) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
...@@ -18,6 +19,8 @@ ...@@ -18,6 +19,8 @@
ntp_snippets::MockContentSuggestionsProvider* _provider; ntp_snippets::MockContentSuggestionsProvider* _provider;
std::unique_ptr<ntp_snippets::AdditionalSuggestionsHelper> std::unique_ptr<ntp_snippets::AdditionalSuggestionsHelper>
_additionalSuggestionsHelper; _additionalSuggestionsHelper;
std::unique_ptr<ScopedBlockSwizzler> _swizzler;
__block BOOL _tapped;
} }
+ (instancetype)sharedInstance { + (instancetype)sharedInstance {
...@@ -53,4 +56,20 @@ ...@@ -53,4 +56,20 @@
service->RegisterProvider(std::move(provider)); 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 @end
...@@ -20,16 +20,6 @@ UICollectionView* CollectionView(); ...@@ -20,16 +20,6 @@ UICollectionView* CollectionView();
// Returns the view corresponding to the fake omnibox. Returns nil if it is not // Returns the view corresponding to the fake omnibox. Returns nil if it is not
// in the view hierarchy. // in the view hierarchy.
UIView* FakeOmnibox(); 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_home
namespace ntp_snippets { namespace ntp_snippets {
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#endif #endif
namespace { namespace {
// Helper method to get the Article category. // Helper method to get the Article category.
ntp_snippets::Category Category() { ntp_snippets::Category Category() {
return ntp_snippets::Category::FromKnownCategory( return ntp_snippets::Category::FromKnownCategory(
...@@ -55,36 +56,6 @@ UIView* SubviewWithAccessibilityIdentifier(NSString* accessibilityID, ...@@ -55,36 +56,6 @@ UIView* SubviewWithAccessibilityIdentifier(NSString* accessibilityID,
} // namespace } // namespace
namespace ntp_home { 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() { UICollectionView* CollectionView() {
return base::mac::ObjCCast<UICollectionView>( return base::mac::ObjCCast<UICollectionView>(
...@@ -99,31 +70,6 @@ UIView* FakeOmnibox() { ...@@ -99,31 +70,6 @@ UIView* FakeOmnibox() {
[[UIApplication sharedApplication] keyWindow]); [[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_home
namespace ntp_snippets { 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