Used IntelliJ's amazing refactoring functions to change the argument ordering of...
[zxing.git] / core / test / src / com / google / zxing / qrcode / encoder / QRCodeTestCase.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 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;
23
24 /**
25  * @author satorux@google.com (Satoru Takabayashi) - creator
26  * @author mysen@google.com (Chris Mysen) - ported from C++
27  */
28 public final class QRCodeTestCase extends TestCase {
29   public void test() {
30     QRCode qrCode = new QRCode();
31     // Initially the QR Code should be invalid.
32     assertFalse(qrCode.isValid());
33
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);
38     qrCode.setVersion(7);
39     qrCode.setMatrixWidth(45);
40     qrCode.setMaskPattern(3);
41     qrCode.setNumTotalBytes(196);
42     qrCode.setNumDataBytes(66);
43     qrCode.setNumECBytes(130);
44     qrCode.setNumRSBlocks(5);
45
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());
55
56     // It still should be invalid.
57     assertFalse(qrCode.isValid());
58
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(x, y, (y + x) % 2);
65       }
66     }
67
68     // Set the matrix.
69     qrCode.setMatrix(matrix);
70     assertEquals(matrix, qrCode.getMatrix());
71
72     // Finally, it should be valid.
73     assertTrue(qrCode.isValid());
74
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));
79       }
80     }
81   }
82
83   public void testToString() {
84     {
85       QRCode qrCode = new QRCode();
86       String expected =
87         "<<\n" +
88         " mode: null\n" +
89         " ecLevel: null\n" +
90         " version: -1\n" +
91         " matrixWidth: -1\n" +
92         " maskPattern: -1\n" +
93         " numTotalBytes: -1\n" +
94         " numDataBytes: -1\n" +
95         " numECBytes: -1\n" +
96         " numRSBlocks: -1\n" +
97         " matrix: null\n" +
98         ">>\n";
99       assertEquals(expected, qrCode.toString());
100     }
101     {
102       String expected =
103         "<<\n" +
104         " mode: BYTE\n" +
105         " ecLevel: H\n" +
106         " version: 1\n" +
107         " matrixWidth: 21\n" +
108         " maskPattern: 3\n" +
109         " numTotalBytes: 26\n" +
110         " numDataBytes: 9\n" +
111         " numECBytes: 17\n" +
112         " numRSBlocks: 1\n" +
113         " matrix:\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" +
135         ">>\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(x, y, (y + x) % 2);
150         }
151       }
152       qrCode.setMatrix(matrix);
153       assertTrue(qrCode.isValid());
154       assertEquals(expected, qrCode.toString());
155     }
156   }
157
158   public void testIsValidMaskPattern() {
159     assertFalse(QRCode.isValidMaskPattern(-1));
160     assertTrue(QRCode.isValidMaskPattern(0));
161     assertTrue(QRCode.isValidMaskPattern(7));
162     assertFalse(QRCode.isValidMaskPattern(8));
163   }
164
165 }