File: pixeldensitymonitor_test.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (143 lines) | stat: -rw-r--r-- 4,254 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Tests for goog.labs.style.PixelDensityMonitor.
 *
 */

goog.provide('goog.labs.style.PixelDensityMonitorTest');
goog.setTestOnly('goog.labs.style.PixelDensityMonitorTest');

goog.require('goog.array');
goog.require('goog.dom.DomHelper');
goog.require('goog.events');
goog.require('goog.labs.style.PixelDensityMonitor');
goog.require('goog.testing.MockControl');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.recordFunction');

var fakeWindow;
var recordFunction;
var monitor;
var mockControl;
var mediaQueryLists;

function setUp() {
  recordFunction = goog.testing.recordFunction();
  mediaQueryLists = [];
  mockControl = new goog.testing.MockControl();
}

function tearDown() {
  mockControl.$verifyAll();
  goog.dispose(monitor);
  goog.dispose(recordFunction);
}

function setUpMonitor(initialRatio, hasMatchMedia) {
  fakeWindow = {devicePixelRatio: initialRatio};

  if (hasMatchMedia) {
    // Every call to matchMedia should return a new media query list with its
    // own set of listeners.
    fakeWindow.matchMedia = function(query) {
      var listeners = [];
      var newList = {
        addListener: function(listener) { listeners.push(listener); },
        removeListener: function(listener) {
          goog.array.remove(listeners, listener);
        },
        callListeners: function() {
          for (var i = 0; i < listeners.length; i++) {
            listeners[i]();
          }
        },
        getListenerCount: function() { return listeners.length; }
      };
      mediaQueryLists.push(newList);
      return newList;
    };
  }

  var domHelper = mockControl.createStrictMock(goog.dom.DomHelper);
  domHelper.getWindow().$returns(fakeWindow);
  mockControl.$replayAll();

  monitor = new goog.labs.style.PixelDensityMonitor(domHelper);
  goog.events.listen(
      monitor, goog.labs.style.PixelDensityMonitor.EventType.CHANGE,
      recordFunction);
}

function setNewRatio(newRatio) {
  fakeWindow.devicePixelRatio = newRatio;
  for (var i = 0; i < mediaQueryLists.length; i++) {
    mediaQueryLists[i].callListeners();
  }
}

function testNormalDensity() {
  setUpMonitor(1, false);
  assertEquals(
      goog.labs.style.PixelDensityMonitor.Density.NORMAL, monitor.getDensity());
}

function testHighDensity() {
  setUpMonitor(1.5, false);
  assertEquals(
      goog.labs.style.PixelDensityMonitor.Density.HIGH, monitor.getDensity());
}

function testNormalDensityIfUndefined() {
  setUpMonitor(undefined, false);
  assertEquals(
      goog.labs.style.PixelDensityMonitor.Density.NORMAL, monitor.getDensity());
}

function testChangeEvent() {
  setUpMonitor(1, true);
  assertEquals(
      goog.labs.style.PixelDensityMonitor.Density.NORMAL, monitor.getDensity());
  monitor.start();

  setNewRatio(2);
  var call = recordFunction.popLastCall();
  assertEquals(
      goog.labs.style.PixelDensityMonitor.Density.HIGH,
      call.getArgument(0).target.getDensity());
  assertEquals(
      goog.labs.style.PixelDensityMonitor.Density.HIGH, monitor.getDensity());

  setNewRatio(1);
  call = recordFunction.popLastCall();
  assertEquals(
      goog.labs.style.PixelDensityMonitor.Density.NORMAL,
      call.getArgument(0).target.getDensity());
  assertEquals(
      goog.labs.style.PixelDensityMonitor.Density.NORMAL, monitor.getDensity());
}

function testListenerIsDisposed() {
  setUpMonitor(1, true);
  monitor.start();

  assertEquals(1, mediaQueryLists.length);
  assertEquals(1, mediaQueryLists[0].getListenerCount());

  goog.dispose(monitor);

  assertEquals(1, mediaQueryLists.length);
  assertEquals(0, mediaQueryLists[0].getListenerCount());
}