2016-06-23 16:47:54 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2016-06-08 23:38:52 +00:00
|
|
|
import {GetTestability, Injectable, Testability, TestabilityRegistry, setTestabilityGetter} from '@angular/core';
|
2016-04-29 00:50:03 +00:00
|
|
|
|
2016-06-08 23:38:52 +00:00
|
|
|
import {getDOM} from '../dom/dom_adapter';
|
2016-05-31 22:22:59 +00:00
|
|
|
import {ListWrapper} from '../facade/collection';
|
|
|
|
|
import {global, isPresent} from '../facade/lang';
|
2016-04-29 00:50:03 +00:00
|
|
|
|
2015-09-02 22:19:26 +00:00
|
|
|
export class BrowserGetTestability implements GetTestability {
|
|
|
|
|
static init() { setTestabilityGetter(new BrowserGetTestability()); }
|
|
|
|
|
|
|
|
|
|
addToWindow(registry: TestabilityRegistry): void {
|
2015-11-17 23:24:36 +00:00
|
|
|
global.getAngularTestability = (elem: any, findInAncestors: boolean = true) => {
|
2015-10-28 07:59:19 +00:00
|
|
|
var testability = registry.findTestabilityInTree(elem, findInAncestors);
|
|
|
|
|
if (testability == null) {
|
|
|
|
|
throw new Error('Could not find testability for element.');
|
|
|
|
|
}
|
2016-08-29 20:08:28 +00:00
|
|
|
return testability;
|
2015-10-28 07:59:19 +00:00
|
|
|
};
|
2015-11-17 23:24:36 +00:00
|
|
|
|
2016-08-29 20:08:28 +00:00
|
|
|
global.getAllAngularTestabilities = () => { return registry.getAllTestabilities(); };
|
2016-01-05 20:56:24 +00:00
|
|
|
|
2016-02-18 21:51:20 +00:00
|
|
|
global.getAllAngularRootElements = () => registry.getAllRootElements();
|
|
|
|
|
|
2016-06-08 22:45:15 +00:00
|
|
|
var whenAllStable = (callback: any /** TODO #9100 */) => {
|
2016-01-05 20:56:24 +00:00
|
|
|
var testabilities = global.getAllAngularTestabilities();
|
|
|
|
|
var count = testabilities.length;
|
|
|
|
|
var didWork = false;
|
2016-06-08 22:45:15 +00:00
|
|
|
var decrement = function(didWork_: any /** TODO #9100 */) {
|
2016-01-05 20:56:24 +00:00
|
|
|
didWork = didWork || didWork_;
|
|
|
|
|
count--;
|
|
|
|
|
if (count == 0) {
|
|
|
|
|
callback(didWork);
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-06-08 23:38:52 +00:00
|
|
|
testabilities.forEach(function(testability: any /** TODO #9100 */) {
|
|
|
|
|
testability.whenStable(decrement);
|
|
|
|
|
});
|
2016-01-05 20:56:24 +00:00
|
|
|
};
|
|
|
|
|
|
2016-08-25 18:12:06 +00:00
|
|
|
if (!global['frameworkStabilizers']) {
|
|
|
|
|
global['frameworkStabilizers'] = ListWrapper.createGrowableSize(0);
|
2016-01-05 20:56:24 +00:00
|
|
|
}
|
2016-08-25 18:12:06 +00:00
|
|
|
global['frameworkStabilizers'].push(whenAllStable);
|
2015-03-23 23:46:18 +00:00
|
|
|
}
|
2015-11-17 23:24:36 +00:00
|
|
|
|
2016-06-08 23:38:52 +00:00
|
|
|
findTestabilityInTree(registry: TestabilityRegistry, elem: any, findInAncestors: boolean):
|
|
|
|
|
Testability {
|
2015-11-17 23:24:36 +00:00
|
|
|
if (elem == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var t = registry.getTestability(elem);
|
|
|
|
|
if (isPresent(t)) {
|
|
|
|
|
return t;
|
|
|
|
|
} else if (!findInAncestors) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-04-29 00:50:03 +00:00
|
|
|
if (getDOM().isShadowRoot(elem)) {
|
|
|
|
|
return this.findTestabilityInTree(registry, getDOM().getHost(elem), true);
|
2015-11-17 23:24:36 +00:00
|
|
|
}
|
2016-04-29 00:50:03 +00:00
|
|
|
return this.findTestabilityInTree(registry, getDOM().parentElement(elem), true);
|
2015-11-17 23:24:36 +00:00
|
|
|
}
|
2015-03-23 23:46:18 +00:00
|
|
|
}
|