util_test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2013 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 frexcept 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. /**
  15. * @fileoverview Unit tests for goog.labs.userAgent.engine.
  16. */
  17. goog.provide('goog.labs.userAgent.utilTest');
  18. goog.require('goog.functions');
  19. goog.require('goog.labs.userAgent.testAgents');
  20. goog.require('goog.labs.userAgent.util');
  21. goog.require('goog.testing.PropertyReplacer');
  22. goog.require('goog.testing.jsunit');
  23. goog.setTestOnly('goog.labs.userAgent.utilTest');
  24. var stubs = new goog.testing.PropertyReplacer();
  25. function tearDown() {
  26. stubs.reset();
  27. }
  28. /**
  29. * Tests parsing a few example UA strings.
  30. */
  31. function testExtractVersionTuples() {
  32. // Old Android
  33. var tuples = goog.labs.userAgent.util.extractVersionTuples(
  34. goog.labs.userAgent.testAgents.ANDROID_BROWSER_235);
  35. assertEquals(4, tuples.length);
  36. assertSameElements(
  37. [
  38. 'Mozilla', '5.0',
  39. 'Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40'
  40. ],
  41. tuples[0]);
  42. assertSameElements(['AppleWebKit', '533.1', 'KHTML, like Gecko'], tuples[1]);
  43. assertSameElements(['Version', '4.0', undefined], tuples[2]);
  44. assertSameElements(['Mobile Safari', '533.1', undefined], tuples[3]);
  45. // IE 9
  46. tuples = goog.labs.userAgent.util.extractVersionTuples(
  47. goog.labs.userAgent.testAgents.IE_9);
  48. assertEquals(1, tuples.length);
  49. assertSameElements(
  50. ['Mozilla', '5.0', 'compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0'],
  51. tuples[0]);
  52. // Opera
  53. tuples = goog.labs.userAgent.util.extractVersionTuples(
  54. goog.labs.userAgent.testAgents.OPERA_10);
  55. assertEquals(3, tuples.length);
  56. assertSameElements(
  57. ['Opera', '9.80', 'S60; SymbOS; Opera Mobi/447; U; en'], tuples[0]);
  58. assertSameElements(['Presto', '2.4.18', undefined], tuples[1]);
  59. assertSameElements(['Version', '10.00', undefined], tuples[2]);
  60. }
  61. function testSetUserAgent() {
  62. var ua = 'Five Finger Death Punch';
  63. goog.labs.userAgent.util.setUserAgent(ua);
  64. assertEquals(ua, goog.labs.userAgent.util.getUserAgent());
  65. assertTrue(goog.labs.userAgent.util.matchUserAgent('Punch'));
  66. assertFalse(goog.labs.userAgent.util.matchUserAgent('punch'));
  67. assertFalse(goog.labs.userAgent.util.matchUserAgent('Mozilla'));
  68. }
  69. function testSetUserAgentIgnoreCase() {
  70. var ua = 'Five Finger Death Punch';
  71. goog.labs.userAgent.util.setUserAgent(ua);
  72. assertEquals(ua, goog.labs.userAgent.util.getUserAgent());
  73. assertTrue(goog.labs.userAgent.util.matchUserAgentIgnoreCase('Punch'));
  74. assertTrue(goog.labs.userAgent.util.matchUserAgentIgnoreCase('punch'));
  75. assertFalse(goog.labs.userAgent.util.matchUserAgentIgnoreCase('Mozilla'));
  76. }
  77. function testNoNavigator() {
  78. stubs.set(
  79. goog.labs.userAgent.util, 'getNavigator_',
  80. goog.functions.constant(undefined));
  81. assertEquals('', goog.labs.userAgent.util.getNativeUserAgentString_());
  82. }
  83. function testNavigatorWithNoUserAgent() {
  84. stubs.set(
  85. goog.labs.userAgent.util, 'getNavigator_',
  86. goog.functions.constant(undefined));
  87. assertEquals('', goog.labs.userAgent.util.getNativeUserAgentString_());
  88. }
  89. function testNavigatorWithUserAgent() {
  90. stubs.set(
  91. goog.labs.userAgent.util, 'getNavigator_',
  92. goog.functions.constant({'userAgent': 'moose'}));
  93. assertEquals('moose', goog.labs.userAgent.util.getNativeUserAgentString_());
  94. }