/// <summary> /// Test case: Add and synchronize a dataset, then wipe data. Wiping data (while /// using unauthorized credentials) should wipe the dataset and the identity, /// so that the information in the dataset should no longer be retrievable. /// Check that the dataset is no longer in local memory and that syncing a /// dataset with the same record key does not cause a conflict. /// </summary> //[TestMethod] //[TestCategory("SyncManager")] public void WipeDataTest() { string failureMessage = string.Empty; CognitoSyncManager syncManager = new CognitoSyncManager(UnAuthCredentials); syncManager.WipeData(false); Dataset d = syncManager.OpenOrCreateDataset("testDataset5"); d.Put("testKey", "testValue"); d.OnSyncConflict += delegate(Dataset dataset, List <SyncConflict> conflicts) { failureMessage += "Expecting SyncSuccess instead of SyncConflict\n"; return(false); }; d.OnSyncFailure += delegate(object sender, SyncFailureEventArgs e) { failureMessage += "Expecting SyncSuccess instead of SyncFailure\n"; }; d.OnSyncSuccess += delegate(object sender, SyncSuccessEventArgs e) { syncManager.WipeData(); Dataset d2 = syncManager.OpenOrCreateDataset("testDataset5"); if (d2.Records.Count != 0) { failureMessage += "Expecting dataset to be empty due to local data wipe.\n"; } d2.Put("testKey", "newTestValue"); d2.OnSyncConflict += delegate(Dataset dataset, List <SyncConflict> conflicts) { failureMessage += "Expecting SyncSuccess instead of SyncConflict\n"; return(false); }; d2.OnSyncFailure += delegate(object sender2, SyncFailureEventArgs e2) { failureMessage += "Expecting SyncSuccess instead of SyncFailure\n"; }; d2.OnSyncSuccess += delegate(object sender2, SyncSuccessEventArgs e2) { if (d2.Get("testKey") != "newTestValue") { failureMessage += "Value for key 'testKey' should be 'newTestValue'\n"; } }; #if BCL35 d2.Synchronize(); #else RunAsSync(async() => await d2.SynchronizeAsync()); #endif }; #if BCL35 d.Synchronize(); #else RunAsSync(async() => await d.SynchronizeAsync()); #endif if (!string.IsNullOrEmpty(failureMessage)) { Assert.Fail(failureMessage); } }
/// <summary> /// Test case: Produce a conflict and check that SyncConflict is triggered. /// Also check that by returning false in SyncConflict, the Synchronize operation /// is aborted and nothing else gets called. /// </summary> //[TestMethod] //[TestCategory("SyncManager")] public void ConflictTest() { string failureMessage = string.Empty; using (CognitoSyncManager syncManager = new CognitoSyncManager(UnAuthCredentials)) { syncManager.WipeData(false); using (Dataset d = syncManager.OpenOrCreateDataset("testDataset3")) { d.Put("testKey3", "the initial value"); d.OnSyncSuccess += delegate(object sender, SyncSuccessEventArgs e) { d.ClearAllDelegates(); syncManager.WipeData(false); using (Dataset d2 = syncManager.OpenOrCreateDataset("testDataset3")) { bool conflictTriggered = false; d2.Put("testKey3", "a different value"); d2.OnSyncConflict += delegate(Dataset dataset, List <SyncConflict> conflicts) { conflictTriggered = true; return(false); }; d2.OnSyncSuccess += delegate(object sender4, SyncSuccessEventArgs e4) { failureMessage += "Expecting OnSyncConflict instead of OnSyncSuccess\n"; }; d2.OnSyncFailure += delegate(object sender4, SyncFailureEventArgs e4) { if (!conflictTriggered) { failureMessage += "Expecting OnSyncConflict instead of OnSyncFailure\n"; } }; #if BCL35 d2.Synchronize(); #else RunAsSync(async() => await d2.SynchronizeAsync()); #endif } }; d.OnSyncFailure += delegate(object sender, SyncFailureEventArgs e) { failureMessage += "Expecting OnSyncSuccess instead of OnSyncFailure\n"; }; #if BCL35 d.Synchronize(); #else RunAsSync(async() => await d.SynchronizeAsync()); #endif } } if (!string.IsNullOrEmpty(failureMessage)) { Assert.Fail(failureMessage); } }
void OnGUI() { float ratio = Screen.width / 600.0f; GUI.skin.label.fontSize = (int)(15 * ratio); GUI.skin.button.fontSize = (int)(15 * ratio); if (syncManager == null) { GUILayout.Space(20); GUILayout.Label("Please setup the Cognito credentials"); return; } if (GetComponent <CharacterList> ().enabled) { if (GUI.Button(new Rect(30 * ratio, 30 * ratio, 120 * ratio, 30 * ratio), "Save")) { SaveToDataset(); } else if (GUI.Button(new Rect(30 * ratio, 70 * ratio, 120 * ratio, 30 * ratio), "Load")) { LoadFromDataset(); } else if (GUI.Button(new Rect(Screen.width - 150 * ratio, 30 * ratio, 120 * ratio, 30 * ratio), "Logout")) { if (credentials.IdentityProvider.Logins.Count > 0) //Auth identity { if (FB.IsLoggedIn) { FB.Logout(); } credentials.ClearCredentialsAndIdentity(); syncManager.WipeData(); } Application.LoadLevel(Application.loadedLevel); } if (credentials.IdentityProvider.Logins.Count == 0) //Unauth { if (GUI.Button(new Rect(Screen.width - 150 * ratio, 70 * ratio, 120 * ratio, 30 * ratio), "Link with FB")) { GetComponent <CharacterList> ().enabled = false; FB.Login("email", FacebookLoginCallback); } } } else { GUI.Label(new Rect(30 * ratio, 30 * ratio, 120 * ratio, 30 * ratio), "Please wait..."); } GUI.Label(new Rect(20 * ratio, Screen.height - 50 * ratio, 600 * ratio, 30 * ratio), "Identity: " + credentials.IdentityProvider.GetCurrentIdentityId()); }
//[TestMethod] //[TestCategory("SyncManager")] public void RemoveEntryTest() { string failureMessage = string.Empty; using (var syncManager = new CognitoSyncManager(UnAuthCredentials)) { syncManager.WipeData(false); Dataset d = syncManager.OpenOrCreateDataset("testRemoveDataset"); long originalCount = d.Metadata.RecordCount; d.Put("testKey", "testValue"); d.OnSyncConflict += delegate(Dataset dataset, List <SyncConflict> conflicts) { failureMessage += "Expecting SyncSuccess instead of SyncConflict\n"; return(false); }; d.OnSyncFailure += delegate(object sender, SyncFailureEventArgs e) { failureMessage += "Expecting SyncSuccess instead of SyncFailure\n"; }; #if BCL35 d.Synchronize(); #else RunAsSync(async() => await d.SynchronizeAsync()); #endif if (!string.IsNullOrEmpty(failureMessage)) { Assert.Fail(failureMessage); } d.Remove("testKey"); #if BCL35 d.Synchronize(); #else RunAsSync(async() => await d.SynchronizeAsync()); #endif if (d.Metadata.RecordCount != originalCount) { failureMessage += "d.Metadata.RecordCount != originalCount\n"; } d.Dispose(); if (!string.IsNullOrEmpty(failureMessage)) { Assert.Fail(failureMessage); } } }
//[TestMethod] //[TestCategory("SyncManager")] public void DatasetLocalStorageTest() { { using (CognitoSyncManager syncManager = new CognitoSyncManager(UnAuthCredentials)) { syncManager.WipeData(false); Dataset d = syncManager.OpenOrCreateDataset("testDataset"); d.Put("testKey", "testValue"); } } { using (CognitoSyncManager syncManager = new CognitoSyncManager(UnAuthCredentials)) { Dataset d = syncManager.OpenOrCreateDataset("testDataset"); Assert.AreEqual("testValue", d.Get("testKey")); } } }
void OnGUI() { GUI.color = Color.gray; GUILayout.BeginArea(new Rect(Screen.width * 0.2f, 0, Screen.width - Screen.width * 0.2f, Screen.height)); GUILayout.Space(20); GUILayout.Label(statusMessage); if (syncManager == null) { GUILayout.EndArea(); return; } GUI.color = Color.white; #if USE_FACEBOOK_LOGIN GUI.enabled = true; #else GUI.enabled = false; #endif if (GUILayout.Button("Connect to Facebook", GUILayout.MinHeight(20), GUILayout.Width(Screen.width * 0.6f))) { #if USE_FACEBOOK_LOGIN statusMessage = "Connecting to Facebook"; disableButton = true; if (!FB.IsInitialized) { FB.Init(delegate() { Debug.Log("starting thread"); // shows to connect the current identityid or create a new identityid with facebook authentication FB.Login("email", FacebookLoginCallback); }); } else { FB.Login("email", FacebookLoginCallback); } #endif } if (disableButton) { GUI.enabled = false; } else { GUI.enabled = true; } GUILayout.Label("Enter PlayerInfo"); playerName = GUILayout.TextField(playerName, GUILayout.MinHeight(20), GUILayout.Width(Screen.width * 0.6f)); alias = GUILayout.TextField(alias, GUILayout.MinHeight(20), GUILayout.Width(Screen.width * 0.6f)); GUILayout.Space(20); GUILayout.Label("Select Difficulty Level"); selectedDifficultyLevel = GUILayout.SelectionGrid(selectedDifficultyLevel, difficultyLevels, 3, GUILayout.MinHeight(20), GUILayout.Width(Screen.width * 0.6f)); GUILayout.Space(20); if (GUILayout.Button("Save offline using SyncManager", GUILayout.MinHeight(20), GUILayout.Width(Screen.width * 0.6f))) { statusMessage = "Saving offline.."; disableButton = true; // syncManager creates or fetches the Dataset locally using SQLiteStorage // OpenOrCreateDataset(datasetName) takes the datasetName which may be like "UserSettings", "UserState" playerInfo.Put("playerName", playerName); playerInfo.Put("alias", alias); playerSettings.Put("selectedDifficultyLevel", selectedDifficultyLevel.ToString()); alias = string.IsNullOrEmpty(playerInfo.Get("alias")) ? "Enter ur alias" : playerInfo.Get("alias"); playerName = string.IsNullOrEmpty(playerInfo.Get("playerName")) ? "Enter ur fullName" : playerInfo.Get("playerName"); selectedDifficultyLevel = string.IsNullOrEmpty(playerSettings.Get("selectedDifficultyLevel")) ? 0 : int.Parse(playerSettings.Get("selectedDifficultyLevel")); statusMessage = "Saved offline !"; disableButton = false; } else if (GUILayout.Button("Sync with CognitoSync Cloud", GUILayout.MinHeight(20), GUILayout.Width(Screen.width * 0.6f))) { statusMessage = "Saving to CognitoSync Cloud.."; disableButton = true; playerInfo.Put("alias", alias); playerInfo.Synchronize(); playerInfo.Put("playerName", playerName); playerSettings.Put("difficultyLevel", selectedDifficultyLevel.ToString()); playerSettings.Synchronize(); } else if (GUILayout.Button("Refresh and Sync to CognitoSync Cloud", GUILayout.MinHeight(20), GUILayout.Width(Screen.width * 0.6f))) { statusMessage = "Refreshing metadata for current player.."; disableButton = true; // RefreshDatasetMetadataAsync fetches all datasets for the current IdentityID // Remote DatasetMetadata is fetched and stored in SQLiteStorage // Records are not fetched until ds.Synchronize() is called on the respective datasets syncManager.RefreshDatasetMetadataAsync(RefreshDatasetMetadataCallback, null); } else if (GUILayout.Button("Delete all local player data", GUILayout.MinHeight(20), GUILayout.Width(Screen.width * 0.6f))) { statusMessage = "Deleting all local data.."; syncManager.WipeData(); playerName = "Enter ur full name"; alias = "Enter ur alias"; selectedDifficultyLevel = 0; statusMessage = "Deleting all local data complete. "; } GUILayout.EndArea(); }
/// <summary> /// Test case: Produce a conflict and check that the three ways provided by the SDK /// for resolving a conflict (local wins, remote wins, and override) work. We also check /// that returning true in SyncConflict allows the Synchronization operationn to continue. /// </summary> //[TestMethod] //[TestCategory("SyncManager")] public void ResolveConflictTest() { string failureMessage = string.Empty; using (CognitoSyncManager syncManager = new CognitoSyncManager(UnAuthCredentials)) { syncManager.WipeData(false); using (Dataset d = syncManager.OpenOrCreateDataset("testDataset4")) { d.Put("a", "1"); d.Put("b", "2"); d.Put("c", "3"); d.OnSyncSuccess += delegate(object sender, SyncSuccessEventArgs e) { d.ClearAllDelegates(); syncManager.WipeData(false); using (Dataset d2 = syncManager.OpenOrCreateDataset("testDataset4")) { d2.Put("a", "10"); d2.Put("b", "20"); d2.Put("c", "30"); bool resolved = false; d2.OnSyncConflict += delegate(Dataset dataset, List <SyncConflict> conflicts) { List <Amazon.CognitoSync.SyncManager.Record> resolvedRecords = new List <Amazon.CognitoSync.SyncManager.Record>(); int i = 0; foreach (SyncConflict conflictRecord in conflicts) { if (i == 0) { resolvedRecords.Add(conflictRecord.ResolveWithLocalRecord()); } else if (i == 1) { resolvedRecords.Add(conflictRecord.ResolveWithValue("42")); } else { resolvedRecords.Add(conflictRecord.ResolveWithRemoteRecord()); } i++; } dataset.Resolve(resolvedRecords); resolved = true; return(true); }; d2.OnSyncSuccess += delegate(object sender4, SyncSuccessEventArgs e4) { if (resolved) { if (d2.Get("a") != "10") { failureMessage += "Value for key 'a' should be '10'\n"; } if (d2.Get("b") != "42") { failureMessage += "Value for key 'b' should be '42'\n"; } if (d2.Get("c") != "3") { failureMessage += "Value for key 'c' should be '3'\n"; } } else { failureMessage += "Expecting SyncConflict instead of SyncSuccess\n"; } }; d2.OnSyncFailure += delegate(object sender4, SyncFailureEventArgs e4) { failureMessage += "Expecting SyncConflict instead of SyncFailure\n"; }; #if BCL35 d2.Synchronize(); #else RunAsSync(async() => await d2.SynchronizeAsync()); #endif } }; #if BCL35 d.Synchronize(); #else RunAsSync(async() => await d.SynchronizeAsync()); #endif } } if (!string.IsNullOrEmpty(failureMessage)) { Assert.Fail(failureMessage); } }
/// <summary> /// Test case: Check that the dataset metadata is modified appropriately when calling Synchronize. /// We test for the dirty bit, the sync count and the last modified timmestamp. /// </summary> //[TestMethod] //[TestCategory("SyncManager")] public void MetadataTest() { string failureMessage = string.Empty; using (CognitoSyncManager syncManager = new CognitoSyncManager(UnAuthCredentials)) { syncManager.WipeData(false); using (Dataset d = syncManager.OpenOrCreateDataset("testDataset6")) { d.Put("testKey3", "the initial value"); //Initial properties var records = d.Records; Record r = d.Records[records.Count - 1]; long initialSyncCount = r.SyncCount; bool initialDirty = r.IsModified; DateTime initialDate = r.DeviceLastModifiedDate.Value; d.OnSyncSuccess += delegate(object sender, SyncSuccessEventArgs e) { //Properties after Synchronize Record r2 = d.Records[records.Count - 1]; long synchronizedSyncCount = r2.SyncCount; bool synchronizedDirty = r2.IsModified; DateTime synchronizedDate = r2.DeviceLastModifiedDate.Value; d.Put("testKey3", "a new value"); //Properties after changing the content again Record r3 = d.Records[records.Count - 1]; long finalSyncCount = r3.SyncCount; bool finalDirty = r3.IsModified; DateTime finalDate = r3.DeviceLastModifiedDate.Value; if (!initialDirty) { failureMessage += "Expected 'initialDirty' to be true\n"; } if (synchronizedDirty) { failureMessage += "Expected 'synchronizedDirty' to be false\n"; } if (!finalDirty) { failureMessage += "Expected 'finalDirty' to be true\n"; } if (synchronizedSyncCount <= initialSyncCount) { failureMessage += "Expected synchronizedSyncCount > initialSyncCount\n"; } if (synchronizedSyncCount != finalSyncCount) { failureMessage += "Expected synchronizedSyncCount == finalSyncCount\n"; } if (finalDate <= initialDate) { failureMessage += "Expected finalDate > initialDate\n"; } if (initialDate != synchronizedDate) { failureMessage += "Expected initialDate == synchronizedDate\n"; } }; d.OnSyncFailure += (object sender, SyncFailureEventArgs e) => { failureMessage += e.Exception.ToString() + "\n"; }; #if BCL35 d.Synchronize(); #else RunAsSync(async() => await d.SynchronizeAsync()); #endif if (!string.IsNullOrEmpty(failureMessage)) { Assert.Fail(failureMessage); } } } }
/// <summary> /// Test Case: /// </summary> //[TestMethod] //[TestCategory("SyncManager")] public void MergeTest() { var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); string uniqueName = ((DateTime.UtcNow - epoch).TotalSeconds).ToString(); string uniqueName2 = uniqueName + "_"; string failureMessage = string.Empty; UnAuthCredentials.Clear(); using (CognitoSyncManager sm1 = new CognitoSyncManager(AuthCredentials)) { sm1.WipeData(false); Thread.Sleep(2000); using (Dataset d = sm1.OpenOrCreateDataset("test")) { d.Put(uniqueName, uniqueName); d.OnSyncSuccess += delegate(object s1, SyncSuccessEventArgs e1) { UnAuthCredentials.Clear(); using (CognitoSyncManager sm2 = new CognitoSyncManager(UnAuthCredentials)) { Thread.Sleep(2000); using (Dataset d2 = sm2.OpenOrCreateDataset("test")) { d2.Put(uniqueName2, uniqueName2); d2.OnSyncSuccess += delegate(object s2, SyncSuccessEventArgs e2) { AuthCredentials.Clear(); UnAuthCredentials.Clear(); //now we will use auth credentials. using (CognitoSyncManager sm3 = new CognitoSyncManager(AuthCredentials)) { Thread.Sleep(2000); using (Dataset d3 = sm3.OpenOrCreateDataset("test")) { bool mergeTriggered = false; d3.OnSyncSuccess += (object sender, SyncSuccessEventArgs e) => { if (!mergeTriggered) { failureMessage += "Expecting DatasetMerged instead of OnSyncSuccess\n"; } }; d3.OnSyncFailure += (object sender, SyncFailureEventArgs e) => { failureMessage += string.Format("Not expecting OnSyncFailure Got exception {0}\n", e.Exception.Message); }; d3.OnSyncConflict += (Dataset dataset, List <SyncConflict> syncConflicts) => { failureMessage += "Not expecting OnSyncConflict\n"; return(false); }; d3.OnDatasetDeleted += (Dataset dataset) => { failureMessage += "Not expecting OnDatasetDeleted\n"; return(false); }; d3.OnDatasetMerged += (Dataset ds, List <string> datasetNames) => { mergeTriggered = true; return(true); }; #if BCL35 d3.Synchronize(); #else RunAsSync(async() => await d3.SynchronizeAsync()); #endif } } }; d2.OnSyncFailure += (object sender, SyncFailureEventArgs e) => { failureMessage += string.Format("Not expecting OnSyncFailure Got exception {0}\n", e.Exception.Message); }; d2.OnSyncConflict += (Dataset dataset, List <SyncConflict> conflicts) => { failureMessage += "Not expecting OnSyncConflict\n"; return(false); }; d2.OnDatasetDeleted += (Dataset dataset) => { failureMessage += "Not expecting OnDatasetDeleted\n"; return(false); }; d2.OnDatasetMerged += (Dataset dataset, List <string> datasetNames) => { failureMessage += "Not expecting OnDatasetMerged\n"; return(false); }; #if BCL35 d2.Synchronize(); #else RunAsSync(async() => await d2.SynchronizeAsync()); #endif } } }; d.OnSyncFailure += delegate(object s, SyncFailureEventArgs e) { failureMessage += string.Format("Not expecting OnSyncFailure Got exception {0}\n", e.Exception.Message); }; d.OnSyncConflict += (Dataset dataset, List <SyncConflict> syncConflicts) => { failureMessage += "Not expecting OnSyncConflict\n"; return(false); }; d.OnDatasetDeleted += (Dataset dataset) => { failureMessage += "Not expecting OnDatasetDeleted\n"; return(false); }; d.OnDatasetMerged += (Dataset dataset, List <string> datasetNames) => { failureMessage += "Not expecting OnDatasetMerged\n"; return(false); }; #if BCL35 d.Synchronize(); #else RunAsSync(async() => await d.SynchronizeAsync()); #endif } } if (!string.IsNullOrEmpty(failureMessage)) { Assert.Fail(failureMessage); } }
// <summary> /// Test case: Store a value in a dataset and sync it. Wipe all local data. /// After synchronizing the dataset we should have our stored value back. /// </summary> //[TestMethod] //[TestCategory("SyncManager")] public void DatasetCloudStorageTest() { string failureMessage = string.Empty; using (CognitoSyncManager syncManager = new CognitoSyncManager(UnAuthCredentials)) { syncManager.WipeData(false); Thread.Sleep(2000); using (Dataset d = syncManager.OpenOrCreateDataset("testDataset2")) { d.Put("key", "he who must not be named"); d.OnSyncSuccess += delegate(object sender, SyncSuccessEventArgs e) { d.ClearAllDelegates(); string erasedValue = d.Get("key"); syncManager.WipeData(false); d.OnSyncSuccess += delegate(object sender2, SyncSuccessEventArgs e2) { string restoredValues = d.Get("key"); if (erasedValue == null) { failureMessage += "erasedValue should not be null\n"; } if (restoredValues == null) { failureMessage += "restoredValues should not be null\n"; } if (erasedValue != restoredValues) { failureMessage += "erasedValue should equal restoredValues\n"; } }; d.OnSyncFailure += delegate(object sender2, SyncFailureEventArgs e2) { failureMessage += "sync failed\n"; }; #if BCL35 d.Synchronize(); #else RunAsSync(async() => await d.SynchronizeAsync()); #endif }; d.OnSyncFailure += delegate(object sender, SyncFailureEventArgs e) { failureMessage += "sync failed\n"; }; d.OnSyncConflict += delegate(Dataset dataset, List <SyncConflict> conflicts) { failureMessage += "Expected SyncSuccess instead of SyncConflict\n"; return(false); }; d.OnDatasetMerged += (Dataset dataset, List <string> datasetNames) => { failureMessage += "Did not expect DatasetMerged\n"; return(false); }; d.OnDatasetDeleted += (Dataset dataset) => { failureMessage += "Did not expect DatasetDeleted\n"; return(false); }; #if BCL35 d.Synchronize(); #else RunAsSync(async() => await d.SynchronizeAsync()); #endif } } if (!string.IsNullOrEmpty(failureMessage)) { Assert.Fail(failureMessage); } }