| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <EarlGrey/EarlGrey.h> | |
| 6 #import <XCTest/XCTest.h> | |
| 7 | |
| 8 #include "base/mac/bind_objc_block.h" | 5 #include "base/mac/bind_objc_block.h" |
| 9 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 13 #import "base/test/ios/wait_util.h" | 10 #import "base/test/ios/wait_util.h" |
| 14 #include "components/strings/grit/components_strings.h" | 11 #include "components/strings/grit/components_strings.h" |
| 15 #include "ios/chrome/browser/experimental_flags.h" | |
| 16 #import "ios/chrome/browser/metrics/tab_usage_recorder.h" | 12 #import "ios/chrome/browser/metrics/tab_usage_recorder.h" |
| 13 #import "ios/chrome/browser/metrics/tab_usage_recorder_test_util.h" |
| 17 #import "ios/chrome/browser/ui/settings/privacy_collection_view_controller.h" | 14 #import "ios/chrome/browser/ui/settings/privacy_collection_view_controller.h" |
| 18 #import "ios/chrome/browser/ui/settings/settings_collection_view_controller.h" | 15 #import "ios/chrome/browser/ui/settings/settings_collection_view_controller.h" |
| 19 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h" | 16 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h" |
| 20 #include "ios/chrome/browser/ui/tools_menu/tools_menu_constants.h" | 17 #include "ios/chrome/browser/ui/tools_menu/tools_menu_constants.h" |
| 21 #include "ios/chrome/browser/ui/ui_util.h" | |
| 22 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 18 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 23 #include "ios/chrome/grit/ios_strings.h" | |
| 24 #import "ios/chrome/test/app/chrome_test_util.h" | 19 #import "ios/chrome/test/app/chrome_test_util.h" |
| 25 #import "ios/chrome/test/app/histogram_test_util.h" | 20 #import "ios/chrome/test/app/histogram_test_util.h" |
| 26 #include "ios/chrome/test/app/navigation_test_util.h" | 21 #include "ios/chrome/test/app/navigation_test_util.h" |
| 27 #import "ios/chrome/test/app/tab_test_util.h" | 22 #import "ios/chrome/test/app/tab_test_util.h" |
| 28 #include "ios/chrome/test/app/web_view_interaction_test_util.h" | 23 #include "ios/chrome/test/app/web_view_interaction_test_util.h" |
| 29 #import "ios/chrome/test/earl_grey/chrome_actions.h" | 24 #import "ios/chrome/test/earl_grey/chrome_actions.h" |
| 30 #import "ios/chrome/test/earl_grey/chrome_assertions.h" | 25 #import "ios/chrome/test/earl_grey/chrome_assertions.h" |
| 31 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" | 26 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 32 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" | 27 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 33 #import "ios/chrome/test/earl_grey/chrome_matchers.h" | 28 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 34 #import "ios/chrome/test/earl_grey/chrome_test_case.h" | 29 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 35 #import "ios/testing/wait_util.h" | 30 #import "ios/testing/wait_util.h" |
| 36 #import "ios/web/public/test/earl_grey/web_view_matchers.h" | 31 #import "ios/web/public/test/earl_grey/web_view_matchers.h" |
| 37 #import "ios/web/public/test/http_server.h" | 32 #import "ios/web/public/test/http_server.h" |
| 38 #import "ios/web/public/test/http_server_util.h" | 33 #import "ios/web/public/test/http_server_util.h" |
| 39 #include "ios/web/public/test/response_providers/delayed_response_provider.h" | 34 #include "ios/web/public/test/response_providers/delayed_response_provider.h" |
| 40 #include "ios/web/public/test/response_providers/html_response_provider.h" | 35 #include "ios/web/public/test/response_providers/html_response_provider.h" |
| 41 #include "ui/base/l10n/l10n_util.h" | |
| 42 #include "ui/base/l10n/l10n_util_mac.h" | 36 #include "ui/base/l10n/l10n_util_mac.h" |
| 37 #include "url/gurl.h" |
| 43 | 38 |
| 44 #if !defined(__has_feature) || !__has_feature(objc_arc) | 39 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 45 #error "This file requires ARC support." | 40 #error "This file requires ARC support." |
| 46 #endif | 41 #endif |
| 47 | 42 |
| 48 using chrome_test_util::OpenLinkInNewTabButton; | 43 using chrome_test_util::OpenLinkInNewTabButton; |
| 44 using tab_usage_recorder_test_util::OpenNewIncognitoTabUsingUIAndEvictMainTabs; |
| 45 using tab_usage_recorder_test_util::SwitchToNormalMode; |
| 49 | 46 |
| 50 namespace { | 47 namespace { |
| 51 | 48 |
| 52 const char kTestUrl1[] = | 49 const char kTestUrl1[] = |
| 53 "http://ios/testing/data/http_server_files/memory_usage.html"; | 50 "http://ios/testing/data/http_server_files/memory_usage.html"; |
| 54 const char kURL1FirstWord[] = "Page"; | 51 const char kURL1FirstWord[] = "Page"; |
| 55 const char kTestUrl2[] = | 52 const char kTestUrl2[] = |
| 56 "http://ios/testing/data/http_server_files/fullscreen.html"; | 53 "http://ios/testing/data/http_server_files/fullscreen.html"; |
| 57 const char kURL2FirstWord[] = "Rugby"; | 54 const char kURL2FirstWord[] = "Rugby"; |
| 58 const char kClearPageScript[] = "document.body.innerHTML='';"; | 55 const char kClearPageScript[] = "document.body.innerHTML='';"; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 id<GREYMatcher> tool_menu_matcher = | 113 id<GREYMatcher> tool_menu_matcher = |
| 117 grey_accessibilityID(kToolbarToolsMenuButtonIdentifier); | 114 grey_accessibilityID(kToolbarToolsMenuButtonIdentifier); |
| 118 WaitAndTap(tool_menu_matcher, @"Tool menu"); | 115 WaitAndTap(tool_menu_matcher, @"Tool menu"); |
| 119 id<GREYMatcher> new_main_tab_button_matcher = | 116 id<GREYMatcher> new_main_tab_button_matcher = |
| 120 grey_accessibilityID(kToolsMenuNewTabId); | 117 grey_accessibilityID(kToolsMenuNewTabId); |
| 121 WaitAndTap(new_main_tab_button_matcher, @"New tab button"); | 118 WaitAndTap(new_main_tab_button_matcher, @"New tab button"); |
| 122 | 119 |
| 123 chrome_test_util::AssertMainTabCount(nb_main_tab + 1); | 120 chrome_test_util::AssertMainTabCount(nb_main_tab + 1); |
| 124 } | 121 } |
| 125 | 122 |
| 126 // Opens a new incognito tab using the UI and evicts any main tab model tabs. | |
| 127 void OpenNewIncognitoTabUsingUIAndEvictMainTabs() { | |
| 128 int nb_incognito_tab = chrome_test_util::GetIncognitoTabCount(); | |
| 129 [ChromeEarlGreyUI openToolsMenu]; | |
| 130 id<GREYMatcher> new_incognito_tab_button_matcher = | |
| 131 grey_accessibilityID(kToolsMenuNewIncognitoTabId); | |
| 132 [[EarlGrey selectElementWithMatcher:new_incognito_tab_button_matcher] | |
| 133 performAction:grey_tap()]; | |
| 134 chrome_test_util::AssertIncognitoTabCount(nb_incognito_tab + 1); | |
| 135 ConditionBlock condition = ^bool { | |
| 136 return chrome_test_util::IsIncognitoMode(); | |
| 137 }; | |
| 138 GREYAssert( | |
| 139 testing::WaitUntilConditionOrTimeout(kWaitElementTimeout, condition), | |
| 140 @"Waiting switch to incognito mode."); | |
| 141 chrome_test_util::EvictOtherTabModelTabs(); | |
| 142 } | |
| 143 | |
| 144 // Closes a tab in the current tab model. Synchronize on tab number afterwards. | 123 // Closes a tab in the current tab model. Synchronize on tab number afterwards. |
| 145 void CloseTabAtIndexAndSync(NSUInteger i) { | 124 void CloseTabAtIndexAndSync(NSUInteger i) { |
| 146 NSUInteger nb_main_tab = chrome_test_util::GetMainTabCount(); | 125 NSUInteger nb_main_tab = chrome_test_util::GetMainTabCount(); |
| 147 chrome_test_util::CloseTabAtIndex(i); | 126 chrome_test_util::CloseTabAtIndex(i); |
| 148 ConditionBlock condition = ^{ | 127 ConditionBlock condition = ^{ |
| 149 return chrome_test_util::GetMainTabCount() == (nb_main_tab - 1); | 128 return chrome_test_util::GetMainTabCount() == (nb_main_tab - 1); |
| 150 }; | 129 }; |
| 151 GREYAssert( | 130 GREYAssert( |
| 152 testing::WaitUntilConditionOrTimeout(kWaitElementTimeout, condition), | 131 testing::WaitUntilConditionOrTimeout(kWaitElementTimeout, condition), |
| 153 @"Waiting for tab to close"); | 132 @"Waiting for tab to close"); |
| 154 } | 133 } |
| 155 | 134 |
| 156 // Closes the tabs switcher. | |
| 157 void CloseTabSwitcher() { | |
| 158 id<GREYMatcher> matcher = chrome_test_util::ButtonWithAccessibilityLabelId( | |
| 159 IDS_IOS_TAB_STRIP_LEAVE_TAB_SWITCHER); | |
| 160 [[EarlGrey selectElementWithMatcher:matcher] performAction:grey_tap()]; | |
| 161 } | |
| 162 | |
| 163 // Swithches to normal mode using swith button (iPad) or stack view (iPhone). | |
| 164 // Assumes current mode is Incognito. | |
| 165 void SwitchToNormalMode() { | |
| 166 GREYAssertTrue(chrome_test_util::IsIncognitoMode(), | |
| 167 @"Switching to normal mode is only allowed from Incognito."); | |
| 168 if (IsIPadIdiom()) { | |
| 169 // Enter the tab switcher. | |
| 170 id<GREYMatcher> tabSwitcherEnterButton = grey_accessibilityLabel( | |
| 171 l10n_util::GetNSStringWithFixup(IDS_IOS_TAB_STRIP_ENTER_TAB_SWITCHER)); | |
| 172 [[EarlGrey selectElementWithMatcher:tabSwitcherEnterButton] | |
| 173 performAction:grey_tap()]; | |
| 174 | |
| 175 // Select the non incognito panel. | |
| 176 id<GREYMatcher> tabSwitcherHeaderPanelButton = | |
| 177 grey_accessibilityLabel(l10n_util::GetNSStringWithFixup( | |
| 178 IDS_IOS_TAB_SWITCHER_HEADER_NON_INCOGNITO_TABS)); | |
| 179 [[EarlGrey selectElementWithMatcher:tabSwitcherHeaderPanelButton] | |
| 180 performAction:grey_tap()]; | |
| 181 | |
| 182 // Leave the tab switcher. | |
| 183 CloseTabSwitcher(); | |
| 184 } else { | |
| 185 [[EarlGrey selectElementWithMatcher:chrome_test_util::ShowTabsButton()] | |
| 186 performAction:grey_tap()]; | |
| 187 [[EarlGrey selectElementWithMatcher: | |
| 188 chrome_test_util::ButtonWithAccessibilityLabelId( | |
| 189 IDS_IOS_TOOLS_MENU_NEW_INCOGNITO_TAB)] | |
| 190 performAction:grey_swipeSlowInDirection(kGREYDirectionRight)]; | |
| 191 [[EarlGrey selectElementWithMatcher:chrome_test_util::ShowTabsButton()] | |
| 192 performAction:grey_tap()]; | |
| 193 } | |
| 194 ConditionBlock condition = ^bool { | |
| 195 return !chrome_test_util::IsIncognitoMode(); | |
| 196 }; | |
| 197 GREYAssert( | |
| 198 testing::WaitUntilConditionOrTimeout(kWaitElementTimeout, condition), | |
| 199 @"Waiting switch to normal mode."); | |
| 200 } | |
| 201 | |
| 202 // Open the settings submenu. Assumes that settings menu is visible. | 135 // Open the settings submenu. Assumes that settings menu is visible. |
| 203 void OpenSettingsSubMenuUnsynced(int submenu) { | 136 void OpenSettingsSubMenuUnsynced(int submenu) { |
| 204 id<GREYMatcher> settings_button_matcher = | 137 id<GREYMatcher> settings_button_matcher = |
| 205 grey_text(l10n_util::GetNSString(submenu)); | 138 grey_text(l10n_util::GetNSString(submenu)); |
| 206 [[[EarlGrey selectElementWithMatcher:settings_button_matcher] | 139 [[[EarlGrey selectElementWithMatcher:settings_button_matcher] |
| 207 usingSearchAction:grey_swipeSlowInDirection(kGREYDirectionUp) | 140 usingSearchAction:grey_swipeSlowInDirection(kGREYDirectionUp) |
| 208 onElementWithMatcher:grey_accessibilityID(kSettingsCollectionViewId)] | 141 onElementWithMatcher:grey_accessibilityID(kSettingsCollectionViewId)] |
| 209 performAction:grey_tap()]; | 142 performAction:grey_tap()]; |
| 210 } | 143 } |
| 211 | 144 |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 | 418 |
| 486 histogramTester.ExpectUniqueSample(kEvictedTabReloadSuccessRate, | 419 histogramTester.ExpectUniqueSample(kEvictedTabReloadSuccessRate, |
| 487 TabUsageRecorder::LOAD_SUCCESS, 1, | 420 TabUsageRecorder::LOAD_SUCCESS, 1, |
| 488 failureBlock); | 421 failureBlock); |
| 489 histogramTester.ExpectUniqueSample(kDidUserWaitForEvictedTabReload, | 422 histogramTester.ExpectUniqueSample(kDidUserWaitForEvictedTabReload, |
| 490 TabUsageRecorder::USER_WAITED, 1, | 423 TabUsageRecorder::USER_WAITED, 1, |
| 491 failureBlock); | 424 failureBlock); |
| 492 histogramTester.ExpectTotalCount(kEvictedTabReloadTime, 1, failureBlock); | 425 histogramTester.ExpectTotalCount(kEvictedTabReloadTime, 1, failureBlock); |
| 493 } | 426 } |
| 494 | 427 |
| 495 // Verify correct recording of metrics when the reloading of an evicted tab | |
| 496 // fails. | |
| 497 - (void)testEvictedTabReloadFailure { | |
| 498 // TODO(crbug.com/709126): Evaluate and re-enable this test if necessary. | |
| 499 #if !TARGET_IPHONE_SIMULATOR | |
| 500 EARL_GREY_TEST_DISABLED(@"Test disabled on device."); | |
| 501 #endif | |
| 502 | |
| 503 web::test::SetUpFileBasedHttpServer(); | |
| 504 chrome_test_util::HistogramTester histogramTester; | |
| 505 FailureBlock failureBlock = ^(NSString* error) { | |
| 506 GREYFail(error); | |
| 507 }; | |
| 508 | |
| 509 // This URL is purposely invalid so it triggers a navigation error. | |
| 510 GURL invalidURL(kTestUrl1); | |
| 511 | |
| 512 chrome_test_util::OpenNewTab(); | |
| 513 [ChromeEarlGrey loadURL:invalidURL]; | |
| 514 [ChromeEarlGrey waitForErrorPage]; | |
| 515 OpenNewIncognitoTabUsingUIAndEvictMainTabs(); | |
| 516 | |
| 517 SwitchToNormalMode(); | |
| 518 [ChromeEarlGrey waitForErrorPage]; | |
| 519 | |
| 520 histogramTester.ExpectUniqueSample(kEvictedTabReloadSuccessRate, | |
| 521 TabUsageRecorder::LOAD_FAILURE, 1, | |
| 522 failureBlock); | |
| 523 histogramTester.ExpectUniqueSample(kDidUserWaitForEvictedTabReload, | |
| 524 TabUsageRecorder::USER_WAITED, 1, | |
| 525 failureBlock); | |
| 526 histogramTester.ExpectTotalCount(kEvictedTabReloadTime, 0, failureBlock); | |
| 527 } | |
| 528 | |
| 529 // Test that USER_DID_NOT_WAIT is reported if the user does not wait for the | 428 // Test that USER_DID_NOT_WAIT is reported if the user does not wait for the |
| 530 // reload to be complete after eviction. | 429 // reload to be complete after eviction. |
| 531 - (void)testEvictedTabSlowReload { | 430 - (void)testEvictedTabSlowReload { |
| 532 std::map<GURL, std::string> responses; | 431 std::map<GURL, std::string> responses; |
| 533 const GURL slowURL = web::test::HttpServer::MakeUrl("http://slow"); | 432 const GURL slowURL = web::test::HttpServer::MakeUrl("http://slow"); |
| 534 responses[slowURL] = "Slow Page"; | 433 responses[slowURL] = "Slow Page"; |
| 535 | 434 |
| 536 web::test::SetUpHttpServer(base::MakeUnique<web::DelayedResponseProvider>( | 435 web::test::SetUpHttpServer(base::MakeUnique<web::DelayedResponseProvider>( |
| 537 base::MakeUnique<HtmlResponseProvider>(responses), kSlowURLDelay)); | 436 base::MakeUnique<HtmlResponseProvider>(responses), kSlowURLDelay)); |
| 538 | 437 |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 936 [[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; | 835 [[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; |
| 937 } | 836 } |
| 938 // The deleted tabs are purged during foregrounding and backgrounding. | 837 // The deleted tabs are purged during foregrounding and backgrounding. |
| 939 chrome_test_util::SimulateTabsBackgrounding(); | 838 chrome_test_util::SimulateTabsBackgrounding(); |
| 940 // Make sure |evicted_tabs_| purged the deleted tabs. | 839 // Make sure |evicted_tabs_| purged the deleted tabs. |
| 941 int evicted = chrome_test_util::GetEvictedMainTabCount(); | 840 int evicted = chrome_test_util::GetEvictedMainTabCount(); |
| 942 GREYAssertEqual(evicted, 0, @"Check number of evicted tabs"); | 841 GREYAssertEqual(evicted, 0, @"Check number of evicted tabs"); |
| 943 } | 842 } |
| 944 | 843 |
| 945 @end | 844 @end |
| OLD | NEW |