void note_store_delete_empty_collection_async(
UiObject *obj,
- Resource *res,
+ int64_t resource_id,
execresult_func resultcb,
void *userdata)
{
ExecJob *job = malloc(sizeof(ExecJob));
- job->resource = res;
+ job->resource = NULL;
job->resultcb = resultcb;
job->userdata = userdata;
job->error = 0;
- job->id1 = res->resource_id;
+ job->id1 = resource_id;
job->id2 = 0;
job->flag = 0;
ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_collection, job, (ui_callback)uithr_execjob_finished, job);
void note_store_delete_collection_async(
UiObject *obj,
- Resource *res,
+ int64_t resource_id,
execresult_func resultcb,
void *userdata)
{
ExecJob *job = malloc(sizeof(ExecJob));
- job->resource = res;
+ job->resource = NULL;
job->resultcb = resultcb;
job->userdata = userdata;
job->error = 0;
- job->id1 = res->resource_id;
+ job->id1 = resource_id;
job->id2 = 0;
job->flag = NOTESTORE_COLLECTION_DELETE_ALL;
ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_collection, job, (ui_callback)uithr_execjob_finished, job);
void note_store_delete_collection_and_move_children_async(
UiObject *obj,
- Resource *res,
+ int64_t resource_id,
int64_t new_parent_id,
execresult_func resultcb,
void *userdata)
{
ExecJob *job = malloc(sizeof(ExecJob));
job->resultcb = resultcb;
- job->resource = res;
+ job->resource = NULL;
job->userdata = userdata;
job->error = 0;
- job->id1 = res->resource_id;
+ job->id1 = resource_id;
job->id2 = new_parent_id;
job->flag = 0;
ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_collection_and_move_children, job, (ui_callback)uithr_execjob_finished, job);
test_res2 = cxListAt(test_res1->children, 0);
CX_TEST_ASSERT(!cx_strcmp(test_res2->nodename, "child_moved_to_group2"));
+
+ ui_close(obj);
}
}
}
}
+static void test_delete_result(UiEvent *event, int error, void *userdata) {
+ int *result = userdata;
+ *result = error;
+}
+
+CX_TEST(test_note_store_delete_empty_collection_async) {
+ CX_TEST_DO {
+ UiObject *obj = ui_dummy_object();
+
+ NoteStore *store = note_store_get();
+ CX_TEST_ASSERT(store);
+
+ // create an empty collection
+ Resource *res0 = cxZalloc(store->mp->allocator, sizeof(Resource));
+ res0->parent_id = store->root->resource_id;
+ res0->nodename = cx_strdup_a(store->mp->allocator, "delete_empty_test").ptr;
+
+ int error = -1;
+ note_store_save_notebook_async(obj, res0, 0, test_save_notebook_result, &error);
+ ui_exec_buffered_mainthread_calls_wait(3);
+
+ // create a collection with children
+ Resource *res1 = cxZalloc(store->mp->allocator, sizeof(Resource));
+ res1->parent_id = store->root->resource_id;
+ res1->nodename = cx_strdup_a(store->mp->allocator, "delete_empty_test_fail").ptr;
+
+ error = -1;
+ note_store_save_notebook_async(obj, res1, 0, test_save_notebook_result, &error);
+ ui_exec_buffered_mainthread_calls_wait(3);
+
+ CX_TEST_ASSERT(error == 0);
+ CX_TEST_ASSERT(res1->resource_id != 0);
+
+ Resource *res1_child = cxZalloc(store->mp->allocator, sizeof(Resource));
+ res1_child->parent_id = res1->resource_id;
+ res1_child->nodename = cx_strdup_a(store->mp->allocator, "child1").ptr;
+
+ error = -1;
+ note_store_save_notebook_async(obj, res1_child, 0, test_save_notebook_result, &error);
+ ui_exec_buffered_mainthread_calls_wait(3);
+
+ CX_TEST_ASSERT(error == 0);
+ CX_TEST_ASSERT(res1->resource_id != 0);
+
+ // test delete
+ int result = -1;
+ note_store_delete_empty_collection_async(obj, res0->resource_id, test_delete_result, &result);
+ ui_exec_buffered_mainthread_calls_wait(3);
+
+ CX_TEST_ASSERT(result == 0);
+
+ // check, if the resource was actually deleted
+ int64_t res0_id = res0->resource_id;
+ int64_t res1_id = res1->resource_id;
+ CX_TEST_ASSERT(!note_store_reload());
+ store = note_store_get();
+ CX_TEST_ASSERT(store);
+
+ Resource *res0_deleted = note_store_get_notebook_by_id(store, res0_id);
+ CX_TEST_ASSERT(res0_deleted == NULL);
+
+ // test delete-fail
+ result = -1;
+ note_store_delete_empty_collection_async(obj, res1_id, test_delete_result, &result);
+ ui_exec_buffered_mainthread_calls_wait(3);
+
+ CX_TEST_ASSERT(result != 0);
+
+ // check, if the resource still exists
+ CX_TEST_ASSERT(!note_store_reload());
+ store = note_store_get();
+ CX_TEST_ASSERT(store);
+
+ Resource *res1_not_deleted = note_store_get_notebook_by_id(store, res1_id);
+ CX_TEST_ASSERT(res1_not_deleted != NULL);
+
+ // cleanup
+ ui_close(obj);
+ }
+}