Skip to content

Commit

Permalink
Somehow the ContentValue construction was always wrong for ages (but …
Browse files Browse the repository at this point in the history
…the app still works!!!!).

So. this commit removed deprecated PowerMockito dependency and adding unit test to the CI.
  • Loading branch information
MewX committed Jul 22, 2023
1 parent 263db49 commit 7eb1b98
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/android-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
profile: Nexus 6
working-directory: ./studio-android/LightNovelLibrary
# script: ./gradlew assembleAlpha connectedAlphaDebugAndroidTest jacocoTestReport coveralls
script: ./gradlew assembleAlpha connectedAlphaDebugAndroidTest
script: ./gradlew assembleAlpha testAlphaDebugUnitTest connectedAlphaDebugAndroidTest
8 changes: 3 additions & 5 deletions studio-android/LightNovelLibrary/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {
debug {
debuggable true
minifyEnabled false
testCoverageEnabled true
// testCoverageEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
}
release {
Expand Down Expand Up @@ -100,14 +100,12 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'junit:junit:4.13.2'
androidTestImplementation 'org.mockito:mockito-core:2.19.0'
androidTestImplementation 'org.mockito:mockito-core:5.4.0'
androidTestImplementation 'com.linkedin.dexmaker:dexmaker:2.19.1'
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.19.1'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-all:1.10.19'
testImplementation 'org.powermock:powermock-module-junit4:1.7.4'
testImplementation 'org.powermock:powermock-api-mockito:1.7.4'
testImplementation 'org.mockito:mockito-core:5.4.0'
}

//jacocoTestReport {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.ContentValues;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import org.mewx.wenku8.BuildConfig;
import org.mewx.wenku8.R;
Expand Down Expand Up @@ -263,11 +264,12 @@ public static int getErrorInfo_ResId(int errNo) {
* This part are the old API writing ways.
* It's not efficient enough, and maybe bug-hidden.
*/
@VisibleForTesting
static Map<String, String> getEncryptedMAP(String str) {
Map<String, String> params = new HashMap<>();
params.put("appver=" + BuildConfig.VERSION_NAME
+ "&request", LightBase64.EncodeBase64(str)
+ "&timetoken=" + System.currentTimeMillis());
params.put("appver", BuildConfig.VERSION_NAME);
params.put("request", LightBase64.EncodeBase64(str));
params.put("timetoken", "" + System.currentTimeMillis());
return params;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package android.util;

/**
* Faking the Android API.
* <p>
* From: <a href="https://stackoverflow.com/a/60318356/4206925">How to mock Base64 in Android?</a>
*/
public class Base64 {

public static String encodeToString(byte[] input, int flags) {
return java.util.Base64.getEncoder().encodeToString(input);
}

public static byte[] decode(String str, int flags) {
return java.util.Base64.getDecoder().decode(str);
}

// add other methods if required...
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,28 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mewx.wenku8.BuildConfig;
import org.mewx.wenku8.util.LightBase64;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Base64;
import java.util.Map;
import java.util.Objects;

import static org.mockito.Matchers.any;


@RunWith(PowerMockRunner.class)
@PrepareForTest({System.class, LightBase64.class, Wenku8API.class})
@RunWith(MockitoJUnitRunner.class)
public class Wenku8APITest {

private static final long CURRENT_TIME = System.currentTimeMillis();

@Before
public void init() {
// mock base64 because it's on Android
PowerMockito.mockStatic(LightBase64.class);
PowerMockito.when(LightBase64.EncodeBase64(any(String.class))).thenAnswer((Answer<String>) invocation -> {
Object[] args = invocation.getArguments();
String input = (String) args[0];
return new String(Base64.getEncoder().encode(input.getBytes()));
});

// init time and make time millis fixed
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.currentTimeMillis()).thenReturn(CURRENT_TIME);

// still need to spy current object
PowerMockito.spy(Wenku8API.class);
}

@Test
public void timeStampFixed() {
Assert.assertEquals(System.currentTimeMillis(), CURRENT_TIME);
}
public void init() {}

@Test
public void testGetEncryptedMAP() {
final String str = "test";
String str = "test";
Map<String, String> map = Wenku8API.getEncryptedMAP(str);
Assert.assertEquals(map.size(), 1);
Assert.assertEquals(map.get("request"), LightBase64.EncodeBase64(str + "&timetoken=" + CURRENT_TIME));

Assert.assertEquals(map.size(), 3);
Assert.assertEquals(map.get("appver"), BuildConfig.VERSION_NAME);
Assert.assertEquals(LightBase64.DecodeBase64String(Objects.requireNonNull(map.get("request"))), str);
Assert.assertTrue(Long.parseLong(Objects.requireNonNull(map.get("timetoken"))) > 0L);
}
}

0 comments on commit 7eb1b98

Please sign in to comment.