Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Core library changes to include wifi type. One measly test included a…
…s well.
- Loading branch information
vikrama
committed
Jun 10, 2010
1 parent
42639ec
commit 0f94ff3
Showing
5 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
core/src/com/google/zxing/client/result/WifiParsedResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2010 ZXing authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.zxing.client.result; | ||
|
||
/** | ||
* @author Vikram Aggarwal | ||
*/ | ||
public final class WifiParsedResult extends ParsedResult { | ||
private final String ssid; | ||
private final String networkEncryption; | ||
private final String password; | ||
|
||
public WifiParsedResult(String networkEncryption, String ssid, String password) { | ||
super(ParsedResultType.WIFI); | ||
this.ssid = ssid; | ||
this.networkEncryption = networkEncryption; | ||
this.password = password; | ||
} | ||
|
||
public String getSsid() { | ||
return ssid; | ||
} | ||
|
||
public String getNetworkEncryption() { | ||
return networkEncryption; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public String getDisplayResult() { | ||
StringBuffer result = new StringBuffer(80); | ||
maybeAppend(ssid, result); | ||
maybeAppend(networkEncryption, result); | ||
maybeAppend(password, result); | ||
return result.toString(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
core/src/com/google/zxing/client/result/WifiResultParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2010 ZXing authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.zxing.client.result; | ||
|
||
import com.google.zxing.Result; | ||
|
||
/** | ||
* Parses a WIFI configuration string. Strings will be of the form: | ||
* WIFI:T:WPA;S:mynetwork;P:mypass;; | ||
* | ||
* The fields can come in any order, and there should be tests to see | ||
* if we can parse them all correctly. | ||
* | ||
* @author Vikram Aggarwal | ||
*/ | ||
final class WifiResultParser extends ResultParser { | ||
|
||
private WifiResultParser() { | ||
} | ||
|
||
public static WifiParsedResult parse(Result result) { | ||
String rawText = result.getText(); | ||
|
||
if (rawText == null || !rawText.startsWith("WIFI:")) { | ||
return null; | ||
} | ||
|
||
// Don't remove leading or trailing whitespace | ||
boolean trim = false; | ||
String ssid = matchSinglePrefixedField("S:", rawText, ';', trim); | ||
String pass = matchSinglePrefixedField("P:", rawText, ';', trim); | ||
String type = matchSinglePrefixedField("T:", rawText, ';', trim); | ||
|
||
return new WifiParsedResult(type, ssid, pass); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
core/test/src/com/google/zxing/client/result/WifiParsedResultTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2007 ZXing authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.zxing.client.result; | ||
|
||
import com.google.zxing.BarcodeFormat; | ||
import com.google.zxing.Result; | ||
import junit.framework.TestCase; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Tests {@link WifiParsedResult}. | ||
* | ||
* @author Vikram Aggarwal | ||
*/ | ||
public final class WifiParsedResultTestCase extends TestCase { | ||
public void testNoPassword() { | ||
doTest("WIFI:S:NoPassword;P:;T:;;", "NoPassword", "", ""); | ||
doTest("WIFI:S:No Password;P:;T:;;", "No Password", "", ""); | ||
} | ||
|
||
public void testWep() { | ||
doTest("WIFI:S:TenChars;P:0123456789;T:WEP;;", "TenChars", "0123456789", "WEP"); | ||
doTest("WIFI:S:TenChars;P:abcde56789;T:WEP;;", "TenChars", "abcde56789", "WEP"); | ||
// Non hex should not fail at this level | ||
doTest("WIFI:S:TenChars;P:hellothere;T:WEP;;", "TenChars", "hellothere", "WEP"); | ||
|
||
// Escaped semicolons | ||
doTest("WIFI:S:Ten\\;\\;Chars;P:0123456789;T:WEP;;", "Ten;;Chars", "0123456789", "WEP"); | ||
// Escaped colons | ||
doTest("WIFI:S:Ten\\:\\:Chars;P:0123456789;T:WEP;;", "Ten::Chars", "0123456789", "WEP"); | ||
|
||
// TODO(vikrama) Need a test for SB as well. | ||
} | ||
|
||
// Put in checks for the length of the password for wep. | ||
public void testWpa() { | ||
doTest("WIFI:S:TenChars;P:wow;T:WPA;;", "TenChars", "wow", "WPA"); | ||
doTest("WIFI:S:TenChars;P:space is silent;T:WPA;;", "TenChars", "space is silent", "WPA"); | ||
doTest("WIFI:S:TenChars;P:hellothere;T:WEP;;", "TenChars", "hellothere", "WEP"); | ||
|
||
// Escaped semicolons | ||
doTest("WIFI:S:TenChars;P:hello\\;there;T:WEP;;", "TenChars", "hello;there", "WEP"); | ||
// Escaped colons | ||
doTest("WIFI:S:TenChars;P:hello\\:there;T:WEP;;", "TenChars", "hello:there", "WEP"); | ||
} | ||
|
||
// Given the string contents for the barcode, check that it matches | ||
// our expectations | ||
private static void doTest(String contents, | ||
String ssid, | ||
String password, | ||
String type) { | ||
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE); | ||
ParsedResult result = ResultParser.parseResult(fakeResult); | ||
|
||
// Ensure it is a wifi code | ||
assertSame(ParsedResultType.WIFI, result.getType()); | ||
WifiParsedResult wifiResult = (WifiParsedResult) result; | ||
|
||
assertEquals(ssid, wifiResult.getSsid()); | ||
assertEquals(password, wifiResult.getPassword()); | ||
assertEquals(type, wifiResult.getNetworkEncryption()); | ||
} | ||
} |