-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from xwiki-contrib/ANDAUTH-38
ANDAUTH - 38 Allow creating several accounts
- Loading branch information
Showing
63 changed files
with
2,173 additions
and
909 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
app/src/androidTest/java/org/xwiki/android/sync/DB/AccountsDaoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.xwiki.android.sync.DB | ||
|
||
import android.content.Context | ||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import androidx.room.Room | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import junit.framework.Assert.assertEquals | ||
import junit.framework.Assert.assertTrue | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.xwiki.android.sync.XWIKI_DEFAULT_SERVER_ADDRESS | ||
import org.xwiki.android.sync.contactdb.AppDatabase | ||
import org.xwiki.android.sync.contactdb.UserAccount | ||
import org.xwiki.android.sync.contactdb.dao.AccountsDao | ||
import java.io.IOException | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class AccountsDaoTest { | ||
|
||
@get:Rule | ||
val instantTaskExecutorRule = InstantTaskExecutorRule() | ||
|
||
private lateinit var accountsDao: AccountsDao | ||
private lateinit var db: AppDatabase | ||
|
||
@Before | ||
fun createDb() { | ||
val context = ApplicationProvider.getApplicationContext<Context>() | ||
// Using an in-memory database because the information stored here disappears when the | ||
// process is killed. | ||
db = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java) | ||
// Allowing main thread queries, just for testing. | ||
.allowMainThreadQueries() | ||
.build() | ||
accountsDao = db.usersDao() | ||
} | ||
|
||
@After | ||
@Throws(IOException::class) | ||
fun closeDb() { | ||
db.close() | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun insertAndGetUser() = runBlocking { | ||
val user = UserAccount( | ||
"testUser1", | ||
XWIKI_DEFAULT_SERVER_ADDRESS | ||
) | ||
accountsDao.insertAccount(user) | ||
val allUsers = accountsDao.getAllAccount() | ||
assertEquals(allUsers[0].accountName, user.accountName) | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun getAllUsers() = runBlocking { | ||
val user1 = UserAccount( | ||
"testUser1", | ||
XWIKI_DEFAULT_SERVER_ADDRESS | ||
) | ||
accountsDao.insertAccount(user1) | ||
val user2 = UserAccount( | ||
"testUser2", | ||
XWIKI_DEFAULT_SERVER_ADDRESS | ||
) | ||
accountsDao.insertAccount(user2) | ||
val allUsers = accountsDao.getAllAccount() | ||
assertEquals(allUsers[0].accountName, user1.accountName) | ||
assertEquals(allUsers[1].accountName, user2.accountName) | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun deleteAllUsers() = runBlocking { | ||
val user1Id = accountsDao.insertAccount( | ||
UserAccount( | ||
"testUser1", | ||
XWIKI_DEFAULT_SERVER_ADDRESS | ||
) | ||
) | ||
val user2Id = accountsDao.insertAccount( | ||
UserAccount( | ||
"testUser2", | ||
XWIKI_DEFAULT_SERVER_ADDRESS | ||
) | ||
) | ||
accountsDao.deleteUser(user1Id) | ||
accountsDao.deleteUser(user2Id) | ||
val allUsers = accountsDao.getAllAccount() | ||
assertTrue(allUsers.isEmpty()) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
app/src/androidTest/java/org/xwiki/android/sync/DB/LiveDataTestUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.xwiki.android.sync.DB | ||
|
||
/* | ||
* Copyright (C) 2017 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.Observer | ||
|
||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds. | ||
* Once we got a notification via onChanged, we stop observing. | ||
*/ | ||
@Throws(InterruptedException::class) | ||
fun <T> LiveData<T>.waitForValue(): T { | ||
val data = arrayOfNulls<Any>(1) | ||
val latch = CountDownLatch(1) | ||
val observer = object : Observer<T> { | ||
override fun onChanged(o: T?) { | ||
data[0] = o | ||
latch.countDown() | ||
this@waitForValue.removeObserver(this) | ||
} | ||
} | ||
this.observeForever(observer) | ||
latch.await(2, TimeUnit.SECONDS) | ||
|
||
return data[0] as T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.