|
| 1 | +import { |
| 2 | + BLACK_ON_WHITE_CSS_CLASS, |
| 3 | + HIGH_CONTRAST_MODE_ACTIVE_CSS_CLASS, |
| 4 | + HighContrastMode, |
| 5 | + HighContrastModeDetector, WHITE_ON_BLACK_CSS_CLASS, |
| 6 | +} from './high-contrast-mode-detector'; |
| 7 | +import {Platform} from '@angular/cdk/platform'; |
| 8 | + |
| 9 | + |
| 10 | +describe('HighContrastModeDetector', () => { |
| 11 | + let fakePlatform: Platform; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + fakePlatform = new Platform(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should detect NONE for non-browser platforms', () => { |
| 18 | + fakePlatform.isBrowser = false; |
| 19 | + const detector = new HighContrastModeDetector(fakePlatform, {}); |
| 20 | + expect(detector.getHighContrastMode()) |
| 21 | + .toBe(HighContrastMode.NONE, 'Expected high-contrast mode `NONE` on non-browser platforms'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should not apply any css classes for non-browser platforms', () => { |
| 25 | + fakePlatform.isBrowser = false; |
| 26 | + const fakeDocument = getFakeDocument(''); |
| 27 | + const detector = new HighContrastModeDetector(fakePlatform, fakeDocument); |
| 28 | + detector._applyBodyHighContrastModeCssClasses(); |
| 29 | + expect(fakeDocument.body.className) |
| 30 | + .toBe('', 'Expected body not to have any CSS classes in non-browser platforms'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should detect WHITE_ON_BLACK when backgrounds are coerced to black', () => { |
| 34 | + const detector = new HighContrastModeDetector(fakePlatform, getFakeDocument('rgb(0,0,0)')); |
| 35 | + expect(detector.getHighContrastMode()) |
| 36 | + .toBe(HighContrastMode.WHITE_ON_BLACK, 'Expected high-contrast mode `WHITE_ON_BLACK`'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should detect BLACK_ON_WHITE when backgrounds are coerced to white ', () => { |
| 40 | + const detector = new HighContrastModeDetector(fakePlatform, getFakeDocument('rgb(1,1,1)')); |
| 41 | + expect(detector.getHighContrastMode()) |
| 42 | + .toBe(HighContrastMode.BLACK_ON_WHITE, 'Expected high-contrast mode `BLACK_ON_WHITE`'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should detect NONE when backgrounds are not coerced ', () => { |
| 46 | + const detector = new HighContrastModeDetector(fakePlatform, getFakeDocument('rgb(1,2,3)')); |
| 47 | + expect(detector.getHighContrastMode()) |
| 48 | + .toBe(HighContrastMode.NONE, 'Expected high-contrast mode `NONE`'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should apply css classes for BLACK_ON_WHITE high-contrast mode', () => { |
| 52 | + const fakeDocument = getFakeDocument('rgb(1,1,1)'); |
| 53 | + const detector = new HighContrastModeDetector(fakePlatform, fakeDocument); |
| 54 | + detector._applyBodyHighContrastModeCssClasses(); |
| 55 | + expect(fakeDocument.body.classList).toContain(HIGH_CONTRAST_MODE_ACTIVE_CSS_CLASS); |
| 56 | + expect(fakeDocument.body.classList).toContain(BLACK_ON_WHITE_CSS_CLASS); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should apply css classes for WHITE_ON_BLACK high-contrast mode', () => { |
| 60 | + const fakeDocument = getFakeDocument('rgb(0,0,0)'); |
| 61 | + const detector = new HighContrastModeDetector(fakePlatform, fakeDocument); |
| 62 | + detector._applyBodyHighContrastModeCssClasses(); |
| 63 | + expect(fakeDocument.body.classList).toContain(HIGH_CONTRAST_MODE_ACTIVE_CSS_CLASS); |
| 64 | + expect(fakeDocument.body.classList).toContain(WHITE_ON_BLACK_CSS_CLASS); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should not apply any css classes when backgrounds are not coerced', () => { |
| 68 | + const fakeDocument = getFakeDocument(''); |
| 69 | + const detector = new HighContrastModeDetector(fakePlatform, fakeDocument); |
| 70 | + detector._applyBodyHighContrastModeCssClasses(); |
| 71 | + expect(fakeDocument.body.className) |
| 72 | + .toBe('', 'Expected body not to have any CSS classes in non-browser platforms'); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | + |
| 77 | +/** Gets a fake document that includes a fake `window.getComputedStyle` implementation. */ |
| 78 | +function getFakeDocument(fakeComputedBackgroundColor: string) { |
| 79 | + return { |
| 80 | + body: document.createElement('body'), |
| 81 | + createElement: (tag: string) => document.createElement(tag), |
| 82 | + defaultView: { |
| 83 | + getComputedStyle: () => ({backgroundColor: fakeComputedBackgroundColor}), |
| 84 | + }, |
| 85 | + }; |
| 86 | +} |
0 commit comments