2015-08-12 10:33:11 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015 Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
describe('jquery.avatar tests', function() {
|
|
|
|
|
|
|
|
var $div;
|
2017-11-06 11:57:59 +03:00
|
|
|
var devicePixelRatio;
|
2015-08-12 10:33:11 +03:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
$('#testArea').append($('<div id="avatardiv">'));
|
|
|
|
$div = $('#avatardiv');
|
|
|
|
|
|
|
|
devicePixelRatio = window.devicePixelRatio;
|
|
|
|
window.devicePixelRatio = 1;
|
2017-12-19 12:02:19 +03:00
|
|
|
|
|
|
|
spyOn(window, 'Image').and.returnValue({
|
|
|
|
onload: function() {
|
|
|
|
},
|
|
|
|
onerror: function() {
|
|
|
|
}
|
|
|
|
});
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
$div.remove();
|
|
|
|
|
2017-11-06 11:57:59 +03:00
|
|
|
window.devicePixelRatio = devicePixelRatio;
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('size', function() {
|
|
|
|
it('undefined', function() {
|
|
|
|
$div.avatar('foo');
|
|
|
|
|
2020-12-29 10:35:09 +03:00
|
|
|
expect(Math.round($div.height())).toEqual(64);
|
|
|
|
expect(Math.round($div.width())).toEqual(64);
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('undefined but div has height', function() {
|
|
|
|
$div.height(9);
|
|
|
|
$div.avatar('foo');
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onerror();
|
|
|
|
|
2020-12-29 10:35:09 +03:00
|
|
|
expect(Math.round($div.height())).toEqual(9);
|
|
|
|
expect(Math.round($div.width())).toEqual(9);
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('undefined but data size is set', function() {
|
|
|
|
$div.data('size', 10);
|
|
|
|
$div.avatar('foo');
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onerror();
|
|
|
|
|
2020-12-29 10:35:09 +03:00
|
|
|
expect(Math.round($div.height())).toEqual(10);
|
|
|
|
expect(Math.round($div.width())).toEqual(10);
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('defined', function() {
|
|
|
|
$div.avatar('foo', 8);
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onerror();
|
|
|
|
|
2020-12-29 10:35:09 +03:00
|
|
|
expect(Math.round($div.height())).toEqual(8);
|
|
|
|
expect(Math.round($div.width())).toEqual(8);
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('undefined user', function() {
|
|
|
|
spyOn($div, 'imageplaceholder');
|
2017-09-20 18:19:35 +03:00
|
|
|
spyOn($div, 'css');
|
2015-08-12 10:33:11 +03:00
|
|
|
|
|
|
|
$div.avatar();
|
|
|
|
|
2016-11-23 20:02:23 +03:00
|
|
|
expect($div.imageplaceholder).toHaveBeenCalledWith('?');
|
2017-09-20 18:19:35 +03:00
|
|
|
expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('no avatar', function() {
|
|
|
|
it('show placeholder for existing user', function() {
|
|
|
|
spyOn($div, 'imageplaceholder');
|
2017-12-19 12:02:19 +03:00
|
|
|
$div.avatar('foo', undefined, undefined, undefined, undefined, 'bar');
|
2015-08-12 10:33:11 +03:00
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onerror();
|
2015-08-12 10:33:11 +03:00
|
|
|
expect($div.imageplaceholder).toHaveBeenCalledWith('foo', 'bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('show placeholder for non existing user', function() {
|
|
|
|
spyOn($div, 'imageplaceholder');
|
2017-09-20 18:19:35 +03:00
|
|
|
spyOn($div, 'css');
|
2015-08-12 10:33:11 +03:00
|
|
|
$div.avatar('foo');
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onerror();
|
2015-08-12 10:33:11 +03:00
|
|
|
|
2017-09-20 19:14:00 +03:00
|
|
|
expect($div.imageplaceholder).toHaveBeenCalledWith('?');
|
2017-09-20 18:19:35 +03:00
|
|
|
expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
it('show no placeholder is ignored', function() {
|
2015-08-12 10:33:11 +03:00
|
|
|
spyOn($div, 'imageplaceholder');
|
2017-12-19 12:02:19 +03:00
|
|
|
spyOn($div, 'css');
|
2015-08-12 10:33:11 +03:00
|
|
|
$div.avatar('foo', undefined, undefined, true);
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onerror();
|
2015-08-12 10:33:11 +03:00
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect($div.imageplaceholder).toHaveBeenCalledWith('?');
|
|
|
|
expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('url generation', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
window.devicePixelRatio = 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('default', function() {
|
|
|
|
window.devicePixelRatio = 1;
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('high DPI icon', function() {
|
|
|
|
window.devicePixelRatio = 4;
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/128');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('high DPI icon round up size', function() {
|
|
|
|
window.devicePixelRatio = 1.9;
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/61');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('valid avatar', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
window.devicePixelRatio = 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('default (no ie8 fix)', function() {
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onload();
|
2015-08-12 10:33:11 +03:00
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image().height).toEqual(32);
|
|
|
|
expect(window.Image().width).toEqual(32);
|
|
|
|
expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('default high DPI icon', function() {
|
|
|
|
window.devicePixelRatio = 1.9;
|
|
|
|
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onload();
|
2015-08-12 10:33:11 +03:00
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image().height).toEqual(32);
|
|
|
|
expect(window.Image().width).toEqual(32);
|
|
|
|
expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/61');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
it('with ie8 fix (ignored)', function() {
|
2015-08-12 10:33:11 +03:00
|
|
|
$div.avatar('foo', 32, true);
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onload();
|
2015-08-12 10:33:11 +03:00
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image().height).toEqual(32);
|
|
|
|
expect(window.Image().width).toEqual(32);
|
|
|
|
expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('unhide div', function() {
|
|
|
|
$div.hide();
|
|
|
|
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onload();
|
|
|
|
|
|
|
|
expect(window.Image().height).toEqual(32);
|
|
|
|
expect(window.Image().width).toEqual(32);
|
|
|
|
expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('callback called', function() {
|
|
|
|
var observer = {callback: function() { dump("FOO"); }};
|
|
|
|
|
|
|
|
spyOn(observer, 'callback');
|
|
|
|
|
|
|
|
$div.avatar('foo', 32, undefined, undefined, function() {
|
|
|
|
observer.callback();
|
|
|
|
});
|
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image).toHaveBeenCalled();
|
|
|
|
window.Image().onload();
|
2015-08-12 10:33:11 +03:00
|
|
|
|
2017-12-19 12:02:19 +03:00
|
|
|
expect(window.Image().height).toEqual(32);
|
|
|
|
expect(window.Image().width).toEqual(32);
|
|
|
|
expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
|
2015-08-12 10:33:11 +03:00
|
|
|
expect(observer.callback).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|