Skip to content

Commit

Permalink
tests: Add avm2/focus_events_mixed_key_mouse test
Browse files Browse the repository at this point in the history
This test verifies the behavior of focus-related events when
changing focus using keyboard and mouse interchangeably.
  • Loading branch information
kjarosh committed Jul 13, 2024
1 parent be14f68 commit 8f9af75
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/tests/swfs/avm2/focus_events_mixed_key_mouse/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package {

import flash.display.InteractiveObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;

[SWF(width="300", height="200")]
public class Test extends MovieClip {
private var sprite1: Sprite;
private var sprite2: Sprite;
private var sprite3: Sprite;

private var logDirty: Boolean = true;

public function Test() {
super();

sprite1 = newSprite(0xFF0000);
sprite1.name = "spriteA";
sprite1.x = 0;
sprite1.y = 100;
sprite1.tabEnabled = true;
sprite1.tabIndex = 1;
sprite2 = newSprite(0x00FF00);
sprite2.name = "spriteA";
sprite2.x = 100;
sprite2.y = 100;
sprite2.tabEnabled = true;
sprite2.tabIndex = 2;
sprite3 = newSprite(0x0000FF);
sprite3.name = "spriteA";
sprite3.x = 200;
sprite3.y = 100;
sprite3.tabEnabled = true;

stage.addChild(sprite1);
stage.addChild(sprite2);
stage.addChild(sprite3);

stage.addEventListener("keyDown", function(evt:KeyboardEvent):void {
if (evt.keyCode == 27 && logDirty) {
logDirty = false;
trace("Escape pressed");
}
if (evt.keyCode == 9) {
trace("Tab pressed");
}
});

function eventListener(obj: InteractiveObject): Function {
return function(evt: Event): void {
var str;
if (evt is MouseEvent) {
str = evt.formatToString(
"MouseEvent", "type", "cancelable", "eventPhase",
"relatedObject", "ctrlKey", "altKey", "shiftKey");
} else {
str = evt.toString();
}
trace(" " + obj.name + ", " + evt.target.name + ": " + str);
logDirty = true;
}
}

for each (var obj: InteractiveObject in [
sprite1, sprite2, sprite3,
stage
]) {
obj.addEventListener("focusIn", eventListener(obj));
obj.addEventListener("focusOut", eventListener(obj));
obj.addEventListener("mouseDown", eventListener(obj));
obj.addEventListener("mouseUp", eventListener(obj));
obj.addEventListener("click", eventListener(obj));
obj.addEventListener("mouseFocusChange", eventListener(obj));
obj.addEventListener("keyFocusChange", eventListener(obj));
obj.addEventListener("rollOut", eventListener(obj));
obj.addEventListener("rollOver", eventListener(obj));
obj.addEventListener("mouseOver", eventListener(obj));
}
}

private function newSprite(color: int): Sprite {
var s:Sprite = new Sprite();
s.graphics.beginFill(color);
s.graphics.drawRect(0, 0, 100, 100);
s.graphics.endFill();
return s;
}
}
}
49 changes: 49 additions & 0 deletions tests/tests/swfs/avm2/focus_events_mixed_key_mouse/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyUp", "key_code": 27 },
{ "type": "MouseMove", "pos": [1, 101] },
{ "type": "MouseDown", "pos": [1, 101], "btn": "Left" },
{ "type": "MouseUp", "pos": [1, 101], "btn": "Left" },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyUp", "key_code": 27 },
{ "type": "MouseMove", "pos": [101, 101] },
{ "type": "MouseDown", "pos": [101, 101], "btn": "Left" },
{ "type": "MouseUp", "pos": [101, 101], "btn": "Left" },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyUp", "key_code": 27 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "MouseMove", "pos": [101, 101] },
{ "type": "MouseDown", "pos": [101, 101], "btn": "Left" },
{ "type": "MouseUp", "pos": [101, 101], "btn": "Left" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyUp", "key_code": 27 },
{ "type": "MouseMove", "pos": [201, 101] },
{ "type": "MouseDown", "pos": [201, 101], "btn": "Left" },
{ "type": "MouseUp", "pos": [201, 101], "btn": "Left" },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyUp", "key_code": 27 },
{ "type": "MouseMove", "pos": [1, 1] },
{ "type": "MouseDown", "pos": [1, 1], "btn": "Left" },
{ "type": "MouseUp", "pos": [1, 1], "btn": "Left" },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyUp", "key_code": 27 },
{ "type": "MouseMove", "pos": [1, 1] },
{ "type": "MouseDown", "pos": [1, 1], "btn": "Left" },
{ "type": "MouseUp", "pos": [1, 1], "btn": "Left" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyUp", "key_code": 27 },
{ "type": "FocusLost" }
]
37 changes: 37 additions & 0 deletions tests/tests/swfs/avm2/focus_events_mixed_key_mouse/input.json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3

coord_map = {
'void': [1, 1],
'sprite1': [1, 101],
'sprite2': [101, 101],
'sprite3': [201, 101],
}

click_seq = [
'tab', 'tab', 'esc',
'sprite1', 'tab', 'esc',
'sprite2', 'tab', 'esc',
'tab', 'sprite2', 'esc',
'sprite3', 'tab', 'esc',
'void', 'tab', 'esc',
'void', 'esc',
]

print('[')

for obj in click_seq:
if obj == 'esc':
print(f' {{ "type": "KeyDown", "key_code": 27 }},')
print(f' {{ "type": "KeyUp", "key_code": 27 }},')
continue
if obj == 'tab':
print(f' {{ "type": "KeyDown", "key_code": 9 }},')
print(f' {{ "type": "KeyUp", "key_code": 9 }},')
continue
pos = coord_map[obj]
print(f' {{ "type": "MouseMove", "pos": {pos} }},')
print(f' {{ "type": "MouseDown", "pos": {pos}, "btn": "Left" }},')
print(f' {{ "type": "MouseUp", "pos": {pos}, "btn": "Left" }},')

print(f' {{ "type": "FocusLost" }}')
print(']')
Loading

0 comments on commit 8f9af75

Please sign in to comment.