2 * Copyright 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.qrcode.encoder;
19 import junit.framework.TestCase;
21 //#include "file/base/file.h"
22 //#include "testing/base/gunit.h"
23 //#include "testing/base/benchmark.h"
24 //#include "wireless/qrcode/qrcode.h"
25 //#include "wireless/qrcode/qrcode_encoder.h"
26 //#include "wireless/qrcode/qrcode_renderer.h"
29 * @author satorux@google.com (Satoru Takabayashi) - creator
30 * @author mysen@google.com (Chris Mysen) - ported from C++
32 public final class RendererTestCase extends TestCase {
33 public void testRenderAsPNG() {
34 QRCode qr_code = new QRCode();
35 assertTrue(Encoder.Encode(new ByteArray("http://www.google.com/"),
36 QRCode.EC_LEVEL_M, qr_code));
38 assertTrue(Renderer.RenderAsPNG(qr_code, 3, result));
39 assertFalse(result.length() == 0);
40 // We don't test the result image in this test. We do that in
44 public void testRenderAsPNGFromData() {
45 QRCode qr_code = new QRCode();
46 assertTrue(Encoder.Encode(new ByteArray("http://www.google.com/"),
47 QRCode.EC_LEVEL_M, qr_code));
49 assertTrue(Renderer.RenderAsPNG(qr_code, 3, result1));
52 assertTrue(Renderer.RenderAsPNGFromData("http://www.google.com/",
55 assertEquals(result1, result2);
58 // ec_level comes from QRCode.EC_LEVEL_[LMQH]
59 static boolean Compare(final String bytes, final int ec_level,
60 final int cell_size, final String golden_base_name) {
62 assertTrue(Renderer.RenderAsPNGFromData(bytes, ec_level,
64 String golden_file_name = "test/data/qrcode_encode/" +
67 File.ReadFileToStringOrDie(golden_file_name, golden);
68 return golden == result;
71 // Golden images are generated with "qrcode_sample.cc". The images
72 // are checked with both eye balls and cell phones.
73 public void testRegressionTest() {
74 assertTrue(Compare("http://www.google.com/", QRCode.EC_LEVEL_M, 3,
75 "renderer-test-01.png"));
76 assertTrue(Compare("12345", QRCode.EC_LEVEL_L, 2,
77 "renderer-test-02.png"));
78 // Test in Katakana in Shift_JIS.
79 byte[] dat = {(byte)0x83,0x65,(byte)0x83,0x58,(byte)0x83,0x67};
80 assertTrue(Compare(new String(dat), QRCode.EC_LEVEL_H, 5,
81 "renderer-test-03.png"));