3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or (at
9
* your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21
GalleryCoreApi::requireOnce('modules/core/classes/GalleryToolkit.class');
24
* Test Toolkit functionality
25
* @package GalleryCore
27
* @author Bharat Mediratta <bharat@menalto.com>
28
* @version $Revision: 15513 $
30
class ToolkitTest extends GalleryTestCase {
32
function ToolkitTest($methodName) {
33
$this->GalleryTestCase($methodName);
39
/* Register a dummy toolkit */
40
$path = 'modules/core/test/phpunit/ToolkitTest.class';
41
$ret = GalleryCoreApi::registerFactoryImplementation(
42
'GalleryToolkit', 'TestToolkit', 'TestToolkit',
43
$path, 'coreTest', null);
45
print $ret->getAsHtml();
46
return $this->failWithStatus($ret);
49
$ret = GalleryCoreApi::registerFactoryImplementation(
50
'GalleryToolkit', 'TestToolkit2', 'TestToolkit2',
51
$path, 'coreTest', null);
53
print $ret->getAsHtml();
54
return $this->failWithStatus($ret);
56
$this->_markToolkitForCleanup('TestToolkit');
57
$this->_markToolkitForCleanup('TestToolkit2');
60
function testOperation() {
61
$params = array(array('type' => 'test-type1',
62
'description' => 'test-description1'),
63
array('type' => 'test-type2',
64
'description' => 'test-description2')
66
$ret = GalleryCoreApi::registerToolkitOperation('TestToolkit',
73
return $this->failWithStatus($ret);
76
/* Get the operation */
77
list ($ret, $operations) = GalleryCoreApi::getToolkitOperations('test/type');
79
return $this->failWithStatus($ret);
82
$this->assertEquals(1, sizeof($operations));
83
$this->assertEquals('test-operation', $operations[0]['name']);
84
$this->assertEquals('test/outputType', $operations[0]['outputMimeType']);
85
$this->assertEquals('test-description', $operations[0]['description']);
86
$this->assertEquals($params, $operations[0]['parameters']);
89
list ($ret, $toolkit, $outputMimeType) =
90
GalleryCoreApi::getToolkitByOperation('test/type', 'test-operation');
92
return $this->failWithStatus($ret);
95
$this->assertEquals(new TestToolkit(), $toolkit);
96
$this->assertEquals('test/outputType', $outputMimeType);
98
/* Register another toolkit with same operation/mimetype, higher priority */
99
$ret = GalleryCoreApi::registerToolkitOperation('TestToolkit2',
104
'test/outputType', 3);
106
return $this->failWithStatus($ret);
109
/* Get the toolkit */
110
list ($ret, $toolkit, $outputMimeType) =
111
GalleryCoreApi::getToolkitByOperation('test/type', 'test-operation');
113
return $this->failWithStatus($ret);
116
$this->assertEquals(new TestToolkit2(), $toolkit);
117
$this->assertEquals('test/outputType', $outputMimeType);
120
$ret = GalleryCoreApi::unregisterToolkit('TestToolkit');
122
return $this->failWithStatus($ret);
124
$ret = GalleryCoreApi::unregisterToolkit('TestToolkit2');
126
return $this->failWithStatus($ret);
129
/* Get the toolkit */
130
list ($ret, $toolkit, $outputMimeType) =
131
GalleryCoreApi::getToolkitByOperation('test/type', 'test-operation');
133
return $this->failWithStatus($ret);
136
$this->assertEquals(null, $toolkit);
139
function testProperty() {
140
$ret = GalleryCoreApi::registerToolkitProperty('TestToolkit',
146
return $this->failWithStatus($ret);
149
/* Get the property */
150
list ($ret, $properties) = GalleryCoreApi::getToolkitProperties('test/type');
152
return $this->failWithStatus($ret);
155
$this->assertEquals(1, sizeof($properties));
156
$this->assertEquals('test-property', $properties[0]['name']);
157
$this->assertEquals('int', $properties[0]['type']);
158
$this->assertEquals('test-description', $properties[0]['description']);
160
/* Get the toolkit */
161
list ($ret, $toolkit) =
162
GalleryCoreApi::getToolkitByProperty('test/type', 'test-property');
164
return $this->failWithStatus($ret);
167
$this->assertEquals(new TestToolkit(), $toolkit);
170
$ret = GalleryCoreApi::unregisterToolkit('TestToolkit');
172
return $this->failWithStatus($ret);
175
/* Get the toolkit */
176
list ($ret, $toolkit) =
177
GalleryCoreApi::getToolkitByProperty('test/type', 'test-property');
179
return $this->failWithStatus($ret);
182
$this->assertEquals(null, $toolkit);
185
function testPropertyMultipleToolkits() {
186
$ret = GalleryCoreApi::registerToolkitProperty('TestToolkit',
192
return $this->failWithStatus($ret);
194
$ret = GalleryCoreApi::registerToolkitProperty('TestToolkit2',
200
return $this->failWithStatus($ret);
203
/* Get the toolkits */
204
list ($ret, $toolkits) =
205
GalleryCoreApi::getToolkitsByProperty('test/type', 'test-property');
207
return $this->failWithStatus($ret);
210
/* Order is non-deterministic (varies by db) */
211
$this->assert((GalleryUtilities::isA($toolkits[0], 'TestToolkit') &&
212
GalleryUtilities::isA($toolkits[1], 'TestToolkit2')) ||
213
(GalleryUtilities::isA($toolkits[0], 'TestToolkit2') &&
214
GalleryUtilities::isA($toolkits[1], 'TestToolkit')),
218
$ret = GalleryCoreApi::unregisterToolkit('TestToolkit2');
220
return $this->failWithStatus($ret);
222
$ret = GalleryCoreApi::unregisterToolkit('TestToolkit');
224
return $this->failWithStatus($ret);
227
/* Get the toolkits */
228
list ($ret, $toolkitIds) =
229
GalleryCoreApi::getToolkitByProperty('test/type', 'test-property');
231
return $this->failWithStatus($ret);
234
$this->assertEquals(null, $toolkitIds);
237
function testOperationSequence() {
238
$params = array(array('type' => 'test-type1',
239
'description' => 'test-description1'),
240
array('type' => 'test-type2',
241
'description' => 'test-description2')
243
$ret = GalleryCoreApi::registerToolkitOperation('TestToolkit',
244
array('test/type1', 'test/unused'),
250
return $this->failWithStatus($ret);
253
$ret = GalleryCoreApi::registerToolkitOperation('TestToolkit',
260
return $this->failWithStatus($ret);
263
list ($ret, $isSupported, $outputMimeType) =
264
GalleryCoreApi::isSupportedOperationSequence('test/type1',
265
'test-operation1|1,2;test-operation2|1,2');
267
return $this->failWithStatus($ret);
270
$this->assert($isSupported, '1-1');
271
$this->assertEquals('test/type3', $outputMimeType, '1-2');
273
/* Unregister operation for single mime-type */
274
$ret = GalleryCoreApi::unregisterToolkitOperation('TestToolkit', 'test-operation1',
275
array('test/unused', 'test/nonexist'));
277
return $this->failWithStatus($ret);
280
list ($ret, $isSupported, $outputMimeType) =
281
GalleryCoreApi::isSupportedOperationSequence('test/type1',
282
'test-operation1|1,2;test-operation2|1,2');
284
return $this->failWithStatus($ret);
287
$this->assert($isSupported, '2-1');
288
$this->assertEquals('test/type3', $outputMimeType, '2-2');
290
/* Unregister single operation */
291
$ret = GalleryCoreApi::unregisterToolkitOperation('TestToolkit', 'test-operation2');
293
return $this->failWithStatus($ret);
296
list ($ret, $isSupported, $outputMimeType) =
297
GalleryCoreApi::isSupportedOperationSequence('test/type1',
298
'test-operation1|1,2;test-operation2|1,2');
300
return $this->failWithStatus($ret);
303
$this->assert(!$isSupported, '3-1');
305
/* Unregister toolkit */
306
$ret = GalleryCoreApi::unregisterToolkit('TestToolkit');
308
return $this->failWithStatus($ret);
311
list ($ret, $isSupported, $outputMimeType) =
312
GalleryCoreApi::isSupportedOperationSequence('test/type1',
313
'test-operation1|1,2;test-operation2|1,2');
315
return $this->failWithStatus($ret);
318
$this->assert(!$isSupported, '4-1');
321
function testGetOperationMimeTypes() {
323
$params = array(array('type' => 'test-type1',
324
'description' => 'test-description1'),
325
array('type' => 'test-type2',
326
'description' => 'test-description2')
328
$ret = GalleryCoreApi::registerToolkitOperation('TestToolkit',
329
array('test/type', 'test/type2'),
335
return $this->failWithStatus($ret);
338
list ($ret, $data) = GalleryCoreApi::getToolkitOperationMimeTypes('test-operation');
340
return $this->failWithStatus($ret);
342
$this->assert(isset($data['test/type']) && in_array('TestToolkit', $data['test/type']),
343
'Operation mime type');
346
function testEstimateDerivativeDimensions() {
347
list ($ret, $source) = GalleryCoreApi::newItemByMimeType('image/jpeg');
349
return $this->failWithStatus($ret);
351
$this->assert(GalleryUtilities::isA($source, 'GalleryPhotoItem'));
353
$source->setWidth(1024);
354
$source->setHeight(768);
356
list ($ret, $derivative) =
357
GalleryCoreApi::newFactoryInstanceByHint('GalleryDerivative', $source->getEntityType());
359
return $this->failWithStatus($ret);
361
$this->assert(GalleryUtilities::isA($derivative, 'GalleryDerivativeImage'));
363
$derivative->setDerivativeOperations('thumbnail|200');
364
$derivative->setWidth(0);
365
$derivative->setHeight(0);
367
$ret = GalleryCoreApi::estimateDerivativeDimensions($derivative, $source);
369
return $this->failWithStatus($ret);
372
$this->assertEquals(200, $derivative->getWidth());
373
$this->assertEquals(150, $derivative->getHeight());
376
function testEstimateDerivativeDimensions2() {
377
list ($ret, $source) = GalleryCoreApi::newItemByMimeType('image/jpeg');
379
return $this->failWithStatus($ret);
381
$this->assert(GalleryUtilities::isA($source, 'GalleryPhotoItem'));
383
$source->setWidth(1024);
384
$source->setHeight(768);
386
list ($ret, $derivative) =
387
GalleryCoreApi::newFactoryInstanceByHint('GalleryDerivative', $source->getEntityType());
389
return $this->failWithStatus($ret);
391
$this->assert(GalleryUtilities::isA($derivative, 'GalleryDerivativeImage'));
393
$derivative->setDerivativeOperations('scale|150');
394
$derivative->setWidth(0);
395
$derivative->setHeight(0);
397
$ret = GalleryCoreApi::estimateDerivativeDimensions($derivative, $source);
399
return $this->failWithStatus($ret);
402
$this->assertEquals(150, $derivative->getWidth());
403
$this->assertEquals(113, $derivative->getHeight());
406
function testEstimateDerivativeDimensions3() {
407
list ($ret, $source) = GalleryCoreApi::newItemByMimeType('image/jpeg');
409
return $this->failWithStatus($ret);
411
$this->assert(GalleryUtilities::isA($source, 'GalleryPhotoItem'));
413
$source->setWidth(1024);
414
$source->setHeight(768);
416
list ($ret, $derivative) =
417
GalleryCoreApi::newFactoryInstanceByHint('GalleryDerivative', $source->getEntityType());
419
return $this->failWithStatus($ret);
421
$this->assert(GalleryUtilities::isA($derivative, 'GalleryDerivativeImage'));
423
$derivative->setDerivativeOperations('crop|1,2,3,4;scale|150');
424
$derivative->setWidth(0);
425
$derivative->setHeight(0);
427
$ret = GalleryCoreApi::estimateDerivativeDimensions($derivative, $source);
429
return $this->failWithStatus($ret);
432
$this->assertEquals(0, $derivative->getWidth());
433
$this->assertEquals(0, $derivative->getHeight());
436
function testEstimateDerivativeDimensions4() {
438
* Test that if the direct source is a derivative too and has smaller dimensions
439
* then we assume the "real" source is larger and allow upscaling in our estimate.
440
* This means our estimate could be too high if the real source actually is smaller
441
* than the desired size, but it will be corrected once the derivative is built.
443
list ($ret, $orig) = GalleryCoreApi::newItemByMimeType('image/jpeg');
445
return $this->failWithStatus($ret);
447
$this->assert(GalleryUtilities::isA($orig, 'GalleryPhotoItem'), 'photo item');
449
$orig->setWidth(400);
450
$orig->setHeight(300);
452
list ($ret, $source) =
453
GalleryCoreApi::newFactoryInstanceByHint('GalleryDerivative', $orig->getEntityType());
455
return $this->failWithStatus($ret);
457
$this->assert(GalleryUtilities::isA($source, 'GalleryDerivativeImage'), 'source deriv');
459
$source->setDerivativeOperations('thumbnail|100');
460
$source->setWidth(100);
461
$source->setHeight(75);
463
list ($ret, $derivative) =
464
GalleryCoreApi::newFactoryInstanceByHint('GalleryDerivative', $orig->getEntityType());
466
return $this->failWithStatus($ret);
468
$this->assert(GalleryUtilities::isA($derivative, 'GalleryDerivativeImage'), 'target deriv');
470
$derivative->setDerivativeOperations('thumbnail|200');
471
$derivative->setWidth(0);
472
$derivative->setHeight(0);
474
$ret = GalleryCoreApi::estimateDerivativeDimensions($derivative, $source);
476
return $this->failWithStatus($ret);
479
$this->assertEquals(200, $derivative->getWidth(), 'width 1');
480
$this->assertEquals(150, $derivative->getHeight(), 'height 1');
483
* Now verify if the direct source is not a derivative and is smaller than the
484
* desired size that we don't upsample.
486
$orig->setWidth(123);
487
$orig->setHeight(66);
489
$ret = GalleryCoreApi::estimateDerivativeDimensions($derivative, $orig);
491
return $this->failWithStatus($ret);
494
$this->assertEquals(123, $derivative->getWidth(), 'width 2');
495
$this->assertEquals(66, $derivative->getHeight(), 'height 2');
498
function testApplyTransform() {
499
$toolkit = new GalleryToolkit();
501
/* Rotate transform of cropped operation results in rotated coordinates*/
502
list ($success, $result) =
503
$toolkit->applyTransform('rotate|90', 'rotate|90;crop|15,15,40,50');
504
$this->assert($success);
505
$this->assertEquals('rotate|90;crop|35,15,50,40', $result, 'test 1');
507
/* Rotate transform of nothing returns nothing */
508
list ($success, $result) = $toolkit->applyTransform('rotate|90', '');
509
$this->assert($success);
510
$this->assertEquals('', $result, 'test 2');
512
/* Empty transform of cropped opreation returns original values */
513
list ($success, $result) = $toolkit->applyTransform('', 'rotate|90;crop|15,15,40,50');
514
$this->assert($success);
515
$this->assertEquals('rotate|90;crop|15,15,40,50', $result, 'test 3');
517
/* Reverse rotate transform of cropped operation results in rotated coordinates*/
518
list ($success, $result) =
519
$toolkit->applyTransform('rotate|90', 'rotate|90;crop|15,15,40,50', true);
520
$this->assert($success);
521
$this->assertEquals('rotate|90;crop|15,45,50,40', $result, 'test 4');
524
function testApplyTransformLocale() {
526
* Verify that percentages don't get written like 12,5 if the current locale
527
* uses a comma for the fraction separator.
529
$currentLocale = setlocale(LC_ALL, '0');
530
foreach (array('de_DE', 'german') as $locale) {
531
if (($newLocale = GalleryTranslator::_setlocale(LC_ALL, $locale)) !== false) {
535
if (empty($newLocale)) {
536
return $this->assert(false, 'Unable to select German locale');
539
$toolkit = new GalleryToolkit();
541
/* Rotate transform of cropped operation results in rotated coordinates */
542
list ($success, $result) =
543
$toolkit->applyTransform('rotate|90', 'rotate|90;crop|15,20.5,40.62,50');
544
$this->assert($success);
545
$this->assertEquals('rotate|90;crop|29.5,15,50,40.62', $result, 'test 1');
547
setlocale(LC_ALL, $currentLocale);
550
function testGetMaximumManagedPriority() {
551
$ret = GalleryCoreApi::registerToolkitOperation('TestToolkit',
552
array('test/type'), 'test-operation', array(),
553
'test-description', 'test/outputType', 40);
555
return $this->failWithStatus($ret);
558
list ($ret, $priority) = GalleryCoreApi::getMaximumManagedToolkitPriority();
560
return $this->failWithStatus($ret);
563
$this->assertEquals(40, $priority);
566
function testGetRedundantPriorities() {
567
$ret = GalleryCoreApi::registerToolkitOperation('TestToolkit',
568
array('test/type'), 'test-operation', array(),
569
'test-description', 'test/outputType', 30);
571
return $this->failWithStatus($ret);
574
$ret = GalleryCoreApi::registerToolkitOperation('TestToolkit2',
575
array('test/type'), 'test-operation', array(),
576
'test-description', 'test/outputType', 27);
578
return $this->failWithStatus($ret);
581
list ($ret, $list) = GalleryCoreApi::getRedundantToolkitPriorities();
583
return $this->failWithStatus($ret);
586
$this->assert(count($list) >= 2, 'count');
587
$this->assertEquals(30, $list['TestToolkit'], 'TestToolkit');
588
$this->assertEquals(27, $list['TestToolkit2'], 'TestToolkit2');
591
function testGetToolkitPriorityById() {
592
$ret = GalleryCoreApi::registerToolkitOperation('TestToolkit',
593
array('test/type'), 'test-operation', array(),
594
'test-description', 'test/outputType', 30);
596
return $this->failWithStatus($ret);
599
/* Validate that toolkit priority is returned correctly */
600
list ($ret, $priority) = GalleryCoreApi::getToolkitPriorityById('TestToolkit');
602
return $this->failWithStatus($ret);
604
$this->assertEquals(30, $priority, 'Toolkit Priority Doesn\'t Match');
606
/* Test return for an undefined toolkit */
607
list ($ret, $priority) = GalleryCoreApi::getToolkitPriorityById('TestToolkit2');
609
return $this->failWithStatus($ret);
611
$this->assertEquals(null, $priority, 'Returned value for undefined toolkit');
614
function testMergeOperations() {
615
$toolkit = new TestToolkit();
616
$this->assertEquals(array(true, 'thumbnail', array(150)),
617
$toolkit->mergeOperations('thumbnail', array(100), 'thumbnail', array(150)),
619
$this->assertEquals(array(true, 'scale', array(800)),
620
$toolkit->mergeOperations('scale', array(600, 500), 'scale', array(800)),
622
$this->assertEquals(array(true, 'resize', array(640, 480)),
623
$toolkit->mergeOperations('resize', array(800, 600), 'resize', array(640, 480)),
625
$this->assertEquals(array(true, 'scale', array(800, 600)),
626
$toolkit->mergeOperations('thumbnail', array(200), 'scale', array(800, 600)),
628
$this->assertEquals(array(true, 'resize', array(640, 480)),
629
$toolkit->mergeOperations('scale', array(800, 600), 'resize', array(640, 480)),
631
$this->assertEquals(array(true, 'thumbnail', array(150)),
632
$toolkit->mergeOperations('resize', array(900, 700), 'thumbnail', array(150)),
634
$this->assertEquals(array(true, 'crop', array(20, 20, 60, 60)),
635
$toolkit->mergeOperations('crop', array(40, 40, 20, 20), 'crop', array(20, 20, 60, 60)),
637
$this->assertEquals(array(false, null, null), $toolkit->mergeOperations(
638
'composite', array('img', 'image/png', 30, 20, 'top-left', 0, 0),
639
'composite', array('myimage', 'image/gif', 60, 60, 'center', 0, 0)),
640
'composite should not merge');
641
$this->assertEquals(array(true, 'rotate', array(180)),
642
$toolkit->mergeOperations('rotate', array(90), 'rotate', array(90)),
644
$this->assertEquals(array(true, 'rotate', array(180)),
645
$toolkit->mergeOperations('rotate', array(-90), 'rotate', array(-90)),
647
$this->assertEquals(array(true, 'rotate', array(-90)),
648
$toolkit->mergeOperations('rotate', array(180), 'rotate', array(90)),
650
$this->assertEquals(array(true, null, null),
651
$toolkit->mergeOperations('rotate', array(180), 'rotate', array(180)),
655
function testUnregisterToolkitsByModuleId() {
656
$params = array(array('type' => 'test-type1', 'description' => 'test-description1'));
657
$ret = GalleryCoreApi::registerToolkitOperation('TestToolkit',
664
return $this->failWithStatus($ret);
667
$ret = GalleryCoreApi::registerToolkitProperty(
668
'TestToolkit2', array('test/type'), 'test-property',
669
'int', 'test-description');
671
return $this->failWithStatus($ret);
674
$ret = GalleryCoreApi::unregisterToolkitsByModuleId('coreTest');
676
return $this->failWithStatus($ret);
679
/* Get the operation */
680
list ($ret, $operations) = GalleryCoreApi::getToolkitOperations('test/type');
682
return $this->failWithStatus($ret);
684
$this->assertEquals(0, sizeof($operations), 'all operations should be gone');
686
list ($ret, $properties) = GalleryCoreApi::getToolkitProperties('test/type');
688
return $this->failWithStatus($ret);
690
$this->assertEquals(0, sizeof($properties), 'all properties should be gone');
697
* @package GalleryCore
698
* @subpackage PHPUnit
700
class TestToolkit extends GalleryToolkit { }
701
class TestToolkit2 { }