Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ require("./tests/java-array-test");
require("./tests/field-access-test");
require("./tests/byte-buffer-test");
require("./tests/dex-interface-implementation");
require("./tests/testClassForNameDiscovery");
require("./tests/testInterfaceImplementation");
require("./tests/testRuntimeImplementedAPIs");
require("./tests/testsInstanceOfOperator");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
describe("Tests Class.forName discovery of runtime generated classes", function () {

// Android framework components (e.g. FragmentFactory) resolve classes with
// Class.forName(className, false, context.getClassLoader()). Runtime generated
// proxies must be discoverable through the app's class loader, otherwise
// framework lookups crash with ClassNotFoundException (see issue #1962 / PR #1951).
//
// The extend calls below are built dynamically so the static binding generator
// cannot pre-generate the proxies and DexFactory.resolveClass takes the runtime
// generation + parent class loader injection path.
var ext = "ex" + "tend";

// the app's PathClassLoader — the same loader the framework uses,
// e.g. in FragmentFactory.loadFragmentClass via context.getClassLoader()
var appClassLoader = com.tns.Runtime.class.getClassLoader();

it("When_extending_a_class_at_runtime_it_should_be_discoverable_through_the_app_class_loader", function () {
var MyObject = java.lang.Object[ext]("ClassForNameDiscoveryObject", {
toString: function () {
return "discoverable";
}
});

var instance = new MyObject();
var className = instance.getClass().getName();

var found = java.lang.Class.forName(className, false, appClassLoader);

expect(found.getName()).toBe(className);
expect(found.equals(instance.getClass())).toBe(true);
});

it("When_implementing_an_interface_at_runtime_it_should_be_discoverable_through_the_app_class_loader", function () {
var MyRunnable = java.lang.Runnable[ext]("ClassForNameDiscoveryRunnable", {
run: function () {
}
});

var instance = new MyRunnable();
var className = instance.getClass().getName();

var found = java.lang.Class.forName(className, false, appClassLoader);

expect(found.getName()).toBe(className);
expect(found.equals(instance.getClass())).toBe(true);
});

it("When_a_runtime_generated_class_is_instantiated_through_reflection_it_should_dispatch_to_the_JS_implementation", function () {
var MyObject = java.lang.Object[ext]("ClassForNameDiscoveryInstantiated", {
toString: function () {
return "created via reflection";
}
});

// make sure the implementation object is registered before Java constructs an instance
var instance = new MyObject();
var className = instance.getClass().getName();

// FragmentFactory resolves the class by name and instantiates it through reflection
var found = java.lang.Class.forName(className, false, appClassLoader);
var created = found.getDeclaredConstructor().newInstance();

expect(created.toString()).toBe("created via reflection");
});
});
43 changes: 43 additions & 0 deletions test-app/app/src/main/assets/app/tests/testNativeTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,49 @@ describe('native timer', () => {
done();
});
});
// these specs schedule from a java-posted runnable so they run outside any
// timer callback: jasmine chains specs through timer callbacks, and when
// the runtime is built with NS_TIMERS_NESTING_CLAMP the nesting clamp
// (>=5 levels -> 4ms minimum) would otherwise make setTimeout(0)
// legitimately lose to a postDelayed(0)
it('preserves order with java handler posts', (done) => {
const order = [];
const handler = new android.os.Handler(android.os.Looper.myLooper());
handler.post(new java.lang.Runnable({
run: () => {
setTimeout(() => order.push(1));
handler.postDelayed(new java.lang.Runnable({ run: () => order.push(2) }), 0);
setTimeout(() => order.push(3));
setTimeout(() => {
expect(order.join(',')).toBe('1,2,3');
done();
}, 100);
}
}));
});

it('interleaves many timers with a java handler post', (done) => {
const order = [];
const handler = new android.os.Handler(android.os.Looper.myLooper());
handler.post(new java.lang.Runnable({
run: () => {
for (let i = 0; i < 50; i++) {
setTimeout(() => order.push('t'));
}
handler.postDelayed(new java.lang.Runnable({ run: () => order.push('j') }), 0);
for (let i = 0; i < 50; i++) {
setTimeout(() => order.push('t'));
}
setTimeout(() => {
// the java runnable must land exactly between the two timer batches
expect(order.indexOf('j')).toBe(50);
expect(order.length).toBe(101);
done();
}, 100);
}
}));
});

it('frees up resources after complete', (done) => {
let timeout = 0;
let interval = 0;
Expand Down
18 changes: 18 additions & 0 deletions test-app/app/src/main/java/com/tns/AndroidJsV8Inspector.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class AndroidJsV8Inspector {

protected native final void dispatchMessage(String message);

private native String handleMessageOnSocketThread(String message);

private Handler mainHandler;

private final Object debugBrkLock;
Expand Down Expand Up @@ -294,6 +296,22 @@ protected void onMessage(final NanoWSD.WebSocketFrame message) {
Log.d("V8Inspector", "To dbg backend: " + message.getTextPayload() + " ThreadId:" + Thread.currentThread().getId());
}

// Network.loadNetworkResource / IO.read / IO.close are served from
// disk on this thread so source maps load even while the isolate is
// paused at a breakpoint or busy running JS; Debugger.pause schedules
// a V8 interrupt and still flows through the queue.
String fastPathResponse = handleMessageOnSocketThread(message.getTextPayload());
if (fastPathResponse != null) {
try {
send(fastPathResponse);
} catch (IOException e) {
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
return;
}

inspectorMessages.offer(message.getTextPayload());

if (!AndroidJsV8Inspector.ReadyToProcessMessages.get()) {
Expand Down
Loading
Loading