also looking at this
feat(android): the first listener shim, and network monitoring on it — 61 → 59
#183
5 files
+246
-13
| @@ -38,8 +38,18 @@ const jobject = jni.jobject; | ||
| 38 | 38 | |
| 39 | 39 | pub const A = struct { |
| 40 | 40 | pub const get_network_status = "getNetworkStatus"; |
| 41 | pub const start_network_monitoring = "startNetworkMonitoring"; | |
| 42 | pub const stop_network_monitoring = "stopNetworkMonitoring"; | |
| 41 | 43 | }; |
| 42 | 44 | |
| 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. | |
| 51 | pub const change_global = "_craftNetworkChangeCallback"; | |
| 52 | ||
| 43 | 53 | /// The transports the Kotlin's `when` distinguishes, in its order. |
| 44 | 54 | /// |
| 45 | 55 | /// 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 { | ||
| 152 | 162 | return .{ .connected = true, .transport = .none }; |
| 153 | 163 | } |
| 154 | 164 | |
| 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. | |
| 171 | pub const net_capability_internet: i32 = 12; | |
| 172 | ||
| 155 | 173 | // ============================================================================= |
| 156 | 174 | // Tests |
| 157 | 175 | // ============================================================================= |
| @@ -351,3 +369,30 @@ test "null capabilities is not connected, and an unknown transport still is" { | ||
| 351 | 369 | try testing.expectEqual(true, odd.connected); |
| 352 | 370 | try testing.expectEqual(Transport.none, odd.transport); |
| 353 | 371 | } |
| 372 | ||
| 373 | test "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 | ||
| 378 | test "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 | ||
| 388 | test "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 | } | |