Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avm2: Add support for focus-related events #16581

Merged
merged 17 commits into from
Jul 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests: Add avm2/focus_events_mixed_key_mouse test
This test verifies the behavior of focus-related events when
changing focus using keyboard and mouse interchangeably.
kjarosh committed Jul 25, 2024
commit 5a614e6413a87dce10b96e3b345bb9050701f04a
98 changes: 98 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,98 @@
package {

import flash.display.DisplayObject;
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 = "sprite1";
sprite1.x = 0;
sprite1.y = 100;
sprite1.tabEnabled = true;
sprite1.tabIndex = 1;
sprite2 = newSprite(0x00FF00);
sprite2.name = "sprite2";
sprite2.x = 100;
sprite2.y = 100;
sprite2.tabEnabled = true;
sprite2.tabIndex = 2;
sprite3 = newSprite(0x0000FF);
sprite3.name = "sprite3";
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 + ", focus: " + objectToString(stage.focus));
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 objectToString(obj: DisplayObject): String {
return "" + obj + ((obj != null) ? " (" + obj.name + ")" : "");
}

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;
}
}
}
42 changes: 42 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,42 @@
[
{ "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": 27 },
{ "type": "KeyUp", "key_code": 27 },
{ "type": "FocusLost" }
]
36 changes: 36 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,36 @@
#!/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', '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