Ejemplo n.º 1
0
        public void TestSimpleSyncCases()
        {
            //create the default database:
            TestKp2aApp app = SetupAppWithDefaultDatabase();


            IOConnection.DeleteFile(new IOConnectionInfo {
                Path = DefaultFilename
            });
            //save it and reload it so we have a base version ("remote" and in the cache)
            SaveDatabase(app);
            app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
            _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId);

            string resultMessage;
            bool   wasSuccessful;

            //sync without changes on any side:
            Synchronize(app, out wasSuccessful, out resultMessage);
            Assert.IsTrue(wasSuccessful);
            Assert.AreEqual(resultMessage, app.GetResourceString(UiStringKey.FilesInSync));

            //go offline:
            TestFileStorage.Offline = true;

            //sync when offline (->error)
            Synchronize(app, out wasSuccessful, out resultMessage);
            Assert.IsFalse(wasSuccessful);
            Assert.AreEqual(resultMessage, "offline");

            //modify the database by adding a group:
            app.GetDb().KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "TestGroup", PwIcon.Apple), true);
            //save the database again (will be saved locally only)
            SaveDatabase(app);

            _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntSaveToRemoteId);

            //go online again:
            TestFileStorage.Offline = false;

            //sync with local changes only (-> upload):
            Synchronize(app, out wasSuccessful, out resultMessage);
            Assert.IsTrue(wasSuccessful);
            Assert.AreEqual(resultMessage, app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));

            //ensure both files are identical and up to date now:
            TestFileStorage.Offline = true;
            var appOfflineLoaded = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);

            _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntOpenFromRemoteId);
            TestFileStorage.Offline = false;
            var appRemoteLoaded = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);

            _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId);

            AssertDatabasesAreEqual(app.GetDb().KpDatabase, appOfflineLoaded.GetDb().KpDatabase);
            AssertDatabasesAreEqual(app.GetDb().KpDatabase, appRemoteLoaded.GetDb().KpDatabase);
        }
        public void TestLoadEditSave()
        {
            //create the default database:
            IKp2aApp app = SetupAppWithDefaultDatabase();

            IOConnection.DeleteFile(new IOConnectionInfo {
                Path = DefaultFilename
            });
            //save it and reload it so we have a base version
            SaveDatabase(app);
            _testCacheSupervisor.AssertNoCall();
            app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
            _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId);
            //modify the database by adding a group:
            app.GetDb().KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "TestGroup", PwIcon.Apple), true);
            //save the database again:
            SaveDatabase(app);
            Assert.IsNull(((TestKp2aApp)app).LastYesNoCancelQuestionTitle);
            _testCacheSupervisor.AssertNoCall();

            //load database to a new app instance:
            IKp2aApp resultApp = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);

            //ensure the change was saved:
            AssertDatabasesAreEqual(app.GetDb().KpDatabase, resultApp.GetDb().KpDatabase);
        }
        public void TestMakeAccessibleWhenOffline()
        {
            SetupFileStorage();

            //read the file once. Should now be in the cache.
            MemoryStream fileContents = ReadToMemoryStream(_fileStorage, CachingTestFile);

            _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.UpdatedCachedFileOnLoadId);

            //check it's the correct data:
            Assert.AreEqual(MemoryStreamToString(fileContents), _defaultCacheFileContents);

            //let the base file storage go offline:
            TestFileStorage.Offline = true;

            //now try to read the file again:
            MemoryStream fileContents2 = ReadToMemoryStream(_fileStorage, CachingTestFile);

            AssertEqual(fileContents, fileContents2);

            _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntOpenFromRemoteId);
        }