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 junit.framework.TestCase;
24 * @author satorux@google.com (Satoru Takabayashi) - creator
25 * @author mysen@google.com (Chris Mysen) - ported from C++
27 public final class QRCodeTestCase extends TestCase {
29 QRCode qrCode = new QRCode();
30 // Initially the QR Code should be invalid.
31 assertFalse(qrCode.isValid());
33 // First, test simple setters and getters.
34 // We use numbers of version 7-H.
35 qrCode.setMode(Mode.BYTE);
36 qrCode.setECLevel(ErrorCorrectionLevel.H);
38 qrCode.setMatrixWidth(45);
39 qrCode.setMaskPattern(3);
40 qrCode.setNumTotalBytes(196);
41 qrCode.setNumDataBytes(66);
42 qrCode.setNumECBytes(130);
43 qrCode.setNumRSBlocks(5);
45 assertEquals(Mode.BYTE, qrCode.getMode());
46 assertEquals(ErrorCorrectionLevel.H, qrCode.getECLevel());
47 assertEquals(7, qrCode.getVersion());
48 assertEquals(45, qrCode.getMatrixWidth());
49 assertEquals(3, qrCode.getMaskPattern());
50 assertEquals(196, qrCode.getNumTotalBytes());
51 assertEquals(66, qrCode.getNumDataBytes());
52 assertEquals(130, qrCode.getNumECBytes());
53 assertEquals(5, qrCode.getNumRSBlocks());
55 // It still should be invalid.
56 assertFalse(qrCode.isValid());
58 // Prepare the matrix.
59 ByteMatrix matrix = new ByteMatrix(45, 45);
60 // Just set bogus zero/one values.
61 for (int y = 0; y < 45; ++y) {
62 for (int x = 0; x < 45; ++x) {
63 matrix.set(x, y, (y + x) % 2);
68 qrCode.setMatrix(matrix);
69 assertEquals(matrix, qrCode.getMatrix());
71 // Finally, it should be valid.
72 assertTrue(qrCode.isValid());
74 // Make sure "at()" returns the same value.
75 for (int y = 0; y < 45; ++y) {
76 for (int x = 0; x < 45; ++x) {
77 assertEquals((y + x) % 2, qrCode.at(x, y));
82 public void testToString() {
84 QRCode qrCode = new QRCode();
90 " matrixWidth: -1\n" +
91 " maskPattern: -1\n" +
92 " numTotalBytes: -1\n" +
93 " numDataBytes: -1\n" +
95 " numRSBlocks: -1\n" +
98 assertEquals(expected, qrCode.toString());
106 " matrixWidth: 21\n" +
107 " maskPattern: 3\n" +
108 " numTotalBytes: 26\n" +
109 " numDataBytes: 9\n" +
110 " numECBytes: 17\n" +
111 " numRSBlocks: 1\n" +
113 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
114 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
115 " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
116 " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 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" +
135 QRCode qrCode = new QRCode();
136 qrCode.setMode(Mode.BYTE);
137 qrCode.setECLevel(ErrorCorrectionLevel.H);
138 qrCode.setVersion(1);
139 qrCode.setMatrixWidth(21);
140 qrCode.setMaskPattern(3);
141 qrCode.setNumTotalBytes(26);
142 qrCode.setNumDataBytes(9);
143 qrCode.setNumECBytes(17);
144 qrCode.setNumRSBlocks(1);
145 ByteMatrix matrix = new ByteMatrix(21, 21);
146 for (int y = 0; y < 21; ++y) {
147 for (int x = 0; x < 21; ++x) {
148 matrix.set(x, y, (y + x) % 2);
151 qrCode.setMatrix(matrix);
152 assertTrue(qrCode.isValid());
153 assertEquals(expected, qrCode.toString());
157 public void testIsValidMaskPattern() {
158 assertFalse(QRCode.isValidMaskPattern(-1));
159 assertTrue(QRCode.isValidMaskPattern(0));
160 assertTrue(QRCode.isValidMaskPattern(7));
161 assertFalse(QRCode.isValidMaskPattern(8));