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.qrcode.decoder.ErrorCorrectionLevel;
20 import com.google.zxing.qrcode.decoder.Mode;
21 import org.junit.Assert;
22 import org.junit.Test;
25 * @author satorux@google.com (Satoru Takabayashi) - creator
26 * @author mysen@google.com (Chris Mysen) - ported from C++
28 public final class QRCodeTestCase extends Assert {
32 QRCode qrCode = new QRCode();
33 // Initially the QR Code should be invalid.
34 assertFalse(qrCode.isValid());
36 // First, test simple setters and getters.
37 // We use numbers of version 7-H.
38 qrCode.setMode(Mode.BYTE);
39 qrCode.setECLevel(ErrorCorrectionLevel.H);
41 qrCode.setMatrixWidth(45);
42 qrCode.setMaskPattern(3);
43 qrCode.setNumTotalBytes(196);
44 qrCode.setNumDataBytes(66);
45 qrCode.setNumECBytes(130);
46 qrCode.setNumRSBlocks(5);
48 assertSame(Mode.BYTE, qrCode.getMode());
49 assertSame(ErrorCorrectionLevel.H, qrCode.getECLevel());
50 assertEquals(7, qrCode.getVersion());
51 assertEquals(45, qrCode.getMatrixWidth());
52 assertEquals(3, qrCode.getMaskPattern());
53 assertEquals(196, qrCode.getNumTotalBytes());
54 assertEquals(66, qrCode.getNumDataBytes());
55 assertEquals(130, qrCode.getNumECBytes());
56 assertEquals(5, qrCode.getNumRSBlocks());
58 // It still should be invalid.
59 assertFalse(qrCode.isValid());
61 // Prepare the matrix.
62 ByteMatrix matrix = new ByteMatrix(45, 45);
63 // Just set bogus zero/one values.
64 for (int y = 0; y < 45; ++y) {
65 for (int x = 0; x < 45; ++x) {
66 matrix.set(x, y, (y + x) % 2);
71 qrCode.setMatrix(matrix);
72 assertEquals(matrix, qrCode.getMatrix());
74 // Finally, it should be valid.
75 assertTrue(qrCode.isValid());
77 // Make sure "at()" returns the same value.
78 for (int y = 0; y < 45; ++y) {
79 for (int x = 0; x < 45; ++x) {
80 assertEquals((y + x) % 2, qrCode.at(x, y));
86 public void testToString() {
88 QRCode qrCode = new QRCode();
94 " matrixWidth: -1\n" +
95 " maskPattern: -1\n" +
96 " numTotalBytes: -1\n" +
97 " numDataBytes: -1\n" +
99 " numRSBlocks: -1\n" +
102 assertEquals(expected, qrCode.toString());
110 " matrixWidth: 21\n" +
111 " maskPattern: 3\n" +
112 " numTotalBytes: 26\n" +
113 " numDataBytes: 9\n" +
114 " numECBytes: 17\n" +
115 " numRSBlocks: 1\n" +
117 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
118 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
119 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
120 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
121 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
122 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
123 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
124 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
125 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
126 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
127 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
128 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
129 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
130 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
131 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
132 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
133 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
134 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
135 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
136 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
137 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
139 QRCode qrCode = new QRCode();
140 qrCode.setMode(Mode.BYTE);
141 qrCode.setECLevel(ErrorCorrectionLevel.H);
142 qrCode.setVersion(1);
143 qrCode.setMatrixWidth(21);
144 qrCode.setMaskPattern(3);
145 qrCode.setNumTotalBytes(26);
146 qrCode.setNumDataBytes(9);
147 qrCode.setNumECBytes(17);
148 qrCode.setNumRSBlocks(1);
149 ByteMatrix matrix = new ByteMatrix(21, 21);
150 for (int y = 0; y < 21; ++y) {
151 for (int x = 0; x < 21; ++x) {
152 matrix.set(x, y, (y + x) % 2);
155 qrCode.setMatrix(matrix);
156 assertTrue(qrCode.isValid());
157 assertEquals(expected, qrCode.toString());
162 public void testIsValidMaskPattern() {
163 assertFalse(QRCode.isValidMaskPattern(-1));
164 assertTrue(QRCode.isValidMaskPattern(0));
165 assertTrue(QRCode.isValidMaskPattern(7));
166 assertFalse(QRCode.isValidMaskPattern(8));