@@ -16,10 +16,269 @@ import 'dart:convert';
16
16
import 'dart:typed_data' ;
17
17
18
18
import 'package:firebase_ai/src/error.dart' ;
19
+ import 'package:firebase_ai/src/imagen_api.dart' ;
19
20
import 'package:firebase_ai/src/imagen_content.dart' ;
20
21
import 'package:flutter_test/flutter_test.dart' ;
21
22
22
23
void main () {
24
+ group ('ImagenSafetyFilterLevel' , () {
25
+ test ('toJson returns correct string values' , () {
26
+ expect (ImagenSafetyFilterLevel .blockLowAndAbove.toJson (),
27
+ 'block_low_and_above' );
28
+ expect (ImagenSafetyFilterLevel .blockMediumAndAbove.toJson (),
29
+ 'block_medium_and_above' );
30
+ expect (ImagenSafetyFilterLevel .blockOnlyHigh.toJson (), 'block_only_high' );
31
+ expect (ImagenSafetyFilterLevel .blockNone.toJson (), 'block_none' );
32
+ });
33
+ });
34
+
35
+ group ('ImagenPersonFilterLevel' , () {
36
+ test ('toJson returns correct string values' , () {
37
+ expect (ImagenPersonFilterLevel .blockAll.toJson (), 'dont_allow' );
38
+ expect (ImagenPersonFilterLevel .allowAdult.toJson (), 'allow_adult' );
39
+ expect (ImagenPersonFilterLevel .allowAll.toJson (), 'allow_all' );
40
+ });
41
+ });
42
+
43
+ group ('ImagenSafetySettings' , () {
44
+ test ('toJson with both values' , () {
45
+ final settings = ImagenSafetySettings (
46
+ ImagenSafetyFilterLevel .blockMediumAndAbove,
47
+ ImagenPersonFilterLevel .allowAdult,
48
+ );
49
+ final json = settings.toJson ();
50
+ expect (json, {
51
+ 'safetySetting' : 'block_medium_and_above' ,
52
+ 'personGeneration' : 'allow_adult' ,
53
+ });
54
+ });
55
+
56
+ test ('toJson with only safetyFilterLevel' , () {
57
+ final settings = ImagenSafetySettings (
58
+ ImagenSafetyFilterLevel .blockMediumAndAbove,
59
+ null ,
60
+ );
61
+ final json = settings.toJson ();
62
+ expect (json, {
63
+ 'safetySetting' : 'block_medium_and_above' ,
64
+ });
65
+ });
66
+
67
+ test ('toJson with only personFilterLevel' , () {
68
+ final settings = ImagenSafetySettings (
69
+ null ,
70
+ ImagenPersonFilterLevel .allowAdult,
71
+ );
72
+ final json = settings.toJson ();
73
+ expect (json, {
74
+ 'personGeneration' : 'allow_adult' ,
75
+ });
76
+ });
77
+
78
+ test ('toJson with null values' , () {
79
+ final settings = ImagenSafetySettings (null , null );
80
+ final json = settings.toJson ();
81
+ expect (json, {});
82
+ });
83
+ });
84
+
85
+ group ('ImagenAspectRatio' , () {
86
+ test ('toJson returns correct string values' , () {
87
+ expect (ImagenAspectRatio .square1x1.toJson (), '1:1' );
88
+ expect (ImagenAspectRatio .portrait9x16.toJson (), '9:16' );
89
+ expect (ImagenAspectRatio .landscape16x9.toJson (), '16:9' );
90
+ expect (ImagenAspectRatio .portrait3x4.toJson (), '3:4' );
91
+ expect (ImagenAspectRatio .landscape4x3.toJson (), '4:3' );
92
+ });
93
+ });
94
+
95
+ group ('ImagenFormat' , () {
96
+ test ('constructor with mimeType and compressionQuality' , () {
97
+ final format = ImagenFormat ('image/jpeg' , 85 );
98
+ expect (format.mimeType, 'image/jpeg' );
99
+ expect (format.compressionQuality, 85 );
100
+ });
101
+
102
+ test ('png constructor' , () {
103
+ final format = ImagenFormat .png ();
104
+ expect (format.mimeType, 'image/png' );
105
+ expect (format.compressionQuality, isNull);
106
+ });
107
+
108
+ test ('jpeg constructor with compressionQuality' , () {
109
+ final format = ImagenFormat .jpeg (compressionQuality: 90 );
110
+ expect (format.mimeType, 'image/jpeg' );
111
+ expect (format.compressionQuality, 90 );
112
+ });
113
+
114
+ test ('jpeg constructor without compressionQuality' , () {
115
+ final format = ImagenFormat .jpeg ();
116
+ expect (format.mimeType, 'image/jpeg' );
117
+ expect (format.compressionQuality, isNull);
118
+ });
119
+
120
+ test ('jpeg constructor logs warning for out of range compressionQuality' ,
121
+ () {
122
+ ImagenFormat .jpeg (compressionQuality: 150 );
123
+ ImagenFormat .jpeg (compressionQuality: - 10 );
124
+ });
125
+
126
+ test ('toJson with mimeType only' , () {
127
+ final format = ImagenFormat ('image/png' , null );
128
+ final json = format.toJson ();
129
+ expect (json, {
130
+ 'mimeType' : 'image/png' ,
131
+ });
132
+ });
133
+
134
+ test ('toJson with mimeType and compressionQuality' , () {
135
+ final format = ImagenFormat ('image/jpeg' , 85 );
136
+ final json = format.toJson ();
137
+ expect (json, {
138
+ 'mimeType' : 'image/jpeg' ,
139
+ 'compressionQuality' : 85 ,
140
+ });
141
+ });
142
+
143
+ test ('png toJson' , () {
144
+ final format = ImagenFormat .png ();
145
+ final json = format.toJson ();
146
+ expect (json, {
147
+ 'mimeType' : 'image/png' ,
148
+ });
149
+ });
150
+
151
+ test ('jpeg toJson with compressionQuality' , () {
152
+ final format = ImagenFormat .jpeg (compressionQuality: 90 );
153
+ final json = format.toJson ();
154
+ expect (json, {
155
+ 'mimeType' : 'image/jpeg' ,
156
+ 'compressionQuality' : 90 ,
157
+ });
158
+ });
159
+ });
160
+
161
+ group ('ImagenGenerationConfig' , () {
162
+ test ('constructor with all parameters' , () {
163
+ final config = ImagenGenerationConfig (
164
+ numberOfImages: 4 ,
165
+ negativePrompt: 'blurry, low quality' ,
166
+ aspectRatio: ImagenAspectRatio .landscape16x9,
167
+ imageFormat: ImagenFormat .jpeg (compressionQuality: 85 ),
168
+ addWatermark: true ,
169
+ );
170
+ expect (config.numberOfImages, 4 );
171
+ expect (config.negativePrompt, 'blurry, low quality' );
172
+ expect (config.aspectRatio, ImagenAspectRatio .landscape16x9);
173
+ expect (config.imageFormat? .mimeType, 'image/jpeg' );
174
+ expect (config.imageFormat? .compressionQuality, 85 );
175
+ expect (config.addWatermark, true );
176
+ });
177
+
178
+ test ('constructor with minimal parameters' , () {
179
+ final config = ImagenGenerationConfig ();
180
+ expect (config.numberOfImages, isNull);
181
+ expect (config.negativePrompt, isNull);
182
+ expect (config.aspectRatio, isNull);
183
+ expect (config.imageFormat, isNull);
184
+ expect (config.addWatermark, isNull);
185
+ });
186
+
187
+ test ('toJson with all parameters' , () {
188
+ final config = ImagenGenerationConfig (
189
+ numberOfImages: 4 ,
190
+ negativePrompt: 'blurry, low quality' ,
191
+ aspectRatio: ImagenAspectRatio .landscape16x9,
192
+ imageFormat: ImagenFormat .jpeg (compressionQuality: 85 ),
193
+ addWatermark: true ,
194
+ );
195
+ final json = config.toJson ();
196
+ expect (json, {
197
+ 'negativePrompt' : 'blurry, low quality' ,
198
+ 'numberOfImages' : 4 ,
199
+ 'aspectRatio' : '16:9' ,
200
+ 'addWatermark' : true ,
201
+ 'outputOptions' : {
202
+ 'mimeType' : 'image/jpeg' ,
203
+ 'compressionQuality' : 85 ,
204
+ },
205
+ });
206
+ });
207
+
208
+ test ('toJson with only negativePrompt' , () {
209
+ final config = ImagenGenerationConfig (
210
+ negativePrompt: 'blurry, low quality' ,
211
+ );
212
+ final json = config.toJson ();
213
+ expect (json, {
214
+ 'negativePrompt' : 'blurry, low quality' ,
215
+ });
216
+ });
217
+
218
+ test ('toJson with only numberOfImages' , () {
219
+ final config = ImagenGenerationConfig (
220
+ numberOfImages: 2 ,
221
+ );
222
+ final json = config.toJson ();
223
+ expect (json, {
224
+ 'numberOfImages' : 2 ,
225
+ });
226
+ });
227
+
228
+ test ('toJson with only aspectRatio' , () {
229
+ final config = ImagenGenerationConfig (
230
+ aspectRatio: ImagenAspectRatio .portrait9x16,
231
+ );
232
+ final json = config.toJson ();
233
+ expect (json, {
234
+ 'aspectRatio' : '9:16' ,
235
+ });
236
+ });
237
+
238
+ test ('toJson with only imageFormat' , () {
239
+ final config = ImagenGenerationConfig (
240
+ imageFormat: ImagenFormat .png (),
241
+ );
242
+ final json = config.toJson ();
243
+ expect (json, {
244
+ 'outputOptions' : {
245
+ 'mimeType' : 'image/png' ,
246
+ },
247
+ });
248
+ });
249
+
250
+ test ('toJson with only addWatermark' , () {
251
+ final config = ImagenGenerationConfig (
252
+ addWatermark: false ,
253
+ );
254
+ final json = config.toJson ();
255
+ expect (json, {
256
+ 'addWatermark' : false ,
257
+ });
258
+ });
259
+
260
+ test ('toJson with empty config' , () {
261
+ final config = ImagenGenerationConfig ();
262
+ final json = config.toJson ();
263
+ expect (json, {});
264
+ });
265
+
266
+ test ('toJson with imageFormat uses correct key name "outputOptions"' , () {
267
+ final config = ImagenGenerationConfig (
268
+ imageFormat: ImagenFormat .jpeg (compressionQuality: 75 ),
269
+ );
270
+ final json = config.toJson ();
271
+
272
+ expect (json.containsKey ('outputOptions' ), isTrue);
273
+ expect (json.containsKey ('outputOption' ), isFalse);
274
+
275
+ expect (json['outputOptions' ], {
276
+ 'mimeType' : 'image/jpeg' ,
277
+ 'compressionQuality' : 75 ,
278
+ });
279
+ });
280
+ });
281
+
23
282
group ('ImagenInlineImage' , () {
24
283
test ('fromJson with valid base64' , () {
25
284
final json = {
0 commit comments