mirror of https://github.com/tasks/tasks
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
226 lines
8.3 KiB
Java
226 lines
8.3 KiB
Java
/**
|
|
* Copyright (c) 2012 Todoroo Inc
|
|
*
|
|
* See the file "LICENSE" for the full license governing this code.
|
|
*/
|
|
package com.todoroo.astrid.gtasks;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
import android.accounts.Account;
|
|
import android.accounts.AccountManager;
|
|
import android.accounts.AccountManagerFuture;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
|
|
import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;
|
|
import com.google.api.services.tasks.model.Tasks;
|
|
import com.todoroo.andlib.service.Autowired;
|
|
import com.todoroo.andlib.service.ContextManager;
|
|
import com.todoroo.andlib.utility.AndroidUtilities;
|
|
import com.todoroo.andlib.utility.DateUtilities;
|
|
import com.todoroo.andlib.utility.Preferences;
|
|
import com.todoroo.astrid.data.Metadata;
|
|
import com.todoroo.astrid.data.Task;
|
|
import com.todoroo.astrid.gtasks.api.GtasksApiUtilities;
|
|
import com.todoroo.astrid.gtasks.api.GtasksInvoker;
|
|
import com.todoroo.astrid.gtasks.auth.GtasksTokenValidator;
|
|
import com.todoroo.astrid.gtasks.sync.GtasksSyncService;
|
|
import com.todoroo.astrid.service.TaskService;
|
|
import com.todoroo.astrid.test.DatabaseTestCase;
|
|
|
|
public class GtasksSyncOnSaveTest extends DatabaseTestCase {
|
|
|
|
@Autowired TaskService taskService;
|
|
@Autowired GtasksSyncService gtasksSyncService;
|
|
@Autowired GtasksMetadataService gtasksMetadataService;
|
|
@Autowired GtasksPreferenceService gtasksPreferenceService;
|
|
|
|
private static GtasksInvoker gtasksService;
|
|
private boolean initialized = false;
|
|
private boolean bypassTests = false;
|
|
private static final String TEST_ACCOUNT = "sync_tester2@astrid.com";
|
|
private static String DEFAULT_LIST = "@default";
|
|
|
|
//Have to wait a long time because sync on save happens in another thread--currently no way to know when finished
|
|
private static final long TIME_TO_WAIT = 8000L;
|
|
|
|
|
|
public void testSyncOnCreate() throws IOException {
|
|
if(bypassTests) return;
|
|
performBasicCreation("");
|
|
}
|
|
|
|
private Task performBasicCreation(String appendToTitle) throws IOException {
|
|
String title = "Created task" + appendToTitle;
|
|
Task localTask = setupLocalTaskModel(title);
|
|
taskService.save(localTask);
|
|
|
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
|
|
|
com.google.api.services.tasks.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
|
assertEquals(title, remoteTask.getTitle());
|
|
return localTask;
|
|
}
|
|
|
|
private Task setupLocalTaskModel(String title) {
|
|
Task localTask = new Task();
|
|
localTask.setTITLE(title);
|
|
return localTask;
|
|
}
|
|
|
|
private com.google.api.services.tasks.model.Task getRemoteTaskForLocalId(long localId) throws IOException {
|
|
Metadata gtasksMetadata = gtasksMetadataService.getTaskMetadata(localId);
|
|
assertNotNull(gtasksMetadata);
|
|
com.google.api.services.tasks.model.Task remoteTask = gtasksService.getGtask(DEFAULT_LIST, gtasksMetadata.getValue(GtasksMetadata.ID));
|
|
assertNotNull(remoteTask);
|
|
return remoteTask;
|
|
}
|
|
|
|
public void testSyncOnTitleUpdate() throws IOException {
|
|
if(bypassTests) return;
|
|
Task localTask = performBasicCreation("-title will change");
|
|
|
|
String newTitle = "Title has changed!";
|
|
localTask.setTITLE(newTitle);
|
|
taskService.save(localTask);
|
|
|
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
|
|
|
com.google.api.services.tasks.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
|
assertEquals(newTitle, remoteTask.getTitle());
|
|
}
|
|
|
|
public void testSyncOnDueDateUpdate() throws IOException {
|
|
if(bypassTests) return;
|
|
Task localTask = performBasicCreation("-due date will change");
|
|
|
|
long dueDate = new Date(115, 2, 14).getTime();
|
|
localTask.setDUE_DATE(dueDate);
|
|
taskService.save(localTask);
|
|
|
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
|
|
|
com.google.api.services.tasks.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
|
assertEquals(dueDate, GtasksApiUtilities.gtasksDueTimeToUnixTime(remoteTask.getDue(), 0));
|
|
}
|
|
|
|
public void testSyncOnNotesUpdate() throws IOException {
|
|
if(bypassTests) return;
|
|
Task localTask = performBasicCreation("-notes will change");
|
|
|
|
String notes = "Noted!";
|
|
localTask.setNOTES(notes);
|
|
taskService.save(localTask);
|
|
|
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
|
|
|
com.google.api.services.tasks.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
|
assertEquals(notes, remoteTask.getNotes());
|
|
}
|
|
|
|
public void testSyncOnCompleted() throws IOException {
|
|
if(bypassTests) return;
|
|
Task localTask = performBasicCreation("-will be completed");
|
|
|
|
long completionDate = (DateUtilities.now() / 1000L) * 1000L;
|
|
localTask.setCompletionDate(completionDate);
|
|
taskService.save(localTask);
|
|
|
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
|
|
|
com.google.api.services.tasks.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
|
assertEquals("completed", remoteTask.getStatus());
|
|
assertEquals(completionDate, GtasksApiUtilities.gtasksCompletedTimeToUnixTime(remoteTask.getCompleted(), 0));
|
|
}
|
|
|
|
public void testSyncOnDeleted() throws IOException {
|
|
if(bypassTests) return;
|
|
Task localTask = performBasicCreation("-will be deleted");
|
|
|
|
long deletionDate = DateUtilities.now();
|
|
localTask.setDELETION_DATE(deletionDate);
|
|
taskService.save(localTask);
|
|
|
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
|
|
|
com.google.api.services.tasks.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
|
assertTrue(remoteTask.getDeleted());
|
|
assertFalse(taskWithTitleExists(localTask.getTITLE()));
|
|
}
|
|
|
|
private boolean taskWithTitleExists(String title) throws IOException {
|
|
Tasks allTasks = gtasksService.getAllGtasksFromListId(DEFAULT_LIST, false, false, 0);
|
|
List<com.google.api.services.tasks.model.Task> items = allTasks.getItems();
|
|
if (items != null) {
|
|
for (com.google.api.services.tasks.model.Task t : items) {
|
|
if (t.getTitle().equals(title))
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
protected void setUp() throws Exception {
|
|
super.setUp();
|
|
|
|
if (!initialized) {
|
|
initializeTestService();
|
|
gtasksSyncService.initialize();
|
|
initialized = true;
|
|
}
|
|
|
|
setupTestList();
|
|
}
|
|
|
|
private void initializeTestService() throws Exception {
|
|
GoogleAccountManager manager = new GoogleAccountManager(ContextManager.getContext());
|
|
Account[] accounts = manager.getAccounts();
|
|
|
|
Account toUse = null;
|
|
for (Account a : accounts) {
|
|
if (a.name.equals(TEST_ACCOUNT)) {
|
|
toUse = a;
|
|
break;
|
|
}
|
|
}
|
|
if (toUse == null) {
|
|
if(accounts.length == 0) {
|
|
bypassTests = true;
|
|
return;
|
|
}
|
|
toUse = accounts[0];
|
|
}
|
|
|
|
Preferences.setString(GtasksPreferenceService.PREF_USER_NAME, toUse.name);
|
|
AccountManagerFuture<Bundle> accountManagerFuture = manager.manager.getAuthToken(toUse, "oauth2:https://www.googleapis.com/auth/tasks", true, null, null);
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
if (authTokenBundle.containsKey(AccountManager.KEY_INTENT)) {
|
|
Intent i = (Intent) authTokenBundle.get(AccountManager.KEY_INTENT);
|
|
ContextManager.getContext().startActivity(i);
|
|
return;
|
|
}
|
|
String authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
authToken = GtasksTokenValidator.validateAuthToken(getContext(), authToken);
|
|
gtasksPreferenceService.setToken(authToken);
|
|
|
|
gtasksService = new GtasksInvoker(authToken);
|
|
|
|
initialized = true;
|
|
}
|
|
|
|
private void setupTestList() throws Exception {
|
|
Tasks defaultListTasks = gtasksService.getAllGtasksFromListId(DEFAULT_LIST, false, false, 0);
|
|
List<com.google.api.services.tasks.model.Task> items = defaultListTasks.getItems();
|
|
if (items != null) {
|
|
for (com.google.api.services.tasks.model.Task t : items) {
|
|
gtasksService.deleteGtask(DEFAULT_LIST, t.getId());
|
|
}
|
|
}
|
|
}
|
|
}
|