Skip to content

Commit

Permalink
Merge branch 'room_experiments' of https://github.com/archie94/haven
Browse files Browse the repository at this point in the history
…into archie94-room_experiments
  • Loading branch information
n8fr8 committed Feb 5, 2019
2 parents b8d4b9e + 68cf160 commit 451b659
Show file tree
Hide file tree
Showing 38 changed files with 1,408 additions and 571 deletions.
46 changes: 45 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
buildscript {
ext.kotlin_version = '1.2.60'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0-alpha01'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

}
Expand All @@ -23,6 +25,12 @@ def getVersionName = { ->

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

repositories {
google()
jcenter()
Expand Down Expand Up @@ -89,6 +97,17 @@ android {
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86"
}

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

// clear app state completely between tests
testInstrumentationRunnerArguments clearPackageData: 'true'

javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}


Expand All @@ -107,10 +126,18 @@ android {
checkReleaseBuilds false
abortOnError false
}

sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
}

dependencies {
<<<<<<< HEAD
implementation 'androidx.appcompat:appcompat:1.0.2'
=======
implementation 'androidx.appcompat:appcompat:1.0.1'
>>>>>>> 68cf160e0173ec91057984f81c802ab3566a3eca
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.preference:preference:1.0.0'
Expand All @@ -119,7 +146,6 @@ dependencies {
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.wdullaer:materialdatetimepicker:4.1.1'
implementation 'com.github.guardianproject:signal-cli-android:v0.6.0-android-beta-1'
implementation 'com.github.satyan:sugar:1.5'
implementation 'net.the4thdimension:audio-wife:1.0.3'
implementation 'com.github.apl-devs:appintro:master' /* use master until androidx ver is released */
implementation 'info.guardianproject.netcipher:netcipher:2.0.0-beta1'
Expand All @@ -140,4 +166,22 @@ dependencies {
implementation 'io.github.silvaren:easyrs:0.5.3'
implementation 'org.jcodec:jcodec:0.2.3'
implementation 'org.jcodec:jcodec-android:0.2.3'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

// Room
implementation "android.arch.persistence.room:runtime:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
implementation "android.arch.lifecycle:runtime:1.1.1"
implementation "android.arch.lifecycle:extensions:1.1.1"

testImplementation "junit:junit:4.12"
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:core:1.0.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation "android.arch.persistence.room:testing:1.1.1"

// android-job
implementation 'com.evernote:android-job:1.2.6'
}
83 changes: 83 additions & 0 deletions schemas/org.havenapp.main.database.HavenEventDB/4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"formatVersion": 1,
"database": {
"version": 4,
"identityHash": "e6812687ff1f63ddcb6ebbf062ac6267",
"entities": [
{
"tableName": "EVENT",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT, `M_START_TIME` INTEGER)",
"fields": [
{
"fieldPath": "id",
"columnName": "ID",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "mStartTime",
"columnName": "M_START_TIME",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"ID"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "EVENT_TRIGGER",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT, `M_TYPE` INTEGER, `M_TIME` INTEGER, `M_EVENT_ID` INTEGER, `M_PATH` TEXT)",
"fields": [
{
"fieldPath": "id",
"columnName": "ID",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "mType",
"columnName": "M_TYPE",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "mTime",
"columnName": "M_TIME",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "mEventId",
"columnName": "M_EVENT_ID",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "mPath",
"columnName": "M_PATH",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"ID"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"e6812687ff1f63ddcb6ebbf062ac6267\")"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.havenapp.main.database.migration

import androidx.test.core.app.ApplicationProvider
import androidx.room.Room
import androidx.room.testing.MigrationTestHelper
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import androidx.test.platform.app.InstrumentationRegistry
import junit.framework.Assert.assertEquals
import org.havenapp.main.database.HavenEventDB
import org.havenapp.main.database.converter.HavenEventDBConverters.Companion.dateToTimestamp
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test

/**
* Created by Arka Prava Basu <[email protected]> on 27/10/18.
*/
class RoomMigrationTest {
@get:Rule
val migrationTestHelper = MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
HavenEventDB::class.java.canonicalName,
FrameworkSQLiteOpenHelperFactory())

private var sugarDbOpenHelper: SugarDbOpenHelper? = null

private val TEST_DB_NAME = "test.db"

@Before
fun setUpDb() {
sugarDbOpenHelper =
SugarDbOpenHelper(ApplicationProvider.getApplicationContext(), TEST_DB_NAME)
SugarDbTestHelper.createTables(sugarDbOpenHelper!!)
}

@Test
fun validateMigrationAndData() {
SugarDbTestHelper.insertEvent(123, sugarDbOpenHelper!!)
SugarDbTestHelper.insertEventTrigger(1, "abcabd", 124, 1, sugarDbOpenHelper!!)

migrationTestHelper.runMigrationsAndValidate(TEST_DB_NAME, 4,
true, RoomMigration())

val migratedDb = getMigratedRoomDb()

val event = migratedDb.getEventDAO().getAllEvent()[0]
val eventTrigger = migratedDb.getEventTriggerDAO().getAllEventTriggers()[0]

assertEquals(dateToTimestamp(event.startTime)?.toInt(), 123)

assertEquals(dateToTimestamp(eventTrigger.time)?.toInt(), 124)
assertEquals(eventTrigger.path, "abcabd")
assertEquals(eventTrigger.type, 1)
}

@After
fun clearDb() {
SugarDbTestHelper.clearDb(sugarDbOpenHelper!!)
}

private fun getMigratedRoomDb(): org.havenapp.main.database.HavenEventDB {
val db = Room.databaseBuilder(ApplicationProvider.getApplicationContext(),
org.havenapp.main.database.HavenEventDB::class.java, TEST_DB_NAME)
.addMigrations(RoomMigration())
.build()

migrationTestHelper.closeWhenFinished(db)

return db
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.havenapp.main.database.migration

import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

/**
* Created by Arka Prava Basu <[email protected]> on 27/10/18.
*/
class SugarDbOpenHelper(context: Context, dbName: String)
: SQLiteOpenHelper(context, dbName, null, 3) {


private val createEventTable =
"CREATE TABLE IF NOT EXISTS EVENT ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_START_TIME INTEGER )"
private val createEventTriggerTable =
"CREATE TABLE IF NOT EXISTS EVENT_TRIGGER ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_EVENT_ID INTEGER, M_PATH TEXT, M_TIME INTEGER , M_TYPE INTEGER )"

override fun onCreate(db: SQLiteDatabase?) {
db?.execSQL(createEventTable)
db?.execSQL(createEventTriggerTable)
}

override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {

}

override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.havenapp.main.database.migration

/**
* Created by Arka Prava Basu <[email protected]> on 28/10/18.
*/
class SugarDbTestHelper {

companion object {
fun createTables(helper: SugarDbOpenHelper) {
val db = helper.writableDatabase

val createEventTable =
"CREATE TABLE IF NOT EXISTS EVENT ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_START_TIME INTEGER )"
val createEventTriggerTable =
"CREATE TABLE IF NOT EXISTS EVENT_TRIGGER ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_EVENT_ID INTEGER, M_PATH TEXT, M_TIME INTEGER , M_TYPE INTEGER )"

db.execSQL(createEventTable)
db.execSQL(createEventTriggerTable)

db.close()
}


fun insertEvent(startTime: Int, helper: SugarDbOpenHelper) {
val db = helper.writableDatabase

db?.execSQL("INSERT INTO EVENT(M_START_TIME) VALUES ($startTime)")

db.close()
}

fun insertEventTrigger(eventId: Int, path: String, startTime: Int,
type: Int, helper: SugarDbOpenHelper) {
val db = helper.writableDatabase

db?.execSQL("INSERT INTO EVENT_TRIGGER(M_EVENT_ID, M_PATH, M_TIME, M_TYPE) VALUES ($eventId, \"$path\", $startTime, $type)")

db.close()
}

fun clearDb(helper: SugarDbOpenHelper) {
val db = helper.writableDatabase

db?.execSQL("DROP TABLE IF EXISTS EVENT")
db?.execSQL("DROP TABLE IF EXISTS EVENT_TRIGGER")

db.close()
}
}
}
4 changes: 4 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/AppTheme"
android:fullBackupContent="@xml/backup_config"
android:supportsRtl="false"
tools:replace="android:allowBackup,android:supportsRtl"
tools:ignore="GoogleAppIndexingWarning">
Expand Down Expand Up @@ -85,6 +86,9 @@
android:name=".service.MonitorService"
android:exported="false" />

<service android:name=".service.RemoveDeletedFilesJob"
android:exported="false" />

<meta-data
android:name="DATABASE"
android:value="haven.db" />
Expand Down
Loading

0 comments on commit 451b659

Please sign in to comment.