Moved ByteArray up to core/common now that it has no dependencies on qrcode/encoder.
[zxing.git] / core / test / src / com / google / zxing / qrcode / encoder / RendererTestCase.java
1 /**
2  * Copyright 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.qrcode.encoder;
18
19 import junit.framework.TestCase;
20
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"
27
28 /**
29  * @author satorux@google.com (Satoru Takabayashi) - creator
30  * @author mysen@google.com (Chris Mysen) - ported from C++
31  */
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));
37     String result;
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
41     // RegressionTest().
42   }
43
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));
48     String result1;
49     assertTrue(Renderer.RenderAsPNG(qr_code, 3, result1));
50
51     String result2;
52     assertTrue(Renderer.RenderAsPNGFromData("http://www.google.com/",
53                                                     QRCode.EC_LEVEL_M, 3,
54                                                     result2));
55     assertEquals(result1, result2);
56   }
57
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) {
61     String result;
62     assertTrue(Renderer.RenderAsPNGFromData(bytes, ec_level,
63                                             cell_size, result));
64     String golden_file_name = "test/data/qrcode_encode/" +
65                               golden_base_name;
66     String golden;
67     File.ReadFileToStringOrDie(golden_file_name, golden);
68     return golden == result;
69   }
70
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"));
82   }
83 }