diff --git a/modules/angular2/src/test_lib/utils.ts b/modules/angular2/src/test_lib/utils.ts index f7a7f9da7b..7a10fca7fd 100644 --- a/modules/angular2/src/test_lib/utils.ts +++ b/modules/angular2/src/test_lib/utils.ts @@ -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; -} diff --git a/modules/angular2/test/core/render/dom/compiler/shadow_css_spec.ts b/modules/angular2/test/core/render/dom/compiler/shadow_css_spec.ts index c60e7430c7..72a992abcd 100644 --- a/modules/angular2/test/core/render/dom/compiler/shadow_css_spec.ts +++ b/modules/angular2/test/core/render/dom/compiler/shadow_css_spec.ts @@ -7,7 +7,8 @@ import { iit, SpyObject, el, - normalizeCSS + normalizeCSS, + browserDetection } from 'angular2/test_lib'; import {ShadowCss} from 'angular2/src/core/render/dom/compiler/shadow_css'; @@ -66,8 +67,7 @@ export function main() { }); } - if (DOM.getUserAgent().indexOf('AppleWebKit') > -1 && - DOM.getUserAgent().indexOf('Edge') == -1) { + if (browserDetection.isWebkit) { it('should handle -webkit-keyframes rules', () => { var css = '@-webkit-keyframes foo {0% {-webkit-transform: translate(-50%) scaleX(0);}}'; var passRe = diff --git a/modules/angular2/test/core/zone/ng_zone_spec.ts b/modules/angular2/test/core/zone/ng_zone_spec.ts index 250bfba080..51729dd811 100644 --- a/modules/angular2/test/core/zone/ng_zone_spec.ts +++ b/modules/angular2/test/core/zone/ng_zone_spec.ts @@ -11,9 +11,7 @@ import { xit, Log, isInInnerZone, - isAndroid, - isEdge, - isIE + browserDetection } from 'angular2/test_lib'; import {PromiseCompleter, PromiseWrapper, TimerWrapper} from 'angular2/src/core/facade/async'; @@ -21,7 +19,8 @@ import {BaseException} from 'angular2/src/core/facade/lang'; import {NgZone} from 'angular2/src/core/zone/ng_zone'; -var needsLongerTimers = isAndroid() || isEdge() || isIE(); +var needsLongerTimers = + browserDetection.isAndroid || browserDetection.isEdge || browserDetection.isIE; // Schedules a macrotask (using a timer) function macroTask(fn: Function, timer = 1): void { // adds longer timers for passing tests in IE and Edge diff --git a/modules/angular2/test/forms/integration_spec.ts b/modules/angular2/test/forms/integration_spec.ts index a6dc8b6315..7ff6891d08 100644 --- a/modules/angular2/test/forms/integration_spec.ts +++ b/modules/angular2/test/forms/integration_spec.ts @@ -15,7 +15,7 @@ import { inject, iit, xit, - isFirefox + browserDetection } from 'angular2/test_lib'; import {DOM} from 'angular2/src/core/dom/dom_adapter'; @@ -740,7 +740,7 @@ export function main() { // In Firefox, effective text selection in the real DOM requires an actual focus // of the field. This is not an issue in a new HTML document. - if (isFirefox()) { + if (browserDetection.isFirefox) { var fakeDoc = DOM.createHtmlDocument(); DOM.appendChild(fakeDoc.body, rootTC.nativeElement); } diff --git a/modules/angular2/test/pipes/async_pipe_spec.ts b/modules/angular2/test/pipes/async_pipe_spec.ts index cfb4852c79..6336bb1bbf 100644 --- a/modules/angular2/test/pipes/async_pipe_spec.ts +++ b/modules/angular2/test/pipes/async_pipe_spec.ts @@ -9,7 +9,8 @@ import { afterEach, AsyncTestCompleter, inject, - SpyObject + SpyObject, + browserDetection } from 'angular2/test_lib'; import {SpyChangeDetectorRef} from './spies'; @@ -120,7 +121,7 @@ export function main() { var completer; var ref; // adds longer timers for passing tests in IE - var timer = (!isBlank(DOM) && DOM.getUserAgent().indexOf("Trident") > -1) ? 50 : 0; + var timer = (!isBlank(DOM) && browserDetection.isIE) ? 50 : 0; beforeEach(() => { completer = PromiseWrapper.completer(); diff --git a/modules/angular2/test/pipes/date_pipe_spec.ts b/modules/angular2/test/pipes/date_pipe_spec.ts index 8960411591..c68813f331 100644 --- a/modules/angular2/test/pipes/date_pipe_spec.ts +++ b/modules/angular2/test/pipes/date_pipe_spec.ts @@ -7,7 +7,7 @@ import { expect, beforeEach, afterEach, - supportsIntlApi + browserDetection } from 'angular2/test_lib'; import {DatePipe} from 'angular2/pipes'; @@ -35,7 +35,7 @@ export function main() { // TODO(mlaval): enable tests when Intl API is no longer used, see // https://github.com/angular/angular/issues/3333 - if (supportsIntlApi()) { + if (browserDetection.supportsIntlApi) { describe("transform", () => { it('should format each component correctly', () => { expect(pipe.transform(date, ['y'])).toEqual('2015'); diff --git a/modules/angular2/test/pipes/number_pipe_spec.ts b/modules/angular2/test/pipes/number_pipe_spec.ts index a25bdc9dd0..7d7b0be948 100644 --- a/modules/angular2/test/pipes/number_pipe_spec.ts +++ b/modules/angular2/test/pipes/number_pipe_spec.ts @@ -7,7 +7,7 @@ import { expect, beforeEach, afterEach, - supportsIntlApi + browserDetection } from 'angular2/test_lib'; import {DecimalPipe, PercentPipe, CurrencyPipe} from 'angular2/pipes'; @@ -15,7 +15,7 @@ import {DecimalPipe, PercentPipe, CurrencyPipe} from 'angular2/pipes'; export function main() { // TODO(mlaval): enable tests when Intl API is no longer used, see // https://github.com/angular/angular/issues/3333 - if (supportsIntlApi()) { + if (browserDetection.supportsIntlApi) { describe("DecimalPipe", () => { var pipe; diff --git a/modules/angular2/test/test_lib/utils_spec.ts b/modules/angular2/test/test_lib/utils_spec.ts new file mode 100644 index 0000000000..5f492e172e --- /dev/null +++ b/modules/angular2/test/test_lib/utils_spec.ts @@ -0,0 +1,172 @@ +import {describe, it, iit, ddescribe, expect, BrowserDetection} from 'angular2/test_lib'; +import {StringMapWrapper} from 'angular2/src/core/facade/collection'; + +export function main() { + describe('BrowserDetection', () => { + + var browsers = [ + { + name: 'Chrome', + ua: 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36', + isFirefox: false, + isAndroid: false, + isEdge: false, + isIE: false, + isWebkit: true, + supportsIntlApi: true + }, + { + name: 'Chrome mobile', + ua: 'Mozilla/5.0 (Linux; Android 5.1.1; D5803 Build/23.4.A.0.546) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.133 Mobile Safari/537.36', + isFirefox: false, + isAndroid: false, + isEdge: false, + isIE: false, + isWebkit: true, + supportsIntlApi: true + }, + { + name: 'Firefox', + ua: 'Mozilla/5.0 (X11; Linux i686; rv:40.0) Gecko/20100101 Firefox/40.0', + isFirefox: true, + isAndroid: false, + isEdge: false, + isIE: false, + isWebkit: false, + supportsIntlApi: false + }, + { + name: 'IE9', + ua: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727)', + isFirefox: false, + isAndroid: false, + isEdge: false, + isIE: true, + isWebkit: false, + supportsIntlApi: false + }, + { + name: 'IE10', + ua: 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C)', + isFirefox: false, + isAndroid: false, + isEdge: false, + isIE: true, + isWebkit: false, + supportsIntlApi: false + }, + { + name: 'IE11', + ua: 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko', + isFirefox: false, + isAndroid: false, + isEdge: false, + isIE: true, + isWebkit: false, + supportsIntlApi: false + }, + { + name: 'Edge', + ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136', + isFirefox: false, + isAndroid: false, + isEdge: true, + isIE: false, + isWebkit: false, + supportsIntlApi: false + }, + { + name: 'Android4.1', + ua: 'Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Android SDK built for x86 Build/JRO03H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30', + isFirefox: false, + isAndroid: true, + isEdge: false, + isIE: false, + isWebkit: true, + supportsIntlApi: false + }, + { + name: 'Android4.2', + ua: 'Mozilla/5.0 (Linux; U; Android 4.2; en-us; Android SDK built for x86 Build/JOP40C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30', + isFirefox: false, + isAndroid: true, + isEdge: false, + isIE: false, + isWebkit: true, + supportsIntlApi: false + }, + { + name: 'Android4.3', + ua: 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; Android SDK built for x86 Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30', + isFirefox: false, + isAndroid: true, + isEdge: false, + isIE: false, + isWebkit: true, + supportsIntlApi: false + }, + { + name: 'Android4.4', + ua: 'Mozilla/5.0 (Linux; Android 4.4.2; Android SDK built for x86 Build/KK) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36', + isFirefox: false, + isAndroid: false, + isEdge: false, + isIE: false, + isWebkit: true, + supportsIntlApi: false + }, + { + name: 'Safari7', + ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/7.1.7 Safari/537.85.16', + isFirefox: false, + isAndroid: false, + isEdge: false, + isIE: false, + isWebkit: true, + supportsIntlApi: false + }, + { + name: 'Safari8', + ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12', + isFirefox: false, + isAndroid: false, + isEdge: false, + isIE: false, + isWebkit: true, + supportsIntlApi: false + }, + { + name: 'iOS7', + ua: 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D167 Safari/9537.53', + isFirefox: false, + isAndroid: false, + isEdge: false, + isIE: false, + isWebkit: true, + supportsIntlApi: false + }, + { + name: 'iOS8', + ua: 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H141 Safari/600.1.4', + isFirefox: false, + isAndroid: false, + isEdge: false, + isIE: false, + isWebkit: true, + supportsIntlApi: false + } + ]; + + browsers.forEach((browser) => { + it(`should detect ${StringMapWrapper.get(browser, 'name')}`, () => { + var bd = new BrowserDetection(StringMapWrapper.get(browser, 'ua')); + expect(bd.isFirefox).toBe(StringMapWrapper.get(browser, 'isFirefox')); + expect(bd.isAndroid).toBe(StringMapWrapper.get(browser, 'isAndroid')); + expect(bd.isEdge).toBe(StringMapWrapper.get(browser, 'isEdge')); + expect(bd.isIE).toBe(StringMapWrapper.get(browser, 'isIE')); + expect(bd.isWebkit).toBe(StringMapWrapper.get(browser, 'isWebkit')); + expect(bd.supportsIntlApi).toBe(StringMapWrapper.get(browser, 'supportsIntlApi')); + }); + }); + }); +}