1e954dc1c0b4765c6c80f4645a32860b88f56b5e
[zxing.git] / core / test / src / com / google / zxing / client / result / WifiParsedResultTestCase.java
1 /*
2  * Copyright 2007 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.client.result;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.Result;
21 import junit.framework.TestCase;
22
23 import java.util.Arrays;
24
25 /**
26  * Tests {@link WifiParsedResult}.
27  *
28  * @author Vikram Aggarwal
29  */
30 public final class WifiParsedResultTestCase extends TestCase {
31   public void testNoPassword() {
32     doTest("WIFI:S:NoPassword;P:;T:;;", "NoPassword", "", "");
33     doTest("WIFI:S:No Password;P:;T:;;", "No Password", "", "");
34   }
35
36   public void testWep() {
37     doTest("WIFI:S:TenChars;P:0123456789;T:WEP;;", "TenChars", "0123456789", "WEP");
38     doTest("WIFI:S:TenChars;P:abcde56789;T:WEP;;", "TenChars", "abcde56789", "WEP");
39     // Non hex should not fail at this level
40     doTest("WIFI:S:TenChars;P:hellothere;T:WEP;;", "TenChars", "hellothere", "WEP");
41
42     // Escaped semicolons
43     doTest("WIFI:S:Ten\\;\\;Chars;P:0123456789;T:WEP;;", "Ten;;Chars", "0123456789", "WEP");
44     // Escaped colons
45     doTest("WIFI:S:Ten\\:\\:Chars;P:0123456789;T:WEP;;", "Ten::Chars", "0123456789", "WEP");
46
47     // TODO(vikrama) Need a test for SB as well.
48   }
49
50   // Put in checks for the length of the password for wep.
51   public void testWpa() {
52     doTest("WIFI:S:TenChars;P:wow;T:WPA;;", "TenChars", "wow", "WPA");
53     doTest("WIFI:S:TenChars;P:space is silent;T:WPA;;", "TenChars", "space is silent", "WPA");
54     doTest("WIFI:S:TenChars;P:hellothere;T:WEP;;", "TenChars", "hellothere", "WEP");
55
56     // Escaped semicolons
57     doTest("WIFI:S:TenChars;P:hello\\;there;T:WEP;;", "TenChars", "hello;there", "WEP");
58     // Escaped colons
59     doTest("WIFI:S:TenChars;P:hello\\:there;T:WEP;;", "TenChars", "hello:there", "WEP");
60   }
61
62   // Given the string contents for the barcode, check that it matches
63   // our expectations
64   private static void doTest(String contents,
65                              String ssid,
66                              String password,
67                              String type) {
68     Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
69     ParsedResult result = ResultParser.parseResult(fakeResult);
70
71     // Ensure it is a wifi code
72     assertSame(ParsedResultType.WIFI, result.getType());
73     WifiParsedResult wifiResult = (WifiParsedResult) result;
74
75     assertEquals(ssid, wifiResult.getSsid());
76     assertEquals(password, wifiResult.getPassword());
77     assertEquals(type, wifiResult.getNetworkEncryption());
78   }
79 }