2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-23 09:47:54 -07:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-08-22 19:16:25 -07:00
|
|
|
import {ɵgetDOM as getDOM} from '@angular/common';
|
2020-04-13 16:40:21 -07:00
|
|
|
import {GetTestability, setTestabilityGetter, Testability, TestabilityRegistry, ɵglobal as global} from '@angular/core';
|
2016-04-28 17:50:03 -07:00
|
|
|
|
2015-09-02 15:19:26 -07:00
|
|
|
export class BrowserGetTestability implements GetTestability {
|
2020-04-13 16:40:21 -07:00
|
|
|
static init() {
|
|
|
|
setTestabilityGetter(new BrowserGetTestability());
|
|
|
|
}
|
2015-09-02 15:19:26 -07:00
|
|
|
|
|
|
|
addToWindow(registry: TestabilityRegistry): void {
|
2017-03-01 15:18:10 -08:00
|
|
|
global['getAngularTestability'] = (elem: any, findInAncestors: boolean = true) => {
|
2016-11-12 14:08:58 +01:00
|
|
|
const testability = registry.findTestabilityInTree(elem, findInAncestors);
|
2015-10-28 08:59:19 +01:00
|
|
|
if (testability == null) {
|
|
|
|
throw new Error('Could not find testability for element.');
|
|
|
|
}
|
2016-08-29 13:08:28 -07:00
|
|
|
return testability;
|
2015-10-28 08:59:19 +01:00
|
|
|
};
|
2015-11-17 15:24:36 -08:00
|
|
|
|
2017-03-01 15:18:10 -08:00
|
|
|
global['getAllAngularTestabilities'] = () => registry.getAllTestabilities();
|
2016-01-05 12:56:24 -08:00
|
|
|
|
2017-03-01 15:18:10 -08:00
|
|
|
global['getAllAngularRootElements'] = () => registry.getAllRootElements();
|
2016-02-18 13:51:20 -08:00
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const whenAllStable = (callback: any /** TODO #9100 */) => {
|
2017-03-01 15:18:10 -08:00
|
|
|
const testabilities = global['getAllAngularTestabilities']();
|
2016-11-12 14:08:58 +01:00
|
|
|
let count = testabilities.length;
|
|
|
|
let didWork = false;
|
|
|
|
const decrement = function(didWork_: any /** TODO #9100 */) {
|
2016-01-05 12:56:24 -08:00
|
|
|
didWork = didWork || didWork_;
|
|
|
|
count--;
|
|
|
|
if (count == 0) {
|
|
|
|
callback(didWork);
|
|
|
|
}
|
|
|
|
};
|
2016-06-08 16:38:52 -07:00
|
|
|
testabilities.forEach(function(testability: any /** TODO #9100 */) {
|
|
|
|
testability.whenStable(decrement);
|
|
|
|
});
|
2016-01-05 12:56:24 -08:00
|
|
|
};
|
|
|
|
|
2016-08-25 11:12:06 -07:00
|
|
|
if (!global['frameworkStabilizers']) {
|
2016-10-21 15:14:44 -07:00
|
|
|
global['frameworkStabilizers'] = [];
|
2016-01-05 12:56:24 -08:00
|
|
|
}
|
2016-08-25 11:12:06 -07:00
|
|
|
global['frameworkStabilizers'].push(whenAllStable);
|
2015-03-23 16:46:18 -07:00
|
|
|
}
|
2015-11-17 15:24:36 -08:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
findTestabilityInTree(registry: TestabilityRegistry, elem: any, findInAncestors: boolean):
|
2017-03-24 09:59:41 -07:00
|
|
|
Testability|null {
|
2015-11-17 15:24:36 -08:00
|
|
|
if (elem == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-11-12 14:08:58 +01:00
|
|
|
const t = registry.getTestability(elem);
|
2017-03-02 09:37:01 -08:00
|
|
|
if (t != null) {
|
2015-11-17 15:24:36 -08:00
|
|
|
return t;
|
|
|
|
} else if (!findInAncestors) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-04-28 17:50:03 -07:00
|
|
|
if (getDOM().isShadowRoot(elem)) {
|
2019-08-30 11:31:24 -07:00
|
|
|
return this.findTestabilityInTree(registry, (<any>elem).host, true);
|
2015-11-17 15:24:36 -08:00
|
|
|
}
|
2019-08-30 11:31:24 -07:00
|
|
|
return this.findTestabilityInTree(registry, elem.parentElement, true);
|
2015-11-17 15:24:36 -08:00
|
|
|
}
|
2015-03-23 16:46:18 -07:00
|
|
|
}
|