test-context-menu.js 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045
  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
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. let { Cc, Ci } = require("chrome");
  6. require("sdk/context-menu");
  7. const { Loader } = require('sdk/test/loader');
  8. const timer = require("sdk/timers");
  9. const { merge } = require("sdk/util/object");
  10. // These should match the same constants in the module.
  11. const ITEM_CLASS = "addon-context-menu-item";
  12. const SEPARATOR_CLASS = "addon-context-menu-separator";
  13. const OVERFLOW_THRESH_DEFAULT = 10;
  14. const OVERFLOW_THRESH_PREF =
  15. "extensions.addon-sdk.context-menu.overflowThreshold";
  16. const OVERFLOW_MENU_CLASS = "addon-content-menu-overflow-menu";
  17. const OVERFLOW_POPUP_CLASS = "addon-content-menu-overflow-popup";
  18. const TEST_DOC_URL = module.uri.replace(/\.js$/, ".html");
  19. const data = require("./fixtures");
  20. // Tests that when present the separator is placed before the separator from
  21. // the old context-menu module
  22. exports.testSeparatorPosition = function (assert, done) {
  23. let test = new TestHelper(assert, done);
  24. let loader = test.newLoader();
  25. // Create the old separator
  26. let oldSeparator = test.contextMenuPopup.ownerDocument.createElement("menuseparator");
  27. oldSeparator.id = "jetpack-context-menu-separator";
  28. test.contextMenuPopup.appendChild(oldSeparator);
  29. // Create an item.
  30. let item = new loader.cm.Item({ label: "item" });
  31. test.showMenu(null, function (popup) {
  32. assert.equal(test.contextMenuSeparator.nextSibling.nextSibling, oldSeparator,
  33. "New separator should appear before the old one");
  34. test.contextMenuPopup.removeChild(oldSeparator);
  35. test.done();
  36. });
  37. };
  38. // Destroying items that were previously created should cause them to be absent
  39. // from the menu.
  40. exports.testConstructDestroy = function (assert, done) {
  41. let test = new TestHelper(assert, done);
  42. let loader = test.newLoader();
  43. // Create an item.
  44. let item = new loader.cm.Item({ label: "item" });
  45. assert.equal(item.parentMenu, loader.cm.contentContextMenu,
  46. "item's parent menu should be correct");
  47. test.showMenu(null, function (popup) {
  48. // It should be present when the menu is shown.
  49. test.checkMenu([item], [], []);
  50. popup.hidePopup();
  51. // Destroy the item. Multiple destroys should be harmless.
  52. item.destroy();
  53. item.destroy();
  54. test.showMenu(null, function (popup) {
  55. // It should be removed from the menu.
  56. test.checkMenu([item], [], [item]);
  57. test.done();
  58. });
  59. });
  60. };
  61. // Destroying an item twice should not cause an error.
  62. exports.testDestroyTwice = function (assert, done) {
  63. let test = new TestHelper(assert, done);
  64. let loader = test.newLoader();
  65. let item = new loader.cm.Item({ label: "item" });
  66. item.destroy();
  67. item.destroy();
  68. test.pass("Destroying an item twice should not cause an error.");
  69. test.done();
  70. };
  71. // CSS selector contexts should cause their items to be present in the menu
  72. // when the menu is invoked on nodes that match the selectors.
  73. exports.testSelectorContextMatch = function (assert, done) {
  74. let test = new TestHelper(assert, done);
  75. let loader = test.newLoader();
  76. let item = new loader.cm.Item({
  77. label: "item",
  78. data: "item",
  79. context: loader.cm.SelectorContext("img")
  80. });
  81. test.withTestDoc(function (window, doc) {
  82. test.showMenu(doc.getElementById("image"), function (popup) {
  83. test.checkMenu([item], [], []);
  84. test.done();
  85. });
  86. });
  87. };
  88. // CSS selector contexts should cause their items to be present in the menu
  89. // when the menu is invoked on nodes that have ancestors that match the
  90. // selectors.
  91. exports.testSelectorAncestorContextMatch = function (assert, done) {
  92. let test = new TestHelper(assert, done);
  93. let loader = test.newLoader();
  94. let item = new loader.cm.Item({
  95. label: "item",
  96. data: "item",
  97. context: loader.cm.SelectorContext("a[href]")
  98. });
  99. test.withTestDoc(function (window, doc) {
  100. test.showMenu(doc.getElementById("span-link"), function (popup) {
  101. test.checkMenu([item], [], []);
  102. test.done();
  103. });
  104. });
  105. };
  106. // CSS selector contexts should cause their items to be absent from the menu
  107. // when the menu is not invoked on nodes that match or have ancestors that
  108. // match the selectors.
  109. exports.testSelectorContextNoMatch = function (assert, done) {
  110. let test = new TestHelper(assert, done);
  111. let loader = test.newLoader();
  112. let item = new loader.cm.Item({
  113. label: "item",
  114. data: "item",
  115. context: loader.cm.SelectorContext("img")
  116. });
  117. test.showMenu(null, function (popup) {
  118. test.checkMenu([item], [item], []);
  119. test.done();
  120. });
  121. };
  122. // Page contexts should cause their items to be present in the menu when the
  123. // menu is not invoked on an active element.
  124. exports.testPageContextMatch = function (assert, done) {
  125. let test = new TestHelper(assert, done);
  126. let loader = test.newLoader();
  127. let items = [
  128. new loader.cm.Item({
  129. label: "item 0"
  130. }),
  131. new loader.cm.Item({
  132. label: "item 1",
  133. context: undefined
  134. }),
  135. new loader.cm.Item({
  136. label: "item 2",
  137. context: loader.cm.PageContext()
  138. }),
  139. new loader.cm.Item({
  140. label: "item 3",
  141. context: [loader.cm.PageContext()]
  142. })
  143. ];
  144. test.showMenu(null, function (popup) {
  145. test.checkMenu(items, [], []);
  146. test.done();
  147. });
  148. };
  149. // Page contexts should cause their items to be absent from the menu when the
  150. // menu is invoked on an active element.
  151. exports.testPageContextNoMatch = function (assert, done) {
  152. let test = new TestHelper(assert, done);
  153. let loader = test.newLoader();
  154. let items = [
  155. new loader.cm.Item({
  156. label: "item 0"
  157. }),
  158. new loader.cm.Item({
  159. label: "item 1",
  160. context: undefined
  161. }),
  162. new loader.cm.Item({
  163. label: "item 2",
  164. context: loader.cm.PageContext()
  165. }),
  166. new loader.cm.Item({
  167. label: "item 3",
  168. context: [loader.cm.PageContext()]
  169. })
  170. ];
  171. test.withTestDoc(function (window, doc) {
  172. test.showMenu(doc.getElementById("image"), function (popup) {
  173. test.checkMenu(items, items, []);
  174. test.done();
  175. });
  176. });
  177. };
  178. // Selection contexts should cause items to appear when a selection exists.
  179. exports.testSelectionContextMatch = function (assert, done) {
  180. let test = new TestHelper(assert, done);
  181. let loader = test.newLoader();
  182. let item = loader.cm.Item({
  183. label: "item",
  184. context: loader.cm.SelectionContext()
  185. });
  186. test.withTestDoc(function (window, doc) {
  187. window.getSelection().selectAllChildren(doc.body);
  188. test.showMenu(null, function (popup) {
  189. test.checkMenu([item], [], []);
  190. test.done();
  191. });
  192. });
  193. };
  194. // Selection contexts should cause items to appear when a selection exists in
  195. // a text field.
  196. exports.testSelectionContextMatchInTextField = function (assert, done) {
  197. let test = new TestHelper(assert, done);
  198. let loader = test.newLoader();
  199. let item = loader.cm.Item({
  200. label: "item",
  201. context: loader.cm.SelectionContext()
  202. });
  203. test.withTestDoc(function (window, doc) {
  204. let textfield = doc.getElementById("textfield");
  205. textfield.setSelectionRange(0, textfield.value.length);
  206. test.showMenu(textfield, function (popup) {
  207. test.checkMenu([item], [], []);
  208. test.done();
  209. });
  210. });
  211. };
  212. // Selection contexts should not cause items to appear when a selection does
  213. // not exist in a text field.
  214. exports.testSelectionContextNoMatchInTextField = function (assert, done) {
  215. let test = new TestHelper(assert, done);
  216. let loader = test.newLoader();
  217. let item = loader.cm.Item({
  218. label: "item",
  219. context: loader.cm.SelectionContext()
  220. });
  221. test.withTestDoc(function (window, doc) {
  222. let textfield = doc.getElementById("textfield");
  223. textfield.setSelectionRange(0, 0);
  224. test.showMenu(textfield, function (popup) {
  225. test.checkMenu([item], [item], []);
  226. test.done();
  227. });
  228. });
  229. };
  230. // Selection contexts should not cause items to appear when a selection does
  231. // not exist.
  232. exports.testSelectionContextNoMatch = function (assert, done) {
  233. let test = new TestHelper(assert, done);
  234. let loader = test.newLoader();
  235. let item = loader.cm.Item({
  236. label: "item",
  237. context: loader.cm.SelectionContext()
  238. });
  239. test.showMenu(null, function (popup) {
  240. test.checkMenu([item], [item], []);
  241. test.done();
  242. });
  243. };
  244. // Selection contexts should cause items to appear when a selection exists even
  245. // for newly opened pages
  246. exports.testSelectionContextInNewTab = function (assert, done) {
  247. let test = new TestHelper(assert, done);
  248. let loader = test.newLoader();
  249. let item = loader.cm.Item({
  250. label: "item",
  251. context: loader.cm.SelectionContext()
  252. });
  253. test.withTestDoc(function (window, doc) {
  254. let link = doc.getElementById("targetlink");
  255. link.click();
  256. test.delayedEventListener(this.tabBrowser, "load", function () {
  257. let browser = test.tabBrowser.selectedBrowser;
  258. let window = browser.contentWindow;
  259. let doc = browser.contentDocument;
  260. window.getSelection().selectAllChildren(doc.body);
  261. test.showMenu(null, function (popup) {
  262. test.checkMenu([item], [], []);
  263. popup.hidePopup();
  264. test.tabBrowser.removeTab(test.tabBrowser.selectedTab);
  265. test.tabBrowser.selectedTab = test.tab;
  266. test.showMenu(null, function (popup) {
  267. test.checkMenu([item], [item], []);
  268. test.done();
  269. });
  270. });
  271. }, true);
  272. });
  273. };
  274. // Selection contexts should work when right clicking a form button
  275. exports.testSelectionContextButtonMatch = function (assert, done) {
  276. let test = new TestHelper(assert, done);
  277. let loader = test.newLoader();
  278. let item = loader.cm.Item({
  279. label: "item",
  280. context: loader.cm.SelectionContext()
  281. });
  282. test.withTestDoc(function (window, doc) {
  283. window.getSelection().selectAllChildren(doc.body);
  284. let button = doc.getElementById("button");
  285. test.showMenu(button, function (popup) {
  286. test.checkMenu([item], [], []);
  287. test.done();
  288. });
  289. });
  290. };
  291. //Selection contexts should work when right clicking a form button
  292. exports.testSelectionContextButtonNoMatch = function (assert, done) {
  293. let test = new TestHelper(assert, done);
  294. let loader = test.newLoader();
  295. let item = loader.cm.Item({
  296. label: "item",
  297. context: loader.cm.SelectionContext()
  298. });
  299. test.withTestDoc(function (window, doc) {
  300. let button = doc.getElementById("button");
  301. test.showMenu(button, function (popup) {
  302. test.checkMenu([item], [item], []);
  303. test.done();
  304. });
  305. });
  306. };
  307. // URL contexts should cause items to appear on pages that match.
  308. exports.testURLContextMatch = function (assert, done) {
  309. let test = new TestHelper(assert, done);
  310. let loader = test.newLoader();
  311. let items = [
  312. loader.cm.Item({
  313. label: "item 0",
  314. context: loader.cm.URLContext(TEST_DOC_URL)
  315. }),
  316. loader.cm.Item({
  317. label: "item 1",
  318. context: loader.cm.URLContext([TEST_DOC_URL, "*.bogus.com"])
  319. }),
  320. loader.cm.Item({
  321. label: "item 2",
  322. context: loader.cm.URLContext([new RegExp(".*\\.html")])
  323. })
  324. ];
  325. test.withTestDoc(function (window, doc) {
  326. test.showMenu(null, function (popup) {
  327. test.checkMenu(items, [], []);
  328. test.done();
  329. });
  330. });
  331. };
  332. // URL contexts should not cause items to appear on pages that do not match.
  333. exports.testURLContextNoMatch = function (assert, done) {
  334. let test = new TestHelper(assert, done);
  335. let loader = test.newLoader();
  336. let items = [
  337. loader.cm.Item({
  338. label: "item 0",
  339. context: loader.cm.URLContext("*.bogus.com")
  340. }),
  341. loader.cm.Item({
  342. label: "item 1",
  343. context: loader.cm.URLContext(["*.bogus.com", "*.gnarly.com"])
  344. }),
  345. loader.cm.Item({
  346. label: "item 2",
  347. context: loader.cm.URLContext([new RegExp(".*\\.js")])
  348. })
  349. ];
  350. test.withTestDoc(function (window, doc) {
  351. test.showMenu(null, function (popup) {
  352. test.checkMenu(items, items, []);
  353. test.done();
  354. });
  355. });
  356. };
  357. // Removing a non-matching URL context after its item is created and the page is
  358. // loaded should cause the item's content script to be evaluated when the
  359. // context menu is next opened.
  360. exports.testURLContextRemove = function (assert, done) {
  361. let test = new TestHelper(assert, done);
  362. let loader = test.newLoader();
  363. let shouldBeEvaled = false;
  364. let context = loader.cm.URLContext("*.bogus.com");
  365. let item = loader.cm.Item({
  366. label: "item",
  367. context: context,
  368. contentScript: 'self.postMessage("ok"); self.on("context", function () true);',
  369. onMessage: function (msg) {
  370. assert.ok(shouldBeEvaled,
  371. "content script should be evaluated when expected");
  372. assert.equal(msg, "ok", "Should have received the right message");
  373. shouldBeEvaled = false;
  374. }
  375. });
  376. test.withTestDoc(function (window, doc) {
  377. test.showMenu(null, function (popup) {
  378. test.checkMenu([item], [item], []);
  379. item.context.remove(context);
  380. shouldBeEvaled = true;
  381. test.hideMenu(function () {
  382. test.showMenu(null, function (popup) {
  383. test.checkMenu([item], [], []);
  384. assert.ok(!shouldBeEvaled,
  385. "content script should have been evaluated");
  386. test.hideMenu(function () {
  387. // Shouldn't get evaluated again
  388. test.showMenu(null, function (popup) {
  389. test.checkMenu([item], [], []);
  390. test.done();
  391. });
  392. });
  393. });
  394. });
  395. });
  396. });
  397. };
  398. // Loading a new page in the same tab should correctly start a new worker for
  399. // any content scripts
  400. exports.testPageReload = function (assert, done) {
  401. let test = new TestHelper(assert, done);
  402. let loader = test.newLoader();
  403. let item = loader.cm.Item({
  404. label: "Item",
  405. contentScript: "var doc = document; self.on('context', function(node) doc.body.getAttribute('showItem') == 'true');"
  406. });
  407. test.withTestDoc(function (window, doc) {
  408. // Set a flag on the document that the item uses
  409. doc.body.setAttribute("showItem", "true");
  410. test.showMenu(null, function (popup) {
  411. // With the attribute true the item should be visible in the menu
  412. test.checkMenu([item], [], []);
  413. test.hideMenu(function() {
  414. let browser = this.tabBrowser.getBrowserForTab(this.tab)
  415. test.delayedEventListener(browser, "load", function() {
  416. test.delayedEventListener(browser, "load", function() {
  417. window = browser.contentWindow;
  418. doc = window.document;
  419. // Set a flag on the document that the item uses
  420. doc.body.setAttribute("showItem", "false");
  421. test.showMenu(null, function (popup) {
  422. // In the new document with the attribute false the item should be
  423. // hidden, but if the contentScript hasn't been reloaded it will
  424. // still see the old value
  425. test.checkMenu([item], [item], []);
  426. test.done();
  427. });
  428. }, true);
  429. browser.loadURI(TEST_DOC_URL, null, null);
  430. }, true);
  431. // Required to make sure we load a new page in history rather than
  432. // just reloading the current page which would unload it
  433. browser.loadURI("about:blank", null, null);
  434. });
  435. });
  436. });
  437. };
  438. // Closing a page after it's been used with a worker should cause the worker
  439. // to be destroyed
  440. /*exports.testWorkerDestroy = function (assert, done) {
  441. let test = new TestHelper(assert, done);
  442. let loader = test.newLoader();
  443. let loadExpected = false;
  444. let item = loader.cm.Item({
  445. label: "item",
  446. contentScript: 'self.postMessage("loaded"); self.on("detach", function () { console.log("saw detach"); self.postMessage("detach") });',
  447. onMessage: function (msg) {
  448. console.log("Saw " + msg)
  449. switch (msg) {
  450. case "loaded":
  451. assert.ok(loadExpected, "Should have seen the load event at the right time");
  452. loadExpected = false;
  453. break;
  454. case "detach":
  455. test.done();
  456. break;
  457. }
  458. }
  459. });
  460. test.withTestDoc(function (window, doc) {
  461. loadExpected = true;
  462. test.showMenu(null, function (popup) {
  463. assert.ok(!loadExpected, "Should have seen a message");
  464. test.checkMenu([item], [], []);
  465. test.closeTab();
  466. });
  467. });
  468. };*/
  469. // Content contexts that return true should cause their items to be present
  470. // in the menu.
  471. exports.testContentContextMatch = function (assert, done) {
  472. let test = new TestHelper(assert, done);
  473. let loader = test.newLoader();
  474. let item = new loader.cm.Item({
  475. label: "item",
  476. contentScript: 'self.on("context", function () true);'
  477. });
  478. test.showMenu(null, function (popup) {
  479. test.checkMenu([item], [], []);
  480. test.done();
  481. });
  482. };
  483. // Content contexts that return false should cause their items to be absent
  484. // from the menu.
  485. exports.testContentContextNoMatch = function (assert, done) {
  486. let test = new TestHelper(assert, done);
  487. let loader = test.newLoader();
  488. let item = new loader.cm.Item({
  489. label: "item",
  490. contentScript: 'self.on("context", function () false);'
  491. });
  492. test.showMenu(null, function (popup) {
  493. test.checkMenu([item], [item], []);
  494. test.done();
  495. });
  496. };
  497. // Content contexts that return undefined should cause their items to be absent
  498. // from the menu.
  499. exports.testContentContextUndefined = function (assert, done) {
  500. let test = new TestHelper(assert, done);
  501. let loader = test.newLoader();
  502. let item = new loader.cm.Item({
  503. label: "item",
  504. contentScript: 'self.on("context", function () {});'
  505. });
  506. test.showMenu(null, function (popup) {
  507. test.checkMenu([item], [item], []);
  508. test.done();
  509. });
  510. };
  511. // Content contexts that return an empty string should cause their items to be
  512. // absent from the menu and shouldn't wipe the label
  513. exports.testContentContextEmptyString = function (assert, done) {
  514. let test = new TestHelper(assert, done);
  515. let loader = test.newLoader();
  516. let item = new loader.cm.Item({
  517. label: "item",
  518. contentScript: 'self.on("context", function () "");'
  519. });
  520. test.showMenu(null, function (popup) {
  521. test.checkMenu([item], [item], []);
  522. assert.equal(item.label, "item", "Label should still be correct");
  523. test.done();
  524. });
  525. };
  526. // If any content contexts returns true then their items should be present in
  527. // the menu.
  528. exports.testMultipleContentContextMatch1 = function (assert, done) {
  529. let test = new TestHelper(assert, done);
  530. let loader = test.newLoader();
  531. let item = new loader.cm.Item({
  532. label: "item",
  533. contentScript: 'self.on("context", function () true); ' +
  534. 'self.on("context", function () false);',
  535. onMessage: function() {
  536. test.fail("Should not have called the second context listener");
  537. }
  538. });
  539. test.showMenu(null, function (popup) {
  540. test.checkMenu([item], [], []);
  541. test.done();
  542. });
  543. };
  544. // If any content contexts returns true then their items should be present in
  545. // the menu.
  546. exports.testMultipleContentContextMatch2 = function (assert, done) {
  547. let test = new TestHelper(assert, done);
  548. let loader = test.newLoader();
  549. let item = new loader.cm.Item({
  550. label: "item",
  551. contentScript: 'self.on("context", function () false); ' +
  552. 'self.on("context", function () true);'
  553. });
  554. test.showMenu(null, function (popup) {
  555. test.checkMenu([item], [], []);
  556. test.done();
  557. });
  558. };
  559. // If any content contexts returns a string then their items should be present
  560. // in the menu.
  561. exports.testMultipleContentContextString1 = function (assert, done) {
  562. let test = new TestHelper(assert, done);
  563. let loader = test.newLoader();
  564. let item = new loader.cm.Item({
  565. label: "item",
  566. contentScript: 'self.on("context", function () "new label"); ' +
  567. 'self.on("context", function () false);'
  568. });
  569. test.showMenu(null, function (popup) {
  570. test.checkMenu([item], [], []);
  571. assert.equal(item.label, "new label", "Label should have changed");
  572. test.done();
  573. });
  574. };
  575. // If any content contexts returns a string then their items should be present
  576. // in the menu.
  577. exports.testMultipleContentContextString2 = function (assert, done) {
  578. let test = new TestHelper(assert, done);
  579. let loader = test.newLoader();
  580. let item = new loader.cm.Item({
  581. label: "item",
  582. contentScript: 'self.on("context", function () false); ' +
  583. 'self.on("context", function () "new label");'
  584. });
  585. test.showMenu(null, function (popup) {
  586. test.checkMenu([item], [], []);
  587. assert.equal(item.label, "new label", "Label should have changed");
  588. test.done();
  589. });
  590. };
  591. // If many content contexts returns a string then the first should take effect
  592. exports.testMultipleContentContextString3 = function (assert, done) {
  593. let test = new TestHelper(assert, done);
  594. let loader = test.newLoader();
  595. let item = new loader.cm.Item({
  596. label: "item",
  597. contentScript: 'self.on("context", function () "new label 1"); ' +
  598. 'self.on("context", function () "new label 2");'
  599. });
  600. test.showMenu(null, function (popup) {
  601. test.checkMenu([item], [], []);
  602. assert.equal(item.label, "new label 1", "Label should have changed");
  603. test.done();
  604. });
  605. };
  606. // Content contexts that return true should cause their items to be present
  607. // in the menu when context clicking an active element.
  608. exports.testContentContextMatchActiveElement = function (assert, done) {
  609. let test = new TestHelper(assert, done);
  610. let loader = test.newLoader();
  611. let items = [
  612. new loader.cm.Item({
  613. label: "item 1",
  614. contentScript: 'self.on("context", function () true);'
  615. }),
  616. new loader.cm.Item({
  617. label: "item 2",
  618. context: undefined,
  619. contentScript: 'self.on("context", function () true);'
  620. }),
  621. // These items will always be hidden by the declarative usage of PageContext
  622. new loader.cm.Item({
  623. label: "item 3",
  624. context: loader.cm.PageContext(),
  625. contentScript: 'self.on("context", function () true);'
  626. }),
  627. new loader.cm.Item({
  628. label: "item 4",
  629. context: [loader.cm.PageContext()],
  630. contentScript: 'self.on("context", function () true);'
  631. })
  632. ];
  633. test.withTestDoc(function (window, doc) {
  634. test.showMenu(doc.getElementById("image"), function (popup) {
  635. test.checkMenu(items, [items[2], items[3]], []);
  636. test.done();
  637. });
  638. });
  639. };
  640. // Content contexts that return false should cause their items to be absent
  641. // from the menu when context clicking an active element.
  642. exports.testContentContextNoMatchActiveElement = function (assert, done) {
  643. let test = new TestHelper(assert, done);
  644. let loader = test.newLoader();
  645. let items = [
  646. new loader.cm.Item({
  647. label: "item 1",
  648. contentScript: 'self.on("context", function () false);'
  649. }),
  650. new loader.cm.Item({
  651. label: "item 2",
  652. context: undefined,
  653. contentScript: 'self.on("context", function () false);'
  654. }),
  655. // These items will always be hidden by the declarative usage of PageContext
  656. new loader.cm.Item({
  657. label: "item 3",
  658. context: loader.cm.PageContext(),
  659. contentScript: 'self.on("context", function () false);'
  660. }),
  661. new loader.cm.Item({
  662. label: "item 4",
  663. context: [loader.cm.PageContext()],
  664. contentScript: 'self.on("context", function () false);'
  665. })
  666. ];
  667. test.withTestDoc(function (window, doc) {
  668. test.showMenu(doc.getElementById("image"), function (popup) {
  669. test.checkMenu(items, items, []);
  670. test.done();
  671. });
  672. });
  673. };
  674. // Content contexts that return undefined should cause their items to be absent
  675. // from the menu when context clicking an active element.
  676. exports.testContentContextNoMatchActiveElement = function (assert, done) {
  677. let test = new TestHelper(assert, done);
  678. let loader = test.newLoader();
  679. let items = [
  680. new loader.cm.Item({
  681. label: "item 1",
  682. contentScript: 'self.on("context", function () {});'
  683. }),
  684. new loader.cm.Item({
  685. label: "item 2",
  686. context: undefined,
  687. contentScript: 'self.on("context", function () {});'
  688. }),
  689. // These items will always be hidden by the declarative usage of PageContext
  690. new loader.cm.Item({
  691. label: "item 3",
  692. context: loader.cm.PageContext(),
  693. contentScript: 'self.on("context", function () {});'
  694. }),
  695. new loader.cm.Item({
  696. label: "item 4",
  697. context: [loader.cm.PageContext()],
  698. contentScript: 'self.on("context", function () {});'
  699. })
  700. ];
  701. test.withTestDoc(function (window, doc) {
  702. test.showMenu(doc.getElementById("image"), function (popup) {
  703. test.checkMenu(items, items, []);
  704. test.done();
  705. });
  706. });
  707. };
  708. // Content contexts that return a string should cause their items to be present
  709. // in the menu and the items' labels to be updated.
  710. exports.testContentContextMatchString = function (assert, done) {
  711. let test = new TestHelper(assert, done);
  712. let loader = test.newLoader();
  713. let item = new loader.cm.Item({
  714. label: "first label",
  715. contentScript: 'self.on("context", function () "second label");'
  716. });
  717. test.showMenu(null, function (popup) {
  718. test.checkMenu([item], [], []);
  719. assert.equal(item.label, "second label",
  720. "item's label should be updated");
  721. test.done();
  722. });
  723. };
  724. // Ensure that contentScriptFile is working correctly
  725. exports.testContentScriptFile = function (assert, done) {
  726. let test = new TestHelper(assert, done);
  727. let loader = test.newLoader();
  728. // Reject remote files
  729. assert.throws(function() {
  730. new loader.cm.Item({
  731. label: "item",
  732. contentScriptFile: "http://mozilla.com/context-menu.js"
  733. });
  734. },
  735. new RegExp("The 'contentScriptFile' option must be a local file URL " +
  736. "or an array of local file URLs."),
  737. "Item throws when contentScriptFile is a remote URL");
  738. // But accept files from data folder
  739. let item = new loader.cm.Item({
  740. label: "item",
  741. contentScriptFile: data.url("test-context-menu.js")
  742. });
  743. test.showMenu(null, function (popup) {
  744. test.checkMenu([item], [], []);
  745. test.done();
  746. });
  747. };
  748. // The args passed to context listeners should be correct.
  749. exports.testContentContextArgs = function (assert, done) {
  750. let test = new TestHelper(assert, done);
  751. let loader = test.newLoader();
  752. let callbacks = 0;
  753. let item = new loader.cm.Item({
  754. label: "item",
  755. contentScript: 'self.on("context", function (node) {' +
  756. ' self.postMessage(node.tagName);' +
  757. ' return false;' +
  758. '});',
  759. onMessage: function (tagName) {
  760. assert.equal(tagName, "HTML", "node should be an HTML element");
  761. if (++callbacks == 2) test.done();
  762. }
  763. });
  764. test.showMenu(null, function () {
  765. if (++callbacks == 2) test.done();
  766. });
  767. };
  768. // Multiple contexts imply intersection, not union, and content context
  769. // listeners should not be called if all declarative contexts are not current.
  770. exports.testMultipleContexts = function (assert, done) {
  771. let test = new TestHelper(assert, done);
  772. let loader = test.newLoader();
  773. let item = new loader.cm.Item({
  774. label: "item",
  775. context: [loader.cm.SelectorContext("a[href]"), loader.cm.PageContext()],
  776. contentScript: 'self.on("context", function () self.postMessage());',
  777. onMessage: function () {
  778. test.fail("Context listener should not be called");
  779. }
  780. });
  781. test.withTestDoc(function (window, doc) {
  782. test.showMenu(doc.getElementById("span-link"), function (popup) {
  783. test.checkMenu([item], [item], []);
  784. test.done();
  785. });
  786. });
  787. };
  788. // Once a context is removed, it should no longer cause its item to appear.
  789. exports.testRemoveContext = function (assert, done) {
  790. let test = new TestHelper(assert, done);
  791. let loader = test.newLoader();
  792. let ctxt = loader.cm.SelectorContext("img");
  793. let item = new loader.cm.Item({
  794. label: "item",
  795. context: ctxt
  796. });
  797. test.withTestDoc(function (window, doc) {
  798. test.showMenu(doc.getElementById("image"), function (popup) {
  799. // The item should be present at first.
  800. test.checkMenu([item], [], []);
  801. popup.hidePopup();
  802. // Remove the img context and check again.
  803. item.context.remove(ctxt);
  804. test.showMenu(doc.getElementById("image"), function (popup) {
  805. test.checkMenu([item], [item], []);
  806. test.done();
  807. });
  808. });
  809. });
  810. };
  811. // Lots of items should overflow into the overflow submenu.
  812. exports.testOverflow = function (assert, done) {
  813. let test = new TestHelper(assert, done);
  814. let loader = test.newLoader();
  815. let items = [];
  816. for (let i = 0; i < OVERFLOW_THRESH_DEFAULT + 1; i++) {
  817. let item = new loader.cm.Item({ label: "item " + i });
  818. items.push(item);
  819. }
  820. test.showMenu(null, function (popup) {
  821. test.checkMenu(items, [], []);
  822. test.done();
  823. });
  824. };
  825. // Module unload should cause all items to be removed.
  826. exports.testUnload = function (assert, done) {
  827. let test = new TestHelper(assert, done);
  828. let loader = test.newLoader();
  829. let item = new loader.cm.Item({ label: "item" });
  830. test.showMenu(null, function (popup) {
  831. // The menu should contain the item.
  832. test.checkMenu([item], [], []);
  833. popup.hidePopup();
  834. // Unload the module.
  835. loader.unload();
  836. test.showMenu(null, function (popup) {
  837. // The item should be removed from the menu.
  838. test.checkMenu([item], [], [item]);
  839. test.done();
  840. });
  841. });
  842. };
  843. // Using multiple module instances to add items without causing overflow should
  844. // work OK. Assumes OVERFLOW_THRESH_DEFAULT >= 2.
  845. exports.testMultipleModulesAdd = function (assert, done) {
  846. let test = new TestHelper(assert, done);
  847. let loader0 = test.newLoader();
  848. let loader1 = test.newLoader();
  849. // Use each module to add an item, then unload each module in turn.
  850. let item0 = new loader0.cm.Item({ label: "item 0" });
  851. let item1 = new loader1.cm.Item({ label: "item 1" });
  852. test.showMenu(null, function (popup) {
  853. // The menu should contain both items.
  854. test.checkMenu([item0, item1], [], []);
  855. popup.hidePopup();
  856. // Unload the first module.
  857. loader0.unload();
  858. test.showMenu(null, function (popup) {
  859. // The first item should be removed from the menu.
  860. test.checkMenu([item0, item1], [], [item0]);
  861. popup.hidePopup();
  862. // Unload the second module.
  863. loader1.unload();
  864. test.showMenu(null, function (popup) {
  865. // Both items should be removed from the menu.
  866. test.checkMenu([item0, item1], [], [item0, item1]);
  867. test.done();
  868. });
  869. });
  870. });
  871. };
  872. // Using multiple module instances to add items causing overflow should work OK.
  873. exports.testMultipleModulesAddOverflow = function (assert, done) {
  874. let test = new TestHelper(assert, done);
  875. let loader0 = test.newLoader();
  876. let loader1 = test.newLoader();
  877. // Use module 0 to add OVERFLOW_THRESH_DEFAULT items.
  878. let items0 = [];
  879. for (let i = 0; i < OVERFLOW_THRESH_DEFAULT; i++) {
  880. let item = new loader0.cm.Item({ label: "item 0 " + i });
  881. items0.push(item);
  882. }
  883. // Use module 1 to add one item.
  884. let item1 = new loader1.cm.Item({ label: "item 1" });
  885. let allItems = items0.concat(item1);
  886. test.showMenu(null, function (popup) {
  887. // The menu should contain all items in overflow.
  888. test.checkMenu(allItems, [], []);
  889. popup.hidePopup();
  890. // Unload the first module.
  891. loader0.unload();
  892. test.showMenu(null, function (popup) {
  893. // The first items should be removed from the menu, which should not
  894. // overflow.
  895. test.checkMenu(allItems, [], items0);
  896. popup.hidePopup();
  897. // Unload the second module.
  898. loader1.unload();
  899. test.showMenu(null, function (popup) {
  900. // All items should be removed from the menu.
  901. test.checkMenu(allItems, [], allItems);
  902. test.done();
  903. });
  904. });
  905. });
  906. };
  907. // Using multiple module instances to modify the menu without causing overflow
  908. // should work OK. This test creates two loaders and:
  909. // loader0 create item -> loader1 create item -> loader0.unload ->
  910. // loader1.unload
  911. exports.testMultipleModulesDiffContexts1 = function (assert, done) {
  912. let test = new TestHelper(assert, done);
  913. let loader0 = test.newLoader();
  914. let loader1 = test.newLoader();
  915. let item0 = new loader0.cm.Item({
  916. label: "item 0",
  917. context: loader0.cm.SelectorContext("img")
  918. });
  919. let item1 = new loader1.cm.Item({ label: "item 1" });
  920. test.showMenu(null, function (popup) {
  921. // The menu should contain item1.
  922. test.checkMenu([item0, item1], [item0], []);
  923. popup.hidePopup();
  924. // Unload module 0.
  925. loader0.unload();
  926. test.showMenu(null, function (popup) {
  927. // item0 should be removed from the menu.
  928. test.checkMenu([item0, item1], [], [item0]);
  929. popup.hidePopup();
  930. // Unload module 1.
  931. loader1.unload();
  932. test.showMenu(null, function (popup) {
  933. // Both items should be removed from the menu.
  934. test.checkMenu([item0, item1], [], [item0, item1]);
  935. test.done();
  936. });
  937. });
  938. });
  939. };
  940. // Using multiple module instances to modify the menu without causing overflow
  941. // should work OK. This test creates two loaders and:
  942. // loader1 create item -> loader0 create item -> loader0.unload ->
  943. // loader1.unload
  944. exports.testMultipleModulesDiffContexts2 = function (assert, done) {
  945. let test = new TestHelper(assert, done);
  946. let loader0 = test.newLoader();
  947. let loader1 = test.newLoader();
  948. let item1 = new loader1.cm.Item({ label: "item 1" });
  949. let item0 = new loader0.cm.Item({
  950. label: "item 0",
  951. context: loader0.cm.SelectorContext("img")
  952. });
  953. test.showMenu(null, function (popup) {
  954. // The menu should contain item1.
  955. test.checkMenu([item0, item1], [item0], []);
  956. popup.hidePopup();
  957. // Unload module 0.
  958. loader0.unload();
  959. test.showMenu(null, function (popup) {
  960. // item0 should be removed from the menu.
  961. test.checkMenu([item0, item1], [], [item0]);
  962. popup.hidePopup();
  963. // Unload module 1.
  964. loader1.unload();
  965. test.showMenu(null, function (popup) {
  966. // Both items should be removed from the menu.
  967. test.checkMenu([item0, item1], [], [item0, item1]);
  968. test.done();
  969. });
  970. });
  971. });
  972. };
  973. // Using multiple module instances to modify the menu without causing overflow
  974. // should work OK. This test creates two loaders and:
  975. // loader0 create item -> loader1 create item -> loader1.unload ->
  976. // loader0.unload
  977. exports.testMultipleModulesDiffContexts3 = function (assert, done) {
  978. let test = new TestHelper(assert, done);
  979. let loader0 = test.newLoader();
  980. let loader1 = test.newLoader();
  981. let item0 = new loader0.cm.Item({
  982. label: "item 0",
  983. context: loader0.cm.SelectorContext("img")
  984. });
  985. let item1 = new loader1.cm.Item({ label: "item 1" });
  986. test.showMenu(null, function (popup) {
  987. // The menu should contain item1.
  988. test.checkMenu([item0, item1], [item0], []);
  989. popup.hidePopup();
  990. // Unload module 1.
  991. loader1.unload();
  992. test.showMenu(null, function (popup) {
  993. // item1 should be removed from the menu.
  994. test.checkMenu([item0, item1], [item0], [item1]);
  995. popup.hidePopup();
  996. // Unload module 0.
  997. loader0.unload();
  998. test.showMenu(null, function (popup) {
  999. // Both items should be removed from the menu.
  1000. test.checkMenu([item0, item1], [], [item0, item1]);
  1001. test.done();
  1002. });
  1003. });
  1004. });
  1005. };
  1006. // Using multiple module instances to modify the menu without causing overflow
  1007. // should work OK. This test creates two loaders and:
  1008. // loader1 create item -> loader0 create item -> loader1.unload ->
  1009. // loader0.unload
  1010. exports.testMultipleModulesDiffContexts4 = function (assert, done) {
  1011. let test = new TestHelper(assert, done);
  1012. let loader0 = test.newLoader();
  1013. let loader1 = test.newLoader();
  1014. let item1 = new loader1.cm.Item({ label: "item 1" });
  1015. let item0 = new loader0.cm.Item({
  1016. label: "item 0",
  1017. context: loader0.cm.SelectorContext("img")
  1018. });
  1019. test.showMenu(null, function (popup) {
  1020. // The menu should contain item1.
  1021. test.checkMenu([item0, item1], [item0], []);
  1022. popup.hidePopup();
  1023. // Unload module 1.
  1024. loader1.unload();
  1025. test.showMenu(null, function (popup) {
  1026. // item1 should be removed from the menu.
  1027. test.checkMenu([item0, item1], [item0], [item1]);
  1028. popup.hidePopup();
  1029. // Unload module 0.
  1030. loader0.unload();
  1031. test.showMenu(null, function (popup) {
  1032. // Both items should be removed from the menu.
  1033. test.checkMenu([item0, item1], [], [item0, item1]);
  1034. test.done();
  1035. });
  1036. });
  1037. });
  1038. };
  1039. // Test interactions between a loaded module, unloading another module, and the
  1040. // menu separator and overflow submenu.
  1041. exports.testMultipleModulesAddRemove = function (assert, done) {
  1042. let test = new TestHelper(assert, done);
  1043. let loader0 = test.newLoader();
  1044. let loader1 = test.newLoader();
  1045. let item = new loader0.cm.Item({ label: "item" });
  1046. test.showMenu(null, function (popup) {
  1047. // The menu should contain the item.
  1048. test.checkMenu([item], [], []);
  1049. popup.hidePopup();
  1050. // Remove the item.
  1051. item.destroy();
  1052. test.showMenu(null, function (popup) {
  1053. // The item should be removed from the menu.
  1054. test.checkMenu([item], [], [item]);
  1055. popup.hidePopup();
  1056. // Unload module 1.
  1057. loader1.unload();
  1058. test.showMenu(null, function (popup) {
  1059. // There shouldn't be any errors involving the menu separator or
  1060. // overflow submenu.
  1061. test.checkMenu([item], [], [item]);
  1062. test.done();
  1063. });
  1064. });
  1065. });
  1066. };
  1067. // Checks that the order of menu items is correct when adding/removing across
  1068. // multiple modules. All items from a single module should remain in a group
  1069. exports.testMultipleModulesOrder = function (assert, done) {
  1070. let test = new TestHelper(assert, done);
  1071. let loader0 = test.newLoader();
  1072. let loader1 = test.newLoader();
  1073. // Use each module to add an item, then unload each module in turn.
  1074. let item0 = new loader0.cm.Item({ label: "item 0" });
  1075. let item1 = new loader1.cm.Item({ label: "item 1" });
  1076. test.showMenu(null, function (popup) {
  1077. // The menu should contain both items.
  1078. test.checkMenu([item0, item1], [], []);
  1079. popup.hidePopup();
  1080. let item2 = new loader0.cm.Item({ label: "item 2" });
  1081. test.showMenu(null, function (popup) {
  1082. // The new item should be grouped with the same items from loader0.
  1083. test.checkMenu([item0, item2, item1], [], []);
  1084. popup.hidePopup();
  1085. let item3 = new loader1.cm.Item({ label: "item 3" });
  1086. test.showMenu(null, function (popup) {
  1087. // Same again
  1088. test.checkMenu([item0, item2, item1, item3], [], []);
  1089. test.done();
  1090. });
  1091. });
  1092. });
  1093. };
  1094. // Checks that the order of menu items is correct when adding/removing across
  1095. // multiple modules when overflowing. All items from a single module should
  1096. // remain in a group
  1097. exports.testMultipleModulesOrderOverflow = function (assert, done) {
  1098. let test = new TestHelper(assert, done);
  1099. let loader0 = test.newLoader();
  1100. let loader1 = test.newLoader();
  1101. let prefs = loader0.loader.require("sdk/preferences/service");
  1102. prefs.set(OVERFLOW_THRESH_PREF, 0);
  1103. // Use each module to add an item, then unload each module in turn.
  1104. let item0 = new loader0.cm.Item({ label: "item 0" });
  1105. let item1 = new loader1.cm.Item({ label: "item 1" });
  1106. test.showMenu(null, function (popup) {
  1107. // The menu should contain both items.
  1108. test.checkMenu([item0, item1], [], []);
  1109. popup.hidePopup();
  1110. let item2 = new loader0.cm.Item({ label: "item 2" });
  1111. test.showMenu(null, function (popup) {
  1112. // The new item should be grouped with the same items from loader0.
  1113. test.checkMenu([item0, item2, item1], [], []);
  1114. popup.hidePopup();
  1115. let item3 = new loader1.cm.Item({ label: "item 3" });
  1116. test.showMenu(null, function (popup) {
  1117. // Same again
  1118. test.checkMenu([item0, item2, item1, item3], [], []);
  1119. test.done();
  1120. });
  1121. });
  1122. });
  1123. };
  1124. // Checks that if a module's items are all hidden then the overflow menu doesn't
  1125. // get hidden
  1126. exports.testMultipleModulesOverflowHidden = function (assert, done) {
  1127. let test = new TestHelper(assert, done);
  1128. let loader0 = test.newLoader();
  1129. let loader1 = test.newLoader();
  1130. let prefs = loader0.loader.require("sdk/preferences/service");
  1131. prefs.set(OVERFLOW_THRESH_PREF, 0);
  1132. // Use each module to add an item, then unload each module in turn.
  1133. let item0 = new loader0.cm.Item({ label: "item 0" });
  1134. let item1 = new loader1.cm.Item({
  1135. label: "item 1",
  1136. context: loader1.cm.SelectorContext("a")
  1137. });
  1138. test.showMenu(null, function (popup) {
  1139. // One should be hidden
  1140. test.checkMenu([item0, item1], [item1], []);
  1141. test.done();
  1142. });
  1143. };
  1144. // Checks that if a module's items are all hidden then the overflow menu doesn't
  1145. // get hidden (reverse order to above)
  1146. exports.testMultipleModulesOverflowHidden2 = function (assert, done) {
  1147. let test = new TestHelper(assert, done);
  1148. let loader0 = test.newLoader();
  1149. let loader1 = test.newLoader();
  1150. let prefs = loader0.loader.require("sdk/preferences/service");
  1151. prefs.set(OVERFLOW_THRESH_PREF, 0);
  1152. // Use each module to add an item, then unload each module in turn.
  1153. let item0 = new loader0.cm.Item({
  1154. label: "item 0",
  1155. context: loader0.cm.SelectorContext("a")
  1156. });
  1157. let item1 = new loader1.cm.Item({ label: "item 1" });
  1158. test.showMenu(null, function (popup) {
  1159. // One should be hidden
  1160. test.checkMenu([item0, item1], [item0], []);
  1161. test.done();
  1162. });
  1163. };
  1164. // Checks that we don't overflow if there are more items than the overflow
  1165. // threshold but not all of them are visible
  1166. exports.testOverflowIgnoresHidden = function (assert, done) {
  1167. let test = new TestHelper(assert, done);
  1168. let loader = test.newLoader();
  1169. let prefs = loader.loader.require("sdk/preferences/service");
  1170. prefs.set(OVERFLOW_THRESH_PREF, 2);
  1171. let allItems = [
  1172. new loader.cm.Item({
  1173. label: "item 0"
  1174. }),
  1175. new loader.cm.Item({
  1176. label: "item 1"
  1177. }),
  1178. new loader.cm.Item({
  1179. label: "item 2",
  1180. context: loader.cm.SelectorContext("a")
  1181. })
  1182. ];
  1183. test.showMenu(null, function (popup) {
  1184. // One should be hidden
  1185. test.checkMenu(allItems, [allItems[2]], []);
  1186. test.done();
  1187. });
  1188. };
  1189. // Checks that we don't overflow if there are more items than the overflow
  1190. // threshold but not all of them are visible
  1191. exports.testOverflowIgnoresHiddenMultipleModules1 = function (assert, done) {
  1192. let test = new TestHelper(assert, done);
  1193. let loader0 = test.newLoader();
  1194. let loader1 = test.newLoader();
  1195. let prefs = loader0.loader.require("sdk/preferences/service");
  1196. prefs.set(OVERFLOW_THRESH_PREF, 2);
  1197. let allItems = [
  1198. new loader0.cm.Item({
  1199. label: "item 0"
  1200. }),
  1201. new loader0.cm.Item({
  1202. label: "item 1"
  1203. }),
  1204. new loader1.cm.Item({
  1205. label: "item 2",
  1206. context: loader1.cm.SelectorContext("a")
  1207. }),
  1208. new loader1.cm.Item({
  1209. label: "item 3",
  1210. context: loader1.cm.SelectorContext("a")
  1211. })
  1212. ];
  1213. test.showMenu(null, function (popup) {
  1214. // One should be hidden
  1215. test.checkMenu(allItems, [allItems[2], allItems[3]], []);
  1216. test.done();
  1217. });
  1218. };
  1219. // Checks that we don't overflow if there are more items than the overflow
  1220. // threshold but not all of them are visible
  1221. exports.testOverflowIgnoresHiddenMultipleModules2 = function (assert, done) {
  1222. let test = new TestHelper(assert, done);
  1223. let loader0 = test.newLoader();
  1224. let loader1 = test.newLoader();
  1225. let prefs = loader0.loader.require("sdk/preferences/service");
  1226. prefs.set(OVERFLOW_THRESH_PREF, 2);
  1227. let allItems = [
  1228. new loader0.cm.Item({
  1229. label: "item 0"
  1230. }),
  1231. new loader0.cm.Item({
  1232. label: "item 1",
  1233. context: loader0.cm.SelectorContext("a")
  1234. }),
  1235. new loader1.cm.Item({
  1236. label: "item 2"
  1237. }),
  1238. new loader1.cm.Item({
  1239. label: "item 3",
  1240. context: loader1.cm.SelectorContext("a")
  1241. })
  1242. ];
  1243. test.showMenu(null, function (popup) {
  1244. // One should be hidden
  1245. test.checkMenu(allItems, [allItems[1], allItems[3]], []);
  1246. test.done();
  1247. });
  1248. };
  1249. // Checks that we don't overflow if there are more items than the overflow
  1250. // threshold but not all of them are visible
  1251. exports.testOverflowIgnoresHiddenMultipleModules3 = function (assert, done) {
  1252. let test = new TestHelper(assert, done);
  1253. let loader0 = test.newLoader();
  1254. let loader1 = test.newLoader();
  1255. let prefs = loader0.loader.require("sdk/preferences/service");
  1256. prefs.set(OVERFLOW_THRESH_PREF, 2);
  1257. let allItems = [
  1258. new loader0.cm.Item({
  1259. label: "item 0",
  1260. context: loader0.cm.SelectorContext("a")
  1261. }),
  1262. new loader0.cm.Item({
  1263. label: "item 1",
  1264. context: loader0.cm.SelectorContext("a")
  1265. }),
  1266. new loader1.cm.Item({
  1267. label: "item 2"
  1268. }),
  1269. new loader1.cm.Item({
  1270. label: "item 3"
  1271. })
  1272. ];
  1273. test.showMenu(null, function (popup) {
  1274. // One should be hidden
  1275. test.checkMenu(allItems, [allItems[0], allItems[1]], []);
  1276. test.done();
  1277. });
  1278. };
  1279. // Tests that we transition between overflowing to non-overflowing to no items
  1280. // and back again
  1281. exports.testOverflowTransition = function (assert, done) {
  1282. let test = new TestHelper(assert, done);
  1283. let loader = test.newLoader();
  1284. let prefs = loader.loader.require("sdk/preferences/service");
  1285. prefs.set(OVERFLOW_THRESH_PREF, 2);
  1286. let pItems = [
  1287. new loader.cm.Item({
  1288. label: "item 0",
  1289. context: loader.cm.SelectorContext("p")
  1290. }),
  1291. new loader.cm.Item({
  1292. label: "item 1",
  1293. context: loader.cm.SelectorContext("p")
  1294. })
  1295. ];
  1296. let aItems = [
  1297. new loader.cm.Item({
  1298. label: "item 2",
  1299. context: loader.cm.SelectorContext("a")
  1300. }),
  1301. new loader.cm.Item({
  1302. label: "item 3",
  1303. context: loader.cm.SelectorContext("a")
  1304. })
  1305. ];
  1306. let allItems = pItems.concat(aItems);
  1307. test.withTestDoc(function (window, doc) {
  1308. test.showMenu(doc.getElementById("link"), function (popup) {
  1309. // The menu should contain all items and will overflow
  1310. test.checkMenu(allItems, [], []);
  1311. popup.hidePopup();
  1312. test.showMenu(doc.getElementById("text"), function (popup) {
  1313. // Only contains hald the items and will not overflow
  1314. test.checkMenu(allItems, aItems, []);
  1315. popup.hidePopup();
  1316. test.showMenu(null, function (popup) {
  1317. // None of the items will be visible
  1318. test.checkMenu(allItems, allItems, []);
  1319. popup.hidePopup();
  1320. test.showMenu(doc.getElementById("text"), function (popup) {
  1321. // Only contains hald the items and will not overflow
  1322. test.checkMenu(allItems, aItems, []);
  1323. popup.hidePopup();
  1324. test.showMenu(doc.getElementById("link"), function (popup) {
  1325. // The menu should contain all items and will overflow
  1326. test.checkMenu(allItems, [], []);
  1327. popup.hidePopup();
  1328. test.showMenu(null, function (popup) {
  1329. // None of the items will be visible
  1330. test.checkMenu(allItems, allItems, []);
  1331. popup.hidePopup();
  1332. test.showMenu(doc.getElementById("link"), function (popup) {
  1333. // The menu should contain all items and will overflow
  1334. test.checkMenu(allItems, [], []);
  1335. test.done();
  1336. });
  1337. });
  1338. });
  1339. });
  1340. });
  1341. });
  1342. });
  1343. });
  1344. };
  1345. // An item's command listener should work.
  1346. exports.testItemCommand = function (assert, done) {
  1347. let test = new TestHelper(assert, done);
  1348. let loader = test.newLoader();
  1349. let item = new loader.cm.Item({
  1350. label: "item",
  1351. data: "item data",
  1352. contentScript: 'self.on("click", function (node, data) {' +
  1353. ' self.postMessage({' +
  1354. ' tagName: node.tagName,' +
  1355. ' data: data' +
  1356. ' });' +
  1357. '});',
  1358. onMessage: function (data) {
  1359. assert.equal(this, item, "`this` inside onMessage should be item");
  1360. assert.equal(data.tagName, "HTML", "node should be an HTML element");
  1361. assert.equal(data.data, item.data, "data should be item data");
  1362. test.done();
  1363. }
  1364. });
  1365. test.showMenu(null, function (popup) {
  1366. test.checkMenu([item], [], []);
  1367. let elt = test.getItemElt(popup, item);
  1368. // create a command event
  1369. let evt = elt.ownerDocument.createEvent('Event');
  1370. evt.initEvent('command', true, true);
  1371. elt.dispatchEvent(evt);
  1372. });
  1373. };
  1374. // A menu's click listener should work and receive bubbling 'command' events from
  1375. // sub-items appropriately. This also tests menus and ensures that when a CSS
  1376. // selector context matches the clicked node's ancestor, the matching ancestor
  1377. // is passed to listeners as the clicked node.
  1378. exports.testMenuCommand = function (assert, done) {
  1379. // Create a top-level menu, submenu, and item, like this:
  1380. // topMenu -> submenu -> item
  1381. // Click the item and make sure the click bubbles.
  1382. let test = new TestHelper(assert, done);
  1383. let loader = test.newLoader();
  1384. let item = new loader.cm.Item({
  1385. label: "submenu item",
  1386. data: "submenu item data",
  1387. context: loader.cm.SelectorContext("a"),
  1388. });
  1389. let submenu = new loader.cm.Menu({
  1390. label: "submenu",
  1391. context: loader.cm.SelectorContext("a"),
  1392. items: [item]
  1393. });
  1394. let topMenu = new loader.cm.Menu({
  1395. label: "top menu",
  1396. contentScript: 'self.on("click", function (node, data) {' +
  1397. ' self.postMessage({' +
  1398. ' tagName: node.tagName,' +
  1399. ' data: data' +
  1400. ' });' +
  1401. '});',
  1402. onMessage: function (data) {
  1403. assert.equal(this, topMenu, "`this` inside top menu should be menu");
  1404. assert.equal(data.tagName, "A", "Clicked node should be anchor");
  1405. assert.equal(data.data, item.data,
  1406. "Clicked item data should be correct");
  1407. test.done();
  1408. },
  1409. items: [submenu],
  1410. context: loader.cm.SelectorContext("a")
  1411. });
  1412. test.withTestDoc(function (window, doc) {
  1413. test.showMenu(doc.getElementById("span-link"), function (popup) {
  1414. test.checkMenu([topMenu], [], []);
  1415. let topMenuElt = test.getItemElt(popup, topMenu);
  1416. let topMenuPopup = topMenuElt.firstChild;
  1417. let submenuElt = test.getItemElt(topMenuPopup, submenu);
  1418. let submenuPopup = submenuElt.firstChild;
  1419. let itemElt = test.getItemElt(submenuPopup, item);
  1420. // create a command event
  1421. let evt = itemElt.ownerDocument.createEvent('Event');
  1422. evt.initEvent('command', true, true);
  1423. itemElt.dispatchEvent(evt);
  1424. });
  1425. });
  1426. };
  1427. // Click listeners should work when multiple modules are loaded.
  1428. exports.testItemCommandMultipleModules = function (assert, done) {
  1429. let test = new TestHelper(assert, done);
  1430. let loader0 = test.newLoader();
  1431. let loader1 = test.newLoader();
  1432. let item0 = loader0.cm.Item({
  1433. label: "loader 0 item",
  1434. contentScript: 'self.on("click", self.postMessage);',
  1435. onMessage: function () {
  1436. test.fail("loader 0 item should not emit click event");
  1437. }
  1438. });
  1439. let item1 = loader1.cm.Item({
  1440. label: "loader 1 item",
  1441. contentScript: 'self.on("click", self.postMessage);',
  1442. onMessage: function () {
  1443. test.pass("loader 1 item clicked as expected");
  1444. test.done();
  1445. }
  1446. });
  1447. test.showMenu(null, function (popup) {
  1448. test.checkMenu([item0, item1], [], []);
  1449. let item1Elt = test.getItemElt(popup, item1);
  1450. // create a command event
  1451. let evt = item1Elt.ownerDocument.createEvent('Event');
  1452. evt.initEvent('command', true, true);
  1453. item1Elt.dispatchEvent(evt);
  1454. });
  1455. };
  1456. // An item's click listener should work.
  1457. exports.testItemClick = function (assert, done) {
  1458. let test = new TestHelper(assert, done);
  1459. let loader = test.newLoader();
  1460. let item = new loader.cm.Item({
  1461. label: "item",
  1462. data: "item data",
  1463. contentScript: 'self.on("click", function (node, data) {' +
  1464. ' self.postMessage({' +
  1465. ' tagName: node.tagName,' +
  1466. ' data: data' +
  1467. ' });' +
  1468. '});',
  1469. onMessage: function (data) {
  1470. assert.equal(this, item, "`this` inside onMessage should be item");
  1471. assert.equal(data.tagName, "HTML", "node should be an HTML element");
  1472. assert.equal(data.data, item.data, "data should be item data");
  1473. test.done();
  1474. }
  1475. });
  1476. test.showMenu(null, function (popup) {
  1477. test.checkMenu([item], [], []);
  1478. let elt = test.getItemElt(popup, item);
  1479. elt.click();
  1480. });
  1481. };
  1482. // A menu's click listener should work and receive bubbling clicks from
  1483. // sub-items appropriately. This also tests menus and ensures that when a CSS
  1484. // selector context matches the clicked node's ancestor, the matching ancestor
  1485. // is passed to listeners as the clicked node.
  1486. exports.testMenuClick = function (assert, done) {
  1487. // Create a top-level menu, submenu, and item, like this:
  1488. // topMenu -> submenu -> item
  1489. // Click the item and make sure the click bubbles.
  1490. let test = new TestHelper(assert, done);
  1491. let loader = test.newLoader();
  1492. let item = new loader.cm.Item({
  1493. label: "submenu item",
  1494. data: "submenu item data",
  1495. context: loader.cm.SelectorContext("a"),
  1496. });
  1497. let submenu = new loader.cm.Menu({
  1498. label: "submenu",
  1499. context: loader.cm.SelectorContext("a"),
  1500. items: [item]
  1501. });
  1502. let topMenu = new loader.cm.Menu({
  1503. label: "top menu",
  1504. contentScript: 'self.on("click", function (node, data) {' +
  1505. ' self.postMessage({' +
  1506. ' tagName: node.tagName,' +
  1507. ' data: data' +
  1508. ' });' +
  1509. '});',
  1510. onMessage: function (data) {
  1511. assert.equal(this, topMenu, "`this` inside top menu should be menu");
  1512. assert.equal(data.tagName, "A", "Clicked node should be anchor");
  1513. assert.equal(data.data, item.data,
  1514. "Clicked item data should be correct");
  1515. test.done();
  1516. },
  1517. items: [submenu],
  1518. context: loader.cm.SelectorContext("a")
  1519. });
  1520. test.withTestDoc(function (window, doc) {
  1521. test.showMenu(doc.getElementById("span-link"), function (popup) {
  1522. test.checkMenu([topMenu], [], []);
  1523. let topMenuElt = test.getItemElt(popup, topMenu);
  1524. let topMenuPopup = topMenuElt.firstChild;
  1525. let submenuElt = test.getItemElt(topMenuPopup, submenu);
  1526. let submenuPopup = submenuElt.firstChild;
  1527. let itemElt = test.getItemElt(submenuPopup, item);
  1528. itemElt.click();
  1529. });
  1530. });
  1531. };
  1532. // Click listeners should work when multiple modules are loaded.
  1533. exports.testItemClickMultipleModules = function (assert, done) {
  1534. let test = new TestHelper(assert, done);
  1535. let loader0 = test.newLoader();
  1536. let loader1 = test.newLoader();
  1537. let item0 = loader0.cm.Item({
  1538. label: "loader 0 item",
  1539. contentScript: 'self.on("click", self.postMessage);',
  1540. onMessage: function () {
  1541. test.fail("loader 0 item should not emit click event");
  1542. }
  1543. });
  1544. let item1 = loader1.cm.Item({
  1545. label: "loader 1 item",
  1546. contentScript: 'self.on("click", self.postMessage);',
  1547. onMessage: function () {
  1548. test.pass("loader 1 item clicked as expected");
  1549. test.done();
  1550. }
  1551. });
  1552. test.showMenu(null, function (popup) {
  1553. test.checkMenu([item0, item1], [], []);
  1554. let item1Elt = test.getItemElt(popup, item1);
  1555. item1Elt.click();
  1556. });
  1557. };
  1558. // Adding a separator to a submenu should work OK.
  1559. exports.testSeparator = function (assert, done) {
  1560. let test = new TestHelper(assert, done);
  1561. let loader = test.newLoader();
  1562. let menu = new loader.cm.Menu({
  1563. label: "submenu",
  1564. items: [new loader.cm.Separator()]
  1565. });
  1566. test.showMenu(null, function (popup) {
  1567. test.checkMenu([menu], [], []);
  1568. test.done();
  1569. });
  1570. };
  1571. // The parentMenu option should work
  1572. exports.testParentMenu = function (assert, done) {
  1573. let test = new TestHelper(assert, done);
  1574. let loader = test.newLoader();
  1575. let menu = new loader.cm.Menu({
  1576. label: "submenu",
  1577. items: [loader.cm.Item({ label: "item 1" })],
  1578. parentMenu: loader.cm.contentContextMenu
  1579. });
  1580. let item = loader.cm.Item({
  1581. label: "item 2",
  1582. parentMenu: menu,
  1583. });
  1584. assert.equal(menu.items[1], item, "Item should be in the sub menu");
  1585. test.showMenu(null, function (popup) {
  1586. test.checkMenu([menu], [], []);
  1587. test.done();
  1588. });
  1589. };
  1590. // Existing context menu modifications should apply to new windows.
  1591. exports.testNewWindow = function (assert, done) {
  1592. let test = new TestHelper(assert, done);
  1593. let loader = test.newLoader();
  1594. let item = new loader.cm.Item({ label: "item" });
  1595. test.withNewWindow(function () {
  1596. test.showMenu(null, function (popup) {
  1597. test.checkMenu([item], [], []);
  1598. test.done();
  1599. });
  1600. });
  1601. };
  1602. // When a new window is opened, items added by an unloaded module should not
  1603. // be present in the menu.
  1604. exports.testNewWindowMultipleModules = function (assert, done) {
  1605. let test = new TestHelper(assert, done);
  1606. let loader = test.newLoader();
  1607. let item = new loader.cm.Item({ label: "item" });
  1608. test.showMenu(null, function (popup) {
  1609. test.checkMenu([item], [], []);
  1610. popup.hidePopup();
  1611. loader.unload();
  1612. test.withNewWindow(function () {
  1613. test.showMenu(null, function (popup) {
  1614. test.checkMenu([item], [], [item]);
  1615. test.done();
  1616. });
  1617. });
  1618. });
  1619. };
  1620. // Existing context menu modifications should not apply to new private windows.
  1621. exports.testNewPrivateWindow = function (assert, done) {
  1622. let test = new TestHelper(assert, done);
  1623. let loader = test.newLoader();
  1624. let item = new loader.cm.Item({ label: "item" });
  1625. test.showMenu(null, function (popup) {
  1626. test.checkMenu([item], [], []);
  1627. popup.hidePopup();
  1628. test.withNewPrivateWindow(function () {
  1629. test.showMenu(null, function (popup) {
  1630. test.checkMenu([], [], []);
  1631. test.done();
  1632. });
  1633. });
  1634. });
  1635. };
  1636. // Existing context menu modifications should apply to new private windows when
  1637. // private browsing support is enabled.
  1638. exports.testNewPrivateEnabledWindow = function (assert, done) {
  1639. let test = new TestHelper(assert, done);
  1640. let loader = test.newPrivateLoader();
  1641. let item = new loader.cm.Item({ label: "item" });
  1642. test.showMenu(null, function (popup) {
  1643. test.checkMenu([item], [], []);
  1644. popup.hidePopup();
  1645. test.withNewPrivateWindow(function () {
  1646. test.showMenu(null, function (popup) {
  1647. test.checkMenu([item], [], []);
  1648. test.done();
  1649. });
  1650. });
  1651. });
  1652. };
  1653. // Existing context menu modifications should apply to new private windows when
  1654. // private browsing support is enabled unless unloaded.
  1655. exports.testNewPrivateEnabledWindowUnloaded = function (assert, done) {
  1656. let test = new TestHelper(assert, done);
  1657. let loader = test.newPrivateLoader();
  1658. let item = new loader.cm.Item({ label: "item" });
  1659. test.showMenu(null, function (popup) {
  1660. test.checkMenu([item], [], []);
  1661. popup.hidePopup();
  1662. loader.unload();
  1663. test.withNewPrivateWindow(function () {
  1664. test.showMenu(null, function (popup) {
  1665. test.checkMenu([], [], []);
  1666. test.done();
  1667. });
  1668. });
  1669. });
  1670. };
  1671. // Items in the context menu should be sorted according to locale.
  1672. exports.testSorting = function (assert, done) {
  1673. let test = new TestHelper(assert, done);
  1674. let loader = test.newLoader();
  1675. // Make an unsorted items list. It'll look like this:
  1676. // item 1, item 0, item 3, item 2, item 5, item 4, ...
  1677. let items = [];
  1678. for (let i = 0; i < OVERFLOW_THRESH_DEFAULT; i += 2) {
  1679. items.push(new loader.cm.Item({ label: "item " + (i + 1) }));
  1680. items.push(new loader.cm.Item({ label: "item " + i }));
  1681. }
  1682. test.showMenu(null, function (popup) {
  1683. test.checkMenu(items, [], []);
  1684. test.done();
  1685. });
  1686. };
  1687. // Items in the overflow menu should be sorted according to locale.
  1688. exports.testSortingOverflow = function (assert, done) {
  1689. let test = new TestHelper(assert, done);
  1690. let loader = test.newLoader();
  1691. // Make an unsorted items list. It'll look like this:
  1692. // item 1, item 0, item 3, item 2, item 5, item 4, ...
  1693. let items = [];
  1694. for (let i = 0; i < OVERFLOW_THRESH_DEFAULT * 2; i += 2) {
  1695. items.push(new loader.cm.Item({ label: "item " + (i + 1) }));
  1696. items.push(new loader.cm.Item({ label: "item " + i }));
  1697. }
  1698. test.showMenu(null, function (popup) {
  1699. test.checkMenu(items, [], []);
  1700. test.done();
  1701. });
  1702. };
  1703. // Multiple modules shouldn't interfere with sorting.
  1704. exports.testSortingMultipleModules = function (assert, done) {
  1705. let test = new TestHelper(assert, done);
  1706. let loader0 = test.newLoader();
  1707. let loader1 = test.newLoader();
  1708. let items0 = [];
  1709. let items1 = [];
  1710. for (let i = 0; i < OVERFLOW_THRESH_DEFAULT; i++) {
  1711. if (i % 2) {
  1712. let item = new loader0.cm.Item({ label: "item " + i });
  1713. items0.push(item);
  1714. }
  1715. else {
  1716. let item = new loader1.cm.Item({ label: "item " + i });
  1717. items1.push(item);
  1718. }
  1719. }
  1720. let allItems = items0.concat(items1);
  1721. test.showMenu(null, function (popup) {
  1722. // All items should be present and sorted.
  1723. test.checkMenu(allItems, [], []);
  1724. popup.hidePopup();
  1725. loader0.unload();
  1726. loader1.unload();
  1727. test.showMenu(null, function (popup) {
  1728. // All items should be removed.
  1729. test.checkMenu(allItems, [], allItems);
  1730. test.done();
  1731. });
  1732. });
  1733. };
  1734. // Content click handlers and context handlers should be able to communicate,
  1735. // i.e., they're eval'ed in the same worker and sandbox.
  1736. exports.testContentCommunication = function (assert, done) {
  1737. let test = new TestHelper(assert, done);
  1738. let loader = test.newLoader();
  1739. let item = new loader.cm.Item({
  1740. label: "item",
  1741. contentScript: 'var potato;' +
  1742. 'self.on("context", function () {' +
  1743. ' potato = "potato";' +
  1744. ' return true;' +
  1745. '});' +
  1746. 'self.on("click", function () {' +
  1747. ' self.postMessage(potato);' +
  1748. '});',
  1749. });
  1750. item.on("message", function (data) {
  1751. assert.equal(data, "potato", "That's a lot of potatoes!");
  1752. test.done();
  1753. });
  1754. test.showMenu(null, function (popup) {
  1755. test.checkMenu([item], [], []);
  1756. let elt = test.getItemElt(popup, item);
  1757. elt.click();
  1758. });
  1759. };
  1760. // When the context menu is invoked on a tab that was already open when the
  1761. // module was loaded, it should contain the expected items and content workers
  1762. // should function as expected.
  1763. exports.testLoadWithOpenTab = function (assert, done) {
  1764. let test = new TestHelper(assert, done);
  1765. test.withTestDoc(function (window, doc) {
  1766. let loader = test.newLoader();
  1767. let item = new loader.cm.Item({
  1768. label: "item",
  1769. contentScript:
  1770. 'self.on("click", function () self.postMessage("click"));',
  1771. onMessage: function (msg) {
  1772. if (msg === "click")
  1773. test.done();
  1774. }
  1775. });
  1776. test.showMenu(null, function (popup) {
  1777. test.checkMenu([item], [], []);
  1778. test.getItemElt(popup, item).click();
  1779. });
  1780. });
  1781. };
  1782. // Bug 732716: Ensure that the node given in `click` event works fine
  1783. // (i.e. is correctly wrapped)
  1784. exports.testDrawImageOnClickNode = function (assert, done) {
  1785. let test = new TestHelper(assert, done);
  1786. test.withTestDoc(function (window, doc) {
  1787. let loader = test.newLoader();
  1788. let item = new loader.cm.Item({
  1789. label: "item",
  1790. context: loader.cm.SelectorContext("img"),
  1791. contentScript: "new " + function() {
  1792. self.on("click", function (img, data) {
  1793. let ctx = document.createElement("canvas").getContext("2d");
  1794. ctx.drawImage(img, 1, 1, 1, 1);
  1795. self.postMessage("done");
  1796. });
  1797. },
  1798. onMessage: function (msg) {
  1799. if (msg === "done")
  1800. test.done();
  1801. }
  1802. });
  1803. test.showMenu(doc.getElementById("image"), function (popup) {
  1804. test.checkMenu([item], [], []);
  1805. test.getItemElt(popup, item).click();
  1806. });
  1807. });
  1808. };
  1809. // Setting an item's label before the menu is ever shown should correctly change
  1810. // its label.
  1811. exports.testSetLabelBeforeShow = function (assert, done) {
  1812. let test = new TestHelper(assert, done);
  1813. let loader = test.newLoader();
  1814. let items = [
  1815. new loader.cm.Item({ label: "a" }),
  1816. new loader.cm.Item({ label: "b" })
  1817. ]
  1818. items[0].label = "z";
  1819. assert.equal(items[0].label, "z");
  1820. test.showMenu(null, function (popup) {
  1821. test.checkMenu(items, [], []);
  1822. test.done();
  1823. });
  1824. };
  1825. // Setting an item's label after the menu is shown should correctly change its
  1826. // label.
  1827. exports.testSetLabelAfterShow = function (assert, done) {
  1828. let test = new TestHelper(assert, done);
  1829. let loader = test.newLoader();
  1830. let items = [
  1831. new loader.cm.Item({ label: "a" }),
  1832. new loader.cm.Item({ label: "b" })
  1833. ];
  1834. test.showMenu(null, function (popup) {
  1835. test.checkMenu(items, [], []);
  1836. popup.hidePopup();
  1837. items[0].label = "z";
  1838. assert.equal(items[0].label, "z");
  1839. test.showMenu(null, function (popup) {
  1840. test.checkMenu(items, [], []);
  1841. test.done();
  1842. });
  1843. });
  1844. };
  1845. // Setting an item's label before the menu is ever shown should correctly change
  1846. // its label.
  1847. exports.testSetLabelBeforeShowOverflow = function (assert, done) {
  1848. let test = new TestHelper(assert, done);
  1849. let loader = test.newLoader();
  1850. let prefs = loader.loader.require("sdk/preferences/service");
  1851. prefs.set(OVERFLOW_THRESH_PREF, 0);
  1852. let items = [
  1853. new loader.cm.Item({ label: "a" }),
  1854. new loader.cm.Item({ label: "b" })
  1855. ]
  1856. items[0].label = "z";
  1857. assert.equal(items[0].label, "z");
  1858. test.showMenu(null, function (popup) {
  1859. test.checkMenu(items, [], []);
  1860. test.done();
  1861. });
  1862. };
  1863. // Setting an item's label after the menu is shown should correctly change its
  1864. // label.
  1865. exports.testSetLabelAfterShowOverflow = function (assert, done) {
  1866. let test = new TestHelper(assert, done);
  1867. let loader = test.newLoader();
  1868. let prefs = loader.loader.require("sdk/preferences/service");
  1869. prefs.set(OVERFLOW_THRESH_PREF, 0);
  1870. let items = [
  1871. new loader.cm.Item({ label: "a" }),
  1872. new loader.cm.Item({ label: "b" })
  1873. ];
  1874. test.showMenu(null, function (popup) {
  1875. test.checkMenu(items, [], []);
  1876. popup.hidePopup();
  1877. items[0].label = "z";
  1878. assert.equal(items[0].label, "z");
  1879. test.showMenu(null, function (popup) {
  1880. test.checkMenu(items, [], []);
  1881. test.done();
  1882. });
  1883. });
  1884. };
  1885. // Setting the label of an item in a Menu should work.
  1886. exports.testSetLabelMenuItem = function (assert, done) {
  1887. let test = new TestHelper(assert, done);
  1888. let loader = test.newLoader();
  1889. let menu = loader.cm.Menu({
  1890. label: "menu",
  1891. items: [loader.cm.Item({ label: "a" })]
  1892. });
  1893. menu.items[0].label = "z";
  1894. assert.equal(menu.items[0].label, "z");
  1895. test.showMenu(null, function (popup) {
  1896. test.checkMenu([menu], [], []);
  1897. test.done();
  1898. });
  1899. };
  1900. // Menu.addItem() should work.
  1901. exports.testMenuAddItem = function (assert, done) {
  1902. let test = new TestHelper(assert, done);
  1903. let loader = test.newLoader();
  1904. let menu = loader.cm.Menu({
  1905. label: "menu",
  1906. items: [
  1907. loader.cm.Item({ label: "item 0" })
  1908. ]
  1909. });
  1910. menu.addItem(loader.cm.Item({ label: "item 1" }));
  1911. menu.addItem(loader.cm.Item({ label: "item 2" }));
  1912. assert.equal(menu.items.length, 3,
  1913. "menu should have correct number of items");
  1914. for (let i = 0; i < 3; i++) {
  1915. assert.equal(menu.items[i].label, "item " + i,
  1916. "item label should be correct");
  1917. assert.equal(menu.items[i].parentMenu, menu,
  1918. "item's parent menu should be correct");
  1919. }
  1920. test.showMenu(null, function (popup) {
  1921. test.checkMenu([menu], [], []);
  1922. test.done();
  1923. });
  1924. };
  1925. // Adding the same item twice to a menu should work as expected.
  1926. exports.testMenuAddItemTwice = function (assert, done) {
  1927. let test = new TestHelper(assert, done);
  1928. let loader = test.newLoader();
  1929. let menu = loader.cm.Menu({
  1930. label: "menu",
  1931. items: []
  1932. });
  1933. let subitem = loader.cm.Item({ label: "item 1" })
  1934. menu.addItem(subitem);
  1935. menu.addItem(loader.cm.Item({ label: "item 0" }));
  1936. menu.addItem(subitem);
  1937. assert.equal(menu.items.length, 2,
  1938. "menu should have correct number of items");
  1939. for (let i = 0; i < 2; i++) {
  1940. assert.equal(menu.items[i].label, "item " + i,
  1941. "item label should be correct");
  1942. }
  1943. test.showMenu(null, function (popup) {
  1944. test.checkMenu([menu], [], []);
  1945. test.done();
  1946. });
  1947. };
  1948. // Menu.removeItem() should work.
  1949. exports.testMenuRemoveItem = function (assert, done) {
  1950. let test = new TestHelper(assert, done);
  1951. let loader = test.newLoader();
  1952. let subitem = loader.cm.Item({ label: "item 1" });
  1953. let menu = loader.cm.Menu({
  1954. label: "menu",
  1955. items: [
  1956. loader.cm.Item({ label: "item 0" }),
  1957. subitem,
  1958. loader.cm.Item({ label: "item 2" })
  1959. ]
  1960. });
  1961. // Removing twice should be harmless.
  1962. menu.removeItem(subitem);
  1963. menu.removeItem(subitem);
  1964. assert.equal(subitem.parentMenu, null,
  1965. "item's parent menu should be correct");
  1966. assert.equal(menu.items.length, 2,
  1967. "menu should have correct number of items");
  1968. assert.equal(menu.items[0].label, "item 0",
  1969. "item label should be correct");
  1970. assert.equal(menu.items[1].label, "item 2",
  1971. "item label should be correct");
  1972. test.showMenu(null, function (popup) {
  1973. test.checkMenu([menu], [], []);
  1974. test.done();
  1975. });
  1976. };
  1977. // Adding an item currently contained in one menu to another menu should work.
  1978. exports.testMenuItemSwap = function (assert, done) {
  1979. let test = new TestHelper(assert, done);
  1980. let loader = test.newLoader();
  1981. let subitem = loader.cm.Item({ label: "item" });
  1982. let menu0 = loader.cm.Menu({
  1983. label: "menu 0",
  1984. items: [subitem]
  1985. });
  1986. let menu1 = loader.cm.Menu({
  1987. label: "menu 1",
  1988. items: []
  1989. });
  1990. menu1.addItem(subitem);
  1991. assert.equal(menu0.items.length, 0,
  1992. "menu should have correct number of items");
  1993. assert.equal(menu1.items.length, 1,
  1994. "menu should have correct number of items");
  1995. assert.equal(menu1.items[0].label, "item",
  1996. "item label should be correct");
  1997. assert.equal(subitem.parentMenu, menu1,
  1998. "item's parent menu should be correct");
  1999. test.showMenu(null, function (popup) {
  2000. test.checkMenu([menu0, menu1], [menu0], []);
  2001. test.done();
  2002. });
  2003. };
  2004. // Destroying an item should remove it from its parent menu.
  2005. exports.testMenuItemDestroy = function (assert, done) {
  2006. let test = new TestHelper(assert, done);
  2007. let loader = test.newLoader();
  2008. let subitem = loader.cm.Item({ label: "item" });
  2009. let menu = loader.cm.Menu({
  2010. label: "menu",
  2011. items: [subitem]
  2012. });
  2013. subitem.destroy();
  2014. assert.equal(menu.items.length, 0,
  2015. "menu should have correct number of items");
  2016. assert.equal(subitem.parentMenu, null,
  2017. "item's parent menu should be correct");
  2018. test.showMenu(null, function (popup) {
  2019. test.checkMenu([menu], [menu], []);
  2020. test.done();
  2021. });
  2022. };
  2023. // Setting Menu.items should work.
  2024. exports.testMenuItemsSetter = function (assert, done) {
  2025. let test = new TestHelper(assert, done);
  2026. let loader = test.newLoader();
  2027. let menu = loader.cm.Menu({
  2028. label: "menu",
  2029. items: [
  2030. loader.cm.Item({ label: "old item 0" }),
  2031. loader.cm.Item({ label: "old item 1" })
  2032. ]
  2033. });
  2034. menu.items = [
  2035. loader.cm.Item({ label: "new item 0" }),
  2036. loader.cm.Item({ label: "new item 1" }),
  2037. loader.cm.Item({ label: "new item 2" })
  2038. ];
  2039. assert.equal(menu.items.length, 3,
  2040. "menu should have correct number of items");
  2041. for (let i = 0; i < 3; i++) {
  2042. assert.equal(menu.items[i].label, "new item " + i,
  2043. "item label should be correct");
  2044. assert.equal(menu.items[i].parentMenu, menu,
  2045. "item's parent menu should be correct");
  2046. }
  2047. test.showMenu(null, function (popup) {
  2048. test.checkMenu([menu], [], []);
  2049. test.done();
  2050. });
  2051. };
  2052. // Setting Item.data should work.
  2053. exports.testItemDataSetter = function (assert, done) {
  2054. let test = new TestHelper(assert, done);
  2055. let loader = test.newLoader();
  2056. let item = loader.cm.Item({ label: "old item 0", data: "old" });
  2057. item.data = "new";
  2058. assert.equal(item.data, "new", "item should have correct data");
  2059. test.showMenu(null, function (popup) {
  2060. test.checkMenu([item], [], []);
  2061. test.done();
  2062. });
  2063. };
  2064. // Open the test doc, load the module, make sure items appear when context-
  2065. // clicking the iframe.
  2066. exports.testAlreadyOpenIframe = function (assert, done) {
  2067. let test = new TestHelper(assert, done);
  2068. test.withTestDoc(function (window, doc) {
  2069. let loader = test.newLoader();
  2070. let item = new loader.cm.Item({
  2071. label: "item"
  2072. });
  2073. test.showMenu(doc.getElementById("iframe"), function (popup) {
  2074. test.checkMenu([item], [], []);
  2075. test.done();
  2076. });
  2077. });
  2078. };
  2079. // Tests that a missing label throws an exception
  2080. exports.testItemNoLabel = function (assert, done) {
  2081. let test = new TestHelper(assert, done);
  2082. let loader = test.newLoader();
  2083. try {
  2084. new loader.cm.Item({});
  2085. assert.ok(false, "Should have seen exception");
  2086. }
  2087. catch (e) {
  2088. assert.ok(true, "Should have seen exception");
  2089. }
  2090. try {
  2091. new loader.cm.Item({ label: null });
  2092. assert.ok(false, "Should have seen exception");
  2093. }
  2094. catch (e) {
  2095. assert.ok(true, "Should have seen exception");
  2096. }
  2097. try {
  2098. new loader.cm.Item({ label: undefined });
  2099. assert.ok(false, "Should have seen exception");
  2100. }
  2101. catch (e) {
  2102. assert.ok(true, "Should have seen exception");
  2103. }
  2104. try {
  2105. new loader.cm.Item({ label: "" });
  2106. assert.ok(false, "Should have seen exception");
  2107. }
  2108. catch (e) {
  2109. assert.ok(true, "Should have seen exception");
  2110. }
  2111. test.done();
  2112. }
  2113. // Tests that items can have an empty data property
  2114. exports.testItemNoData = function (assert, done) {
  2115. let test = new TestHelper(assert, done);
  2116. let loader = test.newLoader();
  2117. function checkData(data) {
  2118. assert.equal(data, undefined, "Data should be undefined");
  2119. }
  2120. let item1 = new loader.cm.Item({
  2121. label: "item 1",
  2122. contentScript: 'self.on("click", function(node, data) self.postMessage(data))',
  2123. onMessage: checkData
  2124. });
  2125. let item2 = new loader.cm.Item({
  2126. label: "item 2",
  2127. data: null,
  2128. contentScript: 'self.on("click", function(node, data) self.postMessage(data))',
  2129. onMessage: checkData
  2130. });
  2131. let item3 = new loader.cm.Item({
  2132. label: "item 3",
  2133. data: undefined,
  2134. contentScript: 'self.on("click", function(node, data) self.postMessage(data))',
  2135. onMessage: checkData
  2136. });
  2137. assert.equal(item1.data, undefined, "Should be no defined data");
  2138. assert.equal(item2.data, null, "Should be no defined data");
  2139. assert.equal(item3.data, undefined, "Should be no defined data");
  2140. test.showMenu(null, function (popup) {
  2141. test.checkMenu([item1, item2, item3], [], []);
  2142. let itemElt = test.getItemElt(popup, item1);
  2143. itemElt.click();
  2144. test.hideMenu(function() {
  2145. test.showMenu(null, function (popup) {
  2146. let itemElt = test.getItemElt(popup, item2);
  2147. itemElt.click();
  2148. test.hideMenu(function() {
  2149. test.showMenu(null, function (popup) {
  2150. let itemElt = test.getItemElt(popup, item3);
  2151. itemElt.click();
  2152. test.done();
  2153. });
  2154. });
  2155. });
  2156. });
  2157. });
  2158. }
  2159. // Tests that items without an image don't attempt to show one
  2160. exports.testItemNoImage = function (assert, done) {
  2161. let test = new TestHelper(assert, done);
  2162. let loader = test.newLoader();
  2163. let item1 = new loader.cm.Item({ label: "item 1" });
  2164. let item2 = new loader.cm.Item({ label: "item 2", image: null });
  2165. let item3 = new loader.cm.Item({ label: "item 3", image: undefined });
  2166. assert.equal(item1.image, undefined, "Should be no defined image");
  2167. assert.equal(item2.image, null, "Should be no defined image");
  2168. assert.equal(item3.image, undefined, "Should be no defined image");
  2169. test.showMenu(null, function (popup) {
  2170. test.checkMenu([item1, item2, item3], [], []);
  2171. test.done();
  2172. });
  2173. }
  2174. // Test image support.
  2175. exports.testItemImage = function (assert, done) {
  2176. let test = new TestHelper(assert, done);
  2177. let loader = test.newLoader();
  2178. let imageURL = data.url("moz_favicon.ico");
  2179. let item = new loader.cm.Item({ label: "item", image: imageURL });
  2180. let menu = new loader.cm.Menu({ label: "menu", image: imageURL, items: [
  2181. loader.cm.Item({ label: "subitem" })
  2182. ]});
  2183. assert.equal(item.image, imageURL, "Should have set the image correctly");
  2184. assert.equal(menu.image, imageURL, "Should have set the image correctly");
  2185. test.showMenu(null, function (popup) {
  2186. test.checkMenu([item, menu], [], []);
  2187. let imageURL2 = data.url("dummy.ico");
  2188. item.image = imageURL2;
  2189. menu.image = imageURL2;
  2190. assert.equal(item.image, imageURL2, "Should have set the image correctly");
  2191. assert.equal(menu.image, imageURL2, "Should have set the image correctly");
  2192. test.checkMenu([item, menu], [], []);
  2193. item.image = null;
  2194. menu.image = null;
  2195. assert.equal(item.image, null, "Should have set the image correctly");
  2196. assert.equal(menu.image, null, "Should have set the image correctly");
  2197. test.checkMenu([item, menu], [], []);
  2198. test.done();
  2199. });
  2200. };
  2201. // Test image URL validation.
  2202. exports.testItemImageValidURL = function (assert, done) {
  2203. let test = new TestHelper(assert, done);
  2204. let loader = test.newLoader();
  2205. assert.throws(function(){
  2206. new loader.cm.Item({
  2207. label: "item 1",
  2208. image: "foo"
  2209. })
  2210. }, /Image URL validation failed/
  2211. );
  2212. assert.throws(function(){
  2213. new loader.cm.Item({
  2214. label: "item 2",
  2215. image: false
  2216. })
  2217. }, /Image URL validation failed/
  2218. );
  2219. assert.throws(function(){
  2220. new loader.cm.Item({
  2221. label: "item 3",
  2222. image: 0
  2223. })
  2224. }, /Image URL validation failed/
  2225. );
  2226. let imageURL = data.url("moz_favicon.ico");
  2227. let item4 = new loader.cm.Item({ label: "item 4", image: imageURL });
  2228. let item5 = new loader.cm.Item({ label: "item 5", image: null });
  2229. let item6 = new loader.cm.Item({ label: "item 6", image: undefined });
  2230. assert.equal(item4.image, imageURL, "Should be proper image URL");
  2231. assert.equal(item5.image, null, "Should be null image");
  2232. assert.equal(item6.image, undefined, "Should be undefined image");
  2233. test.done();
  2234. };
  2235. // Menu.destroy should destroy the item tree rooted at that menu.
  2236. exports.testMenuDestroy = function (assert, done) {
  2237. let test = new TestHelper(assert, done);
  2238. let loader = test.newLoader();
  2239. let menu = loader.cm.Menu({
  2240. label: "menu",
  2241. items: [
  2242. loader.cm.Item({ label: "item 0" }),
  2243. loader.cm.Menu({
  2244. label: "item 1",
  2245. items: [
  2246. loader.cm.Item({ label: "subitem 0" }),
  2247. loader.cm.Item({ label: "subitem 1" }),
  2248. loader.cm.Item({ label: "subitem 2" })
  2249. ]
  2250. }),
  2251. loader.cm.Item({ label: "item 2" })
  2252. ]
  2253. });
  2254. menu.destroy();
  2255. /*let numRegistryEntries = 0;
  2256. loader.globalScope.browserManager.browserWins.forEach(function (bwin) {
  2257. for (let itemID in bwin.items)
  2258. numRegistryEntries++;
  2259. });
  2260. assert.equal(numRegistryEntries, 0, "All items should be unregistered.");*/
  2261. test.showMenu(null, function (popup) {
  2262. test.checkMenu([menu], [], [menu]);
  2263. test.done();
  2264. });
  2265. };
  2266. // Checks that if a menu contains sub items that are hidden then the menu is
  2267. // hidden too. Also checks that content scripts and contexts work for sub items.
  2268. exports.testSubItemContextNoMatchHideMenu = function (assert, done) {
  2269. let test = new TestHelper(assert, done);
  2270. let loader = test.newLoader();
  2271. let items = [
  2272. loader.cm.Menu({
  2273. label: "menu 1",
  2274. items: [
  2275. loader.cm.Item({
  2276. label: "subitem 1",
  2277. context: loader.cm.SelectorContext(".foo")
  2278. })
  2279. ]
  2280. }),
  2281. loader.cm.Menu({
  2282. label: "menu 2",
  2283. items: [
  2284. loader.cm.Item({
  2285. label: "subitem 2",
  2286. contentScript: 'self.on("context", function () false);'
  2287. })
  2288. ]
  2289. }),
  2290. loader.cm.Menu({
  2291. label: "menu 3",
  2292. items: [
  2293. loader.cm.Item({
  2294. label: "subitem 3",
  2295. context: loader.cm.SelectorContext(".foo")
  2296. }),
  2297. loader.cm.Item({
  2298. label: "subitem 4",
  2299. contentScript: 'self.on("context", function () false);'
  2300. })
  2301. ]
  2302. })
  2303. ];
  2304. test.showMenu(null, function (popup) {
  2305. test.checkMenu(items, items, []);
  2306. test.done();
  2307. });
  2308. };
  2309. // Checks that if a menu contains a combination of hidden and visible sub items
  2310. // then the menu is still visible too.
  2311. exports.testSubItemContextMatch = function (assert, done) {
  2312. let test = new TestHelper(assert, done);
  2313. let loader = test.newLoader();
  2314. let hiddenItems = [
  2315. loader.cm.Item({
  2316. label: "subitem 3",
  2317. context: loader.cm.SelectorContext(".foo")
  2318. }),
  2319. loader.cm.Item({
  2320. label: "subitem 6",
  2321. contentScript: 'self.on("context", function () false);'
  2322. })
  2323. ];
  2324. let items = [
  2325. loader.cm.Menu({
  2326. label: "menu 1",
  2327. items: [
  2328. loader.cm.Item({
  2329. label: "subitem 1",
  2330. context: loader.cm.URLContext(TEST_DOC_URL)
  2331. })
  2332. ]
  2333. }),
  2334. loader.cm.Menu({
  2335. label: "menu 2",
  2336. items: [
  2337. loader.cm.Item({
  2338. label: "subitem 2",
  2339. contentScript: 'self.on("context", function () true);'
  2340. })
  2341. ]
  2342. }),
  2343. loader.cm.Menu({
  2344. label: "menu 3",
  2345. items: [
  2346. hiddenItems[0],
  2347. loader.cm.Item({
  2348. label: "subitem 4",
  2349. contentScript: 'self.on("context", function () true);'
  2350. })
  2351. ]
  2352. }),
  2353. loader.cm.Menu({
  2354. label: "menu 4",
  2355. items: [
  2356. loader.cm.Item({
  2357. label: "subitem 5",
  2358. context: loader.cm.URLContext(TEST_DOC_URL)
  2359. }),
  2360. hiddenItems[1]
  2361. ]
  2362. }),
  2363. loader.cm.Menu({
  2364. label: "menu 5",
  2365. items: [
  2366. loader.cm.Item({
  2367. label: "subitem 7",
  2368. context: loader.cm.URLContext(TEST_DOC_URL)
  2369. }),
  2370. loader.cm.Item({
  2371. label: "subitem 8",
  2372. contentScript: 'self.on("context", function () true);'
  2373. })
  2374. ]
  2375. })
  2376. ];
  2377. test.withTestDoc(function (window, doc) {
  2378. test.showMenu(null, function (popup) {
  2379. test.checkMenu(items, hiddenItems, []);
  2380. test.done();
  2381. });
  2382. });
  2383. };
  2384. // Child items should default to visible, not to PageContext
  2385. exports.testSubItemDefaultVisible = function (assert, done) {
  2386. let test = new TestHelper(assert, done);
  2387. let loader = test.newLoader();
  2388. let items = [
  2389. loader.cm.Menu({
  2390. label: "menu 1",
  2391. context: loader.cm.SelectorContext("img"),
  2392. items: [
  2393. loader.cm.Item({
  2394. label: "subitem 1"
  2395. }),
  2396. loader.cm.Item({
  2397. label: "subitem 2",
  2398. context: loader.cm.SelectorContext("img")
  2399. }),
  2400. loader.cm.Item({
  2401. label: "subitem 3",
  2402. context: loader.cm.SelectorContext("a")
  2403. })
  2404. ]
  2405. })
  2406. ];
  2407. // subitem 3 will be hidden
  2408. let hiddenItems = [items[0].items[2]];
  2409. test.withTestDoc(function (window, doc) {
  2410. test.showMenu(doc.getElementById("image"), function (popup) {
  2411. test.checkMenu(items, hiddenItems, []);
  2412. test.done();
  2413. });
  2414. });
  2415. };
  2416. // Tests that the click event on sub menuitem
  2417. // tiggers the click event for the sub menuitem and the parent menu
  2418. exports.testSubItemClick = function (assert, done) {
  2419. let test = new TestHelper(assert, done);
  2420. let loader = test.newLoader();
  2421. let state = 0;
  2422. let items = [
  2423. loader.cm.Menu({
  2424. label: "menu 1",
  2425. items: [
  2426. loader.cm.Item({
  2427. label: "subitem 1",
  2428. data: "foobar",
  2429. contentScript: 'self.on("click", function (node, data) {' +
  2430. ' self.postMessage({' +
  2431. ' tagName: node.tagName,' +
  2432. ' data: data' +
  2433. ' });' +
  2434. '});',
  2435. onMessage: function(msg) {
  2436. assert.equal(msg.tagName, "HTML", "should have seen the right node");
  2437. assert.equal(msg.data, "foobar", "should have seen the right data");
  2438. assert.equal(state, 0, "should have seen the event at the right time");
  2439. state++;
  2440. }
  2441. })
  2442. ],
  2443. contentScript: 'self.on("click", function (node, data) {' +
  2444. ' self.postMessage({' +
  2445. ' tagName: node.tagName,' +
  2446. ' data: data' +
  2447. ' });' +
  2448. '});',
  2449. onMessage: function(msg) {
  2450. assert.equal(msg.tagName, "HTML", "should have seen the right node");
  2451. assert.equal(msg.data, "foobar", "should have seen the right data");
  2452. assert.equal(state, 1, "should have seen the event at the right time");
  2453. test.done();
  2454. }
  2455. })
  2456. ];
  2457. test.withTestDoc(function (window, doc) {
  2458. test.showMenu(null, function (popup) {
  2459. test.checkMenu(items, [], []);
  2460. let topMenuElt = test.getItemElt(popup, items[0]);
  2461. let topMenuPopup = topMenuElt.firstChild;
  2462. let itemElt = test.getItemElt(topMenuPopup, items[0].items[0]);
  2463. itemElt.click();
  2464. });
  2465. });
  2466. };
  2467. // Tests that the command event on sub menuitem
  2468. // tiggers the click event for the sub menuitem and the parent menu
  2469. exports.testSubItemCommand = function (assert, done) {
  2470. let test = new TestHelper(assert, done);
  2471. let loader = test.newLoader();
  2472. let state = 0;
  2473. let items = [
  2474. loader.cm.Menu({
  2475. label: "menu 1",
  2476. items: [
  2477. loader.cm.Item({
  2478. label: "subitem 1",
  2479. data: "foobar",
  2480. contentScript: 'self.on("click", function (node, data) {' +
  2481. ' self.postMessage({' +
  2482. ' tagName: node.tagName,' +
  2483. ' data: data' +
  2484. ' });' +
  2485. '});',
  2486. onMessage: function(msg) {
  2487. assert.equal(msg.tagName, "HTML", "should have seen the right node");
  2488. assert.equal(msg.data, "foobar", "should have seen the right data");
  2489. assert.equal(state, 0, "should have seen the event at the right time");
  2490. state++;
  2491. }
  2492. })
  2493. ],
  2494. contentScript: 'self.on("click", function (node, data) {' +
  2495. ' self.postMessage({' +
  2496. ' tagName: node.tagName,' +
  2497. ' data: data' +
  2498. ' });' +
  2499. '});',
  2500. onMessage: function(msg) {
  2501. assert.equal(msg.tagName, "HTML", "should have seen the right node");
  2502. assert.equal(msg.data, "foobar", "should have seen the right data");
  2503. assert.equal(state, 1, "should have seen the event at the right time");
  2504. state++
  2505. test.done();
  2506. }
  2507. })
  2508. ];
  2509. test.withTestDoc(function (window, doc) {
  2510. test.showMenu(null, function (popup) {
  2511. test.checkMenu(items, [], []);
  2512. let topMenuElt = test.getItemElt(popup, items[0]);
  2513. let topMenuPopup = topMenuElt.firstChild;
  2514. let itemElt = test.getItemElt(topMenuPopup, items[0].items[0]);
  2515. // create a command event
  2516. let evt = itemElt.ownerDocument.createEvent('Event');
  2517. evt.initEvent('command', true, true);
  2518. itemElt.dispatchEvent(evt);
  2519. });
  2520. });
  2521. };
  2522. // Tests that opening a context menu for an outer frame when an inner frame
  2523. // has a selection doesn't activate the SelectionContext
  2524. exports.testSelectionInInnerFrameNoMatch = function (assert, done) {
  2525. let test = new TestHelper(assert, done);
  2526. let loader = test.newLoader();
  2527. let state = 0;
  2528. let items = [
  2529. loader.cm.Item({
  2530. label: "test item",
  2531. context: loader.cm.SelectionContext()
  2532. })
  2533. ];
  2534. test.withTestDoc(function (window, doc) {
  2535. let frame = doc.getElementById("iframe");
  2536. frame.contentWindow.getSelection().selectAllChildren(frame.contentDocument.body);
  2537. test.showMenu(null, function (popup) {
  2538. test.checkMenu(items, items, []);
  2539. test.done();
  2540. });
  2541. });
  2542. };
  2543. // Tests that opening a context menu for an inner frame when the inner frame
  2544. // has a selection does activate the SelectionContext
  2545. exports.testSelectionInInnerFrameMatch = function (assert, done) {
  2546. let test = new TestHelper(assert, done);
  2547. let loader = test.newLoader();
  2548. let state = 0;
  2549. let items = [
  2550. loader.cm.Item({
  2551. label: "test item",
  2552. context: loader.cm.SelectionContext()
  2553. })
  2554. ];
  2555. test.withTestDoc(function (window, doc) {
  2556. let frame = doc.getElementById("iframe");
  2557. frame.contentWindow.getSelection().selectAllChildren(frame.contentDocument.body);
  2558. test.showMenu(frame.contentDocument.getElementById("text"), function (popup) {
  2559. test.checkMenu(items, [], []);
  2560. test.done();
  2561. });
  2562. });
  2563. };
  2564. // Tests that opening a context menu for an inner frame when the outer frame
  2565. // has a selection doesn't activate the SelectionContext
  2566. exports.testSelectionInOuterFrameNoMatch = function (assert, done) {
  2567. let test = new TestHelper(assert, done);
  2568. let loader = test.newLoader();
  2569. let state = 0;
  2570. let items = [
  2571. loader.cm.Item({
  2572. label: "test item",
  2573. context: loader.cm.SelectionContext()
  2574. })
  2575. ];
  2576. test.withTestDoc(function (window, doc) {
  2577. let frame = doc.getElementById("iframe");
  2578. window.getSelection().selectAllChildren(doc.body);
  2579. test.showMenu(frame.contentDocument.getElementById("text"), function (popup) {
  2580. test.checkMenu(items, items, []);
  2581. test.done();
  2582. });
  2583. });
  2584. };
  2585. // Test that the return value of the predicate function determines if
  2586. // item is shown
  2587. exports.testPredicateContextControl = function (assert, done) {
  2588. let test = new TestHelper(assert, done);
  2589. let loader = test.newLoader();
  2590. let itemTrue = loader.cm.Item({
  2591. label: "visible",
  2592. context: loader.cm.PredicateContext(function () { return true; })
  2593. });
  2594. let itemFalse = loader.cm.Item({
  2595. label: "hidden",
  2596. context: loader.cm.PredicateContext(function () { return false; })
  2597. });
  2598. test.showMenu(null, function (popup) {
  2599. test.checkMenu([itemTrue, itemFalse], [itemFalse], []);
  2600. test.done();
  2601. });
  2602. };
  2603. // Test that the data object has the correct document type
  2604. exports.testPredicateContextDocumentType = function (assert, done) {
  2605. let test = new TestHelper(assert, done);
  2606. let loader = test.newLoader();
  2607. let items = [loader.cm.Item({
  2608. label: "item",
  2609. context: loader.cm.PredicateContext(function (data) {
  2610. assert.equal(data.documentType, 'text/html');
  2611. return true;
  2612. })
  2613. })];
  2614. test.withTestDoc(function (window, doc) {
  2615. test.showMenu(null, function (popup) {
  2616. test.checkMenu(items, [], []);
  2617. test.done();
  2618. });
  2619. });
  2620. };
  2621. // Test that the data object has the correct document URL
  2622. exports.testPredicateContextDocumentURL = function (assert, done) {
  2623. let test = new TestHelper(assert, done);
  2624. let loader = test.newLoader();
  2625. let items = [loader.cm.Item({
  2626. label: "item",
  2627. context: loader.cm.PredicateContext(function (data) {
  2628. assert.equal(data.documentURL, TEST_DOC_URL);
  2629. return true;
  2630. })
  2631. })];
  2632. test.withTestDoc(function (window, doc) {
  2633. test.showMenu(null, function (popup) {
  2634. test.checkMenu(items, [], []);
  2635. test.done();
  2636. });
  2637. });
  2638. };
  2639. // Test that the data object has the correct element name
  2640. exports.testPredicateContextTargetName = function (assert, done) {
  2641. let test = new TestHelper(assert, done);
  2642. let loader = test.newLoader();
  2643. let items = [loader.cm.Item({
  2644. label: "item",
  2645. context: loader.cm.PredicateContext(function (data) {
  2646. assert.strictEqual(data.targetName, "input");
  2647. return true;
  2648. })
  2649. })];
  2650. test.withTestDoc(function (window, doc) {
  2651. test.showMenu(doc.getElementById("button"), function (popup) {
  2652. test.checkMenu(items, [], []);
  2653. test.done();
  2654. });
  2655. });
  2656. };
  2657. // Test that the data object has the correct ID
  2658. exports.testPredicateContextTargetIDSet = function (assert, done) {
  2659. let test = new TestHelper(assert, done);
  2660. let loader = test.newLoader();
  2661. let items = [loader.cm.Item({
  2662. label: "item",
  2663. context: loader.cm.PredicateContext(function (data) {
  2664. assert.strictEqual(data.targetID, "button");
  2665. return true;
  2666. })
  2667. })];
  2668. test.withTestDoc(function (window, doc) {
  2669. test.showMenu(doc.getElementById("button"), function (popup) {
  2670. test.checkMenu(items, [], []);
  2671. test.done();
  2672. });
  2673. });
  2674. };
  2675. // Test that the data object has the correct ID
  2676. exports.testPredicateContextTargetIDNotSet = function (assert, done) {
  2677. let test = new TestHelper(assert, done);
  2678. let loader = test.newLoader();
  2679. let items = [loader.cm.Item({
  2680. label: "item",
  2681. context: loader.cm.PredicateContext(function (data) {
  2682. assert.strictEqual(data.targetID, null);
  2683. return true;
  2684. })
  2685. })];
  2686. test.withTestDoc(function (window, doc) {
  2687. test.showMenu(doc.getElementsByClassName("predicate-test-a")[0], function (popup) {
  2688. test.checkMenu(items, [], []);
  2689. test.done();
  2690. });
  2691. });
  2692. };
  2693. // Test that the data object is showing editable correctly for regular text inputs
  2694. exports.testPredicateContextTextBoxIsEditable = function (assert, done) {
  2695. let test = new TestHelper(assert, done);
  2696. let loader = test.newLoader();
  2697. let items = [loader.cm.Item({
  2698. label: "item",
  2699. context: loader.cm.PredicateContext(function (data) {
  2700. assert.strictEqual(data.isEditable, true);
  2701. return true;
  2702. })
  2703. })];
  2704. test.withTestDoc(function (window, doc) {
  2705. test.showMenu(doc.getElementById("textbox"), function (popup) {
  2706. test.checkMenu(items, [], []);
  2707. test.done();
  2708. });
  2709. });
  2710. };
  2711. // Test that the data object is showing editable correctly for readonly text inputs
  2712. exports.testPredicateContextReadonlyTextBoxIsNotEditable = function (assert, done) {
  2713. let test = new TestHelper(assert, done);
  2714. let loader = test.newLoader();
  2715. let items = [loader.cm.Item({
  2716. label: "item",
  2717. context: loader.cm.PredicateContext(function (data) {
  2718. assert.strictEqual(data.isEditable, false);
  2719. return true;
  2720. })
  2721. })];
  2722. test.withTestDoc(function (window, doc) {
  2723. test.showMenu(doc.getElementById("readonly-textbox"), function (popup) {
  2724. test.checkMenu(items, [], []);
  2725. test.done();
  2726. });
  2727. });
  2728. };
  2729. // Test that the data object is showing editable correctly for disabled text inputs
  2730. exports.testPredicateContextDisabledTextBoxIsNotEditable = function (assert, done) {
  2731. let test = new TestHelper(assert, done);
  2732. let loader = test.newLoader();
  2733. let items = [loader.cm.Item({
  2734. label: "item",
  2735. context: loader.cm.PredicateContext(function (data) {
  2736. assert.strictEqual(data.isEditable, false);
  2737. return true;
  2738. })
  2739. })];
  2740. test.withTestDoc(function (window, doc) {
  2741. test.showMenu(doc.getElementById("disabled-textbox"), function (popup) {
  2742. test.checkMenu(items, [], []);
  2743. test.done();
  2744. });
  2745. });
  2746. };
  2747. // Test that the data object is showing editable correctly for text areas
  2748. exports.testPredicateContextTextAreaIsEditable = function (assert, done) {
  2749. let test = new TestHelper(assert, done);
  2750. let loader = test.newLoader();
  2751. let items = [loader.cm.Item({
  2752. label: "item",
  2753. context: loader.cm.PredicateContext(function (data) {
  2754. assert.strictEqual(data.isEditable, true);
  2755. return true;
  2756. })
  2757. })];
  2758. test.withTestDoc(function (window, doc) {
  2759. test.showMenu(doc.getElementById("textfield"), function (popup) {
  2760. test.checkMenu(items, [], []);
  2761. test.done();
  2762. });
  2763. });
  2764. };
  2765. // Test that non-text inputs are not considered editable
  2766. exports.testPredicateContextButtonIsNotEditable = function (assert, done) {
  2767. let test = new TestHelper(assert, done);
  2768. let loader = test.newLoader();
  2769. let items = [loader.cm.Item({
  2770. label: "item",
  2771. context: loader.cm.PredicateContext(function (data) {
  2772. assert.strictEqual(data.isEditable, false);
  2773. return true;
  2774. })
  2775. })];
  2776. test.withTestDoc(function (window, doc) {
  2777. test.showMenu(doc.getElementById("button"), function (popup) {
  2778. test.checkMenu(items, [], []);
  2779. test.done();
  2780. });
  2781. });
  2782. };
  2783. // Test that the data object is showing editable correctly
  2784. exports.testPredicateContextNonInputIsNotEditable = function (assert, done) {
  2785. let test = new TestHelper(assert, done);
  2786. let loader = test.newLoader();
  2787. let items = [loader.cm.Item({
  2788. label: "item",
  2789. context: loader.cm.PredicateContext(function (data) {
  2790. assert.strictEqual(data.isEditable, false);
  2791. return true;
  2792. })
  2793. })];
  2794. test.withTestDoc(function (window, doc) {
  2795. test.showMenu(doc.getElementById("image"), function (popup) {
  2796. test.checkMenu(items, [], []);
  2797. test.done();
  2798. });
  2799. });
  2800. };
  2801. // Test that the data object is showing editable correctly for HTML contenteditable elements
  2802. exports.testPredicateContextEditableElement = function (assert, done) {
  2803. let test = new TestHelper(assert, done);
  2804. let loader = test.newLoader();
  2805. let items = [loader.cm.Item({
  2806. label: "item",
  2807. context: loader.cm.PredicateContext(function (data) {
  2808. assert.strictEqual(data.isEditable, true);
  2809. return true;
  2810. })
  2811. })];
  2812. test.withTestDoc(function (window, doc) {
  2813. test.showMenu(doc.getElementById("editable"), function (popup) {
  2814. test.checkMenu(items, [], []);
  2815. test.done();
  2816. });
  2817. });
  2818. };
  2819. // Test that the data object does not have a selection when there is none
  2820. exports.testPredicateContextNoSelectionInPage = function (assert, done) {
  2821. let test = new TestHelper(assert, done);
  2822. let loader = test.newLoader();
  2823. let items = [loader.cm.Item({
  2824. label: "item",
  2825. context: loader.cm.PredicateContext(function (data) {
  2826. assert.strictEqual(data.selectionText, null);
  2827. return true;
  2828. })
  2829. })];
  2830. test.withTestDoc(function (window, doc) {
  2831. test.showMenu(null, function (popup) {
  2832. test.checkMenu(items, [], []);
  2833. test.done();
  2834. });
  2835. });
  2836. };
  2837. // Test that the data object includes the selected page text
  2838. exports.testPredicateContextSelectionInPage = function (assert, done) {
  2839. let test = new TestHelper(assert, done);
  2840. let loader = test.newLoader();
  2841. let items = [loader.cm.Item({
  2842. label: "item",
  2843. context: loader.cm.PredicateContext(function (data) {
  2844. // since we might get whitespace
  2845. assert.ok(data.selectionText && data.selectionText.search(/^\s*Some text.\s*$/) != -1,
  2846. 'Expected "Some text.", got "' + data.selectionText + '"');
  2847. return true;
  2848. })
  2849. })];
  2850. test.withTestDoc(function (window, doc) {
  2851. window.getSelection().selectAllChildren(doc.getElementById("text"));
  2852. test.showMenu(null, function (popup) {
  2853. test.checkMenu(items, [], []);
  2854. test.done();
  2855. });
  2856. });
  2857. };
  2858. // Test that the data object includes the selected input text
  2859. exports.testPredicateContextSelectionInTextBox = function (assert, done) {
  2860. let test = new TestHelper(assert, done);
  2861. let loader = test.newLoader();
  2862. let items = [loader.cm.Item({
  2863. label: "item",
  2864. context: loader.cm.PredicateContext(function (data) {
  2865. // since we might get whitespace
  2866. assert.strictEqual(data.selectionText, "t v");
  2867. return true;
  2868. })
  2869. })];
  2870. test.withTestDoc(function (window, doc) {
  2871. let textbox = doc.getElementById("textbox");
  2872. textbox.focus();
  2873. textbox.setSelectionRange(3, 6);
  2874. test.showMenu(textbox, function (popup) {
  2875. test.checkMenu(items, [], []);
  2876. test.done();
  2877. });
  2878. });
  2879. };
  2880. // Test that the data object has the correct src for an image
  2881. exports.testPredicateContextTargetSrcSet = function (assert, done) {
  2882. let test = new TestHelper(assert, done);
  2883. let loader = test.newLoader();
  2884. let image;
  2885. let items = [loader.cm.Item({
  2886. label: "item",
  2887. context: loader.cm.PredicateContext(function (data) {
  2888. assert.strictEqual(data.srcURL, image.src);
  2889. return true;
  2890. })
  2891. })];
  2892. test.withTestDoc(function (window, doc) {
  2893. image = doc.getElementById("image");
  2894. test.showMenu(image, function (popup) {
  2895. test.checkMenu(items, [], []);
  2896. test.done();
  2897. });
  2898. });
  2899. };
  2900. // Test that the data object has no src for a link
  2901. exports.testPredicateContextTargetSrcNotSet = function (assert, done) {
  2902. let test = new TestHelper(assert, done);
  2903. let loader = test.newLoader();
  2904. let items = [loader.cm.Item({
  2905. label: "item",
  2906. context: loader.cm.PredicateContext(function (data) {
  2907. assert.strictEqual(data.srcURL, null);
  2908. return true;
  2909. })
  2910. })];
  2911. test.withTestDoc(function (window, doc) {
  2912. test.showMenu(doc.getElementById("link"), function (popup) {
  2913. test.checkMenu(items, [], []);
  2914. test.done();
  2915. });
  2916. });
  2917. };
  2918. // Test that the data object has the correct link set
  2919. exports.testPredicateContextTargetLinkSet = function (assert, done) {
  2920. let test = new TestHelper(assert, done);
  2921. let loader = test.newLoader();
  2922. let image;
  2923. let items = [loader.cm.Item({
  2924. label: "item",
  2925. context: loader.cm.PredicateContext(function (data) {
  2926. assert.strictEqual(data.linkURL, TEST_DOC_URL + "#test");
  2927. return true;
  2928. })
  2929. })];
  2930. test.withTestDoc(function (window, doc) {
  2931. test.showMenu(doc.getElementsByClassName("predicate-test-a")[0], function (popup) {
  2932. test.checkMenu(items, [], []);
  2933. test.done();
  2934. });
  2935. });
  2936. };
  2937. // Test that the data object has no link for an image
  2938. exports.testPredicateContextTargetLinkNotSet = function (assert, done) {
  2939. let test = new TestHelper(assert, done);
  2940. let loader = test.newLoader();
  2941. let items = [loader.cm.Item({
  2942. label: "item",
  2943. context: loader.cm.PredicateContext(function (data) {
  2944. assert.strictEqual(data.linkURL, null);
  2945. return true;
  2946. })
  2947. })];
  2948. test.withTestDoc(function (window, doc) {
  2949. test.showMenu(doc.getElementById("image"), function (popup) {
  2950. test.checkMenu(items, [], []);
  2951. test.done();
  2952. });
  2953. });
  2954. };
  2955. // Test that the data object has the value for an input textbox
  2956. exports.testPredicateContextTargetValueSet = function (assert, done) {
  2957. let test = new TestHelper(assert, done);
  2958. let loader = test.newLoader();
  2959. let image;
  2960. let items = [loader.cm.Item({
  2961. label: "item",
  2962. context: loader.cm.PredicateContext(function (data) {
  2963. assert.strictEqual(data.value, "test value");
  2964. return true;
  2965. })
  2966. })];
  2967. test.withTestDoc(function (window, doc) {
  2968. test.showMenu(doc.getElementById("textbox"), function (popup) {
  2969. test.checkMenu(items, [], []);
  2970. test.done();
  2971. });
  2972. });
  2973. };
  2974. // Test that the data object has no value for an image
  2975. exports.testPredicateContextTargetValueNotSet = function (assert, done) {
  2976. let test = new TestHelper(assert, done);
  2977. let loader = test.newLoader();
  2978. let items = [loader.cm.Item({
  2979. label: "item",
  2980. context: loader.cm.PredicateContext(function (data) {
  2981. assert.strictEqual(data.value, null);
  2982. return true;
  2983. })
  2984. })];
  2985. test.withTestDoc(function (window, doc) {
  2986. test.showMenu(doc.getElementById("image"), function (popup) {
  2987. test.checkMenu(items, [], []);
  2988. test.done();
  2989. });
  2990. });
  2991. };
  2992. // NO TESTS BELOW THIS LINE! ///////////////////////////////////////////////////
  2993. // This makes it easier to run tests by handling things like opening the menu,
  2994. // opening new windows, making assertions, etc. Methods on |test| can be called
  2995. // on instances of this class. Don't forget to call done() to end the test!
  2996. // WARNING: This looks up items in popups by comparing labels, so don't give two
  2997. // items the same label.
  2998. function TestHelper(assert, done) {
  2999. this.assert = assert;
  3000. this.end = done;
  3001. this.loaders = [];
  3002. this.browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
  3003. getService(Ci.nsIWindowMediator).
  3004. getMostRecentWindow("navigator:browser");
  3005. this.overflowThreshValue = require("sdk/preferences/service").
  3006. get(OVERFLOW_THRESH_PREF, OVERFLOW_THRESH_DEFAULT);
  3007. }
  3008. TestHelper.prototype = {
  3009. get contextMenuPopup() {
  3010. return this.browserWindow.document.getElementById("contentAreaContextMenu");
  3011. },
  3012. get contextMenuSeparator() {
  3013. return this.browserWindow.document.querySelector("." + SEPARATOR_CLASS);
  3014. },
  3015. get overflowPopup() {
  3016. return this.browserWindow.document.querySelector("." + OVERFLOW_POPUP_CLASS);
  3017. },
  3018. get overflowSubmenu() {
  3019. return this.browserWindow.document.querySelector("." + OVERFLOW_MENU_CLASS);
  3020. },
  3021. get tabBrowser() {
  3022. return this.browserWindow.gBrowser;
  3023. },
  3024. // Methods on the wrapped test can be called on this object.
  3025. __noSuchMethod__: function (methodName, args) {
  3026. this.assert[methodName].apply(this.assert, args);
  3027. },
  3028. // Asserts that elt, a DOM element representing item, looks OK.
  3029. checkItemElt: function (elt, item) {
  3030. let itemType = this.getItemType(item);
  3031. switch (itemType) {
  3032. case "Item":
  3033. this.assert.equal(elt.localName, "menuitem",
  3034. "Item DOM element should be a xul:menuitem");
  3035. if (typeof(item.data) === "string") {
  3036. this.assert.equal(elt.getAttribute("value"), item.data,
  3037. "Item should have correct data");
  3038. }
  3039. break
  3040. case "Menu":
  3041. this.assert.equal(elt.localName, "menu",
  3042. "Menu DOM element should be a xul:menu");
  3043. let subPopup = elt.firstChild;
  3044. this.assert.ok(subPopup, "xul:menu should have a child");
  3045. this.assert.equal(subPopup.localName, "menupopup",
  3046. "xul:menu's first child should be a menupopup");
  3047. break;
  3048. case "Separator":
  3049. this.assert.equal(elt.localName, "menuseparator",
  3050. "Separator DOM element should be a xul:menuseparator");
  3051. break;
  3052. }
  3053. if (itemType === "Item" || itemType === "Menu") {
  3054. this.assert.equal(elt.getAttribute("label"), item.label,
  3055. "Item should have correct title");
  3056. if (typeof(item.image) === "string") {
  3057. this.assert.equal(elt.getAttribute("image"), item.image,
  3058. "Item should have correct image");
  3059. if (itemType === "Menu")
  3060. this.assert.ok(elt.classList.contains("menu-iconic"),
  3061. "Menus with images should have the correct class")
  3062. else
  3063. this.assert.ok(elt.classList.contains("menuitem-iconic"),
  3064. "Items with images should have the correct class")
  3065. }
  3066. else {
  3067. this.assert.ok(!elt.getAttribute("image"),
  3068. "Item should not have image");
  3069. this.assert.ok(!elt.classList.contains("menu-iconic") && !elt.classList.contains("menuitem-iconic"),
  3070. "The iconic classes should not be present")
  3071. }
  3072. }
  3073. },
  3074. // Asserts that the context menu looks OK given the arguments. presentItems
  3075. // are items that have been added to the menu. absentItems are items that
  3076. // shouldn't match the current context. removedItems are items that have been
  3077. // removed from the menu.
  3078. checkMenu: function (presentItems, absentItems, removedItems) {
  3079. // Count up how many top-level items there are
  3080. let total = 0;
  3081. for (let item of presentItems) {
  3082. if (absentItems.indexOf(item) < 0 && removedItems.indexOf(item) < 0)
  3083. total++;
  3084. }
  3085. let separator = this.contextMenuSeparator;
  3086. if (total == 0) {
  3087. this.assert.ok(!separator || separator.hidden,
  3088. "separator should not be present");
  3089. }
  3090. else {
  3091. this.assert.ok(separator && !separator.hidden,
  3092. "separator should be present");
  3093. }
  3094. let mainNodes = this.browserWindow.document.querySelectorAll("#contentAreaContextMenu > ." + ITEM_CLASS);
  3095. let overflowNodes = this.browserWindow.document.querySelectorAll("." + OVERFLOW_POPUP_CLASS + " > ." + ITEM_CLASS);
  3096. this.assert.ok(mainNodes.length == 0 || overflowNodes.length == 0,
  3097. "Should only see nodes at the top level or in overflow");
  3098. let overflow = this.overflowSubmenu;
  3099. if (this.shouldOverflow(total)) {
  3100. this.assert.ok(overflow && !overflow.hidden,
  3101. "overflow menu should be present");
  3102. this.assert.equal(mainNodes.length, 0,
  3103. "should be no items in the main context menu");
  3104. }
  3105. else {
  3106. this.assert.ok(!overflow || overflow.hidden,
  3107. "overflow menu should not be present");
  3108. // When visible nodes == 0 they could be in overflow or top level
  3109. if (total > 0) {
  3110. this.assert.equal(overflowNodes.length, 0,
  3111. "should be no items in the overflow context menu");
  3112. }
  3113. }
  3114. // Iterate over wherever the nodes have ended up
  3115. let nodes = mainNodes.length ? mainNodes : overflowNodes;
  3116. this.checkNodes(nodes, presentItems, absentItems, removedItems)
  3117. let pos = 0;
  3118. },
  3119. // Recurses through the item hierarchy of presentItems comparing it to the
  3120. // node hierarchy of nodes. Any items in removedItems will be skipped (so
  3121. // should not exist in the XUL), any items in absentItems must exist and be
  3122. // hidden
  3123. checkNodes: function (nodes, presentItems, absentItems, removedItems) {
  3124. let pos = 0;
  3125. for (let item of presentItems) {
  3126. // Removed items shouldn't be in the list
  3127. if (removedItems.indexOf(item) >= 0)
  3128. continue;
  3129. if (nodes.length <= pos) {
  3130. this.assert.ok(false, "Not enough nodes");
  3131. return;
  3132. }
  3133. let hidden = absentItems.indexOf(item) >= 0;
  3134. this.checkItemElt(nodes[pos], item);
  3135. this.assert.equal(nodes[pos].hidden, hidden,
  3136. "hidden should be set correctly");
  3137. // The contents of hidden menus doesn't matter so much
  3138. if (!hidden && this.getItemType(item) == "Menu") {
  3139. this.assert.equal(nodes[pos].firstChild.localName, "menupopup",
  3140. "menu XUL should contain a menupopup");
  3141. this.checkNodes(nodes[pos].firstChild.childNodes, item.items, absentItems, removedItems);
  3142. }
  3143. if (pos > 0)
  3144. this.assert.equal(nodes[pos].previousSibling, nodes[pos - 1],
  3145. "nodes should all be in the same group");
  3146. pos++;
  3147. }
  3148. this.assert.equal(nodes.length, pos,
  3149. "should have checked all the XUL nodes");
  3150. },
  3151. // Attaches an event listener to node. The listener is automatically removed
  3152. // when it's fired (so it's assumed it will fire), and callback is called
  3153. // after a short delay. Since the module we're testing relies on the same
  3154. // event listeners to do its work, this is to give them a little breathing
  3155. // room before callback runs. Inside callback |this| is this object.
  3156. // Optionally you can pass a function to test if the event is the event you
  3157. // want.
  3158. delayedEventListener: function (node, event, callback, useCapture, isValid) {
  3159. const self = this;
  3160. node.addEventListener(event, function handler(evt) {
  3161. if (isValid && !isValid(evt))
  3162. return;
  3163. node.removeEventListener(event, handler, useCapture);
  3164. timer.setTimeout(function () {
  3165. try {
  3166. callback.call(self, evt);
  3167. }
  3168. catch (err) {
  3169. self.assert.fail(err);
  3170. self.end();
  3171. }
  3172. }, 20);
  3173. }, useCapture);
  3174. },
  3175. // Call to finish the test.
  3176. done: function () {
  3177. const self = this;
  3178. function commonDone() {
  3179. this.closeTab();
  3180. while (this.loaders.length) {
  3181. this.loaders[0].unload();
  3182. }
  3183. require("sdk/preferences/service").set(OVERFLOW_THRESH_PREF, self.overflowThreshValue);
  3184. this.end();
  3185. }
  3186. function closeBrowserWindow() {
  3187. if (this.oldBrowserWindow) {
  3188. this.delayedEventListener(this.browserWindow, "unload", commonDone,
  3189. false);
  3190. this.browserWindow.close();
  3191. this.browserWindow = this.oldBrowserWindow;
  3192. delete this.oldBrowserWindow;
  3193. }
  3194. else {
  3195. commonDone.call(this);
  3196. }
  3197. };
  3198. if (this.contextMenuPopup.state == "closed") {
  3199. closeBrowserWindow.call(this);
  3200. }
  3201. else {
  3202. this.delayedEventListener(this.contextMenuPopup, "popuphidden",
  3203. function () closeBrowserWindow.call(this),
  3204. false);
  3205. this.contextMenuPopup.hidePopup();
  3206. }
  3207. },
  3208. closeTab: function() {
  3209. if (this.tab) {
  3210. this.tabBrowser.removeTab(this.tab);
  3211. this.tabBrowser.selectedTab = this.oldSelectedTab;
  3212. this.tab = null;
  3213. }
  3214. },
  3215. // Returns the DOM element in popup corresponding to item.
  3216. // WARNING: The element is found by comparing labels, so don't give two items
  3217. // the same label.
  3218. getItemElt: function (popup, item) {
  3219. let nodes = popup.childNodes;
  3220. for (let i = nodes.length - 1; i >= 0; i--) {
  3221. if (this.getItemType(item) === "Separator") {
  3222. if (nodes[i].localName === "menuseparator")
  3223. return nodes[i];
  3224. }
  3225. else if (nodes[i].getAttribute("label") === item.label)
  3226. return nodes[i];
  3227. }
  3228. return null;
  3229. },
  3230. // Returns "Item", "Menu", or "Separator".
  3231. getItemType: function (item) {
  3232. // Could use instanceof here, but that would require accessing the loader
  3233. // that created the item, and I don't want to A) somehow search through the
  3234. // this.loaders list to find it, and B) assume there are any live loaders at
  3235. // all.
  3236. return /^\[object (Item|Menu|Separator)/.exec(item.toString())[1];
  3237. },
  3238. // Returns a wrapper around a new loader: { loader, cm, unload, globalScope }.
  3239. // loader is a Cuddlefish sandboxed loader, cm is the context menu module,
  3240. // globalScope is the context menu module's global scope, and unload is a
  3241. // function that unloads the loader and associated resources.
  3242. newLoader: function () {
  3243. const self = this;
  3244. let loader = Loader(module);
  3245. let wrapper = {
  3246. loader: loader,
  3247. cm: loader.require("sdk/context-menu"),
  3248. globalScope: loader.sandbox("sdk/context-menu"),
  3249. unload: function () {
  3250. loader.unload();
  3251. let idx = self.loaders.indexOf(wrapper);
  3252. if (idx < 0)
  3253. throw new Error("Test error: tried to unload nonexistent loader");
  3254. self.loaders.splice(idx, 1);
  3255. }
  3256. };
  3257. this.loaders.push(wrapper);
  3258. return wrapper;
  3259. },
  3260. // As above but the loader has private-browsing support enabled.
  3261. newPrivateLoader: function() {
  3262. let base = require("@loader/options");
  3263. // Clone current loader's options adding the private-browsing permission
  3264. let options = merge({}, base, {
  3265. metadata: merge({}, base.metadata || {}, {
  3266. permissions: merge({}, base.metadata.permissions || {}, {
  3267. 'private-browsing': true
  3268. })
  3269. })
  3270. });
  3271. const self = this;
  3272. let loader = Loader(module, null, options);
  3273. let wrapper = {
  3274. loader: loader,
  3275. cm: loader.require("sdk/context-menu"),
  3276. globalScope: loader.sandbox("sdk/context-menu"),
  3277. unload: function () {
  3278. loader.unload();
  3279. let idx = self.loaders.indexOf(wrapper);
  3280. if (idx < 0)
  3281. throw new Error("Test error: tried to unload nonexistent loader");
  3282. self.loaders.splice(idx, 1);
  3283. }
  3284. };
  3285. this.loaders.push(wrapper);
  3286. return wrapper;
  3287. },
  3288. // Returns true if the count crosses the overflow threshold.
  3289. shouldOverflow: function (count) {
  3290. return count >
  3291. (this.loaders.length ?
  3292. this.loaders[0].loader.require("sdk/preferences/service").
  3293. get(OVERFLOW_THRESH_PREF, OVERFLOW_THRESH_DEFAULT) :
  3294. OVERFLOW_THRESH_DEFAULT);
  3295. },
  3296. // Opens the context menu on the current page. If targetNode is null, the
  3297. // menu is opened in the top-left corner. onShowncallback is passed the
  3298. // popup.
  3299. showMenu: function(targetNode, onshownCallback) {
  3300. function sendEvent() {
  3301. this.delayedEventListener(this.browserWindow, "popupshowing",
  3302. function (e) {
  3303. let popup = e.target;
  3304. onshownCallback.call(this, popup);
  3305. }, false);
  3306. let rect = targetNode ?
  3307. targetNode.getBoundingClientRect() :
  3308. { left: 0, top: 0, width: 0, height: 0 };
  3309. let contentWin = targetNode ? targetNode.ownerDocument.defaultView
  3310. : this.browserWindow.content;
  3311. contentWin.
  3312. QueryInterface(Ci.nsIInterfaceRequestor).
  3313. getInterface(Ci.nsIDOMWindowUtils).
  3314. sendMouseEvent("contextmenu",
  3315. rect.left + (rect.width / 2),
  3316. rect.top + (rect.height / 2),
  3317. 2, 1, 0);
  3318. }
  3319. // If a new tab or window has not yet been opened, open a new tab now. For
  3320. // some reason using the tab already opened when the test starts causes
  3321. // leaks. See bug 566351 for details.
  3322. if (!targetNode && !this.oldSelectedTab && !this.oldBrowserWindow) {
  3323. this.oldSelectedTab = this.tabBrowser.selectedTab;
  3324. this.tab = this.tabBrowser.addTab("about:blank");
  3325. let browser = this.tabBrowser.getBrowserForTab(this.tab);
  3326. this.delayedEventListener(browser, "load", function () {
  3327. this.tabBrowser.selectedTab = this.tab;
  3328. sendEvent.call(this);
  3329. }, true);
  3330. }
  3331. else
  3332. sendEvent.call(this);
  3333. },
  3334. hideMenu: function(onhiddenCallback) {
  3335. this.delayedEventListener(this.browserWindow, "popuphidden", onhiddenCallback);
  3336. this.contextMenuPopup.hidePopup();
  3337. },
  3338. // Opens a new browser window. The window will be closed automatically when
  3339. // done() is called.
  3340. withNewWindow: function (onloadCallback) {
  3341. let win = this.browserWindow.OpenBrowserWindow();
  3342. this.delayedEventListener(win, "load", onloadCallback, true);
  3343. this.oldBrowserWindow = this.browserWindow;
  3344. this.browserWindow = win;
  3345. },
  3346. // Opens a new private browser window. The window will be closed
  3347. // automatically when done() is called.
  3348. withNewPrivateWindow: function (onloadCallback) {
  3349. let win = this.browserWindow.OpenBrowserWindow({private: true});
  3350. this.delayedEventListener(win, "load", onloadCallback, true);
  3351. this.oldBrowserWindow = this.browserWindow;
  3352. this.browserWindow = win;
  3353. },
  3354. // Opens a new tab with our test page in the current window. The tab will
  3355. // be closed automatically when done() is called.
  3356. withTestDoc: function (onloadCallback) {
  3357. this.oldSelectedTab = this.tabBrowser.selectedTab;
  3358. this.tab = this.tabBrowser.addTab(TEST_DOC_URL);
  3359. let browser = this.tabBrowser.getBrowserForTab(this.tab);
  3360. this.delayedEventListener(browser, "load", function () {
  3361. this.tabBrowser.selectedTab = this.tab;
  3362. onloadCallback.call(this, browser.contentWindow, browser.contentDocument);
  3363. }, true, function(evt) {
  3364. return evt.target.location == TEST_DOC_URL;
  3365. });
  3366. }
  3367. };
  3368. require('sdk/test').run(exports);