refactor(test_lib): BrowserDetection util

Closes #3805
This commit is contained in:
Marc Laval
2015-08-24 15:41:36 +02:00
parent 551d9a1688
commit be07390859
8 changed files with 223 additions and 38 deletions

View File

@ -22,6 +22,42 @@ export class Log {
result(): string { return ListWrapper.join(this._result, "; "); }
}
export class BrowserDetection {
private _ua: string;
constructor(ua: string) {
if (isPresent(ua)) {
this._ua = ua;
} else {
this._ua = isPresent(DOM) ? DOM.getUserAgent() : '';
}
}
get isFirefox(): boolean { return this._ua.indexOf('Firefox') > -1; }
get isAndroid(): boolean {
return this._ua.indexOf('Mozilla/5.0') > -1 && this._ua.indexOf('Android') > -1 &&
this._ua.indexOf('AppleWebKit') > -1 && this._ua.indexOf('Chrome') == -1;
}
get isEdge(): boolean { return this._ua.indexOf('Edge') > -1; }
get isIE(): boolean { return this._ua.indexOf('Trident') > -1; }
get isWebkit(): boolean {
return this._ua.indexOf('AppleWebKit') > -1 && this._ua.indexOf('Edge') == -1;
}
// The Intl API is only properly supported in recent Chrome and Opera.
// Note: Edge is disguised as Chrome 42, so checking the "Edge" part is needed,
// see https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
get supportsIntlApi(): boolean {
return this._ua.indexOf('Chrome/4') > -1 && this._ua.indexOf('Edge') == -1;
}
}
export var browserDetection = new BrowserDetection(null);
export function dispatchEvent(element, eventType) {
DOM.dispatchEvent(element, DOM.createEvent(eventType));
}
@ -92,26 +128,3 @@ export function stringifyElement(el): string {
return result;
}
// The Intl API is only properly supported in recent Chrome and Opera.
// Note: Edge is disguised as Chrome 42, so checking the "Edge" part is needed,
// see https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
export function supportsIntlApi(): boolean {
return DOM.getUserAgent().indexOf('Chrome/4') > -1 && DOM.getUserAgent().indexOf('Edge') == -1;
}
// TODO(mlaval): extract all browser detection checks from all tests
export function isFirefox(): boolean {
return DOM.getUserAgent().indexOf("Firefox") > -1;
}
export function isAndroid(): boolean {
var ua = DOM.getUserAgent();
return ua.indexOf('Mozilla/5.0') > -1 && ua.indexOf('Android ') > -1 &&
ua.indexOf('AppleWebKit') > -1 && ua.indexOf('Chrome') == -1;
}
export function isEdge(): boolean {
return DOM.getUserAgent().indexOf('Edge') > -1;
}
export function isIE(): boolean {
return DOM.getUserAgent().indexOf('Trident') > -1;
}