Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / utility / cloud_print / pwg_encoder_unittest.cc
1 // Copyright 2013 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.
4
5 #include "base/file_util.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/sha1.h"
8 #include "chrome/utility/cloud_print/bitmap_image.h"
9 #include "chrome/utility/cloud_print/pwg_encoder.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace cloud_print {
13
14 namespace {
15
16 // SHA-1 of golden master for this test, plus null terminating character.
17 // File is in chrome/test/data/printing/test_pwg_generator.pwg.
18 const char kPWGFileSha1[] = {'\x4a', '\xd7', '\x44', '\x29', '\x98', '\xc8',
19                              '\xfe', '\xae', '\x94', '\xbc', '\x9c', '\x8b',
20                              '\x17', '\x7a', '\x7c', '\x94', '\x76', '\x6c',
21                              '\xc9', '\xfb', '\0'};
22
23 const int kRasterWidth = 612;
24 const int kRasterHeight = 792;
25 const int kRasterDPI = 72;
26
27 scoped_ptr<BitmapImage> MakeSampleBitmap() {
28   scoped_ptr<BitmapImage> bitmap_image(
29       new BitmapImage(gfx::Size(kRasterWidth, kRasterHeight),
30                       BitmapImage::RGBA));
31
32   uint32* bitmap_data = reinterpret_cast<uint32*>(
33       bitmap_image->pixel_data());
34
35   for (int i = 0; i < kRasterWidth * kRasterHeight; i++) {
36     bitmap_data[i] = 0xFFFFFF;
37   }
38
39
40   for (int i = 0; i < kRasterWidth; i++) {
41     for (int j = 200; j < 300; j++) {
42       int row_start = j * kRasterWidth;
43       uint32 red = (i * 255)/kRasterWidth;
44       bitmap_data[row_start + i] = red;
45     }
46   }
47
48   // To test run length encoding
49   for (int i = 0; i < kRasterWidth; i++) {
50     for (int j = 400; j < 500; j++) {
51       int row_start = j * kRasterWidth;
52       if ((i/40) % 2 == 0) {
53         bitmap_data[row_start + i] = 255 << 8;
54       } else {
55         bitmap_data[row_start + i] = 255 << 16;
56       }
57     }
58   }
59
60   return bitmap_image.Pass();
61 }
62
63 }  // namespace
64
65 TEST(PwgRasterTest, CompareWithMaster) {
66   std::string output;
67   PwgEncoder encoder;
68   scoped_ptr<BitmapImage> image = MakeSampleBitmap();
69   PwgHeaderInfo header_info;
70   header_info.dpi = kRasterDPI;
71   header_info.total_pages = 1;
72
73   encoder.EncodeDocumentHeader(&output);
74   encoder.EncodePage(*image, header_info, &output);
75
76   EXPECT_EQ(kPWGFileSha1, base::SHA1HashString(output));
77 }
78
79 }  // namespace cloud_print