+ // cleanup
+ ui_close(obj);
+ }
+}
+
+static void note_content_loaded(UiEvent *event, cxmutstr result, void *userdata) {
+ cxmutstr *ptr = userdata;
+ *ptr = result;
+}
+
+CX_TEST(test_note_store_update_note_async) {
+ CX_TEST_DO {
+ UiObject *obj = ui_dummy_object();
+ NoteStore *store = note_store_get();
+ const CxAllocator *a = store->mp->allocator;
+
+ CX_TEST_ASSERT(store && store->root && store->root->children);
+ CX_TEST_ASSERT(cxListSize(store->root->children) > 0);
+
+ Resource *parent = cxListAt(store->root->children, 0);
+ CX_TEST_ASSERT(parent);
+
+ // create a new note first
+ Note *note = cxZalloc(a, sizeof(Note));
+ Resource *res = cxZalloc(a, sizeof(Resource));
+ res->nodename = cx_strdup_a(a, "note2").ptr;
+ res->content = cx_strdup_a(a, "Hello World 2");
+ res->parent_id = parent->resource_id;
+ note->resource = res;
+
+ int error = -1;
+ note_store_new_note_async(obj, note, note_saved, &error);
+ ui_exec_buffered_mainthread_calls_wait(3);
+
+ CX_TEST_ASSERT(error == 0);
+ CX_TEST_ASSERT(note->resource_id > 0);
+ CX_TEST_ASSERT(note->resource->resource_id > 0);
+ CX_TEST_ASSERT(note->resource_id == note->resource->resource_id);
+ CX_TEST_ASSERT(note->note_id > 0);
+
+ // now test the update
+ note->type = 3;
+ cxFree(a, note->resource->nodename);
+ note->resource->nodename = cx_strdup_a(a, "note2_renamed").ptr;
+ cxFree(a, note->resource->content.ptr);
+ note->resource->content = cx_strdup_a(a, "note2 new content");
+
+ int64_t note_id = note->note_id;
+ int64_t resource_id = note->resource_id;
+ error = -1;
+ note_store_update_note_async(obj, note, note_saved, &error);
+ ui_exec_buffered_mainthread_calls_wait(3);
+
+ CX_TEST_ASSERT(error == 0);
+ CX_TEST_ASSERT(note->note_id == note_id);
+ CX_TEST_ASSERT(note->resource_id == resource_id);
+
+ CxList *notes = note_store_get_notes(a, parent->resource_id);
+ Note *updated_note = NULL;
+ CxIterator i = cxListIterator(notes);
+ cx_foreach(Note *, n, i) {
+ if(n->note_id == note_id) {
+ updated_note = n;
+ break;
+ }
+ }
+
+ CX_TEST_ASSERT(updated_note);
+ CX_TEST_ASSERT(updated_note->resource);
+ CX_TEST_ASSERT(!cx_strcmp(updated_note->resource->nodename, note->resource->nodename));
+
+ cxmutstr content = note_store_get_note_content(a, resource_id);
+ CX_TEST_ASSERT(!cx_strcmp(content, note->resource->content));
+
+