Home | History | Annotate | Download | only in cloud_print
      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[] = {
     19   '\xda', '\x5c', '\xca', '\x36', '\x10', '\xb9', '\xa4', '\x16',
     20   '\x2f', '\x98', '\x1b', '\xa6', '\x5f', '\x43', '\x24', '\x33',
     21   '\x60', '\x43', '\x67', '\x34', '\0'
     22 };
     23 
     24 const int kRasterWidth = 612;
     25 const int kRasterHeight = 792;
     26 const int kRasterDPI = 72;
     27 
     28 scoped_ptr<BitmapImage> MakeSampleBitmap() {
     29   scoped_ptr<BitmapImage> bitmap_image(
     30       new BitmapImage(gfx::Size(kRasterWidth, kRasterHeight),
     31                       BitmapImage::RGBA));
     32 
     33   uint32* bitmap_data = reinterpret_cast<uint32*>(
     34       bitmap_image->pixel_data());
     35 
     36   for (int i = 0; i < kRasterWidth * kRasterHeight; i++) {
     37     bitmap_data[i] = 0xFFFFFF;
     38   }
     39 
     40 
     41   for (int i = 0; i < kRasterWidth; i++) {
     42     for (int j = 200; j < 300; j++) {
     43       int row_start = j * kRasterWidth;
     44       uint32 red = (i * 255)/kRasterWidth;
     45       bitmap_data[row_start + i] = red;
     46     }
     47   }
     48 
     49   // To test run length encoding
     50   for (int i = 0; i < kRasterWidth; i++) {
     51     for (int j = 400; j < 500; j++) {
     52       int row_start = j * kRasterWidth;
     53       if ((i/40) % 2 == 0) {
     54         bitmap_data[row_start + i] = 255 << 8;
     55       } else {
     56         bitmap_data[row_start + i] = 255 << 16;
     57       }
     58     }
     59   }
     60 
     61   return bitmap_image.Pass();
     62 }
     63 
     64 }  // namespace
     65 
     66 TEST(PwgRasterTest, CompareWithMaster) {
     67   std::string output;
     68   PwgEncoder encoder;
     69   scoped_ptr<BitmapImage> image = MakeSampleBitmap();
     70 
     71   encoder.EncodeDocumentHeader(&output);
     72   encoder.EncodePage(*image, kRasterDPI, 1, &output);
     73 
     74   EXPECT_EQ(kPWGFileSha1, base::SHA1HashString(output));
     75 }
     76 
     77 }  // namespace cloud_print
     78 
     79