mirror of https://github.com/tasks/tasks
Added ability to sync individual gtasks immediately on save. Properties synced--all relevant task data e.g. title, due date, parent indentation. Properties ignored--ordering in a list. Also fixed several major bugs related to normal syncing.
parent
06330e4f45
commit
6254348933
@ -0,0 +1,326 @@
|
|||||||
|
package com.todoroo.astrid.gtasks.sync;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
|
import com.todoroo.andlib.data.DatabaseDao.ModelUpdateListener;
|
||||||
|
import com.todoroo.andlib.data.Property;
|
||||||
|
import com.todoroo.andlib.service.Autowired;
|
||||||
|
import com.todoroo.andlib.service.DependencyInjectionService;
|
||||||
|
import com.todoroo.andlib.utility.AndroidUtilities;
|
||||||
|
import com.todoroo.andlib.utility.DateUtilities;
|
||||||
|
import com.todoroo.andlib.utility.Preferences;
|
||||||
|
import com.todoroo.astrid.dao.MetadataDao;
|
||||||
|
import com.todoroo.astrid.dao.TaskDao;
|
||||||
|
import com.todoroo.astrid.data.Metadata;
|
||||||
|
import com.todoroo.astrid.data.Task;
|
||||||
|
import com.todoroo.astrid.gtasks.GtasksMetadata;
|
||||||
|
import com.todoroo.astrid.gtasks.GtasksMetadataService;
|
||||||
|
import com.todoroo.astrid.gtasks.GtasksPreferenceService;
|
||||||
|
import com.todoroo.astrid.gtasks.GtasksTaskListUpdater;
|
||||||
|
import com.todoroo.astrid.gtasks.api.GoogleTasksException;
|
||||||
|
import com.todoroo.astrid.gtasks.api.GtasksApiUtilities;
|
||||||
|
import com.todoroo.astrid.gtasks.api.GtasksService;
|
||||||
|
import com.todoroo.astrid.gtasks.api.MoveRequest;
|
||||||
|
import com.todoroo.astrid.gtasks.auth.GtasksTokenValidator;
|
||||||
|
import com.todoroo.astrid.service.MetadataService;
|
||||||
|
import com.todoroo.astrid.utility.Flags;
|
||||||
|
|
||||||
|
public final class GtasksSyncOnSaveService {
|
||||||
|
|
||||||
|
@Autowired MetadataService metadataService;
|
||||||
|
@Autowired MetadataDao metadataDao;
|
||||||
|
@Autowired GtasksMetadataService gtasksMetadataService;
|
||||||
|
@Autowired TaskDao taskDao;
|
||||||
|
@Autowired GtasksPreferenceService gtasksPreferenceService;
|
||||||
|
@Autowired GtasksTaskListUpdater gtasksTaskListUpdater;
|
||||||
|
|
||||||
|
public GtasksSyncOnSaveService() {
|
||||||
|
DependencyInjectionService.getInstance().inject(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Semaphore syncOnSaveSema = new Semaphore(1);
|
||||||
|
|
||||||
|
public void initialize() {
|
||||||
|
taskDao.addListener(new ModelUpdateListener<Task>() {
|
||||||
|
public void onModelUpdated(final Task model) {
|
||||||
|
if (!syncOnSaveEnabled())
|
||||||
|
return;
|
||||||
|
if (gtasksPreferenceService.isOngoing()) //Don't try and sync changes that occur during a normal sync
|
||||||
|
return;
|
||||||
|
if(Flags.checkAndClear(Flags.GTASKS_SUPPRESS_SYNC))
|
||||||
|
return;
|
||||||
|
final ContentValues setValues = model.getSetValues();
|
||||||
|
if(setValues == null || !checkForToken())
|
||||||
|
return;
|
||||||
|
if (!checkValuesForProperties(setValues, TASK_PROPERTIES)) //None of the properties we sync were updated
|
||||||
|
return;
|
||||||
|
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// sleep so metadata associated with task is saved
|
||||||
|
AndroidUtilities.sleepDeep(1000L);
|
||||||
|
outer:
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
syncOnSaveSema.acquire();
|
||||||
|
} catch (InterruptedException ingored) {
|
||||||
|
break outer;
|
||||||
|
}
|
||||||
|
pushTaskOnSave(model, setValues);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.err.println("Sync on save failed"); //$NON-NLS-1$
|
||||||
|
return;
|
||||||
|
} finally {
|
||||||
|
syncOnSaveSema.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
}
|
||||||
|
});//*/
|
||||||
|
|
||||||
|
metadataDao.addListener(new ModelUpdateListener<Metadata>() {
|
||||||
|
public void onModelUpdated(final Metadata model) {
|
||||||
|
if (!syncOnSaveEnabled())
|
||||||
|
return;
|
||||||
|
if (!model.getValue(Metadata.KEY).equals(GtasksMetadata.METADATA_KEY)) //Don't care about non-gtasks metadata
|
||||||
|
return;
|
||||||
|
if (gtasksPreferenceService.isOngoing()) //Don't try and sync changes that occur during a normal sync
|
||||||
|
return;
|
||||||
|
final ContentValues setValues = model.getSetValues();
|
||||||
|
if (setValues == null || !checkForToken())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (checkValuesForProperties(setValues, METADATA_IGNORE_PROPERTIES)) // don't sync the move cases we don't handle
|
||||||
|
return;
|
||||||
|
if (!checkValuesForProperties(setValues, METADATA_PROPERTIES))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (Flags.checkAndClear(Flags.GTASKS_SUPPRESS_SYNC))
|
||||||
|
return;
|
||||||
|
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// sleep so metadata associated with task is saved
|
||||||
|
AndroidUtilities.sleepDeep(1000L);
|
||||||
|
outer:
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
syncOnSaveSema.acquire();
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
break outer;
|
||||||
|
}
|
||||||
|
pushMetadataOnSave(model);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.err.println("Sync on save failed"); //$NON-NLS-1$
|
||||||
|
return;
|
||||||
|
} finally {
|
||||||
|
syncOnSaveSema.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Property<?>[] TASK_PROPERTIES =
|
||||||
|
{ Task.TITLE,
|
||||||
|
Task.NOTES,
|
||||||
|
Task.DUE_DATE,
|
||||||
|
Task.COMPLETION_DATE,
|
||||||
|
Task.DELETION_DATE };
|
||||||
|
|
||||||
|
private static final Property<?>[] METADATA_PROPERTIES =
|
||||||
|
{ GtasksMetadata.INDENT,
|
||||||
|
GtasksMetadata.PARENT_TASK };
|
||||||
|
|
||||||
|
private static final Property<?>[] METADATA_IGNORE_PROPERTIES =
|
||||||
|
{ GtasksMetadata.ORDER,
|
||||||
|
GtasksMetadata.LIST_ID };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks to see if any of the values changed are among the properties we sync
|
||||||
|
* @param values
|
||||||
|
* @param properties
|
||||||
|
* @return false if none of the properties we sync were changed, true otherwise
|
||||||
|
*/
|
||||||
|
private boolean checkValuesForProperties(ContentValues values, Property<?>[] properties) {
|
||||||
|
for (Property<?> property : properties) {
|
||||||
|
if (values.containsKey(property.name))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronize with server when data changes
|
||||||
|
*/
|
||||||
|
private void pushTaskOnSave(Task task, ContentValues values) throws IOException {
|
||||||
|
Metadata gtasksMetadata = gtasksMetadataService.getTaskMetadata(task.getId());
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteModel = null;
|
||||||
|
boolean newlyCreated = false;
|
||||||
|
|
||||||
|
//Initialize the gtasks api service
|
||||||
|
String token = gtasksPreferenceService.getToken();
|
||||||
|
token = GtasksTokenValidator.validateAuthToken(token);
|
||||||
|
if (token == null) {
|
||||||
|
throw new GoogleTasksException("Failed to establish connection for sync on save"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
gtasksPreferenceService.setToken(token);
|
||||||
|
GtasksService gtasksService = new GtasksService(token);
|
||||||
|
|
||||||
|
String remoteId = null;
|
||||||
|
String listId = Preferences.getStringValue(GtasksPreferenceService.PREF_DEFAULT_LIST);
|
||||||
|
if (listId == null) {
|
||||||
|
listId = gtasksService.getGtaskList("@default").id; //$NON-NLS-1$
|
||||||
|
Preferences.setString(GtasksPreferenceService.PREF_DEFAULT_LIST, listId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gtasksMetadata == null || !gtasksMetadata.containsNonNullValue(GtasksMetadata.ID) ||
|
||||||
|
TextUtils.isEmpty(gtasksMetadata.getValue(GtasksMetadata.ID))) { //Create case
|
||||||
|
if (gtasksMetadata == null) {
|
||||||
|
gtasksMetadata = GtasksMetadata.createEmptyMetadata(task.getId());
|
||||||
|
}
|
||||||
|
if (gtasksMetadata.containsNonNullValue(GtasksMetadata.LIST_ID)) {
|
||||||
|
listId = gtasksMetadata.getValue(GtasksMetadata.LIST_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteModel = new com.google.api.services.tasks.v1.model.Task();
|
||||||
|
newlyCreated = true;
|
||||||
|
} else { //update case
|
||||||
|
remoteId = gtasksMetadata.getValue(GtasksMetadata.ID);
|
||||||
|
listId = gtasksMetadata.getValue(GtasksMetadata.LIST_ID);
|
||||||
|
remoteModel = gtasksService.getGtask(listId, remoteId);
|
||||||
|
}
|
||||||
|
|
||||||
|
//If task was newly created but without a title, don't sync--we're in the middle of
|
||||||
|
//creating a task which may end up being cancelled
|
||||||
|
if (newlyCreated &&
|
||||||
|
(!values.containsKey(Task.TITLE.name) || TextUtils.isEmpty(task.getValue(Task.TITLE)))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Update the remote model's changed properties
|
||||||
|
if (values.containsKey(Task.DELETION_DATE.name) && task.isDeleted()) {
|
||||||
|
remoteModel.deleted = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (values.containsKey(Task.TITLE.name)) {
|
||||||
|
remoteModel.title = task.getValue(Task.TITLE);
|
||||||
|
}
|
||||||
|
if (values.containsKey(Task.NOTES.name)) {
|
||||||
|
remoteModel.notes = task.getValue(Task.NOTES);
|
||||||
|
}
|
||||||
|
if (values.containsKey(Task.DUE_DATE.name)) {
|
||||||
|
remoteModel.due = GtasksApiUtilities.unixTimeToGtasksTime(task.getValue(Task.DUE_DATE));
|
||||||
|
}
|
||||||
|
if (values.containsKey(Task.COMPLETION_DATE.name)) {
|
||||||
|
if (task.isCompleted()) {
|
||||||
|
remoteModel.completed = GtasksApiUtilities.unixTimeToGtasksTime(task.getValue(Task.COMPLETION_DATE));
|
||||||
|
remoteModel.status = "completed"; //$NON-NLS-1$
|
||||||
|
} else {
|
||||||
|
remoteModel.completed = null;
|
||||||
|
remoteModel.status = "needsAction"; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!newlyCreated) {
|
||||||
|
gtasksService.updateGtask(listId, remoteModel);
|
||||||
|
} else {
|
||||||
|
String parent = gtasksMetadataService.getRemoteParentId(gtasksMetadata);
|
||||||
|
String priorSibling = gtasksMetadataService.getRemoteSiblingId(listId, gtasksMetadata);
|
||||||
|
|
||||||
|
try { //Make sure the parent task exists on the target list
|
||||||
|
if (parent != null) {
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteParent = gtasksService.getGtask(listId, parent);
|
||||||
|
if (remoteParent == null || (remoteParent.deleted != null && remoteParent.deleted))
|
||||||
|
parent = null;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
parent = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (priorSibling != null) {
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteSibling = gtasksService.getGtask(listId, priorSibling);
|
||||||
|
if (remoteSibling == null || (remoteSibling.deleted != null && remoteSibling.deleted))
|
||||||
|
priorSibling = null;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
priorSibling = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
com.google.api.services.tasks.v1.model.Task created = gtasksService.createGtask(listId, remoteModel, parent, priorSibling);
|
||||||
|
|
||||||
|
//Update the metadata for the newly created task
|
||||||
|
gtasksMetadata.setValue(GtasksMetadata.ID, created.id);
|
||||||
|
gtasksMetadata.setValue(GtasksMetadata.LIST_ID, listId);
|
||||||
|
metadataService.save(gtasksMetadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
task.setValue(Task.MODIFICATION_DATE, DateUtilities.now());
|
||||||
|
task.setValue(Task.LAST_SYNC, DateUtilities.now());
|
||||||
|
Flags.set(Flags.GTASKS_SUPPRESS_SYNC);
|
||||||
|
taskDao.saveExisting(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void pushMetadataOnSave(Metadata model) throws IOException {
|
||||||
|
//Initialize the gtasks api service
|
||||||
|
String token = gtasksPreferenceService.getToken();
|
||||||
|
token = GtasksTokenValidator.validateAuthToken(token);
|
||||||
|
if (token == null) {
|
||||||
|
throw new GoogleTasksException("Failed to establish connection for sync on save"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
gtasksPreferenceService.setToken(token);
|
||||||
|
GtasksService gtasksService = new GtasksService(token);
|
||||||
|
|
||||||
|
String taskId = model.getValue(GtasksMetadata.ID);
|
||||||
|
String listId = model.getValue(GtasksMetadata.LIST_ID);
|
||||||
|
String parent = gtasksMetadataService.getRemoteParentId(model);
|
||||||
|
String priorSibling = gtasksMetadataService.getRemoteSiblingId(listId, model);
|
||||||
|
|
||||||
|
try { //Make sure the parent task exists on the target list
|
||||||
|
if (parent != null) {
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteParent = gtasksService.getGtask(listId, parent);
|
||||||
|
if (remoteParent == null || (remoteParent.deleted != null && remoteParent.deleted))
|
||||||
|
parent = null;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
parent = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (priorSibling != null) {
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteSibling = gtasksService.getGtask(listId, priorSibling);
|
||||||
|
if (remoteSibling == null || (remoteSibling.deleted != null && remoteSibling.deleted))
|
||||||
|
priorSibling = null;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
priorSibling = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
MoveRequest move = new MoveRequest(gtasksService, taskId, listId, parent, priorSibling);
|
||||||
|
move.executePush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean syncOnSaveEnabled() {
|
||||||
|
return Preferences.getBoolean(gtasksPreferenceService.getSyncOnSaveKey(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkForToken() {
|
||||||
|
if (!gtasksPreferenceService.isLoggedIn())
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,208 @@
|
|||||||
|
package com.todoroo.astrid.gtasks;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
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.v1.model.Tasks;
|
||||||
|
import com.timsu.astrid.R;
|
||||||
|
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.GtasksService;
|
||||||
|
import com.todoroo.astrid.gtasks.auth.GtasksTokenValidator;
|
||||||
|
import com.todoroo.astrid.gtasks.sync.GtasksSyncOnSaveService;
|
||||||
|
import com.todoroo.astrid.service.TaskService;
|
||||||
|
import com.todoroo.astrid.test.DatabaseTestCase;
|
||||||
|
|
||||||
|
public class GtasksSyncOnSaveTest extends DatabaseTestCase {
|
||||||
|
|
||||||
|
@Autowired TaskService taskService;
|
||||||
|
@Autowired GtasksSyncOnSaveService gtasksSyncOnSaveService;
|
||||||
|
@Autowired GtasksMetadataService gtasksMetadataService;
|
||||||
|
@Autowired GtasksPreferenceService gtasksPreferenceService;
|
||||||
|
|
||||||
|
private GtasksService gtasksService;
|
||||||
|
private boolean initialized = 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 {
|
||||||
|
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.v1.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
||||||
|
assertEquals(title, remoteTask.title);
|
||||||
|
return localTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task setupLocalTaskModel(String title) {
|
||||||
|
Task localTask = new Task();
|
||||||
|
localTask.setValue(Task.TITLE, title);
|
||||||
|
return localTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
private com.google.api.services.tasks.v1.model.Task getRemoteTaskForLocalId(long localId) throws IOException {
|
||||||
|
Metadata gtasksMetadata = gtasksMetadataService.getTaskMetadata(localId);
|
||||||
|
assertNotNull(gtasksMetadata);
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteTask = gtasksService.getGtask(DEFAULT_LIST, gtasksMetadata.getValue(GtasksMetadata.ID));
|
||||||
|
assertNotNull(remoteTask);
|
||||||
|
return remoteTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSyncOnTitleUpdate() throws IOException {
|
||||||
|
Task localTask = performBasicCreation("-title will change");
|
||||||
|
|
||||||
|
String newTitle = "Title has changed!";
|
||||||
|
localTask.setValue(Task.TITLE, newTitle);
|
||||||
|
taskService.save(localTask);
|
||||||
|
|
||||||
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
||||||
|
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
||||||
|
assertEquals(newTitle, remoteTask.title);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSyncOnDueDateUpdate() throws IOException {
|
||||||
|
Task localTask = performBasicCreation("-due date will change");
|
||||||
|
|
||||||
|
long dueDate = new Date(115, 2, 14).getTime();
|
||||||
|
localTask.setValue(Task.DUE_DATE, dueDate);
|
||||||
|
taskService.save(localTask);
|
||||||
|
|
||||||
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
||||||
|
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
||||||
|
assertEquals(dueDate, GtasksApiUtilities.gtasksDueTimeToUnixTime(remoteTask.due, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSyncOnNotesUpdate() throws IOException {
|
||||||
|
Task localTask = performBasicCreation("-notes will change");
|
||||||
|
|
||||||
|
String notes = "Noted!";
|
||||||
|
localTask.setValue(Task.NOTES, notes);
|
||||||
|
taskService.save(localTask);
|
||||||
|
|
||||||
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
||||||
|
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
||||||
|
assertEquals(notes, remoteTask.notes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSyncOnCompleted() throws IOException {
|
||||||
|
Task localTask = performBasicCreation("-will be completed");
|
||||||
|
|
||||||
|
long completionDate = DateUtilities.now();
|
||||||
|
localTask.setValue(Task.COMPLETION_DATE, completionDate);
|
||||||
|
taskService.save(localTask);
|
||||||
|
|
||||||
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
||||||
|
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
||||||
|
assertEquals("completed", remoteTask.status);
|
||||||
|
assertEquals(completionDate, GtasksApiUtilities.gtasksCompletedTimeToUnixTime(remoteTask.completed, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSyncOnDeleted() throws IOException {
|
||||||
|
Task localTask = performBasicCreation("-will be deleted");
|
||||||
|
|
||||||
|
long deletionDate = DateUtilities.now();
|
||||||
|
localTask.setValue(Task.DELETION_DATE, deletionDate);
|
||||||
|
taskService.save(localTask);
|
||||||
|
|
||||||
|
AndroidUtilities.sleepDeep(TIME_TO_WAIT);
|
||||||
|
|
||||||
|
com.google.api.services.tasks.v1.model.Task remoteTask = getRemoteTaskForLocalId(localTask.getId());
|
||||||
|
assertTrue(remoteTask.deleted);
|
||||||
|
assertFalse(taskWithTitleExists(localTask.getValue(Task.TITLE)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean taskWithTitleExists(String title) throws IOException {
|
||||||
|
Tasks allTasks = gtasksService.getAllGtasksFromListId(DEFAULT_LIST, false);
|
||||||
|
if (allTasks.items != null) {
|
||||||
|
for (com.google.api.services.tasks.v1.model.Task t : allTasks.items) {
|
||||||
|
if (t.title.equals(title))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
|
||||||
|
if (!initialized) {
|
||||||
|
initializeTestService();
|
||||||
|
gtasksSyncOnSaveService.initialize();
|
||||||
|
initialized = true;
|
||||||
|
Preferences.setBoolean(R.string.gtasks_GPr_sync_on_save_key, 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) {
|
||||||
|
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(authToken);
|
||||||
|
gtasksPreferenceService.setToken(authToken);
|
||||||
|
|
||||||
|
gtasksService = new GtasksService(authToken);
|
||||||
|
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupTestList() throws Exception {
|
||||||
|
Tasks defaultListTasks = gtasksService.getAllGtasksFromListId(DEFAULT_LIST, false);
|
||||||
|
if (defaultListTasks.items != null) {
|
||||||
|
for (com.google.api.services.tasks.v1.model.Task t : defaultListTasks.items) {
|
||||||
|
gtasksService.deleteGtask(DEFAULT_LIST, t.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue