1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/cloud_devices/common/printer_description.h"
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/strings/string_util.h"
10 #include "base/values.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace cloud_devices
{
17 // Replaces ' with " to allow readable json constants in tests.
18 // Makes sure that same json value represented by same strings to simplify
20 std::string
NormalizeJson(const std::string
& json
) {
21 std::string result
= json
;
22 base::ReplaceChars(result
, "'", "\"", &result
);
23 scoped_ptr
<base::Value
> value
= base::JSONReader::Read(result
);
25 base::JSONWriter::Write(*value
, &result
);
33 " 'supported_content_type': [ {"
34 " 'content_type': 'image/pwg-raster'"
36 " 'content_type': 'image/jpeg'"
38 " 'pwg_raster_config' : {"
39 " 'document_sheet_back' : 'MANUAL_TUMBLE',"
40 " 'reverse_order_streaming': true"
44 " 'is_default': true,"
45 " 'type': 'STANDARD_COLOR'"
47 " 'type': 'STANDARD_MONOCHROME'"
49 " 'type': 'CUSTOM_MONOCHROME',"
50 " 'vendor_id': '123',"
51 " 'custom_display_name': 'monochrome'"
56 " 'is_default': true,"
57 " 'type': 'LONG_EDGE'"
59 " 'type': 'SHORT_EDGE'"
61 " 'type': 'NO_DUPLEX'"
64 " 'page_orientation': {"
68 " 'type': 'LANDSCAPE'"
70 " 'is_default': true,"
78 " 'is_default': true,"
79 " 'type': 'BORDERLESS',"
81 " 'right_microns': 0,"
82 " 'bottom_microns': 0,"
85 " 'type': 'STANDARD',"
86 " 'top_microns': 100,"
87 " 'right_microns': 200,"
88 " 'bottom_microns': 300,"
89 " 'left_microns': 400"
93 " 'right_microns': 2,"
94 " 'bottom_microns': 3,"
100 " 'horizontal_dpi': 150,"
101 " 'vertical_dpi': 250"
103 " 'is_default': true,"
104 " 'horizontal_dpi': 600,"
105 " 'vertical_dpi': 1600"
110 " 'is_default': true,"
111 " 'type': 'NO_FITTING'"
113 " 'type': 'FIT_TO_PAGE'"
115 " 'type': 'GROW_TO_PAGE'"
117 " 'type': 'SHRINK_TO_PAGE'"
119 " 'type': 'FILL_PAGE'"
126 " 'is_default': true,"
127 " 'name': 'NA_LETTER',"
128 " 'width_microns': 2222,"
129 " 'height_microns': 3333"
132 " 'width_microns': 4444,"
133 " 'height_microns': 5555"
135 " 'name': 'JPN_YOU4',"
136 " 'width_microns': 6666,"
137 " 'height_microns': 7777"
139 " 'width_microns': 1111,"
140 " 'is_continuous_feed': true,"
141 " 'custom_display_name': 'Feed',"
142 " 'vendor_id': 'FEED'"
148 " 'reverse_order': {"
154 const char kDefaultCdd
[] =
159 const char kBadVersionCdd
[] =
166 const char kNoDefaultCdd
[] =
172 " 'type': 'STANDARD_COLOR'"
174 " 'type': 'STANDARD_MONOCHROME'"
180 const char kMultyDefaultCdd
[] =
186 " 'is_default': true,"
187 " 'type': 'STANDARD_COLOR'"
189 " 'is_default': true,"
190 " 'type': 'STANDARD_MONOCHROME'"
200 " 'pwg_raster_config' : {"
201 " 'document_sheet_back' : 'MANUAL_TUMBLE',"
202 " 'reverse_order_streaming': true"
205 " 'type': 'STANDARD_MONOCHROME'"
208 " 'type': 'NO_DUPLEX'"
210 " 'page_orientation': {"
211 " 'type': 'LANDSCAPE'"
219 " 'right_microns': 6,"
220 " 'bottom_microns': 3,"
224 " 'horizontal_dpi': 562,"
225 " 'vertical_dpi': 125"
228 " 'type': 'SHRINK_TO_PAGE'"
239 " 'name': 'ISO_C7C6',"
240 " 'width_microns': 4261,"
241 " 'height_microns': 334"
246 " 'reverse_order': {"
247 " 'reverse_order': true"
252 const char kDefaultCjt
[] =
257 const char kBadVersionCjt
[] =
264 TEST(PrinterDescriptionTest
, CddInit
) {
265 CloudDeviceDescription description
;
266 EXPECT_EQ(NormalizeJson(kDefaultCdd
), NormalizeJson(description
.ToString()));
268 ContentTypesCapability content_types
;
269 PwgRasterConfigCapability pwg_raster
;
270 ColorCapability color
;
271 DuplexCapability duplex
;
272 OrientationCapability orientation
;
273 MarginsCapability margins
;
275 FitToPageCapability fit_to_page
;
276 MediaCapability media
;
277 CopiesCapability copies
;
278 PageRangeCapability page_range
;
279 CollateCapability collate
;
280 ReverseCapability reverse
;
282 EXPECT_FALSE(content_types
.LoadFrom(description
));
283 EXPECT_FALSE(pwg_raster
.LoadFrom(description
));
284 EXPECT_FALSE(color
.LoadFrom(description
));
285 EXPECT_FALSE(duplex
.LoadFrom(description
));
286 EXPECT_FALSE(orientation
.LoadFrom(description
));
287 EXPECT_FALSE(copies
.LoadFrom(description
));
288 EXPECT_FALSE(margins
.LoadFrom(description
));
289 EXPECT_FALSE(dpi
.LoadFrom(description
));
290 EXPECT_FALSE(fit_to_page
.LoadFrom(description
));
291 EXPECT_FALSE(page_range
.LoadFrom(description
));
292 EXPECT_FALSE(media
.LoadFrom(description
));
293 EXPECT_FALSE(collate
.LoadFrom(description
));
294 EXPECT_FALSE(reverse
.LoadFrom(description
));
295 EXPECT_FALSE(media
.LoadFrom(description
));
298 TEST(PrinterDescriptionTest
, CddInvalid
) {
299 CloudDeviceDescription description
;
300 ColorCapability color
;
302 EXPECT_FALSE(description
.InitFromString(NormalizeJson(kBadVersionCdd
)));
304 EXPECT_TRUE(description
.InitFromString(NormalizeJson(kNoDefaultCdd
)));
305 EXPECT_FALSE(color
.LoadFrom(description
));
307 EXPECT_TRUE(description
.InitFromString(NormalizeJson(kMultyDefaultCdd
)));
308 EXPECT_FALSE(color
.LoadFrom(description
));
311 TEST(PrinterDescriptionTest
, CddSetAll
) {
312 CloudDeviceDescription description
;
314 ContentTypesCapability content_types
;
315 PwgRasterConfigCapability pwg_raster_config
;
316 ColorCapability color
;
317 DuplexCapability duplex
;
318 OrientationCapability orientation
;
319 MarginsCapability margins
;
321 FitToPageCapability fit_to_page
;
322 MediaCapability media
;
323 CopiesCapability copies
;
324 PageRangeCapability page_range
;
325 CollateCapability collate
;
326 ReverseCapability reverse
;
328 content_types
.AddOption("image/pwg-raster");
329 content_types
.AddOption("image/jpeg");
331 PwgRasterConfig custom_raster
;
332 custom_raster
.document_sheet_back
= MANUAL_TUMBLE
;
333 custom_raster
.reverse_order_streaming
= true;
334 custom_raster
.rotate_all_pages
= false;
335 pwg_raster_config
.set_value(custom_raster
);
337 color
.AddDefaultOption(Color(STANDARD_COLOR
), true);
338 color
.AddOption(Color(STANDARD_MONOCHROME
));
339 Color
custom(CUSTOM_MONOCHROME
);
340 custom
.vendor_id
= "123";
341 custom
.custom_display_name
= "monochrome";
342 color
.AddOption(custom
);
344 duplex
.AddDefaultOption(LONG_EDGE
, true);
345 duplex
.AddOption(SHORT_EDGE
);
346 duplex
.AddOption(NO_DUPLEX
);
348 orientation
.AddOption(PORTRAIT
);
349 orientation
.AddOption(LANDSCAPE
);
350 orientation
.AddDefaultOption(AUTO_ORIENTATION
, true);
352 margins
.AddDefaultOption(Margins(NO_MARGINS
, 0, 0, 0, 0), true);
353 margins
.AddOption(Margins(STANDARD_MARGINS
, 100, 200, 300, 400));
354 margins
.AddOption(Margins(CUSTOM_MARGINS
, 1, 2, 3, 4));
356 dpi
.AddOption(Dpi(150, 250));
357 dpi
.AddDefaultOption(Dpi(600, 1600), true);
359 fit_to_page
.AddDefaultOption(NO_FITTING
, true);
360 fit_to_page
.AddOption(FIT_TO_PAGE
);
361 fit_to_page
.AddOption(GROW_TO_PAGE
);
362 fit_to_page
.AddOption(SHRINK_TO_PAGE
);
363 fit_to_page
.AddOption(FILL_PAGE
);
365 media
.AddDefaultOption(Media(NA_LETTER
, 2222, 3333), true);
366 media
.AddOption(Media(ISO_A6
, 4444, 5555));
367 media
.AddOption(Media(JPN_YOU4
, 6666, 7777));
368 media
.AddOption(Media("Feed", "FEED", 1111, 0));
370 collate
.set_default_value(false);
371 reverse
.set_default_value(true);
373 content_types
.SaveTo(&description
);
374 color
.SaveTo(&description
);
375 duplex
.SaveTo(&description
);
376 orientation
.SaveTo(&description
);
377 copies
.SaveTo(&description
);
378 margins
.SaveTo(&description
);
379 dpi
.SaveTo(&description
);
380 fit_to_page
.SaveTo(&description
);
381 page_range
.SaveTo(&description
);
382 media
.SaveTo(&description
);
383 collate
.SaveTo(&description
);
384 reverse
.SaveTo(&description
);
385 pwg_raster_config
.SaveTo(&description
);
387 EXPECT_EQ(NormalizeJson(kCdd
), NormalizeJson(description
.ToString()));
390 TEST(PrinterDescriptionTest
, CddGetAll
) {
391 CloudDeviceDescription description
;
392 ASSERT_TRUE(description
.InitFromString(NormalizeJson(kCdd
)));
394 ContentTypesCapability content_types
;
395 PwgRasterConfigCapability pwg_raster_config
;
396 ColorCapability color
;
397 DuplexCapability duplex
;
398 OrientationCapability orientation
;
399 MarginsCapability margins
;
401 FitToPageCapability fit_to_page
;
402 MediaCapability media
;
403 CopiesCapability copies
;
404 PageRangeCapability page_range
;
405 CollateCapability collate
;
406 ReverseCapability reverse
;
408 EXPECT_TRUE(content_types
.LoadFrom(description
));
409 EXPECT_TRUE(color
.LoadFrom(description
));
410 EXPECT_TRUE(duplex
.LoadFrom(description
));
411 EXPECT_TRUE(orientation
.LoadFrom(description
));
412 EXPECT_TRUE(copies
.LoadFrom(description
));
413 EXPECT_TRUE(margins
.LoadFrom(description
));
414 EXPECT_TRUE(dpi
.LoadFrom(description
));
415 EXPECT_TRUE(fit_to_page
.LoadFrom(description
));
416 EXPECT_TRUE(page_range
.LoadFrom(description
));
417 EXPECT_TRUE(media
.LoadFrom(description
));
418 EXPECT_TRUE(collate
.LoadFrom(description
));
419 EXPECT_TRUE(reverse
.LoadFrom(description
));
420 EXPECT_TRUE(media
.LoadFrom(description
));
421 EXPECT_TRUE(pwg_raster_config
.LoadFrom(description
));
423 EXPECT_TRUE(content_types
.Contains("image/pwg-raster"));
424 EXPECT_TRUE(content_types
.Contains("image/jpeg"));
426 EXPECT_EQ(MANUAL_TUMBLE
, pwg_raster_config
.value().document_sheet_back
);
427 EXPECT_TRUE(pwg_raster_config
.value().reverse_order_streaming
);
428 EXPECT_FALSE(pwg_raster_config
.value().rotate_all_pages
);
430 EXPECT_TRUE(color
.Contains(Color(STANDARD_COLOR
)));
431 EXPECT_TRUE(color
.Contains(Color(STANDARD_MONOCHROME
)));
432 Color
custom(CUSTOM_MONOCHROME
);
433 custom
.vendor_id
= "123";
434 custom
.custom_display_name
= "monochrome";
435 EXPECT_TRUE(color
.Contains(custom
));
436 EXPECT_EQ(Color(STANDARD_COLOR
), color
.GetDefault());
438 EXPECT_TRUE(duplex
.Contains(LONG_EDGE
));
439 EXPECT_TRUE(duplex
.Contains(SHORT_EDGE
));
440 EXPECT_TRUE(duplex
.Contains(NO_DUPLEX
));
441 EXPECT_EQ(LONG_EDGE
, duplex
.GetDefault());
443 EXPECT_TRUE(orientation
.Contains(PORTRAIT
));
444 EXPECT_TRUE(orientation
.Contains(LANDSCAPE
));
445 EXPECT_TRUE(orientation
.Contains(AUTO_ORIENTATION
));
446 EXPECT_EQ(AUTO_ORIENTATION
, orientation
.GetDefault());
448 EXPECT_TRUE(margins
.Contains(Margins(NO_MARGINS
, 0, 0, 0, 0)));
449 EXPECT_TRUE(margins
.Contains(Margins(STANDARD_MARGINS
, 100, 200, 300, 400)));
450 EXPECT_TRUE(margins
.Contains(Margins(CUSTOM_MARGINS
, 1, 2, 3, 4)));
451 EXPECT_EQ(Margins(NO_MARGINS
, 0, 0, 0, 0), margins
.GetDefault());
453 EXPECT_TRUE(dpi
.Contains(Dpi(150, 250)));
454 EXPECT_TRUE(dpi
.Contains(Dpi(600, 1600)));
455 EXPECT_EQ(Dpi(600, 1600), dpi
.GetDefault());
457 EXPECT_TRUE(fit_to_page
.Contains(NO_FITTING
));
458 EXPECT_TRUE(fit_to_page
.Contains(FIT_TO_PAGE
));
459 EXPECT_TRUE(fit_to_page
.Contains(GROW_TO_PAGE
));
460 EXPECT_TRUE(fit_to_page
.Contains(SHRINK_TO_PAGE
));
461 EXPECT_TRUE(fit_to_page
.Contains(FILL_PAGE
));
462 EXPECT_EQ(NO_FITTING
, fit_to_page
.GetDefault());
464 EXPECT_TRUE(media
.Contains(Media(NA_LETTER
, 2222, 3333)));
465 EXPECT_TRUE(media
.Contains(Media(ISO_A6
, 4444, 5555)));
466 EXPECT_TRUE(media
.Contains(Media(JPN_YOU4
, 6666, 7777)));
467 EXPECT_TRUE(media
.Contains(Media("Feed", "FEED", 1111, 0)));
468 EXPECT_EQ(Media(NA_LETTER
, 2222, 3333), media
.GetDefault());
470 EXPECT_FALSE(collate
.default_value());
471 EXPECT_TRUE(reverse
.default_value());
473 EXPECT_EQ(NormalizeJson(kCdd
), NormalizeJson(description
.ToString()));
476 TEST(PrinterDescriptionTest
, CjtInit
) {
477 CloudDeviceDescription description
;
478 EXPECT_EQ(NormalizeJson(kDefaultCjt
), NormalizeJson(description
.ToString()));
480 PwgRasterConfigTicketItem pwg_raster_config
;
481 ColorTicketItem color
;
482 DuplexTicketItem duplex
;
483 OrientationTicketItem orientation
;
484 MarginsTicketItem margins
;
486 FitToPageTicketItem fit_to_page
;
487 MediaTicketItem media
;
488 CopiesTicketItem copies
;
489 PageRangeTicketItem page_range
;
490 CollateTicketItem collate
;
491 ReverseTicketItem reverse
;
493 EXPECT_FALSE(pwg_raster_config
.LoadFrom(description
));
494 EXPECT_FALSE(color
.LoadFrom(description
));
495 EXPECT_FALSE(duplex
.LoadFrom(description
));
496 EXPECT_FALSE(orientation
.LoadFrom(description
));
497 EXPECT_FALSE(copies
.LoadFrom(description
));
498 EXPECT_FALSE(margins
.LoadFrom(description
));
499 EXPECT_FALSE(dpi
.LoadFrom(description
));
500 EXPECT_FALSE(fit_to_page
.LoadFrom(description
));
501 EXPECT_FALSE(page_range
.LoadFrom(description
));
502 EXPECT_FALSE(media
.LoadFrom(description
));
503 EXPECT_FALSE(collate
.LoadFrom(description
));
504 EXPECT_FALSE(reverse
.LoadFrom(description
));
505 EXPECT_FALSE(media
.LoadFrom(description
));
508 TEST(PrinterDescriptionTest
, CjtInvalid
) {
509 CloudDeviceDescription ticket
;
510 EXPECT_FALSE(ticket
.InitFromString(NormalizeJson(kBadVersionCjt
)));
513 TEST(PrinterDescriptionTest
, CjtSetAll
) {
514 CloudDeviceDescription description
;
516 PwgRasterConfigTicketItem pwg_raster_config
;
517 ColorTicketItem color
;
518 DuplexTicketItem duplex
;
519 OrientationTicketItem orientation
;
520 MarginsTicketItem margins
;
522 FitToPageTicketItem fit_to_page
;
523 MediaTicketItem media
;
524 CopiesTicketItem copies
;
525 PageRangeTicketItem page_range
;
526 CollateTicketItem collate
;
527 ReverseTicketItem reverse
;
529 PwgRasterConfig custom_raster
;
530 custom_raster
.document_sheet_back
= MANUAL_TUMBLE
;
531 custom_raster
.reverse_order_streaming
= true;
532 custom_raster
.rotate_all_pages
= false;
533 pwg_raster_config
.set_value(custom_raster
);
534 color
.set_value(Color(STANDARD_MONOCHROME
));
535 duplex
.set_value(NO_DUPLEX
);
536 orientation
.set_value(LANDSCAPE
);
537 copies
.set_value(123);
538 margins
.set_value(Margins(CUSTOM_MARGINS
, 7, 6, 3, 1));
539 dpi
.set_value(Dpi(562, 125));
540 fit_to_page
.set_value(SHRINK_TO_PAGE
);
541 PageRange page_ranges
;
542 page_ranges
.push_back(Interval(1, 99));
543 page_ranges
.push_back(Interval(150));
544 page_range
.set_value(page_ranges
);
545 media
.set_value(Media(ISO_C7C6
, 4261, 334));
546 collate
.set_value(false);
547 reverse
.set_value(true);
549 pwg_raster_config
.SaveTo(&description
);
550 color
.SaveTo(&description
);
551 duplex
.SaveTo(&description
);
552 orientation
.SaveTo(&description
);
553 copies
.SaveTo(&description
);
554 margins
.SaveTo(&description
);
555 dpi
.SaveTo(&description
);
556 fit_to_page
.SaveTo(&description
);
557 page_range
.SaveTo(&description
);
558 media
.SaveTo(&description
);
559 collate
.SaveTo(&description
);
560 reverse
.SaveTo(&description
);
562 EXPECT_EQ(NormalizeJson(kCjt
), NormalizeJson(description
.ToString()));
565 TEST(PrinterDescriptionTest
, CjtGetAll
) {
566 CloudDeviceDescription description
;
567 ASSERT_TRUE(description
.InitFromString(NormalizeJson(kCjt
)));
569 ColorTicketItem color
;
570 DuplexTicketItem duplex
;
571 OrientationTicketItem orientation
;
572 MarginsTicketItem margins
;
574 FitToPageTicketItem fit_to_page
;
575 MediaTicketItem media
;
576 CopiesTicketItem copies
;
577 PageRangeTicketItem page_range
;
578 CollateTicketItem collate
;
579 ReverseTicketItem reverse
;
580 PwgRasterConfigTicketItem pwg_raster_config
;
582 EXPECT_TRUE(pwg_raster_config
.LoadFrom(description
));
583 EXPECT_TRUE(color
.LoadFrom(description
));
584 EXPECT_TRUE(duplex
.LoadFrom(description
));
585 EXPECT_TRUE(orientation
.LoadFrom(description
));
586 EXPECT_TRUE(copies
.LoadFrom(description
));
587 EXPECT_TRUE(margins
.LoadFrom(description
));
588 EXPECT_TRUE(dpi
.LoadFrom(description
));
589 EXPECT_TRUE(fit_to_page
.LoadFrom(description
));
590 EXPECT_TRUE(page_range
.LoadFrom(description
));
591 EXPECT_TRUE(media
.LoadFrom(description
));
592 EXPECT_TRUE(collate
.LoadFrom(description
));
593 EXPECT_TRUE(reverse
.LoadFrom(description
));
594 EXPECT_TRUE(media
.LoadFrom(description
));
596 EXPECT_EQ(MANUAL_TUMBLE
, pwg_raster_config
.value().document_sheet_back
);
597 EXPECT_TRUE(pwg_raster_config
.value().reverse_order_streaming
);
598 EXPECT_FALSE(pwg_raster_config
.value().rotate_all_pages
);
599 EXPECT_EQ(color
.value(), Color(STANDARD_MONOCHROME
));
600 EXPECT_EQ(duplex
.value(), NO_DUPLEX
);
601 EXPECT_EQ(orientation
.value(), LANDSCAPE
);
602 EXPECT_EQ(copies
.value(), 123);
603 EXPECT_EQ(margins
.value(), Margins(CUSTOM_MARGINS
, 7, 6, 3, 1));
604 EXPECT_EQ(dpi
.value(), Dpi(562, 125));
605 EXPECT_EQ(fit_to_page
.value(), SHRINK_TO_PAGE
);
606 PageRange page_ranges
;
607 page_ranges
.push_back(Interval(1, 99));
608 page_ranges
.push_back(Interval(150));
609 EXPECT_EQ(page_range
.value(), page_ranges
);
610 EXPECT_EQ(media
.value(), Media(ISO_C7C6
, 4261, 334));
611 EXPECT_FALSE(collate
.value());
612 EXPECT_TRUE(reverse
.value());
614 EXPECT_EQ(NormalizeJson(kCjt
), NormalizeJson(description
.ToString()));
617 } // namespace printer
619 } // namespace cloud_devices