Commit 208ac774 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

Convert ContentSuggestions to EG2

Bug: 987646
Change-Id: I6a7b80daea151ba6bb1400ab578ad35a1a51be53
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1872211Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708136}
parent 74ebfd22
......@@ -229,13 +229,38 @@ source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
}
source_set("eg2_tests") {
defines = [ "CHROME_EARL_GREY_2" ]
configs += [
"//build/config/compiler:enable_arc",
"//build/config/ios:xctest_config",
]
testonly = true
sources = [
"content_suggestions_egtest.mm",
]
deps = [
":content_suggestions_constant",
":eg_test_support+eg2",
"//base",
"//base/test:test_support",
"//components/strings",
"//ios/chrome/app/strings",
"//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/strings",
]
libs = [ "UIKit.framework" ]
}
source_set("eg_tests") {
defines = [ "CHROME_EARL_GREY_1" ]
testonly = true
sources = [
"content_suggestions_egtest.mm",
"ntp_home_egtest.mm",
"ntp_home_provider_test_singleton.h",
"ntp_home_provider_test_singleton.mm",
]
deps = [
":content_suggestions_constant",
......@@ -280,18 +305,86 @@ source_set("eg_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
}
source_set("eg_test_support+eg2") {
defines = [ "CHROME_EARL_GREY_2" ]
configs += [
"//build/config/compiler:enable_arc",
"//build/config/ios:xctest_config",
]
testonly = true
sources = [
"content_suggestions_app_interface.h",
]
deps = [
"//ios/third_party/earl_grey2:test_lib",
]
}
source_set("eg_app_support+eg2") {
configs += [
"//build/config/compiler:enable_arc",
"//build/config/ios:xctest_config",
]
testonly = true
defines = [ "CHROME_EARL_GREY_2" ]
sources = [
"content_suggestions_app_interface.h",
"content_suggestions_app_interface.mm",
"ntp_home_provider_test_singleton.h",
"ntp_home_provider_test_singleton.mm",
"ntp_home_test_utils.h",
"ntp_home_test_utils.mm",
]
deps = [
":content_suggestions_constant",
":content_suggestions_ui",
":content_suggestions_ui_util",
"//base/test:test_support",
"//components/keyed_service/ios",
"//components/ntp_snippets",
"//components/ntp_snippets:test_support",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/ntp_snippets",
"//ios/chrome/browser/ui/util",
"//ios/chrome/test/app:test_support",
"//ios/testing/earl_grey:eg_app_support+eg2",
"//ios/third_party/earl_grey2:app_framework+link",
"//testing/gmock",
"//testing/gtest:gtest",
]
}
source_set("test_support") {
testonly = true
defines = [ "CHROME_EARL_GREY_1" ]
sources = [
"content_suggestions_app_interface.h",
"content_suggestions_app_interface.mm",
"ntp_home_provider_test_singleton.h",
"ntp_home_provider_test_singleton.mm",
"ntp_home_test_utils.h",
"ntp_home_test_utils.mm",
]
deps = [
":content_suggestions_ui_util",
"//components/keyed_service/ios",
"//components/ntp_snippets",
"//components/ntp_snippets:test_support",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/ntp_snippets",
"//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/testing/earl_grey:earl_grey_support",
"//ios/third_party/earl_grey:earl_grey+link",
"//ios/web",
"//testing/gmock",
]
configs += [ "//build/config/compiler:enable_arc" ]
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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>
// App interface for the Content Suggestions.
@interface ContentSuggestionsAppInterface : NSObject
// Sets the fake service up.
+ (void)setUpService;
// Resets the service to the real, non-fake service to avoid leaking the fake.
+ (void)resetService;
// Marks the suggestions as available.
+ (void)makeSuggestionsAvailable;
// Disables the suggestions.
+ (void)disableSuggestions;
// Adds |numberOfSuggestions| suggestions to the list of suggestions provided.
// The suggestions have the name "chromium<suggestionNumber>" and the url
// http://chromium/<suggestionNumber>.
+ (void)addNumberOfSuggestions:(NSInteger)numberOfSuggestions
additionalSuggestionsURL:(NSURL*)URL;
// Add one particular suggestion, following the convention explained above, with
// |suggestionNumber|.
+ (void)addSuggestionNumber:(NSInteger)suggestionNumber;
@end
#endif // IOS_CHROME_BROWSER_UI_CONTENT_SUGGESTIONS_CONTENT_SUGGESTIONS_APP_INTERFACE_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_app_interface.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
#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 "ios/chrome/browser/browser_state/chrome_browser_state.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/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"
#include "ios/chrome/browser/ui/util/ui_util.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
#import "ios/chrome/test/app/chrome_test_util.h"
#import "net/base/mac/url_conversions.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
using ntp_snippets::AdditionalSuggestionsHelper;
using ntp_snippets::Category;
using ntp_snippets::CategoryStatus;
using ntp_snippets::ContentSuggestion;
using ntp_snippets::ContentSuggestionsService;
using ntp_snippets::CreateChromeContentSuggestionsService;
using ntp_snippets::KnownCategories;
using ntp_snippets::MockContentSuggestionsProvider;
using testing::_;
using testing::Invoke;
using testing::WithArg;
namespace {
// Returns a suggestion created from the |category|, |suggestion_id| and the
// |url|.
ContentSuggestion CreateSuggestion(Category category,
std::string suggestion_id,
GURL url) {
ContentSuggestion suggestion(category, suggestion_id, url);
suggestion.set_title(base::UTF8ToUTF16(url.spec()));
return suggestion;
}
} // namespace
@implementation ContentSuggestionsAppInterface
+ (void)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)resetService {
ios::ChromeBrowserState* browserState =
chrome_test_util::GetOriginalBrowserState();
// 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));
}
+ (void)makeSuggestionsAvailable {
[self provider] -> FireCategoryStatusChanged([self category],
CategoryStatus::AVAILABLE);
}
+ (void)disableSuggestions {
[self provider] -> FireCategoryStatusChanged(
[self category],
CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED);
}
+ (void)addNumberOfSuggestions:(NSInteger)numberOfSuggestions
additionalSuggestionsURL:(NSURL*)URL {
GURL newURL = net::GURLWithNSURL(URL);
std::vector<ContentSuggestion> suggestions;
for (NSInteger i = 1; i <= numberOfSuggestions; i++) {
std::string index = base::SysNSStringToUTF8(@(i).stringValue);
suggestions.push_back(
CreateSuggestion([self category], "chromium" + index,
GURL("http://chromium.org/" + index)));
}
[self provider] -> FireSuggestionsChanged([self category],
std::move(suggestions));
// Set up the action when "More" is tapped.
[[ContentSuggestionsTestSingleton sharedInstance]
resetAdditionalSuggestionsHelperWithURL:newURL];
EXPECT_CALL(*[self provider], FetchMock(_, _, _))
.WillOnce(WithArg<2>(
Invoke([[ContentSuggestionsTestSingleton sharedInstance]
additionalSuggestionsHelper],
&AdditionalSuggestionsHelper::SendAdditionalSuggestions)));
}
+ (void)addSuggestionNumber:(NSInteger)suggestionNumber {
std::string index = base::NumberToString(suggestionNumber);
std::vector<ContentSuggestion> suggestions;
suggestions.push_back(CreateSuggestion([self category], "chromium" + index,
GURL("http://chromium.org/" + index)));
[self provider] -> FireSuggestionsChanged([self category],
std::move(suggestions));
}
#pragma mark - Helper
+ (MockContentSuggestionsProvider*)provider {
return [[ContentSuggestionsTestSingleton sharedInstance] provider];
}
+ (Category)category {
return Category::FromKnownCategory(KnownCategories::ARTICLES);
}
@end
......@@ -10,6 +10,10 @@
#include "components/ntp_snippets/content_suggestions_service.h"
#include "components/ntp_snippets/mock_content_suggestions_provider.h"
namespace ntp_snippets {
class AdditionalSuggestionsHelper;
}
// Singleton allowing to register the provider in the +setup and still access it
// from inside the tests.
@interface ContentSuggestionsTestSingleton : NSObject
......@@ -17,6 +21,11 @@
// Shared instance of this singleton.
+ (instancetype)sharedInstance;
// Resets the stored additionalSuggestions helper with |URL|.
- (void)resetAdditionalSuggestionsHelperWithURL:(const GURL&)URL;
// Returns the stored additionalSuggestionsHelper.
- (ntp_snippets::AdditionalSuggestionsHelper*)additionalSuggestionsHelper;
// Returns the provider registered.
- (ntp_snippets::MockContentSuggestionsProvider*)provider;
// Registers a provider in the |service|.
......
......@@ -7,6 +7,7 @@
#include <memory>
#include "components/ntp_snippets/content_suggestion.h"
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_test_utils.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -15,6 +16,8 @@
@implementation ContentSuggestionsTestSingleton {
ntp_snippets::MockContentSuggestionsProvider* _provider;
std::unique_ptr<ntp_snippets::AdditionalSuggestionsHelper>
_additionalSuggestionsHelper;
}
+ (instancetype)sharedInstance {
......@@ -26,6 +29,15 @@
return sharedInstance;
}
- (void)resetAdditionalSuggestionsHelperWithURL:(const GURL&)URL {
_additionalSuggestionsHelper =
std::make_unique<ntp_snippets::AdditionalSuggestionsHelper>(URL);
}
- (ntp_snippets::AdditionalSuggestionsHelper*)additionalSuggestionsHelper {
return _additionalSuggestionsHelper.get();
}
- (ntp_snippets::MockContentSuggestionsProvider*)provider {
return _provider;
}
......
......@@ -4,8 +4,6 @@
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_test_utils.h"
#import <EarlGrey/EarlGrey.h>
#include <string>
#include "base/callback.h"
......@@ -15,6 +13,7 @@
#include "components/ntp_snippets/status.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/testing/earl_grey/earl_grey_app.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -57,10 +56,10 @@ UIView* SubviewWithAccessibilityIdentifier(NSString* accessibilityID,
namespace ntp_home {
id<GREYMatcher> OmniboxWidth(CGFloat width) {
MatchesBlock matches = ^BOOL(UIView* view) {
GREYMatchesBlock matches = ^BOOL(UIView* view) {
return fabs(view.bounds.size.width - width) < 0.001;
};
DescribeToBlock describe = ^void(id<GREYDescription> description) {
GREYDescribeToBlock describe = ^void(id<GREYDescription> description) {
[description
appendText:[NSString stringWithFormat:@"Omnibox has correct width: %g",
width]];
......@@ -71,11 +70,11 @@ id<GREYMatcher> OmniboxWidth(CGFloat width) {
}
id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
MatchesBlock matches = ^BOOL(UIView* view) {
GREYMatchesBlock matches = ^BOOL(UIView* view) {
return view.bounds.size.width >= width - margin &&
view.bounds.size.width <= width + margin;
};
DescribeToBlock describe = ^void(id<GREYDescription> description) {
GREYDescribeToBlock describe = ^void(id<GREYDescription> description) {
[description
appendText:[NSString
stringWithFormat:
......
......@@ -364,6 +364,7 @@ source_set("eg_app_support+eg2") {
"//ios/chrome/browser/ui/bookmarks:bookmarks_ui",
"//ios/chrome/browser/ui/content_suggestions:content_suggestions_constant",
"//ios/chrome/browser/ui/content_suggestions:content_suggestions_ui",
"//ios/chrome/browser/ui/content_suggestions:eg_app_support+eg2",
"//ios/chrome/browser/ui/history:constants",
"//ios/chrome/browser/ui/location_bar:location_bar",
"//ios/chrome/browser/ui/omnibox:omnibox_internal",
......
......@@ -49,6 +49,7 @@ chrome_ios_eg2_test("ios_chrome_ui_eg2tests_module") {
deps = [
"//ios/chrome/browser/ui/activity_services:eg2_tests",
"//ios/chrome/browser/ui/content_suggestions:eg2_tests",
"//ios/chrome/browser/ui/download:eg2_tests",
"//ios/chrome/browser/ui/integration_tests:eg2_tests",
"//ios/chrome/browser/ui/ntp:eg2_tests",
......
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