Skip to content

Commit

Permalink
test(win32-api): update
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Jun 13, 2024
1 parent 36aad37 commit 615b9ad
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 76 deletions.
30 changes: 15 additions & 15 deletions packages/win32-api/test/70.user32.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '../src/index.js'

import { calcLpszClassNotepad, calcLpszNotepad } from './config.unittest.js'
import { user32, user32Sync } from './helper.js'
import { assertsHwnd, user32, user32Sync } from './helper.js'


const tmpMap = new Map<number | string | bigint, M.POINT>()
Expand All @@ -39,7 +39,7 @@ describe(fileShortPath(import.meta.url), () => {

const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null)
assert(hWnd)
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
assertsHwnd(hWnd)

// Change title of the Calculator
await user32.SetWindowTextW(hWnd, ucsBufferFrom(title))
Expand All @@ -54,19 +54,19 @@ describe(fileShortPath(import.meta.url), () => {
point.x = 101
point.y = Math.round(Math.random() * 1000000)

const adress = point.ref().address()
tmpMap.delete(adress)
const address = point.ref().address()
tmpMap.delete(address)

await enumWindows(enumWindowsProc, adress)
console.log({ adress })
await enumWindows(enumWindowsProc, address)
console.log({ address })

const point2 = tmpMap.get(adress)
const point2 = tmpMap.get(address)
assert(point2, 'point2 should be got')
if (point2) {
assert(point.x === point2.x)
assert(point.y === point2.y)
}
tmpMap.delete(adress)
tmpMap.delete(address)

child.kill()
console.log({ killed: child.killed })
Expand All @@ -82,7 +82,7 @@ describe(fileShortPath(import.meta.url), () => {
console.log(new Date().toLocaleTimeString())

const hWnd = user32Sync.FindWindowExW(0, 0, calcLpszClassNotepad, null)
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
assertsHwnd(hWnd)

// Change title of the Calculator
user32Sync.SetWindowTextW(hWnd, ucsBufferFrom(title))
Expand All @@ -100,19 +100,19 @@ describe(fileShortPath(import.meta.url), () => {
point.x = 101
point.y = Math.round(Math.random() * 1000000)

const adress = point.ref().address()
tmpMap.delete(adress)
const address = point.ref().address()
tmpMap.delete(address)

await enumWindows(enumWindowsProc, adress)
console.log({ adress })
await enumWindows(enumWindowsProc, address)
console.log({ address })

const point2 = tmpMap.get(adress)
const point2 = tmpMap.get(address)
assert(point2, 'point2 should be got')
if (point2) {
assert(point.x === point2.x)
assert(point.y === point2.y)
}
tmpMap.delete(adress)
tmpMap.delete(address)

child.kill()
console.log({ killed: child.killed })
Expand Down
10 changes: 5 additions & 5 deletions packages/win32-api/test/71.find-calc-async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ucsBufferFrom, ucsBufferToString } from '../src/index.js'
import * as UP from '../src/index.user32.js'

import { calcLpszWindow } from './config.unittest.js'
import { user32, user32Sync } from './helper.js'
import { assertsHwnd, user32, user32Sync } from './helper.js'


describe.skip(fileShortPath(import.meta.url), () => {
Expand All @@ -23,7 +23,7 @@ describe.skip(fileShortPath(import.meta.url), () => {
console.log(new Date().toLocaleTimeString())

const hWnd = await user32FindWindowEx(0, 0, null, calcLpszWindow)
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
assertsHwnd(hWnd)
child.kill()
})

Expand All @@ -41,7 +41,7 @@ describe.skip(fileShortPath(import.meta.url), () => {
assert(false, err.message)
}

assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
assertsHwnd(hWnd)
child.kill()
done()
})
Expand Down Expand Up @@ -70,7 +70,7 @@ async function findNSetWinTitleAsync(): Promise<void> {
const len = title.length
const hWnd = await user32.FindWindowExW(0, 0, null, ucsBufferFrom(calcLpszWindow))

assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
assertsHwnd(hWnd)
const ret = await user32.SetWindowTextW(hWnd, ucsBufferFrom(title))
assert(ret, 'SetWindowTextW() failed')

Expand All @@ -88,7 +88,7 @@ async function findNSetWinTitleAsyncPartial(): Promise<void> {
const len = title.length
const hWnd = await u32.FindWindowExW(0, 0, null, ucsBufferFrom(calcLpszWindow))

assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
assertsHwnd(hWnd)
// Change title of the Calculator
await u32.SetWindowTextW(hWnd, ucsBufferFrom(title))

Expand Down
12 changes: 6 additions & 6 deletions packages/win32-api/test/72.find-notepad-async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ucsBufferToString, ucsBufferFrom } from '../src/index.js'
import * as UP from '../src/index.user32.js'

import { calcLpszClassNotepad, calcLpszNotepad } from './config.unittest.js'
import { destroyWin, user32, user32Sync } from './helper.js'
import { assertsHwnd, destroyWin, user32, user32Sync } from './helper.js'



Expand All @@ -24,7 +24,7 @@ describe(fileShortPath(import.meta.url), () => {
console.log(new Date().toLocaleTimeString())

const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null)
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
assertsHwnd(hWnd)
await destroyWin(hWnd)
child.kill()
})
Expand All @@ -42,7 +42,7 @@ describe(fileShortPath(import.meta.url), () => {
assert(false, err.message)
}

assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no notepad window')
assertsHwnd(hWnd)
child.kill()
done()
})
Expand Down Expand Up @@ -74,13 +74,13 @@ async function findNSetWinTitleAsync(): Promise<void> {
const size = len + 1
const hWnd = await user32.FindWindowExW(0, 0, calcLpszClassNotepad, null)

assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
assertsHwnd(hWnd)
const ret = await user32.SetWindowTextW(hWnd, ucsBufferFrom(title))
assert(ret, 'SetWindowTextW() failed')

const buf = Buffer.alloc(size * 2)
await user32.GetWindowTextW(hWnd, buf, size)
const str = buf.toString('ucs2').replace(/\0+$/, '')
const str = buf.toString('ucs2').replace(/\0+$/u, '')
assert(str === title.trim(), `title should be changed to "${title}", bug got "${str}"`)
}

Expand All @@ -93,7 +93,7 @@ async function findNSetWinTitleAsyncPartial(): Promise<void> {
const size = len + 1
const hWnd = await u32.FindWindowExW(0, 0, calcLpszClassNotepad, null)

assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
assertsHwnd(hWnd)
// Change title of the Calculator
await u32.SetWindowTextW(hWnd, ucsBufferFrom(title))

Expand Down
13 changes: 2 additions & 11 deletions packages/win32-api/test/gdi32/202.CreateCompatibleBitmap.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import assert from 'node:assert/strict'

import { fileShortPath } from '@waiting/shared-core'
import ref from 'ref-napi'
import { sleep } from 'zx'

import * as CS from '../../src/index.consts.js'
import {
DModel as M,
DTypes as W,
DStruct as DS,
StructFactory,
} from '../../src/index.js'
import { gdi32 } from '../helper.js'
import { assertsHwnd, gdi32 } from '../helper.js'


describe(fileShortPath(import.meta.url), () => {
Expand All @@ -23,7 +14,7 @@ describe(fileShortPath(import.meta.url), () => {

const hBitmap = await gdi32.CreateCompatibleBitmap(hdc, 100, 100)
console.log({ hBitmap })
assert(hBitmap > 0)
assertsHwnd(hBitmap)
})
})
})
Expand Down
37 changes: 27 additions & 10 deletions packages/win32-api/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export async function createWindow(wndProc: M.WNDPROC): Promise<M.HWND> {
ref.NULL,
)

assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'CreateWindowExW() failed')
assertsHwnd(hWnd)

// @FIXME timeout
// await user32.ShowWindow(hWnd, 1)
await user32.UpdateWindow(hWnd)
Expand All @@ -122,22 +123,25 @@ export async function createWindow(wndProc: M.WNDPROC): Promise<M.HWND> {
}

export async function changeTitle(hWnd: M.HANDLE, title: string): Promise<string> {
if (typeof hWnd === 'number' && hWnd > 0
|| Buffer.isBuffer(hWnd) && ! ref.isNull(hWnd) && ref.address(hWnd)
) {
if (typeof hWnd === 'number' && hWnd > 0) {
// Change title of the Calculator
const res = await user32.SetWindowTextW(hWnd, Buffer.from(title + '\0', 'ucs2'))
if (! res) {
throw new Error('changeTitle failed')
}
else {
if (res) {
const tt = await getTitle(hWnd)
return tt
}
throw new Error('changeTitle failed')
}
else {
return ''
else if (Buffer.isBuffer(hWnd) && ! ref.isNull(hWnd) && ref.address(hWnd)) {
// Change title of the Calculator
const res = await user32.SetWindowTextW(hWnd, Buffer.from(title + '\0', 'ucs2'))
if (res) {
const tt = await getTitle(hWnd)
return tt
}
throw new Error('changeTitle failed')
}
return ''
}


Expand All @@ -158,3 +162,16 @@ export async function getTitle(handle: M.HANDLE): Promise<string> {
export function destroyWin(hWnd: M.HWND): Promise<M.BOOL> {
return user32.DestroyWindow(hWnd)
}


export function assertsHwnd(hWnd: M.HWND | string | number | bigint): void {
if (typeof hWnd === 'string') {
assert(hWnd.length > 0, 'found no window')
}
else if (typeof hWnd === 'number' || typeof hWnd === 'bigint') {
assert(hWnd > 0, 'found no window')
}
else {
assert(false, 'found no window')
}
}
11 changes: 2 additions & 9 deletions packages/win32-api/test/user32/201.FindWindowEx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ import assert from 'node:assert/strict'
import { spawn } from 'node:child_process'

import { fileShortPath } from '@waiting/shared-core'
import ref from 'ref-napi'
import { sleep } from 'zx'

import * as CS from '../../src/index.consts.js'
import { user32FindWindowEx } from '../../src/index.fun.js'
import {
DModel as M,
DTypes as W,
DStruct as DS,
StructFactory,
} from '../../src/index.js'
import { calcLpszNotepad } from '../config.unittest.js'
import { assertsHwnd } from '../helper.js'


describe(fileShortPath(import.meta.url), () => {
Expand All @@ -25,7 +18,7 @@ describe(fileShortPath(import.meta.url), () => {

const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null)
assert(hWnd)
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
assertsHwnd(hWnd)

child.kill()
})
Expand Down
4 changes: 2 additions & 2 deletions packages/win32-api/test/user32/54.GetWindowRect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
StructFactory,
} from '../../src/index.js'
import { calcLpszWindow } from '../config.unittest.js'
import { user32, destroyWin } from '../helper.js'
import { user32, destroyWin, assertsHwnd } from '../helper.js'


describe(fileShortPath(import.meta.url), () => {
Expand All @@ -23,7 +23,7 @@ describe(fileShortPath(import.meta.url), () => {
const rect = StructFactory<M.RECT>(DS.RECT)

const hWnd = await user32FindWindowEx(0, 0, null, calcLpszWindow)
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'invalid hWnd')
assertsHwnd(hWnd)

const ret = await user32.GetWindowRect(hWnd, rect.ref())
assert(ret !== 0)
Expand Down
6 changes: 3 additions & 3 deletions packages/win32-api/test/user32/55.SendMessageW.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
StructFactory,
} from '../../src/index.js'
import { calcLpszWindow } from '../config.unittest.js'
import { user32, destroyWin } from '../helper.js'
import { user32, destroyWin, assertsHwnd } from '../helper.js'


describe(fileShortPath(import.meta.url), () => {
Expand All @@ -30,7 +30,7 @@ describe(fileShortPath(import.meta.url), () => {
})

const hWnd = await user32FindWindowEx(0, 0, null, calcLpszWindow)
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0)
assertsHwnd(hWnd)

let ret = await user32.SendMessageW(hWnd, CS.WM_COMMAND, 1000, 3)
assert(ret === 0)
Expand All @@ -44,7 +44,7 @@ describe(fileShortPath(import.meta.url), () => {
await sleep(1500)

const hWnd = await user32FindWindowEx(0, 0, null, calcLpszWindow)
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0)
assertsHwnd(hWnd)

const copyData = StructFactory<M.COPYDATASTRUCT>(DS.COPYDATASTRUCT)
const msgBuff = Buffer.from('foo\0', 'ucs2')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
calcLpszNotepad,
calcLpszNotepadEdit,
} from '../config.unittest.js'
import { user32, destroyWin } from '../helper.js'
import { user32, destroyWin, assertsHwnd } from '../helper.js'


describe(fileShortPath(import.meta.url), () => {
Expand All @@ -22,10 +22,10 @@ describe(fileShortPath(import.meta.url), () => {
await sleep(1500)

const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null)
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0)
assertsHwnd(hWnd)

const hWndEdit = await user32FindWindowEx(hWnd, 0, calcLpszNotepadEdit, null)
assert((typeof hWndEdit === 'string' && hWndEdit.length > 0) || hWndEdit > 0)
assertsHwnd(hWndEdit)

const txt = 'Hello World.' + Math.random().toString()
const msgBuff = ucsBufferFrom(txt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
calcLpszNotepad,
calcLpszNotepadEdit,
} from '../config.unittest.js'
import { user32, destroyWin } from '../helper.js'
import { user32, destroyWin, assertsHwnd } from '../helper.js'


describe(fileShortPath(import.meta.url), () => {
Expand All @@ -22,10 +22,10 @@ describe(fileShortPath(import.meta.url), () => {
await sleep(1500)

const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null)
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0)
assertsHwnd(hWnd)

const hWndEdit = await user32FindWindowEx(hWnd, 0, calcLpszNotepadEdit, null)
assert((typeof hWndEdit === 'string' && hWndEdit.length > 0) || hWndEdit > 0)
assertsHwnd(hWndEdit)

const txt = 'Hello World.' + Math.random().toString()
const msgBuff = ucsBufferFrom(txt)
Expand Down
Loading

0 comments on commit 615b9ad

Please sign in to comment.