[PATCH v4 2/4] windows.media.speech: Partially implement IAsyncOperation.

Bernhard Kölbl besentv at gmail.com
Mon Apr 18 16:42:22 CDT 2022


Signed-off-by: Bernhard Kölbl <besentv at gmail.com>
---
 dlls/windows.media.speech/async.c        | 142 ++++++++++++++++++++---
 dlls/windows.media.speech/tests/speech.c |  88 +++++++-------
 2 files changed, 172 insertions(+), 58 deletions(-)

diff --git a/dlls/windows.media.speech/async.c b/dlls/windows.media.speech/async.c
index 94d0b35d9c1..cf0da23e493 100644
--- a/dlls/windows.media.speech/async.c
+++ b/dlls/windows.media.speech/async.c
@@ -23,6 +23,9 @@
 
 WINE_DEFAULT_DEBUG_CHANNEL(speech);
 
+#define Closed 4
+#define HANDLER_NOT_SET ((void *)~(ULONG_PTR)0)
+
 /*
  *
  * IAsyncOperation<IInspectable*>
@@ -35,6 +38,11 @@ struct async_operation
     IAsyncInfo IAsyncInfo_iface;
     const GUID *iid;
     LONG ref;
+
+    IAsyncOperationCompletedHandler_IInspectable *handler;
+
+    AsyncStatus status;
+    HRESULT hr;
 };
 
 static inline struct async_operation *impl_from_IAsyncOperation_IInspectable(IAsyncOperation_IInspectable *iface)
@@ -84,7 +92,14 @@ static ULONG WINAPI async_operation_Release( IAsyncOperation_IInspectable *iface
     TRACE("iface %p, ref %lu.\n", iface, ref);
 
     if (!ref)
+    {
+        IAsyncInfo_Close(&impl->IAsyncInfo_iface);
+
+        if (impl->handler && impl->handler != HANDLER_NOT_SET)
+            IAsyncOperationCompletedHandler_IInspectable_Release(impl->handler);
+
         free(impl);
+    }
 
     return ref;
 }
@@ -110,21 +125,79 @@ static HRESULT WINAPI async_operation_GetTrustLevel( IAsyncOperation_IInspectabl
 static HRESULT WINAPI async_operation_put_Completed( IAsyncOperation_IInspectable *iface,
                                                      IAsyncOperationCompletedHandler_IInspectable *handler )
 {
-    FIXME("iface %p, handler %p stub!\n", iface, handler);
-    return E_NOTIMPL;
+    struct async_operation *impl = impl_from_IAsyncOperation_IInspectable(iface);
+    HRESULT hr;
+
+    TRACE("iface %p, handler %p.\n", iface, handler);
+
+    if (impl->handler == HANDLER_NOT_SET)
+    {
+        /*
+            impl->hanlder can only be set once with async_operation_put_Completed,
+            so by default we set a non HANDLER_NOT_SET value, in this case NULL.
+        */
+        impl->handler = NULL;
+
+        if (handler)
+        {
+            if (impl->status != Started)
+            {
+                IAsyncOperation_IInspectable *operation = &impl->IAsyncOperation_IInspectable_iface;
+                AsyncStatus status = impl->status;
+
+                IAsyncOperation_IInspectable_AddRef(operation);
+                IAsyncOperationCompletedHandler_IInspectable_AddRef(handler);
+
+                IAsyncOperationCompletedHandler_IInspectable_Invoke(handler, operation, status);
+
+                IAsyncOperationCompletedHandler_IInspectable_Release(handler);
+                IAsyncOperation_IInspectable_Release(operation);
+            }
+        }
+
+        return S_OK;
+    }
+    else if (impl->status == Closed)
+        hr = E_ILLEGAL_METHOD_CALL;
+    else if (impl->handler != HANDLER_NOT_SET)
+        hr = E_ILLEGAL_DELEGATE_ASSIGNMENT;
+    else
+        hr = E_UNEXPECTED;
+
+    return hr;
 }
 
 static HRESULT WINAPI async_operation_get_Completed( IAsyncOperation_IInspectable *iface,
                                                      IAsyncOperationCompletedHandler_IInspectable **handler )
 {
-    FIXME("iface %p, handler %p stub!\n", iface, handler);
-    return E_NOTIMPL;
+    struct async_operation *impl = impl_from_IAsyncOperation_IInspectable(iface);
+    HRESULT hr;
+
+    FIXME("iface %p, handler %p semi stub!\n", iface, handler);
+
+    if (impl->handler != HANDLER_NOT_SET && impl->status == Closed)
+        hr = E_ILLEGAL_METHOD_CALL;
+    else
+        hr = S_OK;
+
+    *handler = NULL; /* Hanlder *seems* to always be NULL from the tests. */
+    return hr;
 }
 
 static HRESULT WINAPI async_operation_GetResults( IAsyncOperation_IInspectable *iface, IInspectable **results )
 {
-    FIXME("iface %p, results %p stub!\n", iface, results);
-    return E_NOTIMPL;
+    /* NOTE: Despite the name, this function only returns one result! */
+    struct async_operation *impl = impl_from_IAsyncOperation_IInspectable(iface);
+    HRESULT hr;
+
+    TRACE("iface %p, results %p.\n", iface, results);
+
+    if (impl->status == Closed)
+        hr = E_ILLEGAL_METHOD_CALL;
+    else
+        hr = S_OK;
+
+    return hr;
 }
 
 static const struct IAsyncOperation_IInspectableVtbl async_operation_vtbl =
@@ -159,26 +232,53 @@ static HRESULT WINAPI async_operation_info_get_Id( IAsyncInfo *iface, UINT32 *id
 
 static HRESULT WINAPI async_operation_info_get_Status( IAsyncInfo *iface, AsyncStatus *status )
 {
-    FIXME("iface %p, status %p stub!\n", iface, status);
-    return E_NOTIMPL;
+    struct async_operation *impl = impl_from_IAsyncInfo(iface);
+    HRESULT hr;
+
+    TRACE("iface %p, status %p.\n", iface, status);
+
+    if (impl->status == Closed)
+        hr = E_ILLEGAL_METHOD_CALL;
+    else
+        hr = S_OK;
+
+    *status = impl->status;
+    return hr;
 }
 
 static HRESULT WINAPI async_operation_info_get_ErrorCode( IAsyncInfo *iface, HRESULT *error_code )
 {
-    FIXME("iface %p, error_code %p stub!\n", iface, error_code);
-    return E_NOTIMPL;
+    struct async_operation *impl = impl_from_IAsyncInfo(iface);
+    TRACE("iface %p, error_code %p.\n", iface, error_code);
+
+    *error_code = impl->hr;
+    return S_OK;
 }
 
 static HRESULT WINAPI async_operation_info_Cancel( IAsyncInfo *iface )
 {
-    FIXME("iface %p stub!\n", iface);
-    return E_NOTIMPL;
+    struct async_operation *impl = impl_from_IAsyncInfo(iface);
+    HRESULT hr;
+
+    TRACE("iface %p.\n", iface);
+    hr = S_OK;
+
+    if (impl->status == Closed)
+        hr = E_ILLEGAL_METHOD_CALL;
+    else if (impl->status == Started)
+        impl->status = Canceled;
+
+    return hr;
 }
 
 static HRESULT WINAPI async_operation_info_Close( IAsyncInfo *iface )
 {
-    FIXME("iface %p stub!\n", iface);
-    return E_NOTIMPL;
+    struct async_operation *impl = impl_from_IAsyncInfo(iface);
+
+    TRACE("iface %p.\n", iface);
+
+    impl->status = Closed;
+    return S_OK;
 }
 
 static const struct IAsyncInfoVtbl async_operation_info_vtbl =
@@ -202,11 +302,14 @@ static const struct IAsyncInfoVtbl async_operation_info_vtbl =
 HRESULT async_operation_create( const GUID *iid, IAsyncOperation_IInspectable **out )
 {
     struct async_operation *impl;
+    HRESULT hr;
+
+    *out = NULL;
 
     if (!(impl = calloc(1, sizeof(*impl))))
     {
-        *out = NULL;
-        return E_OUTOFMEMORY;
+        hr = E_OUTOFMEMORY;
+        goto error;
     }
 
     impl->IAsyncOperation_IInspectable_iface.lpVtbl = &async_operation_vtbl;
@@ -214,7 +317,14 @@ HRESULT async_operation_create( const GUID *iid, IAsyncOperation_IInspectable **
     impl->iid = iid;
     impl->ref = 1;
 
+    impl->handler = HANDLER_NOT_SET;
+    impl->status = Closed;
+
     *out = &impl->IAsyncOperation_IInspectable_iface;
     TRACE("created %p\n", *out);
     return S_OK;
+
+error:
+    free(impl);
+    return hr;
 }
diff --git a/dlls/windows.media.speech/tests/speech.c b/dlls/windows.media.speech/tests/speech.c
index 7fca3fa6179..03608842cd7 100644
--- a/dlls/windows.media.speech/tests/speech.c
+++ b/dlls/windows.media.speech/tests/speech.c
@@ -992,20 +992,20 @@ static void test_SpeechRecognizer(void)
 
         handler = (void*)0xdeadbeef;
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_get_Completed(operation, &handler);
-        todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
-        todo_wine ok(handler == NULL, "Handler had value %p.\n", handler);
+        ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
+        ok(handler == NULL, "Handler had value %p.\n", handler);
 
         if (FAILED(hr)) goto skip_operation;
 
         compilation_result = (void*)0xdeadbeef;
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_GetResults(operation, &compilation_result);
-        todo_wine ok(hr == E_ILLEGAL_METHOD_CALL, "Got unexpected hr %#lx.\n", hr);
-        todo_wine ok(compilation_result == (void*)0xdeadbeef, "Compilation result had value %p.\n", compilation_result);
+        ok(hr == E_ILLEGAL_METHOD_CALL, "Got unexpected hr %#lx.\n", hr);
+        ok(compilation_result == (void*)0xdeadbeef, "Compilation result had value %p.\n", compilation_result);
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_put_Completed(operation, &compilation_handler.IAsyncHandler_Compilation_iface);
-        todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
 
-        todo_wine ok(!WaitForSingleObject(compilation_handler.event_finished, 1000), "Wait for event_finished failed.\n");
+        ok(!WaitForSingleObject(compilation_handler.event_finished, 1000), "Wait for event_finished failed.\n");
         CloseHandle(compilation_handler.event_finished);
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_put_Completed(operation, NULL);
@@ -1014,32 +1014,36 @@ static void test_SpeechRecognizer(void)
         handler = (void*)0xdeadbeef;
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_get_Completed(operation, &handler);
         todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
-        todo_wine ok(handler == NULL || broken(handler != NULL), "Handler had value %p.\n", handler); /* Broken on Win10 1507 x32... */
+        ok(handler == NULL || broken(handler != NULL), "Handler had value %p.\n", handler); /* Broken on Win10 1507 x32... */
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_GetResults(operation, &compilation_result);
         todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
-        todo_wine check_interface(compilation_result, &IID_IAgileObject, TRUE);
 
-        hr = ISpeechRecognitionCompilationResult_get_Status(compilation_result, &result_status);
-        todo_wine ok(hr == S_OK, "ISpeechRecognitionCompilationResult_get_Status failed, hr %#lx.\n", hr);
-        todo_wine ok(result_status == SpeechRecognitionResultStatus_Success, "Got unexpected status %#x.\n", result_status);
+        if (SUCCEEDED(hr))
+        {
+            todo_wine check_interface(compilation_result, &IID_IAgileObject, TRUE);
+
+            hr = ISpeechRecognitionCompilationResult_get_Status(compilation_result, &result_status);
+            todo_wine ok(hr == S_OK, "ISpeechRecognitionCompilationResult_get_Status failed, hr %#lx.\n", hr);
+            todo_wine ok(result_status == SpeechRecognitionResultStatus_Success, "Got unexpected status %#x.\n", result_status);
 
-        ref = ISpeechRecognitionCompilationResult_Release(compilation_result);
-        todo_wine ok(!ref , "Got unexpected ref %lu.\n", ref);
+            ref = ISpeechRecognitionCompilationResult_Release(compilation_result);
+            todo_wine ok(!ref , "Got unexpected ref %lu.\n", ref);
+        }
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_GetResults(operation, &compilation_result);
         todo_wine ok(hr == E_UNEXPECTED, "Got unexpected hr %#lx.\n", hr);
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_QueryInterface(operation, &IID_IAsyncInfo, (void **)&info);
-        todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
 
         /* Check if AsyncInfo and AsyncOperation share the same refcount. */
         IAsyncOperation_SpeechRecognitionCompilationResult_AddRef(operation);
-        todo_wine check_refcount(operation, 3);
-        todo_wine check_refcount(info, 3);
+        check_refcount(operation, 3);
+        check_refcount(info, 3);
 
         IAsyncOperation_SpeechRecognitionCompilationResult_Release(operation);
-        todo_wine check_refcount(info, 2);
+        check_refcount(info, 2);
 
         id = 0xdeadbeef;
         hr = IAsyncInfo_get_Id(info, &id);
@@ -1060,24 +1064,24 @@ static void test_SpeechRecognizer(void)
         todo_wine ok(async_status == Completed, "Status was %#x.\n", async_status);
 
         hr = IAsyncInfo_Close(info);
-        todo_wine ok(hr == S_OK, "IAsyncInfo_Close failed, hr %#lx.\n", hr);
+        ok(hr == S_OK, "IAsyncInfo_Close failed, hr %#lx.\n", hr);
         hr = IAsyncInfo_Close(info);
-        todo_wine ok(hr == S_OK, "IAsyncInfo_Close failed, hr %#lx.\n", hr);
+        ok(hr == S_OK, "IAsyncInfo_Close failed, hr %#lx.\n", hr);
 
         async_status = 0xdeadbeef;
         hr = IAsyncInfo_get_Status(info, &async_status);
-        todo_wine ok(hr == E_ILLEGAL_METHOD_CALL, "IAsyncInfo_get_Status failed, hr %#lx.\n", hr);
-        todo_wine ok(async_status == AsyncStatus_Closed, "Status was %#x.\n", async_status);
+        ok(hr == E_ILLEGAL_METHOD_CALL, "IAsyncInfo_get_Status failed, hr %#lx.\n", hr);
+        ok(async_status == AsyncStatus_Closed, "Status was %#x.\n", async_status);
 
         ref = IAsyncInfo_Release(info);
-        todo_wine ok(ref == 1, "Got unexpected ref %lu.\n", ref);
+        ok(ref == 1, "Got unexpected ref %lu.\n", ref);
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_put_Completed(operation, NULL);
-        todo_wine ok(hr == E_ILLEGAL_METHOD_CALL, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == E_ILLEGAL_METHOD_CALL, "Got unexpected hr %#lx.\n", hr);
 
         handler = (void*)0xdeadbeef;
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_get_Completed(operation, &handler);
-        todo_wine ok(hr == E_ILLEGAL_METHOD_CALL, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == E_ILLEGAL_METHOD_CALL, "Got unexpected hr %#lx.\n", hr);
 
         IAsyncOperation_SpeechRecognitionCompilationResult_Release(operation);
 
@@ -1086,19 +1090,19 @@ static void test_SpeechRecognizer(void)
         compilation_handler2.event_finished = CreateEventW(NULL, FALSE, FALSE, NULL);
         compilation_handler2.thread_id = GetCurrentThreadId();
 
-        todo_wine ok(compilation_handler2.event_finished != NULL, "Finished event wasn't created.\n");
+        ok(compilation_handler2.event_finished != NULL, "Finished event wasn't created.\n");
 
         hr = ISpeechRecognizer_CompileConstraintsAsync(recognizer, &operation);
-        todo_wine ok(hr == S_OK, "ISpeechRecognizer_CompileConstraintsAsync failed, hr %#lx.\n", hr);
-        todo_wine check_interface(operation, &IID_IAgileObject, TRUE);
+        ok(hr == S_OK, "ISpeechRecognizer_CompileConstraintsAsync failed, hr %#lx.\n", hr);
+        check_interface(operation, &IID_IAgileObject, TRUE);
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_QueryInterface(operation, &IID_IAsyncInfo, (void **)&info);
-        todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_put_Completed(operation, &compilation_handler2.IAsyncHandler_Compilation_iface);
-        todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
 
-        todo_wine ok(!WaitForSingleObject(compilation_handler2.event_finished, 1000), "Wait for event_finished failed.\n");
+        ok(!WaitForSingleObject(compilation_handler2.event_finished, 1000), "Wait for event_finished failed.\n");
         CloseHandle(compilation_handler2.event_finished);
 
         async_status = 0xdeadbeef;
@@ -1107,32 +1111,32 @@ static void test_SpeechRecognizer(void)
         todo_wine ok(async_status == Completed, "Status was %#x.\n", async_status);
 
         ref = IAsyncInfo_Release(info);
-        todo_wine ok(ref == 1, "Got unexpected ref %lu.\n", ref);
+        ok(ref == 1, "Got unexpected ref %lu.\n", ref);
         ref = IAsyncOperation_SpeechRecognitionCompilationResult_Release(operation);
-        todo_wine ok(!ref, "Got unexpected ref %lu.\n", ref);
+        ok(!ref, "Got unexpected ref %lu.\n", ref);
 
         /* Test if AsyncInfo_Close waits for the handler to finish. */
         hr = RoActivateInstance(hstr, &inspectable_2);
-        todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
 
         hr = IInspectable_QueryInterface(inspectable_2, &IID_ISpeechRecognizer, (void **)&recognizer_2);
-        todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
 
         compilation_handler_create_static(&compilation_handler3);
         compilation_handler3.event_block = CreateEventW(NULL, FALSE, FALSE, NULL);
         compilation_handler3.event_finished = CreateEventW(NULL, FALSE, FALSE, NULL);
         compilation_handler3.thread_id = GetCurrentThreadId();
 
-        todo_wine ok(compilation_handler3.event_finished != NULL, "Finished event wasn't created.\n");
+        ok(compilation_handler3.event_finished != NULL, "Finished event wasn't created.\n");
 
         hr = ISpeechRecognizer_CompileConstraintsAsync(recognizer_2, &operation);
-        todo_wine ok(hr == S_OK, "ISpeechRecognizer_CompileConstraintsAsync failed, hr %#lx.\n", hr);
+        ok(hr == S_OK, "ISpeechRecognizer_CompileConstraintsAsync failed, hr %#lx.\n", hr);
 
         block_param.handler = &compilation_handler3.IAsyncHandler_Compilation_iface;
         block_param.operation = operation;
         blocked_thread = CreateThread(NULL, 0, async_operation_block_thread, &block_param, 0, NULL);
         thread_status = WaitForSingleObject(blocked_thread, 1000);
-        todo_wine ok(thread_status == WAIT_TIMEOUT || broken(thread_status == WAIT_OBJECT_0), "Wait for block_thread didn't time out.\n"); /* Broken on Win10 1507 x32... */
+        ok(thread_status == WAIT_TIMEOUT || broken(thread_status == WAIT_OBJECT_0), "Wait for block_thread didn't time out.\n"); /* Broken on Win10 1507 x32... */
 
         if (thread_status == WAIT_TIMEOUT)
         {
@@ -1140,25 +1144,25 @@ static void test_SpeechRecognizer(void)
             todo_wine check_refcount(operation, 3);
 
             hr = IAsyncOperation_SpeechRecognitionCompilationResult_QueryInterface(operation, &IID_IAsyncInfo, (void **)&info);
-            todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
+            ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
 
             close_param.info = info;
             close_thread = CreateThread(NULL, 0, async_operation_close_thread, &close_param, 0, NULL);
-            todo_wine ok(!WaitForSingleObject(close_thread, 100), "Wait for close_thread failed.\n");
+            ok(!WaitForSingleObject(close_thread, 100), "Wait for close_thread failed.\n");
 
             CloseHandle(close_thread);
         }
 
         SetEvent(compilation_handler3.event_block);
-        todo_wine ok(!WaitForSingleObject(compilation_handler3.event_finished, 500), "Wait for event_finished failed.\n");
-        todo_wine ok(!WaitForSingleObject(blocked_thread, 1000), "Wait for block_thread failed.\n");
+        ok(!WaitForSingleObject(compilation_handler3.event_finished, 500), "Wait for event_finished failed.\n");
+        ok(!WaitForSingleObject(blocked_thread, 1000), "Wait for block_thread failed.\n");
 
         CloseHandle(blocked_thread);
         CloseHandle(compilation_handler3.event_block);
         CloseHandle(compilation_handler3.event_finished);
 
         ref = IAsyncInfo_Release(info);
-        todo_wine ok(ref == 1, "Got unexpected ref %lu.\n", ref);
+        ok(ref == 1, "Got unexpected ref %lu.\n", ref);
 
 skip_operation:
         ref = IAsyncOperation_SpeechRecognitionCompilationResult_Release(operation);
-- 
2.35.1




More information about the wine-devel mailing list