Commit aa253567 authored by Ricki Hirner's avatar Ricki Hirner

Migrate to JUnit tests; make OpenTask tests optional

parent 791d5e58
buildscript {
ext.kotlin_version = '1.2.10'
ext.kotlin_version = '1.2.21'
ext.dokka_version = '0.9.15'
repositories {
......@@ -19,6 +19,9 @@ buildscript {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
apply plugin: 'com.android.library'
......@@ -32,6 +35,8 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
......@@ -59,10 +64,11 @@ dependencies {
compile 'org.mnode.ical4j:ical4j:2.1.5'
compile 'org.slf4j:slf4j-jdk14:1.7.25'
androidTestCompile 'org.projectlombok:lombok:1.16.16'
androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile 'com.android.support.test:rules:1.0.1'
testCompile 'junit:junit:4.12'
testCompile 'org.projectlombok:lombok:1.16.16'
testCompileOnly 'org.projectlombok:lombok:1.16.20'
}
// grant permissions for unit tests
......
/*
* Copyright (c) 2013 – 2015 Ricki Hirner (bitfire web engineering).
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*/
package at.bitfire.ical4android;
import android.accounts.Account;
import android.content.ContentProviderClient;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.CalendarContract;
import android.test.InstrumentationTestCase;
import android.util.Log;
import org.dmfs.tasks.contract.TaskContract;
import java.io.FileNotFoundException;
import at.bitfire.ical4android.impl.TestCalendar;
public class AndroidCalendarTest extends InstrumentationTestCase {
private static final String TAG = "ical4android.Calendar";
final Account testAccount = new Account("ical4android.AndroidCalendarTest", TaskContract.LOCAL_ACCOUNT_TYPE);
ContentProviderClient provider;
@Override
public void setUp() throws Exception {
provider = getInstrumentation().getContext().getContentResolver().acquireContentProviderClient(CalendarContract.AUTHORITY);
assertNotNull(provider);
}
@Override
public void tearDown() throws Exception {
provider.release();
}
public void testManageCalendars() throws CalendarStorageException, FileNotFoundException {
// create calendar
ContentValues info = new ContentValues();
info.put(CalendarContract.Calendars.NAME, "TestCalendar");
info.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "ical4android Test Calendar");
info.put(CalendarContract.Calendars.VISIBLE, 0);
info.put(CalendarContract.Calendars.SYNC_EVENTS, 0);
Uri uri = TestCalendar.create(testAccount, provider, info);
assertNotNull(uri);
Log.i(TAG, "Created calendar: " + uri);
// query task list
TestCalendar calendar = (TestCalendar)TestCalendar.findByID(testAccount, provider, TestCalendar.Factory.INSTANCE, ContentUris.parseId(uri));
assertNotNull(calendar);
// delete task list
assertEquals(1, calendar.delete());
}
}
/*
* Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.ical4android
import android.Manifest
import android.accounts.Account
import android.content.ContentProviderClient
import android.content.ContentUris
import android.content.ContentValues
import android.os.Build
import android.provider.CalendarContract
import android.support.test.InstrumentationRegistry
import android.support.test.filters.MediumTest
import android.support.test.rule.GrantPermissionRule
import at.bitfire.ical4android.impl.TestCalendar
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
class AndroidCalendarTest {
@JvmField
@Rule
val permissionRule = GrantPermissionRule.grant(
Manifest.permission.READ_CALENDAR,
Manifest.permission.WRITE_CALENDAR
)!!
private val testAccount = Account("ical4android.AndroidCalendarTest", CalendarContract.ACCOUNT_TYPE_LOCAL)
private lateinit var provider: ContentProviderClient
@Before
fun prepare() {
provider = InstrumentationRegistry.getInstrumentation().targetContext.contentResolver.acquireContentProviderClient(CalendarContract.AUTHORITY)
}
@After
fun shutdown() {
if (Build.VERSION.SDK_INT >= 24)
provider.close()
else
provider.release()
}
@MediumTest
@Test
fun testManageCalendars() {
// create calendar
val info = ContentValues()
info.put(CalendarContract.Calendars.NAME, "TestCalendar")
info.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "ical4android Test Calendar")
info.put(CalendarContract.Calendars.VISIBLE, 0)
info.put(CalendarContract.Calendars.SYNC_EVENTS, 0)
val uri = AndroidCalendar.create(testAccount, provider, info)
assertNotNull(uri)
// query task list
val calendar = AndroidCalendar.findByID(testAccount, provider, TestCalendar.Factory.INSTANCE, ContentUris.parseId(uri))
assertNotNull(calendar)
// delete task list
assertEquals(1, calendar.delete())
}
}
/*
* Copyright (c) 2013 – 2015 Ricki Hirner (bitfire web engineering).
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*/
package at.bitfire.ical4android;
import android.accounts.Account;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.test.InstrumentationTestCase;
import android.util.Log;
import org.dmfs.tasks.contract.TaskContract;
import java.io.FileNotFoundException;
import at.bitfire.ical4android.impl.TestTaskList;
public class AndroidTaskListTest extends InstrumentationTestCase {
private static final String TAG = "ical4android.TaskList";
final Account testAccount = new Account("ical4android.AndroidTaskListTest", TaskContract.LOCAL_ACCOUNT_TYPE);
TaskProvider provider;
@Override
public void setUp() throws Exception {
provider = AndroidTaskList.acquireTaskProvider(getInstrumentation().getContext());
assertNotNull(provider);
Log.i(TAG, "Acquired context for " + provider.getName());
}
@Override
public void tearDown() throws Exception {
provider.close();
}
public void testManageTaskLists() throws CalendarStorageException, FileNotFoundException {
// create task list
ContentValues info = new ContentValues();
info.put(TaskContract.TaskLists.LIST_NAME, "Test Task List");
info.put(TaskContract.TaskLists.LIST_COLOR, 0xffff0000);
info.put(TaskContract.TaskLists.OWNER, "test@example.com");
info.put(TaskContract.TaskLists.SYNC_ENABLED, 1);
info.put(TaskContract.TaskLists.VISIBLE, 1);
Uri uri = TestTaskList.create(testAccount, provider, info);
assertNotNull(uri);
// query task list
TestTaskList taskList = TestTaskList.findByID(testAccount, provider, TestTaskList.Factory.FACTORY, ContentUris.parseId(uri));
assertNotNull(taskList);
// delete task list
assertEquals(1, taskList.delete());
}
}
/*
* Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.ical4android
import android.accounts.Account
import android.content.ContentUris
import android.content.ContentValues
import android.support.test.InstrumentationRegistry.getInstrumentation
import android.support.test.filters.MediumTest
import at.bitfire.ical4android.impl.TestTaskList
import org.dmfs.tasks.contract.TaskContract
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assume.assumeNotNull
import org.junit.Before
import org.junit.Test
class AndroidTaskListTest {
private var provider: TaskProvider? = null
private val testAccount = Account("AndroidTaskListTest", TaskContract.LOCAL_ACCOUNT_TYPE)
init {
TestUtils.requestTaskPermissions()
}
@Before
fun prepare() {
val providerOrNull = AndroidTaskList.acquireTaskProvider(getInstrumentation().targetContext)
assumeNotNull(providerOrNull)
provider = providerOrNull!!
}
@After
fun shutdown() {
provider?.close()
}
@MediumTest
@Test
fun testManageTaskLists() {
// create task list
val info = ContentValues()
info.put(TaskContract.TaskLists.LIST_NAME, "Test Task List")
info.put(TaskContract.TaskLists.LIST_COLOR, 0xffff0000)
info.put(TaskContract.TaskLists.OWNER, "test@example.com")
info.put(TaskContract.TaskLists.SYNC_ENABLED, 1)
info.put(TaskContract.TaskLists.VISIBLE, 1)
val uri = AndroidTaskList.create(testAccount, provider!!, info)
assertNotNull(uri)
// query task list
val taskList = AndroidTaskList.findByID(testAccount, provider!!, TestTaskList.Factory.FACTORY, ContentUris.parseId(uri))
assertNotNull(taskList)
// delete task list
assertEquals(1, taskList.delete())
}
}
/*
* Copyright (c) 2013 – 2015 Ricki Hirner (bitfire web engineering).
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*/
package at.bitfire.ical4android;
import android.accounts.Account;
import android.content.ContentUris;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.CalendarContract;
import android.test.InstrumentationTestCase;
import android.util.Log;
import net.fortuna.ical4j.model.Date;
import net.fortuna.ical4j.model.TimeZone;
import net.fortuna.ical4j.model.property.DtStart;
import net.fortuna.ical4j.model.property.Due;
import net.fortuna.ical4j.model.property.Organizer;
import org.dmfs.tasks.contract.TaskContract;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.text.ParseException;
import at.bitfire.ical4android.impl.TestTask;
import at.bitfire.ical4android.impl.TestTaskList;
import lombok.Cleanup;
public class AndroidTaskTest extends InstrumentationTestCase {
private static final String TAG = "ical4android.TaskTest";
private static final TimeZone tzVienna = DateUtils.tzRegistry.getTimeZone("Europe/Vienna");
TaskProvider provider;
final Account testAccount = new Account("ical4android.AndroidTaskTest", CalendarContract.ACCOUNT_TYPE_LOCAL);
Uri taskListUri;
AndroidTaskList taskList;
// helpers
private Uri syncAdapterURI(Uri uri) {
return TaskProvider.syncAdapterUri(uri, testAccount);
}
// initialization
@Override
public void setUp() throws RemoteException, FileNotFoundException, CalendarStorageException {
provider = AndroidTaskList.acquireTaskProvider(getInstrumentation().getTargetContext());
assertNotNull("Couldn't access task provider", provider);
taskList = TestTaskList.findOrCreate(testAccount, provider);
assertNotNull("Couldn't find/create test task list", taskList);
taskListUri = ContentUris.withAppendedId(provider.taskListsUri(), taskList.getId());
Log.i(TAG, "Prepared test task list " + taskListUri);
}
@Override
public void tearDown() throws CalendarStorageException {
Log.i(TAG, "Deleting test task list");
taskList.delete();
}
// tests
public void testAddTask() throws URISyntaxException, ParseException, FileNotFoundException, CalendarStorageException {
// build and write event to calendar provider
Task task = new Task();
task.setUid("sample1@testAddEvent");
task.setSummary("Sample event");
task.setDescription("Sample event with date/time");
task.setLocation("Sample location");
task.setDtStart(new DtStart("20150501T120000", tzVienna));
task.setDue(new Due("20150501T140000", tzVienna));
task.setOrganizer(new Organizer("mailto:organizer@example.com"));
assertFalse(task.isAllDay());
// add to task list
Uri uri = new TestTask(taskList, task).add();
assertNotNull("Couldn't add task", uri);
// read and parse event from calendar provider
@Cleanup("delete") TestTask testTask = new TestTask(taskList, ContentUris.parseId(uri));
assertNotNull("Inserted task is not here", testTask);
Task task2 = testTask.getTask();
assertNotNull("Inserted task is empty", task2);
// compare with original event
assertEquals(task.getSummary(), task2.getSummary());
assertEquals(task.getDescription(), task2.getDescription());
assertEquals(task.getLocation(), task2.getLocation());
assertEquals(task.getDtStart(), task2.getDtStart());
}
public void testAddTaskWithInvalidDue() throws ParseException, FileNotFoundException {
Task task = new Task();
task.setUid("invalidDUE@ical4android.tests");
task.setSummary("Task with invalid DUE");
task.setDtStart(new DtStart(new Date("20150102")));
task.setDue(new Due(new Date("20150101")));
try {
Uri uri = new TestTask(taskList, task).add();
fail();
} catch(CalendarStorageException ignored) {}
}
public void testUpdateTask() throws URISyntaxException, ParseException, FileNotFoundException, CalendarStorageException {
// add test event without reminder
Task task = new Task();
task.setUid("sample1@testAddEvent");
task.setSummary("Sample event");
task.setDescription("Sample event with date/time");
task.setLocation("Sample location");
task.setDtStart(new DtStart("20150501T120000", tzVienna));
assertFalse(task.isAllDay());
Uri uri = new TestTask(taskList, task).add();
// update test event in calendar
@Cleanup("delete") TestTask testTask = new TestTask(taskList, ContentUris.parseId(uri));
task = testTask.getTask();
task.setSummary("Updated event"); // change value
task.setDtStart(null); // remove value
task.setDue(new Due("20150501T140000", tzVienna)); // add value
testTask.update(task);
// read again and verify result
testTask = new TestTask(taskList, ContentUris.parseId(uri));
Task updatedTask = testTask.getTask();
assertEquals(task.getSummary(), updatedTask.getSummary());
assertEquals(task.getDtStart(), updatedTask.getDtStart());
assertEquals(task.getDue(), updatedTask.getDue());
}
public void testBuildAllDayTask() throws ParseException, FileNotFoundException, CalendarStorageException {
// add all-day event to calendar provider
Task task = new Task();
task.setSummary("All-day task");
task.setDescription("All-day task for testing");
task.setLocation("Sample location testBuildAllDayTask");
task.setDtStart(new DtStart(new Date("20150501")));
task.setDue(new Due(new Date("20150502")));
assertTrue(task.isAllDay());
Uri uri = new TestTask(taskList, task).add();
assertNotNull("Couldn't add event", uri);
// read again and verify result
@Cleanup("delete") TestTask testTask = new TestTask(taskList, ContentUris.parseId(uri));
Task task2 = testTask.getTask();
// compare with original event
assertEquals(task.getSummary(), task2.getSummary());
assertEquals(task.getDescription(), task2.getDescription());
assertEquals(task.getLocation(), task2.getLocation());
assertEquals(task.getDtStart().getDate(), task2.getDtStart().getDate());
assertEquals(task.getDue().getDate(), task2.getDue().getDate());
assertTrue(task2.isAllDay());
}
public void testGetTimeZone() throws Exception {
// no date/time
TestTask t = new TestTask(taskList, new Task());
assertEquals(TimeZone.getDefault(), t.getTimeZone());
// dtstart with date (no time)
t = new TestTask(taskList, new Task());
t.getTask().setDtStart(new DtStart("20150101"));
assertEquals(TimeZone.getDefault(), t.getTimeZone());
// dtstart with time
t = new TestTask(taskList, new Task());
t.getTask().setDtStart(new DtStart("20150101", tzVienna));
assertEquals(tzVienna, t.getTimeZone());
}
}
/*
* Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.ical4android
import android.accounts.Account
import android.content.ContentUris
import android.net.Uri
import android.support.test.InstrumentationRegistry.getInstrumentation
import android.support.test.filters.MediumTest
import at.bitfire.ical4android.impl.TestTask
import at.bitfire.ical4android.impl.TestTaskList
import net.fortuna.ical4j.model.Date
import net.fortuna.ical4j.model.TimeZone
import net.fortuna.ical4j.model.property.DtStart
import net.fortuna.ical4j.model.property.Due
import net.fortuna.ical4j.model.property.Organizer
import org.dmfs.tasks.contract.TaskContract
import org.junit.After
import org.junit.Assert.*
import org.junit.Assume.assumeNotNull
import org.junit.Before
import org.junit.Test
class AndroidTaskTest {
private val tzVienna = DateUtils.tzRegistry.getTimeZone("Europe/Vienna")
private var provider: TaskProvider? = null
private val testAccount = Account("AndroidTaskTest", TaskContract.LOCAL_ACCOUNT_TYPE)
private lateinit var taskListUri: Uri
private var taskList: TestTaskList? = null
init {
TestUtils.requestTaskPermissions()
}
@Before
fun prepare() {
// connect to OpenTasks
val providerOrNull = AndroidTaskList.acquireTaskProvider(getInstrumentation().targetContext)
assumeNotNull(providerOrNull)
provider = providerOrNull!!
taskList = TestTaskList.findOrCreate(testAccount, provider)
assertNotNull("Couldn't find/create test task list", taskList)
taskListUri = ContentUris.withAppendedId(provider!!.taskListsUri(), taskList!!.id)
}
@After
fun shutdown() {
taskList?.delete()
provider?.close()
}
// tests
@MediumTest
@Test
fun testAddTask() {
// build and write event to calendar provider
val task = Task()
task.uid = "sample1@testAddEvent"
task.summary = "Sample event"
task.description = "Sample event with date/time"
task.location = "Sample location"
task.dtStart = DtStart("20150501T120000", tzVienna)
task.due = Due("20150501T140000", tzVienna)
task.organizer = Organizer("mailto:organizer@example.com")
assertFalse(task.isAllDay())
// add to task list
val uri = TestTask(taskList, task).add()
assertNotNull("Couldn't add task", uri)
// read and parse event from calendar provider
val testTask = TestTask(taskList, ContentUris.parseId(uri))
try {
assertNotNull("Inserted task is not here", testTask)
val task2 = testTask.task
assertNotNull("Inserted task is empty", task2)
// compare with original event
assertEquals(task.summary, task2!!.summary)
assertEquals(task.description, task2.description)
assertEquals(task.location, task2.location)
assertEquals(task.dtStart, task2.dtStart)
} finally {
testTask.delete()
}
}
@MediumTest
@Test(expected = CalendarStorageException::class)
fun testAddTaskWithInvalidDue() {
val task = Task()
task.uid = "invalidDUE@ical4android.tests"
task.summary = "Task with invalid DUE"
task.dtStart = DtStart(Date("20150102"))
task.due = Due(Date("20150101"))
TestTask(taskList, task).add()
}
@MediumTest
@Test
fun testUpdateTask() {
// add test event without reminder
val task = Task()
task.uid = "sample1@testAddEvent"
task.summary = "Sample event"
task.description = "Sample event with date/time"
task.location = "Sample location"
task.dtStart = DtStart("20150501T120000", tzVienna)
assertFalse(task.isAllDay())
val uri = TestTask(taskList, task).add()
assertNotNull(uri)
val testTask = TestTask(taskList, ContentUris.parseId(uri))
try {
// update test event in calendar
val task2 = testTask.task!!
task2.summary = "Updated event" // change value
task2.dtStart = null // remove value
task2.due = Due("20150501T140000", tzVienna) // add value
testTask.update(task)
// read again and verify result
val updatedTask = TestTask(taskList, ContentUris.parseId(uri)).task!!
assertEquals(task.summary, updatedTask.summary)
assertEquals(task.dtStart, updatedTask.dtStart)
assertEquals(task.due, updatedTask.due)
} finally {
testTask.delete()
}
}
@MediumTest
@Test
fun testBuildAllDayTask() {
// add all-day event to calendar provider
val task = Task()
task.summary = "All-day task"
task.description = "All-day task for testing"
task.location = "Sample location testBuildAllDayTask"
task.dtStart = DtStart(Date("20150501"))
task.due = Due(Date("20150502"))
assertTrue(task.isAllDay())
val uri = TestTask(taskList, task).add()
assertNotNull(uri)
val testTask = TestTask(taskList, ContentUris.parseId(uri))
try {
// read again and verify result
val task2 = testTask.task!!
assertEquals(task.summary, task2.summary)
assertEquals(task.description, task2.description)
assertEquals(task.location, task2.location)
assertEquals(task.dtStart!!.date, task2.dtStart!!.date)
assertEquals(task.due!!.date, task2.due!!.date)
assertTrue(task2.isAllDay())
} finally {
testTask.delete()
}
}