123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- // Copyright 2012 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- goog.provide('goog.dom.vendorTest');
- goog.setTestOnly('goog.dom.vendorTest');
- goog.require('goog.array');
- goog.require('goog.dom.vendor');
- goog.require('goog.labs.userAgent.util');
- goog.require('goog.testing.MockUserAgent');
- goog.require('goog.testing.PropertyReplacer');
- goog.require('goog.testing.jsunit');
- goog.require('goog.userAgent');
- goog.require('goog.userAgentTestUtil');
- var documentMode;
- var mockUserAgent;
- var propertyReplacer = new goog.testing.PropertyReplacer();
- function setUp() {
- mockUserAgent = new goog.testing.MockUserAgent();
- mockUserAgent.install();
- }
- function tearDown() {
- goog.dispose(mockUserAgent);
- documentMode = undefined;
- propertyReplacer.reset();
- }
- goog.userAgent.getDocumentMode_ = function() {
- return documentMode;
- };
- var UserAgents = {GECKO: 'GECKO', IE: 'IE', OPERA: 'OPERA', WEBKIT: 'WEBKIT'};
- /**
- * Return whether a given user agent has been detected.
- * @param {number} agent Value in UserAgents.
- * @return {boolean} Whether the user agent has been detected.
- */
- function getUserAgentDetected_(agent) {
- switch (agent) {
- case UserAgents.GECKO:
- return goog.userAgent.GECKO;
- case UserAgents.IE:
- return goog.userAgent.IE;
- case UserAgents.OPERA:
- return goog.userAgent.OPERA;
- case UserAgents.WEBKIT:
- return goog.userAgent.WEBKIT;
- }
- return null;
- }
- /**
- * Test browser detection for a user agent configuration.
- * @param {Array<number>} expectedAgents Array of expected userAgents.
- * @param {string} uaString User agent string.
- * @param {string=} opt_product Navigator product string.
- * @param {string=} opt_vendor Navigator vendor string.
- */
- function assertUserAgent(expectedAgents, uaString, opt_product, opt_vendor) {
- var mockNavigator =
- {'userAgent': uaString, 'product': opt_product, 'vendor': opt_vendor};
- mockUserAgent.setNavigator(mockNavigator);
- mockUserAgent.setUserAgentString(uaString);
- // Force reread of navigator.userAgent;
- goog.labs.userAgent.util.setUserAgent(null);
- goog.userAgentTestUtil.reinitializeUserAgent();
- for (var ua in UserAgents) {
- var isExpected = goog.array.contains(expectedAgents, UserAgents[ua]);
- assertEquals(isExpected, getUserAgentDetected_(UserAgents[ua]));
- }
- }
- /**
- * Tests for the vendor prefix for Webkit.
- */
- function testVendorPrefixWebkit() {
- assertUserAgent([UserAgents.WEBKIT], 'WebKit');
- assertEquals('-webkit', goog.dom.vendor.getVendorPrefix());
- }
- /**
- * Tests for the vendor prefix for Mozilla/Gecko.
- */
- function testVendorPrefixGecko() {
- assertUserAgent([UserAgents.GECKO], 'Gecko', 'Gecko');
- assertEquals('-moz', goog.dom.vendor.getVendorPrefix());
- }
- /**
- * Tests for the vendor prefix for Opera.
- */
- function testVendorPrefixOpera() {
- assertUserAgent([UserAgents.OPERA], 'Opera');
- assertEquals('-o', goog.dom.vendor.getVendorPrefix());
- }
- /**
- * Tests for the vendor prefix for IE.
- */
- function testVendorPrefixIE() {
- assertUserAgent([UserAgents.IE], 'MSIE');
- assertEquals('-ms', goog.dom.vendor.getVendorPrefix());
- }
- /**
- * Tests for the vendor Js prefix for Webkit.
- */
- function testVendorJsPrefixWebkit() {
- assertUserAgent([UserAgents.WEBKIT], 'WebKit');
- assertEquals('Webkit', goog.dom.vendor.getVendorJsPrefix());
- }
- /**
- * Tests for the vendor Js prefix for Mozilla/Gecko.
- */
- function testVendorJsPrefixGecko() {
- assertUserAgent([UserAgents.GECKO], 'Gecko', 'Gecko');
- assertEquals('Moz', goog.dom.vendor.getVendorJsPrefix());
- }
- /**
- * Tests for the vendor Js prefix for Opera.
- */
- function testVendorJsPrefixOpera() {
- assertUserAgent([UserAgents.OPERA], 'Opera');
- assertEquals('O', goog.dom.vendor.getVendorJsPrefix());
- }
- /**
- * Tests for the vendor Js prefix for IE.
- */
- function testVendorJsPrefixIE() {
- assertUserAgent([UserAgents.IE], 'MSIE');
- assertEquals('ms', goog.dom.vendor.getVendorJsPrefix());
- }
- /**
- * Tests for the vendor Js prefix if no UA detected.
- */
- function testVendorJsPrefixNone() {
- assertUserAgent([], '');
- assertNull(goog.dom.vendor.getVendorJsPrefix());
- }
- /**
- * Tests for the prefixed property name on Webkit.
- */
- function testPrefixedPropertyNameWebkit() {
- assertUserAgent([UserAgents.WEBKIT], 'WebKit');
- assertEquals(
- 'webkitFoobar', goog.dom.vendor.getPrefixedPropertyName('foobar'));
- }
- /**
- * Tests for the prefixed property name on Webkit in an object.
- */
- function testPrefixedPropertyNameWebkitAndObject() {
- var mockDocument = {
- // setting a value of 0 on purpose, to ensure we only look for property
- // names, not their values.
- 'webkitFoobar': 0
- };
- assertUserAgent([UserAgents.WEBKIT], 'WebKit');
- assertEquals(
- 'webkitFoobar',
- goog.dom.vendor.getPrefixedPropertyName('foobar', mockDocument));
- }
- /**
- * Tests for the prefixed property name.
- */
- function testPrefixedPropertyName() {
- assertUserAgent([], '');
- assertNull(goog.dom.vendor.getPrefixedPropertyName('foobar'));
- }
- /**
- * Tests for the prefixed property name in an object.
- */
- function testPrefixedPropertyNameAndObject() {
- var mockDocument = {'foobar': 0};
- assertUserAgent([], '');
- assertEquals(
- 'foobar',
- goog.dom.vendor.getPrefixedPropertyName('foobar', mockDocument));
- }
- /**
- * Tests for the prefixed property name when it doesn't exist.
- */
- function testPrefixedPropertyNameAndObjectIsEmpty() {
- var mockDocument = {};
- assertUserAgent([], '');
- assertNull(goog.dom.vendor.getPrefixedPropertyName('foobar', mockDocument));
- }
- /**
- * Test for prefixed event type.
- */
- function testPrefixedEventType() {
- assertUserAgent([], '');
- assertEquals('foobar', goog.dom.vendor.getPrefixedEventType('foobar'));
- }
- /**
- * Test for browser-specific prefixed event type.
- */
- function testPrefixedEventTypeForBrowser() {
- assertUserAgent([UserAgents.WEBKIT], 'WebKit');
- assertEquals('webkitfoobar', goog.dom.vendor.getPrefixedEventType('foobar'));
- }
- function assertIe(uaString, expectedVersion) {
- assertUserAgent([UserAgents.IE], uaString);
- assertEquals(
- 'User agent ' + uaString + ' should have had version ' + expectedVersion +
- ' but had ' + goog.userAgent.VERSION,
- expectedVersion, goog.userAgent.VERSION);
- }
- function assertGecko(uaString, expectedVersion) {
- assertUserAgent([UserAgents.GECKO], uaString, 'Gecko');
- assertEquals(
- 'User agent ' + uaString + ' should have had version ' + expectedVersion +
- ' but had ' + goog.userAgent.VERSION,
- expectedVersion, goog.userAgent.VERSION);
- }
|