Skip to content

Commit

Permalink
* Add more tests
Browse files Browse the repository at this point in the history
* Start reporting errors from RxJava
* Others
  • Loading branch information
Andr3Carvalh0 committed Mar 19, 2018
1 parent fc5aeed commit 9e993cc
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 21 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,11 @@ fabric.properties

### Java ###
# Compiled class file
./filepicker/build
./filepicker/build/*
filepicker/build
filepicker/build/*

twoway-view/build
twoway-view/build/*

# Log file

Expand Down
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
13 changes: 9 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ android {
dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' })
implementation 'com.github.hotchemi:permissionsdispatcher:3.0.1'
implementation ('com.mikepenz:aboutlibraries:6.0.6@aar') { transitive = true }
implementation "com.android.support:appcompat-v7:" + project.ext.AppCompatVersion
Expand All @@ -65,17 +64,23 @@ dependencies {
implementation 'com.annimon:stream:1.1.9'
implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.1'
implementation "android.arch.persistence.room:runtime:1.0.0"

// Test helpers for Room
testImplementation "android.arch.persistence.room:testing:1.0.0"
implementation "com.android.support:preference-v7:" + project.ext.AppCompatVersion
implementation "com.android.support:preference-v14:" + project.ext.AppCompatVersion

//Tests
androidTestImplementation 'org.mockito:mockito-core:1.10.19'
testImplementation "android.arch.persistence.room:testing:1.0.0"
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' })

//Annotations
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:3.0.1'

//third-party
implementation project(':filepicker')
implementation project(':floatingsearchview')
implementation project(':twoway-view')
Expand Down
2 changes: 0 additions & 2 deletions app/src/androidTest/java/io/mgba/DatabaseTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import io.mgba.Data.Database.Database;
Expand Down
29 changes: 29 additions & 0 deletions app/src/androidTest/java/io/mgba/LibraryTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.mgba;

import android.app.Application;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import io.mgba.Model.Interfaces.ILibrary;
import io.mgba.Model.Library;
import io.mgba.Utils.TestsDB;
import io.mgba.Utils.TestsFLM;

@RunWith(AndroidJUnit4.class)
public class LibraryTests {

private ILibrary library;

@Before
public void init() {
Context ctx = InstrumentationRegistry.getTargetContext();
library = new Library(((mgba)ctx.getApplicationContext()), new TestsDB(), new TestsFLM());
}

}
61 changes: 61 additions & 0 deletions app/src/androidTest/java/io/mgba/Utils/TestsDB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.mgba.Utils;

import org.junit.BeforeClass;

import java.util.LinkedList;
import java.util.List;

import io.mgba.Data.Database.Game;
import io.mgba.Data.Platform;
import io.mgba.Model.Interfaces.IDatabase;

public class TestsDB implements IDatabase{
private static List<Game> games;

@BeforeClass
public static void init() {
games = new LinkedList<>();
games.add(new Game("/a.gba", "Pokemon", "Some desc", "2000", "I", "", "", "AAA", false, Platform.GBA));
games.add(new Game("/b.gba", "Metroid", "Some desc", "2000", "I", "", "", "AAA", true, Platform.GBA));
games.add(new Game("/c.gba", "Zelda", "Some desc", "2000", "I", "", "", "AAA", true, Platform.GBC));
games.add(new Game("/d.gba", "Mario", "Some desc", "2000", "I", "", "", "AAA", false, Platform.GBC));

}

@Override
public List<Game> getGamesForPlatform(Platform platform) {
return games;
}

@Override
public List<Game> getFavouritesGames() {
return games;
}

@Override
public void insert(Game... games) {

}

@Override
public void delete() {

}

@Override
public void delete(Game game) {
System.out.println("");


}

@Override
public List<Game> queryForGames(String query) {
return null;
}

@Override
public List<Game> getGames() {
return null;
}
}
45 changes: 45 additions & 0 deletions app/src/androidTest/java/io/mgba/Utils/TestsFLM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.mgba.Utils;

import com.google.common.base.Predicate;

import org.junit.BeforeClass;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import io.mgba.Model.Interfaces.IFilesManager;

public class TestsFLM implements IFilesManager {
private static List<File> files;

private String dir = "";

@BeforeClass
public static void init() {
files = new LinkedList<>();
files.add(new File("/a.b"));
files.add(new File("/c.d"));
files.add(new File("/e.f"));
files.add(new File("/g.h"));
}

@Override
public List<File> getGameList(Predicate predicate) {
return files;
}

@Override
public List<File> getGameList() {
return files;
}

@Override
public String getCurrentDirectory() {
return dir;
}

@Override
public void setCurrentDirectory(String directory) {
dir = directory;
}
}
7 changes: 6 additions & 1 deletion app/src/main/java/io/mgba/Model/IO/FilesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ public FilesManager(String directory) {
* @return the file's extension
*/
public static String getFileExtension(File file){
return file.getName()
String name = file.getName();

if(!name.contains("."))
return name.substring(name.length() - 3);

return name
.substring(file.getName().lastIndexOf("."))
.substring(1)
.toLowerCase();
Expand Down
26 changes: 20 additions & 6 deletions app/src/main/java/io/mgba/Model/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ public Library(mgba application) {
application.inject(this);
}

public Library(mgba application, IDatabase database, IFilesManager filesService) {
this.application = application;
this.database = database;
this.filesService = filesService;
}


@Override
public Single<List<Game>> prepareGames(Platform platform) {
return Single.create(subscriber -> {
Single<List<Game>> ret = Single.create(subscriber -> {

if(platform == null){
subscriber.onSuccess(new LinkedList<>());
Expand All @@ -45,11 +52,13 @@ public Single<List<Game>> prepareGames(Platform platform) {

subscriber.onSuccess(games);
});

return ret.doOnError(mgba::report);
}

@Override
public Single<List<Game>> query(String query) {
return Single.create(subscriber -> {
Single<List<Game>> ret = Single.create(subscriber -> {

if(query == null || query.length() == 0){
subscriber.onSuccess(new LinkedList<>());
Expand All @@ -60,11 +69,13 @@ public Single<List<Game>> query(String query) {

subscriber.onSuccess(games);
});

return ret.doOnError(mgba::report);
}

@Override
public Single<List<Game>> reloadGames(Platform... platform) {
return Single.create(subscriber -> {
Single<List<Game>> ret = Single.create(subscriber -> {
//clean up possible removed files from content provider
List<Game> games = database.getGames();

Expand All @@ -75,9 +86,11 @@ public Single<List<Game>> reloadGames(Platform... platform) {
games.addAll(updatedList);

Collections.sort(games, (o1, o2) -> o1.getName().compareTo(o2.getName()));

subscriber.onSuccess(filter(Arrays.asList(platform), games));
});

return ret.doOnError(mgba::report);

}

@Override
Expand All @@ -98,8 +111,9 @@ private void removeGamesFromDatabase(List<Game> games){
private List<Game> processNewGames(List<Game> games){
return Stream.of(filesService.getGameList())
.map(f -> new Game(f.getAbsolutePath(), getPlatform(f)))
.filter(f -> games.size() == 0 ||
Stream.of(games).anyMatch(g -> g.getFile().equals(f.getFile()) && g.needsUpdate()))
.filter(f -> games.size() == 0
|| Stream.of(games).anyMatch(g -> g.getFile().equals(f.getFile())
&& g.needsUpdate()))
.map(g -> {
Stream.of(games).filter(g1 -> g1.equals(g)).forEach(games::remove);

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/io/mgba/Presenter/GamesPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.mgba.Data.Database.Game;
import io.mgba.Data.Platform;
import io.mgba.R;
import io.mgba.mgba;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
Expand Down Expand Up @@ -57,7 +58,8 @@ public void loadGames(io.mgba.UI.Activities.Interfaces.ILibrary databaseHelper,
// A do nothing kinda of method just so I can update UI.
// On a rx thread that need to be non UI.
private Single<Boolean> updateView(){
return Single.create(s -> s.onSuccess(true));
Single<Boolean> ret = Single.create(s -> s.onSuccess(true));
return ret.doOnError(mgba::report);
}

@Override
Expand Down
22 changes: 17 additions & 5 deletions app/src/main/java/io/mgba/mgba.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ private ModelComponent initModelComponent_Dagger(mgba application){
.build();
}

public static void printLog(String tag, String message){
if(BuildConfig.DEBUG)
Log.v(tag, message);
}

public void savePreference(String key, String value) {
preparePreferencesController();
preferencesController.save(key, value);
Expand Down Expand Up @@ -136,4 +131,21 @@ public void inject(MainPresenter target) {
public void inject(Library library) {
modelComponent.inject(library);
}

public static void report(Throwable error) {
if(BuildConfig.DEBUG) {

Log.e(" --- An error occurred:", error.getMessage());
Log.e(" --- StackTrace:", error.getStackTrace().toString());

}

}

public static void printLog(String tag, String message){
if(BuildConfig.DEBUG)
Log.v(tag, message);
}


}
29 changes: 29 additions & 0 deletions app/src/test/java/io/mgba/FileManagerTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.mgba;

import org.junit.Test;

import java.io.File;
import io.mgba.Model.IO.FilesManager;

import static org.junit.Assert.assertEquals;

public class FileManagerTests {

@Test
public void getExtensionOnFileWithExtension(){
assertEquals("test", FilesManager.getFileExtension(new File("/something.test")));
}


@Test
public void getExtensionOnFileWithoutExtension(){
assertEquals("est", FilesManager.getFileExtension(new File("/somethingtest")));
}

@Test
public void getFilenameWithoutExtension(){
assertEquals("something", FilesManager.getFileWithoutExtension(new File("/something.test")));
}


}

0 comments on commit 9e993cc

Please sign in to comment.