Debian Codegraph @

closure-library/closure/goog/ui/emoji/fast_nonprogressive_emojipicker_test.js

raw
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 goog.provide('goog.ui.emoji.FastNonProgressiveEmojiPickerTest');
16 goog.setTestOnly('goog.ui.emoji.FastNonProgressiveEmojiPickerTest');
17
18 goog.require('goog.Promise');
19 goog.require('goog.dom.classlist');
20 goog.require('goog.events');
21 goog.require('goog.events.EventType');
22 goog.require('goog.net.EventType');
23 goog.require('goog.style');
24 goog.require('goog.testing.jsunit');
25 goog.require('goog.ui.emoji.Emoji');
26 goog.require('goog.ui.emoji.EmojiPicker');
27 goog.require('goog.ui.emoji.SpriteInfo');
28
29
30 var images;
31 var palette;
32 var picker;
33 var sprite = '../../demos/emoji/sprite.png';
34 var sprite2 = '../../demos/emoji/sprite2.png';
35
36
37 /**
38 * Creates a SpriteInfo object with the specified properties. If the image is
39 * sprited via CSS, then only the first parameter needs a value. If the image
40 * is sprited via metadata, then the first parameter should be left null.
41 *
42 * @param {?string} cssClass CSS class to properly display the sprited image.
43 * @param {string=} opt_url Url of the sprite image.
44 * @param {number=} opt_width Width of the image being sprited.
45 * @param {number=} opt_height Height of the image being sprited.
46 * @param {number=} opt_xOffset Positive x offset of the image being sprited
47 * within the sprite.
48 * @param {number=} opt_yOffset Positive y offset of the image being sprited
49 * within the sprite.
50 * @param {boolean=} opt_animated Whether the sprite info is for an animated
51 * emoji.
52 */
53 function si(
54 cssClass, opt_url, opt_width, opt_height, opt_xOffset, opt_yOffset,
55 opt_animated) {
56 return new goog.ui.emoji.SpriteInfo(
57 cssClass, opt_url, opt_width, opt_height, opt_xOffset, opt_yOffset,
58 opt_animated);
59 }
60
61
62 // This group contains a mix of sprited emoji via css, sprited emoji via
63 // metadata, and non-sprited emoji.
64 var spritedEmoji2 = [
65 'Emoji 1',
66 [
67 ['../../demos/emoji/200.gif', 'std.200', si('SPRITE_200')],
68 ['../../demos/emoji/201.gif', 'std.201', si('SPRITE_201')],
69 ['../../demos/emoji/202.gif', 'std.202', si('SPRITE_202')],
70 ['../../demos/emoji/203.gif', 'std.203', si('SPRITE_203')],
71 ['../../demos/emoji/204.gif', 'std.204', si('SPRITE_204')],
72 ['../../demos/emoji/205.gif', 'std.205', si('SPRITE_205')],
73 ['../../demos/emoji/206.gif', 'std.206', si('SPRITE_206')],
74 ['../../demos/emoji/2BC.gif', 'std.2BC', si('SPRITE_2BC')],
75 ['../../demos/emoji/2BD.gif', 'std.2BD', si('SPRITE_2BD')],
76 ['../../demos/emoji/2BE.gif', 'std.2BE', si(null, sprite, 18, 18, 36, 54)],
77 ['../../demos/emoji/2BF.gif', 'std.2BF', si(null, sprite, 18, 18, 0, 126)],
78 ['../../demos/emoji/2C0.gif', 'std.2C0', si(null, sprite, 18, 18, 18, 305)],
79 ['../../demos/emoji/2C1.gif', 'std.2C1', si(null, sprite, 18, 18, 0, 287)],
80 ['../../demos/emoji/2C2.gif', 'std.2C2', si(null, sprite, 18, 18, 18, 126)],
81 ['../../demos/emoji/2C3.gif', 'std.2C3', si(null, sprite, 18, 18, 36, 234)],
82 ['../../demos/emoji/2C4.gif', 'std.2C4', si(null, sprite, 18, 18, 36, 72)],
83 ['../../demos/emoji/2C5.gif', 'std.2C5', si(null, sprite, 18, 18, 54, 54)],
84 ['../../demos/emoji/2C6.gif', 'std.2C6'],
85 ['../../demos/emoji/2C7.gif', 'std.2C7'],
86 ['../../demos/emoji/2C8.gif', 'std.2C8'],
87 ['../../demos/emoji/2C9.gif', 'std.2C9'],
88 [
89 '../../demos/emoji/2CA.gif', 'std.2CA',
90 si(null, sprite2, 18, 20, 36, 72, 1)
91 ],
92 [
93 '../../demos/emoji/2E3.gif', 'std.2E3', si(null, sprite2, 18, 18, 0, 0, 1)
94 ],
95 [
96 '../../demos/emoji/2EF.gif', 'std.2EF',
97 si(null, sprite2, 18, 20, 0, 300, 1)
98 ],
99 [
100 '../../demos/emoji/2F1.gif', 'std.2F1',
101 si(null, sprite2, 18, 18, 0, 320, 1)
102 ]
103 ]
104 ];
105
106
107 /**
108 * Returns true if the two paths end with the same file.
109 *
110 * E.g., ('../../cool.gif', 'file:///home/usr/somewhere/cool.gif') --> true
111 *
112 * @param {string} path1 First url
113 * @param {string} path2 Second url
114 */
115 function checkPathsEndWithSameFile(path1, path2) {
116 var pieces1 = path1.split('/');
117 var file1 = pieces1[pieces1.length - 1];
118 var pieces2 = path2.split('/');
119 var file2 = pieces2[pieces2.length - 1];
120
121 return file1 == file2;
122 }
123
124
125 /**
126 * Checks and verifies the structure of a non-progressive fast-loading picker
127 * after the animated emoji have loaded.
128 */
129 function testStructure() {
130 var emoji = spritedEmoji2;
131
132 for (var i = 0; i < emoji[1].length; i++) {
133 palette.setSelectedIndex(i);
134 var emojiInfo = emoji[1][i];
135 var cell = palette.getSelectedItem();
136 var id = cell.getAttribute(goog.ui.emoji.Emoji.ATTRIBUTE);
137 var inner = /** @type {Element} */ (cell.firstChild);
138
139 // Check that the cell is a div wrapped around something else, and that the
140 // outer div contains the goomoji attribute
141 assertEquals(
142 'The palette item should be a div wrapped around something',
143 cell.tagName, 'DIV');
144 assertNotNull('The outer div is not wrapped around another element', inner);
145 assertEquals(
146 'The palette item should have the goomoji attribute',
147 cell.getAttribute(goog.ui.emoji.Emoji.ATTRIBUTE), emojiInfo[1]);
148 assertEquals(
149 'The palette item should have the data-goomoji attribute',
150 cell.getAttribute(goog.ui.emoji.Emoji.DATA_ATTRIBUTE), emojiInfo[1]);
151
152 // Now check the contents of the cells
153 var url = emojiInfo[0]; // url of the animated emoji
154 var spriteInfo = emojiInfo[2];
155 if (spriteInfo) {
156 assertEquals(inner.tagName, 'DIV');
157 if (spriteInfo.isAnimated()) {
158 var img = images[id];
159 checkPathsEndWithSameFile(
160 goog.style.getStyle(inner, 'background-image'), url);
161 assertEquals(
162 String(img.width), goog.style.getStyle(inner, 'width')
163 .replace(/px/g, '')
164 .replace(/pt/g, ''));
165 assertEquals(
166 String(img.height), goog.style.getStyle(inner, 'height')
167 .replace(/px/g, '')
168 .replace(/pt/g, ''));
169 assertEquals(
170 '0 0', goog.style.getStyle(inner, 'background-position')
171 .replace(/px/g, '')
172 .replace(/pt/g, ''));
173 } else {
174 var cssClass = spriteInfo.getCssClass();
175 if (cssClass) {
176 assertTrue(
177 'Sprite should have its CSS class set',
178 goog.dom.classlist.contains(inner, cssClass));
179 } else {
180 checkPathsEndWithSameFile(
181 goog.style.getStyle(inner, 'background-image'),
182 spriteInfo.getUrl());
183 assertEquals(
184 spriteInfo.getWidthCssValue(),
185 goog.style.getStyle(inner, 'width'));
186 assertEquals(
187 spriteInfo.getHeightCssValue(),
188 goog.style.getStyle(inner, 'height'));
189 assertEquals(
190 (spriteInfo.getXOffsetCssValue() + ' ' +
191 spriteInfo.getYOffsetCssValue())
192 .replace(/px/g, '')
193 .replace(/pt/g, ''),
194 goog.style.getStyle(inner, 'background-position')
195 .replace(/px/g, '')
196 .replace(/pt/g, ''));
197 }
198 }
199 } else {
200 // A non-sprited emoji is just an img
201 assertEquals(inner.tagName, 'IMG');
202 checkPathsEndWithSameFile(inner.src, emojiInfo[0]);
203 }
204 }
205 }
206
207
208 function setUp() {
209 var defaultImg = '../../demos/emoji/none.gif';
210 picker = new goog.ui.emoji.EmojiPicker(defaultImg);
211 picker.setDelayedLoad(false);
212 picker.setManualLoadOfAnimatedEmoji(true);
213 picker.setProgressiveRender(false);
214 picker.addEmojiGroup(spritedEmoji2[0], spritedEmoji2[1]);
215 picker.render();
216
217 palette = picker.getPage(0);
218 var imageLoader = palette.getImageLoader();
219 images = {};
220
221 return new goog.Promise(function(resolve, reject) {
222 goog.events.listen(imageLoader, goog.net.EventType.COMPLETE, resolve);
223 goog.events.listen(imageLoader, goog.events.EventType.LOAD, function(e) {
224 var image = e.target;
225 images[image.id] = image;
226 });
227
228 // Now we load the animated emoji and check the structure again. The
229 // animated emoji will be different.
230 picker.manuallyLoadAnimatedEmoji();
231 });
232 }
233
234
235 function tearDown() {
236 picker.dispose();
237 }