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/mac/scoped_nsobject.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/metrics/metrics_pref_names.h" |
| 11 #include "components/metrics/metrics_reporting_default_state.h" |
| 12 #include "components/prefs/pref_member.h" |
| 13 #include "components/prefs/pref_service.h" |
| 14 #include "components/signin/core/browser/signin_manager.h" |
| 15 #import "ios/chrome/app/main_controller.h" |
| 16 #include "ios/chrome/browser/application_context.h" |
| 17 #import "ios/chrome/browser/geolocation/omnibox_geolocation_controller+Testing.h
" |
| 18 #import "ios/chrome/browser/geolocation/omnibox_geolocation_controller.h" |
| 19 #import "ios/chrome/browser/geolocation/test_location_manager.h" |
| 20 #include "ios/chrome/browser/signin/signin_manager_factory.h" |
| 21 #include "ios/chrome/browser/sync/sync_setup_service.h" |
| 22 #include "ios/chrome/browser/sync/sync_setup_service_factory.h" |
| 23 #import "ios/chrome/browser/ui/first_run/first_run_chrome_signin_view_controller
.h" |
| 24 #include "ios/chrome/browser/ui/first_run/welcome_to_chrome_view_controller.h" |
| 25 #include "ios/chrome/grit/ios_strings.h" |
| 26 #import "ios/chrome/test/app/chrome_test_util.h" |
| 27 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 28 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 29 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity.h" |
| 30 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.
h" |
| 31 #include "ui/base/l10n/l10n_util.h" |
| 32 |
| 33 namespace { |
| 34 |
| 35 // Returns a fake identity. |
| 36 ChromeIdentity* GetFakeIdentity() { |
| 37 return [FakeChromeIdentity identityWithEmail:@"foo@gmail.com" |
| 38 gaiaID:@"fooID" |
| 39 name:@"Fake Foo"]; |
| 40 } |
| 41 |
| 42 // Taps the button with accessibility labelId |message_id|. |
| 43 void TapButtonWithLabelId(int message_id) { |
| 44 id<GREYMatcher> matcher = chrome_test_util::buttonWithAccessibilityLabel( |
| 45 l10n_util::GetNSString(message_id)); |
| 46 [[EarlGrey selectElementWithMatcher:matcher] performAction:grey_tap()]; |
| 47 } |
| 48 |
| 49 // Asserts that |identity| is actually signed in to the active profile. |
| 50 void AssertAuthenticatedIdentityInActiveProfile(ChromeIdentity* identity) { |
| 51 [[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; |
| 52 |
| 53 ios::ChromeBrowserState* browser_state = |
| 54 chrome_test_util::GetOriginalBrowserState(); |
| 55 AccountInfo info = |
| 56 ios::SigninManagerFactory::GetForBrowserState(browser_state) |
| 57 ->GetAuthenticatedAccountInfo(); |
| 58 |
| 59 GREYAssertEqual(base::SysNSStringToUTF8(identity.gaiaID), info.gaia, |
| 60 @"Unexpected Gaia ID of the signed in user [expected = " |
| 61 @"\"%@\", actual = \"%s\"]", |
| 62 identity.gaiaID, info.gaia.c_str()); |
| 63 } |
| 64 } |
| 65 |
| 66 @interface MainController (ExposedForTesting) |
| 67 - (void)showFirstRunUI; |
| 68 @end |
| 69 |
| 70 // Tests first run settings and navigation. |
| 71 @interface FirstRunTestCase : ChromeTestCase |
| 72 @end |
| 73 |
| 74 @implementation FirstRunTestCase |
| 75 |
| 76 - (void)setUp { |
| 77 [super setUp]; |
| 78 |
| 79 BooleanPrefMember metricsEnabledPref; |
| 80 metricsEnabledPref.Init(metrics::prefs::kMetricsReportingEnabled, |
| 81 GetApplicationContext()->GetLocalState()); |
| 82 metricsEnabledPref.SetValue(NO); |
| 83 IntegerPrefMember defaultOptInPref; |
| 84 defaultOptInPref.Init(metrics::prefs::kMetricsDefaultOptIn, |
| 85 GetApplicationContext()->GetLocalState()); |
| 86 defaultOptInPref.SetValue(metrics::EnableMetricsDefault::DEFAULT_UNKNOWN); |
| 87 |
| 88 base::scoped_nsobject<TestLocationManager> locationManager( |
| 89 [[TestLocationManager alloc] init]); |
| 90 [locationManager setLocationServicesEnabled:NO]; |
| 91 [[OmniboxGeolocationController sharedInstance] |
| 92 setLocationManager:locationManager]; |
| 93 } |
| 94 |
| 95 + (void)tearDown { |
| 96 IntegerPrefMember defaultOptInPref; |
| 97 defaultOptInPref.Init(metrics::prefs::kMetricsDefaultOptIn, |
| 98 GetApplicationContext()->GetLocalState()); |
| 99 defaultOptInPref.SetValue(metrics::EnableMetricsDefault::DEFAULT_UNKNOWN); |
| 100 |
| 101 [[OmniboxGeolocationController sharedInstance] setLocationManager:nil]; |
| 102 |
| 103 [super tearDown]; |
| 104 } |
| 105 |
| 106 // Navigates to the terms of service and back. |
| 107 - (void)testTermsAndConditions { |
| 108 [chrome_test_util::GetMainController() showFirstRunUI]; |
| 109 |
| 110 id<GREYMatcher> termsOfServiceLink = |
| 111 grey_accessibilityLabel(@"Terms of Service"); |
| 112 [[EarlGrey selectElementWithMatcher:termsOfServiceLink] |
| 113 performAction:grey_tap()]; |
| 114 |
| 115 [[EarlGrey selectElementWithMatcher:grey_text(@"Chromium Terms of Service")] |
| 116 assertWithMatcher:grey_sufficientlyVisible()]; |
| 117 |
| 118 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"back_bar_button")] |
| 119 performAction:grey_tap()]; |
| 120 |
| 121 // Ensure we went back to the First Run screen. |
| 122 [[EarlGrey selectElementWithMatcher:termsOfServiceLink] |
| 123 assertWithMatcher:grey_sufficientlyVisible()]; |
| 124 } |
| 125 |
| 126 // Toggle the UMA checkbox. |
| 127 - (void)testToggleMetricsOn { |
| 128 [chrome_test_util::GetMainController() showFirstRunUI]; |
| 129 |
| 130 id<GREYMatcher> metrics = |
| 131 grey_accessibilityID(kUMAMetricsButtonAccessibilityIdentifier); |
| 132 [[EarlGrey selectElementWithMatcher:metrics] performAction:grey_tap()]; |
| 133 |
| 134 id<GREYMatcher> optInAccept = chrome_test_util::buttonWithAccessibilityLabel( |
| 135 l10n_util::GetNSString(IDS_IOS_FIRSTRUN_OPT_IN_ACCEPT_BUTTON)); |
| 136 [[EarlGrey selectElementWithMatcher:optInAccept] performAction:grey_tap()]; |
| 137 |
| 138 BOOL metricsOptIn = GetApplicationContext()->GetLocalState()->GetBoolean( |
| 139 metrics::prefs::kMetricsReportingEnabled); |
| 140 GREYAssert( |
| 141 metricsOptIn != [WelcomeToChromeViewController defaultStatsCheckboxValue], |
| 142 @"Metrics reporting pref is incorrect."); |
| 143 } |
| 144 |
| 145 // Dismisses the first run screens. |
| 146 - (void)testDismissFirstRun { |
| 147 [chrome_test_util::GetMainController() showFirstRunUI]; |
| 148 |
| 149 id<GREYMatcher> optInAccept = chrome_test_util::buttonWithAccessibilityLabel( |
| 150 l10n_util::GetNSString(IDS_IOS_FIRSTRUN_OPT_IN_ACCEPT_BUTTON)); |
| 151 [[EarlGrey selectElementWithMatcher:optInAccept] performAction:grey_tap()]; |
| 152 |
| 153 PrefService* preferences = GetApplicationContext()->GetLocalState(); |
| 154 GREYAssert( |
| 155 preferences->GetBoolean(metrics::prefs::kMetricsReportingEnabled) == |
| 156 [WelcomeToChromeViewController defaultStatsCheckboxValue], |
| 157 @"Metrics reporting does not match."); |
| 158 |
| 159 id<GREYMatcher> skipSignIn = |
| 160 grey_accessibilityID(kSignInSkipButtonAccessibilityIdentifier); |
| 161 [[EarlGrey selectElementWithMatcher:skipSignIn] performAction:grey_tap()]; |
| 162 |
| 163 id<GREYMatcher> newTab = |
| 164 grey_kindOfClass(NSClassFromString(@"NewTabPageView")); |
| 165 [[EarlGrey selectElementWithMatcher:newTab] |
| 166 assertWithMatcher:grey_sufficientlyVisible()]; |
| 167 } |
| 168 |
| 169 // Signs in to an account and then taps the Undo button to sign out. |
| 170 - (void)testSignInAndUndo { |
| 171 ChromeIdentity* identity = GetFakeIdentity(); |
| 172 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 173 identity); |
| 174 |
| 175 // Launch First Run and accept tems of services. |
| 176 [chrome_test_util::GetMainController() showFirstRunUI]; |
| 177 TapButtonWithLabelId(IDS_IOS_FIRSTRUN_OPT_IN_ACCEPT_BUTTON); |
| 178 |
| 179 // Sign In |identity|. |
| 180 TapButtonWithLabelId(IDS_IOS_ACCOUNT_CONSISTENCY_SETUP_SIGNIN_BUTTON); |
| 181 AssertAuthenticatedIdentityInActiveProfile(identity); |
| 182 |
| 183 // Undo the sign-in and dismiss the Sign In screen. |
| 184 TapButtonWithLabelId(IDS_IOS_ACCOUNT_CONSISTENCY_CONFIRMATION_UNDO_BUTTON); |
| 185 TapButtonWithLabelId(IDS_IOS_FIRSTRUN_ACCOUNT_CONSISTENCY_SKIP_BUTTON); |
| 186 |
| 187 // |identity| shouldn't be signed in. |
| 188 AssertAuthenticatedIdentityInActiveProfile(nil); |
| 189 } |
| 190 |
| 191 // Signs in to an account and then taps the Advanced link to go to settings. |
| 192 - (void)testSignInAndTapSettingsLink { |
| 193 ChromeIdentity* identity = GetFakeIdentity(); |
| 194 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 195 identity); |
| 196 |
| 197 // Launch First Run and accept tems of services. |
| 198 [chrome_test_util::GetMainController() showFirstRunUI]; |
| 199 TapButtonWithLabelId(IDS_IOS_FIRSTRUN_OPT_IN_ACCEPT_BUTTON); |
| 200 |
| 201 // Sign In |identity|. |
| 202 TapButtonWithLabelId(IDS_IOS_ACCOUNT_CONSISTENCY_SETUP_SIGNIN_BUTTON); |
| 203 AssertAuthenticatedIdentityInActiveProfile(identity); |
| 204 |
| 205 // Tap Settings link. |
| 206 id<GREYMatcher> settings_link_matcher = grey_allOf( |
| 207 grey_accessibilityLabel(@"Settings"), grey_sufficientlyVisible(), nil); |
| 208 [[EarlGrey selectElementWithMatcher:settings_link_matcher] |
| 209 performAction:grey_tap()]; |
| 210 |
| 211 // Check Sync hasn't started yet, allowing the user to change somes settings. |
| 212 SyncSetupService* sync_service = SyncSetupServiceFactory::GetForBrowserState( |
| 213 chrome_test_util::GetOriginalBrowserState()); |
| 214 GREYAssertFalse(sync_service->HasFinishedInitialSetup(), |
| 215 @"Sync shouldn't have finished its original setup yet"); |
| 216 |
| 217 // Close Settings, user is still signed in and sync is now starting. |
| 218 TapButtonWithLabelId(IDS_IOS_NAVIGATION_BAR_DONE_BUTTON); |
| 219 AssertAuthenticatedIdentityInActiveProfile(identity); |
| 220 GREYAssertTrue(sync_service->HasFinishedInitialSetup(), |
| 221 @"Sync should have finished its original setup"); |
| 222 } |
| 223 |
| 224 @end |
OLD | NEW |