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") { ...@@ -229,13 +229,38 @@ source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ] 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") { source_set("eg_tests") {
defines = [ "CHROME_EARL_GREY_1" ]
testonly = true testonly = true
sources = [ sources = [
"content_suggestions_egtest.mm", "content_suggestions_egtest.mm",
"ntp_home_egtest.mm", "ntp_home_egtest.mm",
"ntp_home_provider_test_singleton.h",
"ntp_home_provider_test_singleton.mm",
] ]
deps = [ deps = [
":content_suggestions_constant", ":content_suggestions_constant",
...@@ -280,18 +305,86 @@ source_set("eg_tests") { ...@@ -280,18 +305,86 @@ source_set("eg_tests") {
configs += [ "//build/config/compiler:enable_arc" ] 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") { source_set("test_support") {
testonly = true testonly = true
defines = [ "CHROME_EARL_GREY_1" ]
sources = [ 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.h",
"ntp_home_test_utils.mm", "ntp_home_test_utils.mm",
] ]
deps = [ deps = [
":content_suggestions_ui_util",
"//components/keyed_service/ios",
"//components/ntp_snippets", "//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",
"//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/test/app:test_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",
"//testing/gmock",
] ]
configs += [ "//build/config/compiler:enable_arc" ] 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
...@@ -2,9 +2,6 @@ ...@@ -2,9 +2,6 @@
// 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 <memory>
#include <vector> #include <vector>
...@@ -14,50 +11,30 @@ ...@@ -14,50 +11,30 @@
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#import "base/test/ios/wait_util.h" #import "base/test/ios/wait_util.h"
#include "base/test/scoped_command_line.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 "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h" #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_app_interface.h"
#include "ios/chrome/browser/chrome_switches.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"
#import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_header_item.h"
#import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_learn_more_item.h"
#include "ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_utils.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"
#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"
#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/testing/earl_grey/earl_grey_test.h"
#import "net/base/mac/url_conversions.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 "testing/gmock/include/gmock/gmock.h"
#include "ui/strings/grit/ui_strings.h" #include "ui/strings/grit/ui_strings.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."
#endif #endif
using ntp_snippets::AdditionalSuggestionsHelper; #if defined(CHROME_EARL_GREY_2)
using ntp_snippets::Category; #pragma clang diagnostic push
using ntp_snippets::CategoryStatus; #pragma clang diagnostic ignored "-Wc++98-compat-extra-semi"
using ntp_snippets::ContentSuggestion; GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(ContentSuggestionsAppInterface);
using ntp_snippets::ContentSuggestionsService; #pragma clang diagnostic pop
using ntp_snippets::CreateChromeContentSuggestionsService; #endif // defined(CHROME_EARL_GREY_2)
using ntp_snippets::KnownCategories;
using ntp_snippets::MockContentSuggestionsProvider;
using testing::_;
using testing::Invoke;
using testing::WithArg;
namespace { namespace {
...@@ -90,17 +67,6 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse( ...@@ -90,17 +67,6 @@ std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
return std::move(http_response); return std::move(http_response);
} }
// Returns a suggestion created from the |category|, |suggestion_id| and the
// |url|.
ContentSuggestion Suggestion(Category category,
std::string suggestion_id,
GURL url) {
ContentSuggestion suggestion(category, suggestion_id, url);
suggestion.set_title(base::UTF8ToUTF16(url.spec()));
return suggestion;
}
// Select the cell with the |matcher| by scrolling the collection. // Select the cell with the |matcher| by scrolling the collection.
// 200 is a reasonable scroll displacement that works for all UI elements, while // 200 is a reasonable scroll displacement that works for all UI elements, while
// not being too slow. // not being too slow.
...@@ -123,72 +89,47 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) { ...@@ -123,72 +89,47 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) {
// Test case for the ContentSuggestion UI. // Test case for the ContentSuggestion UI.
@interface ContentSuggestionsTestCase : ChromeTestCase @interface ContentSuggestionsTestCase : ChromeTestCase
// 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) Category category;
@end @end
@implementation ContentSuggestionsTestCase @implementation ContentSuggestionsTestCase
#pragma mark - Setup/Teardown #pragma mark - Setup/Teardown
#if defined(CHROME_EARL_GREY_2)
+ (void)setUpForTestCase {
[super setUpForTestCase];
[self setUpHelper];
}
#elif defined(CHROME_EARL_GREY_1)
+ (void)setUp { + (void)setUp {
[super setUp]; [super setUp];
[self setUpHelper];
}
#else
#error Not an EarlGrey Test
#endif
+ (void)setUpHelper {
[self closeAllTabs]; [self closeAllTabs];
ios::ChromeBrowserState* browserState =
chrome_test_util::GetOriginalBrowserState(); [ContentSuggestionsAppInterface setUpService];
// 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 =
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));
[super tearDown];
}
// Per crbug.com/845186, Disable flakey iPad Retina tests that are limited [ContentSuggestionsAppInterface resetService];
// to iOS 10.2.
+ (NSArray*)testInvocations { [super tearDown];
#if TARGET_IPHONE_SIMULATOR
if ([ChromeEarlGrey isIPadIdiom] && !base::ios::IsRunningOnOrLater(10, 3, 0))
return @[];
#endif // TARGET_IPHONE_SIMULATOR
return [super testInvocations];
} }
- (void)setUp { - (void)setUp {
self.provider->FireCategoryStatusChanged(self.category,
CategoryStatus::AVAILABLE);
[super setUp]; [super setUp];
[ContentSuggestionsAppInterface makeSuggestionsAvailable];
} }
- (void)tearDown { - (void)tearDown {
self.provider->FireCategoryStatusChanged( [ContentSuggestionsAppInterface disableSuggestions];
self.category, CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED);
[ChromeEarlGrey clearBrowsingHistory]; [ChromeEarlGrey clearBrowsingHistory];
[super tearDown]; [super tearDown];
} }
...@@ -204,20 +145,9 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) { ...@@ -204,20 +145,9 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) {
const GURL pageURL = self.testServer->GetURL(kPageURL); const GURL pageURL = self.testServer->GetURL(kPageURL);
// Add 3 suggestions, persisted accross page loads. // Add 3 suggestions, persisted accross page loads.
std::vector<ContentSuggestion> suggestions; [ContentSuggestionsAppInterface
suggestions.push_back( addNumberOfSuggestions:3
Suggestion(self.category, "chromium1", GURL("http://chromium.org/1"))); additionalSuggestionsURL:net::NSURLWithGURL(pageURL)];
suggestions.push_back(
Suggestion(self.category, "chromium2", GURL("http://chromium.org/2")));
suggestions.push_back(
Suggestion(self.category, "chromium3", GURL("http://chromium.org/3")));
self.provider->FireSuggestionsChanged(self.category, std::move(suggestions));
// Set up the action when "More" is tapped.
AdditionalSuggestionsHelper helper(pageURL);
EXPECT_CALL(*self.provider, FetchMock(_, _, _))
.WillOnce(WithArg<2>(Invoke(
&helper, &AdditionalSuggestionsHelper::SendAdditionalSuggestions)));
// Tap on more, which adds 10 elements. // Tap on more, which adds 10 elements.
[CellWithMatcher(chrome_test_util::ButtonWithAccessibilityLabelId( [CellWithMatcher(chrome_test_util::ButtonWithAccessibilityLabelId(
...@@ -243,20 +173,12 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) { ...@@ -243,20 +173,12 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) {
// Tests that when the page is reloaded using the tools menu, the suggestions // Tests that when the page is reloaded using the tools menu, the suggestions
// are updated. // are updated.
- (void)testReloadPage { - (void)testReloadPage {
// Add 2 suggestions. // Add 2 suggestions, persisted accross page loads.
std::vector<ContentSuggestion> suggestions; [ContentSuggestionsAppInterface addNumberOfSuggestions:2
suggestions.push_back( additionalSuggestionsURL:nil];
Suggestion(self.category, "chromium1", GURL("http://chromium.org/1")));
suggestions.push_back(
Suggestion(self.category, "chromium2", GURL("http://chromium.org/2")));
self.provider->FireSuggestionsChanged(self.category, std::move(suggestions));
// Change the suggestions to have one the second one. // Change the suggestions to have one the second one.
std::vector<ContentSuggestion> suggestionsOnly; [ContentSuggestionsAppInterface addSuggestionNumber:2];
suggestionsOnly.push_back(
Suggestion(self.category, "chromium2", GURL("http://chromium.org/2")));
self.provider->FireSuggestionsChanged(self.category,
std::move(suggestionsOnly));
// Check that the first suggestion is still displayed. // Check that the first suggestion is still displayed.
[CellWithMatcher(grey_accessibilityID(@"http://chromium.org/1")) [CellWithMatcher(grey_accessibilityID(@"http://chromium.org/1"))
...@@ -282,28 +204,16 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) { ...@@ -282,28 +204,16 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) {
const GURL pageURL = self.testServer->GetURL(kPageURL); const GURL pageURL = self.testServer->GetURL(kPageURL);
// Add 3 suggestions, persisted accross page loads. // Add 3 suggestions, persisted accross page loads.
std::vector<ContentSuggestion> suggestions; [ContentSuggestionsAppInterface
suggestions.push_back( addNumberOfSuggestions:3
Suggestion(self.category, "chromium1", GURL("http://chromium.org/1"))); additionalSuggestionsURL:net::NSURLWithGURL(pageURL)];
suggestions.push_back(
Suggestion(self.category, "chromium2", GURL("http://chromium.org/2")));
suggestions.push_back(
Suggestion(self.category, "chromium3", GURL("http://chromium.org/3")));
self.provider->FireSuggestionsChanged(self.category, std::move(suggestions));
// Set up the action when "More" is tapped.
AdditionalSuggestionsHelper helper(pageURL);
EXPECT_CALL(*self.provider, FetchMock(_, _, _))
.WillOnce(WithArg<2>(Invoke(
&helper, &AdditionalSuggestionsHelper::SendAdditionalSuggestions)));
// Tap on more, which adds 10 elements. // Tap on more, which adds 10 elements.
[CellWithMatcher(chrome_test_util::ButtonWithAccessibilityLabelId( [CellWithMatcher(chrome_test_util::ButtonWithAccessibilityLabelId(
IDS_IOS_CONTENT_SUGGESTIONS_FOOTER_TITLE)) performAction:grey_tap()]; IDS_IOS_CONTENT_SUGGESTIONS_FOOTER_TITLE)) performAction:grey_tap()];
// Make sure to scroll to the bottom. // Make sure to scroll to the bottom.
[CellWithMatcher(grey_accessibilityID( [CellWithMatcher(grey_accessibilityID(@"Learn more"))
[ContentSuggestionsLearnMoreItem accessibilityIdentifier]))
assertWithMatcher:grey_notNil()]; assertWithMatcher:grey_notNil()];
// Open the last item. After the extra space of the last suggestion is // Open the last item. After the extra space of the last suggestion is
...@@ -338,20 +248,15 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) { ...@@ -338,20 +248,15 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) {
- (void)testLearnMore { - (void)testLearnMore {
id<GREYAction> action = id<GREYAction> action =
grey_scrollInDirectionWithStartPoint(kGREYDirectionDown, 200, 0.5, 0.5); grey_scrollInDirectionWithStartPoint(kGREYDirectionDown, 200, 0.5, 0.5);
[[[EarlGrey selectElementWithMatcher:grey_accessibilityID( [[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Learn more")]
[ContentSuggestionsLearnMoreItem
accessibilityIdentifier])]
usingSearchAction:action usingSearchAction:action
onElementWithMatcher:chrome_test_util::ContentSuggestionCollectionView()] onElementWithMatcher:chrome_test_util::ContentSuggestionCollectionView()]
assertWithMatcher:grey_nil()]; assertWithMatcher:grey_nil()];
std::vector<ContentSuggestion> suggestions; [ContentSuggestionsAppInterface addNumberOfSuggestions:1
suggestions.push_back( additionalSuggestionsURL:nil];
Suggestion(self.category, "chromium", GURL("http://chromium.org")));
self.provider->FireSuggestionsChanged(self.category, std::move(suggestions));
[CellWithMatcher(grey_accessibilityID( [CellWithMatcher(grey_accessibilityID(@"Learn more"))
[ContentSuggestionsLearnMoreItem accessibilityIdentifier]))
assertWithMatcher:grey_sufficientlyVisible()]; assertWithMatcher:grey_sufficientlyVisible()];
} }
...@@ -477,7 +382,7 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) { ...@@ -477,7 +382,7 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) {
- (void)testMostVisitedLongPress { - (void)testMostVisitedLongPress {
[self setupMostVisitedTileLongPress]; [self setupMostVisitedTileLongPress];
if (!IsRegularXRegularSizeClass()) { if (![ChromeEarlGrey isRegularXRegularSizeClass]) {
[[EarlGrey selectElementWithMatcher: [[EarlGrey selectElementWithMatcher:
chrome_test_util::ButtonWithAccessibilityLabelId( chrome_test_util::ButtonWithAccessibilityLabelId(
IDS_APP_CANCEL)] assertWithMatcher:grey_interactable()]; IDS_APP_CANCEL)] assertWithMatcher:grey_interactable()];
...@@ -490,20 +395,6 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) { ...@@ -490,20 +395,6 @@ GREYElementInteraction* CellWithMatcher(id<GREYMatcher> matcher) {
assertWithMatcher:grey_nil()]; assertWithMatcher:grey_nil()];
} }
#pragma mark - Properties
- (ios::ChromeBrowserState*)browserState {
return chrome_test_util::GetOriginalBrowserState();
}
- (MockContentSuggestionsProvider*)provider {
return [[ContentSuggestionsTestSingleton sharedInstance] provider];
}
- (Category)category {
return Category::FromKnownCategory(KnownCategories::ARTICLES);
}
#pragma mark - Test utils #pragma mark - Test utils
// Setup a most visited tile, and open the context menu by long pressing on it. // Setup a most visited tile, and open the context menu by long pressing on it.
......
...@@ -10,6 +10,10 @@ ...@@ -10,6 +10,10 @@
#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"
namespace ntp_snippets {
class AdditionalSuggestionsHelper;
}
// Singleton allowing to register the provider in the +setup and still access it // Singleton allowing to register the provider in the +setup and still access it
// from inside the tests. // from inside the tests.
@interface ContentSuggestionsTestSingleton : NSObject @interface ContentSuggestionsTestSingleton : NSObject
...@@ -17,6 +21,11 @@ ...@@ -17,6 +21,11 @@
// Shared instance of this singleton. // Shared instance of this singleton.
+ (instancetype)sharedInstance; + (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. // Returns the provider registered.
- (ntp_snippets::MockContentSuggestionsProvider*)provider; - (ntp_snippets::MockContentSuggestionsProvider*)provider;
// Registers a provider in the |service|. // Registers a provider in the |service|.
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <memory> #include <memory>
#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"
#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."
...@@ -15,6 +16,8 @@ ...@@ -15,6 +16,8 @@
@implementation ContentSuggestionsTestSingleton { @implementation ContentSuggestionsTestSingleton {
ntp_snippets::MockContentSuggestionsProvider* _provider; ntp_snippets::MockContentSuggestionsProvider* _provider;
std::unique_ptr<ntp_snippets::AdditionalSuggestionsHelper>
_additionalSuggestionsHelper;
} }
+ (instancetype)sharedInstance { + (instancetype)sharedInstance {
...@@ -26,6 +29,15 @@ ...@@ -26,6 +29,15 @@
return sharedInstance; 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 { - (ntp_snippets::MockContentSuggestionsProvider*)provider {
return _provider; return _provider;
} }
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
#import "ios/chrome/browser/ui/content_suggestions/ntp_home_test_utils.h" #import "ios/chrome/browser/ui/content_suggestions/ntp_home_test_utils.h"
#import <EarlGrey/EarlGrey.h>
#include <string> #include <string>
#include "base/callback.h" #include "base/callback.h"
...@@ -15,6 +13,7 @@ ...@@ -15,6 +13,7 @@
#include "components/ntp_snippets/status.h" #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/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/testing/earl_grey/earl_grey_app.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."
...@@ -57,10 +56,10 @@ UIView* SubviewWithAccessibilityIdentifier(NSString* accessibilityID, ...@@ -57,10 +56,10 @@ UIView* SubviewWithAccessibilityIdentifier(NSString* accessibilityID,
namespace ntp_home { namespace ntp_home {
id<GREYMatcher> OmniboxWidth(CGFloat width) { id<GREYMatcher> OmniboxWidth(CGFloat width) {
MatchesBlock matches = ^BOOL(UIView* view) { GREYMatchesBlock matches = ^BOOL(UIView* view) {
return fabs(view.bounds.size.width - width) < 0.001; return fabs(view.bounds.size.width - width) < 0.001;
}; };
DescribeToBlock describe = ^void(id<GREYDescription> description) { GREYDescribeToBlock describe = ^void(id<GREYDescription> description) {
[description [description
appendText:[NSString stringWithFormat:@"Omnibox has correct width: %g", appendText:[NSString stringWithFormat:@"Omnibox has correct width: %g",
width]]; width]];
...@@ -71,11 +70,11 @@ id<GREYMatcher> OmniboxWidth(CGFloat width) { ...@@ -71,11 +70,11 @@ id<GREYMatcher> OmniboxWidth(CGFloat width) {
} }
id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) { id<GREYMatcher> OmniboxWidthBetween(CGFloat width, CGFloat margin) {
MatchesBlock matches = ^BOOL(UIView* view) { GREYMatchesBlock matches = ^BOOL(UIView* view) {
return view.bounds.size.width >= width - margin && return view.bounds.size.width >= width - margin &&
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 [description
appendText:[NSString appendText:[NSString
stringWithFormat: stringWithFormat:
......
...@@ -364,6 +364,7 @@ source_set("eg_app_support+eg2") { ...@@ -364,6 +364,7 @@ source_set("eg_app_support+eg2") {
"//ios/chrome/browser/ui/bookmarks:bookmarks_ui", "//ios/chrome/browser/ui/bookmarks:bookmarks_ui",
"//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/content_suggestions:eg_app_support+eg2",
"//ios/chrome/browser/ui/history:constants", "//ios/chrome/browser/ui/history:constants",
"//ios/chrome/browser/ui/location_bar:location_bar", "//ios/chrome/browser/ui/location_bar:location_bar",
"//ios/chrome/browser/ui/omnibox:omnibox_internal", "//ios/chrome/browser/ui/omnibox:omnibox_internal",
......
...@@ -49,6 +49,7 @@ chrome_ios_eg2_test("ios_chrome_ui_eg2tests_module") { ...@@ -49,6 +49,7 @@ chrome_ios_eg2_test("ios_chrome_ui_eg2tests_module") {
deps = [ deps = [
"//ios/chrome/browser/ui/activity_services:eg2_tests", "//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/download:eg2_tests",
"//ios/chrome/browser/ui/integration_tests:eg2_tests", "//ios/chrome/browser/ui/integration_tests:eg2_tests",
"//ios/chrome/browser/ui/ntp: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