Skip to content

Commit

Permalink
test(core): update Web Platform feature detection (#24861)
Browse files Browse the repository at this point in the history
PR Close #24861
  • Loading branch information
robwormald authored and mhevery committed Aug 31, 2018
1 parent 6e6489a commit d76a7d6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/elements/test/create-custom-element_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import {Component, DoBootstrap, EventEmitter, Injector, Input, NgModule, Output, destroyPlatform} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {browserDetection} from '@angular/platform-browser/testing/src/browser_util';
import {Subject} from 'rxjs';

import {NgElementConstructor, createCustomElement} from '../src/create-custom-element';
Expand All @@ -19,7 +20,7 @@ type WithFooBar = {
barBar: string
};

if (typeof customElements !== 'undefined') {
if (browserDetection.supportsCustomElements) {
describe('createCustomElement', () => {
let NgElementCtor: NgElementConstructor<WithFooBar>;
let strategy: TestStrategy;
Expand Down
5 changes: 3 additions & 2 deletions packages/elements/test/slots_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import {Component, ComponentFactoryResolver, EventEmitter, Injector, Input, NgModule, Output, ViewEncapsulation, destroyPlatform} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {browserDetection} from '@angular/platform-browser/testing/src/browser_util';
import {Subject} from 'rxjs';

import {NgElement, NgElementConstructor, createCustomElement} from '../src/create-custom-element';
Expand All @@ -18,8 +19,8 @@ type WithFooBar = {
fooFoo: string,
barBar: string
};

if (typeof customElements !== 'undefined') {
// we only run these tests in browsers that support Shadom DOM slots natively
if (browserDetection.supportsCustomElements && browserDetection.supportsShadowDom) {
describe('slots', () => {
let testContainer: HTMLDivElement;

Expand Down
3 changes: 1 addition & 2 deletions packages/platform-browser/test/dom/dom_renderer_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ import {NAMESPACE_URIS} from '../../src/dom/dom_renderer';
});
});

// other browsers don't support shadow dom
if (browserDetection.isChromeDesktop) {
if (browserDetection.supportsDeprecatedShadowDomV0) {
it('should allow to style components with emulated encapsulation and no encapsulation inside of components with shadow DOM',
() => {
const fixture = TestBed.createComponent(SomeApp);
Expand Down
7 changes: 2 additions & 5 deletions packages/platform-browser/test/dom/shadow_dom_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
import {Component, EventEmitter, Injector, Input, NgModule, Output, Renderer2, ViewEncapsulation, destroyPlatform} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {browserDetection} from '@angular/platform-browser/testing/src/browser_util';
import {expect} from '@angular/platform-browser/testing/src/matchers';

function supportsShadowDOMV1() {
const testEl = document.createElement('div');
return (typeof customElements !== 'undefined') && (typeof testEl.attachShadow !== 'undefined');
}

if (supportsShadowDOMV1()) {
if (browserDetection.supportsShadowDom) {
describe('ShadowDOM Support', () => {

let testContainer: HTMLDivElement;
Expand Down
17 changes: 17 additions & 0 deletions packages/platform-browser/testing/src/browser_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ export class BrowserDetection {
return this._ua.indexOf('Chrome') > -1 && this._ua.indexOf('Chrome/3') > -1 &&
this._ua.indexOf('Edge') == -1;
}

get supportsCustomElements() { return (typeof(<any>global).customElements !== 'undefined'); }

get supportsDeprecatedCustomCustomElementsV0() {
return (typeof(document as any).registerElement !== 'undefined');
}

get supportsShadowDom() {
const testEl = document.createElement('div');
return (typeof customElements !== 'undefined') && (typeof testEl.attachShadow !== 'undefined');
}

get supportsDeprecatedShadowDomV0() {
const testEl = document.createElement('div') as any;
return (typeof customElements !== 'undefined') &&
(typeof testEl.createShadowRoot !== 'undefined');
}
}

BrowserDetection.setup();
Expand Down

0 comments on commit d76a7d6

Please sign in to comment.