Skip to content

Commit 8c19a19

Browse files
committed
Browser: Window: Added test for animation frame.
Moved MessagePort from Browser to Core since Node.js has it too.
1 parent 66259b4 commit 8c19a19

10 files changed

Lines changed: 29 additions & 20 deletions

File tree

‎Browser/src/Test/TestMyWindow.st‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,20 @@ onCanceledCallback
1818
self assert: [ false ].
1919
!
2020
testAnimation
21-
"Todo: Test this with Animation class:
22-
requestAnimationFrame: callback
23-
cancelAnimationFrame: requestId"
21+
| window requestId |
22+
window := Window default.
23+
requestId := window requestAnimationFrame: [ :time | self onAnimationFrame: time ].
24+
self assert: [ requestId class = Integer ].
25+
requestId := window requestAnimationFrame: [ :time | self onCanceledAnimationFrame: time ].
26+
"Cancel previous request before it executes."
27+
window cancelAnimationFrame: requestId.
28+
!
29+
onAnimationFrame: time
30+
self assert: [ ( time class = Float ) & ( time > 0 ) ].
31+
!
32+
onCanceledAnimationFrame: time
33+
"This method should not get called."
34+
self assert: [ false ].
2435
!
2536
testOtherMethods
2637
| window element |

‎Smalltalk/Browser/Window/Window.st‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ cancelIdleCallback: handle
174174
"Animation."
175175

176176
requestAnimationFrame: callback
177-
INLINE 'this.js.requestAnimationFrame( callback.js )'.
177+
"Callback should have 1 argument: the frame timestamp in milliseconds.
178+
Returns the request id, to be used with cancelAnimationFrame:."
179+
| jsCallback |
180+
jsCallback := [ :jsTime | callback value: ( Float fromJs: jsTime ) ].
181+
^ Integer fromJs: INLINE 'this.js.requestAnimationFrame( jsCallback.js )'.
178182
!
179183
cancelAnimationFrame: requestId
180184
INLINE 'this.js.cancelAnimationFrame( requestId.js )'.

‎Smalltalk/Browser/Workers/Test/MyTestSharedWorker.st‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ start
1111
!
1212
onConnect: event
1313
| port |
14-
port := ( event portsAs: MessagePort ) first.
14+
port := event ports first.
1515
port onMessage: [ :messageEvent | self onMessage: messageEvent port: port ].
1616
!
1717
onMessage: event port: port

‎Smalltalk/Browser/Workers/Test/MyTestWorker.st‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ onMessage: event
1818

1919
"Reply on the port transferred with the message."
2020
message = 'Port' ifTrue: [
21-
port := ( event portsAs: MessagePort ) first.
21+
port := event ports first.
2222
port postMessage: 'Hello, port!'.
2323
port close ].
2424

Smalltalk/Browser/Workers/MessageChannel.st renamed to Smalltalk/Core/Event/MessageChannel.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CLASS MessageChannel EXTENDS JsObject MODULE Browser CLASSVARS '' VARS ''
1+
CLASS MessageChannel EXTENDS JsObject MODULE Core CLASSVARS '' VARS ''
22

33
"Implements JS MessageChannel: a channel with 2 entangled MessagePorts.
44
A message posted on one port is received on the other port."

‎Smalltalk/Core/Event/MessageEvent.st‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ origin
2424
^ String fromJs: INLINE 'this.js.origin'.
2525
!
2626
ports
27-
"The ports are MessagePorts of the platform (Browser or Node).
28-
Use portsAs: to get them as instances of a specific ST class."
29-
^ self portsAs: EventTarget.
30-
!
31-
portsAs: elementClass
32-
^ Array fromJs: INLINE 'this.js.ports' elementClass: elementClass.
27+
"The MessagePorts transferred with the message."
28+
^ Array fromJs: INLINE 'this.js.ports' elementClass: MessagePort.
3329
!
3430
source
3531
"A MessagePort, WindowProxy or ServiceWorker, or nil."
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CLASS MessagePort EXTENDS EventTarget MODULE Browser CLASSVARS '' VARS ''
1+
CLASS MessagePort EXTENDS EventTarget MODULE Core CLASSVARS '' VARS ''
22

33
"Implements JS MessagePort: one of the two ports of a MessageChannel."
44

Smalltalk/Browser/Workers/Test/TestMessageChannel.st renamed to Smalltalk/Core/Event/Test/TestMessageChannel.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CLASS TestMessageChannel EXTENDS Test MODULE TestBrowser CLASSVARS '' VARS ''
1+
CLASS TestMessageChannel EXTENDS Test MODULE TestCore CLASSVARS '' VARS ''
22

33
METHODS
44

‎Smalltalk/Core/Event/Test/TestMessageEvent.st‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ testOptions
2626
!
2727
testPortsAndSource
2828
| jsChannel options event jsPort jsSource |
29-
"MessageChannel is not in Core, but its JS class exists in both Browser and Node."
30-
jsChannel := INLINE 'new MessageChannel()'.
29+
jsChannel := MessageChannel new js.
3130
options := JsObject fromJs: INLINE '{ ports: [ jsChannel.port1 ], source: jsChannel.port2 }'.
3231
event := MessageEvent type: 'message' options: options.
3332
self assert: [ event ports length = 1 ].
34-
self assert: [ event ports first class = EventTarget ].
33+
self assert: [ event ports first class = MessagePort ].
3534
jsPort := event ports first js.
3635
self assert: [ Boolean fromJs: INLINE 'jsPort === jsChannel.port1' ].
37-
self assert: [ ( event portsAs: JsObject ) first class = JsObject ].
3836
self assert: [ event source class = EventTarget ].
3937
jsSource := event source js.
4038
self assert: [ Boolean fromJs: INLINE 'jsSource === jsChannel.port2' ].

Smalltalk/Browser/Workers/Test/TestMessagePort.st renamed to Smalltalk/Core/Event/Test/TestMessagePort.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CLASS TestMessagePort EXTENDS Test MODULE TestBrowser CLASSVARS '' VARS ''
1+
CLASS TestMessagePort EXTENDS Test MODULE TestCore CLASSVARS '' VARS ''
22

33
"Most MessagePort tests are async: the asserts run in the message handlers."
44

0 commit comments

Comments
 (0)