useragenttestutil.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2006 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. /**
  15. * @fileoverview Shared test function to reset the constants in
  16. * goog.userAgent.*
  17. */
  18. goog.provide('goog.userAgentTestUtil');
  19. goog.provide('goog.userAgentTestUtil.UserAgents');
  20. goog.require('goog.labs.userAgent.browser');
  21. goog.require('goog.labs.userAgent.engine');
  22. goog.require('goog.labs.userAgent.platform');
  23. goog.require('goog.object');
  24. goog.require('goog.userAgent');
  25. goog.require('goog.userAgent.keyboard');
  26. goog.require('goog.userAgent.platform');
  27. goog.require('goog.userAgent.product');
  28. /** @suppress {extraRequire} */
  29. goog.require('goog.userAgent.product.isVersion');
  30. goog.setTestOnly('goog.userAgentTestUtil');
  31. /**
  32. * Rerun the initialization code to set all of the goog.userAgent constants.
  33. * @suppress {accessControls}
  34. */
  35. goog.userAgentTestUtil.reinitializeUserAgent = function() {
  36. // Unfortunately we can't isolate the useragent setting in a function
  37. // we can call, because things rely on it compiling to nothing when
  38. // one of the ASSUME flags is set, and the compiler isn't smart enough
  39. // to do that when the setting is done inside a function that's inlined.
  40. goog.userAgent.OPERA = goog.labs.userAgent.browser.isOpera();
  41. goog.userAgent.IE = goog.labs.userAgent.browser.isIE();
  42. goog.userAgent.EDGE = goog.labs.userAgent.engine.isEdge();
  43. goog.userAgent.EDGE_OR_IE = goog.userAgent.EDGE || goog.userAgent.IE;
  44. goog.userAgent.GECKO = goog.labs.userAgent.engine.isGecko();
  45. goog.userAgent.WEBKIT = goog.labs.userAgent.engine.isWebKit();
  46. goog.userAgent.MOBILE = goog.userAgent.isMobile_();
  47. goog.userAgent.SAFARI = goog.userAgent.WEBKIT;
  48. // Platform in goog.userAgent.
  49. goog.userAgent.PLATFORM = goog.userAgent.determinePlatform_();
  50. goog.userAgent.MAC = goog.labs.userAgent.platform.isMacintosh();
  51. goog.userAgent.WINDOWS = goog.labs.userAgent.platform.isWindows();
  52. goog.userAgent.LINUX = goog.userAgent.isLegacyLinux_();
  53. goog.userAgent.X11 = goog.userAgent.isX11_();
  54. goog.userAgent.ANDROID = goog.labs.userAgent.platform.isAndroid();
  55. goog.userAgent.IPAD = goog.labs.userAgent.platform.isIpad();
  56. goog.userAgent.IPHONE = goog.labs.userAgent.platform.isIphone();
  57. goog.userAgent.IPOD = goog.labs.userAgent.platform.isIpod();
  58. goog.userAgent.VERSION = goog.userAgent.determineVersion_();
  59. // Platform in goog.userAgent.platform.
  60. goog.userAgent.platform.VERSION = goog.userAgent.platform.determineVersion_();
  61. // Update goog.userAgent.product
  62. goog.userAgent.product.ANDROID =
  63. goog.labs.userAgent.browser.isAndroidBrowser();
  64. goog.userAgent.product.CHROME = goog.labs.userAgent.browser.isChrome();
  65. goog.userAgent.product.EDGE = goog.labs.userAgent.browser.isEdge();
  66. goog.userAgent.product.FIREFOX = goog.labs.userAgent.browser.isFirefox();
  67. goog.userAgent.product.IE = goog.labs.userAgent.browser.isIE();
  68. goog.userAgent.product.IPAD = goog.labs.userAgent.platform.isIpad();
  69. goog.userAgent.product.IPHONE = goog.userAgent.product.isIphoneOrIpod_();
  70. goog.userAgent.product.OPERA = goog.labs.userAgent.browser.isOpera();
  71. goog.userAgent.product.SAFARI = goog.userAgent.product.isSafariDesktop_();
  72. // Still uses its own implementation.
  73. goog.userAgent.product.VERSION = goog.userAgent.product.determineVersion_();
  74. // goog.userAgent.keyboard
  75. goog.userAgent.keyboard.MAC_KEYBOARD =
  76. goog.userAgent.keyboard.determineMacKeyboard_();
  77. // Reset cache so calls to isVersionOrHigher don't use cached version.
  78. goog.object.clear(goog.userAgent.isVersionOrHigherCache_);
  79. };
  80. /**
  81. * Browser definitions.
  82. * @enum {string}
  83. */
  84. goog.userAgentTestUtil.UserAgents = {
  85. GECKO: 'GECKO',
  86. IE: 'IE',
  87. OPERA: 'OPERA',
  88. WEBKIT: 'WEBKIT',
  89. EDGE: 'EDGE'
  90. };
  91. /**
  92. * Return whether a given user agent has been detected.
  93. * @param {string} agent Value in UserAgents.
  94. * @return {boolean} Whether the user agent has been detected.
  95. */
  96. goog.userAgentTestUtil.getUserAgentDetected = function(agent) {
  97. switch (agent) {
  98. case goog.userAgentTestUtil.UserAgents.GECKO:
  99. return goog.userAgent.GECKO;
  100. case goog.userAgentTestUtil.UserAgents.IE:
  101. return goog.userAgent.IE;
  102. case goog.userAgentTestUtil.UserAgents.EDGE:
  103. return goog.userAgent.EDGE;
  104. case goog.userAgentTestUtil.UserAgents.OPERA:
  105. return goog.userAgent.OPERA;
  106. case goog.userAgentTestUtil.UserAgents.WEBKIT:
  107. return goog.userAgent.WEBKIT;
  108. }
  109. throw Error('Unrecognized user agent');
  110. };