[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

needsWork-test[jx8-android] 070 - Dynamic proxy


self print: nil.
SwikiCodeRobot @> #testJx8AndroidDynamicProxy

"Prepare RunInterface"
(Smalltalk includesKey: #RunInterface) ifTrue: [
	^self print: 'OK: RunInterface ready'; cancelFileIn
]!

JavaObject
	buildSubclass: #RunInterface
	in: 'org.aleReimondo.test'!
JavaObject
	buildSubclass: #ListenerTester
	in: 'org.aleReimondo.test'!

RunInterface buildFunctions: #(
	#(run0Args #(_interface))
	#(run1Args #(_interface))
	#(runWithReturn #(_interface))
	#(runInAnotherThread #(_interface))
	#(runEquals #(_interface))
	#(runHashCode #(_interface))
	#(setInstance #(_interface))
	#(runEqualsInstance #(_interface))
	#(runToString #(_interface))
)!

ListenerTester buildFunctions: #(
	raiseEvent
	#(setListener: #(_listener))
)!




"0 Arguments"
| count proxy |
count := 0.
proxy := Java "current"
	newProxy: 'org.aleReimondo.test.RunInterface$Interface0Arg'
	functions: #run -> [ self print: 'count=',count. count := count + 1 ].
RunInterface instance run0Args: proxy. 
count = 2 ifFalse: [ self error: 'Wrong result'].


"1 Arguments"
| data proxy |
data := ''.
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$Interface1Arg'
	functions: #run -> [:str| data := data ,str ].
RunInterface instance run1Args: proxy.
data = #test1test1 ifFalse: [ self error: 'Wrong result'].


"1 Arguments with return data"
| proxy result |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [:i| i + 1 ].
RunInterface instance runWithReturn: proxy.
result = 43 ifFalse: [ self error: 'Wrong result (',result toString,')'].


"Listener test"
| data proxy |
data := ''.
proxy := Java
	newProxy: 'org.aleReimondo.test.ListenerInterface'
	functions: #onEvent -> [:list :runtime| data := #onEvent ].
ListenerTester instance
	listener: proxy;
	raiseEvent.
data = #onEvent ifFalse: [ self error: 'Wrong result'].


"thread/waitForThread"
| count proxy thread timeout waitForThread |
count := 0.
proxy := Java
	newProxy: 'java.lang.Runnable'
	functions: #run -> [ count := count + 1 ].
thread := JavaThread target: proxy.
thread start.
timeout := 50.
waitForThread := [
	count ~= 1 ifTrue: [
		count < 0 ifTrue: [ self error: 'Timeout - thread/waitForThread' ].
		waitForThread valueDeferred: 100.
	]
].
waitForThread value


"thread issue #143"
| proxy |
self error: 'Test excluded - can CRASH (DNU on java side AFTER this test is finished)'.
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [:i| i - 1 ].
RunInterface instance handle
	#runInAnotherThreadAsync: proxy
	callback: [:err :result|
		result = 45 ifFalse: [ self print: 'Wrong result in test "thread issue #143"'].
	]


"java equals()"
| proxy result |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [ ].
result := RunInterface instance runEquals: proxy.
result = false ifFalse: [ self error: 'Wrong result'].


"java equals() same instance"
| proxy result |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [ ].
result := RunInterface instance
	instance: proxy;
	runEquals: proxy.
result = true ifFalse: [ self error: 'Wrong result'].


"java equals() different instance"
| proxy other result |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [ ].
other := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [ ].
result := RunInterface instance
	instance: proxy;
	runEquals: other.
result = false ifFalse: [ self error: 'Wrong result'].


"js equals()"
| proxy result |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [:obj| true ].
result := RunInterface instance runEquals: proxy.
result = true ifFalse: [ self error: 'Wrong result'].


"java hashCode()"
| proxy h1 h2 hs |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [ ].
h1 := RunInterface instance runHashCode: proxy.
h2 := RunInterface instance runHashCode: proxy.
h1 = h2 ifFalse: [ self error: 'Wrong result'].
hs := JavaSystem identityHashCode: proxy json.
h1 = hs ifFalse: [ self error: 'Wrong hashCode'].


"js hashCode()"
| proxy result |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #hashCode -> [ 1234 ].
result := RunInterface instance runHashCode: proxy.
result = 1234 ifFalse: [ self error: 'Wrong result'].


"java toString()"
| proxy result |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #hashCode -> [ 1234 ].
result := RunInterface instance runToString: proxy.
result = '[object Object]' ifFalse: [ self error: 'Wrong result'].


"js toString()"
| proxy result hello |
hello := '[hello World]'.
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #toString -> [ hello ].
result := RunInterface instance runToString: proxy.
result = hello ifFalse: [ self error: 'Wrong result'].


"js string error"
| proxy result |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [ self error: #hello ].
[ RunInterface instance runWithReturn: proxy.
  self error: 'This should not happen'
] on: Error do: [:ex| self print: 'Ok - ' ,ex toString stream nextLine ].


"invocationHandler"
| proxy fn |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [:i | i + 2 ].
fn := (proxy handle basicAt: #invocationHandler) basicAt: #run.
(fn value: 42) = 44 ifFalse: [ self error: 'Wrong result'].


"unref"
| proxy |
proxy := Java
	newProxy: 'org.aleReimondo.test.RunInterface$InterfaceWithReturn'
	functions: #run -> [:i | i + 1 ].
proxy handle unref.
proxy handle unref.