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 com.google.zxing.common.ByteMatrix;
20 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
21 import com.google.zxing.qrcode.decoder.Mode;
22 import junit.framework.TestCase;
25 * @author satorux@google.com (Satoru Takabayashi) - creator
26 * @author mysen@google.com (Chris Mysen) - ported from C++
28 public final class QRCodeTestCase extends TestCase {
30 QRCode qrCode = new QRCode();
31 // Initially the QR Code should be invalid.
32 assertFalse(qrCode.isValid());
34 // First, test simple setters and getters.
35 // We use numbers of version 7-H.
36 qrCode.setMode(Mode.BYTE);
37 qrCode.setECLevel(ErrorCorrectionLevel.H);
39 qrCode.setMatrixWidth(45);
40 qrCode.setMaskPattern(3);
41 qrCode.setNumTotalBytes(196);
42 qrCode.setNumDataBytes(66);
43 qrCode.setNumECBytes(130);
44 qrCode.setNumRSBlocks(5);
46 assertEquals(Mode.BYTE, qrCode.getMode());
47 assertEquals(ErrorCorrectionLevel.H, qrCode.getECLevel());
48 assertEquals(7, qrCode.getVersion());
49 assertEquals(45, qrCode.getMatrixWidth());
50 assertEquals(3, qrCode.getMaskPattern());
51 assertEquals(196, qrCode.getNumTotalBytes());
52 assertEquals(66, qrCode.getNumDataBytes());
53 assertEquals(130, qrCode.getNumECBytes());
54 assertEquals(5, qrCode.getNumRSBlocks());
56 // It still should be invalid.
57 assertFalse(qrCode.isValid());
59 // Prepare the matrix.
60 ByteMatrix matrix = new ByteMatrix(45, 45);
61 // Just set bogus zero/one values.
62 for (int y = 0; y < 45; ++y) {
63 for (int x = 0; x < 45; ++x) {
64 matrix.set(y, x, (y + x) % 2);
69 qrCode.setMatrix(matrix);
70 assertEquals(matrix, qrCode.getMatrix());
72 // Finally, it should be valid.
73 assertTrue(qrCode.isValid());
75 // Make sure "at()" returns the same value.
76 for (int y = 0; y < 45; ++y) {
77 for (int x = 0; x < 45; ++x) {
78 assertEquals((y + x) % 2, qrCode.at(x, y));
83 public void testToString() {
85 QRCode qrCode = new QRCode();
91 " matrixWidth: -1\n" +
92 " maskPattern: -1\n" +
93 " numTotalBytes: -1\n" +
94 " numDataBytes: -1\n" +
96 " numRSBlocks: -1\n" +
99 assertEquals(expected, qrCode.toString());
107 " matrixWidth: 21\n" +
108 " maskPattern: 3\n" +
109 " numTotalBytes: 26\n" +
110 " numDataBytes: 9\n" +
111 " numECBytes: 17\n" +
112 " numRSBlocks: 1\n" +
114 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
115 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
116 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
117 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
118 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
119 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
120 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
121 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
122 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
123 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
124 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
125 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
126 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
127 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
128 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
129 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
130 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
131 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
132 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
133 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
134 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
136 QRCode qrCode = new QRCode();
137 qrCode.setMode(Mode.BYTE);
138 qrCode.setECLevel(ErrorCorrectionLevel.H);
139 qrCode.setVersion(1);
140 qrCode.setMatrixWidth(21);
141 qrCode.setMaskPattern(3);
142 qrCode.setNumTotalBytes(26);
143 qrCode.setNumDataBytes(9);
144 qrCode.setNumECBytes(17);
145 qrCode.setNumRSBlocks(1);
146 ByteMatrix matrix = new ByteMatrix(21, 21);
147 for (int y = 0; y < 21; ++y) {
148 for (int x = 0; x < 21; ++x) {
149 matrix.set(y, x, (y + x) % 2);
152 qrCode.setMatrix(matrix);
153 assertTrue(qrCode.isValid());
154 assertEquals(expected, qrCode.toString());
158 public void testIsValidVersion() {
159 assertFalse(QRCode.isValidVersion(0));
160 assertTrue(QRCode.isValidVersion(1));
161 assertTrue(QRCode.isValidVersion(40));
162 assertFalse(QRCode.isValidVersion(0));
165 public void testIsValidMatrixWidth() {
166 assertFalse(QRCode.isValidMatrixWidth(20));
167 assertTrue(QRCode.isValidMatrixWidth(21));
168 assertTrue(QRCode.isValidMatrixWidth(177));
169 assertFalse(QRCode.isValidMatrixWidth(178));
172 public void testIsValidMaskPattern() {
173 assertFalse(QRCode.isValidMaskPattern(-1));
174 assertTrue(QRCode.isValidMaskPattern(0));
175 assertTrue(QRCode.isValidMaskPattern(7));
176 assertFalse(QRCode.isValidMaskPattern(8));