timezonedetection_test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2008 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.locale.timeZoneDetectionTest');
  15. goog.setTestOnly('goog.locale.timeZoneDetectionTest');
  16. goog.require('goog.locale.timeZoneDetection');
  17. goog.require('goog.testing.jsunit');
  18. /**
  19. * Mock date class with simplified properties of Date class for testing.
  20. * @constructor
  21. */
  22. function MockDate() {
  23. /**
  24. * Time zone offset. For time zones with daylight saving, the different
  25. * offsets are represented as array of offsets.
  26. * @type {Array<number>}
  27. * @private
  28. */
  29. this.timezoneOffset_ = [];
  30. /**
  31. * Counter storing the index of next offset value to be returned from the
  32. * array of offset values.
  33. * @type {number}
  34. * @private
  35. */
  36. this.offsetArrayCounter_ = 0;
  37. }
  38. /**
  39. * Does nothing because setting the time to calculate offset is not needed
  40. * in the mock class.
  41. * @param {number} ms Ignored.
  42. */
  43. MockDate.prototype.setTime = function(ms) {
  44. // Do nothing.
  45. };
  46. /**
  47. * Sets the time zone offset.
  48. * @param {Array<number>} offset Time zone offset.
  49. */
  50. MockDate.prototype.setTimezoneOffset = function(offset) {
  51. this.timezoneOffset_ = offset;
  52. };
  53. /**
  54. * Returns consecutive offsets from array of time zone offsets on each call.
  55. * @return {number} Time zone offset.
  56. */
  57. MockDate.prototype.getTimezoneOffset = function() {
  58. return this.timezoneOffset_.length > 1 ?
  59. this.timezoneOffset_[this.offsetArrayCounter_++] :
  60. this.timezoneOffset_[0];
  61. };
  62. function testGetFingerprint() {
  63. var mockDate = new MockDate();
  64. mockDate.setTimezoneOffset([-480]);
  65. var fingerprint = goog.locale.timeZoneDetection.getFingerprint(mockDate);
  66. assertEquals(32, fingerprint);
  67. mockDate = new MockDate();
  68. mockDate.setTimezoneOffset(
  69. [480, 420, 420, 480, 480, 420, 420, 420, 420, 420, 420, 420, 420]);
  70. fingerprint = goog.locale.timeZoneDetection.getFingerprint(mockDate);
  71. assertEquals(1294772902, fingerprint);
  72. }
  73. function testDetectTimeZone() {
  74. var mockDate = new MockDate();
  75. mockDate.setTimezoneOffset([-480]);
  76. var timeZoneId =
  77. goog.locale.timeZoneDetection.detectTimeZone(undefined, mockDate);
  78. assertEquals('Asia/Hong_Kong', timeZoneId);
  79. mockDate = new MockDate();
  80. mockDate.setTimezoneOffset(
  81. [480, 420, 420, 480, 480, 420, 420, 420, 420, 420, 420, 420, 420]);
  82. timeZoneId = goog.locale.timeZoneDetection.detectTimeZone('US', mockDate);
  83. assertEquals('America/Los_Angeles', timeZoneId);
  84. mockDate = new MockDate();
  85. mockDate.setTimezoneOffset(
  86. [480, 420, 420, 480, 480, 420, 420, 420, 420, 420, 420, 420, 420]);
  87. timeZoneId = goog.locale.timeZoneDetection.detectTimeZone('CA', mockDate);
  88. assertEquals('America/Dawson', timeZoneId);
  89. }
  90. function testGetTimeZoneList() {
  91. var mockDate = new MockDate();
  92. mockDate.setTimezoneOffset(
  93. [480, 420, 420, 480, 480, 420, 420, 420, 420, 420, 420, 420, 420]);
  94. var timeZoneList =
  95. goog.locale.timeZoneDetection.getTimeZoneList(undefined, mockDate);
  96. assertEquals('America/Los_Angeles', timeZoneList[0]);
  97. assertEquals('America/Whitehorse', timeZoneList[4]);
  98. assertEquals(5, timeZoneList.length);
  99. mockDate = new MockDate();
  100. mockDate.setTimezoneOffset([-480]);
  101. timeZoneList =
  102. goog.locale.timeZoneDetection.getTimeZoneList(undefined, mockDate);
  103. assertEquals('Asia/Hong_Kong', timeZoneList[0]);
  104. assertEquals('Asia/Chongqing', timeZoneList[7]);
  105. assertEquals(16, timeZoneList.length);
  106. timeZoneList = goog.locale.timeZoneDetection.getTimeZoneList('AU', mockDate);
  107. assertEquals(1, timeZoneList.length);
  108. assertEquals('Australia/Perth', timeZoneList[0]);
  109. }