At last update to JUnit 4.x
[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 org.junit.Assert;
22 import org.junit.Test;
23
24 /**
25  * Tests {@link WifiParsedResult}.
26  *
27  * @author Vikram Aggarwal
28  */
29 public final class WifiParsedResultTestCase extends Assert {
30   public void testNoPassword() {
31     doTest("WIFI:S:NoPassword;P:;T:;;", "NoPassword", "", "");
32     doTest("WIFI:S:No Password;P:;T:;;", "No Password", "", "");
33   }
34
35   @Test
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   /**
51    * Put in checks for the length of the password for wep.
52    */
53   @Test
54   public void testWpa() {
55     doTest("WIFI:S:TenChars;P:wow;T:WPA;;", "TenChars", "wow", "WPA");
56     doTest("WIFI:S:TenChars;P:space is silent;T:WPA;;", "TenChars", "space is silent", "WPA");
57     doTest("WIFI:S:TenChars;P:hellothere;T:WEP;;", "TenChars", "hellothere", "WEP");
58
59     // Escaped semicolons
60     doTest("WIFI:S:TenChars;P:hello\\;there;T:WEP;;", "TenChars", "hello;there", "WEP");
61     // Escaped colons
62     doTest("WIFI:S:TenChars;P:hello\\:there;T:WEP;;", "TenChars", "hello:there", "WEP");
63   }
64
65   /**
66    * Given the string contents for the barcode, check that it matches our expectations
67    */
68   private static void doTest(String contents,
69                              String ssid,
70                              String password,
71                              String type) {
72     Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
73     ParsedResult result = ResultParser.parseResult(fakeResult);
74
75     // Ensure it is a wifi code
76     assertSame(ParsedResultType.WIFI, result.getType());
77     WifiParsedResult wifiResult = (WifiParsedResult) result;
78
79     assertEquals(ssid, wifiResult.getSsid());
80     assertEquals(password, wifiResult.getPassword());
81     assertEquals(type, wifiResult.getNetworkEncryption());
82   }
83 }