1a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch/*
2a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * Copyright (C) 2010 Apple Inc. All rights reserved.
3a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *
4a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * Redistribution and use in source and binary forms, with or without
5a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * modification, are permitted provided that the following conditions
6a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * are met:
7a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * 1. Redistributions of source code must retain the above copyright
8a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *    notice, this list of conditions and the following disclaimer.
9a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * 2. Redistributions in binary form must reproduce the above copyright
10a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *    notice, this list of conditions and the following disclaimer in the
11a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *    documentation and/or other materials provided with the distribution.
12a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *
13a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * THE POSSIBILITY OF SUCH DAMAGE.
24a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch */
25a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
26a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include "PlatformUtilities.h"
27a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
28a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include <WebKit2/WKStringCF.h>
29a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include <WebKit2/WKURLCF.h>
30a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include <wtf/RetainPtr.h>
31a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
322bde8e466a4451c7319e3a072d118917957d6554Steve Block#if USE(CFNETWORK)
332bde8e466a4451c7319e3a072d118917957d6554Steve Block#include <WebKit2/WKURLResponseCF.h>
342bde8e466a4451c7319e3a072d118917957d6554Steve Block#endif
352bde8e466a4451c7319e3a072d118917957d6554Steve Block
36a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochnamespace TestWebKitAPI {
37a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochnamespace Util {
38a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
394576aa36e9a9671459299c7963ac95aa94beaea9Shimeng (Simon) Wang#ifdef DEBUG_ALL
4081bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdochconst char* injectedBundleDLL = "TestWebKitAPIInjectedBundle_debug.dll";
41a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#else
4281bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdochconst char* injectedBundleDLL = "TestWebKitAPIInjectedBundle.dll";
43a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#endif
44a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
45a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochvoid run(bool* done)
46a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
47a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    while (!*done) {
48a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        MSG msg;
49a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        BOOL result = ::GetMessageW(&msg, 0, 0, 0);
50a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        if (!result || result == -1)
51a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch            break;
522daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch
532daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        if (shouldTranslateMessage(msg))
542daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch            ::TranslateMessage(&msg);
55a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        ::DispatchMessage(&msg);
56a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    }
57a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
58a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
592daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdochbool shouldTranslateMessage(const MSG& msg)
602daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch{
612daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    // Only these four messages are actually translated by ::TranslateMessage or ::TranslateAccelerator.
622daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    // It's useless (though harmless) to call those functions for other messages, so we always allow other messages to be translated.
632daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    if (msg.message != WM_KEYDOWN && msg.message != WM_SYSKEYDOWN && msg.message != WM_KEYUP && msg.message != WM_SYSKEYUP)
642daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        return true;
652daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch
662daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    wchar_t className[256];
672daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    if (!::GetClassNameW(msg.hwnd, className, ARRAYSIZE(className)))
682daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        return true;
692daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch
702daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    // Don't call TranslateMessage() on key events destined for a WebKit2 view, WebKit will do this if it doesn't handle the message.
712daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    // It would be nice to use some API here instead of hard-coding the window class name.
722daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    return wcscmp(className, L"WebKit2WebViewWindowClass");
732daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch}
742daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch
7581bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdochvoid sleep(double seconds)
7681bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch{
7781bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch    ::Sleep(seconds * 1000);
7881bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch}
7981bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch
80a94275402997c11dd2e778633dacf4b7e630a35dBen MurdochRetainPtr<CFStringRef> cf(const char* utf8String)
81a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
82a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    return RetainPtr<CFStringRef>(AdoptCF, CFStringCreateWithCString(kCFAllocatorDefault, utf8String, kCFStringEncodingUTF8));
83a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
84a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
85a94275402997c11dd2e778633dacf4b7e630a35dBen MurdochWKStringRef createInjectedBundlePath()
86a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
87a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    RetainPtr<CFURLRef> executableURL(AdoptCF, CFBundleCopyExecutableURL(CFBundleGetMainBundle()));
88a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    RetainPtr<CFURLRef> executableContainerURL(AdoptCF, CFURLCreateCopyDeletingLastPathComponent(0, executableURL.get()));
8981bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch    RetainPtr<CFStringRef> dllFilename(AdoptCF, CFStringCreateWithCStringNoCopy(0, injectedBundleDLL, kCFStringEncodingWindowsLatin1, kCFAllocatorNull));
90a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    RetainPtr<CFURLRef> bundleURL(AdoptCF, CFURLCreateCopyAppendingPathComponent(0, executableContainerURL.get(), dllFilename.get(), false));
91a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    RetainPtr<CFStringRef> bundlePath(AdoptCF, CFURLCopyFileSystemPath(bundleURL.get(), kCFURLWindowsPathStyle));
92a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    return WKStringCreateWithCFString(bundlePath.get());
93a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
94a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
95a94275402997c11dd2e778633dacf4b7e630a35dBen MurdochWKURLRef createURLForResource(const char* resource, const char* extension)
96a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
97a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    RetainPtr<CFURLRef> url(AdoptCF, CFBundleCopyResourceURL(CFBundleGetMainBundle(), cf(resource).get(), cf(extension).get(), 0));
98a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    return WKURLCreateWithCFURL(url.get());
99a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
100a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
101a94275402997c11dd2e778633dacf4b7e630a35dBen MurdochWKURLRef URLForNonExistentResource()
102a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
103a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    return WKURLCreateWithUTF8CString("file:///does-not-exist.html");
104a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
105a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
1062bde8e466a4451c7319e3a072d118917957d6554Steve BlockWKRetainPtr<WKStringRef> MIMETypeForWKURLResponse(WKURLResponseRef wkResponse)
1072bde8e466a4451c7319e3a072d118917957d6554Steve Block{
1082bde8e466a4451c7319e3a072d118917957d6554Steve Block#if USE(CFNETWORK)
1092bde8e466a4451c7319e3a072d118917957d6554Steve Block    RetainPtr<CFURLResponseRef> response(AdoptCF, WKURLResponseCopyCFURLResponse(0, wkResponse));
1102bde8e466a4451c7319e3a072d118917957d6554Steve Block    return adoptWK(WKStringCreateWithCFString(CFURLResponseGetMIMEType(response.get())));
1112bde8e466a4451c7319e3a072d118917957d6554Steve Block#else
1122bde8e466a4451c7319e3a072d118917957d6554Steve Block    return 0;
1132bde8e466a4451c7319e3a072d118917957d6554Steve Block#endif
1142bde8e466a4451c7319e3a072d118917957d6554Steve Block}
1152bde8e466a4451c7319e3a072d118917957d6554Steve Block
116a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochbool isKeyDown(WKNativeEventPtr event)
117a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
118a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    return event->message == WM_KEYDOWN;
119a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
120a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
121a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch} // namespace Util
122a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch} // namespace TestWebKitAPI
123