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") {
testonly = true
sources = [
"content_suggestions_egtest.mm",
"ntp_home_egtest.mm",
]
deps = [
":content_suggestions_constant",
......@@ -246,10 +247,14 @@ 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" ]
......@@ -347,10 +352,15 @@ 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",
......@@ -374,13 +384,18 @@ 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 <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
// App interface for the Content Suggestions.
@interface ContentSuggestionsAppInterface : NSObject
......@@ -32,6 +32,36 @@
// |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,9 +11,15 @@
#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"
......@@ -26,6 +32,7 @@
#error "This file requires ARC support."
#endif
using content_suggestions::searchFieldWidth;
using ntp_snippets::AdditionalSuggestionsHelper;
using ntp_snippets::Category;
using ntp_snippets::CategoryStatus;
......@@ -125,6 +132,74 @@ 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 {
......
......@@ -21,6 +21,11 @@ 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.
......@@ -32,6 +37,12 @@ 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,6 +8,7 @@
#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."
......@@ -18,6 +19,8 @@
ntp_snippets::MockContentSuggestionsProvider* _provider;
std::unique_ptr<ntp_snippets::AdditionalSuggestionsHelper>
_additionalSuggestionsHelper;
std::unique_ptr<ScopedBlockSwizzler> _swizzler;
__block BOOL _tapped;
}
+ (instancetype)sharedInstance {
......@@ -53,4 +56,20 @@
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,16 +20,6 @@ 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,6 +20,7 @@
#endif
namespace {
// Helper method to get the Article category.
ntp_snippets::Category Category() {
return ntp_snippets::Category::FromKnownCategory(
......@@ -55,36 +56,6 @@ 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>(
......@@ -99,31 +70,6 @@ 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