Module: Yamatanooroti::WindowsTestCaseModule

Included in:
WindowsTestCase
Defined in:
lib/yamatanooroti/windows.rb

Constant Summary collapse

DL =
Yamatanooroti::WindowsDefinition

Instance Method Summary collapse

Instance Method Details

#assert_screen(expected_lines) ⇒ Object



472
473
474
475
476
477
478
479
# File 'lib/yamatanooroti/windows.rb', line 472

def assert_screen(expected_lines)
  case expected_lines
  when Array
    assert_equal(expected_lines, @result)
  when String
    assert_equal(expected_lines, @result.join("\n").sub(/\n*\z/, "\n"))
  end
end

#closeObject



431
432
433
434
435
436
437
# File 'lib/yamatanooroti/windows.rb', line 431

def close
  sleep @wait
  # read first before kill the console process including output
  @result = retrieve_screen

  free_resources
end

#resultObject



468
469
470
# File 'lib/yamatanooroti/windows.rb', line 468

def result
  @result
end

#start_terminal(height, width, command, wait: 1, startup_message: nil) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/yamatanooroti/windows.rb', line 481

def start_terminal(height, width, command, wait: 1, startup_message: nil)
  @height = height
  @width = width
  @wait = wait
  @result = nil
  setup_console(height, width)
  launch(command.map{ |c| quote_command_arg(c) }.join(' '))
  case startup_message
  when String
    check_startup_message = ->(message) { message.start_with?(startup_message) }
  when Regexp
    check_startup_message = ->(message) { startup_message.match?(message) }
  end
  if check_startup_message
    loop do
      screen = retrieve_screen.join("\n").sub(/\n*\z/, "\n")
      break if check_startup_message.(screen)
      sleep @wait
    end
  end
end

#write(str) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/yamatanooroti/windows.rb', line 363

def write(str)
  sleep @wait
  str.tr!("\n", "\r")
  records = Fiddle::Pointer.malloc(DL::INPUT_RECORD_WITH_KEY_EVENT.size * str.size * 2, DL::FREE)
  str.chars.each_with_index do |c, i|
    record_index = i * 2
    r = DL::INPUT_RECORD_WITH_KEY_EVENT.new(records + DL::INPUT_RECORD_WITH_KEY_EVENT.size * record_index)
    r.EventType = DL::KEY_EVENT
    r.bKeyDown = 1
    r.wRepeatCount = 0
    r.wVirtualKeyCode = 0
    r.wVirtualScanCode = 0
    r.UnicodeChar = c.unpack('U').first
    r.dwControlKeyState = 0
    record_index = i * 2 + 1
    r = DL::INPUT_RECORD_WITH_KEY_EVENT.new(records + DL::INPUT_RECORD_WITH_KEY_EVENT.size * record_index)
    r.EventType = DL::KEY_EVENT
    r.bKeyDown = 0
    r.wRepeatCount = 0
    r.wVirtualKeyCode = 0
    r.wVirtualScanCode = 0
    r.UnicodeChar = c.unpack('U').first
    r.dwControlKeyState = 0
  end
  written_size = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DWORD, DL::FREE)
  r = DL.WriteConsoleInputW(DL.GetStdHandle(DL::STD_INPUT_HANDLE), records, str.size * 2, written_size)
  error_message(r, 'WriteConsoleInput')
end