pixeldensitymonitor_test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 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 Tests for goog.labs.style.PixelDensityMonitor.
  16. *
  17. */
  18. goog.provide('goog.labs.style.PixelDensityMonitorTest');
  19. goog.setTestOnly('goog.labs.style.PixelDensityMonitorTest');
  20. goog.require('goog.array');
  21. goog.require('goog.dom.DomHelper');
  22. goog.require('goog.events');
  23. goog.require('goog.labs.style.PixelDensityMonitor');
  24. goog.require('goog.testing.MockControl');
  25. goog.require('goog.testing.jsunit');
  26. goog.require('goog.testing.recordFunction');
  27. var fakeWindow;
  28. var recordFunction;
  29. var monitor;
  30. var mockControl;
  31. var mediaQueryLists;
  32. function setUp() {
  33. recordFunction = goog.testing.recordFunction();
  34. mediaQueryLists = [];
  35. mockControl = new goog.testing.MockControl();
  36. }
  37. function tearDown() {
  38. mockControl.$verifyAll();
  39. goog.dispose(monitor);
  40. goog.dispose(recordFunction);
  41. }
  42. function setUpMonitor(initialRatio, hasMatchMedia) {
  43. fakeWindow = {devicePixelRatio: initialRatio};
  44. if (hasMatchMedia) {
  45. // Every call to matchMedia should return a new media query list with its
  46. // own set of listeners.
  47. fakeWindow.matchMedia = function(query) {
  48. var listeners = [];
  49. var newList = {
  50. addListener: function(listener) { listeners.push(listener); },
  51. removeListener: function(listener) {
  52. goog.array.remove(listeners, listener);
  53. },
  54. callListeners: function() {
  55. for (var i = 0; i < listeners.length; i++) {
  56. listeners[i]();
  57. }
  58. },
  59. getListenerCount: function() { return listeners.length; }
  60. };
  61. mediaQueryLists.push(newList);
  62. return newList;
  63. };
  64. }
  65. var domHelper = mockControl.createStrictMock(goog.dom.DomHelper);
  66. domHelper.getWindow().$returns(fakeWindow);
  67. mockControl.$replayAll();
  68. monitor = new goog.labs.style.PixelDensityMonitor(domHelper);
  69. goog.events.listen(
  70. monitor, goog.labs.style.PixelDensityMonitor.EventType.CHANGE,
  71. recordFunction);
  72. }
  73. function setNewRatio(newRatio) {
  74. fakeWindow.devicePixelRatio = newRatio;
  75. for (var i = 0; i < mediaQueryLists.length; i++) {
  76. mediaQueryLists[i].callListeners();
  77. }
  78. }
  79. function testNormalDensity() {
  80. setUpMonitor(1, false);
  81. assertEquals(
  82. goog.labs.style.PixelDensityMonitor.Density.NORMAL, monitor.getDensity());
  83. }
  84. function testHighDensity() {
  85. setUpMonitor(1.5, false);
  86. assertEquals(
  87. goog.labs.style.PixelDensityMonitor.Density.HIGH, monitor.getDensity());
  88. }
  89. function testNormalDensityIfUndefined() {
  90. setUpMonitor(undefined, false);
  91. assertEquals(
  92. goog.labs.style.PixelDensityMonitor.Density.NORMAL, monitor.getDensity());
  93. }
  94. function testChangeEvent() {
  95. setUpMonitor(1, true);
  96. assertEquals(
  97. goog.labs.style.PixelDensityMonitor.Density.NORMAL, monitor.getDensity());
  98. monitor.start();
  99. setNewRatio(2);
  100. var call = recordFunction.popLastCall();
  101. assertEquals(
  102. goog.labs.style.PixelDensityMonitor.Density.HIGH,
  103. call.getArgument(0).target.getDensity());
  104. assertEquals(
  105. goog.labs.style.PixelDensityMonitor.Density.HIGH, monitor.getDensity());
  106. setNewRatio(1);
  107. call = recordFunction.popLastCall();
  108. assertEquals(
  109. goog.labs.style.PixelDensityMonitor.Density.NORMAL,
  110. call.getArgument(0).target.getDensity());
  111. assertEquals(
  112. goog.labs.style.PixelDensityMonitor.Density.NORMAL, monitor.getDensity());
  113. }
  114. function testListenerIsDisposed() {
  115. setUpMonitor(1, true);
  116. monitor.start();
  117. assertEquals(1, mediaQueryLists.length);
  118. assertEquals(1, mediaQueryLists[0].getListenerCount());
  119. goog.dispose(monitor);
  120. assertEquals(1, mediaQueryLists.length);
  121. assertEquals(0, mediaQueryLists[0].getListenerCount());
  122. }