/toolkit/mozapps/plugins/tests/browser_bug435788.js

http://github.com/zpao/v8monkey · JavaScript · 461 lines · 357 code · 88 blank · 16 comment · 14 complexity · d2f0977fe154e7c52e8a761baf59ecee MD5 · raw file

  1. const TEST_ROOT = "http://example.com/browser/toolkit/mozapps/plugins/tests/";
  2. let tmp = {};
  3. Components.utils.import("resource://gre/modules/AddonManager.jsm", tmp);
  4. let AddonManager = tmp.AddonManager;
  5. var gPFS;
  6. function test() {
  7. waitForExplicitFinish();
  8. prepare_test_1();
  9. }
  10. function finishTest() {
  11. Services.prefs.clearUserPref("pfs.datasource.url");
  12. finish();
  13. }
  14. // Gets the number of plugin items in the detected list
  15. function getListCount() {
  16. var list = gPFS.document.getElementById("pluginList");
  17. return list.childNodes.length;
  18. }
  19. // Gets wether the list contains a particular plugin name
  20. function hasListItem(name, version) {
  21. var label = name + " " + (version ? version : "");
  22. var list = gPFS.document.getElementById("pluginList");
  23. for (var i = 0; i < list.childNodes.length; i++) {
  24. if (list.childNodes[i].label == label)
  25. return true;
  26. }
  27. return false;
  28. }
  29. // Gets the number of plugin results
  30. function getResultCount() {
  31. var list = gPFS.document.getElementById("pluginResultList");
  32. return list.childNodes.length;
  33. }
  34. // Gets the plugin result for a particular plugin name
  35. function getResultItem(name, version) {
  36. var label = name + " " + (version ? version : "");
  37. var list = gPFS.document.getElementById("pluginResultList");
  38. for (var i = 0; i < list.childNodes.length; i++) {
  39. if (list.childNodes[i].childNodes[1].value == label) {
  40. var item = {
  41. name: name,
  42. version: version,
  43. status: null
  44. };
  45. if (list.childNodes[i].childNodes[2].tagName == "label")
  46. item.status = list.childNodes[i].childNodes[2].value;
  47. return item;
  48. }
  49. }
  50. return null;
  51. }
  52. // Logs the currently displaying wizard page
  53. function page_shown() {
  54. function show_button_state(name) {
  55. var button = gPFS.document.documentElement.getButton(name);
  56. info("Button " + name + ". hidden: " + button.hidden +
  57. ", disabled: " + button.disabled);
  58. }
  59. info("Page shown: " +
  60. gPFS.document.documentElement.currentPage.getAttribute("label"));
  61. show_button_state("next");
  62. show_button_state("finish");
  63. }
  64. function pfs_loaded() {
  65. info("PFS loaded");
  66. var docEle = gPFS.document.documentElement;
  67. var onwizardfinish = function () {
  68. info("wizardfinish event");
  69. };
  70. var onwizardnext = function () {
  71. info("wizardnext event");
  72. };
  73. docEle.addEventListener("pageshow", page_shown, false);
  74. docEle.addEventListener("wizardfinish", onwizardfinish, false);
  75. docEle.addEventListener("wizardnext", onwizardnext, false);
  76. gPFS.addEventListener("unload", function() {
  77. info("unload event");
  78. gPFS.removeEventListener("unload", arguments.callee, false);
  79. docEle.removeEventListener("pageshow", page_shown, false);
  80. docEle.removeEventListener("wizardfinish", onwizardfinish, false);
  81. docEle.removeEventListener("wizardnext", onwizardnext, false);
  82. }, false);
  83. page_shown();
  84. }
  85. function startTest(num, missingPluginsArray) {
  86. info("Test " + num);
  87. gPFS = window.openDialog("chrome://mozapps/content/plugins/pluginInstallerWizard.xul",
  88. "PFSWindow", "chrome,centerscreen,resizable=yes",
  89. {plugins: missingPluginsArray});
  90. var testScope = this;
  91. gPFS.addEventListener("load", function () {
  92. gPFS.removeEventListener("load", arguments.callee, false);
  93. pfs_loaded();
  94. var seenAvailable = false;
  95. var expectAvailable = typeof testScope["test_" + num + "_available"] == "function";
  96. function availableListener() {
  97. seenAvailable = true;
  98. if (expectAvailable) {
  99. executeSoon(function () {
  100. testScope["test_" + num + "_available"]();
  101. gPFS.document.documentElement.getButton("next").click();
  102. });
  103. } else {
  104. ok(false, "Should not have found plugins to install");
  105. }
  106. }
  107. function completeListener() {
  108. if (expectAvailable)
  109. ok(seenAvailable, "Should have seen the list of available plugins");
  110. executeSoon(testScope["test_" + num + "_complete"]);
  111. }
  112. gPFS.document.documentElement.wizardPages[1].addEventListener("pageshow", availableListener);
  113. gPFS.document.documentElement.wizardPages[4].addEventListener("pageshow", completeListener);
  114. gPFS.addEventListener("unload", function () {
  115. gPFS.removeEventListener("unload", arguments.callee, false);
  116. gPFS.document.documentElement.wizardPages[1].removeEventListener("pageshow", availableListener, false);
  117. gPFS.document.documentElement.wizardPages[4].removeEventListener("pageshow", completeListener, false);
  118. num++;
  119. if (typeof testScope["prepare_test_" + num] == "function")
  120. testScope["prepare_test_" + num]();
  121. else
  122. finishTest();
  123. });
  124. });
  125. }
  126. function clickFinish() {
  127. var finish = gPFS.document.documentElement.getButton("finish");
  128. ok(!finish.hidden, "Finish button should not be hidden");
  129. ok(!finish.disabled, "Finish button should not be disabled");
  130. finish.click();
  131. }
  132. // Test a working installer
  133. function prepare_test_1() {
  134. Services.prefs.setCharPref("pfs.datasource.url", TEST_ROOT + "pfs_bug435788_1.rdf");
  135. var missingPluginsArray = {
  136. "application/x-working-plugin": {
  137. mimetype: "application/x-working-plugin",
  138. pluginsPage: ""
  139. }
  140. };
  141. startTest(1, missingPluginsArray);
  142. }
  143. function test_1_available() {
  144. is(getListCount(), 1, "Should have found 1 plugin to install");
  145. ok(hasListItem("Test plugin 1", null), "Should have seen the right plugin name");
  146. }
  147. function test_1_complete() {
  148. is(getResultCount(), 1, "Should have attempted to install 1 plugin");
  149. var item = getResultItem("Test plugin 1", null);
  150. ok(item, "Should have seen the installed item");
  151. is(item.status, "Installed", "Should have been a successful install");
  152. clickFinish();
  153. }
  154. // Test a broken installer (returns exit code 1)
  155. function prepare_test_2() {
  156. var missingPluginsArray = {
  157. "application/x-broken-installer": {
  158. mimetype: "application/x-broken-installer",
  159. pluginsPage: ""
  160. }
  161. };
  162. startTest(2, missingPluginsArray);
  163. }
  164. function test_2_available() {
  165. is(getListCount(), 1, "Should have found 1 plugin to install");
  166. ok(hasListItem("Test plugin 2", null), "Should have seen the right plugin name");
  167. }
  168. function test_2_complete() {
  169. is(getResultCount(), 1, "Should have attempted to install 1 plugin");
  170. var item = getResultItem("Test plugin 2", null);
  171. ok(item, "Should have seen the installed item");
  172. is(item.status, "Failed", "Should have been a failed install");
  173. clickFinish();
  174. }
  175. // Test both working and broken together
  176. function prepare_test_3() {
  177. var missingPluginsArray = {
  178. "application/x-working-plugin": {
  179. mimetype: "application/x-working-plugin",
  180. pluginsPage: ""
  181. },
  182. "application/x-broken-installer": {
  183. mimetype: "application/x-broken-installer",
  184. pluginsPage: ""
  185. }
  186. };
  187. startTest(3, missingPluginsArray);
  188. }
  189. function test_3_available() {
  190. is(getListCount(), 2, "Should have found 2 plugins to install");
  191. ok(hasListItem("Test plugin 1", null), "Should have seen the right plugin name");
  192. ok(hasListItem("Test plugin 2", null), "Should have seen the right plugin name");
  193. }
  194. function test_3_complete() {
  195. is(getResultCount(), 2, "Should have attempted to install 2 plugins");
  196. var item = getResultItem("Test plugin 1", null);
  197. ok(item, "Should have seen the installed item");
  198. is(item.status, "Installed", "Should have been a successful install");
  199. item = getResultItem("Test plugin 2", null);
  200. ok(item, "Should have seen the installed item");
  201. is(item.status, "Failed", "Should have been a failed install");
  202. clickFinish();
  203. }
  204. // Test an installer with a bad hash
  205. function prepare_test_4() {
  206. var missingPluginsArray = {
  207. "application/x-broken-plugin-hash": {
  208. mimetype: "application/x-broken-plugin-hash",
  209. pluginsPage: ""
  210. }
  211. };
  212. startTest(4, missingPluginsArray);
  213. }
  214. function test_4_available() {
  215. is(getListCount(), 1, "Should have found 1 plugin to install");
  216. ok(hasListItem("Test plugin 3", null), "Should have seen the right plugin name");
  217. }
  218. function test_4_complete() {
  219. is(getResultCount(), 1, "Should have attempted to install 1 plugin");
  220. var item = getResultItem("Test plugin 3", null);
  221. ok(item, "Should have seen the installed item");
  222. is(item.status, "Failed", "Should have not been a successful install");
  223. clickFinish();
  224. }
  225. // Test a working xpi
  226. function prepare_test_5() {
  227. var missingPluginsArray = {
  228. "application/x-working-extension": {
  229. mimetype: "application/x-working-extension",
  230. pluginsPage: ""
  231. }
  232. };
  233. startTest(5, missingPluginsArray);
  234. }
  235. function test_5_available() {
  236. is(getListCount(), 1, "Should have found 1 plugin to install");
  237. ok(hasListItem("Test extension 1", null), "Should have seen the right plugin name");
  238. }
  239. function test_5_complete() {
  240. is(getResultCount(), 1, "Should have attempted to install 1 plugin");
  241. var item = getResultItem("Test extension 1", null);
  242. ok(item, "Should have seen the installed item");
  243. is(item.status, "Installed", "Should have been a successful install");
  244. AddonManager.getAllInstalls(function(installs) {
  245. is(installs.length, 1, "Should be just one install");
  246. is(installs[0].state, AddonManager.STATE_INSTALLED, "Should be fully installed");
  247. is(installs[0].addon.id, "bug435788_1@tests.mozilla.org", "Should have installed the extension");
  248. installs[0].cancel();
  249. clickFinish();
  250. });
  251. }
  252. // Test a broke xpi (no install.rdf)
  253. function prepare_test_6() {
  254. var missingPluginsArray = {
  255. "application/x-broken-extension": {
  256. mimetype: "application/x-broken-extension",
  257. pluginsPage: ""
  258. }
  259. };
  260. startTest(6, missingPluginsArray);
  261. }
  262. function test_6_available() {
  263. is(getListCount(), 1, "Should have found 1 plugin to install");
  264. ok(hasListItem("Test extension 2", null), "Should have seen the right plugin name");
  265. }
  266. function test_6_complete() {
  267. is(getResultCount(), 1, "Should have attempted to install 1 plugin");
  268. var item = getResultItem("Test extension 2", null);
  269. ok(item, "Should have seen the installed item");
  270. is(item.status, "Failed", "Should have been a failed install");
  271. clickFinish();
  272. }
  273. // Test both working and broken xpi
  274. function prepare_test_7() {
  275. var missingPluginsArray = {
  276. "application/x-working-extension": {
  277. mimetype: "application/x-working-extension",
  278. pluginsPage: ""
  279. },
  280. "application/x-broken-extension": {
  281. mimetype: "application/x-broken-extension",
  282. pluginsPage: ""
  283. }
  284. };
  285. startTest(7, missingPluginsArray);
  286. }
  287. function test_7_available() {
  288. is(getListCount(), 2, "Should have found 2 plugins to install");
  289. ok(hasListItem("Test extension 1", null), "Should have seen the right plugin name");
  290. ok(hasListItem("Test extension 2", null), "Should have seen the right plugin name");
  291. }
  292. function test_7_complete() {
  293. is(getResultCount(), 2, "Should have attempted to install 2 plugins");
  294. var item = getResultItem("Test extension 1", null);
  295. ok(item, "Should have seen the installed item");
  296. is(item.status, "Installed", "Should have been a failed install");
  297. item = getResultItem("Test extension 2", null);
  298. ok(item, "Should have seen the installed item");
  299. is(item.status, "Failed", "Should have been a failed install");
  300. AddonManager.getAllInstalls(function(installs) {
  301. is(installs.length, 1, "Should be one active installs");
  302. installs[0].cancel();
  303. clickFinish();
  304. });
  305. }
  306. // Test an xpi with a bad hash
  307. function prepare_test_8() {
  308. var missingPluginsArray = {
  309. "application/x-broken-extension-hash": {
  310. mimetype: "application/x-broken-extension-hash",
  311. pluginsPage: ""
  312. }
  313. };
  314. startTest(8, missingPluginsArray);
  315. }
  316. function test_8_available() {
  317. is(getListCount(), 1, "Should have found 1 plugin to install");
  318. ok(hasListItem("Test extension 3", null), "Should have seen the right plugin name");
  319. }
  320. function test_8_complete() {
  321. is(getResultCount(), 1, "Should have attempted to install 1 plugin");
  322. var item = getResultItem("Test extension 3", null);
  323. ok(item, "Should have seen the installed item");
  324. is(item.status, "Failed", "Should have not been a successful install");
  325. AddonManager.getAllInstalls(function(installs) {
  326. is(installs.length, 0, "Should not be any installs");
  327. clickFinish();
  328. });
  329. }
  330. // Test when no plugin exists in the datasource
  331. function prepare_test_9() {
  332. var missingPluginsArray = {
  333. "application/x-unknown-plugin": {
  334. mimetype: "application/x-unknown-plugin",
  335. pluginsPage: ""
  336. }
  337. };
  338. startTest(9, missingPluginsArray);
  339. }
  340. function test_9_complete() {
  341. is(getResultCount(), 0, "Should have found no plugins");
  342. clickFinish();
  343. }
  344. // Test when the datasource is invalid xml
  345. function prepare_test_10() {
  346. Services.prefs.setCharPref("pfs.datasource.url", TEST_ROOT + "pfs_bug435788_2.rdf");
  347. var missingPluginsArray = {
  348. "application/x-broken-xml": {
  349. mimetype: "application/x-broken-xml",
  350. pluginsPage: ""
  351. }
  352. };
  353. startTest(10, missingPluginsArray);
  354. }
  355. function test_10_complete() {
  356. is(getResultCount(), 0, "Should have found no plugins");
  357. clickFinish();
  358. }
  359. // Test when no datasource is returned
  360. function prepare_test_11() {
  361. Services.prefs.setCharPref("pfs.datasource.url", TEST_ROOT + "pfs_bug435788_foo.rdf");
  362. var missingPluginsArray = {
  363. "application/x-missing-xml": {
  364. mimetype: "application/x-missing-xml",
  365. pluginsPage: ""
  366. }
  367. };
  368. startTest(11, missingPluginsArray);
  369. }
  370. function test_11_complete() {
  371. is(getResultCount(), 0, "Should have found no plugins");
  372. clickFinish();
  373. }