Debian Codegraph @

closure-library/closure/goog/labs/style/pixeldensitymonitor_test.js

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