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") { ...@@ -46,6 +46,7 @@ source_set("unit_tests") {
"//ios/chrome/browser/device_sharing", "//ios/chrome/browser/device_sharing",
"//ios/chrome/browser/geolocation", "//ios/chrome/browser/geolocation",
"//ios/chrome/browser/metrics", "//ios/chrome/browser/metrics",
"//ios/chrome/browser/ntp_snippets",
"//ios/chrome/browser/tabs", "//ios/chrome/browser/tabs",
"//ios/chrome/browser/u2f", "//ios/chrome/browser/u2f",
"//ios/chrome/browser/ui:ui_internal", "//ios/chrome/browser/ui:ui_internal",
......
...@@ -23,9 +23,12 @@ ...@@ -23,9 +23,12 @@
#import "ios/chrome/app/main_application_delegate.h" #import "ios/chrome/app/main_application_delegate.h"
#import "ios/chrome/app/startup/content_suggestions_scheduler_notifications.h" #import "ios/chrome/app/startup/content_suggestions_scheduler_notifications.h"
#import "ios/chrome/browser/app_startup_parameters.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" #include "ios/chrome/browser/chrome_url_constants.h"
#import "ios/chrome/browser/device_sharing/device_sharing_manager.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" #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/tabs/tab_model.h"
#import "ios/chrome/browser/ui/browser_view_controller.h" #import "ios/chrome/browser/ui/browser_view_controller.h"
#import "ios/chrome/browser/ui/commands/browser_commands.h" #import "ios/chrome/browser/ui/commands/browser_commands.h"
...@@ -33,6 +36,7 @@ ...@@ -33,6 +36,7 @@
#import "ios/chrome/browser/ui/safe_mode/safe_mode_coordinator.h" #import "ios/chrome/browser/ui/safe_mode/safe_mode_coordinator.h"
#import "ios/chrome/browser/ui/settings/settings_navigation_controller.h" #import "ios/chrome/browser/ui/settings/settings_navigation_controller.h"
#import "ios/chrome/test/base/scoped_block_swizzler.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/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/distribution/app_distribution_provider.h"
#include "ios/public/provider/chrome/browser/test_chrome_browser_provider.h" #include "ios/public/provider/chrome/browser/test_chrome_browser_provider.h"
...@@ -40,7 +44,6 @@ ...@@ -40,7 +44,6 @@
#import "ios/testing/ocmock_complex_type_helper.h" #import "ios/testing/ocmock_complex_type_helper.h"
#include "ios/web/net/request_tracker_impl.h" #include "ios/web/net/request_tracker_impl.h"
#include "ios/web/public/test/test_web_thread_bundle.h" #include "ios/web/public/test/test_web_thread_bundle.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h" #import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h" #include "third_party/ocmock/gtest_support.h"
...@@ -111,7 +114,7 @@ class FakeChromeBrowserProvider : public ios::TestChromeBrowserProvider { ...@@ -111,7 +114,7 @@ class FakeChromeBrowserProvider : public ios::TestChromeBrowserProvider {
} // namespace } // namespace
class AppStateTest : public PlatformTest { class AppStateTest : public BlockCleanupTest {
protected: protected:
AppStateTest() { AppStateTest() {
browser_launcher_mock_ = browser_launcher_mock_ =
...@@ -125,6 +128,17 @@ class AppStateTest : public PlatformTest { ...@@ -125,6 +128,17 @@ class AppStateTest : public PlatformTest {
[OCMockObject mockForProtocol:@protocol(BrowserViewInformation)]; [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) { void initializeIncognitoBlocker(UIWindow* window) {
id application = [OCMockObject niceMockForClass:[UIApplication class]]; id application = [OCMockObject niceMockForClass:[UIApplication class]];
id memoryHelper = [OCMockObject mockForClass:[MemoryWarningHelper class]]; id memoryHelper = [OCMockObject mockForClass:[MemoryWarningHelper class]];
...@@ -155,9 +169,9 @@ class AppStateTest : public PlatformTest { ...@@ -155,9 +169,9 @@ class AppStateTest : public PlatformTest {
}] currentBrowserState]; }] currentBrowserState];
} }
void stubNullBrowserState(id BVC) { void stubBrowserState(id BVC) {
[[[BVC stub] andDo:^(NSInvocation* invocation) { [[[BVC stub] andDo:^(NSInvocation* invocation) {
ios::ChromeBrowserState* browserState = nullptr; ios::ChromeBrowserState* browserState = getBrowserState();
[invocation setReturnValue:&browserState]; [invocation setReturnValue:&browserState];
}] browserState]; }] browserState];
} }
...@@ -270,10 +284,12 @@ class AppStateTest : public PlatformTest { ...@@ -270,10 +284,12 @@ class AppStateTest : public PlatformTest {
id getApplicationDelegateMock() { return main_application_delegate_; } id getApplicationDelegateMock() { return main_application_delegate_; }
id getWindowMock() { return window_; } id getWindowMock() { return window_; }
id getBrowserViewInformationMock() { return browser_view_information_; } id getBrowserViewInformationMock() { return browser_view_information_; }
ios::ChromeBrowserState* getBrowserState() { return browser_state_.get(); }
BOOL metricsMediatorHasBeenCalled() { return metrics_mediator_called_; } BOOL metricsMediatorHasBeenCalled() { return metrics_mediator_called_; }
private: private:
web::TestWebThreadBundle thread_bundle_;
AppState* app_state_; AppState* app_state_;
id browser_launcher_mock_; id browser_launcher_mock_;
id startup_information_mock_; id startup_information_mock_;
...@@ -287,6 +303,7 @@ class AppStateTest : public PlatformTest { ...@@ -287,6 +303,7 @@ class AppStateTest : public PlatformTest {
std::unique_ptr<ScopedBlockSwizzler> handle_startup_swizzler_; std::unique_ptr<ScopedBlockSwizzler> handle_startup_swizzler_;
std::unique_ptr<ScopedBlockSwizzler> metrics_mediator_swizzler_; std::unique_ptr<ScopedBlockSwizzler> metrics_mediator_swizzler_;
__block BOOL metrics_mediator_called_; __block BOOL metrics_mediator_called_;
std::unique_ptr<TestChromeBrowserState> browser_state_;
}; };
// TODO(crbug.com/585700): remove this. // TODO(crbug.com/585700): remove this.
...@@ -585,7 +602,7 @@ TEST_F(AppStateTest, resumeSessionWithStartupParameters) { ...@@ -585,7 +602,7 @@ TEST_F(AppStateTest, resumeSessionWithStartupParameters) {
[[mainTabModel expect] resetSessionMetrics]; [[mainTabModel expect] resetSessionMetrics];
[[[browserViewInformation stub] andReturn:mainTabModel] mainTabModel]; [[[browserViewInformation stub] andReturn:mainTabModel] mainTabModel];
id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]]; id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]];
stubNullBrowserState(mainBVC); stubBrowserState(mainBVC);
[[[browserViewInformation stub] andReturn:mainBVC] mainBVC]; [[[browserViewInformation stub] andReturn:mainBVC] mainBVC];
// Swizzle Startup Parameters. // Swizzle Startup Parameters.
...@@ -634,7 +651,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPTabSwitcher) { ...@@ -634,7 +651,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPTabSwitcher) {
[[[tabSwitcher stub] andReturnValue:@YES] openNewTabFromTabSwitcher]; [[[tabSwitcher stub] andReturnValue:@YES] openNewTabFromTabSwitcher];
id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]]; id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]];
stubNullBrowserState(mainBVC); stubBrowserState(mainBVC);
[[[browserViewInformation stub] andReturn:mainBVC] mainBVC]; [[[browserViewInformation stub] andReturn:mainBVC] mainBVC];
UIWindow* window = [[UIWindow alloc] init]; UIWindow* window = [[UIWindow alloc] init];
...@@ -677,7 +694,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPNoTabSwitcher) { ...@@ -677,7 +694,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPNoTabSwitcher) {
[[dispatcher expect] openNewTab:[OCMArg any]]; [[dispatcher expect] openNewTab:[OCMArg any]];
id currentBVC = [OCMockObject mockForClass:[BrowserViewController class]]; id currentBVC = [OCMockObject mockForClass:[BrowserViewController class]];
stubNullBrowserState(currentBVC); stubBrowserState(currentBVC);
[[[currentBVC stub] andReturn:dispatcher] dispatcher]; [[[currentBVC stub] andReturn:dispatcher] dispatcher];
[[[browserViewInformation stub] andReturn:mainTabModel] mainTabModel]; [[[browserViewInformation stub] andReturn:mainTabModel] mainTabModel];
...@@ -689,7 +706,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPNoTabSwitcher) { ...@@ -689,7 +706,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPNoTabSwitcher) {
[[[tabSwitcher stub] andReturnValue:@NO] openNewTabFromTabSwitcher]; [[[tabSwitcher stub] andReturnValue:@NO] openNewTabFromTabSwitcher];
id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]]; id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]];
stubNullBrowserState(mainBVC); stubBrowserState(mainBVC);
[[[browserViewInformation stub] andReturn:mainBVC] mainBVC]; [[[browserViewInformation stub] andReturn:mainBVC] mainBVC];
UIWindow* window = [[UIWindow alloc] init]; UIWindow* window = [[UIWindow alloc] init];
...@@ -727,7 +744,7 @@ TEST_F(AppStateTest, applicationWillEnterForeground) { ...@@ -727,7 +744,7 @@ TEST_F(AppStateTest, applicationWillEnterForeground) {
browserViewInformation]; browserViewInformation];
[[[browserViewInformation stub] andReturn:tabModel] currentTabModel]; [[[browserViewInformation stub] andReturn:tabModel] currentTabModel];
id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]]; id mainBVC = [OCMockObject mockForClass:[BrowserViewController class]];
stubNullBrowserState(mainBVC); stubBrowserState(mainBVC);
[[[browserViewInformation stub] andReturn:mainBVC] mainBVC]; [[[browserViewInformation stub] andReturn:mainBVC] mainBVC];
[[metricsMediator expect] updateMetricsStateBasedOnPrefsUserTriggered:NO]; [[metricsMediator expect] updateMetricsStateBasedOnPrefsUserTriggered:NO];
[[memoryHelper expect] resetForegroundMemoryWarningCount]; [[memoryHelper expect] resetForegroundMemoryWarningCount];
...@@ -737,7 +754,7 @@ TEST_F(AppStateTest, applicationWillEnterForeground) { ...@@ -737,7 +754,7 @@ TEST_F(AppStateTest, applicationWillEnterForeground) {
id contentSuggestionsNotifier = id contentSuggestionsNotifier =
OCMClassMock([ContentSuggestionsSchedulerNotifications class]); OCMClassMock([ContentSuggestionsSchedulerNotifications class]);
OCMExpect([contentSuggestionsNotifier notifyForeground:nullptr]); OCMExpect([contentSuggestionsNotifier notifyForeground:getBrowserState()]);
stubNullCurrentBrowserState(browserViewInformation); stubNullCurrentBrowserState(browserViewInformation);
......
...@@ -32,6 +32,12 @@ IOSChromeContentSuggestionsServiceFactory::GetForBrowserState( ...@@ -32,6 +32,12 @@ IOSChromeContentSuggestionsServiceFactory::GetForBrowserState(
GetInstance()->GetServiceForBrowserState(browser_state, true)); GetInstance()->GetServiceForBrowserState(browser_state, true));
} }
// static
BrowserStateKeyedServiceFactory::TestingFactoryFunction
IOSChromeContentSuggestionsServiceFactory::GetDefaultFactory() {
return &ntp_snippets::CreateChromeContentSuggestionsServiceWithProviders;
}
IOSChromeContentSuggestionsServiceFactory:: IOSChromeContentSuggestionsServiceFactory::
IOSChromeContentSuggestionsServiceFactory() IOSChromeContentSuggestionsServiceFactory()
: BrowserStateKeyedServiceFactory( : BrowserStateKeyedServiceFactory(
......
...@@ -31,6 +31,10 @@ class IOSChromeContentSuggestionsServiceFactory ...@@ -31,6 +31,10 @@ class IOSChromeContentSuggestionsServiceFactory
static IOSChromeContentSuggestionsServiceFactory* GetInstance(); static IOSChromeContentSuggestionsServiceFactory* GetInstance();
static ntp_snippets::ContentSuggestionsService* GetForBrowserState( static ntp_snippets::ContentSuggestionsService* GetForBrowserState(
ios::ChromeBrowserState* browser_state); 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: private:
friend struct base::DefaultSingletonTraits< 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