Skip to content

Commit

Permalink
Fixed crash on lower API devices when accessing the history.
Browse files Browse the repository at this point in the history
Upgrade gradle to v6.5 and plugin to 4.1.0
Fixed Instrumentation Test
v3.1.0 (20)
  • Loading branch information
Kamuno committed Oct 21, 2020
1 parent bbed62d commit 2e70fc6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 28 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.secuso.privacyFriendlyCodeScanner"
minSdkVersion 17
targetSdkVersion 29
versionCode 19
versionName "3.0.4"
versionCode 20
versionName "3.1.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
buildFeatures.dataBinding = true
vectorDrawables.useSupportLibrary = true
Expand Down Expand Up @@ -42,9 +42,9 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'androidx.cardview:cardview:1.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.secuso.privacyfriendlycodescanner.qrscanner;

import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.testing.MigrationTestHelper;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import androidx.room.Room;
import androidx.room.testing.MigrationTestHelper;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.secuso.privacyfriendlycodescanner.qrscanner.database.AppDatabase;
import com.secuso.privacyfriendlycodescanner.qrscanner.database.DBHandler;
Expand Down Expand Up @@ -49,26 +50,26 @@ public class DatabaseMigrationInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

assertEquals("com.secuso.privacyfriendlycodescanner.qrscanner", appContext.getPackageName());
}

@Before
public void prepareDatabase() {
InstrumentationRegistry.getTargetContext().deleteDatabase(TEST_DB_NAME);
InstrumentationRegistry.getInstrumentation().getTargetContext().deleteDatabase(TEST_DB_NAME);
}

@After
public void deleteDatabase() {
InstrumentationRegistry.getTargetContext().deleteDatabase(TEST_DB_NAME);
InstrumentationRegistry.getInstrumentation().getTargetContext().deleteDatabase(TEST_DB_NAME);
}

@Test
public void testMigrationToRoomDatabase_1_2() throws Exception {

// Create the old database with version 1
DBHandler dbHandler = new DBHandler(InstrumentationRegistry.getTargetContext(), TEST_DB_NAME);
DBHandler dbHandler = new DBHandler(InstrumentationRegistry.getInstrumentation().getTargetContext(), TEST_DB_NAME);

dbHandler.addContent(data1);
dbHandler.addContent(data2);
Expand Down Expand Up @@ -107,7 +108,7 @@ public void testMigrationToRoomDatabase_1_2() throws Exception {
}

private AppDatabase getMigratedRoomDatabase() {
AppDatabase database = Room.databaseBuilder(InstrumentationRegistry.getTargetContext(),
AppDatabase database = Room.databaseBuilder(InstrumentationRegistry.getInstrumentation().getTargetContext(),
AppDatabase.class, TEST_DB_NAME)
.addMigrations(AppDatabase.MIGRATIONS)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;

import com.secuso.privacyfriendlycodescanner.qrscanner.database.AppRepository;
import com.secuso.privacyfriendlycodescanner.qrscanner.database.HistoryItem;
Expand All @@ -18,7 +19,7 @@ public class HistoryViewModel extends AndroidViewModel {

private ExecutorService executorService = Executors.newSingleThreadExecutor();

private MediatorLiveData<List<HistoryItem>> historyItemsLiveData = new MediatorLiveData<>();
private final MediatorLiveData<List<HistoryItem>> historyItemsLiveData = new MediatorLiveData<>();

public HistoryViewModel(@NonNull final Application application) {
super(application);
Expand All @@ -31,14 +32,7 @@ public LiveData<List<HistoryItem>> getHistoryItems() {
}

private void loadHistories() {
executorService.execute(() -> {
LiveData<List<HistoryItem>> historyEntries = AppRepository.getInstance(getApplication()).getHistoryEntriesLiveData();

//TODO: this has to be done on the ui thread
historyItemsLiveData.addSource(historyEntries, data -> {
//Log.d("HistoryViewModel", "### \t\t ### \t Number of History Items: \t" + data.size());
historyItemsLiveData.setValue((List<HistoryItem>) data);
});
});
LiveData<List<HistoryItem>> historyEntries = AppRepository.getInstance(getApplication()).getHistoryEntriesLiveData();
historyItemsLiveData.addSource(historyEntries, historyItemsLiveData::setValue);
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.android.tools.build:gradle:4.1.0'


// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Oct 06 16:40:04 CEST 2020
#Wed Oct 21 09:18:59 CEST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip

0 comments on commit 2e70fc6

Please sign in to comment.