vendor_test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.dom.vendorTest');
  15. goog.setTestOnly('goog.dom.vendorTest');
  16. goog.require('goog.array');
  17. goog.require('goog.dom.vendor');
  18. goog.require('goog.labs.userAgent.util');
  19. goog.require('goog.testing.MockUserAgent');
  20. goog.require('goog.testing.PropertyReplacer');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.userAgent');
  23. goog.require('goog.userAgentTestUtil');
  24. var documentMode;
  25. var mockUserAgent;
  26. var propertyReplacer = new goog.testing.PropertyReplacer();
  27. function setUp() {
  28. mockUserAgent = new goog.testing.MockUserAgent();
  29. mockUserAgent.install();
  30. }
  31. function tearDown() {
  32. goog.dispose(mockUserAgent);
  33. documentMode = undefined;
  34. propertyReplacer.reset();
  35. }
  36. goog.userAgent.getDocumentMode_ = function() {
  37. return documentMode;
  38. };
  39. var UserAgents = {GECKO: 'GECKO', IE: 'IE', OPERA: 'OPERA', WEBKIT: 'WEBKIT'};
  40. /**
  41. * Return whether a given user agent has been detected.
  42. * @param {number} agent Value in UserAgents.
  43. * @return {boolean} Whether the user agent has been detected.
  44. */
  45. function getUserAgentDetected_(agent) {
  46. switch (agent) {
  47. case UserAgents.GECKO:
  48. return goog.userAgent.GECKO;
  49. case UserAgents.IE:
  50. return goog.userAgent.IE;
  51. case UserAgents.OPERA:
  52. return goog.userAgent.OPERA;
  53. case UserAgents.WEBKIT:
  54. return goog.userAgent.WEBKIT;
  55. }
  56. return null;
  57. }
  58. /**
  59. * Test browser detection for a user agent configuration.
  60. * @param {Array<number>} expectedAgents Array of expected userAgents.
  61. * @param {string} uaString User agent string.
  62. * @param {string=} opt_product Navigator product string.
  63. * @param {string=} opt_vendor Navigator vendor string.
  64. */
  65. function assertUserAgent(expectedAgents, uaString, opt_product, opt_vendor) {
  66. var mockNavigator =
  67. {'userAgent': uaString, 'product': opt_product, 'vendor': opt_vendor};
  68. mockUserAgent.setNavigator(mockNavigator);
  69. mockUserAgent.setUserAgentString(uaString);
  70. // Force reread of navigator.userAgent;
  71. goog.labs.userAgent.util.setUserAgent(null);
  72. goog.userAgentTestUtil.reinitializeUserAgent();
  73. for (var ua in UserAgents) {
  74. var isExpected = goog.array.contains(expectedAgents, UserAgents[ua]);
  75. assertEquals(isExpected, getUserAgentDetected_(UserAgents[ua]));
  76. }
  77. }
  78. /**
  79. * Tests for the vendor prefix for Webkit.
  80. */
  81. function testVendorPrefixWebkit() {
  82. assertUserAgent([UserAgents.WEBKIT], 'WebKit');
  83. assertEquals('-webkit', goog.dom.vendor.getVendorPrefix());
  84. }
  85. /**
  86. * Tests for the vendor prefix for Mozilla/Gecko.
  87. */
  88. function testVendorPrefixGecko() {
  89. assertUserAgent([UserAgents.GECKO], 'Gecko', 'Gecko');
  90. assertEquals('-moz', goog.dom.vendor.getVendorPrefix());
  91. }
  92. /**
  93. * Tests for the vendor prefix for Opera.
  94. */
  95. function testVendorPrefixOpera() {
  96. assertUserAgent([UserAgents.OPERA], 'Opera');
  97. assertEquals('-o', goog.dom.vendor.getVendorPrefix());
  98. }
  99. /**
  100. * Tests for the vendor prefix for IE.
  101. */
  102. function testVendorPrefixIE() {
  103. assertUserAgent([UserAgents.IE], 'MSIE');
  104. assertEquals('-ms', goog.dom.vendor.getVendorPrefix());
  105. }
  106. /**
  107. * Tests for the vendor Js prefix for Webkit.
  108. */
  109. function testVendorJsPrefixWebkit() {
  110. assertUserAgent([UserAgents.WEBKIT], 'WebKit');
  111. assertEquals('Webkit', goog.dom.vendor.getVendorJsPrefix());
  112. }
  113. /**
  114. * Tests for the vendor Js prefix for Mozilla/Gecko.
  115. */
  116. function testVendorJsPrefixGecko() {
  117. assertUserAgent([UserAgents.GECKO], 'Gecko', 'Gecko');
  118. assertEquals('Moz', goog.dom.vendor.getVendorJsPrefix());
  119. }
  120. /**
  121. * Tests for the vendor Js prefix for Opera.
  122. */
  123. function testVendorJsPrefixOpera() {
  124. assertUserAgent([UserAgents.OPERA], 'Opera');
  125. assertEquals('O', goog.dom.vendor.getVendorJsPrefix());
  126. }
  127. /**
  128. * Tests for the vendor Js prefix for IE.
  129. */
  130. function testVendorJsPrefixIE() {
  131. assertUserAgent([UserAgents.IE], 'MSIE');
  132. assertEquals('ms', goog.dom.vendor.getVendorJsPrefix());
  133. }
  134. /**
  135. * Tests for the vendor Js prefix if no UA detected.
  136. */
  137. function testVendorJsPrefixNone() {
  138. assertUserAgent([], '');
  139. assertNull(goog.dom.vendor.getVendorJsPrefix());
  140. }
  141. /**
  142. * Tests for the prefixed property name on Webkit.
  143. */
  144. function testPrefixedPropertyNameWebkit() {
  145. assertUserAgent([UserAgents.WEBKIT], 'WebKit');
  146. assertEquals(
  147. 'webkitFoobar', goog.dom.vendor.getPrefixedPropertyName('foobar'));
  148. }
  149. /**
  150. * Tests for the prefixed property name on Webkit in an object.
  151. */
  152. function testPrefixedPropertyNameWebkitAndObject() {
  153. var mockDocument = {
  154. // setting a value of 0 on purpose, to ensure we only look for property
  155. // names, not their values.
  156. 'webkitFoobar': 0
  157. };
  158. assertUserAgent([UserAgents.WEBKIT], 'WebKit');
  159. assertEquals(
  160. 'webkitFoobar',
  161. goog.dom.vendor.getPrefixedPropertyName('foobar', mockDocument));
  162. }
  163. /**
  164. * Tests for the prefixed property name.
  165. */
  166. function testPrefixedPropertyName() {
  167. assertUserAgent([], '');
  168. assertNull(goog.dom.vendor.getPrefixedPropertyName('foobar'));
  169. }
  170. /**
  171. * Tests for the prefixed property name in an object.
  172. */
  173. function testPrefixedPropertyNameAndObject() {
  174. var mockDocument = {'foobar': 0};
  175. assertUserAgent([], '');
  176. assertEquals(
  177. 'foobar',
  178. goog.dom.vendor.getPrefixedPropertyName('foobar', mockDocument));
  179. }
  180. /**
  181. * Tests for the prefixed property name when it doesn't exist.
  182. */
  183. function testPrefixedPropertyNameAndObjectIsEmpty() {
  184. var mockDocument = {};
  185. assertUserAgent([], '');
  186. assertNull(goog.dom.vendor.getPrefixedPropertyName('foobar', mockDocument));
  187. }
  188. /**
  189. * Test for prefixed event type.
  190. */
  191. function testPrefixedEventType() {
  192. assertUserAgent([], '');
  193. assertEquals('foobar', goog.dom.vendor.getPrefixedEventType('foobar'));
  194. }
  195. /**
  196. * Test for browser-specific prefixed event type.
  197. */
  198. function testPrefixedEventTypeForBrowser() {
  199. assertUserAgent([UserAgents.WEBKIT], 'WebKit');
  200. assertEquals('webkitfoobar', goog.dom.vendor.getPrefixedEventType('foobar'));
  201. }
  202. function assertIe(uaString, expectedVersion) {
  203. assertUserAgent([UserAgents.IE], uaString);
  204. assertEquals(
  205. 'User agent ' + uaString + ' should have had version ' + expectedVersion +
  206. ' but had ' + goog.userAgent.VERSION,
  207. expectedVersion, goog.userAgent.VERSION);
  208. }
  209. function assertGecko(uaString, expectedVersion) {
  210. assertUserAgent([UserAgents.GECKO], uaString, 'Gecko');
  211. assertEquals(
  212. 'User agent ' + uaString + ' should have had version ' + expectedVersion +
  213. ' but had ' + goog.userAgent.VERSION,
  214. expectedVersion, goog.userAgent.VERSION);
  215. }