2016-06-23 09:47:54 -07: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 16:38:52 -07:00
|
|
|
import {GetTestability, Injectable, Testability, TestabilityRegistry, setTestabilityGetter} from '@angular/core';
|
2016-04-28 17:50:03 -07:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
import {getDOM} from '../dom/dom_adapter';
|
2016-05-31 15:22:59 -07:00
|
|
|
import {ListWrapper} from '../facade/collection';
|
|
|
|
import {global, isPresent} from '../facade/lang';
|
2016-04-28 17:50:03 -07:00
|
|
|
|
|
|
|
|
2015-03-23 16:46:18 -07:00
|
|
|
|
|
|
|
class PublicTestability {
|
2015-10-28 15:04:55 -07:00
|
|
|
/** @internal */
|
2015-05-20 09:48:15 -07:00
|
|
|
_testability: Testability;
|
2015-03-23 16:46:18 -07:00
|
|
|
|
2015-05-20 09:48:15 -07:00
|
|
|
constructor(testability: Testability) { this._testability = testability; }
|
2015-03-23 16:46:18 -07:00
|
|
|
|
2015-08-27 17:44:59 +02:00
|
|
|
isStable(): boolean { return this._testability.isStable(); }
|
|
|
|
|
2015-05-20 09:48:15 -07:00
|
|
|
whenStable(callback: Function) { this._testability.whenStable(callback); }
|
2015-03-23 16:46:18 -07:00
|
|
|
|
2015-10-10 22:11:13 -07:00
|
|
|
findBindings(using: any, provider: string, exactMatch: boolean): any[] {
|
|
|
|
return this.findProviders(using, provider, exactMatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
findProviders(using: any, provider: string, exactMatch: boolean): any[] {
|
|
|
|
return this._testability.findBindings(using, provider, exactMatch);
|
2015-03-23 16:46:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 15:19:26 -07:00
|
|
|
export class BrowserGetTestability implements GetTestability {
|
|
|
|
static init() { setTestabilityGetter(new BrowserGetTestability()); }
|
|
|
|
|
|
|
|
addToWindow(registry: TestabilityRegistry): void {
|
2015-11-17 15:24:36 -08:00
|
|
|
global.getAngularTestability = (elem: any, findInAncestors: boolean = true) => {
|
2015-10-28 08:59:19 +01:00
|
|
|
var testability = registry.findTestabilityInTree(elem, findInAncestors);
|
|
|
|
if (testability == null) {
|
|
|
|
throw new Error('Could not find testability for element.');
|
|
|
|
}
|
|
|
|
return new PublicTestability(testability);
|
|
|
|
};
|
2015-11-17 15:24:36 -08:00
|
|
|
|
|
|
|
global.getAllAngularTestabilities = () => {
|
2015-07-30 15:51:06 -07:00
|
|
|
var testabilities = registry.getAllTestabilities();
|
|
|
|
return testabilities.map((testability) => { return new PublicTestability(testability); });
|
|
|
|
};
|
2016-01-05 12:56:24 -08:00
|
|
|
|
2016-02-18 13:51:20 -08:00
|
|
|
global.getAllAngularRootElements = () => registry.getAllRootElements();
|
|
|
|
|
2016-06-08 15:45:15 -07:00
|
|
|
var whenAllStable = (callback: any /** TODO #9100 */) => {
|
2016-01-05 12:56:24 -08:00
|
|
|
var testabilities = global.getAllAngularTestabilities();
|
|
|
|
var count = testabilities.length;
|
|
|
|
var didWork = false;
|
2016-06-08 15:45:15 -07:00
|
|
|
var 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
|
|
|
};
|
|
|
|
|
|
|
|
if (!global.frameworkStabilizers) {
|
|
|
|
global.frameworkStabilizers = ListWrapper.createGrowableSize(0);
|
|
|
|
}
|
|
|
|
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):
|
|
|
|
Testability {
|
2015-11-17 15:24:36 -08:00
|
|
|
if (elem == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var t = registry.getTestability(elem);
|
|
|
|
if (isPresent(t)) {
|
|
|
|
return t;
|
|
|
|
} else if (!findInAncestors) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-04-28 17:50:03 -07:00
|
|
|
if (getDOM().isShadowRoot(elem)) {
|
|
|
|
return this.findTestabilityInTree(registry, getDOM().getHost(elem), true);
|
2015-11-17 15:24:36 -08:00
|
|
|
}
|
2016-04-28 17:50:03 -07:00
|
|
|
return this.findTestabilityInTree(registry, getDOM().parentElement(elem), true);
|
2015-11-17 15:24:36 -08:00
|
|
|
}
|
2015-03-23 16:46:18 -07:00
|
|
|
}
|