From bfa1bbc73fa7c541cc87031a806b5fb66cc9bbe6 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Wed, 13 May 2020 21:16:55 +0200 Subject: [PATCH] refactor(platform): change deprecated APIs for version 10 Changes the APIs that were deprecated for v10 in the `cdk/platform` module. BREAKING CHANGES: * The `_platformId` parameter in the `Platform` constructor is now required. --- src/cdk/a11y/focus-trap/focus-trap.spec.ts | 6 +- .../high-contrast-mode-detector.spec.ts | 7 +- .../interactivity-checker.spec.ts | 270 +++++++++++------- src/cdk/platform/platform.ts | 7 +- .../ng-update/data/constructor-checks.ts | 6 + .../mdc-input/input.spec.ts | 13 +- .../mdc-slider/slider.spec.ts | 8 +- src/material/checkbox/testing/shared.spec.ts | 3 +- src/material/input/input.spec.ts | 18 +- src/material/radio/testing/shared.spec.ts | 3 +- .../slide-toggle/testing/shared.spec.ts | 3 +- tools/public_api_guard/cdk/platform.d.ts | 4 +- 12 files changed, 212 insertions(+), 136 deletions(-) diff --git a/src/cdk/a11y/focus-trap/focus-trap.spec.ts b/src/cdk/a11y/focus-trap/focus-trap.spec.ts index 4abfb9ab3ce7..b0ec9a9a317c 100644 --- a/src/cdk/a11y/focus-trap/focus-trap.spec.ts +++ b/src/cdk/a11y/focus-trap/focus-trap.spec.ts @@ -1,5 +1,5 @@ import {Platform} from '@angular/cdk/platform'; -import {Component, PLATFORM_ID, ViewChild} from '@angular/core'; +import {Component, ViewChild} from '@angular/core'; import {async, ComponentFixture, TestBed} from '@angular/core/testing'; import {A11yModule, FocusTrap, CdkTrapFocus} from '../index'; @@ -48,9 +48,9 @@ describe('FocusTrap', () => { // focus event handler directly. const result = focusTrapInstance.focusLastTabbableElement(); - const platformId = TestBed.inject(PLATFORM_ID); + const platform = TestBed.inject(Platform); // In iOS button elements are never tabbable, so the last element will be the input. - const lastElement = new Platform(platformId).IOS ? 'input' : 'button'; + const lastElement = platform.IOS ? 'input' : 'button'; expect(document.activeElement!.nodeName.toLowerCase()) .toBe(lastElement, `Expected ${lastElement} element to be focused`); diff --git a/src/cdk/a11y/high-contrast-mode/high-contrast-mode-detector.spec.ts b/src/cdk/a11y/high-contrast-mode/high-contrast-mode-detector.spec.ts index 82bd797f5e49..fa80bfc6a0e2 100644 --- a/src/cdk/a11y/high-contrast-mode/high-contrast-mode-detector.spec.ts +++ b/src/cdk/a11y/high-contrast-mode/high-contrast-mode-detector.spec.ts @@ -5,14 +5,15 @@ import { HighContrastModeDetector, WHITE_ON_BLACK_CSS_CLASS, } from './high-contrast-mode-detector'; import {Platform} from '@angular/cdk/platform'; +import {inject} from '@angular/core/testing'; describe('HighContrastModeDetector', () => { let fakePlatform: Platform; - beforeEach(() => { - fakePlatform = new Platform(); - }); + beforeEach(inject([Platform], (p: Platform) => { + fakePlatform = p; + })); it('should detect NONE for non-browser platforms', () => { fakePlatform.isBrowser = false; diff --git a/src/cdk/a11y/interactivity-checker/interactivity-checker.spec.ts b/src/cdk/a11y/interactivity-checker/interactivity-checker.spec.ts index 02801cc76b98..e663603949eb 100644 --- a/src/cdk/a11y/interactivity-checker/interactivity-checker.spec.ts +++ b/src/cdk/a11y/interactivity-checker/interactivity-checker.spec.ts @@ -1,18 +1,18 @@ import {Platform} from '@angular/cdk/platform'; +import {inject} from '@angular/core/testing'; import {InteractivityChecker} from './interactivity-checker'; describe('InteractivityChecker', () => { - const platform: Platform = new Platform(); - + let platform: Platform; let testContainerElement: HTMLElement; let checker: InteractivityChecker; - beforeEach(() => { + beforeEach(inject([Platform, InteractivityChecker], (p: Platform, i: InteractivityChecker) => { testContainerElement = document.createElement('div'); document.body.appendChild(testContainerElement); - - checker = new InteractivityChecker(platform); - }); + platform = p; + checker = i; + })); afterEach(() => { document.body.removeChild(testContainerElement); @@ -21,7 +21,7 @@ describe('InteractivityChecker', () => { describe('isDisabled', () => { it('should return true for disabled elements', () => { - let elements = createElements('input', 'textarea', 'select', 'button', 'mat-checkbox'); + const elements = createElements('input', 'textarea', 'select', 'button', 'mat-checkbox'); elements.forEach(el => el.setAttribute('disabled', '')); appendElements(elements); @@ -32,7 +32,7 @@ describe('InteractivityChecker', () => { }); it('should return false for elements without disabled', () => { - let elements = createElements('input', 'textarea', 'select', 'button', 'mat-checkbox'); + const elements = createElements('input', 'textarea', 'select', 'button', 'mat-checkbox'); appendElements(elements); elements.forEach(el => { @@ -46,7 +46,7 @@ describe('InteractivityChecker', () => { it('should return false for a `display: none` element', () => { testContainerElement.innerHTML = ``; - let input = testContainerElement.querySelector('input') as HTMLElement; + const input = testContainerElement.querySelector('input') as HTMLElement; expect(checker.isVisible(input)) .toBe(false, 'Expected element with `display: none` to not be visible'); @@ -57,7 +57,7 @@ describe('InteractivityChecker', () => { `
`; - let input = testContainerElement.querySelector('input') as HTMLElement; + const input = testContainerElement.querySelector('input') as HTMLElement; expect(checker.isVisible(input)) .toBe(false, 'Expected element with `display: none` parent to not be visible'); @@ -66,7 +66,7 @@ describe('InteractivityChecker', () => { it('should return false for a `visibility: hidden` element', () => { testContainerElement.innerHTML = ``; - let input = testContainerElement.querySelector('input') as HTMLElement; + const input = testContainerElement.querySelector('input') as HTMLElement; expect(checker.isVisible(input)) .toBe(false, 'Expected element with `visibility: hidden` to not be visible'); @@ -77,7 +77,7 @@ describe('InteractivityChecker', () => { `
`; - let input = testContainerElement.querySelector('input') as HTMLElement; + const input = testContainerElement.querySelector('input') as HTMLElement; expect(checker.isVisible(input)) .toBe(false, 'Expected element with `visibility: hidden` parent to not be visible'); @@ -91,7 +91,7 @@ describe('InteractivityChecker', () => { `; - let input = testContainerElement.querySelector('input') as HTMLElement; + const input = testContainerElement.querySelector('input') as HTMLElement; expect(checker.isVisible(input)) .toBe(true, 'Expected element with `visibility: hidden` ancestor and closer ' + @@ -99,7 +99,7 @@ describe('InteractivityChecker', () => { }); it('should return true for an element without visibility modifiers', () => { - let input = document.createElement('input'); + const input = document.createElement('input'); testContainerElement.appendChild(input); expect(checker.isVisible(input)) @@ -109,7 +109,7 @@ describe('InteractivityChecker', () => { describe('isFocusable', () => { it('should return true for native form controls', () => { - let elements = createElements('input', 'textarea', 'select', 'button'); + const elements = createElements('input', 'textarea', 'select', 'button'); appendElements(elements); elements.forEach(el => { @@ -118,7 +118,7 @@ describe('InteractivityChecker', () => { }); it('should return true for an anchor with an href', () => { - let anchor = document.createElement('a'); + const anchor = document.createElement('a'); anchor.href = 'google.com'; testContainerElement.appendChild(anchor); @@ -126,7 +126,7 @@ describe('InteractivityChecker', () => { }); it('should return false for an anchor without an href', () => { - let anchor = document.createElement('a'); + const anchor = document.createElement('a'); testContainerElement.appendChild(anchor); expect(checker.isFocusable(anchor)) @@ -134,7 +134,7 @@ describe('InteractivityChecker', () => { }); it('should return false for disabled form controls', () => { - let elements = createElements('input', 'textarea', 'select', 'button'); + const elements = createElements('input', 'textarea', 'select', 'button'); elements.forEach(el => el.setAttribute('disabled', '')); appendElements(elements); @@ -147,7 +147,7 @@ describe('InteractivityChecker', () => { it('should return false for a `display: none` element', () => { testContainerElement.innerHTML = ``; - let input = testContainerElement.querySelector('input') as HTMLElement; + const input = testContainerElement.querySelector('input') as HTMLElement; expect(checker.isFocusable(input)) .toBe(false, 'Expected element with `display: none` to not be visible'); @@ -158,7 +158,7 @@ describe('InteractivityChecker', () => { `
`; - let input = testContainerElement.querySelector('input') as HTMLElement; + const input = testContainerElement.querySelector('input') as HTMLElement; expect(checker.isFocusable(input)) .toBe(false, 'Expected element with `display: none` parent to not be visible'); @@ -167,7 +167,7 @@ describe('InteractivityChecker', () => { it('should return false for a `visibility: hidden` element', () => { testContainerElement.innerHTML = ``; - let input = testContainerElement.querySelector('input') as HTMLElement; + const input = testContainerElement.querySelector('input') as HTMLElement; expect(checker.isFocusable(input)) .toBe(false, 'Expected element with `visibility: hidden` not to be focusable'); @@ -178,7 +178,7 @@ describe('InteractivityChecker', () => { `
`; - let input = testContainerElement.querySelector('input') as HTMLElement; + const input = testContainerElement.querySelector('input') as HTMLElement; expect(checker.isFocusable(input)) .toBe(false, 'Expected element with `visibility: hidden` parent not to be focusable'); @@ -192,7 +192,7 @@ describe('InteractivityChecker', () => { `; - let input = testContainerElement.querySelector('input') as HTMLElement; + const input = testContainerElement.querySelector('input') as HTMLElement; expect(checker.isFocusable(input)) .toBe(true, 'Expected element with `visibility: hidden` ancestor and closer ' + @@ -200,7 +200,7 @@ describe('InteractivityChecker', () => { }); it('should return false for an element with an empty tabindex', () => { - let element = document.createElement('div'); + const element = document.createElement('div'); element.setAttribute('tabindex', ''); testContainerElement.appendChild(element); @@ -209,7 +209,7 @@ describe('InteractivityChecker', () => { }); it('should return false for an element with a non-numeric tabindex', () => { - let element = document.createElement('div'); + const element = document.createElement('div'); element.setAttribute('tabindex', 'abba'); testContainerElement.appendChild(element); @@ -218,7 +218,7 @@ describe('InteractivityChecker', () => { }); it('should return true for an element with contenteditable', () => { - let element = document.createElement('div'); + const element = document.createElement('div'); element.setAttribute('contenteditable', ''); testContainerElement.appendChild(element); @@ -228,7 +228,7 @@ describe('InteractivityChecker', () => { it('should return false for inert div and span', () => { - let elements = createElements('div', 'span'); + const elements = createElements('div', 'span'); appendElements(elements); elements.forEach(el => { @@ -242,40 +242,53 @@ describe('InteractivityChecker', () => { describe('isTabbable', () => { - it('should respect the tabindex for video elements with controls', + it('should respect the tabindex for video elements with controls', () => { // Do not run for Blink, Firefox and iOS because those treat video elements // with controls different and are covered in other tests. - runIf(!platform.BLINK && !platform.FIREFOX && !platform.IOS, () => { - let video = createFromTemplate('