1 // Copyright 2017 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 package org.chromium.chrome.browser.autofill.settings; 6 7 import android.view.KeyEvent; 8 import android.widget.EditText; 9 10 import org.chromium.base.ThreadUtils; 11 import org.chromium.base.test.util.CallbackHelper; 12 import org.chromium.chrome.browser.autofill.prefeditor.EditorDialog; 13 import org.chromium.chrome.browser.autofill.prefeditor.EditorObserverForTest; 14 import org.chromium.chrome.test.ChromeBrowserTestRule; 15 import org.chromium.content_public.browser.test.util.TestThreadUtils; 16 17 import java.util.List; 18 import java.util.concurrent.TimeoutException; 19 20 /** 21 * Custom ChromeBrowserTestRule to test Autofill. 22 */ 23 class AutofillTestRule extends ChromeBrowserTestRule implements EditorObserverForTest { 24 final CallbackHelper mClickUpdate; 25 final CallbackHelper mEditorTextUpdate; 26 final CallbackHelper mPreferenceUpdate; 27 final CallbackHelper mValidationUpdate; 28 29 private EditorDialog mEditorDialog; 30 AutofillTestRule()31 AutofillTestRule() { 32 mClickUpdate = new CallbackHelper(); 33 mEditorTextUpdate = new CallbackHelper(); 34 mPreferenceUpdate = new CallbackHelper(); 35 mValidationUpdate = new CallbackHelper(); 36 AutofillProfilesFragment.setObserverForTest(AutofillTestRule.this); 37 } 38 setTextInEditorAndWait(final String[] values)39 protected void setTextInEditorAndWait(final String[] values) throws TimeoutException { 40 int callCount = mEditorTextUpdate.getCallCount(); 41 TestThreadUtils.runOnUiThreadBlocking(() -> { 42 List<EditText> fields = mEditorDialog.getEditableTextFieldsForTest(); 43 for (int i = 0; i < values.length; i++) { 44 fields.get(i).setText(values[i]); 45 } 46 }); 47 mEditorTextUpdate.waitForCallback(callCount); 48 } 49 clickInEditorAndWait(final int resourceId)50 protected void clickInEditorAndWait(final int resourceId) throws TimeoutException { 51 int callCount = mClickUpdate.getCallCount(); 52 TestThreadUtils.runOnUiThreadBlockingNoException( 53 () -> mEditorDialog.findViewById(resourceId).performClick()); 54 mClickUpdate.waitForCallback(callCount); 55 } 56 clickInEditorAndWaitForValidationError(final int resourceId)57 protected void clickInEditorAndWaitForValidationError(final int resourceId) 58 throws TimeoutException { 59 int callCount = mValidationUpdate.getCallCount(); 60 TestThreadUtils.runOnUiThreadBlockingNoException( 61 () -> mEditorDialog.findViewById(resourceId).performClick()); 62 mValidationUpdate.waitForCallback(callCount); 63 } 64 sendKeycodeToTextFieldInEditorAndWait( final int keycode, final int textFieldIndex)65 protected void sendKeycodeToTextFieldInEditorAndWait( 66 final int keycode, final int textFieldIndex) throws TimeoutException { 67 int callCount = mClickUpdate.getCallCount(); 68 TestThreadUtils.runOnUiThreadBlocking(() -> { 69 List<EditText> fields = mEditorDialog.getEditableTextFieldsForTest(); 70 fields.get(textFieldIndex) 71 .dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keycode)); 72 fields.get(textFieldIndex).dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keycode)); 73 }); 74 mClickUpdate.waitForCallback(callCount); 75 } 76 waitForThePreferenceUpdate()77 protected void waitForThePreferenceUpdate() throws TimeoutException { 78 int callCount = mPreferenceUpdate.getCallCount(); 79 mPreferenceUpdate.waitForCallback(callCount); 80 } 81 setEditorDialogAndWait(EditorDialog editorDialog)82 protected void setEditorDialogAndWait(EditorDialog editorDialog) throws TimeoutException { 83 int callCount = mClickUpdate.getCallCount(); 84 mEditorDialog = editorDialog; 85 mClickUpdate.waitForCallback(callCount); 86 } 87 88 @Override onEditorDismiss()89 public void onEditorDismiss() { 90 ThreadUtils.assertOnUiThread(); 91 mPreferenceUpdate.notifyCalled(); 92 } 93 94 @Override onEditorTextUpdate()95 public void onEditorTextUpdate() { 96 ThreadUtils.assertOnUiThread(); 97 mEditorTextUpdate.notifyCalled(); 98 } 99 @Override onEditorReadyToEdit()100 public void onEditorReadyToEdit() { 101 ThreadUtils.assertOnUiThread(); 102 mClickUpdate.notifyCalled(); 103 } 104 105 @Override onEditorValidationError()106 public void onEditorValidationError() { 107 ThreadUtils.assertOnUiThread(); 108 mValidationUpdate.notifyCalled(); 109 } 110 } 111