summaryrefslogtreecommitdiffstats
path: root/ui/file_manager/image_loader/image_loader_unittest.js
blob: f6cf64ad48363d11837dfe126b2ed16956d1dcd2 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * Test case:
 * - Source image: 200x50
 * - Target: max size is 100x100
 */
function testNormalImage() {
  var source = new Image();
  source.width = 200;
  source.height = 50;
  var options = {
    maxWidth: 100,
    maxHeight: 100,
    orientation: ImageOrientation.fromDriveOrientation(0)
  };
  var result = ImageLoader.calculateCopyParameters(source, options);
  assertEquals(0, result.source.x);
  assertEquals(0, result.source.y);
  assertEquals(200, result.source.width);
  assertEquals(50, result.source.height);
  assertEquals(0, result.target.x);
  assertEquals(0, result.target.y);
  assertEquals(100, result.target.width);
  assertEquals(25, result.target.height);
  assertEquals(100, result.canvas.width);
  assertEquals(25, result.canvas.height);
};

/**
 * Test case:
 * - Source image: 50x200 90 deg clock-wise rotated image.
 * - Target: max size is 100x100
 */
function testRotatedImage() {
  var source = new Image();
  source.width = 50;
  source.height = 200;
  var options = {
    maxWidth: 100,
    maxHeight: 100,
    orientation: ImageOrientation.fromDriveOrientation(1)
  };
  var result = ImageLoader.calculateCopyParameters(source, options);
  assertEquals(0, result.source.x);
  assertEquals(0, result.source.y);
  assertEquals(50, result.source.width);
  assertEquals(200, result.source.height);
  assertEquals(0, result.target.x);
  assertEquals(0, result.target.y);
  assertEquals(25, result.target.width);
  assertEquals(100, result.target.height);
  assertEquals(100, result.canvas.width);
  assertEquals(25, result.canvas.height);
}

/**
 * Test case:
 * - Source image: 800x100
 * - Target: 50x50 cropped image.
 */
function testCroppedImage() {
  var source = new Image();
  source.width = 800;
  source.height = 100;
  var options = {
    width: 50,
    height: 50,
    crop: true,
    orientation: ImageOrientation.fromDriveOrientation(0)
  };
  var result = ImageLoader.calculateCopyParameters(source, options);
  assertEquals(350, result.source.x);
  assertEquals(0, result.source.y);
  assertEquals(100, result.source.width);
  assertEquals(100, result.source.height);
  assertEquals(0, result.target.x);
  assertEquals(0, result.target.y);
  assertEquals(50, result.target.width);
  assertEquals(50, result.target.height);
  assertEquals(50, result.canvas.width);
  assertEquals(50, result.canvas.height);
}

/**
 * Test case:
 * - Source image: 200x25
 * - Target: 50x50 cropped image.
 */
function testCroppedImageWithResize() {
  var source = new Image();
  source.width = 200;
  source.height = 25;
  var options = {
    width: 50,
    height: 50,
    crop: true,
    orientation: ImageOrientation.fromDriveOrientation(0)
  };
  var result = ImageLoader.calculateCopyParameters(source, options);
  assertEquals(87, result.source.x);
  assertEquals(0, result.source.y);
  assertEquals(25, result.source.width);
  assertEquals(25, result.source.height);
  assertEquals(0, result.target.x);
  assertEquals(0, result.target.y);
  assertEquals(50, result.target.width);
  assertEquals(50, result.target.height);
  assertEquals(50, result.canvas.width);
  assertEquals(50, result.canvas.height);
}

/**
 * Test case:
 * - Source image: 20x10
 * - Target: 50x50 cropped image.
 */
function testCroppedTinyImage() {
  var source = new Image();
  source.width = 20;
  source.height = 10;
  var options = {
    width: 50,
    height: 50,
    crop: true,
    orientation: ImageOrientation.fromDriveOrientation(0)
  };
  var result = ImageLoader.calculateCopyParameters(source, options);
  assertEquals(5, result.source.x);
  assertEquals(0, result.source.y);
  assertEquals(10, result.source.width);
  assertEquals(10, result.source.height);
  assertEquals(0, result.target.x);
  assertEquals(0, result.target.y);
  assertEquals(50, result.target.width);
  assertEquals(50, result.target.height);
  assertEquals(50, result.canvas.width);
  assertEquals(50, result.canvas.height);
}

/**
 * Test case:
 * - Source image: 100x400 90 degree clock-wise rotated.
 * - Target: 50x50 cropped image
 */
function testCroppedRotatedImage() {
  var source = new Image();
  source.width = 100;
  source.height = 400;
  var options = {
    width: 50,
    height: 50,
    crop: true,
    orientation: ImageOrientation.fromDriveOrientation(1)
  };
  var result = ImageLoader.calculateCopyParameters(source, options);
  assertEquals(0, result.source.x);
  assertEquals(150, result.source.y);
  assertEquals(100, result.source.width);
  assertEquals(100, result.source.height);
  assertEquals(0, result.target.x);
  assertEquals(0, result.target.y);
  assertEquals(50, result.target.width);
  assertEquals(50, result.target.height);
  assertEquals(50, result.canvas.width);
  assertEquals(50, result.canvas.height);
}