Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
Unit test for QRCodeUtils.class #363 (#366)
Browse files Browse the repository at this point in the history
Add tests for hotspotInfo() and generateQRBitMap() of QRCodeUtils
  • Loading branch information
shubhamkumar1739 authored Mar 22, 2020
1 parent 6d03204 commit f616549
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ 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() {
}

public static Observable<Bitmap> 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();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit f616549

Please sign in to comment.