OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import <EarlGrey/EarlGrey.h> |
| 6 #import <XCTest/XCTest.h> |
| 7 |
| 8 #include "base/logging.h" |
| 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/mac/scoped_nsobject.h" |
| 11 #import "ios/chrome/app/chrome_overlay_window.h" |
| 12 #import "ios/chrome/app/safe_mode/safe_mode_view_controller.h" |
| 13 #import "ios/chrome/browser/ui/main/main_view_controller.h" |
| 14 #include "ios/chrome/grit/ios_chromium_strings.h" |
| 15 #import "ios/chrome/test/base/scoped_block_swizzler.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Returns the top view controller for rendering the Safe Mode Controller. |
| 21 UIViewController* GetActiveViewController() { |
| 22 UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow]; |
| 23 DCHECK([mainWindow isKindOfClass:[ChromeOverlayWindow class]]); |
| 24 MainViewController* main_view_controller = |
| 25 base::mac::ObjCCast<MainViewController>([mainWindow rootViewController]); |
| 26 return main_view_controller.activeViewController; |
| 27 } |
| 28 |
| 29 // Verifies that |message| is displayed. |
| 30 void AssertMessageOnPage(NSString* message) { |
| 31 id<GREYMatcher> messageMatcher = [GREYMatchers matcherForText:message]; |
| 32 [[EarlGrey selectElementWithMatcher:messageMatcher] |
| 33 assertWithMatcher:grey_notNil()]; |
| 34 } |
| 35 |
| 36 // Verifies that |message| is not displayed. |
| 37 void AssertMessageNotOnPage(NSString* message) { |
| 38 id<GREYMatcher> messageMatcher = [GREYMatchers matcherForText:message]; |
| 39 [[EarlGrey selectElementWithMatcher:messageMatcher] |
| 40 assertWithMatcher:grey_nil()]; |
| 41 } |
| 42 |
| 43 // Verifies that the button to reload chrome is displayed. |
| 44 void AssertTryAgainButtonOnPage() { |
| 45 NSString* tryAgain = |
| 46 NSLocalizedString(@"IDS_IOS_SAFE_MODE_RELOAD_CHROME", @""); |
| 47 // This is uppercased to match MDC button label convention. |
| 48 NSString* tryAgainPrimaryAction = |
| 49 [tryAgain uppercaseStringWithLocale:[NSLocale currentLocale]]; |
| 50 id<GREYMatcher> tryAgainMatcher = |
| 51 [GREYMatchers matcherForButtonTitle:tryAgainPrimaryAction]; |
| 52 [[EarlGrey selectElementWithMatcher:tryAgainMatcher] |
| 53 assertWithMatcher:grey_notNil()]; |
| 54 } |
| 55 |
| 56 } // namespace |
| 57 |
| 58 // Tests the display of Safe Mode Controller under different error states of |
| 59 // jailbroken-ness and whether a crash dump was saved. |
| 60 @interface SafeModeTestCase : ChromeTestCase |
| 61 @end |
| 62 |
| 63 @implementation SafeModeTestCase |
| 64 |
| 65 // Tests that Safe Mode crash upload screen is displayed when there are crash |
| 66 // reports to upload. |
| 67 - (void)testSafeModeSendingCrashReport { |
| 68 // Mocks the +hasReportToUpload method by swizzling to return positively that |
| 69 // there are crash reports to upload. |
| 70 ScopedBlockSwizzler hasReport([SafeModeViewController class], |
| 71 @selector(hasReportToUpload), ^{ |
| 72 return YES; |
| 73 }); |
| 74 |
| 75 // Instantiates a Safe Mode controller and displays it. |
| 76 base::scoped_nsobject<SafeModeViewController> safeModeController( |
| 77 [[SafeModeViewController alloc] initWithDelegate:nil]); |
| 78 [GetActiveViewController() presentViewController:safeModeController |
| 79 animated:NO |
| 80 completion:nil]; |
| 81 // Verifies screen content that shows that crash report is being uploaded. |
| 82 AssertMessageOnPage(NSLocalizedString(@"IDS_IOS_SAFE_MODE_AW_SNAP", @"")); |
| 83 AssertMessageOnPage( |
| 84 NSLocalizedString(@"IDS_IOS_SAFE_MODE_UNKNOWN_CAUSE", @"")); |
| 85 AssertTryAgainButtonOnPage(); |
| 86 AssertMessageOnPage( |
| 87 NSLocalizedString(@"IDS_IOS_SAFE_MODE_SENDING_CRASH_REPORT", @"")); |
| 88 } |
| 89 |
| 90 // Tests that Safe Mode screen is displayed with a message that there are |
| 91 // jailbroken mods that caused a crash. Crash reports are not sent. |
| 92 - (void)testSafeModeDetectedThirdPartyMods { |
| 93 // Mocks the +detectedThirdPartyMods method by swizzling to return positively |
| 94 // that device appears to be jailbroken and contains third party mods. |
| 95 ScopedBlockSwizzler thirdParty([SafeModeViewController class], |
| 96 @selector(detectedThirdPartyMods), ^{ |
| 97 return YES; |
| 98 }); |
| 99 // Returns an empty list to simulate no known mods detected. |
| 100 ScopedBlockSwizzler badModules([SafeModeViewController class], |
| 101 @selector(startupCrashModules), ^{ |
| 102 return @[]; |
| 103 }); |
| 104 |
| 105 // Instantiates a Safe Mode controller and displays it. |
| 106 base::scoped_nsobject<SafeModeViewController> safeModeController( |
| 107 [[SafeModeViewController alloc] initWithDelegate:nil]); |
| 108 [GetActiveViewController() presentViewController:safeModeController |
| 109 animated:NO |
| 110 completion:nil]; |
| 111 // Verifies screen content that does not show crash report being uploaded. |
| 112 // When devices are jailbroken, the crash reports are not very useful. |
| 113 AssertMessageOnPage(NSLocalizedString(@"IDS_IOS_SAFE_MODE_AW_SNAP", @"")); |
| 114 AssertMessageOnPage( |
| 115 NSLocalizedString(@"IDS_IOS_SAFE_MODE_TWEAKS_FOUND", @"")); |
| 116 AssertTryAgainButtonOnPage(); |
| 117 AssertMessageNotOnPage( |
| 118 NSLocalizedString(@"IDS_IOS_SAFE_MODE_SENDING_CRASH_REPORT", @"")); |
| 119 } |
| 120 |
| 121 // Tests that Safe Mode screen is displayed with a message that there are |
| 122 // jailbroken mods listing the names of the known to be bad mods that caused a |
| 123 // crash. Crash reports are not sent. |
| 124 - (void)testSafeModeBothThirdPartyModsAndHasReport { |
| 125 // Mocks the +detectedThirdPartyMods method by swizzling to return positively |
| 126 // that device appears to be jailbroken and contains third party mods. |
| 127 ScopedBlockSwizzler thirdParty([SafeModeViewController class], |
| 128 @selector(detectedThirdPartyMods), ^{ |
| 129 return YES; |
| 130 }); |
| 131 // Mocked list of bad jailbroken mods. These will be checked later. |
| 132 NSArray* badModulesList = @[ @"iAmBad", @"MJackson" ]; |
| 133 ScopedBlockSwizzler badModules([SafeModeViewController class], |
| 134 @selector(startupCrashModules), ^{ |
| 135 return badModulesList; |
| 136 }); |
| 137 ScopedBlockSwizzler hasReport([SafeModeViewController class], |
| 138 @selector(hasReportToUpload), ^{ |
| 139 return YES; |
| 140 }); |
| 141 // Instantiates a Safe Mode controller and displays it. |
| 142 base::scoped_nsobject<SafeModeViewController> safeModeController( |
| 143 [[SafeModeViewController alloc] initWithDelegate:nil]); |
| 144 [GetActiveViewController() presentViewController:safeModeController |
| 145 animated:NO |
| 146 completion:nil]; |
| 147 // Verifies screen content that does not show crash report being uploaded. |
| 148 // When devices are jailbroken, the crash reports are not very useful. |
| 149 AssertMessageOnPage(NSLocalizedString(@"IDS_IOS_SAFE_MODE_AW_SNAP", @"")); |
| 150 // Constructs the list of bad mods based on |badModulesList| above. |
| 151 NSString* message = |
| 152 [NSLocalizedString(@"IDS_IOS_SAFE_MODE_NAMED_TWEAKS_FOUND", @"") |
| 153 stringByAppendingString:@"\n\n iAmBad\n MJackson"]; |
| 154 AssertMessageOnPage(message); |
| 155 AssertTryAgainButtonOnPage(); |
| 156 AssertMessageNotOnPage( |
| 157 NSLocalizedString(@"IDS_IOS_SAFE_MODE_SENDING_CRASH_REPORT", @"")); |
| 158 } |
| 159 |
| 160 @end |
OLD | NEW |