private void GGSyncCornerCaseTest() { GGSync syncTest = new GGSync("*****@*****.**", "cs2103trocks"); GGList testList = new GGList(); PrintSperateLine(); Console.WriteLine("\nPlease delete GG to-do calendar."); Console.ReadKey(); Console.WriteLine("\nTest: Sync with empty GGList"); Console.ReadKey(); PreTestPrint(testList); syncTest.SyncWithGoogleCalendar(testList); PostTestPrint(testList); Console.WriteLine("\nPlease check: there should be no event on both sides"); PrintSperateLine(); Console.WriteLine("\nTest: Add to empty calendar"); Console.ReadKey(); for (int i = 0; i < 3; i++) { testList.AddGGItem(new GGItem("corner test description " + i, DateTime.Now.AddDays(-i).AddHours(i), "corner test tag " + i)); } PreTestPrint(testList); syncTest.SyncWithGoogleCalendar(testList); PostTestPrint(testList); Console.WriteLine("\nPlease check: there should be 3 events on Google Calendar"); PrintSperateLine(); Console.WriteLine("\nTest: Add to non-empty calendar"); Console.ReadKey(); for (int i = 3; i < 5; i++) { GGItem testItem = new GGItem("corner test description " + i, DateTime.Now.AddDays(-i).AddHours(i), "corner test tag " + i); testList.AddGGItem(testItem); Console.WriteLine("\nAdd to local GGList: " + testItem.ToString()); } PreTestPrint(testList); syncTest.SyncWithGoogleCalendar(testList); PostTestPrint(testList); Console.WriteLine("\nPlease check: there should be 5 events on Google Calendar"); PrintSperateLine(); Console.WriteLine("\nTest: Sync after deleting all events in local GGList"); Console.ReadKey(); testList.ClearGGList(); PreTestPrint(testList); syncTest.SyncWithGoogleCalendar(testList); PostTestPrint(testList); Console.WriteLine("\nPlease check: there should be no event on Google Calendar"); Console.ReadKey(); PrintSperateLine(); Console.WriteLine("\nTest: Sync after deleting all events on Google Calendar"); for (int i = 0; i < 3; i++) { testList.AddGGItem(new GGItem("corner test description " + i, DateTime.Now.AddDays(-i).AddHours(i), "corner test tag " + i)); } syncTest.SyncWithGoogleCalendar(testList); Console.WriteLine("\nPlease delete all 3 events on Google Calendar"); Console.ReadKey(); PreTestPrint(testList); syncTest.SyncWithGoogleCalendar(testList); PostTestPrint(testList); Console.WriteLine("Please check: there should be no events in local GGList"); Console.ReadKey(); PrintSperateLine(); Console.WriteLine("\nTest: Delete the same event both sides"); testList.AddGGItem(new GGItem("corner test description 0", DateTime.Now, "corner test tag 0")); syncTest.SyncWithGoogleCalendar(testList); Console.WriteLine("Please delete the only event on Google Calendar"); Console.ReadKey(); testList.ClearGGList(); PreTestPrint(testList); syncTest.SyncWithGoogleCalendar(testList); PostTestPrint(testList); Console.WriteLine("Please check: there should be no event on both sides"); Console.ReadKey(); }
private void GGSyncUnitTest() { GGSync mySync = new GGSync("*****@*****.**", "cs2103trocks"); PrintSperateLine(); Console.WriteLine("\nPlease delete GG to-do calendar\n"); Console.ReadKey(); GGList myList = new GGList(); for (int i = 0; i < 3; i++) { myList.AddGGItem(new GGItem("desciption_test" + i, DateTime.Now.AddDays(i + 1), "tag_test" + i)); } PrintSperateLine(); Console.WriteLine("\nTest: add to empty google calender\n"); PreTestPrint(myList); mySync.SyncWithGoogleCalendar(myList); PostTestPrint(myList); Console.WriteLine("\nPlease check if Google Calendar has 3 events."); PrintSperateLine(); Console.WriteLine("\nTest: local has the latest version\n"); Console.ReadKey(); myList.GetGGItemAt(0).SetDescription(myList.GetGGItemAt(0).GetDescription() + " local updated"); Console.WriteLine("\nupdate local: " + myList.GetGGItemAt(0).ToString()); GGItem newGGItem = new GGItem("description_test_new", DateTime.Now.AddDays(5).AddHours(3), "tag_test3"); myList.AddGGItem(newGGItem); Console.WriteLine("\nadd local: " + newGGItem.ToString()); PreTestPrint(myList); mySync.SyncWithGoogleCalendar(myList); PostTestPrint(myList); Console.WriteLine("\nPlease check Google Calendar: event0->description: 'local updated' appended"); Console.WriteLine("\nPlease check Google Calendar: new event added: tag_test3"); PrintSperateLine(); Console.WriteLine("\nTest: server has the latest version\n"); Console.WriteLine("\nPlease modified one task, add one task and delete one task on calendar"); Console.ReadKey(); PreTestPrint(myList); mySync.SyncWithGoogleCalendar(myList); PostTestPrint(myList); Console.WriteLine("\nPlease check: there should be 4 items after sync"); PrintSperateLine(); Console.WriteLine("\nTest: both have some latest events\n"); myList.GetGGItemAt(2).SetTag(myList.GetGGItemAt(2).GetTag() + " local update"); Console.WriteLine("\nupdate local: " + myList.GetGGItemAt(2).ToString()); Console.WriteLine("\nPlease update on server"); Console.ReadKey(); PreTestPrint(myList); mySync.SyncWithGoogleCalendar(myList); PostTestPrint(myList); Console.WriteLine("\nPlease check Google Calendar: event_2->tag: 'local update' appended"); Console.WriteLine("\nPlease check: there should be 4 items after sync"); PrintSperateLine(); Console.WriteLine("\nTest: delete from server"); Console.WriteLine("\nRemove at local list: " + myList.GetGGItemAt(0)); myList.GetDeletedList().Add(myList.GetGGItemAt(0)); myList.GetInnerList().Remove(myList.GetGGItemAt(0)); Console.ReadKey(); PreTestPrint(myList); mySync.SyncWithGoogleCalendar(myList); PostTestPrint(myList); Console.WriteLine("\nPlease check Google Calendar: there should be 3 events"); Console.ReadKey(); }
/// <summary> /// Sync local GGItem to GG calendar /// </summary> /// <param name="addToLocal">List of GGItems to be added to local GGList</param> /// <param name="removeFromLocal">List of GGItems to be removed from local GGList</param> /// <param name="GGService">Google calendar service object</param> /// <param name="GGCalendar">GG calendar</param> /// <param name="GGEvents">Google event query results</param> /// <param name="server">List of bools to indicate if a Google event has a local version</param> /// <param name="ggItem">The local GGItem to be synced</param> private void SyncFromLocalToServer(List<GGItem> addToLocal, List<GGItem> removeFromLocal, CalendarService GGService, CalendarEntry GGCalendar, EventFeed GGEvents, List<bool> server, GGItem ggItem) { if (ggItem.GetEventAbsoluteUrl() == String.Empty) { // Not synced : add to GG Calendar Log("Never synced"); ggItem.SetEventAbsoluteUrl(AddGGEvent(GGService, GGCalendar, ggItem)); Log("Add to server: " + ggItem.ToString()); } else { // Synced before Log("Synced before"); string id = ggItem.GetEventAbsoluteUrl(); // Find the coresponding Google event AtomEntry theEvent = FindGoogleEvent(GGEvents, server, id); if (theEvent == null) { // Not found: deleted on GG calendar : remove from local list Log("Event is deleted on server"); removeFromLocal.Add(ggItem); Log("Removed in local list"); } else { // Found SolveConflict(addToLocal, removeFromLocal, GGService, GGCalendar, ggItem, theEvent); } } }
/// <summary> /// Sync GG calendar events to local list /// </summary> /// <param name="addToLocal">List of GGItems to be added to local GGList</param> /// <param name="GGEvents">Google event query results</param> /// <param name="server">List of bools to indicate if a Google event has a local version</param> private void SyncFromServerToLocal(List<GGItem> addToLocal, EventFeed GGEvents, List<bool> server) { // Loop through Google events for (int i = 0; i < GGEvents.Entries.Count; i++) { if (!server[i]) { // Google event does not has a local version, create on GG calendar : add to local list EventEntry e = (EventEntry)GGEvents.Entries[i]; GGItem newGGItem = new GGItem(e.Title.Text, e.Times[0].EndTime, ExtractTagFromContents(e.Content.Content), DateTime.Parse(e.Updated.ToLongTimeString()), e.Id.AbsoluteUri, ExtractPathFromContents(e.Content.Content)); addToLocal.Add(newGGItem); Log("Add to local: " + newGGItem.ToString()); } } }
/// <summary> /// Solve conflict by comparing last modified time /// </summary> /// <param name="addToLocal">List of GGItems to be added to local GGList</param> /// <param name="removeFromLocal">List of GGItems to be removed from local GGList</param> /// <param name="GGService">Google calendar service object</param> /// <param name="GGCalendar">GG calendar</param> /// <param name="ggItem">The local GGItem to be compared</param> /// <param name="theEvent">The Google event to be compared</param> private void SolveConflict(List<GGItem> addToLocal, List<GGItem> removeFromLocal, CalendarService GGService, CalendarEntry GGCalendar, GGItem ggItem, AtomEntry theEvent) { if (theEvent.Updated.CompareTo(ggItem.GetLastModifiedTime()) < 0) { // Local is the latest version : delete on server, then add the latest one Log("Local is the latest"); theEvent.Delete(); Log("Delete on server"); ggItem.SetEventAbsoluteUrl(AddGGEvent(GGService, GGCalendar, ggItem)); Log("Add to server: " + ggItem.ToString()); } else { // Server is the latest version : delete on local, then add the latest one Log("Server is the latest"); EventEntry e = (EventEntry)theEvent; GGItem newGGItem = new GGItem(e.Title.Text, e.Times[0].EndTime, ExtractTagFromContents(e.Content.Content), DateTime.Parse(e.Updated.ToLongTimeString()), e.Id.AbsoluteUri, ExtractPathFromContents(e.Content.Content)); removeFromLocal.Add(ggItem); addToLocal.Add(newGGItem); Log("Update to lsocal: " + newGGItem.ToString()); } }