test_session.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const {utils: Cu} = Components;
  6. Cu.import("resource://gre/modules/Preferences.jsm");
  7. Cu.import("resource://gre/modules/Services.jsm");
  8. Cu.import("chrome://marionette/content/error.js");
  9. Cu.import("chrome://marionette/content/session.js");
  10. add_test(function test_Timeouts_ctor() {
  11. let ts = new session.Timeouts();
  12. equal(ts.implicit, 0);
  13. equal(ts.pageLoad, 300000);
  14. equal(ts.script, 30000);
  15. run_next_test();
  16. });
  17. add_test(function test_Timeouts_toString() {
  18. equal(new session.Timeouts().toString(), "[object session.Timeouts]");
  19. run_next_test();
  20. });
  21. add_test(function test_Timeouts_toJSON() {
  22. let ts = new session.Timeouts();
  23. deepEqual(ts.toJSON(), {"implicit": 0, "page load": 300000, "script": 30000});
  24. run_next_test();
  25. });
  26. add_test(function test_Timeouts_fromJSON() {
  27. let json = {
  28. "implicit": 10,
  29. "page load": 20,
  30. "script": 30,
  31. };
  32. let ts = session.Timeouts.fromJSON(json);
  33. equal(ts.implicit, json["implicit"]);
  34. equal(ts.pageLoad, json["page load"]);
  35. equal(ts.script, json["script"]);
  36. run_next_test();
  37. });
  38. add_test(function test_PageLoadStrategy() {
  39. equal(session.PageLoadStrategy.None, "none");
  40. equal(session.PageLoadStrategy.Eager, "eager");
  41. equal(session.PageLoadStrategy.Normal, "normal");
  42. run_next_test();
  43. });
  44. add_test(function test_Proxy_ctor() {
  45. let p = new session.Proxy();
  46. let props = [
  47. "proxyType",
  48. "httpProxy",
  49. "httpProxyPort",
  50. "sslProxy",
  51. "sslProxyPort",
  52. "ftpProxy",
  53. "ftpProxyPort",
  54. "socksProxy",
  55. "socksProxyPort",
  56. "socksVersion",
  57. "proxyAutoconfigUrl",
  58. ];
  59. for (let prop of props) {
  60. ok(prop in p, `${prop} in ${JSON.stringify(props)}`);
  61. equal(p[prop], null);
  62. }
  63. run_next_test();
  64. });
  65. add_test(function test_Proxy_init() {
  66. let p = new session.Proxy();
  67. // no changed made, and 5 (system) is default
  68. equal(p.init(), false);
  69. equal(Preferences.get("network.proxy.type"), 5);
  70. // pac
  71. p.proxyType = "pac";
  72. p.proxyAutoconfigUrl = "http://localhost:1234";
  73. ok(p.init());
  74. equal(Preferences.get("network.proxy.type"), 2);
  75. equal(Preferences.get("network.proxy.autoconfig_url"),
  76. "http://localhost:1234");
  77. // autodetect
  78. p = new session.Proxy();
  79. p.proxyType = "autodetect";
  80. ok(p.init());
  81. equal(Preferences.get("network.proxy.type"), 4);
  82. // system
  83. p = new session.Proxy();
  84. p.proxyType = "system";
  85. ok(p.init());
  86. equal(Preferences.get("network.proxy.type"), 5);
  87. // noproxy
  88. p = new session.Proxy();
  89. p.proxyType = "noproxy";
  90. ok(p.init());
  91. equal(Preferences.get("network.proxy.type"), 0);
  92. run_next_test();
  93. });
  94. add_test(function test_Proxy_toString() {
  95. equal(new session.Proxy().toString(), "[object session.Proxy]");
  96. run_next_test();
  97. });
  98. add_test(function test_Proxy_toJSON() {
  99. let p = new session.Proxy();
  100. deepEqual(p.toJSON(), {});
  101. p = new session.Proxy();
  102. p.proxyType = "manual";
  103. deepEqual(p.toJSON(), {proxyType: "manual"});
  104. run_next_test();
  105. });
  106. add_test(function test_Proxy_fromJSON() {
  107. deepEqual({}, session.Proxy.fromJSON(undefined).toJSON());
  108. deepEqual({}, session.Proxy.fromJSON(null).toJSON());
  109. for (let typ of [true, 42, "foo", []]) {
  110. Assert.throws(() => session.Proxy.fromJSON(typ), InvalidArgumentError);
  111. }
  112. // must contain proxyType
  113. Assert.throws(() => session.Proxy.fromJSON({}), InvalidArgumentError);
  114. deepEqual({proxyType: "foo"},
  115. session.Proxy.fromJSON({proxyType: "foo"}).toJSON());
  116. // manual
  117. session.Proxy.fromJSON({proxyType: "manual"});
  118. for (let proxy of ["httpProxy", "sslProxy", "ftpProxy", "socksProxy"]) {
  119. let manual = {proxyType: "manual"};
  120. for (let typ of [true, 42, [], {}, null]) {
  121. manual[proxy] = typ;
  122. Assert.throws(() => session.Proxy.fromJSON(manual),
  123. InvalidArgumentError);
  124. }
  125. manual[proxy] = "foo";
  126. Assert.throws(() => session.Proxy.fromJSON(manual),
  127. InvalidArgumentError);
  128. for (let typ of ["bar", true, [], {}, null, undefined]) {
  129. manual[proxy + "Port"] = typ;
  130. Assert.throws(() => session.Proxy.fromJSON(manual),
  131. InvalidArgumentError);
  132. }
  133. manual[proxy] = "foo";
  134. manual[proxy + "Port"] = 1234;
  135. let expected = {
  136. "proxyType": "manual",
  137. [proxy]: "foo",
  138. [proxy + "Port"]: 1234,
  139. };
  140. if (proxy == "socksProxy") {
  141. manual.socksProxyVersion = 42;
  142. expected.socksProxyVersion = 42;
  143. }
  144. deepEqual(expected, session.Proxy.fromJSON(manual).toJSON());
  145. }
  146. Assert.throws(() => session.Proxy.fromJSON(
  147. {proxyType: "manual", socksProxy: "foo", socksProxyPort: 1234}),
  148. InvalidArgumentError);
  149. run_next_test();
  150. });
  151. add_test(function test_Capabilities_ctor() {
  152. let caps = new session.Capabilities();
  153. ok(caps.has("browserName"));
  154. ok(caps.has("browserVersion"));
  155. ok(caps.has("platformName"));
  156. ok(caps.has("platformVersion"));
  157. equal(session.PageLoadStrategy.Normal, caps.get("pageLoadStrategy"));
  158. equal(false, caps.get("acceptInsecureCerts"));
  159. ok(caps.get("timeouts") instanceof session.Timeouts);
  160. ok(caps.get("proxy") instanceof session.Proxy);
  161. ok(caps.has("rotatable"));
  162. equal(0, caps.get("specificationLevel"));
  163. ok(caps.has("moz:processID"));
  164. ok(caps.has("moz:profile"));
  165. equal(false, caps.get("moz:accessibilityChecks"));
  166. run_next_test();
  167. });
  168. add_test(function test_Capabilities_toString() {
  169. equal("[object session.Capabilities]", new session.Capabilities().toString());
  170. run_next_test();
  171. });
  172. add_test(function test_Capabilities_toJSON() {
  173. let caps = new session.Capabilities();
  174. let json = caps.toJSON();
  175. equal(caps.get("browserName"), json.browserName);
  176. equal(caps.get("browserVersion"), json.browserVersion);
  177. equal(caps.get("platformName"), json.platformName);
  178. equal(caps.get("platformVersion"), json.platformVersion);
  179. equal(caps.get("pageLoadStrategy"), json.pageLoadStrategy);
  180. equal(caps.get("acceptInsecureCerts"), json.acceptInsecureCerts);
  181. deepEqual(caps.get("timeouts").toJSON(), json.timeouts);
  182. equal(undefined, json.proxy);
  183. equal(caps.get("rotatable"), json.rotatable);
  184. equal(caps.get("specificationLevel"), json.specificationLevel);
  185. equal(caps.get("moz:processID"), json["moz:processID"]);
  186. equal(caps.get("moz:profile"), json["moz:profile"]);
  187. equal(caps.get("moz:accessibilityChecks"), json["moz:accessibilityChecks"]);
  188. run_next_test();
  189. });
  190. add_test(function test_Capabilities_fromJSON() {
  191. const {fromJSON} = session.Capabilities;
  192. // plain
  193. for (let typ of [{}, null, undefined]) {
  194. ok(fromJSON(typ, {merge: true}).has("browserName"));
  195. ok(fromJSON(typ, {merge: false}).has("browserName"));
  196. }
  197. for (let typ of [true, 42, "foo", []]) {
  198. Assert.throws(() =>
  199. fromJSON(typ, {merge: true}), InvalidArgumentError);
  200. Assert.throws(() =>
  201. fromJSON(typ, {merge: false}), InvalidArgumentError);
  202. }
  203. // merging
  204. let desired = {"moz:accessibilityChecks": false};
  205. let required = {"moz:accessibilityChecks": true};
  206. let matched = fromJSON(
  207. {desiredCapabilities: desired, requiredCapabilities: required},
  208. {merge: true});
  209. ok(matched.has("moz:accessibilityChecks"));
  210. equal(true, matched.get("moz:accessibilityChecks"));
  211. // desiredCapabilities/requriedCapabilities types
  212. for (let typ of [undefined, null, {}]) {
  213. ok(fromJSON({desiredCapabilities: typ}, {merge: true}));
  214. ok(fromJSON({requiredCapabilities: typ}, {merge: true}));
  215. }
  216. for (let typ of [true, 42, "foo", []]) {
  217. Assert.throws(() => fromJSON({desiredCapabilities: typ}, {merge: true}));
  218. Assert.throws(() => fromJSON({requiredCapabilities: typ}, {merge: true}));
  219. }
  220. // matching
  221. let caps = new session.Capabilities();
  222. ok(fromJSON({browserName: caps.get("browserName")}));
  223. ok(fromJSON({browserName: null}));
  224. ok(fromJSON({browserName: undefined}));
  225. ok(fromJSON({browserName: "*"}));
  226. Assert.throws(() => fromJSON({browserName: "foo"}));
  227. ok(fromJSON({browserVersion: caps.get("browserVersion")}));
  228. ok(fromJSON({browserVersion: null}));
  229. ok(fromJSON({browserVersion: undefined}));
  230. ok(fromJSON({browserVersion: "*"}));
  231. Assert.throws(() => fromJSON({browserVersion: "foo"}));
  232. ok(fromJSON({platformName: caps.get("platformName")}));
  233. ok(fromJSON({platformName: null}));
  234. ok(fromJSON({platformName: undefined}));
  235. ok(fromJSON({platformName: "*"}));
  236. Assert.throws(() => fromJSON({platformName: "foo"}));
  237. ok(fromJSON({platformVersion: caps.get("platformVersion")}));
  238. ok(fromJSON({platformVersion: null}));
  239. ok(fromJSON({platformVersion: undefined}));
  240. ok(fromJSON({platformVersion: "*"}));
  241. Assert.throws(() => fromJSON({platformVersion: "foo"}));
  242. caps = fromJSON({acceptInsecureCerts: true});
  243. equal(true, caps.get("acceptInsecureCerts"));
  244. caps = fromJSON({acceptInsecureCerts: false});
  245. equal(false, caps.get("acceptInsecureCerts"));
  246. Assert.throws(() => fromJSON({acceptInsecureCerts: "foo"}));
  247. for (let strategy of Object.values(session.PageLoadStrategy)) {
  248. caps = fromJSON({pageLoadStrategy: strategy});
  249. equal(strategy, caps.get("pageLoadStrategy"));
  250. }
  251. Assert.throws(() => fromJSON({pageLoadStrategy: "foo"}));
  252. let proxyConfig = {proxyType: "manual"};
  253. caps = fromJSON({proxy: proxyConfig});
  254. equal("manual", caps.get("proxy").proxyType);
  255. let timeoutsConfig = {implicit: 123};
  256. caps = fromJSON({timeouts: timeoutsConfig});
  257. equal(123, caps.get("timeouts").implicit);
  258. equal(0, caps.get("specificationLevel"));
  259. caps = fromJSON({specificationLevel: 123});
  260. equal(123, caps.get("specificationLevel"));
  261. Assert.throws(() => fromJSON({specificationLevel: "foo"}));
  262. Assert.throws(() => fromJSON({specificationLevel: -1}));
  263. caps = fromJSON({"moz:accessibilityChecks": true});
  264. equal(true, caps.get("moz:accessibilityChecks"));
  265. caps = fromJSON({"moz:accessibilityChecks": false});
  266. equal(false, caps.get("moz:accessibilityChecks"));
  267. Assert.throws(() => fromJSON({"moz:accessibilityChecks": "foo"}));
  268. run_next_test();
  269. });
  270. // use session.Proxy.toJSON to test marshal
  271. add_test(function test_marshal() {
  272. let proxy = new session.Proxy();
  273. // drop empty fields
  274. deepEqual({}, proxy.toJSON());
  275. proxy.proxyType = "manual";
  276. deepEqual({proxyType: "manual"}, proxy.toJSON());
  277. proxy.proxyType = null;
  278. deepEqual({}, proxy.toJSON());
  279. proxy.proxyType = undefined;
  280. deepEqual({}, proxy.toJSON());
  281. // iterate over object literals
  282. proxy.proxyType = {foo: "bar"};
  283. deepEqual({proxyType: {foo: "bar"}}, proxy.toJSON());
  284. // iterate over complex object that implement toJSON
  285. proxy.proxyType = new session.Proxy();
  286. deepEqual({}, proxy.toJSON());
  287. proxy.proxyType.proxyType = "manual";
  288. deepEqual({proxyType: {proxyType: "manual"}}, proxy.toJSON());
  289. // drop objects with no entries
  290. proxy.proxyType = {foo: {}};
  291. deepEqual({}, proxy.toJSON());
  292. proxy.proxyType = {foo: new session.Proxy()};
  293. deepEqual({}, proxy.toJSON());
  294. run_next_test();
  295. });