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 |
| 7 #include "base/strings/sys_string_conversions.h" |
| 8 #import "base/test/ios/wait_util.h" |
| 9 #include "ios/chrome/browser/autofill/form_input_accessory_view_controller.h" |
| 10 #include "ios/chrome/browser/ui/ui_util.h" |
| 11 #include "ios/chrome/grit/ios_strings.h" |
| 12 #import "ios/chrome/test/app/chrome_test_util.h" |
| 13 #import "ios/chrome/test/app/tab_test_util.h" |
| 14 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 15 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 17 #import "ios/testing/earl_grey/disabled_test_macros.h" |
| 18 #import "ios/testing/wait_util.h" |
| 19 #import "ios/web/public/test/earl_grey/web_view_actions.h" |
| 20 #import "ios/web/public/test/earl_grey/web_view_matchers.h" |
| 21 #import "ios/web/public/test/http_server.h" |
| 22 #include "ios/web/public/test/http_server_util.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char kFormElementId1[] = "username"; |
| 27 const char kFormElementId2[] = "otherstuff"; |
| 28 |
| 29 // If an element is focused in the webview, returns its ID. Returns an empty |
| 30 // NSString otherwise. |
| 31 NSString* GetFocusedElementId() { |
| 32 NSString* js = @"(function() {" |
| 33 " return document.activeElement.id;" |
| 34 "})();"; |
| 35 NSError* error = nil; |
| 36 NSString* result = chrome_test_util::ExecuteJavaScript(js, &error); |
| 37 GREYAssertTrue(!error, @"Unexpected error when executing JavaScript."); |
| 38 return result; |
| 39 } |
| 40 |
| 41 // Verifies that |elementId| is the selected element in the web page. |
| 42 void AssertElementIsFocused(const std::string& element_id) { |
| 43 NSString* description = |
| 44 [NSString stringWithFormat:@"Timeout waiting for the focused element in " |
| 45 @"the webview to be \"%@\"", |
| 46 base::SysUTF8ToNSString(element_id.c_str())]; |
| 47 ConditionBlock condition = ^{ |
| 48 return base::SysNSStringToUTF8(GetFocusedElementId()) == element_id; |
| 49 }; |
| 50 GREYAssert(testing::WaitUntilConditionOrTimeout(10, condition), description); |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 55 // Tests autofill's keyboard and keyboard's accessories handling. |
| 56 @interface FormInputTestCase : ChromeTestCase |
| 57 @end |
| 58 |
| 59 @implementation FormInputTestCase |
| 60 |
| 61 - (void)tearDown { |
| 62 // |testFindDefaultFormAssistControls| disables synchronization. |
| 63 // This makes sure it is enabled if that test has failed and did not enable it |
| 64 // back. |
| 65 [[GREYConfiguration sharedInstance] |
| 66 setValue:@YES |
| 67 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 68 [super tearDown]; |
| 69 } |
| 70 |
| 71 // Tests finding the correct "next" and "previous" form assist controls in the |
| 72 // iOS built-in form assist view. |
| 73 - (void)testFindDefaultFormAssistControls { |
| 74 // This test is not relevant on iPads: |
| 75 // the previous and next buttons are not shown in our keyboard input |
| 76 // accessory. Instead, they appear in the native keyboard's shortcut bar (to |
| 77 // the left and right of the QuickType suggestions). |
| 78 if (IsIPadIdiom()) { |
| 79 EARL_GREY_TEST_SKIPPED(@"Skipped for iPad (no hidden toolbar in tablet)"); |
| 80 } |
| 81 |
| 82 web::test::SetUpFileBasedHttpServer(); |
| 83 GURL URL = web::test::HttpServer::MakeUrl( |
| 84 "http://ios/testing/data/http_server_files/multi_field_form.html"); |
| 85 [ChromeEarlGrey loadURL:URL]; |
| 86 |
| 87 id<GREYMatcher> webViewMatcher = |
| 88 chrome_test_util::webViewContainingText("hello!"); |
| 89 [[EarlGrey selectElementWithMatcher:webViewMatcher] |
| 90 assertWithMatcher:grey_notNil()]; |
| 91 |
| 92 // Opening the keyboard from a webview blocks EarlGrey's synchronization. |
| 93 [[GREYConfiguration sharedInstance] |
| 94 setValue:@NO |
| 95 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 96 |
| 97 // Brings up the keyboard by tapping on one of the form's field. |
| 98 [[EarlGrey |
| 99 selectElementWithMatcher:web::webViewInWebState( |
| 100 chrome_test_util::GetCurrentWebState())] |
| 101 performAction:web::webViewTapElement( |
| 102 chrome_test_util::GetCurrentWebState(), |
| 103 kFormElementId1)]; |
| 104 |
| 105 id<GREYMatcher> nextButtonMatcher = |
| 106 chrome_test_util::buttonWithAccessibilityLabelId( |
| 107 IDS_IOS_AUTOFILL_ACCNAME_NEXT_FIELD); |
| 108 id<GREYMatcher> previousButtonMatcher = |
| 109 chrome_test_util::buttonWithAccessibilityLabelId( |
| 110 IDS_IOS_AUTOFILL_ACCNAME_PREVIOUS_FIELD); |
| 111 id<GREYMatcher> closeButtonMatcher = |
| 112 chrome_test_util::buttonWithAccessibilityLabelId( |
| 113 IDS_IOS_AUTOFILL_ACCNAME_HIDE_KEYBOARD); |
| 114 |
| 115 // Wait until the keyboard's "Next" button appeared. |
| 116 NSString* description = @"Wait for the keyboard's \"Next\" button to appear."; |
| 117 ConditionBlock condition = ^{ |
| 118 NSError* error = nil; |
| 119 [[EarlGrey selectElementWithMatcher:nextButtonMatcher] |
| 120 assertWithMatcher:grey_notNil() |
| 121 error:&error]; |
| 122 return (error == nil); |
| 123 }; |
| 124 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 125 testing::kWaitForUIElementTimeout, condition), |
| 126 description); |
| 127 base::test::ios::SpinRunLoopWithMinDelay(base::TimeDelta::FromSeconds(1)); |
| 128 |
| 129 // Verifies that the taped element is focused. |
| 130 AssertElementIsFocused(kFormElementId1); |
| 131 |
| 132 // Tap the "Next" button. |
| 133 [[EarlGrey selectElementWithMatcher:nextButtonMatcher] |
| 134 performAction:grey_tap()]; |
| 135 AssertElementIsFocused(kFormElementId2); |
| 136 |
| 137 // Tap the "Previous" button. |
| 138 [[EarlGrey selectElementWithMatcher:previousButtonMatcher] |
| 139 performAction:grey_tap()]; |
| 140 AssertElementIsFocused(kFormElementId1); |
| 141 |
| 142 // Tap the "Close" button. |
| 143 [[EarlGrey selectElementWithMatcher:closeButtonMatcher] |
| 144 performAction:grey_tap()]; |
| 145 |
| 146 [[GREYConfiguration sharedInstance] |
| 147 setValue:@YES |
| 148 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 149 } |
| 150 |
| 151 // Tests that trying to programmatically dismiss the keyboard when it isn't |
| 152 // visible doesn't crash the browser. |
| 153 - (void)testCloseKeyboardWhenNotVisible { |
| 154 FormInputAccessoryViewController* inputAccessoryViewController = |
| 155 chrome_test_util::GetInputAccessoryViewController(); |
| 156 GREYAssertNotNil(inputAccessoryViewController, |
| 157 @"The tab's input accessory view should not be non nil."); |
| 158 [inputAccessoryViewController closeKeyboardWithoutButtonPress]; |
| 159 } |
| 160 |
| 161 @end |
OLD | NEW |