1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/settings/tests/test_settings_data_uris.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,154 @@ 1.4 +<!DOCTYPE html> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=806374 1.8 +--> 1.9 +<head> 1.10 + <title>Test for Bug 806374 Settings API</title> 1.11 + <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> 1.12 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.13 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.14 +</head> 1.15 +<body> 1.16 + 1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=821630">Mozilla Bug 821630</a> 1.18 +<p id="display"></p> 1.19 +<div id="content" style="display: none"> 1.20 + 1.21 +</div> 1.22 +<pre id="test"> 1.23 +<script class="testbody" type="text/javascript;version=1.7"> 1.24 + 1.25 +"use strict"; 1.26 + 1.27 +if (SpecialPowers.isMainProcess()) { 1.28 + SpecialPowers.Cu.import("resource://gre/modules/SettingsChangeNotifier.jsm"); 1.29 +} 1.30 + 1.31 +SpecialPowers.addPermission("settings-read", true, document); 1.32 +SpecialPowers.addPermission("settings-write", true, document); 1.33 + 1.34 +function onUnwantedSuccess() { 1.35 + ok(false, "onUnwantedSuccess: shouldn't get here"); 1.36 +} 1.37 + 1.38 +function onFailure() { 1.39 + return function(s) { 1.40 + if (s) { 1.41 + ok(false, "in on Failure! - " + s); 1.42 + } else { 1.43 + ok(false, "in on Failure!"); 1.44 + } 1.45 + } 1.46 +} 1.47 + 1.48 +let mozSettings = window.navigator.mozSettings; 1.49 +let req; 1.50 + 1.51 +// A simple data URI that will be converted to a blob. 1.52 +let dataURI = "data:text/html;charset=utf-8,%3C!DOCTYPE html>%3Cbody style='background:black;"; 1.53 + 1.54 +function checkBlob(blob) { 1.55 + try { 1.56 + let url = URL.createObjectURL(blob); 1.57 + ok(true, "Valid blob"); 1.58 + } catch (e) { 1.59 + ok(false, "Valid blob"); 1.60 + } 1.61 +} 1.62 + 1.63 +let steps = [ 1.64 + function() { 1.65 + let lock = mozSettings.createLock(); 1.66 + req = lock.clear(); 1.67 + req.onsuccess = next; 1.68 + req.onerror = onFailure("Deleting database"); 1.69 + }, 1.70 + function() { 1.71 + function obs(e) { 1.72 + checkBlob(e.settingValue); 1.73 + mozSettings.removeObserver("test1", obs); 1.74 + next(); 1.75 + } 1.76 + mozSettings.addObserver("test1", obs); 1.77 + next(); 1.78 + }, 1.79 + function() { 1.80 + // next is called by the observer above 1.81 + let req = mozSettings.createLock().set({"test1": dataURI}); 1.82 + req.onerror = onFailure("Saving blob"); 1.83 + }, 1.84 + function() { 1.85 + let req = mozSettings.createLock().get("test1"); 1.86 + req.onsuccess = function(event) { 1.87 + checkBlob(event.target.result["test1"]); 1.88 + next(); 1.89 + }; 1.90 + req.onerror = onFailure("Getting blob"); 1.91 + }, 1.92 + function() { 1.93 + let req = mozSettings.createLock().set({"test2": [1, 2, dataURI, 4]}); 1.94 + req.onsuccess = next; 1.95 + req.onerror = onFailure("Saving array"); 1.96 + }, 1.97 + function() { 1.98 + let req = mozSettings.createLock().get("test2"); 1.99 + req.onsuccess = function(event) { 1.100 + let val = event.target.result["test2"]; 1.101 + ok(Array.isArray(val), "Result is an array"); 1.102 + ok(val[0] == 1 && val[1] == 2 && val[3] == 4, "Primitives are preserved"); 1.103 + checkBlob(val[2]); 1.104 + next(); 1.105 + }; 1.106 + req.onerror = onFailure("Getting array"); 1.107 + }, 1.108 + function() { 1.109 + let req = mozSettings.createLock().set({"test3": {foo: "bar", baz: {number: 1, arr: [dataURI]}}}); 1.110 + req.onsuccess = next(); 1.111 + req.onerror = onFailure("Saving object"); 1.112 + }, 1.113 + function() { 1.114 + let req = mozSettings.createLock().get("test3"); 1.115 + req.onsuccess = function(event) { 1.116 + let val = event.target.result["test3"]; 1.117 + ok(typeof(val) == "object", "Result is an object"); 1.118 + ok("foo" in val && typeof(val.foo) == "string", "String property preserved"); 1.119 + ok("baz" in val && typeof(val.baz) == "object", "Object property preserved"); 1.120 + let baz = val.baz; 1.121 + ok("number" in baz && baz.number == 1, "Primite inside object preserved"); 1.122 + ok("arr" in baz && Array.isArray(baz.arr), "Array inside object is preserved"); 1.123 + checkBlob(baz.arr[0]); 1.124 + next(); 1.125 + }; 1.126 + req.onerror = onFailure("Getting object"); 1.127 + }, 1.128 + function() { 1.129 + let req = mozSettings.createLock().clear(); 1.130 + req.onsuccess = function() { 1.131 + next(); 1.132 + }; 1.133 + req.onerror = onFailure("Deleting database"); 1.134 + }, 1.135 + function () { 1.136 + ok(true, "all done!\n"); 1.137 + SimpleTest.finish(); 1.138 + } 1.139 +]; 1.140 + 1.141 +function next() { 1.142 + try { 1.143 + let step = steps.shift(); 1.144 + if (step) { 1.145 + step(); 1.146 + } 1.147 + } catch(ex) { 1.148 + ok(false, "Caught exception", ex); 1.149 + } 1.150 +} 1.151 + 1.152 +SimpleTest.waitForExplicitFinish(); 1.153 +addLoadEvent(next); 1.154 +</script> 1.155 +</pre> 1.156 +</body> 1.157 +</html>