test_Realm.js (3680B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { Realm, WindowRealm } = ChromeUtils.importESModule( 7 "chrome://remote/content/shared/Realm.sys.mjs" 8 ); 9 10 add_task(function test_id() { 11 const realm1 = new Realm(); 12 const id1 = realm1.id; 13 Assert.equal(typeof id1, "string"); 14 15 const realm2 = new Realm(); 16 const id2 = realm2.id; 17 Assert.equal(typeof id2, "string"); 18 19 Assert.notEqual(id1, id2, "Ids for different realms are different"); 20 }); 21 22 add_task(function test_handleObjectMap() { 23 const realm = new Realm(); 24 25 // Test an unknown handle. 26 Assert.equal( 27 realm.getObjectForHandle("unknown"), 28 undefined, 29 "Unknown handles return undefined" 30 ); 31 32 // Test creating a simple handle. 33 const object = {}; 34 const handle = realm.getHandleForObject(object); 35 Assert.equal(typeof handle, "string", "Created a valid handle"); 36 Assert.equal( 37 realm.getObjectForHandle(handle), 38 object, 39 "Using the handle returned the original object" 40 ); 41 42 // Test another handle for the same object. 43 const secondHandle = realm.getHandleForObject(object); 44 Assert.equal(typeof secondHandle, "string", "Created a valid handle"); 45 Assert.notEqual(secondHandle, handle, "A different handle was generated"); 46 Assert.equal( 47 realm.getObjectForHandle(secondHandle), 48 object, 49 "Using the second handle also returned the original object" 50 ); 51 52 // Test using the handles in another realm. 53 const otherRealm = new Realm(); 54 Assert.equal( 55 otherRealm.getObjectForHandle(handle), 56 undefined, 57 "A realm returns undefined for handles from another realm" 58 ); 59 60 // Removing an unknown handle should not throw or have any side effect on 61 // existing handles. 62 realm.removeObjectHandle("unknown"); 63 Assert.equal(realm.getObjectForHandle(handle), object); 64 Assert.equal(realm.getObjectForHandle(secondHandle), object); 65 66 // Remove the second handle 67 realm.removeObjectHandle(secondHandle); 68 Assert.equal( 69 realm.getObjectForHandle(handle), 70 object, 71 "The first handle is still resolving the object" 72 ); 73 Assert.equal( 74 realm.getObjectForHandle(secondHandle), 75 undefined, 76 "The second handle returns undefined after calling removeObjectHandle" 77 ); 78 79 // Remove the original handle 80 realm.removeObjectHandle(handle); 81 Assert.equal( 82 realm.getObjectForHandle(handle), 83 undefined, 84 "The first handle returns undefined as well" 85 ); 86 }); 87 88 add_task(async function test_windowRealm_isSandbox() { 89 const windowlessBrowser = Services.appShell.createWindowlessBrowser(false); 90 const contentWindow = windowlessBrowser.docShell.domWindow; 91 92 const realm1 = new WindowRealm(contentWindow); 93 Assert.equal(realm1.isSandbox, false); 94 95 const realm2 = new WindowRealm(contentWindow, { sandboxName: "test" }); 96 Assert.equal(realm2.isSandbox, true); 97 }); 98 99 add_task(async function test_windowRealm_userActivationEnabled() { 100 const windowlessBrowser = Services.appShell.createWindowlessBrowser(false); 101 const contentWindow = windowlessBrowser.docShell.domWindow; 102 const userActivation = contentWindow.navigator.userActivation; 103 104 const realm = new WindowRealm(contentWindow); 105 106 Assert.equal(realm.userActivationEnabled, false); 107 Assert.equal(userActivation.isActive && userActivation.hasBeenActive, false); 108 109 realm.userActivationEnabled = true; 110 Assert.equal(realm.userActivationEnabled, true); 111 Assert.equal(userActivation.isActive && userActivation.hasBeenActive, true); 112 113 realm.userActivationEnabled = false; 114 Assert.equal(realm.userActivationEnabled, false); 115 Assert.equal(userActivation.isActive && userActivation.hasBeenActive, false); 116 });