ReviewOS

also looking at this

craft-native/craft

feat(android): the first listener shim, and network monitoring on it — 61 → 59

#183
Merged glennmichael123 wants to merge feat/android-network-watch into main
5 files +246 -13
packages/zig/src/bridge_android_network.zigmodified+45-0
Changes to packages/zig/src/bridge_android_network.zig
@@ -38,8 +38,18 @@ const jobject = jni.jobject;
3838
3939pub const A = struct {
4040 pub const get_network_status = "getNetworkStatus";
41 pub const start_network_monitoring = "startNetworkMonitoring";
42 pub const stop_network_monitoring = "stopNetworkMonitoring";
4143};
4244
45/// The global the injected JS assigns for network changes.
46///
47/// Not a promise: `onNetworkChange(cb)` stores the callback once and every
48/// change calls it, so this global outlives any single reply — which is why
49/// the guard in front of it matters more here than elsewhere. A change
50/// arriving before the page has registered is dropped rather than throwing.
51pub const change_global = "_craftNetworkChangeCallback";
52
4353/// The transports the Kotlin's `when` distinguishes, in its order.
4454///
4555/// Order is contract: `when` takes the first match, so a connection that is
@@ -152,6 +162,14 @@ pub fn read(j: Jni, activity: jobject) !NetworkStatus {
152162 return .{ .connected = true, .transport = .none };
153163}
154164
165/// `NetworkCapabilities.NET_CAPABILITY_INTERNET`, the one capability the
166/// shim's `NetworkRequest` asks for.
167///
168/// Written down rather than read, because it is the Kotlin holder that builds
169/// the request — this constant is here only so the test can say what the
170/// holder is expected to have asked for.
171pub const net_capability_internet: i32 = 12;
172
155173// =============================================================================
156174// Tests
157175// =============================================================================
@@ -351,3 +369,30 @@ test "null capabilities is not connected, and an unknown transport still is" {
351369 try testing.expectEqual(true, odd.connected);
352370 try testing.expectEqual(Transport.none, odd.transport);
353371}
372
373test "the monitoring pair names itself as the shim does" {
374 try testing.expectEqualStrings("startNetworkMonitoring", A.start_network_monitoring);
375 try testing.expectEqualStrings("stopNetworkMonitoring", A.stop_network_monitoring);
376}
377
378test "a change is delivered to the callback the page registered, not a promise" {
379 // `onNetworkChange(cb)` assigns this once and every change calls it. A
380 // promise global is assigned per call and consumed; this one is not, so a
381 // page that never called `onNetworkChange` leaves it undefined and the
382 // guard in `settle` is what stops a ReferenceError inside
383 // evaluateJavascript, where nothing would see it.
384 try testing.expectEqualStrings("_craftNetworkChangeCallback", change_global);
385 try testing.expect(!std.mem.eql(u8, change_global, "_craftNetworkResolve"));
386}
387
388test "a change carries the same object getNetworkStatus returns" {
389 // `sendNetworkChange` calls `getNetworkStatus()` and sends the result
390 // verbatim, so the event payload and the polled answer cannot drift —
391 // and here they are literally the same function.
392 const json = try render(testing.allocator, .{ .connected = true, .transport = .wifi });
393 defer testing.allocator.free(json);
394
395 try testing.expectEqualStrings(
396 \\{"isConnected":true,"type":"wifi","isWifi":true,"isCellular":false}
397 , json);
398}