Commit 11bc1f22 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

Prepare AppState test for ContentSuggestions

This CL makes sure that the AppState unit tests can run with
ContentSuggestions enabled.

Bug: 764720
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I0773b29330f1a0b139b0f25a707e9d58c1f87cd8
Reviewed-on: https://chromium-review.googlesource.com/758838
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#515520}
parent cc8ad040
......@@ -46,6 +46,7 @@ source_set("unit_tests") {
"//ios/chrome/browser/device_sharing",
"//ios/chrome/browser/geolocation",
"//ios/chrome/browser/metrics",
"//ios/chrome/browser/ntp_snippets",
"//ios/chrome/browser/tabs",
"//ios/chrome/browser/u2f",
"//ios/chrome/browser/ui:ui_internal",
......
......@@ -23,9 +23,12 @@
#import "ios/chrome/app/main_application_delegate.h"
#import "ios/chrome/app/startup/content_suggestions_scheduler_notifications.h"
#import "ios/chrome/browser/app_startup_parameters.h"
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#include "ios/chrome/browser/chrome_url_constants.h"
#import "ios/chrome/browser/device_sharing/device_sharing_manager.h"
#include "ios/chrome/browser/experimental_flags.h"
#import "ios/chrome/browser/geolocation/omnibox_geolocation_config.h"
#include "ios/chrome/browser/ntp_snippets/ios_chrome_content_suggestions_service_factory.h"
#import "ios/chrome/browser/tabs/tab_model.h"
#import "ios/chrome/browser/ui/browser_view_controller.h"
#import "ios/chrome/browser/ui/commands/browser_commands.h"
......@@ -33,6 +36,7 @@
#import "ios/chrome/browser/ui/safe_mode/safe_mode_coordinator.h"
#import "ios/chrome/browser/ui/settings/settings_navigation_controller.h"
#import "ios/chrome/test/base/scoped_block_swizzler.h"
#include "ios/chrome/test/block_cleanup_test.h"
#include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_provider.h"
#include "ios/public/provider/chrome/browser/distribution/app_distribution_provider.h"
#include "ios/public/provider/chrome/browser/test_chrome_browser_provider.h"
......@@ -40,7 +44,6 @@
#import "ios/testing/ocmock_complex_type_helper.h"
#include "ios/web/net/request_tracker_impl.h"
#include "ios/web/public/test/test_web_thread_bundle.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
......@@ -111,7 +114,7 @@ class FakeChromeBrowserProvider : public ios::TestChromeBrowserProvider {
} // namespace
class AppStateTest : public PlatformTest {
class AppStateTest : public BlockCleanupTest {
protected:
AppStateTest() {
browser_launcher_mock_ =
......@@ -125,6 +128,17 @@ class AppStateTest : public PlatformTest {
[OCMockObject mockForProtocol:@protocol(BrowserViewInformation)];
}
void SetUp() override {
BlockCleanupTest::SetUp();
TestChromeBrowserState::Builder test_cbs_builder;
if (experimental_flags::IsSuggestionsUIEnabled()) {
test_cbs_builder.AddTestingFactory(
IOSChromeContentSuggestionsServiceFactory::GetInstance(),
IOSChromeContentSuggestionsServiceFactory::GetDefaultFactory());
}
browser_state_ = test_cbs_builder.Build();
}
void initializeIncognitoBlocker(UIWindow* window) {
id application = [OCMockObject niceMockForClass:[UIApplication class]];
id memoryHelper = [OCMockObject mockForClass:[MemoryWarningHelper class]];
......@@ -155,9 +169,9 @@ class AppStateTest : public PlatformTest {
}] currentBrowserState];
}
void stubNullBrowserState(id BVC) {
void stubBrowserState(id BVC) {
[[[BVC stub] andDo:^(NSInvocation* invocation) {
ios::ChromeBrowserState* browserState = nullptr;
ios::ChromeBrowserState* browserState = getBrowserState();
[invocation setReturnValue:&browserState];
}] browserState];
}
......@@ -270,10 +284,12 @@ class AppStateTest : public PlatformTest {
id getApplicationDelegateMock() { return main_application_delegate_; }
id getWindowMock() { return window_; }
id getBrowserViewInformationMock() { return browser_view_information_; }
ios::ChromeBrowserState* getBrowserState() { return browser_state_.get(); }
BOOL metricsMediatorHasBeenCalled() { return metrics_mediator_called_; }
private:
web::TestWebThreadBundle thread_bundle_;
AppState* app_state_;
id browser_launcher_mock_;
id startup_information_mock_;
......@@ -287,6 +303,7 @@ class AppStateTest : public PlatformTest {
std::unique_ptr<ScopedBlockSwizzler> handle_startup_swizzler_;
std::unique_ptr<ScopedBlockSwizzler> metrics_mediator_swizzler_;
__block BOOL metrics_mediator_called_;
std::unique_ptr<TestChromeBrowserState> browser_state_;
};
// TODO(crbug.com/585700): remove this.
......@@ -585,7 +602,7 @@ TEST_F(AppStateTest, resumeSessionWithStartupParameters) {
[[mainTabModel expect] resetSessionMetrics];
[[[browserViewInformation stub] andReturn:mainTabModel] mainTabModel];
id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]];
stubNullBrowserState(mainBVC);
stubBrowserState(mainBVC);
[[[browserViewInformation stub] andReturn:mainBVC] mainBVC];
// Swizzle Startup Parameters.
......@@ -634,7 +651,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPTabSwitcher) {
[[[tabSwitcher stub] andReturnValue:@YES] openNewTabFromTabSwitcher];
id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]];
stubNullBrowserState(mainBVC);
stubBrowserState(mainBVC);
[[[browserViewInformation stub] andReturn:mainBVC] mainBVC];
UIWindow* window = [[UIWindow alloc] init];
......@@ -677,7 +694,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPNoTabSwitcher) {
[[dispatcher expect] openNewTab:[OCMArg any]];
id currentBVC = [OCMockObject mockForClass:[BrowserViewController class]];
stubNullBrowserState(currentBVC);
stubBrowserState(currentBVC);
[[[currentBVC stub] andReturn:dispatcher] dispatcher];
[[[browserViewInformation stub] andReturn:mainTabModel] mainTabModel];
......@@ -689,7 +706,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPNoTabSwitcher) {
[[[tabSwitcher stub] andReturnValue:@NO] openNewTabFromTabSwitcher];
id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]];
stubNullBrowserState(mainBVC);
stubBrowserState(mainBVC);
[[[browserViewInformation stub] andReturn:mainBVC] mainBVC];
UIWindow* window = [[UIWindow alloc] init];
......@@ -727,7 +744,7 @@ TEST_F(AppStateTest, applicationWillEnterForeground) {
browserViewInformation];
[[[browserViewInformation stub] andReturn:tabModel] currentTabModel];
id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]];
stubNullBrowserState(mainBVC);
stubBrowserState(mainBVC);
[[[browserViewInformation stub] andReturn:mainBVC] mainBVC];
[[metricsMediator expect] updateMetricsStateBasedOnPrefsUserTriggered:NO];
[[memoryHelper expect] resetForegroundMemoryWarningCount];
......@@ -737,7 +754,7 @@ TEST_F(AppStateTest, applicationWillEnterForeground) {
id contentSuggestionsNotifier =
OCMClassMock([ContentSuggestionsSchedulerNotifications class]);
OCMExpect([contentSuggestionsNotifier notifyForeground:nullptr]);
OCMExpect([contentSuggestionsNotifier notifyForeground:getBrowserState()]);
stubNullCurrentBrowserState(browserViewInformation);
......
......@@ -32,6 +32,12 @@ IOSChromeContentSuggestionsServiceFactory::GetForBrowserState(
GetInstance()->GetServiceForBrowserState(browser_state, true));
}
// static
BrowserStateKeyedServiceFactory::TestingFactoryFunction
IOSChromeContentSuggestionsServiceFactory::GetDefaultFactory() {
return &ntp_snippets::CreateChromeContentSuggestionsServiceWithProviders;
}
IOSChromeContentSuggestionsServiceFactory::
IOSChromeContentSuggestionsServiceFactory()
: BrowserStateKeyedServiceFactory(
......
......@@ -31,6 +31,10 @@ class IOSChromeContentSuggestionsServiceFactory
static IOSChromeContentSuggestionsServiceFactory* GetInstance();
static ntp_snippets::ContentSuggestionsService* GetForBrowserState(
ios::ChromeBrowserState* browser_state);
// Returns the default factory used to build ContentSuggestionsService. Can be
// registered with SetTestingFactory to use the ContentSuggestionsService
// instance during testing.
static TestingFactoryFunction GetDefaultFactory();
private:
friend struct base::DefaultSingletonTraits<
......
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