1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-27 04:03:00 +02:00
bitwarden-browser/apps/browser/src/autofill/services/autofill-overlay-content.service.spec.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1613 lines
61 KiB
TypeScript
Raw Normal View History

[PM-4229] Autofill Overlay MVP (#6507) * [PM-3914] Refactor Browser Extension Popouts * [PM-3914] Refactor Browser Extension Popouts * [PM-3914] Refactor Browser Extension Popouts * [PM-3914] Adding enums for the browser popout type * [PM-3914] Making the methods for getting a window in a targeted manner public * [PM-3914] Refactoing implementation * [PM-3914] Updating deprecated api call * [PM-3914] Fixing issues found when testing behavior * [PM-3914] Reimplementing behavior based on feedback from platform team * [PM-3914] Adding method of ensuring previously opened single action window is force closed for vault item password reprompts * [PM-3914] Taking into consideration feedback regarding the browser popup utils service and implementating requested changes * [PM-3914] Removing unnecesssary class dependencies * [PM-3914] Adding method for uniquely setting up password reprompt windows * [PM-3914] Modifying method * [PM-3914] Adding jest tests and documentation for AuthPopoutWindow util * [PM-3914] Adding jest tests and documentation for VaultPopoutWindow * [PM-3914] Adding jest tests for the debouncing method within autofill service * [PM-3914] Adding jest tests for the new BrowserApi methods * [PM-3914] Adding jest tests to the BrowserPopupUtils class * [PM-3914] Updating inPrivateMode reference * [PM-3914] Updating inPrivateMode reference * [PM-3914] Modifying comment * [PM-3914] Moviing implementation for openCurrentPagePopout to the BrowserPopupUtils * [PM-3914] Applying feedback * [PM-3914] Applying feedback * [PM-3914] Applying feedback * [PM-3983] Refactoring implementation of `setContentScrollY` to facilitate having a potential delay * [PM-3914] Applying feedback regarding setContentScrollY to the implementation * [PM-3914] Modifying early return within the run method of the ContextMenuClickedHandler * [PM-3914] Adding test for VaultPopoutWindow * [PM-4229] Autofill Overlay MVP * [PM-2855] Add Settings to Enable Autofill Overlay (#6509) * [PM-2855] Add Settings to Enable Autofill Overlay * [PM-2855] Removing unnecessary key * [PM-3914] Applying work done within PM-4366 to facilitate opening the popout window as a popup rather than a normal window * [PM-3914] Updating the BrowserApi.removeTab method to leverage a callback structure for the promise rather than an async away structure * [PM-3036] Adding jest tests for added passkeys popout windows * [PM-3914] Adjsuting logic for turning off the warning when FIDO2 credentials are saved * [PM-3914] Fixing height to design * [PM-3914] Fixing call to Fido2 Popout * [PM-3914] Fixing add/edit from fido2 popout * [PM-3914] Fixing add/edit from fido2 popout * [PM-3914] Fixing jest tests for updated elements * [PM-3914] Reverting how context menu actions are passed to the view component * [PM-3914] Reverting re-instantiation of config service within main.background.ts * [PM-3914] Adding jest test for BrowserAPI removeTab method * [PM-3914] Adding method to handle parsing the popout url path * [PM-3914] Removing JSDOC comment elements * [PM-3914] Removing await from method call * [PM-3914] Simplifying implementation on add/edit * [PM-3032] Adding more direct reference to view item action in context menus * [PM-3034] Modify Autofill Callout to Consider Autofill Overlay Visibility (#6510) * [PM-2855] Add Settings to Enable Autofill Overlay * [PM-2855] Removing unnecessary key * [PM-3034] Modify Autofill Callout to Consider Autofill Overlay Visibility * [PM-3034] Adding translated strings * [PM-3034] Updating boolean logic for showing the callout to remove unnecessary negation of boolean statement * [PM-3914] Adjusting routing on Fido2 component to pass the singleActionPopout param to the route when opening the add-edit component * [PM-3914] Adding singleActionPopout param to the fido2 component routing * [PM-3914] Updating implementation details for how we build the extension url path * [PM-3914] Reworking implementation for isSingleActionPopoutOpen to clean up iterative logic * [PM-3914] Merging work from master and fixing merge conflicts * [PM-3914] Fixing merge conflict introduced from master * [PM-3914] Reworking closure of single action popouts to ensure they close the window instead of attempting to close the tab * [PM-3036] Implement Autofill Overlay Unlock State (#6514) * [PM-2855] Add Settings to Enable Autofill Overlay * [PM-2855] Removing unnecessary key * [PM-3034] Modify Autofill Callout to Consider Autofill Overlay Visibility * [PM-3034] Adding translated strings * [PM-3034] Add Autofill Overlay Vault Locked State * [PM-3036] Bootstrap Autofill Overlay implementation and add locked vault state * [PM-3032] Removing add/edit cipher message * [PM-3036] Fixing lint error found within overlay background * [PM-3036] Setting properties within the autofill component method to be protected * [PM-3034] Updating boolean logic for showing the callout to remove unnecessary negation of boolean statement * [PM-3036] Applying feedback from browser popout refactor PR * [PM-3036] Adding ownership over the website icon service file to the autofill team * [PM-3036] Updating the `autoFillOverlayVisibility` setting to be a client-scoped setting rather than account-scoped * [PM-3036] Reworking jest setup implementation to facilitate approach recommended within code review * [PM-3036] Updating WebsiteIconService to act as a single function reference and moving it to be under the vault team as codeowners * [PM-3032] Show Matching Logins When User Interacts with Field (#6516) * [PM-3032] Show Matching Logins When User Interacts with Field * [PM-3032] Fixing issue found when changing pages * [PM-3032] Addressing feedback within PR * [PM-3032] Addressing feedback within PR * [PM-3033] Allow User to Fill Matching Logins within Overlay (#6517) * [PM-3033] Allow User to Fill Matching Logins within Overlay * [PM-3035] Allow adding new items when no ciphers found in overlay (#6518) * [PM-2319] Refactoring implementation to leverage styles within the encapsulated custom elements rather than inline on those elements * [PM-2319] Leveraging globalThis to avoid potential DOM clobbering within implementation * [PM-2319] Fixing issue where styles can override visibility of overlay icon and list * [PM-2319] Fixing issue where styles can override visibility of overlay icon and list * [PM-2319] Implementing more secure method for ensuring overlay is visible * [PM-2319] Optimizing implementation of mutation observers on elements that need to enforce CSS styling * [PM-2319] Refactoring how we handle mutation observers to allow for a more streamlined implementation approach * [PM-2319] Implementing view cipher item initial workflow * [PM-2319] Implementing obfruscation of username within login ciphers * [PM-2747] Fixing logic error incorporated when merging in master * [PM-2130] Fixing issue with autofill service unit tests * [PM-2130] Fixing issue with autofill service unit tests * [PM-2747] Fixing issue present with notification bar merge * [PM-2130] Fixing test test for when we need to handle a password reprompt * [PM-2319] Fixing issue present with context menu handler * [PM-2319] Implementing fixes for password reprompt when autofilling from overlay * [PM-2319] Working through accessibility and focus order on overlay elements * [PM-2319] Finishing out focus redirection approach for focus out of overlay list * [PM-2319] Working through screen reader accessibility including aria attributes * [PM-2319] Adding guard to usage of extension privacy api * [PM-2319] Adding guard to usage of extension privacy api * [PM-2319] Adding aria description for fill cipher elements * [PM-2319] Refactoring implementation * [PM-2319] Working through implementation of view cipher tirggers when overlay set to view an element * [PM-2319] Refining implementation for viewing vault item from overlay * [PM-2319] Applying fix for context menu ciphers * [PM-2319] Modifying namespace for overlay icon to overlay button * [PM-2319] Refactoring OverlayButton * [PM-2319] Refactoring OverlayButton * [PM-2319] Adding translations for overlay content * [PM-2319] Refactoring OverlayBackground class * [PM-2319] Refactoring OverlayBackground class to more optimially store and retrieve cipher data for the overlay elements * [PM-2319] Refactoring OverlayBackground class * [PM-2319] Refactoring AutofillOverlayList class structure * [PM-2319] Implementing randomization of custom element names for elements injected into tab * [PM-2319] Updating how we handle referencing port messages within the OverlayIframe service * [PM-3465] Optimization of CollectPageDetails Message within Autofill * [PM-3465] Implementing caching for CollectPage details call * [PM-3465] Implementing caching for CollectPage details call * [PM-3465] Implementing method for ensuring that getPageDetails is not called when no fields appear within a frame * [PM-3465] Implementing Mutation Observer to handle updating autofill fields when DOM updates * [PM-2747] Fixing wording for webpack script * [PM-2130] - Audit, Modularize, and Refactor Core autofill.js File (#5453) * split up autofill.ts, first pass * remove modification tracking comments * lessen and localize eslint disables * additional typing and formatting * update autofill v2 with PR #5364 changes (update/i18n confirm dialogs) * update autofill v2 with PR #4155 changes (add autofill support for textarea) Co-Authored-By: Manuel <mr-manuel@outlook.it> * move commonly used string values to constants * ts cleanup * [PM-2130] Starting work to re-architect autofillv2.ts * [PM-2130] Starting work to re-architect autofillv2.ts * [PM-2130] Working through autofill collect method * [PM-2130] Marking Removal of documentUUID as dead code * [PM-2130] Refining the implementation of collect and moving broken out utils back into class implementation * [PM-2130] Applying small refactors to AutofillCollect * [PM-2130] Refining the implementation of getAutofillFieldLabelTag to help with readability of the method * [PM-2130] Implementing jest tests for AutofillCollect methods * [PM-2130] Refining implementation for AutofillCollect * [PM-2200] Unit tests for autofill content script utilities with slight refactors (#5544) * add unit tests for urlNotSecure * add test coverage command * add unit tests for canSeeElementToStyle * canSeeElementToStyle should not return true if `animateTheFilling` or `currentEl` is false * add tests for selectAllFromDoc and getElementByOpId * clean up getElementByOpId * address some typing issues * add tests for setValueForElementByEvent, setValueForElement, and doSimpleSetByQuery * clean up setValueForElement and setValueForElementByEvent * more typescript cleanup * add tests for doClickByOpId and touchAllPasswordFields * add tests for doFocusByOpId and doClickByQuery * misc fill cleanup * move functions between collect and fill utils and replace getElementForOPID for duplicate getElementByOpId * add tests for isKnownTag and isElementVisible * rename addProp and remove redundant focusElement in favor of doFocusElement * cleanup * fix checkNodeType * add tests for shiftForLeftLabel * clean up and rename checkNodeType, isKnownTag, and shiftForLeftLabel * add tests for getFormElements * clean up getFormElements * add tests for getElementAttrValue, getElementValue, getSelectElementOptions, getLabelTop, and queryDoc * clean up and rename queryDoc to queryDocument * misc cleanup and rename getElementAttrValue to getPropertyOrAttribute * rebase cleanup * prettier formatting * [PM-2130] Fixing linting issues * [PM-2130] Fixing linting issues * [PM-2130] Migrating implementation for collect methods and tests for those methods into AutofillCollect context * [PM-2130] Migrating getPropertyOrAttribute method from utils to AutofillCollect * [PM-2130] Continuing migration of methods from collect utils into AutofillCollect * [PM-2130] Rework of isViewable method to better handle behavior for how we identify if an element is currently within the viewport * [PM-2130] Filling out implementation of autofill-insert * [PM-2130] Refining AutofillInsert * [PM-2130] Implementing jest tests for AutofillCollect methods and breaking out visibility related logic to a separate service * [PM-2130] Fixing jest tests for AutofillCollect * [PM-2130] Fixing jest tests for AutofillInit * [PM-2130] Adjusting how the AutofillFieldVisibilityService class is used in AutofillCollect * [PM-2130] Working through AutofillInsert implementation * [PM-2130] Migrating methods from fill.ts to AutofillInsert * [PM-2130] Migrating methods from fill.ts to AutofillInsert * [PM-2130] Applying fix for IntersectionObserver when triggering behavior in Safari and fixing issue with how we trigger an input event shortly after filling in a field * [PM-2130] Refactoring AutofillCollect to service CollectAutofillContentService * [PM-2130] Refactoring AutofillInsert to service InsertAutofillContentService * [PM-2130] Further organization of implementation * [PM-2130] Filling out missing jest test for AutofillInit.fillForm method * [PM-2130] Migrating the last of the collect jest tests to InsertAutofillContentService * [PM-2130] Further refactoring of elements including typing information * [PM-2130] Implementing jest tests for InsertAutofillContentService * [PM-2130] Implementing jest tests for InsertAutofillContentService * [PM-2130] Organization and refactoring of methods within InsertAutofillContent * [PM-2130] Implementation of jest tests for InsertAutofillContentService * [PM-2130] Implementation of Jest Test for IntertAutofillContentService * [PM-2130] Finalizing migration of methods and jest tests from util files into Autofill serivces * [PM-2130] Cleaning up dead code comments * [PM-2130] Removing unnecessary constants * [PM-2130] Finalizing jest tests for InsertAutofillContentService * [PM-2130] Refactoring FieldVisibiltyService to DomElementVisibilityService to allow service to act in a more general manner * [PM-2130] Implementing jest tests for DomElementVisibilityService * [PM-2130] Implementing jest tests for DomElementVisibilityService * [PM-2130] Implementing jest tests for DomElementVisibilityService * [PM-2130] Implementing jest tests for DomElementVisibilityService * [PM-2130] Breaking out the callback method used to resolve the IntersectionObserver promise * [PM-2130] Adding a comment explaining a fix for Safari * [PM-2130] Adding a comment explaining a fix for Safari * [PM-2130] Applying changes required for PM-2762 to implementation, and ensuring jest tests exist to validate the behavior * [PM-2130] Removing usage of IntersectionObserver when identifying element visibility due to broken interactions with React Components * [PM-2130] Fixing issue found when attempting to capture the elementAtCenterPoint in determining file visibility * [PM-2100] Create Unit Test Suite for autofill.service.ts (#5371) * [PM-2100] Create Unit Test Suite for Autofill.service.ts * [PM-2100] Finishing out tests for the getFormsWithPasswordFields method * [PM-2100] Implementing tests for the doAutofill method within the autofill service * [PM-2100] Working through implementation of doAutofill method * [PM-2100] Working through implementation of doAutofill method * [PM-2100] Finishing implementatino of isUntrustedIframe method within autofill service * [PM-2100] Finishing implementation of doAutoFill method within autofill service * [PM-2100] Finishing implementation of doAutoFillOnTab method within autofill service * [PM-2100] Working through tests for generateFillScript * [PM-2100] Finalizing generateFillScript method testing * [PM-2100] Starting implementation of generateLoginFillScript * [PM-2100] Working through tests for generateLoginFillScript * [PM-2100] Finalizing generateLoginFillScript method testing * [PM-2100] Removing unnecessary jest config file * [PM-2100] Fixing jest tests based on changes implemented within PM-2130 * [PM-2100] Fixing autofill mocks * [PM-2100] Fixing AutofillService jest tests * [PM-2100] Handling missing tests within coverage of AutofillService * [PM-2100] Handling missing tests within coverage of AutofillService.generateLoginFillScript * [PM-2100] Writing tests for AutofillService.generateCardFillScript * [PM-2100] Finalizing tests for AutofillService.generateCardFillScript * [PM-2100] Adding additional tests to cover changes introduced by TOTOP autofill PR * [PM-2100] Adding jest tests for Autofill.generateIdentityFillScript * [PM-2100] Finalizing tests for AutofillService.generateIdentityFillScript * [PM-2100] Implementing tests for AutofillService * [PM-2100] Implementing tests for AutofillService.loadPasswordFields * [PM-2100] Implementing tests for AutofillService.findUsernameField * [PM-2100] Implementing tests for AutofillService.findTotpField * [PM-2100] Implementing tests for AutofillService.fieldPropertyIsPrefixMatch * [PM-2100] Finalizing tests for AutofillService * [PM-2100] Modyfing placement of autofill-mocks * [PM-2100] Modyfing placement of autofill-mocks * [PM-2100] Removal of jest transform declaration * [PM-2130] Fixing issue with autofill service unit tests * [PM-2130] Fixing issue with autofill service unit tests * [PM-2130] Fixing test test for when we need to handle a password reprompt --------- Co-authored-by: Manuel <mr-manuel@outlook.it> Co-authored-by: Cesar Gonzalez <cgonzalez@bitwarden.com> Co-authored-by: Cesar Gonzalez <cesar.a.gonzalezcs@gmail.com> * [PM-2747] Finanlizing implementation of attribute updates on cached values * [PM-2319] Refactoring implementation to reposition OverlayIframe classes * [PM-3465] Finalizing implementation of mutation observer behavior and CollectPageDetails optimization * [PM-3465] Adding jest tests for introduced functionality * [PM-3465] Finalizing jest tests and comments within implementation * [PM-3465] Removing a TODO by incorrporating a method for deep querying for a password field element * [PM-3465] Removing a TODO by incorrporating a method for deep querying for a password field element * [PM-3285] Migrating Changes from PM-1407 into autofill v2 refactor implementation * [PM-2747] Addressing stylistic changes requested from code review * [PM-2319] Refactoring implementation * [PM-2747] Add Support for Feature Flag of Autofill Version (#5695) * [PM-2100] Create Unit Test Suite for Autofill.service.ts * [PM-2100] Finishing out tests for the getFormsWithPasswordFields method * [PM-2100] Implementing tests for the doAutofill method within the autofill service * [PM-2100] Working through implementation of doAutofill method * [PM-2100] Working through implementation of doAutofill method * [PM-2100] Finishing implementatino of isUntrustedIframe method within autofill service * [PM-2100] Finishing implementation of doAutoFill method within autofill service * [PM-2100] Finishing implementation of doAutoFillOnTab method within autofill service * [PM-2100] Working through tests for generateFillScript * split up autofill.ts, first pass * remove modification tracking comments * lessen and localize eslint disables * additional typing and formatting * update autofill v2 with PR #5364 changes (update/i18n confirm dialogs) * update autofill v2 with PR #4155 changes (add autofill support for textarea) Co-Authored-By: Manuel <mr-manuel@outlook.it> * move commonly used string values to constants * ts cleanup * [PM-2100] Finalizing generateFillScript method testing * [PM-2100] Starting implementation of generateLoginFillScript * [PM-2100] Working through tests for generateLoginFillScript * [PM-2100] Finalizing generateLoginFillScript method testing * [PM-2130] Starting work to re-architect autofillv2.ts * [PM-2130] Starting work to re-architect autofillv2.ts * [PM-2130] Working through autofill collect method * [PM-2130] Marking Removal of documentUUID as dead code * [PM-2130] Refining the implementation of collect and moving broken out utils back into class implementation * [PM-2130] Applying small refactors to AutofillCollect * [PM-2130] Refining the implementation of getAutofillFieldLabelTag to help with readability of the method * [PM-2130] Implementing jest tests for AutofillCollect methods * [PM-2130] Refining implementation for AutofillCollect * [PM-2200] Unit tests for autofill content script utilities with slight refactors (#5544) * add unit tests for urlNotSecure * add test coverage command * add unit tests for canSeeElementToStyle * canSeeElementToStyle should not return true if `animateTheFilling` or `currentEl` is false * add tests for selectAllFromDoc and getElementByOpId * clean up getElementByOpId * address some typing issues * add tests for setValueForElementByEvent, setValueForElement, and doSimpleSetByQuery * clean up setValueForElement and setValueForElementByEvent * more typescript cleanup * add tests for doClickByOpId and touchAllPasswordFields * add tests for doFocusByOpId and doClickByQuery * misc fill cleanup * move functions between collect and fill utils and replace getElementForOPID for duplicate getElementByOpId * add tests for isKnownTag and isElementVisible * rename addProp and remove redundant focusElement in favor of doFocusElement * cleanup * fix checkNodeType * add tests for shiftForLeftLabel * clean up and rename checkNodeType, isKnownTag, and shiftForLeftLabel * add tests for getFormElements * clean up getFormElements * add tests for getElementAttrValue, getElementValue, getSelectElementOptions, getLabelTop, and queryDoc * clean up and rename queryDoc to queryDocument * misc cleanup and rename getElementAttrValue to getPropertyOrAttribute * rebase cleanup * prettier formatting * [PM-2130] Fixing linting issues * [PM-2130] Fixing linting issues * [PM-2130] Migrating implementation for collect methods and tests for those methods into AutofillCollect context * [PM-2130] Migrating getPropertyOrAttribute method from utils to AutofillCollect * [PM-2130] Continuing migration of methods from collect utils into AutofillCollect * [PM-2130] Rework of isViewable method to better handle behavior for how we identify if an element is currently within the viewport * [PM-2130] Filling out implementation of autofill-insert * [PM-2130] Refining AutofillInsert * [PM-2130] Implementing jest tests for AutofillCollect methods and breaking out visibility related logic to a separate service * [PM-2130] Fixing jest tests for AutofillCollect * [PM-2130] Fixing jest tests for AutofillInit * [PM-2130] Adjusting how the AutofillFieldVisibilityService class is used in AutofillCollect * [PM-2130] Working through AutofillInsert implementation * [PM-2130] Migrating methods from fill.ts to AutofillInsert * [PM-2130] Migrating methods from fill.ts to AutofillInsert * [PM-2130] Applying fix for IntersectionObserver when triggering behavior in Safari and fixing issue with how we trigger an input event shortly after filling in a field * [PM-2130] Refactoring AutofillCollect to service CollectAutofillContentService * [PM-2130] Refactoring AutofillInsert to service InsertAutofillContentService * [PM-2130] Further organization of implementation * [PM-2130] Filling out missing jest test for AutofillInit.fillForm method * [PM-2130] Migrating the last of the collect jest tests to InsertAutofillContentService * [PM-2130] Further refactoring of elements including typing information * [PM-2130] Implementing jest tests for InsertAutofillContentService * [PM-2130] Implementing jest tests for InsertAutofillContentService * [PM-2130] Organization and refactoring of methods within InsertAutofillContent * [PM-2130] Implementation of jest tests for InsertAutofillContentService * [PM-2130] Implementation of Jest Test for IntertAutofillContentService * [PM-2130] Finalizing migration of methods and jest tests from util files into Autofill serivces * [PM-2130] Cleaning up dead code comments * [PM-2130] Removing unnecessary constants * [PM-2130] Finalizing jest tests for InsertAutofillContentService * [PM-2130] Refactoring FieldVisibiltyService to DomElementVisibilityService to allow service to act in a more general manner * [PM-2130] Implementing jest tests for DomElementVisibilityService * [PM-2130] Implementing jest tests for DomElementVisibilityService * [PM-2130] Implementing jest tests for DomElementVisibilityService * [PM-2130] Implementing jest tests for DomElementVisibilityService * [PM-2130] Breaking out the callback method used to resolve the IntersectionObserver promise * [PM-2100] Removing unnecessary jest config file * [PM-2100] Fixing jest tests based on changes implemented within PM-2130 * [PM-2100] Fixing autofill mocks * [PM-2100] Fixing AutofillService jest tests * [PM-2100] Handling missing tests within coverage of AutofillService * [PM-2100] Handling missing tests within coverage of AutofillService.generateLoginFillScript * [PM-2100] Writing tests for AutofillService.generateCardFillScript * [PM-2100] Finalizing tests for AutofillService.generateCardFillScript * [PM-2100] Adding additional tests to cover changes introduced by TOTOP autofill PR * [PM-2100] Adding jest tests for Autofill.generateIdentityFillScript * [PM-2100] Finalizing tests for AutofillService.generateIdentityFillScript * [PM-2100] Implementing tests for AutofillService * [PM-2130] Adding a comment explaining a fix for Safari * [PM-2130] Adding a comment explaining a fix for Safari * [PM-2100] Implementing tests for AutofillService.loadPasswordFields * [PM-2100] Implementing tests for AutofillService.findUsernameField * [PM-2100] Implementing tests for AutofillService.findTotpField * [PM-2100] Implementing tests for AutofillService.fieldPropertyIsPrefixMatch * [PM-2100] Finalizing tests for AutofillService * [PM-2747] Add Support for Feature Flag of Autofill Version * [PM-2747] Adding Support for Manifest v3 within the implementation * [PM-2747] Modifying how the feature flag for autofill is named * [PM-2747] Modifying main.background.ts to load the ConfigApiService correctly * [PM-2747] Refactoring trigger of autofill scripts to be a simple immediately invoked function * [PM-2100] Modyfing placement of autofill-mocks * [PM-2100] Modyfing placement of autofill-mocks * [PM-2100] Removal of jest transform declaration * [PM-2130] Applying changes required for PM-2762 to implementation, and ensuring jest tests exist to validate the behavior * [PM-2747] Modifying how we inject the autofill scripts to ensure we are injecting into all frames within a page * [PM-2130] Removing usage of IntersectionObserver when identifying element visibility due to broken interactions with React Components * [PM-2130] Fixing issue found when attempting to capture the elementAtCenterPoint in determining file visibility * [PM-2100] Create Unit Test Suite for autofill.service.ts (#5371) * [PM-2100] Create Unit Test Suite for Autofill.service.ts * [PM-2100] Finishing out tests for the getFormsWithPasswordFields method * [PM-2100] Implementing tests for the doAutofill method within the autofill service * [PM-2100] Working through implementation of doAutofill method * [PM-2100] Working through implementation of doAutofill method * [PM-2100] Finishing implementatino of isUntrustedIframe method within autofill service * [PM-2100] Finishing implementation of doAutoFill method within autofill service * [PM-2100] Finishing implementation of doAutoFillOnTab method within autofill service * [PM-2100] Working through tests for generateFillScript * [PM-2100] Finalizing generateFillScript method testing * [PM-2100] Starting implementation of generateLoginFillScript * [PM-2100] Working through tests for generateLoginFillScript * [PM-2100] Finalizing generateLoginFillScript method testing * [PM-2100] Removing unnecessary jest config file * [PM-2100] Fixing jest tests based on changes implemented within PM-2130 * [PM-2100] Fixing autofill mocks * [PM-2100] Fixing AutofillService jest tests * [PM-2100] Handling missing tests within coverage of AutofillService * [PM-2100] Handling missing tests within coverage of AutofillService.generateLoginFillScript * [PM-2100] Writing tests for AutofillService.generateCardFillScript * [PM-2100] Finalizing tests for AutofillService.generateCardFillScript * [PM-2100] Adding additional tests to cover changes introduced by TOTOP autofill PR * [PM-2100] Adding jest tests for Autofill.generateIdentityFillScript * [PM-2100] Finalizing tests for AutofillService.generateIdentityFillScript * [PM-2100] Implementing tests for AutofillService * [PM-2100] Implementing tests for AutofillService.loadPasswordFields * [PM-2100] Implementing tests for AutofillService.findUsernameField * [PM-2100] Implementing tests for AutofillService.findTotpField * [PM-2100] Implementing tests for AutofillService.fieldPropertyIsPrefixMatch * [PM-2100] Finalizing tests for AutofillService * [PM-2100] Modyfing placement of autofill-mocks * [PM-2100] Modyfing placement of autofill-mocks * [PM-2100] Removal of jest transform declaration * [PM-2747] Applying a fix for a race condition that can occur when loading the notification bar and autofiller script login * [PM-2747] Reverting removal of autofill npm action. Now this will force usage of autofill-v2 regardless of whether a feature flag is set or not * [PM-2747] Fixing logic error incorporated when merging in master * [PM-2130] Fixing issue with autofill service unit tests * [PM-2130] Fixing issue with autofill service unit tests * [PM-2747] Fixing issue present with notification bar merge * [PM-2130] Fixing test test for when we need to handle a password reprompt * [PM-2747] Fixing wording for webpack script * [PM-2747] Addressing stylistic changes requested from code review * [PM-2747] Addressing stylistic changes requested from code review --------- Co-authored-by: Jonathan Prusik <jprusik@classynemesis.com> Co-authored-by: Manuel <mr-manuel@outlook.it> Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com> * [PM-3285] Applying stylistic changes suggested by code review for the feature flag implementation * [PM-3285] Adding temporary console log to validate which version is being used * [PM-2319] Adjusting translation content * [PM-3465] Implementing a methodology for sorting the autofill field elements after awaiting the results of each element * [PM-3465] Implementing a methodology for sorting the autofill field elements after awaiting the results of each element * [PM-3465] Implementing a methodology for using cached field values when requerying DOM for elements * [PM-2319] Adjusting translation content * [PM-2319] Adding typing information for OverlayBackground * [PM-2319] Removing unnecesssary methods within OverlayBackground and AutofillOverlayContentService * [PM-2319] Refactoring implementation and incorpoarting BrowserApi class more effectively * [PM-2319] Fixing issue found with opening overaly element during reprompt of vault item * [PM-2319] Fixing issue found with auth status not updating when overlay is initializing * [PM-2319] Implementing a method for initializing the overlay with the user auth status * [PM-2319] Fixing issue where shadowRoot elements might not initialize overlay on setup * [PM-2319] Implementing await for runFillScriptAction * [PM-2319] Implementing methodology for having list of elements hide after user starts inputting within field * [PM-2319] Removing unnecesssary methods within OverlayBackground and AutofillOverlayContentService * [PM-2319] Fixing tab focus issue * [PM-2319] Fixing issue where page details would unload sooner than desired * [PM-2319] Fixing tab focus issues present on page details * [PM-2319] Adjusting how we iterate over cipher data * [PM-2319] Refactoring overlay background * [PM-2319] Adding typing information for OverlayBackground * [PM-2319] Adding typing information for OverlayBackground * [PM-2319] Refactoring and optimizing for loops * [PM-2319] Refactoring and optimizing how we listen for overlay element ports * [PM-2319] Implementing method for ensuring overlay removes itself if user scrolls focused input element out of viewport * [PM-2319] Replacing usage of foreach for a regular for loop * [PM-2319] Replacing usage of foreach for a regular for loop * [PM-2319] Refactoring forEach loops within CollectAutofillContent and moving autofill utils to a top level * [PM-2319] Refactoring getRandomCustomElementName util method * [PM-2319] Refactoring implementation * [PM-2319] Refactoring implementation * [PM-2319] Replacing hardcoded values for events with constant enum * [PM-2319] Adding reduced animation declaration for fill * [PM-2319] Adjusting implementation of mutation observer to better handle insertion of elements around overlay * [PM-2319] Fixing jest test * [PM-2319] Implementing method for ensuring tab focus from the overlay button can move to the correct place * [PM-2319] Refactoring implementation * [PM-3285] Removing temporary console log indicating which version of autofill the user is currently loading * [PM-3465] Adding scripting api reference to the manifest v3 json file * [PM-2319] Splitting shared logic within the overlay page implementations to act as a parent class for the overlay button and list pages * [PM-2319] Updating file names for page scripts * [PM-2319] Updating file names for page scripts * [PM-2319] Fixing issues present with overlay background when updating auth status * [PM-2319] Refactoring implementation * [PM-2319] Fixing cache invalidation issues present with the collect page details optimization * [PM-3465] Updating implementation to deal with cache invalidation issues * [PM-3465] Implementing jest tests for added collect autofill content class elements * [PM-3465] Removing scripting API permissiong within manifest v3 json file * [PM-2319] Adding scripting api to manifest v3 * [PM-2319] Fixing issue present with non visible fields having an overlay element * [PM-3465] Implementing method for removing cached page details if the window location has updated * [PM-3465] Fixing issue found with query selector generated while collecting page details * [PM-2319] Commenting out code that overrides default browser autofill behavior in chrome * [PM-3465] Fixing jest tests * [PM-3465] Fixing jest tests * [PM-2319] Adding typing information for OverlayBackground * [PM-2319] Updating typing information for the Overlay Background * [PM-2319] Adding typing information for notification changes * [PM-2319] Finalizing OverlayBackground typing info and removing browser autofill override method * [PM-2319] Refining typing information within different service classes * [PM-2319] Finalizing typing information within implementation * [PM-2319] Further refinement and fixes for icon element * [PM-2319] Fixing issue where submission of form and presentation of notification bar can offset the overlay element * [PM-2319] Fixing issues present with keyboard focus and determining when to open the overlay upon user interaction * [PM-2319] Adding in change to fix issue where autofill is occurring when iframes exist * [PM-2319] Implementing lazy load of UI elements * [PM-2319] Fixing issue present with lazy loading of cipher elements * [PM-2319] Fixing issue present with lazy loading of cipher elements * [PM-2319] Modifying offset for the ciphers list container * [PM-2319] Fixing issue encountered with autofilling using keyboard * [PM-2319] Modifying initialization of iframe element * [PM-2319] Fixing an issue where login ciphers that do not contain a user name will not display within the overlay list * [PM-2855] [PM-3034] Add Setting to Enable Autofill Overlay (#6194) * [PM-2855] Add Settings to Enable Autofil Overlay * [PM-2855] Adding feature flag for overlay * [PM-2855] Implementing autofill overlay setting within browser extension * [PM-2855] Implementing autofill overlay appearance setting * [PM-2855] Implementing behavior within autofill overlay to conditionally display either the icon or the full list on focus of an element * [PM-2855] Implementing a fix for when focus changes with the form field visible * [PM-2855] Modifying rules for how the callout appears within the current-tab component * [PM-2855] Modifying enum for autofill overlay appearance * [PM-2855] Implementing check to ensure autofill overlay setting is not visible if the feature flag is not set * [PM-2855] Fixing jest tests within implementation * [PM-2855] Modifying how we pull the overlay appearance information for the end user * [PM-2855] Applying changes to the structure for how the overlay settings are identified and verified * [PM-2855] Applying changes to the structure for how the overlay settings are identified and verified * [PM-2855] Adding translations content * [PM-2855] Modifying implementation for how autofill settings populate and present themselves * [PM-2855] Modifying implementation for how autofill settings populate and present themselves * [PM-2855] Adding the ability to override autofill permissions within Chrome as an opt-in * [PM-2855] Modifying message sent when vault item reprompt popout is opened * [PM-2855] Fixing issue encountered with how we handle lazy loading vaul items * [PM-2855] Fixing issue present when iframe is updating position when the window focus changes * [PM-3982] Implement Autofill Overlay unit tests (#6337) * [PM-2319] Jest Tests for Autofill Overlay MVP * [PM-2319] Jest test stubs for OverlayBackground * add tests and cleanup (#6341) * [PM-3983] Implementing test for `updateAutofillOverlayCiphers` * [PM-3983] Implementing test for `updateAutofillOverlayCiphers` * [PM-3983] Working through jest tests for overlay background * [PM-3983] Adding jest tests for OverlayBackground * [PM-3983] Adding jest tests for OverlayBackground; * [PM-3983] Adding jest tests for getAuthStatus * [PM-3983] Adding jest tests for getAuthStatus * [PM-3983] Adding jest tests for getTranslations * [PM-3983] Finalizing jest tests for OverlayBackground * [PM-3983] Finalizing jest tests for OverlayBackground * [PM-3982] Updating unit tests within AutofillInit * [PM-3982] Adding jest tests for AutofillOverlayIframeElement, AutofillOverlayButtonIframe, and AutofillOverlayListIframe * [PM-3982] Adding jest tests for the AutofillOverlayIframeService class * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3992] AutofillOverlayContentService class unit tests * [PM-3982] Filling out unit tests for the AutofillService class * [PM-3982] Implementing unit tests for the AutofillOverlayPageElement custom element class * [PM-3982] Updating elements to better allow for testing of the AutofillOverlayList and AutofillOverlayButton classes * [PM-3982] Adding jest tests for AutofillOverlayList custom element class * [PM-3982] Adding jest tests for AutofillOverlayList custom element class * [PM-3982] Adding jest tests for the AutofillOverlayButton custom element class * [PM-3982] Adding jest tests for the AutofillOverlayButton custom element class * [PM-3982] Updating obsolete snapshot * add tests for AutofillOverlayIframeService * [PM-3982] Refactoring * [PM-3982] Refactoring --------- Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com> Co-authored-by: Jonathan Prusik <jprusik@classynemesis.com> --------- Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com> Co-authored-by: Jonathan Prusik <jprusik@classynemesis.com> * [PM-2319] Adjusting implementation for how we open the unlock popout to facilitate skipping the notification * [PM-2319] Adjusting typing information within the OverlayBackground class and fixing issue found within the AutofillOverlayList implementation * [PM-2319] Adjusting JSDOC comment within NotificationBackground * [PM-2319] Refactoring OverlayBackground tests * [PM-2319] Refactoring OverlayBackground tests * [PM-2319] Refactoring JSDOC comments * [PM-2319] Adding jest tests to modified TabsBackground class * [PM-2319] Refactoring jest tests for AutofillInit * [PM-2319] Refactoring AutofillInit JSDOC messages * [PM-2319] Applying refactors to AutofillInit * [PM-2319] Applying refactors to fying info for the AutofillOverlayIframeService * [PM-2319] Adding the ability to apply the extension theme to the overlay elements * [PM-2319] Adjusting background offset on darker themes * [PM-2319] Adjusting background offset on darker themes * [PM-2319] Adding JSDOC comments to the overlay iframe service * [PM-2319] Cleaning up implementation * [PM-2319] Cleaning up implementation * [PM-2319] Adding removal of unknown manifest key, `sandbox`, from the Firefox manifest * [PM-2319] Updating manifest v3 implementation to facilitate presentation of the overlay page elements * [PM-2319] Adding documentation to the changes to BrowserApi * [PM-2855] Removing unnecessary key * [PM-2319] Removing unnecesssary abstraction file * [PM-3035] Reverting changes to package-lock.json * [PM-3035] Reverting changes to package-lock.json * [PM-3035] Reverting added logs --------- Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com> Co-authored-by: Manuel <mr-manuel@outlook.it> Co-authored-by: Jonathan Prusik <jprusik@classynemesis.com> --------- Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com> Co-authored-by: Manuel <mr-manuel@outlook.it> Co-authored-by: Jonathan Prusik <jprusik@classynemesis.com> * [PM-3032] Fixing issue with flashing background on overlay iframe list element * [PM-3032] Modifying how we determine the size of the overlay button element to facilitate smaller scaling on larger sized input elements * [PM-3032] Modifying how load actions are handled within the browser view component to clarify the triggered logic. * [PM-3032] Adjusting implementation to how we trigger copy actions * [PM-3032] Setting copyActions to be a static member of the view component class * [PM-3032] Merging in changes --------- Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com> Co-authored-by: Manuel <mr-manuel@outlook.it> Co-authored-by: Jonathan Prusik <jprusik@classynemesis.com> --------- Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com> Co-authored-by: Manuel <mr-manuel@outlook.it> Co-authored-by: Jonathan Prusik <jprusik@classynemesis.com> * [PM-3914] Fixing issue within Opera where lock and login routes can persist if user opens the extension popout in a new window before locking or logging out * [PM-3914] Setting the extensionUrls that are cheked as a variable outside of the scope fo the openUlockPopout method to ensure it does not have to be rebuilt each time the method is called * [PM-4744] Page Details that Update after Mutation Observer has Triggered Do Not Update within Overlay Background (#6848) * [PM-4743] Windows Chromium Browser is Not Updating Overlay Ciphers on Tab Update (#6863) * [PM-4763] Fixing Issues with the Overlay UI Positioning and Presentation (#6864) * [PM-4763] Fixing overlay UI issues * [PM-4736] Implementing a method to ensure that the overlay is refreshed anytime the overlay has lost visibility * [PM-4763] Implementing a fix for a delayed opening of the overlay element where elements in the documentElement could potentially overlay our own UI element * [PM-4763] Implementing a fix for when the visibility of the dom changes to facilitate removing the overlay element if necessary * [PM-4763] Fixing jest tests * [PM-4763] Fixing global references * [PM-4790] Overlay not resetting on scroll of websites that do not scroll body element (#6877) * [PM-4790] Overlay not resetting on scroll of websites that do not scrollt he body element * [PM-4790] Setting up the scroll event to capture rather than setting mousewheel and touchmove events * [PM-4790] Setting up constants for referenced events * [PM-4229] Fixing issue found when collecting page details * [PM-4229] Implementing optimization to ensure we only rebuild the autofill item if the overlay needs to set the listeners on the field * [PM-4229] Adjusting copy for autofill callout message --------- Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com> Co-authored-by: Manuel <mr-manuel@outlook.it> Co-authored-by: Jonathan Prusik <jprusik@classynemesis.com>
2023-11-20 19:34:04 +01:00
import { mock } from "jest-mock-extended";
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
import { EVENTS } from "../constants";
import { createAutofillFieldMock } from "../jest/autofill-mocks";
import { flushPromises } from "../jest/testing-utils";
import AutofillField from "../models/autofill-field";
import { ElementWithOpId, FormFieldElement } from "../types";
import {
AutofillOverlayElement,
AutofillOverlayVisibility,
RedirectFocusDirection,
} from "../utils/autofill-overlay.enum";
import { AutoFillConstants } from "./autofill-constants";
import AutofillOverlayContentService from "./autofill-overlay-content.service";
const defaultWindowReadyState = document.readyState;
const defaultDocumentVisibilityState = document.visibilityState;
describe("AutofillOverlayContentService", () => {
let autofillOverlayContentService: AutofillOverlayContentService;
let sendExtensionMessageSpy: jest.SpyInstance;
beforeEach(() => {
autofillOverlayContentService = new AutofillOverlayContentService();
sendExtensionMessageSpy = jest
.spyOn(autofillOverlayContentService as any, "sendExtensionMessage")
.mockResolvedValue(undefined);
Object.defineProperty(document, "readyState", {
value: defaultWindowReadyState,
writable: true,
});
Object.defineProperty(document, "visibilityState", {
value: defaultDocumentVisibilityState,
writable: true,
});
Object.defineProperty(document, "activeElement", {
value: null,
writable: true,
});
Object.defineProperty(window, "innerHeight", {
value: 1080,
writable: true,
});
});
afterEach(() => {
jest.clearAllMocks();
});
describe("init", () => {
let setupGlobalEventListenersSpy: jest.SpyInstance;
let setupMutationObserverSpy: jest.SpyInstance;
beforeEach(() => {
jest.spyOn(document, "addEventListener");
jest.spyOn(window, "addEventListener");
setupGlobalEventListenersSpy = jest.spyOn(
autofillOverlayContentService as any,
"setupGlobalEventListeners"
);
setupMutationObserverSpy = jest.spyOn(
autofillOverlayContentService as any,
"setupMutationObserver"
);
});
it("sets up a DOMContentLoaded event listener that triggers setting up the mutation observers", () => {
Object.defineProperty(document, "readyState", {
value: "loading",
writable: true,
});
autofillOverlayContentService.init();
expect(document.addEventListener).toHaveBeenCalledWith(
"DOMContentLoaded",
setupGlobalEventListenersSpy
);
expect(setupGlobalEventListenersSpy).not.toHaveBeenCalled();
});
it("sets up a visibility change listener for the DOM", () => {
const handleVisibilityChangeEventSpy = jest.spyOn(
autofillOverlayContentService as any,
"handleVisibilityChangeEvent"
);
autofillOverlayContentService.init();
expect(document.addEventListener).toHaveBeenCalledWith(
"visibilitychange",
handleVisibilityChangeEventSpy
);
});
it("sets up a focus out listener for the window", () => {
const handleFormFieldBlurEventSpy = jest.spyOn(
autofillOverlayContentService as any,
"handleFormFieldBlurEvent"
);
autofillOverlayContentService.init();
expect(window.addEventListener).toHaveBeenCalledWith("focusout", handleFormFieldBlurEventSpy);
});
it("sets up mutation observers for the body and html element", () => {
jest
.spyOn(globalThis, "MutationObserver")
.mockImplementation(() => mock<MutationObserver>({ observe: jest.fn() }));
const handleOverlayElementMutationObserverUpdateSpy = jest.spyOn(
autofillOverlayContentService as any,
"handleOverlayElementMutationObserverUpdate"
);
const handleBodyElementMutationObserverUpdateSpy = jest.spyOn(
autofillOverlayContentService as any,
"handleBodyElementMutationObserverUpdate"
);
const handleDocumentElementMutationObserverUpdateSpy = jest.spyOn(
autofillOverlayContentService as any,
"handleDocumentElementMutationObserverUpdate"
);
autofillOverlayContentService.init();
expect(setupMutationObserverSpy).toHaveBeenCalledTimes(1);
expect(globalThis.MutationObserver).toHaveBeenNthCalledWith(
1,
handleOverlayElementMutationObserverUpdateSpy
);
expect(globalThis.MutationObserver).toHaveBeenNthCalledWith(
2,
handleBodyElementMutationObserverUpdateSpy
);
expect(globalThis.MutationObserver).toHaveBeenNthCalledWith(
3,
handleDocumentElementMutationObserverUpdateSpy
);
});
});
describe("setupAutofillOverlayListenerOnField", () => {
let autofillFieldElement: ElementWithOpId<FormFieldElement>;
let autofillFieldData: AutofillField;
beforeEach(() => {
document.body.innerHTML = `
<form id="validFormId">
<input type="text" id="username-field" placeholder="username" />
<input type="password" id="password-field" placeholder="password" />
</form>
`;
autofillFieldElement = document.getElementById(
"username-field"
) as ElementWithOpId<FormFieldElement>;
autofillFieldElement.opid = "op-1";
jest.spyOn(autofillFieldElement, "addEventListener");
autofillFieldData = createAutofillFieldMock({
opid: "username-field",
form: "validFormId",
placeholder: "username",
elementNumber: 1,
});
});
describe("skips setup for ignored form fields", () => {
beforeEach(() => {
autofillFieldData = mock<AutofillField>();
});
it("ignores fields that are readonly", () => {
autofillFieldData.readonly = true;
autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalled();
});
it("ignores fields that contain a disabled attribute", () => {
autofillFieldData.disabled = true;
autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalled();
});
it("ignores fields that are not viewable", () => {
autofillFieldData.viewable = false;
autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalled();
});
it("ignores fields that are part of the ExcludedAutofillTypes", () => {
AutoFillConstants.ExcludedAutofillTypes.forEach((excludedType) => {
autofillFieldData.type = excludedType;
autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalled();
});
});
it("ignores fields that contain the keyword `search`", () => {
autofillFieldData.placeholder = "search";
autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalled();
});
it("ignores fields that contain the keyword `captcha` ", () => {
autofillFieldData.placeholder = "captcha";
autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalled();
});
it("ignores fields that do not appear as a login field", () => {
autofillFieldData.placeholder = "not-a-login-field";
autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalled();
});
});
describe("identifies the overlay visibility setting", () => {
it("defaults the overlay visibility setting to `OnFieldFocus` if a value is not set", async () => {
sendExtensionMessageSpy.mockResolvedValueOnce(undefined);
autofillOverlayContentService["autofillOverlayVisibility"] = undefined;
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("getAutofillOverlayVisibility");
expect(autofillOverlayContentService["autofillOverlayVisibility"]).toEqual(
AutofillOverlayVisibility.OnFieldFocus
);
});
it("sets the overlay visibility setting to the value returned from the background script", async () => {
sendExtensionMessageSpy.mockResolvedValueOnce(AutofillOverlayVisibility.OnFieldFocus);
autofillOverlayContentService["autofillOverlayVisibility"] = undefined;
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(autofillOverlayContentService["autofillOverlayVisibility"]).toEqual(
AutofillOverlayVisibility.OnFieldFocus
);
});
});
describe("sets up form field element listeners", () => {
it("removes all cached event listeners from the form field element", async () => {
jest.spyOn(autofillFieldElement, "removeEventListener");
const inputHandler = jest.fn();
const clickHandler = jest.fn();
const focusHandler = jest.fn();
autofillOverlayContentService["eventHandlersMemo"] = {
"op-1-username-field-input-handler": inputHandler,
"op-1-username-field-click-handler": clickHandler,
"op-1-username-field-focus-handler": focusHandler,
};
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(autofillFieldElement.removeEventListener).toHaveBeenNthCalledWith(
1,
"input",
inputHandler
);
expect(autofillFieldElement.removeEventListener).toHaveBeenNthCalledWith(
2,
"click",
clickHandler
);
expect(autofillFieldElement.removeEventListener).toHaveBeenNthCalledWith(
3,
"focus",
focusHandler
);
});
describe("form field blur event listener", () => {
beforeEach(async () => {
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
});
it("updates the isFieldCurrentlyFocused value to false", async () => {
autofillOverlayContentService["isFieldCurrentlyFocused"] = true;
autofillFieldElement.dispatchEvent(new Event("blur"));
expect(autofillOverlayContentService["isFieldCurrentlyFocused"]).toEqual(false);
});
it("sends a message to the background to check if the overlay is focused", () => {
autofillFieldElement.dispatchEvent(new Event("blur"));
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("checkAutofillOverlayFocused");
});
});
describe("form field keyup event listener", () => {
beforeEach(async () => {
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
jest.spyOn(globalThis.customElements, "define").mockImplementation();
});
it("removes the autofill overlay when the `Escape` key is pressed", () => {
jest.spyOn(autofillOverlayContentService as any, "removeAutofillOverlay");
autofillFieldElement.dispatchEvent(new KeyboardEvent("keyup", { code: "Escape" }));
expect(autofillOverlayContentService.removeAutofillOverlay).toHaveBeenCalled();
});
it("repositions the overlay if autofill is not currently filling when the `Enter` key is pressed", () => {
const handleOverlayRepositionEventSpy = jest.spyOn(
autofillOverlayContentService as any,
"handleOverlayRepositionEvent"
);
autofillOverlayContentService["isCurrentlyFilling"] = false;
autofillFieldElement.dispatchEvent(new KeyboardEvent("keyup", { code: "Enter" }));
expect(handleOverlayRepositionEventSpy).toHaveBeenCalled();
});
it("skips repositioning the overlay if autofill is currently filling when the `Enter` key is pressed", () => {
const handleOverlayRepositionEventSpy = jest.spyOn(
autofillOverlayContentService as any,
"handleOverlayRepositionEvent"
);
autofillOverlayContentService["isCurrentlyFilling"] = true;
autofillFieldElement.dispatchEvent(new KeyboardEvent("keyup", { code: "Enter" }));
expect(handleOverlayRepositionEventSpy).not.toHaveBeenCalled();
});
it("opens the overlay list and focuses it after a delay if it is not visible when the `ArrowDown` key is pressed", async () => {
jest.useFakeTimers();
const updateMostRecentlyFocusedFieldSpy = jest.spyOn(
autofillOverlayContentService as any,
"updateMostRecentlyFocusedField"
);
const openAutofillOverlaySpy = jest.spyOn(
autofillOverlayContentService as any,
"openAutofillOverlay"
);
autofillOverlayContentService["isOverlayListVisible"] = false;
autofillFieldElement.dispatchEvent(new KeyboardEvent("keyup", { code: "ArrowDown" }));
await flushPromises();
expect(updateMostRecentlyFocusedFieldSpy).toHaveBeenCalledWith(autofillFieldElement);
expect(openAutofillOverlaySpy).toHaveBeenCalledWith({ isOpeningFullOverlay: true });
expect(sendExtensionMessageSpy).not.toHaveBeenCalledWith("focusAutofillOverlayList");
jest.advanceTimersByTime(150);
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("focusAutofillOverlayList");
});
it("focuses the overlay list when the `ArrowDown` key is pressed", () => {
autofillOverlayContentService["isOverlayListVisible"] = true;
autofillFieldElement.dispatchEvent(new KeyboardEvent("keyup", { code: "ArrowDown" }));
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("focusAutofillOverlayList");
});
});
describe("form field input change event listener", () => {
beforeEach(() => {
jest.spyOn(globalThis.customElements, "define").mockImplementation();
});
it("ignores span elements that trigger the listener", async () => {
const spanAutofillFieldElement = document.createElement(
"span"
) as ElementWithOpId<HTMLSpanElement>;
jest.spyOn(autofillOverlayContentService as any, "storeModifiedFormElement");
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
spanAutofillFieldElement,
autofillFieldData
);
spanAutofillFieldElement.dispatchEvent(new Event("input"));
expect(autofillOverlayContentService["storeModifiedFormElement"]).not.toHaveBeenCalled();
});
it("stores the field as a user filled field if the form field data indicates that it is for a username", async () => {
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("input"));
expect(autofillOverlayContentService["userFilledFields"].username).toEqual(
autofillFieldElement
);
});
it("stores the field as a user filled field if the form field is of type password", async () => {
const passwordFieldElement = document.getElementById(
"password-field"
) as ElementWithOpId<FormFieldElement>;
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
passwordFieldElement,
autofillFieldData
);
passwordFieldElement.dispatchEvent(new Event("input"));
expect(autofillOverlayContentService["userFilledFields"].password).toEqual(
passwordFieldElement
);
});
it("removes the overlay if the form field element has a value and the user is not authed", async () => {
jest.spyOn(autofillOverlayContentService as any, "isUserAuthed").mockReturnValue(false);
const removeAutofillOverlayListSpy = jest.spyOn(
autofillOverlayContentService as any,
"removeAutofillOverlayList"
);
(autofillFieldElement as HTMLInputElement).value = "test";
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("input"));
expect(removeAutofillOverlayListSpy).toHaveBeenCalled();
});
it("removes the overlay if the form field element has a value and the overlay ciphers are populated", async () => {
jest.spyOn(autofillOverlayContentService as any, "isUserAuthed").mockReturnValue(true);
autofillOverlayContentService["isOverlayCiphersPopulated"] = true;
const removeAutofillOverlayListSpy = jest.spyOn(
autofillOverlayContentService as any,
"removeAutofillOverlayList"
);
(autofillFieldElement as HTMLInputElement).value = "test";
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("input"));
expect(removeAutofillOverlayListSpy).toHaveBeenCalled();
});
it("opens the autofill overlay if the form field is empty", async () => {
jest.spyOn(autofillOverlayContentService as any, "openAutofillOverlay");
(autofillFieldElement as HTMLInputElement).value = "";
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("input"));
expect(autofillOverlayContentService["openAutofillOverlay"]).toHaveBeenCalled();
});
it("opens the autofill overlay if the form field is empty and the user is authed", async () => {
jest.spyOn(autofillOverlayContentService as any, "isUserAuthed").mockReturnValue(true);
jest.spyOn(autofillOverlayContentService as any, "openAutofillOverlay");
(autofillFieldElement as HTMLInputElement).value = "";
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("input"));
expect(autofillOverlayContentService["openAutofillOverlay"]).toHaveBeenCalled();
});
it("opens the autofill overlay if the form field is empty and the overlay ciphers are not populated", async () => {
jest.spyOn(autofillOverlayContentService as any, "isUserAuthed").mockReturnValue(false);
autofillOverlayContentService["isOverlayCiphersPopulated"] = false;
jest.spyOn(autofillOverlayContentService as any, "openAutofillOverlay");
(autofillFieldElement as HTMLInputElement).value = "";
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("input"));
expect(autofillOverlayContentService["openAutofillOverlay"]).toHaveBeenCalled();
});
});
describe("form field click event listener", () => {
beforeEach(async () => {
jest
.spyOn(autofillOverlayContentService as any, "triggerFormFieldFocusedAction")
.mockImplementation();
autofillOverlayContentService["isOverlayListVisible"] = false;
autofillOverlayContentService["isOverlayListVisible"] = false;
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
});
it("triggers the field focused handler if the overlay is not visible", async () => {
autofillFieldElement.dispatchEvent(new Event("click"));
expect(autofillOverlayContentService["triggerFormFieldFocusedAction"]).toHaveBeenCalled();
});
it("skips triggering the field focused handler if the overlay list is visible", () => {
autofillOverlayContentService["isOverlayListVisible"] = true;
autofillFieldElement.dispatchEvent(new Event("click"));
expect(
autofillOverlayContentService["triggerFormFieldFocusedAction"]
).not.toHaveBeenCalled();
});
it("skips triggering the field focused handler if the overlay button is visible", () => {
autofillOverlayContentService["isOverlayButtonVisible"] = true;
autofillFieldElement.dispatchEvent(new Event("click"));
expect(
autofillOverlayContentService["triggerFormFieldFocusedAction"]
).not.toHaveBeenCalled();
});
});
describe("form field focus event listener", () => {
let updateMostRecentlyFocusedFieldSpy: jest.SpyInstance;
beforeEach(() => {
jest.spyOn(globalThis.customElements, "define").mockImplementation();
updateMostRecentlyFocusedFieldSpy = jest.spyOn(
autofillOverlayContentService as any,
"updateMostRecentlyFocusedField"
);
autofillOverlayContentService["isCurrentlyFilling"] = false;
});
it("skips triggering the handler logic if autofill is currently filling", async () => {
autofillOverlayContentService["isCurrentlyFilling"] = true;
autofillOverlayContentService["mostRecentlyFocusedField"] = autofillFieldElement;
autofillOverlayContentService["autofillOverlayVisibility"] =
AutofillOverlayVisibility.OnFieldFocus;
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("focus"));
expect(updateMostRecentlyFocusedFieldSpy).not.toHaveBeenCalled();
});
it("updates the most recently focused field", async () => {
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("focus"));
expect(updateMostRecentlyFocusedFieldSpy).toHaveBeenCalledWith(autofillFieldElement);
expect(autofillOverlayContentService["mostRecentlyFocusedField"]).toEqual(
autofillFieldElement
);
});
it("removes the overlay list if the autofill visibility is set to onClick", async () => {
autofillOverlayContentService["overlayListElement"] = document.createElement("div");
autofillOverlayContentService["autofillOverlayVisibility"] =
AutofillOverlayVisibility.OnButtonClick;
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("focus"));
await flushPromises();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", {
overlayElement: "autofill-overlay-list",
});
});
it("removes the overlay list if the form element has a value and the focused field is newly focused", async () => {
autofillOverlayContentService["overlayListElement"] = document.createElement("div");
autofillOverlayContentService["mostRecentlyFocusedField"] = document.createElement(
"input"
) as ElementWithOpId<HTMLInputElement>;
(autofillFieldElement as HTMLInputElement).value = "test";
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("focus"));
await flushPromises();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", {
overlayElement: "autofill-overlay-list",
});
});
it("opens the autofill overlay if the form element has no value", async () => {
autofillOverlayContentService["overlayListElement"] = document.createElement("div");
(autofillFieldElement as HTMLInputElement).value = "";
autofillOverlayContentService["autofillOverlayVisibility"] =
AutofillOverlayVisibility.OnFieldFocus;
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("focus"));
await flushPromises();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("openAutofillOverlay");
});
it("opens the autofill overlay if the overlay ciphers are not populated and the user is authed", async () => {
autofillOverlayContentService["overlayListElement"] = document.createElement("div");
(autofillFieldElement as HTMLInputElement).value = "";
autofillOverlayContentService["autofillOverlayVisibility"] =
AutofillOverlayVisibility.OnFieldFocus;
jest.spyOn(autofillOverlayContentService as any, "isUserAuthed").mockReturnValue(true);
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("focus"));
await flushPromises();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("openAutofillOverlay");
});
it("updates the overlay button position if the focus event is not opening the overlay", async () => {
autofillOverlayContentService["autofillOverlayVisibility"] =
AutofillOverlayVisibility.OnFieldFocus;
(autofillFieldElement as HTMLInputElement).value = "test";
autofillOverlayContentService["isOverlayCiphersPopulated"] = true;
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
autofillFieldElement.dispatchEvent(new Event("focus"));
await flushPromises();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillOverlayPosition", {
overlayElement: AutofillOverlayElement.Button,
});
});
});
});
it("triggers the form field focused handler if the current active element in the document is the passed form field", async () => {
const documentRoot = autofillFieldElement.getRootNode() as Document;
Object.defineProperty(documentRoot, "activeElement", {
value: autofillFieldElement,
writable: true,
});
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("openAutofillOverlay");
expect(autofillOverlayContentService["mostRecentlyFocusedField"]).toEqual(
autofillFieldElement
);
});
it("sets the most recently focused field to the passed form field element if the value is not set", async () => {
autofillOverlayContentService["mostRecentlyFocusedField"] = undefined;
await autofillOverlayContentService.setupAutofillOverlayListenerOnField(
autofillFieldElement,
autofillFieldData
);
expect(autofillOverlayContentService["mostRecentlyFocusedField"]).toEqual(
autofillFieldElement
);
});
});
describe("openAutofillOverlay", () => {
let autofillFieldElement: ElementWithOpId<FormFieldElement>;
beforeEach(() => {
document.body.innerHTML = `
<form id="validFormId">
<input type="text" id="username-field" placeholder="username" />
<input type="password" id="password-field" placeholder="password" />
</form>
`;
autofillFieldElement = document.getElementById(
"username-field"
) as ElementWithOpId<FormFieldElement>;
autofillFieldElement.opid = "op-1";
autofillOverlayContentService["mostRecentlyFocusedField"] = autofillFieldElement;
});
it("skips opening the overlay if a field has not been recently focused", () => {
autofillOverlayContentService["mostRecentlyFocusedField"] = undefined;
autofillOverlayContentService["openAutofillOverlay"]();
expect(sendExtensionMessageSpy).not.toHaveBeenCalled();
});
it("focuses the most recent overlay field if the field is not focused", () => {
jest.spyOn(autofillFieldElement, "getRootNode").mockReturnValue(document);
Object.defineProperty(document, "activeElement", {
value: document.createElement("div"),
writable: true,
});
const focusMostRecentOverlayFieldSpy = jest.spyOn(
autofillOverlayContentService as any,
"focusMostRecentOverlayField"
);
autofillOverlayContentService["openAutofillOverlay"]({ isFocusingFieldElement: true });
expect(focusMostRecentOverlayFieldSpy).toHaveBeenCalled();
});
it("skips focusing the most recent overlay field if the field is already focused", () => {
jest.spyOn(autofillFieldElement, "getRootNode").mockReturnValue(document);
Object.defineProperty(document, "activeElement", {
value: autofillFieldElement,
writable: true,
});
const focusMostRecentOverlayFieldSpy = jest.spyOn(
autofillOverlayContentService as any,
"focusMostRecentOverlayField"
);
autofillOverlayContentService["openAutofillOverlay"]({ isFocusingFieldElement: true });
expect(focusMostRecentOverlayFieldSpy).not.toHaveBeenCalled();
});
it("stores the user's auth status", () => {
autofillOverlayContentService["authStatus"] = undefined;
autofillOverlayContentService["openAutofillOverlay"]({
authStatus: AuthenticationStatus.Unlocked,
});
expect(autofillOverlayContentService["authStatus"]).toEqual(AuthenticationStatus.Unlocked);
});
it("opens both autofill overlay elements", () => {
autofillOverlayContentService["mostRecentlyFocusedField"] = autofillFieldElement;
autofillOverlayContentService["openAutofillOverlay"]();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillOverlayPosition", {
overlayElement: AutofillOverlayElement.Button,
});
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillOverlayPosition", {
overlayElement: AutofillOverlayElement.List,
});
});
it("opens the autofill overlay button only if overlay visibility is set for onButtonClick", () => {
autofillOverlayContentService["autofillOverlayVisibility"] =
AutofillOverlayVisibility.OnButtonClick;
autofillOverlayContentService["openAutofillOverlay"]({ isOpeningFullOverlay: false });
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillOverlayPosition", {
overlayElement: AutofillOverlayElement.Button,
});
expect(sendExtensionMessageSpy).not.toHaveBeenCalledWith("updateAutofillOverlayPosition", {
overlayElement: AutofillOverlayElement.List,
});
});
it("overrides the onButtonClick visibility setting to open both overlay elements", () => {
autofillOverlayContentService["autofillOverlayVisibility"] =
AutofillOverlayVisibility.OnButtonClick;
autofillOverlayContentService["openAutofillOverlay"]({ isOpeningFullOverlay: true });
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillOverlayPosition", {
overlayElement: AutofillOverlayElement.Button,
});
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillOverlayPosition", {
overlayElement: AutofillOverlayElement.List,
});
});
it("sends an extension message requesting an re-collection of page details if they need to update", () => {
jest.spyOn(autofillOverlayContentService as any, "sendExtensionMessage");
autofillOverlayContentService.pageDetailsUpdateRequired = true;
autofillOverlayContentService["openAutofillOverlay"]();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("bgCollectPageDetails", {
sender: "autofillOverlayContentService",
});
});
});
describe("focusMostRecentOverlayField", () => {
it("focuses the most recently focused overlay field", () => {
const mostRecentlyFocusedField = document.createElement(
"input"
) as ElementWithOpId<HTMLInputElement>;
autofillOverlayContentService["mostRecentlyFocusedField"] = mostRecentlyFocusedField;
jest.spyOn(mostRecentlyFocusedField, "focus");
autofillOverlayContentService["focusMostRecentOverlayField"]();
expect(mostRecentlyFocusedField.focus).toHaveBeenCalled();
});
});
describe("blurMostRecentOverlayField", () => {
it("removes focus from the most recently focused overlay field", () => {
const mostRecentlyFocusedField = document.createElement(
"input"
) as ElementWithOpId<HTMLInputElement>;
autofillOverlayContentService["mostRecentlyFocusedField"] = mostRecentlyFocusedField;
jest.spyOn(mostRecentlyFocusedField, "blur");
autofillOverlayContentService["blurMostRecentOverlayField"]();
expect(mostRecentlyFocusedField.blur).toHaveBeenCalled();
});
});
describe("removeAutofillOverlay", () => {
it("disconnects the body's mutation observer", () => {
const bodyMutationObserver = mock<MutationObserver>();
autofillOverlayContentService["bodyElementMutationObserver"] = bodyMutationObserver;
autofillOverlayContentService.removeAutofillOverlay();
expect(bodyMutationObserver.disconnect).toHaveBeenCalled();
});
});
describe("removeAutofillOverlayButton", () => {
beforeEach(() => {
document.body.innerHTML = `<div class="overlay-button"></div>`;
autofillOverlayContentService["overlayButtonElement"] = document.querySelector(
".overlay-button"
) as HTMLElement;
});
it("removes the overlay button from the DOM", () => {
const overlayButtonElement = document.querySelector(".overlay-button") as HTMLElement;
autofillOverlayContentService["isOverlayButtonVisible"] = true;
autofillOverlayContentService.removeAutofillOverlay();
expect(autofillOverlayContentService["isOverlayButtonVisible"]).toEqual(false);
expect(document.body.contains(overlayButtonElement)).toEqual(false);
});
it("sends a message to the background indicating that the overlay button has been closed", () => {
autofillOverlayContentService.removeAutofillOverlay();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", {
overlayElement: AutofillOverlayElement.Button,
});
});
it("removes the overlay reposition event listeners", () => {
jest.spyOn(globalThis.document.body, "removeEventListener");
jest.spyOn(globalThis, "removeEventListener");
const handleOverlayRepositionEventSpy = jest.spyOn(
autofillOverlayContentService as any,
"handleOverlayRepositionEvent"
);
autofillOverlayContentService.removeAutofillOverlay();
expect(globalThis.removeEventListener).toHaveBeenCalledWith(
EVENTS.SCROLL,
handleOverlayRepositionEventSpy,
{
capture: true,
}
);
expect(globalThis.removeEventListener).toHaveBeenCalledWith(
EVENTS.RESIZE,
handleOverlayRepositionEventSpy
);
});
});
describe("removeAutofillOverlayList", () => {
beforeEach(() => {
document.body.innerHTML = `<div class="overlay-list"></div>`;
autofillOverlayContentService["overlayListElement"] = document.querySelector(
".overlay-list"
) as HTMLElement;
});
it("removes the overlay list element from the dom", () => {
const overlayListElement = document.querySelector(".overlay-list") as HTMLElement;
autofillOverlayContentService["isOverlayListVisible"] = true;
autofillOverlayContentService.removeAutofillOverlay();
expect(autofillOverlayContentService["isOverlayListVisible"]).toEqual(false);
expect(document.body.contains(overlayListElement)).toEqual(false);
});
it("sends a message to the extension background indicating that the overlay list has closed", () => {
autofillOverlayContentService.removeAutofillOverlay();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", {
overlayElement: AutofillOverlayElement.List,
});
});
});
describe("addNewVaultItem", () => {
it("skips sending the message if the overlay list is not visible", () => {
autofillOverlayContentService["isOverlayListVisible"] = false;
autofillOverlayContentService.addNewVaultItem();
expect(sendExtensionMessageSpy).not.toHaveBeenCalled();
});
it("sends a message that facilitates adding a new vault item with empty fields", () => {
autofillOverlayContentService["isOverlayListVisible"] = true;
autofillOverlayContentService.addNewVaultItem();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayAddNewVaultItem", {
login: {
username: "",
password: "",
uri: "http://localhost/",
hostname: "localhost",
},
});
});
it("sends a message that facilitates adding a new vault item with data from user filled fields", () => {
document.body.innerHTML = `
<form id="validFormId">
<input type="text" id="username-field" placeholder="username" />
<input type="password" id="password-field" placeholder="password" />
</form>
`;
const usernameField = document.getElementById(
"username-field"
) as ElementWithOpId<HTMLInputElement>;
const passwordField = document.getElementById(
"password-field"
) as ElementWithOpId<HTMLInputElement>;
usernameField.value = "test-username";
passwordField.value = "test-password";
autofillOverlayContentService["isOverlayListVisible"] = true;
autofillOverlayContentService["userFilledFields"] = {
username: usernameField,
password: passwordField,
};
autofillOverlayContentService.addNewVaultItem();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayAddNewVaultItem", {
login: {
username: "test-username",
password: "test-password",
uri: "http://localhost/",
hostname: "localhost",
},
});
});
});
describe("redirectOverlayFocusOut", () => {
let autofillFieldElement: ElementWithOpId<FormFieldElement>;
let autofillFieldFocusSpy: jest.SpyInstance;
let findTabsSpy: jest.SpyInstance;
let previousFocusableElement: HTMLElement;
let nextFocusableElement: HTMLElement;
beforeEach(() => {
document.body.innerHTML = `
<div class="previous-focusable-element" tabindex="0"></div>
<form id="validFormId">
<input type="text" id="username-field" placeholder="username" />
<input type="password" id="password-field" placeholder="password" />
</form>
<div class="next-focusable-element" tabindex="0"></div>
`;
autofillFieldElement = document.getElementById(
"username-field"
) as ElementWithOpId<FormFieldElement>;
autofillFieldElement.opid = "op-1";
previousFocusableElement = document.querySelector(
".previous-focusable-element"
) as HTMLElement;
nextFocusableElement = document.querySelector(".next-focusable-element") as HTMLElement;
autofillFieldFocusSpy = jest.spyOn(autofillFieldElement, "focus");
findTabsSpy = jest.spyOn(autofillOverlayContentService as any, "findTabs");
autofillOverlayContentService["isOverlayListVisible"] = true;
autofillOverlayContentService["mostRecentlyFocusedField"] = autofillFieldElement;
autofillOverlayContentService["focusableElements"] = [
previousFocusableElement,
autofillFieldElement,
nextFocusableElement,
];
});
it("skips focusing an element if the overlay is not visible", () => {
autofillOverlayContentService["isOverlayListVisible"] = false;
autofillOverlayContentService.redirectOverlayFocusOut(RedirectFocusDirection.Next);
expect(findTabsSpy).not.toHaveBeenCalled();
});
it("skips focusing an element if no recently focused field exists", () => {
autofillOverlayContentService["mostRecentlyFocusedField"] = undefined;
autofillOverlayContentService.redirectOverlayFocusOut(RedirectFocusDirection.Next);
expect(findTabsSpy).not.toHaveBeenCalled();
});
it("focuses the most recently focused field if the focus direction is `Current`", () => {
autofillOverlayContentService.redirectOverlayFocusOut(RedirectFocusDirection.Current);
expect(findTabsSpy).not.toHaveBeenCalled();
expect(autofillFieldFocusSpy).toHaveBeenCalled();
});
it("removes the overlay if the focus direction is `Current`", () => {
jest.useFakeTimers();
const removeAutofillOverlaySpy = jest.spyOn(
autofillOverlayContentService as any,
"removeAutofillOverlay"
);
autofillOverlayContentService.redirectOverlayFocusOut(RedirectFocusDirection.Current);
jest.advanceTimersByTime(150);
expect(removeAutofillOverlaySpy).toHaveBeenCalled();
});
it("finds all focusable tabs if the focusable elements array is not populated", () => {
autofillOverlayContentService["focusableElements"] = [];
findTabsSpy.mockReturnValue([
previousFocusableElement,
autofillFieldElement,
nextFocusableElement,
]);
autofillOverlayContentService.redirectOverlayFocusOut(RedirectFocusDirection.Next);
expect(findTabsSpy).toHaveBeenCalledWith(globalThis.document.body, { getShadowRoot: true });
});
it("focuses the previous focusable element if the focus direction is `Previous`", () => {
jest.spyOn(previousFocusableElement, "focus");
autofillOverlayContentService.redirectOverlayFocusOut(RedirectFocusDirection.Previous);
expect(autofillFieldFocusSpy).not.toHaveBeenCalled();
expect(previousFocusableElement.focus).toHaveBeenCalled();
});
it("focuses the next focusable element if the focus direction is `Next`", () => {
jest.spyOn(nextFocusableElement, "focus");
autofillOverlayContentService.redirectOverlayFocusOut(RedirectFocusDirection.Next);
expect(autofillFieldFocusSpy).not.toHaveBeenCalled();
expect(nextFocusableElement.focus).toHaveBeenCalled();
});
});
describe("handleOverlayRepositionEvent", () => {
beforeEach(() => {
document.body.innerHTML = `
<form id="validFormId">
<input type="text" id="username-field" placeholder="username" />
<input type="password" id="password-field" placeholder="password" />
</form>
`;
const usernameField = document.getElementById(
"username-field"
) as ElementWithOpId<HTMLInputElement>;
autofillOverlayContentService["mostRecentlyFocusedField"] = usernameField;
autofillOverlayContentService["setOverlayRepositionEventListeners"]();
autofillOverlayContentService["isOverlayButtonVisible"] = true;
autofillOverlayContentService["isOverlayListVisible"] = true;
jest
.spyOn(autofillOverlayContentService as any, "recentlyFocusedFieldIsCurrentlyFocused")
.mockReturnValue(true);
});
it("skips handling the overlay reposition event if the overlay button and list elements are not visible", () => {
autofillOverlayContentService["isOverlayButtonVisible"] = false;
autofillOverlayContentService["isOverlayListVisible"] = false;
globalThis.dispatchEvent(new Event(EVENTS.RESIZE));
expect(sendExtensionMessageSpy).not.toHaveBeenCalled();
});
it("hides the overlay elements", () => {
globalThis.dispatchEvent(new Event(EVENTS.SCROLL));
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillOverlayHidden", {
display: "none",
});
expect(autofillOverlayContentService["isOverlayButtonVisible"]).toEqual(false);
expect(autofillOverlayContentService["isOverlayListVisible"]).toEqual(false);
});
it("clears the user interaction timeout", () => {
jest.useFakeTimers();
const clearTimeoutSpy = jest.spyOn(globalThis, "clearTimeout");
autofillOverlayContentService["userInteractionEventTimeout"] = setTimeout(jest.fn(), 123);
globalThis.dispatchEvent(new Event(EVENTS.SCROLL));
expect(clearTimeoutSpy).toHaveBeenCalledWith(expect.anything());
});
it("removes the overlay completely if the field is not focused", () => {
jest.useFakeTimers();
jest
.spyOn(autofillOverlayContentService as any, "recentlyFocusedFieldIsCurrentlyFocused")
.mockReturnValue(false);
const removeAutofillOverlaySpy = jest.spyOn(
autofillOverlayContentService as any,
"removeAutofillOverlay"
);
autofillOverlayContentService["mostRecentlyFocusedField"] = undefined;
globalThis.dispatchEvent(new Event(EVENTS.SCROLL));
jest.advanceTimersByTime(800);
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillOverlayHidden", {
display: "block",
});
expect(autofillOverlayContentService["isOverlayButtonVisible"]).toEqual(true);
expect(autofillOverlayContentService["isOverlayListVisible"]).toEqual(true);
expect(removeAutofillOverlaySpy).toHaveBeenCalled();
});
it("updates the overlay position if the most recently focused field is still within the viewport", async () => {
jest.useFakeTimers();
jest
.spyOn(autofillOverlayContentService as any, "updateMostRecentlyFocusedField")
.mockImplementation(() => {
autofillOverlayContentService["focusedFieldData"] = {
focusedFieldRects: {
top: 100,
},
focusedFieldStyles: {},
};
});
const clearUserInteractionEventTimeoutSpy = jest.spyOn(
autofillOverlayContentService as any,
"clearUserInteractionEventTimeout"
);
globalThis.dispatchEvent(new Event(EVENTS.SCROLL));
jest.advanceTimersByTime(800);
await flushPromises();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillOverlayPosition", {
overlayElement: AutofillOverlayElement.Button,
});
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillOverlayPosition", {
overlayElement: AutofillOverlayElement.List,
});
expect(clearUserInteractionEventTimeoutSpy).toHaveBeenCalled();
});
it("removes the autofill overlay if the focused field is outside of the viewport", async () => {
jest.useFakeTimers();
jest
.spyOn(autofillOverlayContentService as any, "updateMostRecentlyFocusedField")
.mockImplementation(() => {
autofillOverlayContentService["focusedFieldData"] = {
focusedFieldRects: {
top: 4000,
},
focusedFieldStyles: {},
};
});
const removeAutofillOverlaySpy = jest.spyOn(
autofillOverlayContentService as any,
"removeAutofillOverlay"
);
globalThis.dispatchEvent(new Event(EVENTS.SCROLL));
jest.advanceTimersByTime(800);
await flushPromises();
expect(removeAutofillOverlaySpy).toHaveBeenCalled();
});
});
describe("handleOverlayElementMutationObserverUpdate", () => {
let usernameField: ElementWithOpId<HTMLInputElement>;
beforeEach(() => {
document.body.innerHTML = `
<form id="validFormId">
<input type="text" id="username-field" placeholder="username" />
<input type="password" id="password-field" placeholder="password" />
</form>
`;
usernameField = document.getElementById(
"username-field"
) as ElementWithOpId<HTMLInputElement>;
usernameField.style.setProperty("display", "block", "important");
jest.spyOn(usernameField, "removeAttribute");
jest.spyOn(usernameField.style, "setProperty");
jest
.spyOn(
autofillOverlayContentService as any,
"isTriggeringExcessiveMutationObserverIterations"
)
.mockReturnValue(false);
});
it("skips handling the mutation if excessive mutation observer events are triggered", () => {
jest
.spyOn(
autofillOverlayContentService as any,
"isTriggeringExcessiveMutationObserverIterations"
)
.mockReturnValue(true);
autofillOverlayContentService["handleOverlayElementMutationObserverUpdate"]([
mock<MutationRecord>({
target: usernameField,
}),
]);
expect(usernameField.removeAttribute).not.toHaveBeenCalled();
});
it("skips handling the mutation if the record type is not for `attributes`", () => {
autofillOverlayContentService["handleOverlayElementMutationObserverUpdate"]([
mock<MutationRecord>({
target: usernameField,
type: "childList",
}),
]);
expect(usernameField.removeAttribute).not.toHaveBeenCalled();
});
it("removes all element attributes that are not the style attribute", () => {
autofillOverlayContentService["handleOverlayElementMutationObserverUpdate"]([
mock<MutationRecord>({
target: usernameField,
type: "attributes",
attributeName: "placeholder",
}),
]);
expect(usernameField.removeAttribute).toHaveBeenCalledWith("placeholder");
});
it("removes all attached style attributes and sets the default styles", () => {
autofillOverlayContentService["handleOverlayElementMutationObserverUpdate"]([
mock<MutationRecord>({
target: usernameField,
type: "attributes",
attributeName: "style",
}),
]);
expect(usernameField.removeAttribute).toHaveBeenCalledWith("style");
expect(usernameField.style.setProperty).toHaveBeenCalledWith("all", "initial", "important");
expect(usernameField.style.setProperty).toHaveBeenCalledWith(
"position",
"fixed",
"important"
);
expect(usernameField.style.setProperty).toHaveBeenCalledWith("display", "block", "important");
});
});
describe("handleBodyElementMutationObserverUpdate", () => {
let overlayButtonElement: HTMLElement;
let overlayListElement: HTMLElement;
beforeEach(() => {
document.body.innerHTML = `
<div class="overlay-button"></div>
<div class="overlay-list"></div>
`;
overlayButtonElement = document.querySelector(".overlay-button") as HTMLElement;
overlayListElement = document.querySelector(".overlay-list") as HTMLElement;
autofillOverlayContentService["overlayButtonElement"] = overlayButtonElement;
autofillOverlayContentService["overlayListElement"] = overlayListElement;
autofillOverlayContentService["isOverlayListVisible"] = true;
jest.spyOn(globalThis.document.body, "insertBefore");
jest
.spyOn(
autofillOverlayContentService as any,
"isTriggeringExcessiveMutationObserverIterations"
)
.mockReturnValue(false);
});
it("skips handling the mutation if the overlay elements are not present in the DOM", () => {
autofillOverlayContentService["overlayButtonElement"] = undefined;
autofillOverlayContentService["overlayListElement"] = undefined;
autofillOverlayContentService["handleBodyElementMutationObserverUpdate"]();
expect(globalThis.document.body.insertBefore).not.toHaveBeenCalled();
});
it("skips handling the mutation if excessive mutations are being triggered", () => {
jest
.spyOn(
autofillOverlayContentService as any,
"isTriggeringExcessiveMutationObserverIterations"
)
.mockReturnValue(true);
autofillOverlayContentService["handleBodyElementMutationObserverUpdate"]();
expect(globalThis.document.body.insertBefore).not.toHaveBeenCalled();
});
it("skips re-arranging the DOM elements if the last child of the body is the overlay list and the second to last child of the body is the overlay button", () => {
autofillOverlayContentService["handleBodyElementMutationObserverUpdate"]();
expect(globalThis.document.body.insertBefore).not.toHaveBeenCalled();
});
it("skips re-arranging the DOM elements if the last child is the overlay button and the overlay list is not visible", () => {
overlayListElement.remove();
autofillOverlayContentService["isOverlayListVisible"] = false;
autofillOverlayContentService["handleBodyElementMutationObserverUpdate"]();
expect(globalThis.document.body.insertBefore).not.toHaveBeenCalled();
});
it("positions the overlay button before the overlay list if an element has inserted itself after the button element", () => {
const injectedElement = document.createElement("div");
document.body.insertBefore(injectedElement, overlayListElement);
autofillOverlayContentService["handleBodyElementMutationObserverUpdate"]();
expect(globalThis.document.body.insertBefore).toHaveBeenCalledWith(
overlayButtonElement,
overlayListElement
);
});
it("positions the overlay button before the overlay list if the elements have inserted in incorrect order", () => {
document.body.appendChild(overlayButtonElement);
autofillOverlayContentService["handleBodyElementMutationObserverUpdate"]();
expect(globalThis.document.body.insertBefore).toHaveBeenCalledWith(
overlayButtonElement,
overlayListElement
);
});
it("positions the last child before the overlay button if it is not the overlay list", () => {
const injectedElement = document.createElement("div");
document.body.appendChild(injectedElement);
autofillOverlayContentService["handleBodyElementMutationObserverUpdate"]();
expect(globalThis.document.body.insertBefore).toHaveBeenCalledWith(
injectedElement,
overlayButtonElement
);
});
});
describe("handleDocumentElementMutationObserverUpdate", () => {
let overlayButtonElement: HTMLElement;
let overlayListElement: HTMLElement;
beforeEach(() => {
document.body.innerHTML = `
<div class="overlay-button"></div>
<div class="overlay-list"></div>
`;
document.head.innerHTML = `<title>test</title>`;
overlayButtonElement = document.querySelector(".overlay-button") as HTMLElement;
overlayListElement = document.querySelector(".overlay-list") as HTMLElement;
autofillOverlayContentService["overlayButtonElement"] = overlayButtonElement;
autofillOverlayContentService["overlayListElement"] = overlayListElement;
autofillOverlayContentService["isOverlayListVisible"] = true;
jest.spyOn(globalThis.document.body, "appendChild");
jest
.spyOn(
autofillOverlayContentService as any,
"isTriggeringExcessiveMutationObserverIterations"
)
.mockReturnValue(false);
});
it("skips modification of the DOM if the overlay button and list elements are not present", () => {
autofillOverlayContentService["overlayButtonElement"] = undefined;
autofillOverlayContentService["overlayListElement"] = undefined;
autofillOverlayContentService["handleDocumentElementMutationObserverUpdate"]([
mock<MutationRecord>(),
]);
expect(globalThis.document.body.appendChild).not.toHaveBeenCalled();
});
it("skips modification of the DOM if excessive mutation events are being triggered", () => {
jest
.spyOn(
autofillOverlayContentService as any,
"isTriggeringExcessiveMutationObserverIterations"
)
.mockReturnValue(true);
autofillOverlayContentService["handleDocumentElementMutationObserverUpdate"]([
mock<MutationRecord>(),
]);
expect(globalThis.document.body.appendChild).not.toHaveBeenCalled();
});
it("ignores the mutation record if the record is not of type `childlist`", () => {
autofillOverlayContentService["handleDocumentElementMutationObserverUpdate"]([
mock<MutationRecord>({
type: "attributes",
}),
]);
expect(globalThis.document.body.appendChild).not.toHaveBeenCalled();
});
it("ignores the mutation record if the record does not contain any added nodes", () => {
autofillOverlayContentService["handleDocumentElementMutationObserverUpdate"]([
mock<MutationRecord>({
type: "childList",
addedNodes: mock<NodeList>({ length: 0 }),
}),
]);
expect(globalThis.document.body.appendChild).not.toHaveBeenCalled();
});
it("ignores mutations for the document body and head", () => {
autofillOverlayContentService["handleDocumentElementMutationObserverUpdate"]([
{
type: "childList",
addedNodes: document.querySelectorAll("body, head"),
} as unknown as MutationRecord,
]);
expect(globalThis.document.body.appendChild).not.toHaveBeenCalled();
});
it("appends the identified node to the body", async () => {
jest.useFakeTimers();
const injectedElement = document.createElement("div");
injectedElement.id = "test";
document.documentElement.appendChild(injectedElement);
autofillOverlayContentService["handleDocumentElementMutationObserverUpdate"]([
{
type: "childList",
addedNodes: document.querySelectorAll("#test"),
} as unknown as MutationRecord,
]);
jest.advanceTimersByTime(10);
expect(globalThis.document.body.appendChild).toHaveBeenCalledWith(injectedElement);
});
});
describe("isTriggeringExcessiveMutationObserverIterations", () => {
it("clears any existing reset timeout", () => {
jest.useFakeTimers();
const clearTimeoutSpy = jest.spyOn(globalThis, "clearTimeout");
autofillOverlayContentService["mutationObserverIterationsResetTimeout"] = setTimeout(
jest.fn(),
123
);
autofillOverlayContentService["isTriggeringExcessiveMutationObserverIterations"]();
expect(clearTimeoutSpy).toHaveBeenCalledWith(expect.anything());
});
it("will reset the number of mutationObserverIterations after two seconds", () => {
jest.useFakeTimers();
autofillOverlayContentService["mutationObserverIterations"] = 10;
autofillOverlayContentService["isTriggeringExcessiveMutationObserverIterations"]();
jest.advanceTimersByTime(2000);
expect(autofillOverlayContentService["mutationObserverIterations"]).toEqual(0);
});
it("will blur the overlay field and remove the autofill overlay if excessive mutation observer iterations are triggering", async () => {
autofillOverlayContentService["mutationObserverIterations"] = 101;
const blurMostRecentOverlayFieldSpy = jest.spyOn(
autofillOverlayContentService as any,
"blurMostRecentOverlayField"
);
const removeAutofillOverlaySpy = jest.spyOn(
autofillOverlayContentService as any,
"removeAutofillOverlay"
);
autofillOverlayContentService["isTriggeringExcessiveMutationObserverIterations"]();
await flushPromises();
expect(blurMostRecentOverlayFieldSpy).toHaveBeenCalled();
expect(removeAutofillOverlaySpy).toHaveBeenCalled();
});
});
describe("handleVisibilityChangeEvent", () => {
it("skips removing the overlay if the document is visible", () => {
jest.spyOn(autofillOverlayContentService as any, "removeAutofillOverlay");
autofillOverlayContentService["handleVisibilityChangeEvent"]();
expect(autofillOverlayContentService["removeAutofillOverlay"]).not.toHaveBeenCalled();
});
it("removes the overlay if the document is not visible", () => {
Object.defineProperty(document, "visibilityState", {
value: "hidden",
writable: true,
});
jest.spyOn(autofillOverlayContentService as any, "removeAutofillOverlay");
autofillOverlayContentService["handleVisibilityChangeEvent"]();
expect(autofillOverlayContentService["removeAutofillOverlay"]).toHaveBeenCalled();
});
});
});