From f616549043e93640b1acae1afd8d39842d6125dd Mon Sep 17 00:00:00 2001 From: Shubham Kumar <40279181+shubhamkumar1739@users.noreply.github.com> Date: Sun, 22 Mar 2020 20:24:53 +0530 Subject: [PATCH] Unit test for QRCodeUtils.class #363 (#366) Add tests for hotspotInfo() and generateQRBitMap() of QRCodeUtils --- .../org/odk/share/utilities/QRCodeUtils.java | 3 +- .../odk/share/utilities/QRCodeUtilsTest.java | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 skunkworks_crow/src/test/java/org/odk/share/utilities/QRCodeUtilsTest.java diff --git a/skunkworks_crow/src/main/java/org/odk/share/utilities/QRCodeUtils.java b/skunkworks_crow/src/main/java/org/odk/share/utilities/QRCodeUtils.java index da899a1b..2d1f9ae6 100644 --- a/skunkworks_crow/src/main/java/org/odk/share/utilities/QRCodeUtils.java +++ b/skunkworks_crow/src/main/java/org/odk/share/utilities/QRCodeUtils.java @@ -29,6 +29,7 @@ public class QRCodeUtils { public static final String PORT = "port"; public static final String PROTECTED = "protected"; public static final String PASSWORD = "password"; + public static final int SIDE_LENGTH = 400; private QRCodeUtils() { } @@ -36,7 +37,7 @@ private QRCodeUtils() { public static Observable generateQRCode(String ssid, int port, String password) { return Observable.create(emitter -> { String qrCodeInfo = createHotspotInfo(ssid, port, password); - emitter.onNext(generateQRBitMap(qrCodeInfo, 400)); + emitter.onNext(generateQRBitMap(qrCodeInfo, SIDE_LENGTH)); emitter.onComplete(); }); } diff --git a/skunkworks_crow/src/test/java/org/odk/share/utilities/QRCodeUtilsTest.java b/skunkworks_crow/src/test/java/org/odk/share/utilities/QRCodeUtilsTest.java new file mode 100644 index 00000000..00a3021b --- /dev/null +++ b/skunkworks_crow/src/test/java/org/odk/share/utilities/QRCodeUtilsTest.java @@ -0,0 +1,55 @@ +package org.odk.share.utilities; + +import android.graphics.Bitmap; + +import com.google.zxing.WriterException; + +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.io.IOException; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@RunWith(RobolectricTestRunner.class) +public class QRCodeUtilsTest { + + /** + * {@link Test} create Hotspot Info of {@link QRCodeUtils} + */ + @Test + public void createHotspotInfoTest() throws JSONException { + String ssid = "DeviceSSID"; + int port = 3; + String password = "password"; + String hotspotInfo = QRCodeUtils.createHotspotInfo(ssid, port, password); + JSONObject hotspotInfoJson = new JSONObject(hotspotInfo); + assertTrue(hotspotInfoJson.get(QRCodeUtils.SSID).equals(ssid)); + assertTrue(hotspotInfoJson.getInt(QRCodeUtils.PORT) == port); + assertTrue(hotspotInfoJson.getBoolean(QRCodeUtils.PROTECTED)); + assertTrue(hotspotInfoJson.get(QRCodeUtils.PASSWORD).equals(password)); + + password = null; + hotspotInfo = QRCodeUtils.createHotspotInfo(ssid, port, password); + hotspotInfoJson = new JSONObject(hotspotInfo); + assertTrue(hotspotInfoJson.get(QRCodeUtils.SSID).equals(ssid)); + assertFalse(hotspotInfoJson.getBoolean(QRCodeUtils.PROTECTED)); + assertTrue(hotspotInfoJson.getInt(QRCodeUtils.PORT) == port); + } + + /** + * {@link Test} generate QR bitmap of {@link QRCodeUtils} + */ + @Test + public void generateQRBitMapTest() throws IOException, WriterException { + String data = "Test Bitmap"; + int sideLength = QRCodeUtils.SIDE_LENGTH; + Bitmap bitmap = QRCodeUtils.generateQRBitMap(data, sideLength); + assertTrue(bitmap.getHeight() == sideLength); + assertTrue(bitmap.getWidth() == sideLength); + } +}