Skip to content

Commit

Permalink
test: update
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Jul 20, 2022
1 parent 03ad2ce commit a150229
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 6 additions & 4 deletions packages/win32-api/test/70.user32.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
DStruct as DS,
retrieveStructFromPtrAddress,
StructFactory,
genUcsBufferFrom,
ucsBufferToString,
} from '../src/index.js'

import { calcLpszClassNotepad, calcLpszNotepad } from './config.unittest.js'
Expand All @@ -40,7 +42,7 @@ describe(fileShortPath(import.meta.url), () => {
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')

// Change title of the Calculator
await user32.SetWindowTextW(hWnd, Buffer.from(title + '\0', 'ucs2'))
await user32.SetWindowTextW(hWnd, genUcsBufferFrom(title))

const len = title.length
assert(len > 0)
Expand Down Expand Up @@ -83,15 +85,15 @@ describe(fileShortPath(import.meta.url), () => {
assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')

// Change title of the Calculator
user32Sync.SetWindowTextW(hWnd, Buffer.from(title + '\0', 'ucs2'))
user32Sync.SetWindowTextW(hWnd, genUcsBufferFrom(title))

const len = title.length + 1
assert(len > 0)
const buf = Buffer.alloc(len * 2)
let str = ''

user32Sync.GetWindowTextW(hWnd, buf, len)
str = buf.toString('ucs2').replace(/\0+$/, '')
str = ucsBufferToString(buf)
assert(str === title, `title should be changed to ${title}, bug got ${str}`)

const point = StructFactory<M.POINT>(DS.POINT)
Expand Down Expand Up @@ -138,7 +140,7 @@ function createEnumWinProc(): M.WNDENUMPROC {
const maxLen = 127
const buf = Buffer.alloc(maxLen * 2)
const len = user32Sync.GetWindowTextW(hWnd, buf, maxLen)
const name = buf.toString('ucs2').replace(/\0+$/, '')
const name = ucsBufferToString(buf)
name && console.log(name, len)

if (len && name === title) {
Expand Down
9 changes: 5 additions & 4 deletions packages/win32-api/test/71.find-calc-async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import assert from 'node:assert/strict'
import { fileShortPath } from '@waiting/shared-core'
import { sleep } from 'zx'

import { genUcsBufferFrom, ucsBufferToString } from '../src/index.js'
import * as UP from '../src/index.user32.js'

import { calcLpszWindow } from './config.unittest.js'
Expand Down Expand Up @@ -68,12 +69,12 @@ async function findNSetWinTitleAsync(): Promise<void> {
const hWnd = await user32.FindWindowExW(0, 0, null, calcLpszWindow)

assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
const ret = await user32.SetWindowTextW(hWnd, Buffer.from(title + '\0', 'ucs2'))
const ret = await user32.SetWindowTextW(hWnd, genUcsBufferFrom(title))
assert(ret, 'SetWindowTextW() failed')

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

Expand All @@ -87,11 +88,11 @@ async function findNSetWinTitleAsyncPartial(): Promise<void> {

assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window')
// Change title of the Calculator
await u32.SetWindowTextW(hWnd, Buffer.from(title + '\0', 'ucs2'))
await u32.SetWindowTextW(hWnd, genUcsBufferFrom(title))

const buf = Buffer.alloc(len * 2)
await u32.GetWindowTextW(hWnd, buf, len + 1)
const str = buf.toString('ucs2').replace(/\0+$/, '')
const str = ucsBufferToString(buf)
assert(str === title, `title should be changed to ${title}, bug got ${str}`)
}

0 comments on commit a150229

Please sign in to comment.