1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#undef WINVER
#undef _WIN32_WINNT
#define WINVER 0x602
#define _WIN32_WINNT 0x602
#include <windows.h>
#include <objbase.h>
#include <combaseapi.h>
#include <atlcore.h>
#include <atlstr.h>
#include <wininet.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <propkey.h>
#include <propvarutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <strsafe.h>
#include <io.h>
#include <shellapi.h>
static const WCHAR* kFirefoxExe = L"firefox.exe";
static const WCHAR* kDefaultMetroBrowserIDPathKey = L"FirefoxURL";
static const WCHAR* kDemoMetroBrowserIDPathKey = L"Mozilla.Firefox.URL";
static void Log(const wchar_t *fmt, ...)
{
va_list a = NULL;
wchar_t szDebugString[1024];
if(!lstrlenW(fmt))
return;
va_start(a,fmt);
vswprintf(szDebugString, 1024, fmt, a);
va_end(a);
if(!lstrlenW(szDebugString))
return;
wprintf(L"INFO | metrotestharness.exe | %s\n", szDebugString);
fflush(stdout);
}
static void Fail(const wchar_t *fmt, ...)
{
va_list a = NULL;
wchar_t szDebugString[1024];
if(!lstrlenW(fmt))
return;
va_start(a,fmt);
vswprintf(szDebugString, 1024, fmt, a);
va_end(a);
if(!lstrlenW(szDebugString))
return;
wprintf(L"TEST-UNEXPECTED-FAIL | metrotestharness.exe | %s\n", szDebugString);
fflush(stdout);
}
/*
* Retrieve our module dir path.
*
* @aPathBuffer Buffer to fill
*/
static bool GetModulePath(CStringW& aPathBuffer)
{
WCHAR buffer[MAX_PATH];
memset(buffer, 0, sizeof(buffer));
if (!GetModuleFileName(NULL, buffer, MAX_PATH)) {
Fail(L"GetModuleFileName failed.");
return false;
}
WCHAR* slash = wcsrchr(buffer, '\\');
if (!slash)
return false;
*slash = '\0';
aPathBuffer = buffer;
return true;
}
/*
* Retrieve 'module dir path\firefox.exe'
*
* @aPathBuffer Buffer to fill
*/
static bool GetDesktopBrowserPath(CStringW& aPathBuffer)
{
if (!GetModulePath(aPathBuffer))
return false;
// ceh.exe sits in dist/bin root with the desktop browser. Since this
// is a firefox only component, this hardcoded filename is ok.
aPathBuffer.Append(L"\\");
aPathBuffer.Append(kFirefoxExe);
return true;
}
/*
* Retrieve the app model id of the firefox metro browser.
*
* @aPathBuffer Buffer to fill
* @aCharLength Length of buffer to fill in characters
*/
static bool GetDefaultBrowserAppModelID(WCHAR* aIDBuffer,
long aCharLength)
{
if (!aIDBuffer || aCharLength <= 0)
return false;
memset(aIDBuffer, 0, (sizeof(WCHAR)*aCharLength));
HKEY key;
if (RegOpenKeyExW(HKEY_CLASSES_ROOT, kDefaultMetroBrowserIDPathKey,
0, KEY_READ, &key) != ERROR_SUCCESS) {
if (RegOpenKeyExW(HKEY_CLASSES_ROOT, kDemoMetroBrowserIDPathKey,
0, KEY_READ, &key) != ERROR_SUCCESS) {
return false;
}
}
DWORD len = aCharLength * sizeof(WCHAR);
memset(aIDBuffer, 0, len);
if (RegQueryValueExW(key, L"AppUserModelID", NULL, NULL,
(LPBYTE)aIDBuffer, &len) != ERROR_SUCCESS || !len) {
RegCloseKey(key);
return false;
}
RegCloseKey(key);
return true;
}
CString sAppParams;
static bool Launch()
{
Log(L"Launching browser...");
DWORD processID;
// The interface that allows us to activate the browser
IApplicationActivationManager* activateMgr = NULL;
if (FAILED(CoCreateInstance(CLSID_ApplicationActivationManager, NULL,
CLSCTX_LOCAL_SERVER,
IID_IApplicationActivationManager,
(void**)&activateMgr))) {
Fail(L"CoCreateInstance CLSID_ApplicationActivationManager failed.");
return false;
}
HRESULT hr;
WCHAR appModelID[256];
// Activation is based on the browser's registered app model id
if (!GetDefaultBrowserAppModelID(appModelID, (sizeof(appModelID)/sizeof(WCHAR)))) {
Fail(L"GetDefaultBrowserAppModelID failed.");
activateMgr->Release();
return false;
}
Log(L"App model id='%s'", appModelID);
// Hand off focus rights to the out-of-process activation server. Without
// this the metro interface won't launch.
hr = CoAllowSetForegroundWindow(activateMgr, NULL);
if (FAILED(hr)) {
Fail(L"CoAllowSetForegroundWindow result %X", hr);
activateMgr->Release();
return false;
}
Log(L"Harness process id: %d", GetCurrentProcessId());
// Because we can't pass command line args, we store params in a
// tests.ini file in dist/bin which the browser picks up on launch.
char path[MAX_PATH];
if (!GetModuleFileNameA(NULL, path, MAX_PATH)) {
Fail(L"GetModuleFileNameA errorno=%d", GetLastError());
activateMgr->Release();
return false;
}
char* slash = strrchr(path, '\\');
if (!slash)
return false;
*slash = '\0'; // no trailing slash
CStringA testFilePath = path;
testFilePath += "\\tests.ini";
HANDLE hTestFile = CreateFileA(testFilePath, GENERIC_WRITE,
0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hTestFile == INVALID_HANDLE_VALUE) {
Fail(L"CreateFileA errorno=%d", GetLastError());
activateMgr->Release();
return false;
}
CStringA asciiParams = sAppParams;
if (!WriteFile(hTestFile, asciiParams, asciiParams.GetLength(), NULL, 0)) {
CloseHandle(hTestFile);
Fail(L"WriteFile errorno=%d", GetLastError());
activateMgr->Release();
return false;
}
FlushFileBuffers(hTestFile);
CloseHandle(hTestFile);
// Launch firefox
hr = activateMgr->ActivateApplication(appModelID, L"", AO_NOERRORUI, &processID);
if (FAILED(hr)) {
Fail(L"ActivateApplication result %X", hr);
activateMgr->Release();
return false;
}
Log(L"Activation succeeded. processid=%d", processID);
HANDLE child = OpenProcess(SYNCHRONIZE, FALSE, processID);
if (!child) {
Fail(L"Couldn't find child process. (%d)", GetLastError());
activateMgr->Release();
return false;
}
Log(L"Waiting on child process...");
MSG msg;
DWORD waitResult = WAIT_TIMEOUT;
while ((waitResult = WaitForSingleObject(child, 10)) != WAIT_OBJECT_0) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Log(L"Exiting.");
activateMgr->Release();
DeleteFileA(testFilePath);
return true;
}
int wmain(int argc, WCHAR* argv[])
{
CoInitialize(NULL);
int idx;
for (idx = 1; idx < argc; idx++) {
sAppParams.Append(argv[idx]);
sAppParams.Append(L" ");
}
sAppParams.Trim();
Log(L"args: '%s'", sAppParams);
Launch();
CoUninitialize();
return 0;
}
|