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