Home | History | Annotate | Download | only in fwl
      1 // Copyright 2017 PDFium 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 "xfa/fwl/cfx_barcode.h"
      6 
      7 #include <memory>
      8 #include <string>
      9 #include <utility>
     10 
     11 #include "core/fxcrt/fx_coordinates.h"
     12 #include "core/fxcrt/fx_string.h"
     13 #include "core/fxge/cfx_defaultrenderdevice.h"
     14 #include "core/fxge/cfx_renderdevice.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 #include "testing/test_support.h"
     17 #include "third_party/base/ptr_util.h"
     18 
     19 class BarcodeTest : public testing::Test {
     20  public:
     21   void SetUp() override {
     22     BC_Library_Init();
     23     barcode_ = pdfium::MakeUnique<CFX_Barcode>();
     24 
     25     auto device = pdfium::MakeUnique<CFX_DefaultRenderDevice>();
     26     auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
     27     if (bitmap->Create(640, 480, FXDIB_Rgb32))
     28       bitmap_ = bitmap;
     29     ASSERT_TRUE(bitmap_);
     30     ASSERT_TRUE(device->Attach(bitmap_, false, nullptr, false));
     31     device_ = std::move(device);
     32   }
     33 
     34   void TearDown() override {
     35     bitmap_.Reset();
     36     device_.reset();
     37     barcode_.reset();
     38     BC_Library_Destroy();
     39   }
     40 
     41   CFX_Barcode* barcode() const { return barcode_.get(); }
     42 
     43   bool Create(BC_TYPE type) {
     44     if (!barcode_->Create(type))
     45       return false;
     46 
     47     barcode_->SetModuleHeight(300);
     48     barcode_->SetModuleWidth(420);
     49     barcode_->SetHeight(298);
     50     barcode_->SetWidth(418);
     51     return true;
     52   }
     53 
     54   bool RenderDevice() {
     55     return barcode_->RenderDevice(device_.get(), &matrix_);
     56   }
     57 
     58   std::string BitmapChecksum() {
     59     return GenerateMD5Base16(bitmap_->GetBuffer(),
     60                              bitmap_->GetPitch() * bitmap_->GetHeight());
     61   }
     62 
     63  protected:
     64   CFX_Matrix matrix_;
     65   std::unique_ptr<CFX_Barcode> barcode_;
     66   std::unique_ptr<CFX_RenderDevice> device_;
     67   RetainPtr<CFX_DIBitmap> bitmap_;
     68 };
     69 
     70 TEST_F(BarcodeTest, Code39) {
     71   EXPECT_TRUE(Create(BC_CODE39));
     72   EXPECT_TRUE(barcode()->Encode(L"clams"));
     73   RenderDevice();
     74   EXPECT_EQ("cd4cd3f36da38ff58d9f621827018903", BitmapChecksum());
     75 }
     76 
     77 TEST_F(BarcodeTest, CodaBar) {
     78   EXPECT_TRUE(Create(BC_CODABAR));
     79   EXPECT_TRUE(barcode()->Encode(L"clams"));
     80   RenderDevice();
     81   EXPECT_EQ("481189dc4f86eddb8c42343c9b8ef1dd", BitmapChecksum());
     82 }
     83 
     84 TEST_F(BarcodeTest, Code128) {
     85   EXPECT_TRUE(Create(BC_CODE128));
     86   EXPECT_TRUE(barcode()->Encode(L"clams"));
     87   RenderDevice();
     88   EXPECT_EQ("11b21c178a9fd866d8be196c2103b263", BitmapChecksum());
     89 }
     90 
     91 TEST_F(BarcodeTest, Code128_B) {
     92   EXPECT_TRUE(Create(BC_CODE128_B));
     93   EXPECT_TRUE(barcode()->Encode(L"clams"));
     94   RenderDevice();
     95   EXPECT_EQ("11b21c178a9fd866d8be196c2103b263", BitmapChecksum());
     96 }
     97 
     98 TEST_F(BarcodeTest, Code128_C) {
     99   EXPECT_TRUE(Create(BC_CODE128_C));
    100   EXPECT_TRUE(barcode()->Encode(L"clams"));
    101   RenderDevice();
    102   EXPECT_EQ("6284ec8503d5a948c9518108da33cdd3", BitmapChecksum());
    103 }
    104 
    105 TEST_F(BarcodeTest, Ean8) {
    106   EXPECT_TRUE(Create(BC_EAN8));
    107   EXPECT_TRUE(barcode()->Encode(L"clams"));
    108   RenderDevice();
    109   EXPECT_EQ("22d85bcb02d48f48813f02a1cc9cfe8c", BitmapChecksum());
    110 }
    111 
    112 TEST_F(BarcodeTest, UPCA) {
    113   EXPECT_TRUE(Create(BC_UPCA));
    114   EXPECT_TRUE(barcode()->Encode(L"clams"));
    115   RenderDevice();
    116   EXPECT_EQ("cce41fc30852744c44b3353059b568b4", BitmapChecksum());
    117 }
    118 
    119 TEST_F(BarcodeTest, Ean13) {
    120   EXPECT_TRUE(Create(BC_EAN13));
    121   EXPECT_TRUE(barcode()->Encode(L"clams"));
    122   RenderDevice();
    123   EXPECT_EQ("187091ec1fd1830fc4d41d40a923d4fb", BitmapChecksum());
    124 }
    125 
    126 TEST_F(BarcodeTest, Pdf417) {
    127   EXPECT_TRUE(Create(BC_PDF417));
    128   EXPECT_TRUE(barcode()->Encode(L"clams"));
    129   RenderDevice();
    130   EXPECT_EQ("2bdb9b39f20c5763da6a0d7c7b1f6933", BitmapChecksum());
    131 }
    132 
    133 TEST_F(BarcodeTest, DataMatrix) {
    134   EXPECT_TRUE(Create(BC_DATAMATRIX));
    135   EXPECT_TRUE(barcode()->Encode(L"clams"));
    136   RenderDevice();
    137   EXPECT_EQ("5e5cd9a680b86fcd4ffd53ed36e3c980", BitmapChecksum());
    138 }
    139 
    140 TEST_F(BarcodeTest, QrCode) {
    141   EXPECT_TRUE(Create(BC_QR_CODE));
    142   EXPECT_TRUE(barcode()->Encode(L"clams"));
    143   RenderDevice();
    144   EXPECT_EQ("4751c6e0f67749fabe24f787128decee", BitmapChecksum());
    145 }
    146