@@ -4,26 +4,16 @@ import {StyleModule} from './index';
4
4
import { By } from '@angular/platform-browser' ;
5
5
import { TAB } from '../keyboard/keycodes' ;
6
6
import { FocusOriginMonitor } from './focus-classes' ;
7
- import { PlatformModule } from '../platform/index' ;
8
- import { Platform } from '../platform/platform' ;
9
7
10
-
11
- // NOTE: Firefox only fires focus & blur events when it is the currently active window.
12
- // This is not always the case on our CI setup, therefore we disable tests that depend on these
13
- // events firing for Firefox. We may be able to fix this by configuring our CI to start Firefox with
14
- // the following preference: focusmanager.testmode = true
15
-
16
-
17
- describe ( 'FocusOriginMonitor' , ( ) => {
8
+ fdescribe ( 'FocusOriginMonitor' , ( ) => {
18
9
let fixture : ComponentFixture < PlainButton > ;
19
10
let buttonElement : HTMLElement ;
20
11
let buttonRenderer : Renderer ;
21
12
let focusOriginMonitor : FocusOriginMonitor ;
22
- let platform : Platform ;
23
13
24
14
beforeEach ( async ( ( ) => {
25
15
TestBed . configureTestingModule ( {
26
- imports : [ StyleModule , PlatformModule ] ,
16
+ imports : [ StyleModule ] ,
27
17
declarations : [
28
18
PlainButton ,
29
19
] ,
@@ -32,21 +22,27 @@ describe('FocusOriginMonitor', () => {
32
22
TestBed . compileComponents ( ) ;
33
23
} ) ) ;
34
24
35
- beforeEach ( inject ( [ FocusOriginMonitor , Platform ] , ( fom : FocusOriginMonitor , pfm : Platform ) => {
25
+ beforeEach ( inject ( [ FocusOriginMonitor ] , ( fom : FocusOriginMonitor ) => {
36
26
fixture = TestBed . createComponent ( PlainButton ) ;
37
27
fixture . detectChanges ( ) ;
38
28
39
29
buttonElement = fixture . debugElement . query ( By . css ( 'button' ) ) . nativeElement ;
40
30
buttonRenderer = fixture . componentInstance . renderer ;
41
31
focusOriginMonitor = fom ;
42
- platform = pfm ;
43
32
44
33
focusOriginMonitor . registerElementForFocusClasses ( buttonElement , buttonRenderer ) ;
34
+
35
+ // On Saucelabs, browsers will run simultaneously and therefore can't focus all browser windows
36
+ // at the same time. This is problematic when testing focus states. Chrome and Firefox
37
+ // only fire FocusEvents when the window is focused. This issue also appears locally.
38
+ let _nativeButtonFocus = buttonElement . focus . bind ( buttonElement ) ;
39
+ buttonElement . focus = ( ) => {
40
+ document . hasFocus ( ) ? _nativeButtonFocus ( ) : dispatchFocusEvent ( buttonElement ) ;
41
+ } ;
42
+
45
43
} ) ) ;
46
44
47
45
it ( 'manually registered element should receive focus classes' , async ( ( ) => {
48
- if ( platform . FIREFOX ) { return ; }
49
-
50
46
buttonElement . focus ( ) ;
51
47
fixture . detectChanges ( ) ;
52
48
@@ -59,8 +55,6 @@ describe('FocusOriginMonitor', () => {
59
55
} ) ) ;
60
56
61
57
it ( 'should detect focus via keyboard' , async ( ( ) => {
62
- if ( platform . FIREFOX ) { return ; }
63
-
64
58
// Simulate focus via keyboard.
65
59
dispatchKeydownEvent ( document , TAB ) ;
66
60
buttonElement . focus ( ) ;
@@ -79,8 +73,6 @@ describe('FocusOriginMonitor', () => {
79
73
} ) ) ;
80
74
81
75
it ( 'should detect focus via mouse' , async ( ( ) => {
82
- if ( platform . FIREFOX ) { return ; }
83
-
84
76
// Simulate focus via mouse.
85
77
dispatchMousedownEvent ( document ) ;
86
78
buttonElement . focus ( ) ;
@@ -99,8 +91,6 @@ describe('FocusOriginMonitor', () => {
99
91
} ) ) ;
100
92
101
93
it ( 'should detect programmatic focus' , async ( ( ) => {
102
- if ( platform . FIREFOX ) { return ; }
103
-
104
94
// Programmatically focus.
105
95
buttonElement . focus ( ) ;
106
96
fixture . detectChanges ( ) ;
@@ -118,8 +108,6 @@ describe('FocusOriginMonitor', () => {
118
108
} ) ) ;
119
109
120
110
it ( 'focusVia keyboard should simulate keyboard focus' , async ( ( ) => {
121
- if ( platform . FIREFOX ) { return ; }
122
-
123
111
focusOriginMonitor . focusVia ( buttonElement , buttonRenderer , 'keyboard' ) ;
124
112
fixture . detectChanges ( ) ;
125
113
@@ -136,8 +124,6 @@ describe('FocusOriginMonitor', () => {
136
124
} ) ) ;
137
125
138
126
it ( 'focusVia mouse should simulate mouse focus' , async ( ( ) => {
139
- if ( platform . FIREFOX ) { return ; }
140
-
141
127
focusOriginMonitor . focusVia ( buttonElement , buttonRenderer , 'mouse' ) ;
142
128
fixture . detectChanges ( ) ;
143
129
@@ -154,8 +140,6 @@ describe('FocusOriginMonitor', () => {
154
140
} ) ) ;
155
141
156
142
it ( 'focusVia program should simulate programmatic focus' , async ( ( ) => {
157
- if ( platform . FIREFOX ) { return ; }
158
-
159
143
focusOriginMonitor . focusVia ( buttonElement , buttonRenderer , 'program' ) ;
160
144
fixture . detectChanges ( ) ;
161
145
@@ -176,11 +160,10 @@ describe('FocusOriginMonitor', () => {
176
160
describe ( 'cdkFocusClasses' , ( ) => {
177
161
let fixture : ComponentFixture < ButtonWithFocusClasses > ;
178
162
let buttonElement : HTMLElement ;
179
- let platform : Platform ;
180
163
181
164
beforeEach ( async ( ( ) => {
182
165
TestBed . configureTestingModule ( {
183
- imports : [ StyleModule , PlatformModule ] ,
166
+ imports : [ StyleModule ] ,
184
167
declarations : [
185
168
ButtonWithFocusClasses ,
186
169
] ,
@@ -189,21 +172,18 @@ describe('cdkFocusClasses', () => {
189
172
TestBed . compileComponents ( ) ;
190
173
} ) ) ;
191
174
192
- beforeEach ( inject ( [ Platform ] , ( pfm : Platform ) => {
175
+ beforeEach ( ( ) => {
193
176
fixture = TestBed . createComponent ( ButtonWithFocusClasses ) ;
194
177
fixture . detectChanges ( ) ;
195
178
196
179
buttonElement = fixture . debugElement . query ( By . css ( 'button' ) ) . nativeElement ;
197
- platform = pfm ;
198
- } ) ) ;
180
+ } ) ;
199
181
200
182
it ( 'should initially not be focused' , ( ) => {
201
183
expect ( buttonElement . classList . length ) . toBe ( 0 , 'button should not have focus classes' ) ;
202
184
} ) ;
203
185
204
186
it ( 'should detect focus via keyboard' , async ( ( ) => {
205
- if ( platform . FIREFOX ) { return ; }
206
-
207
187
// Simulate focus via keyboard.
208
188
dispatchKeydownEvent ( document , TAB ) ;
209
189
buttonElement . focus ( ) ;
@@ -222,8 +202,6 @@ describe('cdkFocusClasses', () => {
222
202
} ) ) ;
223
203
224
204
it ( 'should detect focus via mouse' , async ( ( ) => {
225
- if ( platform . FIREFOX ) { return ; }
226
-
227
205
// Simulate focus via mouse.
228
206
dispatchMousedownEvent ( document ) ;
229
207
buttonElement . focus ( ) ;
@@ -242,8 +220,6 @@ describe('cdkFocusClasses', () => {
242
220
} ) ) ;
243
221
244
222
it ( 'should detect programmatic focus' , async ( ( ) => {
245
- if ( platform . FIREFOX ) { return ; }
246
-
247
223
// Programmatically focus.
248
224
buttonElement . focus ( ) ;
249
225
fixture . detectChanges ( ) ;
@@ -291,3 +267,10 @@ function dispatchKeydownEvent(element: Node, keyCode: number) {
291
267
} ) ;
292
268
element . dispatchEvent ( event ) ;
293
269
}
270
+
271
+ /** Dispatches a focus event on the specified element. */
272
+ function dispatchFocusEvent ( element : Node , type = 'focus' ) {
273
+ let event = document . createEvent ( 'Event' ) ;
274
+ event . initEvent ( type , true , true ) ;
275
+ element . dispatchEvent ( event ) ;
276
+ }
0 commit comments