Commit 728b90dd authored by Olivier Robin's avatar Olivier Robin Committed by Commit Bot

Use ScopedKeyWindow in AppStateTest

Change-Id: I98d66c0f0a0a21fe6aa99190c0248faad10dae35
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2403468
Commit-Queue: Olivier Robin <olivierrobin@chromium.org>
Commit-Queue: Mark Cogan <marq@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805776}
parent 5d095104
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
#import "ios/chrome/browser/ui/util/multi_window_support.h" #import "ios/chrome/browser/ui/util/multi_window_support.h"
#include "ios/chrome/test/block_cleanup_test.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"
#import "ios/chrome/test/scoped_key_window.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"
#include "ios/public/provider/chrome/browser/user_feedback/test_user_feedback_provider.h" #include "ios/public/provider/chrome/browser/user_feedback/test_user_feedback_provider.h"
...@@ -625,10 +626,11 @@ TEST_F(AppStateTest, resumeSessionWithStartupParameters) { ...@@ -625,10 +626,11 @@ TEST_F(AppStateTest, resumeSessionWithStartupParameters) {
// Swizzle Startup Parameters. // Swizzle Startup Parameters.
swizzleHandleStartupParameters(tabOpener, getBrowserState()); swizzleHandleStartupParameters(tabOpener, getBrowserState());
UIWindow* window = [[UIWindow alloc] init]; ScopedKeyWindow scopedKeyWindow;
AppState* appState = getAppStateWithOpenNTPAndIncognitoBlock(NO, window); AppState* appState =
getAppStateWithOpenNTPAndIncognitoBlock(NO, scopedKeyWindow.Get());
ASSERT_EQ(NSUInteger(1), [window subviews].count); ASSERT_EQ(NSUInteger(1), [scopedKeyWindow.Get() subviews].count);
// Action. // Action.
[appState resumeSessionWithTabOpener:tabOpener [appState resumeSessionWithTabOpener:tabOpener
...@@ -636,7 +638,7 @@ TEST_F(AppStateTest, resumeSessionWithStartupParameters) { ...@@ -636,7 +638,7 @@ TEST_F(AppStateTest, resumeSessionWithStartupParameters) {
connectionInformation:getConnectionInformationMock()]; connectionInformation:getConnectionInformationMock()];
// Test. // Test.
EXPECT_EQ(NSUInteger(0), [window subviews].count); EXPECT_EQ(NSUInteger(0), [scopedKeyWindow.Get() subviews].count);
EXPECT_EQ(1, getProfileSessionDurationsService()->session_started_count()); EXPECT_EQ(1, getProfileSessionDurationsService()->session_started_count());
EXPECT_EQ(0, getProfileSessionDurationsService()->session_ended_count()); EXPECT_EQ(0, getProfileSessionDurationsService()->session_ended_count());
} }
...@@ -678,11 +680,11 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPTabSwitcher) { ...@@ -678,11 +680,11 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPTabSwitcher) {
id tabSwitcher = [OCMockObject mockForProtocol:@protocol(TabSwitching)]; id tabSwitcher = [OCMockObject mockForProtocol:@protocol(TabSwitching)];
[[[tabSwitcher stub] andReturnValue:@YES] openNewTabFromTabSwitcher]; [[[tabSwitcher stub] andReturnValue:@YES] openNewTabFromTabSwitcher];
ScopedKeyWindow scopedKeyWindow;
AppState* appState =
getAppStateWithOpenNTPAndIncognitoBlock(YES, scopedKeyWindow.Get());
UIWindow* window = [[UIWindow alloc] init]; ASSERT_EQ(NSUInteger(1), [scopedKeyWindow.Get() subviews].count);
AppState* appState = getAppStateWithOpenNTPAndIncognitoBlock(YES, window);
ASSERT_EQ(NSUInteger(1), [window subviews].count);
// Action. // Action.
[appState resumeSessionWithTabOpener:tabOpener [appState resumeSessionWithTabOpener:tabOpener
...@@ -690,7 +692,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPTabSwitcher) { ...@@ -690,7 +692,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPTabSwitcher) {
connectionInformation:getConnectionInformationMock()]; connectionInformation:getConnectionInformationMock()];
// Test. // Test.
EXPECT_EQ(NSUInteger(0), [window subviews].count); EXPECT_EQ(NSUInteger(0), [scopedKeyWindow.Get() subviews].count);
} }
// Test that -resumeSessionWithTabOpener removes incognito blocker, // Test that -resumeSessionWithTabOpener removes incognito blocker,
...@@ -743,11 +745,12 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPNoTabSwitcher) { ...@@ -743,11 +745,12 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPNoTabSwitcher) {
id tabSwitcher = [OCMockObject mockForProtocol:@protocol(TabSwitching)]; id tabSwitcher = [OCMockObject mockForProtocol:@protocol(TabSwitching)];
[[[tabSwitcher stub] andReturnValue:@NO] openNewTabFromTabSwitcher]; [[[tabSwitcher stub] andReturnValue:@NO] openNewTabFromTabSwitcher];
UIWindow* window = [[UIWindow alloc] init]; ScopedKeyWindow scopedKeyWindow;
AppState* appState = getAppStateWithOpenNTPAndIncognitoBlock(YES, window); AppState* appState =
getAppStateWithOpenNTPAndIncognitoBlock(YES, scopedKeyWindow.Get());
// incognitoBlocker. // incognitoBlocker.
ASSERT_EQ(NSUInteger(1), [window subviews].count); ASSERT_EQ(NSUInteger(1), [scopedKeyWindow.Get() subviews].count);
// Action. // Action.
[appState resumeSessionWithTabOpener:tabOpener [appState resumeSessionWithTabOpener:tabOpener
...@@ -755,7 +758,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPNoTabSwitcher) { ...@@ -755,7 +758,7 @@ TEST_F(AppStateTest, resumeSessionShouldOpenNTPNoTabSwitcher) {
connectionInformation:getConnectionInformationMock()]; connectionInformation:getConnectionInformationMock()];
// Test. // Test.
EXPECT_EQ(NSUInteger(0), [window subviews].count); EXPECT_EQ(NSUInteger(0), [scopedKeyWindow.Get() subviews].count);
} }
// Tests that -applicationWillEnterForeground resets components as needed. // Tests that -applicationWillEnterForeground resets components as needed.
...@@ -887,7 +890,7 @@ TEST_F(AppStateTest, ...@@ -887,7 +890,7 @@ TEST_F(AppStateTest,
// Tests that -applicationDidEnterBackground creates an incognito blocker. // Tests that -applicationDidEnterBackground creates an incognito blocker.
TEST_F(AppStateTest, applicationDidEnterBackgroundIncognito) { TEST_F(AppStateTest, applicationDidEnterBackgroundIncognito) {
// Setup. // Setup.
UIWindow* window = [[UIWindow alloc] init]; ScopedKeyWindow scopedKeyWindow;
id application = [OCMockObject niceMockForClass:[UIApplication class]]; id application = [OCMockObject niceMockForClass:[UIApplication class]];
id memoryHelper = [OCMockObject mockForClass:[MemoryWarningHelper class]]; id memoryHelper = [OCMockObject mockForClass:[MemoryWarningHelper class]];
StubBrowserInterfaceProvider* interfaceProvider = getInterfaceProvider(); StubBrowserInterfaceProvider* interfaceProvider = getInterfaceProvider();
...@@ -897,7 +900,7 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundIncognito) { ...@@ -897,7 +900,7 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundIncognito) {
id browserLauncher = getBrowserLauncherMock(); id browserLauncher = getBrowserLauncherMock();
BrowserInitializationStageType stage = INITIALIZATION_STAGE_FOREGROUND; BrowserInitializationStageType stage = INITIALIZATION_STAGE_FOREGROUND;
AppState* appState = getAppStateWithRealWindow(window); AppState* appState = getAppStateWithRealWindow(scopedKeyWindow.Get());
[[startupInformation expect] expireFirstUserActionRecorder]; [[startupInformation expect] expireFirstUserActionRecorder];
[[[memoryHelper stub] andReturnValue:@0] foregroundMemoryWarningCount]; [[[memoryHelper stub] andReturnValue:@0] foregroundMemoryWarningCount];
...@@ -907,7 +910,7 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundIncognito) { ...@@ -907,7 +910,7 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundIncognito) {
swizzleMetricsMediatorDisableReporting(); swizzleMetricsMediatorDisableReporting();
ASSERT_EQ(NSUInteger(0), [window subviews].count); ASSERT_EQ(NSUInteger(0), [scopedKeyWindow.Get() subviews].count);
// Action. // Action.
[appState applicationDidEnterBackground:application [appState applicationDidEnterBackground:application
...@@ -917,14 +920,14 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundIncognito) { ...@@ -917,14 +920,14 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundIncognito) {
// Tests. // Tests.
EXPECT_OCMOCK_VERIFY(startupInformation); EXPECT_OCMOCK_VERIFY(startupInformation);
EXPECT_TRUE(metricsMediatorHasBeenCalled()); EXPECT_TRUE(metricsMediatorHasBeenCalled());
EXPECT_EQ(NSUInteger(1), [window subviews].count); EXPECT_EQ(NSUInteger(1), [scopedKeyWindow.Get() subviews].count);
} }
// Tests that -applicationDidEnterBackground do nothing if the application has // Tests that -applicationDidEnterBackground do nothing if the application has
// never been in a Foreground stage. // never been in a Foreground stage.
TEST_F(AppStateTest, applicationDidEnterBackgroundStageBackground) { TEST_F(AppStateTest, applicationDidEnterBackgroundStageBackground) {
// Setup. // Setup.
UIWindow* window = [[UIWindow alloc] init]; ScopedKeyWindow scopedKeyWindow;
id application = [OCMockObject mockForClass:[UIApplication class]]; id application = [OCMockObject mockForClass:[UIApplication class]];
id memoryHelper = [OCMockObject mockForClass:[MemoryWarningHelper class]]; id memoryHelper = [OCMockObject mockForClass:[MemoryWarningHelper class]];
id browserLauncher = getBrowserLauncherMock(); id browserLauncher = getBrowserLauncherMock();
...@@ -933,22 +936,23 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundStageBackground) { ...@@ -933,22 +936,23 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundStageBackground) {
[[[browserLauncher stub] andReturnValue:@(stage)] browserInitializationStage]; [[[browserLauncher stub] andReturnValue:@(stage)] browserInitializationStage];
[[[browserLauncher stub] andReturn:nil] interfaceProvider]; [[[browserLauncher stub] andReturn:nil] interfaceProvider];
ASSERT_EQ(NSUInteger(0), [window subviews].count); ASSERT_EQ(NSUInteger(0), [scopedKeyWindow.Get() subviews].count);
// Action. // Action.
[getAppStateWithRealWindow(window) applicationDidEnterBackground:application [getAppStateWithRealWindow(scopedKeyWindow.Get())
memoryHelper:memoryHelper applicationDidEnterBackground:application
incognitoContentVisible:YES]; memoryHelper:memoryHelper
incognitoContentVisible:YES];
// Tests. // Tests.
EXPECT_EQ(NSUInteger(0), [window subviews].count); EXPECT_EQ(NSUInteger(0), [scopedKeyWindow.Get() subviews].count);
} }
// Tests that -applicationDidEnterBackground does not create an incognito // Tests that -applicationDidEnterBackground does not create an incognito
// blocker if there is no incognito tab. // blocker if there is no incognito tab.
TEST_F(AppStateTest, applicationDidEnterBackgroundNoIncognitoBlocker) { TEST_F(AppStateTest, applicationDidEnterBackgroundNoIncognitoBlocker) {
// Setup. // Setup.
UIWindow* window = [[UIWindow alloc] init]; ScopedKeyWindow scopedKeyWindow;
id application = [OCMockObject niceMockForClass:[UIApplication class]]; id application = [OCMockObject niceMockForClass:[UIApplication class]];
id memoryHelper = [OCMockObject mockForClass:[MemoryWarningHelper class]]; id memoryHelper = [OCMockObject mockForClass:[MemoryWarningHelper class]];
StubBrowserInterfaceProvider* interfaceProvider = getInterfaceProvider(); StubBrowserInterfaceProvider* interfaceProvider = getInterfaceProvider();
...@@ -958,7 +962,7 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundNoIncognitoBlocker) { ...@@ -958,7 +962,7 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundNoIncognitoBlocker) {
id browserLauncher = getBrowserLauncherMock(); id browserLauncher = getBrowserLauncherMock();
BrowserInitializationStageType stage = INITIALIZATION_STAGE_FOREGROUND; BrowserInitializationStageType stage = INITIALIZATION_STAGE_FOREGROUND;
AppState* appState = getAppStateWithRealWindow(window); AppState* appState = getAppStateWithRealWindow(scopedKeyWindow.Get());
[[startupInformation expect] expireFirstUserActionRecorder]; [[startupInformation expect] expireFirstUserActionRecorder];
[[[memoryHelper stub] andReturnValue:@0] foregroundMemoryWarningCount]; [[[memoryHelper stub] andReturnValue:@0] foregroundMemoryWarningCount];
...@@ -968,7 +972,7 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundNoIncognitoBlocker) { ...@@ -968,7 +972,7 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundNoIncognitoBlocker) {
swizzleMetricsMediatorDisableReporting(); swizzleMetricsMediatorDisableReporting();
ASSERT_EQ(NSUInteger(0), [window subviews].count); ASSERT_EQ(NSUInteger(0), [scopedKeyWindow.Get() subviews].count);
// Action. // Action.
[appState applicationDidEnterBackground:application [appState applicationDidEnterBackground:application
...@@ -978,5 +982,5 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundNoIncognitoBlocker) { ...@@ -978,5 +982,5 @@ TEST_F(AppStateTest, applicationDidEnterBackgroundNoIncognitoBlocker) {
// Tests. // Tests.
EXPECT_OCMOCK_VERIFY(startupInformation); EXPECT_OCMOCK_VERIFY(startupInformation);
EXPECT_TRUE(metricsMediatorHasBeenCalled()); EXPECT_TRUE(metricsMediatorHasBeenCalled());
EXPECT_EQ(NSUInteger(0), [window subviews].count); EXPECT_EQ(NSUInteger(0), [scopedKeyWindow.Get() subviews].count);
} }
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