browser/devtools/tilt/test/browser_tilt_arcball.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     3 "use strict";
     5 function cloneUpdate(update) {
     6   return {
     7     rotation: quat4.create(update.rotation),
     8     translation: vec3.create(update.translation)
     9   };
    10 }
    12 function isExpectedUpdate(update1, update2) {
    13   if (update1.length !== update2.length) {
    14     return false;
    15   }
    16   for (let i = 0, len = update1.length; i < len; i++) {
    17     if (!isApproxVec(update1[i].rotation, update2[i].rotation) ||
    18         !isApproxVec(update1[i].translation, update2[i].translation)) {
    19       info("isExpectedUpdate expected " + JSON.stringify(update1), ", got " +
    20                                           JSON.stringify(update2) + " instead.");
    21       return false;
    22     }
    23   }
    24   return true;
    25 }
    27 function test() {
    28   let arcball1 = new TiltVisualizer.Arcball(window, 123, 456);
    30   is(arcball1.width, 123,
    31     "The first arcball width wasn't set correctly.");
    32   is(arcball1.height, 456,
    33     "The first arcball height wasn't set correctly.");
    34   is(arcball1.radius, 123,
    35     "The first arcball radius wasn't implicitly set correctly.");
    38   let arcball2 = new TiltVisualizer.Arcball(window, 987, 654);
    40   is(arcball2.width, 987,
    41     "The second arcball width wasn't set correctly.");
    42   is(arcball2.height, 654,
    43     "The second arcball height wasn't set correctly.");
    44   is(arcball2.radius, 654,
    45     "The second arcball radius wasn't implicitly set correctly.");
    48   let arcball3 = new TiltVisualizer.Arcball(window, 512, 512);
    50   let sphereVec = vec3.create();
    51   arcball3._pointToSphere(123, 456, 256, 512, 512, sphereVec);
    53   ok(isApproxVec(sphereVec, [-0.009765625, 0.390625, 0.9204980731010437]),
    54     "The _pointToSphere() function didn't map the coordinates correctly.");
    56   let stack1 = [];
    57   let expect1 = [
    58     { rotation: [
    59       -0.08877250552177429, 0.0242881178855896,
    60       -0.04222869873046875, -0.9948599338531494],
    61       translation: [0, 0, 0] },
    62     { rotation: [
    63       -0.13086390495300293, 0.03413732722401619,
    64       -0.06334304809570312, -0.9887855648994446],
    65       translation: [0, 0, 0] },
    66     { rotation: [
    67       -0.15138940513134003, 0.03854173421859741,
    68       -0.07390022277832031, -0.9849540591239929],
    69       translation: [0, 0, 0] },
    70     { rotation: [
    71       -0.1615273654460907, 0.040619146078825,
    72       -0.0791788101196289, -0.9828477501869202],
    73       translation: [0, 0, 0] },
    74     { rotation: [
    75       -0.16656573116779327, 0.04162723943591118,
    76       -0.0818181037902832, -0.9817478656768799],
    77       translation: [0, 0, 0] },
    78     { rotation: [
    79       -0.16907735168933868, 0.042123712599277496,
    80       -0.08313775062561035, -0.9811863303184509],
    81       translation: [0, 0, 0] },
    82     { rotation: [
    83       -0.17033125460147858, 0.042370058596134186,
    84       -0.08379757404327393, -0.9809026718139648],
    85       translation: [0, 0, 0] },
    86     { rotation: [
    87       -0.17095772922039032, 0.04249274358153343,
    88       -0.08412748575210571, -0.9807600975036621],
    89       translation: [0, 0, 0] },
    90     { rotation: [
    91       -0.17127084732055664, 0.04255397245287895,
    92       -0.0842924416065216, -0.9806886315345764],
    93       translation: [0, 0, 0] },
    94     { rotation: [
    95       -0.171427384018898, 0.042584557086229324,
    96       -0.08437491953372955, -0.9806528687477112],
    97       translation: [0, 0, 0] }];
    99   arcball3.mouseDown(10, 10, 1);
   100   arcball3.mouseMove(10, 100);
   101   for (let i1 = 0; i1 < 10; i1++) {
   102     stack1.push(cloneUpdate(arcball3.update()));
   103   }
   105   ok(isExpectedUpdate(stack1, expect1),
   106     "Mouse down & move events didn't create the expected transform. results.");
   108   let stack2 = [];
   109   let expect2 = [
   110     { rotation: [
   111       -0.1684110015630722, 0.04199237748980522,
   112       -0.0827873945236206, -0.9813361167907715],
   113       translation: [0, 0, 0] },
   114     { rotation: [
   115       -0.16936375200748444, 0.04218007251620293,
   116       -0.08328840136528015, -0.9811217188835144],
   117       translation: [0, 0, 0] },
   118     { rotation: [
   119       -0.17003019154071808, 0.04231100529432297,
   120       -0.08363909274339676, -0.9809709787368774],
   121       translation: [0, 0, 0] },
   122     { rotation: [
   123       -0.17049652338027954, 0.042402446269989014,
   124       -0.0838845893740654, -0.9808651208877563],
   125       translation: [0, 0, 0] },
   126     { rotation: [
   127       -0.17082282900810242, 0.042466338723897934,
   128       -0.08405643701553345, -0.9807908535003662],
   129       translation: [0, 0, 0] },
   130     { rotation: [
   131       -0.17105120420455933, 0.04251104220747948,
   132       -0.08417671173810959, -0.9807388186454773],
   133       translation: [0, 0, 0] },
   134     { rotation: [
   135       -0.17121103405952454, 0.04254228621721268,
   136       -0.08426092565059662, -0.9807023406028748],
   137       translation: [0, 0, 0] },
   138     { rotation: [
   139       -0.17132291197776794, 0.042564138770103455,
   140       -0.08431987464427948, -0.9806767106056213],
   141       translation: [0, 0, 0] },
   142     { rotation: [
   143       -0.1714012324810028, 0.04257945716381073,
   144       -0.08436112850904465, -0.9806588888168335],
   145       translation: [0, 0, 0] },
   146     { rotation: [
   147       -0.17145603895187378, 0.042590171098709106,
   148       -0.08439001441001892, -0.9806463718414307],
   149       translation: [0, 0, 0] }];
   151   arcball3.mouseUp(100, 100);
   152   for (let i2 = 0; i2 < 10; i2++) {
   153     stack2.push(cloneUpdate(arcball3.update()));
   154   }
   156   ok(isExpectedUpdate(stack2, expect2),
   157     "Mouse up events didn't create the expected transformation results.");
   159   let stack3 = [];
   160   let expect3 = [
   161     { rotation: [
   162       -0.17149439454078674, 0.04259764403104782,
   163       -0.08441022783517838, -0.9806375503540039],
   164       translation: [0, 0, -1] },
   165     { rotation: [
   166       -0.17152123153209686, 0.04260288551449776,
   167       -0.08442437648773193, -0.980631411075592],
   168       translation: [0, 0, -1.899999976158142] },
   169     { rotation: [
   170       -0.1715400665998459, 0.04260658100247383,
   171       -0.08443428575992584, -0.9806271195411682],
   172       translation: [0, 0, -2.7100000381469727] },
   173     { rotation: [
   174       -0.17155319452285767, 0.04260912910103798,
   175       -0.08444121479988098, -0.9806240797042847],
   176       translation: [0, 0, -3.439000129699707] },
   177     { rotation: [
   178       -0.17156240344047546, 0.042610932141542435,
   179       -0.08444607257843018, -0.9806219935417175],
   180       translation: [0, 0, -4.095099925994873] },
   181     { rotation: [
   182       -0.1715688556432724, 0.042612191289663315,
   183       -0.08444946259260178, -0.9806205034255981],
   184       translation: [0, 0, -4.685589790344238] },
   185     { rotation: [
   186       -0.17157337069511414, 0.04261308163404465,
   187       -0.0844518393278122, -0.980619490146637],
   188       translation: [0, 0, -5.217031002044678] },
   189     { rotation: [
   190       -0.17157652974128723, 0.0426136814057827,
   191       -0.0844535157084465, -0.9806187748908997],
   192       translation: [0, 0, -5.6953277587890625] },
   193     { rotation: [
   194       -0.17157875001430511, 0.04261413961648941,
   195       -0.08445467799901962, -0.9806182980537415],
   196       translation: [0, 0, -6.125794887542725] },
   197     { rotation: [
   198       -0.17158031463623047, 0.04261442646384239,
   199       -0.08445550501346588, -0.980617880821228],
   200       translation: [0, 0, -6.5132155418396] }];
   202   arcball3.zoom(10);
   203   for (let i3 = 0; i3 < 10; i3++) {
   204     stack3.push(cloneUpdate(arcball3.update()));
   205   }
   207   ok(isExpectedUpdate(stack3, expect3),
   208     "Mouse zoom events didn't create the expected transformation results.");
   210   let stack4 = [];
   211   let expect4 = [
   212     { rotation: [
   213       -0.17158135771751404, 0.04261462762951851,
   214       -0.08445606380701065, -0.9806176424026489],
   215       translation: [0, 0, -6.861894130706787] },
   216     { rotation: [
   217       -0.1715821474790573, 0.04261479899287224,
   218       -0.08445646613836288, -0.9806175231933594],
   219       translation: [0, 0, -7.1757049560546875] },
   220     { rotation: [
   221       -0.1715826541185379, 0.0426148846745491,
   222       -0.08445674180984497, -0.980617344379425],
   223       translation: [0, 0, -7.458134651184082] },
   224     { rotation: [
   225       -0.17158304154872894, 0.04261497035622597,
   226       -0.08445693552494049, -0.9806172847747803],
   227       translation: [0, 0, -7.7123212814331055] },
   228     { rotation: [
   229       -0.17158329486846924, 0.042615000158548355,
   230       -0.08445708453655243, -0.9806172251701355],
   231       translation: [0, 0, -7.941089153289795] },
   232     { rotation: [
   233       -0.17158347368240356, 0.04261505603790283,
   234       -0.084457166492939, -0.9806172251701355],
   235       translation: [0, 0, -8.146980285644531] },
   236     { rotation: [
   237       -0.1715836226940155, 0.04261508584022522,
   238       -0.08445724099874496, -0.9806171655654907],
   239       translation: [0, 0, -8.332282066345215] },
   240     { rotation: [
   241       -0.17158368229866028, 0.04261508584022522,
   242       -0.08445728570222855, -0.980617105960846],
   243       translation: [0, 0, -8.499053955078125] },
   244     { rotation: [
   245       -0.17158377170562744, 0.04261511191725731,
   246       -0.08445732295513153, -0.980617105960846],
   247       translation: [0, 0, -8.649148941040039] },
   248     { rotation: [
   249       -0.17158380150794983, 0.04261511191725731,
   250       -0.08445733785629272, -0.980617105960846],
   251       translation: [0, 0, -8.784234046936035] }];
   253   arcball3.keyDown(arcball3.rotateKeys.left);
   254   arcball3.keyDown(arcball3.rotateKeys.right);
   255   arcball3.keyDown(arcball3.rotateKeys.up);
   256   arcball3.keyDown(arcball3.rotateKeys.down);
   257   arcball3.keyDown(arcball3.panKeys.left);
   258   arcball3.keyDown(arcball3.panKeys.right);
   259   arcball3.keyDown(arcball3.panKeys.up);
   260   arcball3.keyDown(arcball3.panKeys.down);
   261   for (let i4 = 0; i4 < 10; i4++) {
   262     stack4.push(cloneUpdate(arcball3.update()));
   263   }
   265   ok(isExpectedUpdate(stack4, expect4),
   266     "Key down events didn't create the expected transformation results.");
   268   let stack5 = [];
   269   let expect5 = [
   270     { rotation: [
   271       -0.1715838462114334, 0.04261511191725731,
   272       -0.08445736765861511, -0.980617105960846],
   273       translation: [0, 0, -8.905810356140137] },
   274     { rotation: [
   275       -0.1715838462114334, 0.04261511191725731,
   276       -0.08445736765861511, -0.980617105960846],
   277       translation: [0, 0, -9.015229225158691] },
   278     { rotation: [
   279       -0.1715838462114334, 0.04261511191725731,
   280       -0.08445736765861511, -0.980617105960846],
   281       translation: [0, 0, -9.113706588745117] },
   282     { rotation: [
   283       -0.1715838611125946, 0.04261511191725731,
   284       -0.0844573825597763, -0.9806170463562012],
   285       translation: [0, 0, -9.202336311340332] },
   286     { rotation: [
   287       -0.17158392071723938, 0.0426151417195797,
   288       -0.0844573974609375, -0.980617105960846],
   289       translation: [0, 0, -9.282102584838867] },
   290     { rotation: [
   291       -0.17158392071723938, 0.0426151417195797,
   292       -0.0844573974609375, -0.980617105960846],
   293       translation: [0, 0, -9.35389232635498] },
   294     { rotation: [
   295       -0.17158392071723938, 0.0426151417195797,
   296       -0.0844573974609375, -0.980617105960846],
   297       translation: [0, 0, -9.418502807617188] },
   298     { rotation: [
   299       -0.17158392071723938, 0.0426151417195797,
   300       -0.0844573974609375, -0.980617105960846],
   301       translation: [0, 0, -9.476652145385742] },
   302     { rotation: [
   303       -0.17158392071723938, 0.0426151417195797,
   304       -0.0844573974609375, -0.980617105960846],
   305       translation: [0, 0, -9.528986930847168] },
   306     { rotation: [
   307       -0.17158392071723938, 0.0426151417195797,
   308       -0.0844573974609375, -0.980617105960846],
   309       translation: [0, 0, -9.576087951660156] }];
   311   arcball3.keyUp(arcball3.rotateKeys.left);
   312   arcball3.keyUp(arcball3.rotateKeys.right);
   313   arcball3.keyUp(arcball3.rotateKeys.up);
   314   arcball3.keyUp(arcball3.rotateKeys.down);
   315   arcball3.keyUp(arcball3.panKeys.left);
   316   arcball3.keyUp(arcball3.panKeys.right);
   317   arcball3.keyUp(arcball3.panKeys.up);
   318   arcball3.keyUp(arcball3.panKeys.down);
   319   for (let i5 = 0; i5 < 10; i5++) {
   320     stack5.push(cloneUpdate(arcball3.update()));
   321   }
   323   ok(isExpectedUpdate(stack5, expect5),
   324     "Key up events didn't create the expected transformation results.");
   326   let stack6 = [];
   327   let expect6 = [
   328     { rotation: [
   329       -0.17158392071723938, 0.0426151417195797,
   330       -0.0844573974609375, -0.980617105960846],
   331       translation: [0, 0, -9.618478775024414] },
   332     { rotation: [
   333       -0.17158392071723938, 0.0426151417195797,
   334       -0.0844573974609375, -0.980617105960846],
   335       translation: [0, 0, -6.156630992889404] },
   336     { rotation: [
   337       -0.17158392071723938, 0.0426151417195797,
   338       -0.0844573974609375, -0.980617105960846],
   339       translation: [0, 0, 0.4590320587158203] },
   340     { rotation: [
   341       -0.17158392071723938, 0.0426151417195797,
   342       -0.0844573974609375, -0.980617105960846],
   343       translation: [0, 0, 9.913128852844238] },
   344     { rotation: [
   345       -0.17158392071723938, 0.0426151417195797,
   346       -0.0844573974609375, -0.980617105960846],
   347       translation: [0, 0, 21.921815872192383] },
   348     { rotation: [
   349       -0.17158392071723938, 0.0426151417195797,
   350       -0.0844573974609375, -0.980617105960846],
   351       translation: [0, 0, 36.22963333129883] },
   352     { rotation: [
   353       -0.17158392071723938, 0.0426151417195797,
   354       -0.0844573974609375, -0.980617105960846],
   355       translation: [0, 0, 52.60667037963867] },
   356     { rotation: [
   357       -0.17158392071723938, 0.0426151417195797,
   358       -0.0844573974609375, -0.980617105960846],
   359       translation: [0, 0, 70.84600067138672] },
   360     { rotation: [
   361       -0.17158392071723938, 0.0426151417195797,
   362       -0.0844573974609375, -0.980617105960846],
   363       translation: [0, 0, 90.76139831542969] },
   364     { rotation: [
   365       -0.17158392071723938, 0.0426151417195797,
   366       -0.0844573974609375, -0.980617105960846],
   367       translation: [0, 0, 112.18525695800781] }];
   369   arcball3.keyDown(arcball3.zoomKeys["in"][0]);
   370   arcball3.keyDown(arcball3.zoomKeys["in"][1]);
   371   arcball3.keyDown(arcball3.zoomKeys["in"][2]);
   372   for (let i6 = 0; i6 < 10; i6++) {
   373     stack6.push(cloneUpdate(arcball3.update()));
   374   }
   375   arcball3.keyUp(arcball3.zoomKeys["in"][0]);
   376   arcball3.keyUp(arcball3.zoomKeys["in"][1]);
   377   arcball3.keyUp(arcball3.zoomKeys["in"][2]);
   379   ok(isExpectedUpdate(stack6, expect6),
   380     "Key zoom in events didn't create the expected transformation results.");
   382   let stack7 = [];
   383   let expect7 = [
   384     { rotation: [
   385       -0.17158392071723938, 0.0426151417195797,
   386       -0.0844573974609375, -0.980617105960846],
   387       translation: [0, 0, 134.96673583984375] },
   388     { rotation: [
   389       -0.17158392071723938, 0.0426151417195797,
   390       -0.0844573974609375, -0.980617105960846],
   391       translation: [0, 0, 151.97006225585938] },
   392     { rotation: [
   393       -0.17158392071723938, 0.0426151417195797,
   394       -0.0844573974609375, -0.980617105960846],
   395       translation: [0, 0, 163.77305603027344] },
   396     { rotation: [
   397       -0.17158392071723938, 0.0426151417195797,
   398       -0.0844573974609375, -0.980617105960846],
   399       translation: [0, 0, 170.895751953125] },
   400     { rotation: [
   401       -0.17158392071723938, 0.0426151417195797,
   402       -0.0844573974609375, -0.980617105960846],
   403       translation: [0, 0, 173.80618286132812] },
   404     { rotation: [
   405       -0.17158392071723938, 0.0426151417195797,
   406       -0.0844573974609375, -0.980617105960846],
   407       translation: [0, 0, 172.92556762695312] },
   408     { rotation: [
   409       -0.17158392071723938, 0.0426151417195797,
   410       -0.0844573974609375, -0.980617105960846],
   411       translation: [0, 0, 168.6330108642578] },
   412     { rotation: [
   413       -0.17158392071723938, 0.0426151417195797,
   414       -0.0844573974609375, -0.980617105960846],
   415       translation: [0, 0, 161.26971435546875] },
   416     { rotation: [
   417       -0.17158392071723938, 0.0426151417195797,
   418       -0.0844573974609375, -0.980617105960846],
   419       translation: [0, 0, 151.1427459716797] },
   420     { rotation: [
   421       -0.17158392071723938, 0.0426151417195797,
   422       -0.0844573974609375, -0.980617105960846],
   423       translation: [0, 0, 138.52847290039062] }];
   425   arcball3.keyDown(arcball3.zoomKeys["out"][0]);
   426   arcball3.keyDown(arcball3.zoomKeys["out"][1]);
   427   for (let i7 = 0; i7 < 10; i7++) {
   428     stack7.push(cloneUpdate(arcball3.update()));
   429   }
   430   arcball3.keyUp(arcball3.zoomKeys["out"][0]);
   431   arcball3.keyUp(arcball3.zoomKeys["out"][1]);
   433   ok(isExpectedUpdate(stack7, expect7),
   434     "Key zoom out events didn't create the expected transformation results.");
   436   let stack8 = [];
   437   let expect8 = [
   438     { rotation: [
   439       -0.17158392071723938, 0.0426151417195797,
   440       -0.0844573974609375, -0.980617105960846],
   441       translation: [0, 0, 123.67562866210938] },
   442     { rotation: [
   443       -0.17158392071723938, 0.0426151417195797,
   444       -0.0844573974609375, -0.980617105960846],
   445       translation: [0, 0, 111.30806732177734] },
   446     { rotation: [
   447       -0.17158392071723938, 0.0426151417195797,
   448       -0.0844573974609375, -0.980617105960846],
   449       translation: [0, 0, 100.17726135253906] },
   450     { rotation: [
   451       -0.17158392071723938, 0.0426151417195797,
   452       -0.0844573974609375, -0.980617105960846],
   453       translation: [0, 0, 90.15953826904297] },
   454     { rotation: [
   455       -0.17158392071723938, 0.0426151417195797,
   456       -0.0844573974609375, -0.980617105960846],
   457       translation: [0, 0, 81.14358520507812] },
   458     { rotation: [
   459       -0.17158392071723938, 0.0426151417195797,
   460       -0.0844573974609375, -0.980617105960846],
   461       translation: [0, 0, 73.02922821044922] },
   462     { rotation: [
   463       -0.17158392071723938, 0.0426151417195797,
   464       -0.0844573974609375, -0.980617105960846],
   465       translation: [0, 0, 65.72630310058594] },
   466     { rotation: [
   467       -0.17158392071723938, 0.0426151417195797,
   468       -0.0844573974609375, -0.980617105960846],
   469       translation: [0, 0, 59.15367126464844] },
   470     { rotation: [
   471       -0.17158392071723938, 0.0426151417195797,
   472       -0.0844573974609375, -0.980617105960846],
   473       translation: [0, 0, 53.238304138183594] },
   474     { rotation: [
   475       -0.17158392071723938, 0.0426151417195797,
   476       -0.0844573974609375, -0.980617105960846],
   477       translation: [0, 0, 47.91447448730469] }];
   479   arcball3.keyDown(arcball3.zoomKeys["unzoom"]);
   480   for (let i8 = 0; i8 < 10; i8++) {
   481     stack8.push(cloneUpdate(arcball3.update()));
   482   }
   483   arcball3.keyUp(arcball3.zoomKeys["unzoom"]);
   485   ok(isExpectedUpdate(stack8, expect8),
   486     "Key zoom reset events didn't create the expected transformation results.");
   489   arcball3.resize(123, 456);
   490   is(arcball3.width, 123,
   491     "The third arcball width wasn't updated correctly.");
   492   is(arcball3.height, 456,
   493     "The third arcball height wasn't updated correctly.");
   494   is(arcball3.radius, 123,
   495     "The third arcball radius wasn't implicitly updated correctly.");
   496 }

mercurial