InputReader_test.cpp 218 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "../InputReader.h"
  17. #include <utils/List.h>
  18. #include <gtest/gtest.h>
  19. #include <math.h>
  20. namespace android {
  21. // An arbitrary time value.
  22. static const nsecs_t ARBITRARY_TIME = 1234;
  23. // Arbitrary display properties.
  24. static const int32_t DISPLAY_ID = 0;
  25. static const int32_t DISPLAY_WIDTH = 480;
  26. static const int32_t DISPLAY_HEIGHT = 800;
  27. // Error tolerance for floating point assertions.
  28. static const float EPSILON = 0.001f;
  29. template<typename T>
  30. static inline T min(T a, T b) {
  31. return a < b ? a : b;
  32. }
  33. static inline float avg(float x, float y) {
  34. return (x + y) / 2;
  35. }
  36. // --- FakePointerController ---
  37. class FakePointerController : public PointerControllerInterface {
  38. bool mHaveBounds;
  39. float mMinX, mMinY, mMaxX, mMaxY;
  40. float mX, mY;
  41. int32_t mButtonState;
  42. protected:
  43. virtual ~FakePointerController() { }
  44. public:
  45. FakePointerController() :
  46. mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
  47. mButtonState(0) {
  48. }
  49. void setBounds(float minX, float minY, float maxX, float maxY) {
  50. mHaveBounds = true;
  51. mMinX = minX;
  52. mMinY = minY;
  53. mMaxX = maxX;
  54. mMaxY = maxY;
  55. }
  56. virtual void setPosition(float x, float y) {
  57. mX = x;
  58. mY = y;
  59. }
  60. virtual void setButtonState(int32_t buttonState) {
  61. mButtonState = buttonState;
  62. }
  63. virtual int32_t getButtonState() const {
  64. return mButtonState;
  65. }
  66. virtual void getPosition(float* outX, float* outY) const {
  67. *outX = mX;
  68. *outY = mY;
  69. }
  70. private:
  71. virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
  72. *outMinX = mMinX;
  73. *outMinY = mMinY;
  74. *outMaxX = mMaxX;
  75. *outMaxY = mMaxY;
  76. return mHaveBounds;
  77. }
  78. virtual void move(float deltaX, float deltaY) {
  79. mX += deltaX;
  80. if (mX < mMinX) mX = mMinX;
  81. if (mX > mMaxX) mX = mMaxX;
  82. mY += deltaY;
  83. if (mY < mMinY) mY = mMinY;
  84. if (mY > mMaxY) mY = mMaxY;
  85. }
  86. virtual void fade(Transition) {
  87. }
  88. virtual void unfade(Transition) {
  89. }
  90. virtual void setPresentation(Presentation) {
  91. }
  92. virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32) {
  93. }
  94. virtual void clearSpots() {
  95. }
  96. };
  97. // --- FakeInputReaderPolicy ---
  98. class FakeInputReaderPolicy : public InputReaderPolicyInterface {
  99. InputReaderConfiguration mConfig;
  100. KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
  101. Vector<InputDeviceInfo> mInputDevices;
  102. TouchAffineTransformation transform;
  103. protected:
  104. virtual ~FakeInputReaderPolicy() { }
  105. public:
  106. FakeInputReaderPolicy() {
  107. }
  108. void setDisplayInfo(int32_t displayId, int32_t width, int32_t height, int32_t orientation) {
  109. // Set the size of both the internal and external display at the same time.
  110. bool isRotated = (orientation == DISPLAY_ORIENTATION_90
  111. || orientation == DISPLAY_ORIENTATION_270);
  112. DisplayViewport v;
  113. v.displayId = displayId;
  114. v.orientation = orientation;
  115. v.logicalLeft = 0;
  116. v.logicalTop = 0;
  117. v.logicalRight = isRotated ? height : width;
  118. v.logicalBottom = isRotated ? width : height;
  119. v.physicalLeft = 0;
  120. v.physicalTop = 0;
  121. v.physicalRight = isRotated ? height : width;
  122. v.physicalBottom = isRotated ? width : height;
  123. v.deviceWidth = isRotated ? height : width;
  124. v.deviceHeight = isRotated ? width : height;
  125. mConfig.setDisplayInfo(false /*external*/, v);
  126. mConfig.setDisplayInfo(true /*external*/, v);
  127. }
  128. void addExcludedDeviceName(const String8& deviceName) {
  129. mConfig.excludedDeviceNames.push(deviceName);
  130. }
  131. void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
  132. mPointerControllers.add(deviceId, controller);
  133. }
  134. const InputReaderConfiguration* getReaderConfiguration() const {
  135. return &mConfig;
  136. }
  137. const Vector<InputDeviceInfo>& getInputDevices() const {
  138. return mInputDevices;
  139. }
  140. TouchAffineTransformation getTouchAffineTransformation(const String8& inputDeviceDescriptor,
  141. int32_t surfaceRotation) {
  142. return transform;
  143. }
  144. void setTouchAffineTransformation(const TouchAffineTransformation t) {
  145. transform = t;
  146. }
  147. private:
  148. virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
  149. *outConfig = mConfig;
  150. }
  151. virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
  152. return mPointerControllers.valueFor(deviceId);
  153. }
  154. virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
  155. mInputDevices = inputDevices;
  156. }
  157. virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
  158. return NULL;
  159. }
  160. virtual String8 getDeviceAlias(const InputDeviceIdentifier&) {
  161. return String8::empty();
  162. }
  163. };
  164. // --- FakeInputListener ---
  165. class FakeInputListener : public InputListenerInterface {
  166. private:
  167. List<NotifyConfigurationChangedArgs> mNotifyConfigurationChangedArgsQueue;
  168. List<NotifyDeviceResetArgs> mNotifyDeviceResetArgsQueue;
  169. List<NotifyKeyArgs> mNotifyKeyArgsQueue;
  170. List<NotifyMotionArgs> mNotifyMotionArgsQueue;
  171. List<NotifySwitchArgs> mNotifySwitchArgsQueue;
  172. protected:
  173. virtual ~FakeInputListener() { }
  174. public:
  175. FakeInputListener() {
  176. }
  177. void assertNotifyConfigurationChangedWasCalled(
  178. NotifyConfigurationChangedArgs* outEventArgs = NULL) {
  179. ASSERT_FALSE(mNotifyConfigurationChangedArgsQueue.empty())
  180. << "Expected notifyConfigurationChanged() to have been called.";
  181. if (outEventArgs) {
  182. *outEventArgs = *mNotifyConfigurationChangedArgsQueue.begin();
  183. }
  184. mNotifyConfigurationChangedArgsQueue.erase(mNotifyConfigurationChangedArgsQueue.begin());
  185. }
  186. void assertNotifyDeviceResetWasCalled(
  187. NotifyDeviceResetArgs* outEventArgs = NULL) {
  188. ASSERT_FALSE(mNotifyDeviceResetArgsQueue.empty())
  189. << "Expected notifyDeviceReset() to have been called.";
  190. if (outEventArgs) {
  191. *outEventArgs = *mNotifyDeviceResetArgsQueue.begin();
  192. }
  193. mNotifyDeviceResetArgsQueue.erase(mNotifyDeviceResetArgsQueue.begin());
  194. }
  195. void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = NULL) {
  196. ASSERT_FALSE(mNotifyKeyArgsQueue.empty())
  197. << "Expected notifyKey() to have been called.";
  198. if (outEventArgs) {
  199. *outEventArgs = *mNotifyKeyArgsQueue.begin();
  200. }
  201. mNotifyKeyArgsQueue.erase(mNotifyKeyArgsQueue.begin());
  202. }
  203. void assertNotifyKeyWasNotCalled() {
  204. ASSERT_TRUE(mNotifyKeyArgsQueue.empty())
  205. << "Expected notifyKey() to not have been called.";
  206. }
  207. void assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs = NULL) {
  208. ASSERT_FALSE(mNotifyMotionArgsQueue.empty())
  209. << "Expected notifyMotion() to have been called.";
  210. if (outEventArgs) {
  211. *outEventArgs = *mNotifyMotionArgsQueue.begin();
  212. }
  213. mNotifyMotionArgsQueue.erase(mNotifyMotionArgsQueue.begin());
  214. }
  215. void assertNotifyMotionWasNotCalled() {
  216. ASSERT_TRUE(mNotifyMotionArgsQueue.empty())
  217. << "Expected notifyMotion() to not have been called.";
  218. }
  219. void assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs = NULL) {
  220. ASSERT_FALSE(mNotifySwitchArgsQueue.empty())
  221. << "Expected notifySwitch() to have been called.";
  222. if (outEventArgs) {
  223. *outEventArgs = *mNotifySwitchArgsQueue.begin();
  224. }
  225. mNotifySwitchArgsQueue.erase(mNotifySwitchArgsQueue.begin());
  226. }
  227. private:
  228. virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
  229. mNotifyConfigurationChangedArgsQueue.push_back(*args);
  230. }
  231. virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) {
  232. mNotifyDeviceResetArgsQueue.push_back(*args);
  233. }
  234. virtual void notifyKey(const NotifyKeyArgs* args) {
  235. mNotifyKeyArgsQueue.push_back(*args);
  236. }
  237. virtual void notifyMotion(const NotifyMotionArgs* args) {
  238. mNotifyMotionArgsQueue.push_back(*args);
  239. }
  240. virtual void notifySwitch(const NotifySwitchArgs* args) {
  241. mNotifySwitchArgsQueue.push_back(*args);
  242. }
  243. };
  244. // --- FakeEventHub ---
  245. class FakeEventHub : public EventHubInterface {
  246. struct KeyInfo {
  247. int32_t keyCode;
  248. uint32_t flags;
  249. };
  250. struct Device {
  251. InputDeviceIdentifier identifier;
  252. uint32_t classes;
  253. PropertyMap configuration;
  254. KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
  255. KeyedVector<int, bool> relativeAxes;
  256. KeyedVector<int32_t, int32_t> keyCodeStates;
  257. KeyedVector<int32_t, int32_t> scanCodeStates;
  258. KeyedVector<int32_t, int32_t> switchStates;
  259. KeyedVector<int32_t, int32_t> absoluteAxisValue;
  260. KeyedVector<int32_t, KeyInfo> keysByScanCode;
  261. KeyedVector<int32_t, KeyInfo> keysByUsageCode;
  262. KeyedVector<int32_t, bool> leds;
  263. Vector<VirtualKeyDefinition> virtualKeys;
  264. Device(uint32_t classes) :
  265. classes(classes) {
  266. }
  267. };
  268. KeyedVector<int32_t, Device*> mDevices;
  269. Vector<String8> mExcludedDevices;
  270. List<RawEvent> mEvents;
  271. protected:
  272. virtual ~FakeEventHub() {
  273. for (size_t i = 0; i < mDevices.size(); i++) {
  274. delete mDevices.valueAt(i);
  275. }
  276. }
  277. public:
  278. FakeEventHub() { }
  279. void addDevice(int32_t deviceId, const String8& name, uint32_t classes) {
  280. Device* device = new Device(classes);
  281. device->identifier.name = name;
  282. mDevices.add(deviceId, device);
  283. enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
  284. }
  285. void removeDevice(int32_t deviceId) {
  286. delete mDevices.valueFor(deviceId);
  287. mDevices.removeItem(deviceId);
  288. enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
  289. }
  290. void finishDeviceScan() {
  291. enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
  292. }
  293. void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
  294. Device* device = getDevice(deviceId);
  295. device->configuration.addProperty(key, value);
  296. }
  297. void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
  298. Device* device = getDevice(deviceId);
  299. device->configuration.addAll(configuration);
  300. }
  301. void addAbsoluteAxis(int32_t deviceId, int axis,
  302. int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
  303. Device* device = getDevice(deviceId);
  304. RawAbsoluteAxisInfo info;
  305. info.valid = true;
  306. info.minValue = minValue;
  307. info.maxValue = maxValue;
  308. info.flat = flat;
  309. info.fuzz = fuzz;
  310. info.resolution = resolution;
  311. device->absoluteAxes.add(axis, info);
  312. }
  313. void addRelativeAxis(int32_t deviceId, int32_t axis) {
  314. Device* device = getDevice(deviceId);
  315. device->relativeAxes.add(axis, true);
  316. }
  317. void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
  318. Device* device = getDevice(deviceId);
  319. device->keyCodeStates.replaceValueFor(keyCode, state);
  320. }
  321. void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
  322. Device* device = getDevice(deviceId);
  323. device->scanCodeStates.replaceValueFor(scanCode, state);
  324. }
  325. void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
  326. Device* device = getDevice(deviceId);
  327. device->switchStates.replaceValueFor(switchCode, state);
  328. }
  329. void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
  330. Device* device = getDevice(deviceId);
  331. device->absoluteAxisValue.replaceValueFor(axis, value);
  332. }
  333. void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
  334. int32_t keyCode, uint32_t flags) {
  335. Device* device = getDevice(deviceId);
  336. KeyInfo info;
  337. info.keyCode = keyCode;
  338. info.flags = flags;
  339. if (scanCode) {
  340. device->keysByScanCode.add(scanCode, info);
  341. }
  342. if (usageCode) {
  343. device->keysByUsageCode.add(usageCode, info);
  344. }
  345. }
  346. void addLed(int32_t deviceId, int32_t led, bool initialState) {
  347. Device* device = getDevice(deviceId);
  348. device->leds.add(led, initialState);
  349. }
  350. bool getLedState(int32_t deviceId, int32_t led) {
  351. Device* device = getDevice(deviceId);
  352. return device->leds.valueFor(led);
  353. }
  354. Vector<String8>& getExcludedDevices() {
  355. return mExcludedDevices;
  356. }
  357. void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
  358. Device* device = getDevice(deviceId);
  359. device->virtualKeys.push(definition);
  360. }
  361. void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
  362. int32_t code, int32_t value) {
  363. RawEvent event;
  364. event.when = when;
  365. event.deviceId = deviceId;
  366. event.type = type;
  367. event.code = code;
  368. event.value = value;
  369. mEvents.push_back(event);
  370. if (type == EV_ABS) {
  371. setAbsoluteAxisValue(deviceId, code, value);
  372. }
  373. }
  374. void assertQueueIsEmpty() {
  375. ASSERT_EQ(size_t(0), mEvents.size())
  376. << "Expected the event queue to be empty (fully consumed).";
  377. }
  378. private:
  379. Device* getDevice(int32_t deviceId) const {
  380. ssize_t index = mDevices.indexOfKey(deviceId);
  381. return index >= 0 ? mDevices.valueAt(index) : NULL;
  382. }
  383. virtual uint32_t getDeviceClasses(int32_t deviceId) const {
  384. Device* device = getDevice(deviceId);
  385. return device ? device->classes : 0;
  386. }
  387. virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
  388. Device* device = getDevice(deviceId);
  389. return device ? device->identifier : InputDeviceIdentifier();
  390. }
  391. virtual int32_t getDeviceControllerNumber(int32_t) const {
  392. return 0;
  393. }
  394. virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
  395. Device* device = getDevice(deviceId);
  396. if (device) {
  397. *outConfiguration = device->configuration;
  398. }
  399. }
  400. virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
  401. RawAbsoluteAxisInfo* outAxisInfo) const {
  402. Device* device = getDevice(deviceId);
  403. if (device) {
  404. ssize_t index = device->absoluteAxes.indexOfKey(axis);
  405. if (index >= 0) {
  406. *outAxisInfo = device->absoluteAxes.valueAt(index);
  407. return OK;
  408. }
  409. }
  410. outAxisInfo->clear();
  411. return -1;
  412. }
  413. virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
  414. Device* device = getDevice(deviceId);
  415. if (device) {
  416. return device->relativeAxes.indexOfKey(axis) >= 0;
  417. }
  418. return false;
  419. }
  420. virtual bool hasInputProperty(int32_t, int) const {
  421. return false;
  422. }
  423. virtual status_t mapKey(int32_t deviceId,
  424. int32_t scanCode, int32_t usageCode, int32_t metaState,
  425. int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
  426. Device* device = getDevice(deviceId);
  427. if (device) {
  428. const KeyInfo* key = getKey(device, scanCode, usageCode);
  429. if (key) {
  430. if (outKeycode) {
  431. *outKeycode = key->keyCode;
  432. }
  433. if (outFlags) {
  434. *outFlags = key->flags;
  435. }
  436. if (outMetaState) {
  437. *outMetaState = metaState;
  438. }
  439. return OK;
  440. }
  441. }
  442. return NAME_NOT_FOUND;
  443. }
  444. const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
  445. if (usageCode) {
  446. ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
  447. if (index >= 0) {
  448. return &device->keysByUsageCode.valueAt(index);
  449. }
  450. }
  451. if (scanCode) {
  452. ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
  453. if (index >= 0) {
  454. return &device->keysByScanCode.valueAt(index);
  455. }
  456. }
  457. return NULL;
  458. }
  459. virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
  460. return NAME_NOT_FOUND;
  461. }
  462. virtual void setExcludedDevices(const Vector<String8>& devices) {
  463. mExcludedDevices = devices;
  464. }
  465. virtual size_t getEvents(int, RawEvent* buffer, size_t) {
  466. if (mEvents.empty()) {
  467. return 0;
  468. }
  469. *buffer = *mEvents.begin();
  470. mEvents.erase(mEvents.begin());
  471. return 1;
  472. }
  473. virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
  474. Device* device = getDevice(deviceId);
  475. if (device) {
  476. ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
  477. if (index >= 0) {
  478. return device->scanCodeStates.valueAt(index);
  479. }
  480. }
  481. return AKEY_STATE_UNKNOWN;
  482. }
  483. virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
  484. Device* device = getDevice(deviceId);
  485. if (device) {
  486. ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
  487. if (index >= 0) {
  488. return device->keyCodeStates.valueAt(index);
  489. }
  490. }
  491. return AKEY_STATE_UNKNOWN;
  492. }
  493. virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
  494. Device* device = getDevice(deviceId);
  495. if (device) {
  496. ssize_t index = device->switchStates.indexOfKey(sw);
  497. if (index >= 0) {
  498. return device->switchStates.valueAt(index);
  499. }
  500. }
  501. return AKEY_STATE_UNKNOWN;
  502. }
  503. virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
  504. int32_t* outValue) const {
  505. Device* device = getDevice(deviceId);
  506. if (device) {
  507. ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
  508. if (index >= 0) {
  509. *outValue = device->absoluteAxisValue.valueAt(index);
  510. return OK;
  511. }
  512. }
  513. *outValue = 0;
  514. return -1;
  515. }
  516. virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
  517. uint8_t* outFlags) const {
  518. bool result = false;
  519. Device* device = getDevice(deviceId);
  520. if (device) {
  521. for (size_t i = 0; i < numCodes; i++) {
  522. for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
  523. if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
  524. outFlags[i] = 1;
  525. result = true;
  526. }
  527. }
  528. for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
  529. if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
  530. outFlags[i] = 1;
  531. result = true;
  532. }
  533. }
  534. }
  535. }
  536. return result;
  537. }
  538. virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
  539. Device* device = getDevice(deviceId);
  540. if (device) {
  541. ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
  542. return index >= 0;
  543. }
  544. return false;
  545. }
  546. virtual bool hasLed(int32_t deviceId, int32_t led) const {
  547. Device* device = getDevice(deviceId);
  548. return device && device->leds.indexOfKey(led) >= 0;
  549. }
  550. virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
  551. Device* device = getDevice(deviceId);
  552. if (device) {
  553. ssize_t index = device->leds.indexOfKey(led);
  554. if (index >= 0) {
  555. device->leds.replaceValueAt(led, on);
  556. } else {
  557. ADD_FAILURE()
  558. << "Attempted to set the state of an LED that the EventHub declared "
  559. "was not present. led=" << led;
  560. }
  561. }
  562. }
  563. virtual void getVirtualKeyDefinitions(int32_t deviceId,
  564. Vector<VirtualKeyDefinition>& outVirtualKeys) const {
  565. outVirtualKeys.clear();
  566. Device* device = getDevice(deviceId);
  567. if (device) {
  568. outVirtualKeys.appendVector(device->virtualKeys);
  569. }
  570. }
  571. virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
  572. return NULL;
  573. }
  574. virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
  575. return false;
  576. }
  577. virtual void vibrate(int32_t, nsecs_t) {
  578. }
  579. virtual void cancelVibrate(int32_t) {
  580. }
  581. virtual bool isExternal(int32_t) const {
  582. return false;
  583. }
  584. virtual void dump(String8&) {
  585. }
  586. virtual void monitor() {
  587. }
  588. virtual void requestReopenDevices() {
  589. }
  590. virtual void wake() {
  591. }
  592. };
  593. // --- FakeInputReaderContext ---
  594. class FakeInputReaderContext : public InputReaderContext {
  595. sp<EventHubInterface> mEventHub;
  596. sp<InputReaderPolicyInterface> mPolicy;
  597. sp<InputListenerInterface> mListener;
  598. int32_t mGlobalMetaState;
  599. bool mUpdateGlobalMetaStateWasCalled;
  600. int32_t mGeneration;
  601. public:
  602. FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
  603. const sp<InputReaderPolicyInterface>& policy,
  604. const sp<InputListenerInterface>& listener) :
  605. mEventHub(eventHub), mPolicy(policy), mListener(listener),
  606. mGlobalMetaState(0) {
  607. }
  608. virtual ~FakeInputReaderContext() { }
  609. void assertUpdateGlobalMetaStateWasCalled() {
  610. ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
  611. << "Expected updateGlobalMetaState() to have been called.";
  612. mUpdateGlobalMetaStateWasCalled = false;
  613. }
  614. void setGlobalMetaState(int32_t state) {
  615. mGlobalMetaState = state;
  616. }
  617. private:
  618. virtual void updateGlobalMetaState() {
  619. mUpdateGlobalMetaStateWasCalled = true;
  620. }
  621. virtual int32_t getGlobalMetaState() {
  622. return mGlobalMetaState;
  623. }
  624. virtual EventHubInterface* getEventHub() {
  625. return mEventHub.get();
  626. }
  627. virtual InputReaderPolicyInterface* getPolicy() {
  628. return mPolicy.get();
  629. }
  630. virtual InputListenerInterface* getListener() {
  631. return mListener.get();
  632. }
  633. virtual void disableVirtualKeysUntil(nsecs_t) {
  634. }
  635. virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
  636. return false;
  637. }
  638. virtual void fadePointer() {
  639. }
  640. virtual void requestTimeoutAtTime(nsecs_t) {
  641. }
  642. virtual int32_t bumpGeneration() {
  643. return ++mGeneration;
  644. }
  645. virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) {
  646. }
  647. virtual void dispatchExternalStylusState(const StylusState&) {
  648. }
  649. };
  650. // --- FakeInputMapper ---
  651. class FakeInputMapper : public InputMapper {
  652. uint32_t mSources;
  653. int32_t mKeyboardType;
  654. int32_t mMetaState;
  655. KeyedVector<int32_t, int32_t> mKeyCodeStates;
  656. KeyedVector<int32_t, int32_t> mScanCodeStates;
  657. KeyedVector<int32_t, int32_t> mSwitchStates;
  658. Vector<int32_t> mSupportedKeyCodes;
  659. RawEvent mLastEvent;
  660. bool mConfigureWasCalled;
  661. bool mResetWasCalled;
  662. bool mProcessWasCalled;
  663. public:
  664. FakeInputMapper(InputDevice* device, uint32_t sources) :
  665. InputMapper(device),
  666. mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
  667. mMetaState(0),
  668. mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
  669. }
  670. virtual ~FakeInputMapper() { }
  671. void setKeyboardType(int32_t keyboardType) {
  672. mKeyboardType = keyboardType;
  673. }
  674. void setMetaState(int32_t metaState) {
  675. mMetaState = metaState;
  676. }
  677. void assertConfigureWasCalled() {
  678. ASSERT_TRUE(mConfigureWasCalled)
  679. << "Expected configure() to have been called.";
  680. mConfigureWasCalled = false;
  681. }
  682. void assertResetWasCalled() {
  683. ASSERT_TRUE(mResetWasCalled)
  684. << "Expected reset() to have been called.";
  685. mResetWasCalled = false;
  686. }
  687. void assertProcessWasCalled(RawEvent* outLastEvent = NULL) {
  688. ASSERT_TRUE(mProcessWasCalled)
  689. << "Expected process() to have been called.";
  690. if (outLastEvent) {
  691. *outLastEvent = mLastEvent;
  692. }
  693. mProcessWasCalled = false;
  694. }
  695. void setKeyCodeState(int32_t keyCode, int32_t state) {
  696. mKeyCodeStates.replaceValueFor(keyCode, state);
  697. }
  698. void setScanCodeState(int32_t scanCode, int32_t state) {
  699. mScanCodeStates.replaceValueFor(scanCode, state);
  700. }
  701. void setSwitchState(int32_t switchCode, int32_t state) {
  702. mSwitchStates.replaceValueFor(switchCode, state);
  703. }
  704. void addSupportedKeyCode(int32_t keyCode) {
  705. mSupportedKeyCodes.add(keyCode);
  706. }
  707. private:
  708. virtual uint32_t getSources() {
  709. return mSources;
  710. }
  711. virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
  712. InputMapper::populateDeviceInfo(deviceInfo);
  713. if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
  714. deviceInfo->setKeyboardType(mKeyboardType);
  715. }
  716. }
  717. virtual void configure(nsecs_t, const InputReaderConfiguration*, uint32_t) {
  718. mConfigureWasCalled = true;
  719. }
  720. virtual void reset(nsecs_t) {
  721. mResetWasCalled = true;
  722. }
  723. virtual void process(const RawEvent* rawEvent) {
  724. mLastEvent = *rawEvent;
  725. mProcessWasCalled = true;
  726. }
  727. virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
  728. ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
  729. return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
  730. }
  731. virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
  732. ssize_t index = mScanCodeStates.indexOfKey(scanCode);
  733. return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
  734. }
  735. virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
  736. ssize_t index = mSwitchStates.indexOfKey(switchCode);
  737. return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
  738. }
  739. virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
  740. const int32_t* keyCodes, uint8_t* outFlags) {
  741. bool result = false;
  742. for (size_t i = 0; i < numCodes; i++) {
  743. for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
  744. if (keyCodes[i] == mSupportedKeyCodes[j]) {
  745. outFlags[i] = 1;
  746. result = true;
  747. }
  748. }
  749. }
  750. return result;
  751. }
  752. virtual int32_t getMetaState() {
  753. return mMetaState;
  754. }
  755. virtual void fadePointer() {
  756. }
  757. };
  758. // --- InstrumentedInputReader ---
  759. class InstrumentedInputReader : public InputReader {
  760. InputDevice* mNextDevice;
  761. public:
  762. InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
  763. const sp<InputReaderPolicyInterface>& policy,
  764. const sp<InputListenerInterface>& listener) :
  765. InputReader(eventHub, policy, listener),
  766. mNextDevice(NULL) {
  767. }
  768. virtual ~InstrumentedInputReader() {
  769. if (mNextDevice) {
  770. delete mNextDevice;
  771. }
  772. }
  773. void setNextDevice(InputDevice* device) {
  774. mNextDevice = device;
  775. }
  776. InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const String8& name,
  777. uint32_t classes) {
  778. InputDeviceIdentifier identifier;
  779. identifier.name = name;
  780. int32_t generation = deviceId + 1;
  781. return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
  782. classes);
  783. }
  784. protected:
  785. virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
  786. const InputDeviceIdentifier& identifier, uint32_t classes) {
  787. if (mNextDevice) {
  788. InputDevice* device = mNextDevice;
  789. mNextDevice = NULL;
  790. return device;
  791. }
  792. return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
  793. }
  794. friend class InputReaderTest;
  795. };
  796. // --- InputReaderTest ---
  797. class InputReaderTest : public testing::Test {
  798. protected:
  799. sp<FakeInputListener> mFakeListener;
  800. sp<FakeInputReaderPolicy> mFakePolicy;
  801. sp<FakeEventHub> mFakeEventHub;
  802. sp<InstrumentedInputReader> mReader;
  803. virtual void SetUp() {
  804. mFakeEventHub = new FakeEventHub();
  805. mFakePolicy = new FakeInputReaderPolicy();
  806. mFakeListener = new FakeInputListener();
  807. mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
  808. }
  809. virtual void TearDown() {
  810. mReader.clear();
  811. mFakeListener.clear();
  812. mFakePolicy.clear();
  813. mFakeEventHub.clear();
  814. }
  815. void addDevice(int32_t deviceId, const String8& name, uint32_t classes,
  816. const PropertyMap* configuration) {
  817. mFakeEventHub->addDevice(deviceId, name, classes);
  818. if (configuration) {
  819. mFakeEventHub->addConfigurationMap(deviceId, configuration);
  820. }
  821. mFakeEventHub->finishDeviceScan();
  822. mReader->loopOnce();
  823. mReader->loopOnce();
  824. mFakeEventHub->assertQueueIsEmpty();
  825. }
  826. FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
  827. const String8& name, uint32_t classes, uint32_t sources,
  828. const PropertyMap* configuration) {
  829. InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
  830. FakeInputMapper* mapper = new FakeInputMapper(device, sources);
  831. device->addMapper(mapper);
  832. mReader->setNextDevice(device);
  833. addDevice(deviceId, name, classes, configuration);
  834. return mapper;
  835. }
  836. };
  837. TEST_F(InputReaderTest, GetInputDevices) {
  838. ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"),
  839. INPUT_DEVICE_CLASS_KEYBOARD, NULL));
  840. ASSERT_NO_FATAL_FAILURE(addDevice(2, String8("ignored"),
  841. 0, NULL)); // no classes so device will be ignored
  842. Vector<InputDeviceInfo> inputDevices;
  843. mReader->getInputDevices(inputDevices);
  844. ASSERT_EQ(1U, inputDevices.size());
  845. ASSERT_EQ(1, inputDevices[0].getId());
  846. ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.string());
  847. ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
  848. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
  849. ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
  850. // Should also have received a notification describing the new input devices.
  851. inputDevices = mFakePolicy->getInputDevices();
  852. ASSERT_EQ(1U, inputDevices.size());
  853. ASSERT_EQ(1, inputDevices[0].getId());
  854. ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.string());
  855. ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
  856. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
  857. ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
  858. }
  859. TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
  860. FakeInputMapper* mapper = NULL;
  861. ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
  862. INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
  863. mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
  864. ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
  865. AINPUT_SOURCE_ANY, AKEYCODE_A))
  866. << "Should return unknown when the device id is >= 0 but unknown.";
  867. ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
  868. AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
  869. << "Should return unknown when the device id is valid but the sources are not supported by the device.";
  870. ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
  871. AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
  872. << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
  873. ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
  874. AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
  875. << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
  876. ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
  877. AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
  878. << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
  879. }
  880. TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
  881. FakeInputMapper* mapper = NULL;
  882. ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
  883. INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
  884. mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
  885. ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
  886. AINPUT_SOURCE_ANY, KEY_A))
  887. << "Should return unknown when the device id is >= 0 but unknown.";
  888. ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
  889. AINPUT_SOURCE_TRACKBALL, KEY_A))
  890. << "Should return unknown when the device id is valid but the sources are not supported by the device.";
  891. ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
  892. AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
  893. << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
  894. ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
  895. AINPUT_SOURCE_TRACKBALL, KEY_A))
  896. << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
  897. ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
  898. AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
  899. << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
  900. }
  901. TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
  902. FakeInputMapper* mapper = NULL;
  903. ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
  904. INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
  905. mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
  906. ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
  907. AINPUT_SOURCE_ANY, SW_LID))
  908. << "Should return unknown when the device id is >= 0 but unknown.";
  909. ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
  910. AINPUT_SOURCE_TRACKBALL, SW_LID))
  911. << "Should return unknown when the device id is valid but the sources are not supported by the device.";
  912. ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
  913. AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
  914. << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
  915. ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
  916. AINPUT_SOURCE_TRACKBALL, SW_LID))
  917. << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
  918. ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
  919. AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
  920. << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
  921. }
  922. TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
  923. FakeInputMapper* mapper = NULL;
  924. ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
  925. INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
  926. mapper->addSupportedKeyCode(AKEYCODE_A);
  927. mapper->addSupportedKeyCode(AKEYCODE_B);
  928. const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
  929. uint8_t flags[4] = { 0, 0, 0, 1 };
  930. ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
  931. << "Should return false when device id is >= 0 but unknown.";
  932. ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
  933. flags[3] = 1;
  934. ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
  935. << "Should return false when device id is valid but the sources are not supported by the device.";
  936. ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
  937. flags[3] = 1;
  938. ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
  939. << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
  940. ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
  941. flags[3] = 1;
  942. ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
  943. << "Should return false when the device id is < 0 but the sources are not supported by any device.";
  944. ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
  945. flags[3] = 1;
  946. ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
  947. << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
  948. ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
  949. }
  950. TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
  951. addDevice(1, String8("ignored"), INPUT_DEVICE_CLASS_KEYBOARD, NULL);
  952. NotifyConfigurationChangedArgs args;
  953. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
  954. ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
  955. }
  956. TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
  957. FakeInputMapper* mapper = NULL;
  958. ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
  959. INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
  960. mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
  961. mReader->loopOnce();
  962. ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
  963. RawEvent event;
  964. ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
  965. ASSERT_EQ(0, event.when);
  966. ASSERT_EQ(1, event.deviceId);
  967. ASSERT_EQ(EV_KEY, event.type);
  968. ASSERT_EQ(KEY_A, event.code);
  969. ASSERT_EQ(1, event.value);
  970. }
  971. // --- InputDeviceTest ---
  972. class InputDeviceTest : public testing::Test {
  973. protected:
  974. static const char* DEVICE_NAME;
  975. static const int32_t DEVICE_ID;
  976. static const int32_t DEVICE_GENERATION;
  977. static const int32_t DEVICE_CONTROLLER_NUMBER;
  978. static const uint32_t DEVICE_CLASSES;
  979. sp<FakeEventHub> mFakeEventHub;
  980. sp<FakeInputReaderPolicy> mFakePolicy;
  981. sp<FakeInputListener> mFakeListener;
  982. FakeInputReaderContext* mFakeContext;
  983. InputDevice* mDevice;
  984. virtual void SetUp() {
  985. mFakeEventHub = new FakeEventHub();
  986. mFakePolicy = new FakeInputReaderPolicy();
  987. mFakeListener = new FakeInputListener();
  988. mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
  989. mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
  990. InputDeviceIdentifier identifier;
  991. identifier.name = DEVICE_NAME;
  992. mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
  993. DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
  994. }
  995. virtual void TearDown() {
  996. delete mDevice;
  997. delete mFakeContext;
  998. mFakeListener.clear();
  999. mFakePolicy.clear();
  1000. mFakeEventHub.clear();
  1001. }
  1002. };
  1003. const char* InputDeviceTest::DEVICE_NAME = "device";
  1004. const int32_t InputDeviceTest::DEVICE_ID = 1;
  1005. const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
  1006. const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
  1007. const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
  1008. | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
  1009. TEST_F(InputDeviceTest, ImmutableProperties) {
  1010. ASSERT_EQ(DEVICE_ID, mDevice->getId());
  1011. ASSERT_STREQ(DEVICE_NAME, mDevice->getName());
  1012. ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
  1013. }
  1014. TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
  1015. // Configuration.
  1016. InputReaderConfiguration config;
  1017. mDevice->configure(ARBITRARY_TIME, &config, 0);
  1018. // Reset.
  1019. mDevice->reset(ARBITRARY_TIME);
  1020. NotifyDeviceResetArgs resetArgs;
  1021. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
  1022. ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
  1023. ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
  1024. // Metadata.
  1025. ASSERT_TRUE(mDevice->isIgnored());
  1026. ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
  1027. InputDeviceInfo info;
  1028. mDevice->getDeviceInfo(&info);
  1029. ASSERT_EQ(DEVICE_ID, info.getId());
  1030. ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.string());
  1031. ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
  1032. ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
  1033. // State queries.
  1034. ASSERT_EQ(0, mDevice->getMetaState());
  1035. ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
  1036. << "Ignored device should return unknown key code state.";
  1037. ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
  1038. << "Ignored device should return unknown scan code state.";
  1039. ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
  1040. << "Ignored device should return unknown switch state.";
  1041. const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
  1042. uint8_t flags[2] = { 0, 1 };
  1043. ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
  1044. << "Ignored device should never mark any key codes.";
  1045. ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
  1046. ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
  1047. }
  1048. TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
  1049. // Configuration.
  1050. mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
  1051. FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
  1052. mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
  1053. mapper1->setMetaState(AMETA_ALT_ON);
  1054. mapper1->addSupportedKeyCode(AKEYCODE_A);
  1055. mapper1->addSupportedKeyCode(AKEYCODE_B);
  1056. mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
  1057. mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
  1058. mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
  1059. mapper1->setScanCodeState(3, AKEY_STATE_UP);
  1060. mapper1->setSwitchState(4, AKEY_STATE_DOWN);
  1061. mDevice->addMapper(mapper1);
  1062. FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
  1063. mapper2->setMetaState(AMETA_SHIFT_ON);
  1064. mDevice->addMapper(mapper2);
  1065. InputReaderConfiguration config;
  1066. mDevice->configure(ARBITRARY_TIME, &config, 0);
  1067. String8 propertyValue;
  1068. ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
  1069. << "Device should have read configuration during configuration phase.";
  1070. ASSERT_STREQ("value", propertyValue.string());
  1071. ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
  1072. ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
  1073. // Reset
  1074. mDevice->reset(ARBITRARY_TIME);
  1075. ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
  1076. ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
  1077. NotifyDeviceResetArgs resetArgs;
  1078. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
  1079. ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
  1080. ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
  1081. // Metadata.
  1082. ASSERT_FALSE(mDevice->isIgnored());
  1083. ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
  1084. InputDeviceInfo info;
  1085. mDevice->getDeviceInfo(&info);
  1086. ASSERT_EQ(DEVICE_ID, info.getId());
  1087. ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.string());
  1088. ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
  1089. ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
  1090. // State queries.
  1091. ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
  1092. << "Should query mappers and combine meta states.";
  1093. ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
  1094. << "Should return unknown key code state when source not supported.";
  1095. ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
  1096. << "Should return unknown scan code state when source not supported.";
  1097. ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
  1098. << "Should return unknown switch state when source not supported.";
  1099. ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
  1100. << "Should query mapper when source is supported.";
  1101. ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
  1102. << "Should query mapper when source is supported.";
  1103. ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
  1104. << "Should query mapper when source is supported.";
  1105. const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
  1106. uint8_t flags[4] = { 0, 0, 0, 1 };
  1107. ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
  1108. << "Should do nothing when source is unsupported.";
  1109. ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
  1110. ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
  1111. ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
  1112. ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
  1113. ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
  1114. << "Should query mapper when source is supported.";
  1115. ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
  1116. ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
  1117. ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
  1118. ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
  1119. // Event handling.
  1120. RawEvent event;
  1121. mDevice->process(&event, 1);
  1122. ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
  1123. ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
  1124. }
  1125. // --- InputMapperTest ---
  1126. class InputMapperTest : public testing::Test {
  1127. protected:
  1128. static const char* DEVICE_NAME;
  1129. static const int32_t DEVICE_ID;
  1130. static const int32_t DEVICE_GENERATION;
  1131. static const int32_t DEVICE_CONTROLLER_NUMBER;
  1132. static const uint32_t DEVICE_CLASSES;
  1133. sp<FakeEventHub> mFakeEventHub;
  1134. sp<FakeInputReaderPolicy> mFakePolicy;
  1135. sp<FakeInputListener> mFakeListener;
  1136. FakeInputReaderContext* mFakeContext;
  1137. InputDevice* mDevice;
  1138. virtual void SetUp() {
  1139. mFakeEventHub = new FakeEventHub();
  1140. mFakePolicy = new FakeInputReaderPolicy();
  1141. mFakeListener = new FakeInputListener();
  1142. mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
  1143. InputDeviceIdentifier identifier;
  1144. identifier.name = DEVICE_NAME;
  1145. mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
  1146. DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
  1147. mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
  1148. }
  1149. virtual void TearDown() {
  1150. delete mDevice;
  1151. delete mFakeContext;
  1152. mFakeListener.clear();
  1153. mFakePolicy.clear();
  1154. mFakeEventHub.clear();
  1155. }
  1156. void addConfigurationProperty(const char* key, const char* value) {
  1157. mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8(key), String8(value));
  1158. }
  1159. void addMapperAndConfigure(InputMapper* mapper) {
  1160. mDevice->addMapper(mapper);
  1161. mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
  1162. mDevice->reset(ARBITRARY_TIME);
  1163. }
  1164. void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
  1165. int32_t orientation) {
  1166. mFakePolicy->setDisplayInfo(displayId, width, height, orientation);
  1167. mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
  1168. InputReaderConfiguration::CHANGE_DISPLAY_INFO);
  1169. }
  1170. static void process(InputMapper* mapper, nsecs_t when, int32_t deviceId, int32_t type,
  1171. int32_t code, int32_t value) {
  1172. RawEvent event;
  1173. event.when = when;
  1174. event.deviceId = deviceId;
  1175. event.type = type;
  1176. event.code = code;
  1177. event.value = value;
  1178. mapper->process(&event);
  1179. }
  1180. static void assertMotionRange(const InputDeviceInfo& info,
  1181. int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
  1182. const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
  1183. ASSERT_TRUE(range != NULL) << "Axis: " << axis << " Source: " << source;
  1184. ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
  1185. ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
  1186. ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
  1187. ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
  1188. ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
  1189. ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
  1190. }
  1191. static void assertPointerCoords(const PointerCoords& coords,
  1192. float x, float y, float pressure, float size,
  1193. float touchMajor, float touchMinor, float toolMajor, float toolMinor,
  1194. float orientation, float distance) {
  1195. ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
  1196. ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
  1197. ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
  1198. ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
  1199. ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
  1200. ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
  1201. ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
  1202. ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
  1203. ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
  1204. ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
  1205. }
  1206. static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
  1207. float actualX, actualY;
  1208. controller->getPosition(&actualX, &actualY);
  1209. ASSERT_NEAR(x, actualX, 1);
  1210. ASSERT_NEAR(y, actualY, 1);
  1211. }
  1212. };
  1213. const char* InputMapperTest::DEVICE_NAME = "device";
  1214. const int32_t InputMapperTest::DEVICE_ID = 1;
  1215. const int32_t InputMapperTest::DEVICE_GENERATION = 2;
  1216. const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
  1217. const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
  1218. // --- SwitchInputMapperTest ---
  1219. class SwitchInputMapperTest : public InputMapperTest {
  1220. protected:
  1221. };
  1222. TEST_F(SwitchInputMapperTest, GetSources) {
  1223. SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
  1224. addMapperAndConfigure(mapper);
  1225. ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
  1226. }
  1227. TEST_F(SwitchInputMapperTest, GetSwitchState) {
  1228. SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
  1229. addMapperAndConfigure(mapper);
  1230. mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
  1231. ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
  1232. mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
  1233. ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
  1234. }
  1235. TEST_F(SwitchInputMapperTest, Process) {
  1236. SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
  1237. addMapperAndConfigure(mapper);
  1238. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_LID, 1);
  1239. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
  1240. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_HEADPHONE_INSERT, 0);
  1241. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1242. NotifySwitchArgs args;
  1243. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
  1244. ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
  1245. ASSERT_EQ((1 << SW_LID) | (1 << SW_JACK_PHYSICAL_INSERT), args.switchValues);
  1246. ASSERT_EQ((1 << SW_LID) | (1 << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
  1247. args.switchMask);
  1248. ASSERT_EQ(uint32_t(0), args.policyFlags);
  1249. }
  1250. // --- KeyboardInputMapperTest ---
  1251. class KeyboardInputMapperTest : public InputMapperTest {
  1252. protected:
  1253. void testDPadKeyRotation(KeyboardInputMapper* mapper,
  1254. int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
  1255. };
  1256. void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
  1257. int32_t originalScanCode, int32_t, int32_t rotatedKeyCode) {
  1258. NotifyKeyArgs args;
  1259. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, 1);
  1260. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1261. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
  1262. ASSERT_EQ(originalScanCode, args.scanCode);
  1263. ASSERT_EQ(rotatedKeyCode, args.keyCode);
  1264. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, 0);
  1265. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1266. ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
  1267. ASSERT_EQ(originalScanCode, args.scanCode);
  1268. ASSERT_EQ(rotatedKeyCode, args.keyCode);
  1269. }
  1270. TEST_F(KeyboardInputMapperTest, GetSources) {
  1271. KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
  1272. AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
  1273. addMapperAndConfigure(mapper);
  1274. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
  1275. }
  1276. TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
  1277. const int32_t USAGE_A = 0x070004;
  1278. const int32_t USAGE_UNKNOWN = 0x07ffff;
  1279. mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
  1280. mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
  1281. KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
  1282. AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
  1283. addMapperAndConfigure(mapper);
  1284. // Key down by scan code.
  1285. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1286. EV_KEY, KEY_HOME, 1);
  1287. NotifyKeyArgs args;
  1288. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1289. ASSERT_EQ(DEVICE_ID, args.deviceId);
  1290. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
  1291. ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
  1292. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
  1293. ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
  1294. ASSERT_EQ(KEY_HOME, args.scanCode);
  1295. ASSERT_EQ(AMETA_NONE, args.metaState);
  1296. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
  1297. ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
  1298. ASSERT_EQ(ARBITRARY_TIME, args.downTime);
  1299. // Key up by scan code.
  1300. process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
  1301. EV_KEY, KEY_HOME, 0);
  1302. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1303. ASSERT_EQ(DEVICE_ID, args.deviceId);
  1304. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
  1305. ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
  1306. ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
  1307. ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
  1308. ASSERT_EQ(KEY_HOME, args.scanCode);
  1309. ASSERT_EQ(AMETA_NONE, args.metaState);
  1310. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
  1311. ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
  1312. ASSERT_EQ(ARBITRARY_TIME, args.downTime);
  1313. // Key down by usage code.
  1314. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1315. EV_MSC, MSC_SCAN, USAGE_A);
  1316. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1317. EV_KEY, 0, 1);
  1318. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1319. ASSERT_EQ(DEVICE_ID, args.deviceId);
  1320. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
  1321. ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
  1322. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
  1323. ASSERT_EQ(AKEYCODE_A, args.keyCode);
  1324. ASSERT_EQ(0, args.scanCode);
  1325. ASSERT_EQ(AMETA_NONE, args.metaState);
  1326. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
  1327. ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
  1328. ASSERT_EQ(ARBITRARY_TIME, args.downTime);
  1329. // Key up by usage code.
  1330. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1331. EV_MSC, MSC_SCAN, USAGE_A);
  1332. process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
  1333. EV_KEY, 0, 0);
  1334. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1335. ASSERT_EQ(DEVICE_ID, args.deviceId);
  1336. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
  1337. ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
  1338. ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
  1339. ASSERT_EQ(AKEYCODE_A, args.keyCode);
  1340. ASSERT_EQ(0, args.scanCode);
  1341. ASSERT_EQ(AMETA_NONE, args.metaState);
  1342. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
  1343. ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
  1344. ASSERT_EQ(ARBITRARY_TIME, args.downTime);
  1345. // Key down with unknown scan code or usage code.
  1346. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1347. EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
  1348. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1349. EV_KEY, KEY_UNKNOWN, 1);
  1350. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1351. ASSERT_EQ(DEVICE_ID, args.deviceId);
  1352. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
  1353. ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
  1354. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
  1355. ASSERT_EQ(0, args.keyCode);
  1356. ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
  1357. ASSERT_EQ(AMETA_NONE, args.metaState);
  1358. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
  1359. ASSERT_EQ(0U, args.policyFlags);
  1360. ASSERT_EQ(ARBITRARY_TIME, args.downTime);
  1361. // Key up with unknown scan code or usage code.
  1362. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1363. EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
  1364. process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
  1365. EV_KEY, KEY_UNKNOWN, 0);
  1366. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1367. ASSERT_EQ(DEVICE_ID, args.deviceId);
  1368. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
  1369. ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
  1370. ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
  1371. ASSERT_EQ(0, args.keyCode);
  1372. ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
  1373. ASSERT_EQ(AMETA_NONE, args.metaState);
  1374. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
  1375. ASSERT_EQ(0U, args.policyFlags);
  1376. ASSERT_EQ(ARBITRARY_TIME, args.downTime);
  1377. }
  1378. TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
  1379. mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
  1380. mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
  1381. KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
  1382. AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
  1383. addMapperAndConfigure(mapper);
  1384. // Initial metastate.
  1385. ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
  1386. // Metakey down.
  1387. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1388. EV_KEY, KEY_LEFTSHIFT, 1);
  1389. NotifyKeyArgs args;
  1390. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1391. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
  1392. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
  1393. ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
  1394. // Key down.
  1395. process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
  1396. EV_KEY, KEY_A, 1);
  1397. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1398. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
  1399. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
  1400. // Key up.
  1401. process(mapper, ARBITRARY_TIME + 2, DEVICE_ID,
  1402. EV_KEY, KEY_A, 0);
  1403. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1404. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
  1405. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
  1406. // Metakey up.
  1407. process(mapper, ARBITRARY_TIME + 3, DEVICE_ID,
  1408. EV_KEY, KEY_LEFTSHIFT, 0);
  1409. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1410. ASSERT_EQ(AMETA_NONE, args.metaState);
  1411. ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
  1412. ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
  1413. }
  1414. TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
  1415. mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
  1416. mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
  1417. mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
  1418. mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
  1419. KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
  1420. AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
  1421. addMapperAndConfigure(mapper);
  1422. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1423. DISPLAY_WIDTH, DISPLAY_HEIGHT,
  1424. DISPLAY_ORIENTATION_90);
  1425. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1426. KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
  1427. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1428. KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
  1429. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1430. KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
  1431. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1432. KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
  1433. }
  1434. TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
  1435. mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
  1436. mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
  1437. mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
  1438. mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
  1439. KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
  1440. AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
  1441. addConfigurationProperty("keyboard.orientationAware", "1");
  1442. addMapperAndConfigure(mapper);
  1443. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1444. DISPLAY_WIDTH, DISPLAY_HEIGHT,
  1445. DISPLAY_ORIENTATION_0);
  1446. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1447. KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
  1448. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1449. KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
  1450. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1451. KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
  1452. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1453. KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
  1454. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1455. DISPLAY_WIDTH, DISPLAY_HEIGHT,
  1456. DISPLAY_ORIENTATION_90);
  1457. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1458. KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
  1459. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1460. KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
  1461. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1462. KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
  1463. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1464. KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
  1465. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1466. DISPLAY_WIDTH, DISPLAY_HEIGHT,
  1467. DISPLAY_ORIENTATION_180);
  1468. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1469. KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
  1470. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1471. KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
  1472. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1473. KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
  1474. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1475. KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
  1476. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1477. DISPLAY_WIDTH, DISPLAY_HEIGHT,
  1478. DISPLAY_ORIENTATION_270);
  1479. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1480. KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
  1481. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1482. KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
  1483. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1484. KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
  1485. ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
  1486. KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
  1487. // Special case: if orientation changes while key is down, we still emit the same keycode
  1488. // in the key up as we did in the key down.
  1489. NotifyKeyArgs args;
  1490. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1491. DISPLAY_WIDTH, DISPLAY_HEIGHT,
  1492. DISPLAY_ORIENTATION_270);
  1493. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, 1);
  1494. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1495. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
  1496. ASSERT_EQ(KEY_UP, args.scanCode);
  1497. ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
  1498. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1499. DISPLAY_WIDTH, DISPLAY_HEIGHT,
  1500. DISPLAY_ORIENTATION_180);
  1501. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, 0);
  1502. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  1503. ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
  1504. ASSERT_EQ(KEY_UP, args.scanCode);
  1505. ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
  1506. }
  1507. TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
  1508. KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
  1509. AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
  1510. addMapperAndConfigure(mapper);
  1511. mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
  1512. ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
  1513. mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
  1514. ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
  1515. }
  1516. TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
  1517. KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
  1518. AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
  1519. addMapperAndConfigure(mapper);
  1520. mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
  1521. ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
  1522. mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
  1523. ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
  1524. }
  1525. TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
  1526. KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
  1527. AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
  1528. addMapperAndConfigure(mapper);
  1529. mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
  1530. const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
  1531. uint8_t flags[2] = { 0, 0 };
  1532. ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
  1533. ASSERT_TRUE(flags[0]);
  1534. ASSERT_FALSE(flags[1]);
  1535. }
  1536. TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
  1537. mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
  1538. mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
  1539. mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
  1540. mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
  1541. mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
  1542. mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
  1543. KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
  1544. AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
  1545. addMapperAndConfigure(mapper);
  1546. // Initialization should have turned all of the lights off.
  1547. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
  1548. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
  1549. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
  1550. // Toggle caps lock on.
  1551. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1552. EV_KEY, KEY_CAPSLOCK, 1);
  1553. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1554. EV_KEY, KEY_CAPSLOCK, 0);
  1555. ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
  1556. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
  1557. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
  1558. ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
  1559. // Toggle num lock on.
  1560. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1561. EV_KEY, KEY_NUMLOCK, 1);
  1562. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1563. EV_KEY, KEY_NUMLOCK, 0);
  1564. ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
  1565. ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
  1566. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
  1567. ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
  1568. // Toggle caps lock off.
  1569. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1570. EV_KEY, KEY_CAPSLOCK, 1);
  1571. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1572. EV_KEY, KEY_CAPSLOCK, 0);
  1573. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
  1574. ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
  1575. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
  1576. ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
  1577. // Toggle scroll lock on.
  1578. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1579. EV_KEY, KEY_SCROLLLOCK, 1);
  1580. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1581. EV_KEY, KEY_SCROLLLOCK, 0);
  1582. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
  1583. ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
  1584. ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
  1585. ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
  1586. // Toggle num lock off.
  1587. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1588. EV_KEY, KEY_NUMLOCK, 1);
  1589. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1590. EV_KEY, KEY_NUMLOCK, 0);
  1591. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
  1592. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
  1593. ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
  1594. ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
  1595. // Toggle scroll lock off.
  1596. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1597. EV_KEY, KEY_SCROLLLOCK, 1);
  1598. process(mapper, ARBITRARY_TIME, DEVICE_ID,
  1599. EV_KEY, KEY_SCROLLLOCK, 0);
  1600. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
  1601. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
  1602. ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
  1603. ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
  1604. }
  1605. // --- CursorInputMapperTest ---
  1606. class CursorInputMapperTest : public InputMapperTest {
  1607. protected:
  1608. static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
  1609. sp<FakePointerController> mFakePointerController;
  1610. virtual void SetUp() {
  1611. InputMapperTest::SetUp();
  1612. mFakePointerController = new FakePointerController();
  1613. mFakePolicy->setPointerController(DEVICE_ID, mFakePointerController);
  1614. }
  1615. void testMotionRotation(CursorInputMapper* mapper,
  1616. int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
  1617. };
  1618. const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
  1619. void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
  1620. int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
  1621. NotifyMotionArgs args;
  1622. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, originalX);
  1623. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, originalY);
  1624. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1625. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  1626. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
  1627. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  1628. float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
  1629. float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
  1630. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1631. }
  1632. TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
  1633. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1634. addConfigurationProperty("cursor.mode", "pointer");
  1635. addMapperAndConfigure(mapper);
  1636. ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
  1637. }
  1638. TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
  1639. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1640. addConfigurationProperty("cursor.mode", "navigation");
  1641. addMapperAndConfigure(mapper);
  1642. ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
  1643. }
  1644. TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
  1645. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1646. addConfigurationProperty("cursor.mode", "pointer");
  1647. addMapperAndConfigure(mapper);
  1648. InputDeviceInfo info;
  1649. mapper->populateDeviceInfo(&info);
  1650. // Initially there may not be a valid motion range.
  1651. ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
  1652. ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
  1653. ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
  1654. AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
  1655. // When the bounds are set, then there should be a valid motion range.
  1656. mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
  1657. InputDeviceInfo info2;
  1658. mapper->populateDeviceInfo(&info2);
  1659. ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
  1660. AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
  1661. 1, 800 - 1, 0.0f, 0.0f));
  1662. ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
  1663. AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
  1664. 2, 480 - 1, 0.0f, 0.0f));
  1665. ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
  1666. AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
  1667. 0.0f, 1.0f, 0.0f, 0.0f));
  1668. }
  1669. TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
  1670. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1671. addConfigurationProperty("cursor.mode", "navigation");
  1672. addMapperAndConfigure(mapper);
  1673. InputDeviceInfo info;
  1674. mapper->populateDeviceInfo(&info);
  1675. ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
  1676. AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
  1677. -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
  1678. ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
  1679. AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
  1680. -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
  1681. ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
  1682. AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
  1683. 0.0f, 1.0f, 0.0f, 0.0f));
  1684. }
  1685. TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
  1686. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1687. addConfigurationProperty("cursor.mode", "navigation");
  1688. addMapperAndConfigure(mapper);
  1689. mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
  1690. NotifyMotionArgs args;
  1691. // Button press.
  1692. // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
  1693. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1);
  1694. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1695. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  1696. ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
  1697. ASSERT_EQ(DEVICE_ID, args.deviceId);
  1698. ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
  1699. ASSERT_EQ(uint32_t(0), args.policyFlags);
  1700. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
  1701. ASSERT_EQ(0, args.flags);
  1702. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
  1703. ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
  1704. ASSERT_EQ(0, args.edgeFlags);
  1705. ASSERT_EQ(uint32_t(1), args.pointerCount);
  1706. ASSERT_EQ(0, args.pointerProperties[0].id);
  1707. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
  1708. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  1709. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1710. ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
  1711. ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
  1712. ASSERT_EQ(ARBITRARY_TIME, args.downTime);
  1713. // Button release. Should have same down time.
  1714. process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_KEY, BTN_MOUSE, 0);
  1715. process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1716. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  1717. ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
  1718. ASSERT_EQ(DEVICE_ID, args.deviceId);
  1719. ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
  1720. ASSERT_EQ(uint32_t(0), args.policyFlags);
  1721. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
  1722. ASSERT_EQ(0, args.flags);
  1723. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
  1724. ASSERT_EQ(0, args.buttonState);
  1725. ASSERT_EQ(0, args.edgeFlags);
  1726. ASSERT_EQ(uint32_t(1), args.pointerCount);
  1727. ASSERT_EQ(0, args.pointerProperties[0].id);
  1728. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
  1729. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  1730. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1731. ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
  1732. ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
  1733. ASSERT_EQ(ARBITRARY_TIME, args.downTime);
  1734. }
  1735. TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
  1736. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1737. addConfigurationProperty("cursor.mode", "navigation");
  1738. addMapperAndConfigure(mapper);
  1739. NotifyMotionArgs args;
  1740. // Motion in X but not Y.
  1741. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 1);
  1742. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1743. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  1744. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
  1745. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  1746. 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1747. // Motion in Y but not X.
  1748. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, -2);
  1749. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1750. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  1751. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
  1752. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  1753. 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1754. }
  1755. TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
  1756. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1757. addConfigurationProperty("cursor.mode", "navigation");
  1758. addMapperAndConfigure(mapper);
  1759. NotifyMotionArgs args;
  1760. // Button press.
  1761. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1);
  1762. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1763. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  1764. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
  1765. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  1766. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1767. // Button release.
  1768. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0);
  1769. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1770. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  1771. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
  1772. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  1773. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1774. }
  1775. TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
  1776. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1777. addConfigurationProperty("cursor.mode", "navigation");
  1778. addMapperAndConfigure(mapper);
  1779. NotifyMotionArgs args;
  1780. // Combined X, Y and Button.
  1781. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 1);
  1782. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, -2);
  1783. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1);
  1784. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1785. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  1786. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
  1787. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  1788. 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
  1789. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1790. // Move X, Y a bit while pressed.
  1791. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 2);
  1792. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 1);
  1793. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1794. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  1795. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
  1796. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  1797. 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
  1798. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1799. // Release Button.
  1800. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0);
  1801. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1802. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  1803. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
  1804. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  1805. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1806. }
  1807. TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
  1808. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1809. addConfigurationProperty("cursor.mode", "navigation");
  1810. addMapperAndConfigure(mapper);
  1811. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1812. DISPLAY_WIDTH, DISPLAY_HEIGHT,
  1813. DISPLAY_ORIENTATION_90);
  1814. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
  1815. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
  1816. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
  1817. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
  1818. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
  1819. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
  1820. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
  1821. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
  1822. }
  1823. TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
  1824. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1825. addConfigurationProperty("cursor.mode", "navigation");
  1826. addConfigurationProperty("cursor.orientationAware", "1");
  1827. addMapperAndConfigure(mapper);
  1828. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1829. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0);
  1830. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
  1831. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
  1832. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
  1833. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
  1834. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
  1835. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
  1836. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
  1837. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
  1838. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1839. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_90);
  1840. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
  1841. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
  1842. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
  1843. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
  1844. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
  1845. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
  1846. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
  1847. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
  1848. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1849. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_180);
  1850. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
  1851. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
  1852. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
  1853. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
  1854. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
  1855. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
  1856. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
  1857. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
  1858. setDisplayInfoAndReconfigure(DISPLAY_ID,
  1859. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_270);
  1860. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
  1861. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
  1862. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
  1863. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
  1864. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
  1865. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
  1866. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
  1867. ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
  1868. }
  1869. TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
  1870. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  1871. addConfigurationProperty("cursor.mode", "pointer");
  1872. addMapperAndConfigure(mapper);
  1873. mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
  1874. mFakePointerController->setPosition(100, 200);
  1875. mFakePointerController->setButtonState(0);
  1876. NotifyMotionArgs motionArgs;
  1877. NotifyKeyArgs keyArgs;
  1878. // press BTN_LEFT, release BTN_LEFT
  1879. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_LEFT, 1);
  1880. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1881. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1882. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  1883. ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
  1884. ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
  1885. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1886. 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1887. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_LEFT, 0);
  1888. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1889. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1890. ASSERT_EQ(0, motionArgs.buttonState);
  1891. ASSERT_EQ(0, mFakePointerController->getButtonState());
  1892. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  1893. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1894. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1895. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1896. ASSERT_EQ(0, motionArgs.buttonState);
  1897. ASSERT_EQ(0, mFakePointerController->getButtonState());
  1898. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  1899. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1900. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1901. // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
  1902. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_RIGHT, 1);
  1903. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MIDDLE, 1);
  1904. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1905. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1906. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  1907. ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
  1908. motionArgs.buttonState);
  1909. ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
  1910. mFakePointerController->getButtonState());
  1911. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1912. 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1913. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_RIGHT, 0);
  1914. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1915. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1916. ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
  1917. ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
  1918. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  1919. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1920. 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1921. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MIDDLE, 0);
  1922. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1923. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1924. ASSERT_EQ(0, motionArgs.buttonState);
  1925. ASSERT_EQ(0, mFakePointerController->getButtonState());
  1926. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  1927. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1928. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1929. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1930. ASSERT_EQ(0, motionArgs.buttonState);
  1931. ASSERT_EQ(0, mFakePointerController->getButtonState());
  1932. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  1933. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1934. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1935. // press BTN_BACK, release BTN_BACK
  1936. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_BACK, 1);
  1937. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1938. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  1939. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  1940. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  1941. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1942. ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
  1943. ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
  1944. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  1945. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1946. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1947. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_BACK, 0);
  1948. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1949. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1950. ASSERT_EQ(0, motionArgs.buttonState);
  1951. ASSERT_EQ(0, mFakePointerController->getButtonState());
  1952. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  1953. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1954. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1955. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  1956. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  1957. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  1958. // press BTN_SIDE, release BTN_SIDE
  1959. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_SIDE, 1);
  1960. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1961. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  1962. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  1963. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  1964. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1965. ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
  1966. ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
  1967. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  1968. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1969. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1970. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_SIDE, 0);
  1971. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1972. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1973. ASSERT_EQ(0, motionArgs.buttonState);
  1974. ASSERT_EQ(0, mFakePointerController->getButtonState());
  1975. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  1976. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1977. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1978. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  1979. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  1980. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  1981. // press BTN_FORWARD, release BTN_FORWARD
  1982. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_FORWARD, 1);
  1983. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1984. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  1985. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  1986. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  1987. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1988. ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
  1989. ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
  1990. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  1991. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  1992. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  1993. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_FORWARD, 0);
  1994. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  1995. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  1996. ASSERT_EQ(0, motionArgs.buttonState);
  1997. ASSERT_EQ(0, mFakePointerController->getButtonState());
  1998. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  1999. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2000. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  2001. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2002. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  2003. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  2004. // press BTN_EXTRA, release BTN_EXTRA
  2005. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_EXTRA, 1);
  2006. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  2007. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2008. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  2009. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  2010. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2011. ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
  2012. ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
  2013. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  2014. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2015. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  2016. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_EXTRA, 0);
  2017. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  2018. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2019. ASSERT_EQ(0, motionArgs.buttonState);
  2020. ASSERT_EQ(0, mFakePointerController->getButtonState());
  2021. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  2022. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2023. 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  2024. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2025. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  2026. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  2027. }
  2028. TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
  2029. CursorInputMapper* mapper = new CursorInputMapper(mDevice);
  2030. addConfigurationProperty("cursor.mode", "pointer");
  2031. addMapperAndConfigure(mapper);
  2032. mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
  2033. mFakePointerController->setPosition(100, 200);
  2034. mFakePointerController->setButtonState(0);
  2035. NotifyMotionArgs args;
  2036. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 10);
  2037. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 20);
  2038. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  2039. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  2040. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
  2041. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  2042. 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
  2043. ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
  2044. }
  2045. // --- TouchInputMapperTest ---
  2046. class TouchInputMapperTest : public InputMapperTest {
  2047. protected:
  2048. static const int32_t RAW_X_MIN;
  2049. static const int32_t RAW_X_MAX;
  2050. static const int32_t RAW_Y_MIN;
  2051. static const int32_t RAW_Y_MAX;
  2052. static const int32_t RAW_TOUCH_MIN;
  2053. static const int32_t RAW_TOUCH_MAX;
  2054. static const int32_t RAW_TOOL_MIN;
  2055. static const int32_t RAW_TOOL_MAX;
  2056. static const int32_t RAW_PRESSURE_MIN;
  2057. static const int32_t RAW_PRESSURE_MAX;
  2058. static const int32_t RAW_ORIENTATION_MIN;
  2059. static const int32_t RAW_ORIENTATION_MAX;
  2060. static const int32_t RAW_DISTANCE_MIN;
  2061. static const int32_t RAW_DISTANCE_MAX;
  2062. static const int32_t RAW_TILT_MIN;
  2063. static const int32_t RAW_TILT_MAX;
  2064. static const int32_t RAW_ID_MIN;
  2065. static const int32_t RAW_ID_MAX;
  2066. static const int32_t RAW_SLOT_MIN;
  2067. static const int32_t RAW_SLOT_MAX;
  2068. static const float X_PRECISION;
  2069. static const float Y_PRECISION;
  2070. static const float GEOMETRIC_SCALE;
  2071. static const TouchAffineTransformation AFFINE_TRANSFORM;
  2072. static const VirtualKeyDefinition VIRTUAL_KEYS[2];
  2073. enum Axes {
  2074. POSITION = 1 << 0,
  2075. TOUCH = 1 << 1,
  2076. TOOL = 1 << 2,
  2077. PRESSURE = 1 << 3,
  2078. ORIENTATION = 1 << 4,
  2079. MINOR = 1 << 5,
  2080. ID = 1 << 6,
  2081. DISTANCE = 1 << 7,
  2082. TILT = 1 << 8,
  2083. SLOT = 1 << 9,
  2084. TOOL_TYPE = 1 << 10,
  2085. };
  2086. void prepareDisplay(int32_t orientation);
  2087. void prepareVirtualKeys();
  2088. void prepareLocationCalibration();
  2089. int32_t toRawX(float displayX);
  2090. int32_t toRawY(float displayY);
  2091. float toCookedX(float rawX, float rawY);
  2092. float toCookedY(float rawX, float rawY);
  2093. float toDisplayX(int32_t rawX);
  2094. float toDisplayY(int32_t rawY);
  2095. };
  2096. const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
  2097. const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
  2098. const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
  2099. const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
  2100. const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
  2101. const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
  2102. const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
  2103. const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
  2104. const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = RAW_TOUCH_MIN;
  2105. const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = RAW_TOUCH_MAX;
  2106. const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
  2107. const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
  2108. const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
  2109. const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
  2110. const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
  2111. const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
  2112. const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
  2113. const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
  2114. const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
  2115. const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
  2116. const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
  2117. const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
  2118. const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
  2119. TouchAffineTransformation(1, -2, 3, -4, 5, -6);
  2120. const float TouchInputMapperTest::GEOMETRIC_SCALE =
  2121. avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
  2122. float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
  2123. const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
  2124. { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
  2125. { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
  2126. };
  2127. void TouchInputMapperTest::prepareDisplay(int32_t orientation) {
  2128. setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation);
  2129. }
  2130. void TouchInputMapperTest::prepareVirtualKeys() {
  2131. mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
  2132. mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
  2133. mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
  2134. mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
  2135. }
  2136. void TouchInputMapperTest::prepareLocationCalibration() {
  2137. mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
  2138. }
  2139. int32_t TouchInputMapperTest::toRawX(float displayX) {
  2140. return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
  2141. }
  2142. int32_t TouchInputMapperTest::toRawY(float displayY) {
  2143. return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
  2144. }
  2145. float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
  2146. AFFINE_TRANSFORM.applyTo(rawX, rawY);
  2147. return rawX;
  2148. }
  2149. float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
  2150. AFFINE_TRANSFORM.applyTo(rawX, rawY);
  2151. return rawY;
  2152. }
  2153. float TouchInputMapperTest::toDisplayX(int32_t rawX) {
  2154. return float(rawX - RAW_X_MIN) * DISPLAY_WIDTH / (RAW_X_MAX - RAW_X_MIN + 1);
  2155. }
  2156. float TouchInputMapperTest::toDisplayY(int32_t rawY) {
  2157. return float(rawY - RAW_Y_MIN) * DISPLAY_HEIGHT / (RAW_Y_MAX - RAW_Y_MIN + 1);
  2158. }
  2159. // --- SingleTouchInputMapperTest ---
  2160. class SingleTouchInputMapperTest : public TouchInputMapperTest {
  2161. protected:
  2162. void prepareButtons();
  2163. void prepareAxes(int axes);
  2164. void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
  2165. void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
  2166. void processUp(SingleTouchInputMapper* mappery);
  2167. void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
  2168. void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
  2169. void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
  2170. void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
  2171. void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
  2172. void processSync(SingleTouchInputMapper* mapper);
  2173. };
  2174. void SingleTouchInputMapperTest::prepareButtons() {
  2175. mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
  2176. }
  2177. void SingleTouchInputMapperTest::prepareAxes(int axes) {
  2178. if (axes & POSITION) {
  2179. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
  2180. RAW_X_MIN, RAW_X_MAX, 0, 0);
  2181. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
  2182. RAW_Y_MIN, RAW_Y_MAX, 0, 0);
  2183. }
  2184. if (axes & PRESSURE) {
  2185. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
  2186. RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
  2187. }
  2188. if (axes & TOOL) {
  2189. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
  2190. RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
  2191. }
  2192. if (axes & DISTANCE) {
  2193. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
  2194. RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
  2195. }
  2196. if (axes & TILT) {
  2197. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
  2198. RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
  2199. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
  2200. RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
  2201. }
  2202. }
  2203. void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
  2204. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 1);
  2205. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, x);
  2206. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, y);
  2207. }
  2208. void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
  2209. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, x);
  2210. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, y);
  2211. }
  2212. void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
  2213. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0);
  2214. }
  2215. void SingleTouchInputMapperTest::processPressure(
  2216. SingleTouchInputMapper* mapper, int32_t pressure) {
  2217. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_PRESSURE, pressure);
  2218. }
  2219. void SingleTouchInputMapperTest::processToolMajor(
  2220. SingleTouchInputMapper* mapper, int32_t toolMajor) {
  2221. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
  2222. }
  2223. void SingleTouchInputMapperTest::processDistance(
  2224. SingleTouchInputMapper* mapper, int32_t distance) {
  2225. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_DISTANCE, distance);
  2226. }
  2227. void SingleTouchInputMapperTest::processTilt(
  2228. SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
  2229. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TILT_X, tiltX);
  2230. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TILT_Y, tiltY);
  2231. }
  2232. void SingleTouchInputMapperTest::processKey(
  2233. SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
  2234. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, value);
  2235. }
  2236. void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
  2237. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  2238. }
  2239. TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
  2240. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2241. prepareButtons();
  2242. prepareAxes(POSITION);
  2243. addMapperAndConfigure(mapper);
  2244. ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
  2245. }
  2246. TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
  2247. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2248. mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
  2249. mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
  2250. prepareButtons();
  2251. prepareAxes(POSITION);
  2252. addMapperAndConfigure(mapper);
  2253. ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
  2254. }
  2255. TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
  2256. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2257. prepareButtons();
  2258. prepareAxes(POSITION);
  2259. addConfigurationProperty("touch.deviceType", "touchPad");
  2260. addMapperAndConfigure(mapper);
  2261. ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
  2262. }
  2263. TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
  2264. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2265. prepareButtons();
  2266. prepareAxes(POSITION);
  2267. addConfigurationProperty("touch.deviceType", "touchScreen");
  2268. addMapperAndConfigure(mapper);
  2269. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
  2270. }
  2271. TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
  2272. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2273. addConfigurationProperty("touch.deviceType", "touchScreen");
  2274. prepareDisplay(DISPLAY_ORIENTATION_0);
  2275. prepareButtons();
  2276. prepareAxes(POSITION);
  2277. prepareVirtualKeys();
  2278. addMapperAndConfigure(mapper);
  2279. // Unknown key.
  2280. ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
  2281. // Virtual key is down.
  2282. int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
  2283. int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
  2284. processDown(mapper, x, y);
  2285. processSync(mapper);
  2286. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
  2287. ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
  2288. // Virtual key is up.
  2289. processUp(mapper);
  2290. processSync(mapper);
  2291. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
  2292. ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
  2293. }
  2294. TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
  2295. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2296. addConfigurationProperty("touch.deviceType", "touchScreen");
  2297. prepareDisplay(DISPLAY_ORIENTATION_0);
  2298. prepareButtons();
  2299. prepareAxes(POSITION);
  2300. prepareVirtualKeys();
  2301. addMapperAndConfigure(mapper);
  2302. // Unknown key.
  2303. ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
  2304. // Virtual key is down.
  2305. int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
  2306. int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
  2307. processDown(mapper, x, y);
  2308. processSync(mapper);
  2309. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
  2310. ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
  2311. // Virtual key is up.
  2312. processUp(mapper);
  2313. processSync(mapper);
  2314. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
  2315. ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
  2316. }
  2317. TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
  2318. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2319. addConfigurationProperty("touch.deviceType", "touchScreen");
  2320. prepareDisplay(DISPLAY_ORIENTATION_0);
  2321. prepareButtons();
  2322. prepareAxes(POSITION);
  2323. prepareVirtualKeys();
  2324. addMapperAndConfigure(mapper);
  2325. const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
  2326. uint8_t flags[2] = { 0, 0 };
  2327. ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
  2328. ASSERT_TRUE(flags[0]);
  2329. ASSERT_FALSE(flags[1]);
  2330. }
  2331. TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
  2332. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2333. addConfigurationProperty("touch.deviceType", "touchScreen");
  2334. prepareDisplay(DISPLAY_ORIENTATION_0);
  2335. prepareButtons();
  2336. prepareAxes(POSITION);
  2337. prepareVirtualKeys();
  2338. addMapperAndConfigure(mapper);
  2339. mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
  2340. NotifyKeyArgs args;
  2341. // Press virtual key.
  2342. int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
  2343. int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
  2344. processDown(mapper, x, y);
  2345. processSync(mapper);
  2346. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  2347. ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
  2348. ASSERT_EQ(DEVICE_ID, args.deviceId);
  2349. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
  2350. ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
  2351. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
  2352. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
  2353. ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
  2354. ASSERT_EQ(KEY_HOME, args.scanCode);
  2355. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
  2356. ASSERT_EQ(ARBITRARY_TIME, args.downTime);
  2357. // Release virtual key.
  2358. processUp(mapper);
  2359. processSync(mapper);
  2360. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
  2361. ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
  2362. ASSERT_EQ(DEVICE_ID, args.deviceId);
  2363. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
  2364. ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
  2365. ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
  2366. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
  2367. ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
  2368. ASSERT_EQ(KEY_HOME, args.scanCode);
  2369. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
  2370. ASSERT_EQ(ARBITRARY_TIME, args.downTime);
  2371. // Should not have sent any motions.
  2372. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
  2373. }
  2374. TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
  2375. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2376. addConfigurationProperty("touch.deviceType", "touchScreen");
  2377. prepareDisplay(DISPLAY_ORIENTATION_0);
  2378. prepareButtons();
  2379. prepareAxes(POSITION);
  2380. prepareVirtualKeys();
  2381. addMapperAndConfigure(mapper);
  2382. mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
  2383. NotifyKeyArgs keyArgs;
  2384. // Press virtual key.
  2385. int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
  2386. int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
  2387. processDown(mapper, x, y);
  2388. processSync(mapper);
  2389. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2390. ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
  2391. ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
  2392. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
  2393. ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
  2394. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  2395. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
  2396. ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
  2397. ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
  2398. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
  2399. ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
  2400. // Move out of bounds. This should generate a cancel and a pointer down since we moved
  2401. // into the display area.
  2402. y -= 100;
  2403. processMove(mapper, x, y);
  2404. processSync(mapper);
  2405. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2406. ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
  2407. ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
  2408. ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
  2409. ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
  2410. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  2411. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
  2412. | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
  2413. ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
  2414. ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
  2415. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
  2416. ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
  2417. NotifyMotionArgs motionArgs;
  2418. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2419. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  2420. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  2421. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  2422. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  2423. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  2424. ASSERT_EQ(0, motionArgs.flags);
  2425. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  2426. ASSERT_EQ(0, motionArgs.buttonState);
  2427. ASSERT_EQ(0, motionArgs.edgeFlags);
  2428. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  2429. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  2430. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2431. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2432. toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
  2433. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  2434. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  2435. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  2436. // Keep moving out of bounds. Should generate a pointer move.
  2437. y -= 50;
  2438. processMove(mapper, x, y);
  2439. processSync(mapper);
  2440. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2441. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  2442. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  2443. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  2444. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  2445. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2446. ASSERT_EQ(0, motionArgs.flags);
  2447. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  2448. ASSERT_EQ(0, motionArgs.buttonState);
  2449. ASSERT_EQ(0, motionArgs.edgeFlags);
  2450. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  2451. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  2452. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2453. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2454. toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
  2455. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  2456. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  2457. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  2458. // Release out of bounds. Should generate a pointer up.
  2459. processUp(mapper);
  2460. processSync(mapper);
  2461. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2462. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  2463. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  2464. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  2465. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  2466. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  2467. ASSERT_EQ(0, motionArgs.flags);
  2468. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  2469. ASSERT_EQ(0, motionArgs.buttonState);
  2470. ASSERT_EQ(0, motionArgs.edgeFlags);
  2471. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  2472. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  2473. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2474. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2475. toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
  2476. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  2477. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  2478. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  2479. // Should not have sent any more keys or motions.
  2480. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
  2481. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
  2482. }
  2483. TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
  2484. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2485. addConfigurationProperty("touch.deviceType", "touchScreen");
  2486. prepareDisplay(DISPLAY_ORIENTATION_0);
  2487. prepareButtons();
  2488. prepareAxes(POSITION);
  2489. prepareVirtualKeys();
  2490. addMapperAndConfigure(mapper);
  2491. mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
  2492. NotifyMotionArgs motionArgs;
  2493. // Initially go down out of bounds.
  2494. int32_t x = -10;
  2495. int32_t y = -10;
  2496. processDown(mapper, x, y);
  2497. processSync(mapper);
  2498. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
  2499. // Move into the display area. Should generate a pointer down.
  2500. x = 50;
  2501. y = 75;
  2502. processMove(mapper, x, y);
  2503. processSync(mapper);
  2504. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2505. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  2506. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  2507. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  2508. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  2509. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  2510. ASSERT_EQ(0, motionArgs.flags);
  2511. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  2512. ASSERT_EQ(0, motionArgs.buttonState);
  2513. ASSERT_EQ(0, motionArgs.edgeFlags);
  2514. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  2515. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  2516. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2517. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2518. toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
  2519. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  2520. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  2521. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  2522. // Release. Should generate a pointer up.
  2523. processUp(mapper);
  2524. processSync(mapper);
  2525. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2526. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  2527. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  2528. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  2529. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  2530. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  2531. ASSERT_EQ(0, motionArgs.flags);
  2532. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  2533. ASSERT_EQ(0, motionArgs.buttonState);
  2534. ASSERT_EQ(0, motionArgs.edgeFlags);
  2535. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  2536. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  2537. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2538. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2539. toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
  2540. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  2541. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  2542. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  2543. // Should not have sent any more keys or motions.
  2544. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
  2545. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
  2546. }
  2547. TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
  2548. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2549. addConfigurationProperty("touch.deviceType", "touchScreen");
  2550. prepareDisplay(DISPLAY_ORIENTATION_0);
  2551. prepareButtons();
  2552. prepareAxes(POSITION);
  2553. prepareVirtualKeys();
  2554. addMapperAndConfigure(mapper);
  2555. mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
  2556. NotifyMotionArgs motionArgs;
  2557. // Down.
  2558. int32_t x = 100;
  2559. int32_t y = 125;
  2560. processDown(mapper, x, y);
  2561. processSync(mapper);
  2562. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2563. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  2564. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  2565. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  2566. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  2567. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  2568. ASSERT_EQ(0, motionArgs.flags);
  2569. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  2570. ASSERT_EQ(0, motionArgs.buttonState);
  2571. ASSERT_EQ(0, motionArgs.edgeFlags);
  2572. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  2573. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  2574. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2575. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2576. toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
  2577. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  2578. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  2579. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  2580. // Move.
  2581. x += 50;
  2582. y += 75;
  2583. processMove(mapper, x, y);
  2584. processSync(mapper);
  2585. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2586. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  2587. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  2588. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  2589. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  2590. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2591. ASSERT_EQ(0, motionArgs.flags);
  2592. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  2593. ASSERT_EQ(0, motionArgs.buttonState);
  2594. ASSERT_EQ(0, motionArgs.edgeFlags);
  2595. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  2596. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  2597. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2598. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2599. toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
  2600. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  2601. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  2602. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  2603. // Up.
  2604. processUp(mapper);
  2605. processSync(mapper);
  2606. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2607. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  2608. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  2609. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  2610. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  2611. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  2612. ASSERT_EQ(0, motionArgs.flags);
  2613. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  2614. ASSERT_EQ(0, motionArgs.buttonState);
  2615. ASSERT_EQ(0, motionArgs.edgeFlags);
  2616. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  2617. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  2618. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2619. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  2620. toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
  2621. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  2622. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  2623. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  2624. // Should not have sent any more keys or motions.
  2625. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
  2626. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
  2627. }
  2628. TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
  2629. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2630. addConfigurationProperty("touch.deviceType", "touchScreen");
  2631. prepareButtons();
  2632. prepareAxes(POSITION);
  2633. addConfigurationProperty("touch.orientationAware", "0");
  2634. addMapperAndConfigure(mapper);
  2635. NotifyMotionArgs args;
  2636. // Rotation 90.
  2637. prepareDisplay(DISPLAY_ORIENTATION_90);
  2638. processDown(mapper, toRawX(50), toRawY(75));
  2639. processSync(mapper);
  2640. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  2641. ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
  2642. ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
  2643. processUp(mapper);
  2644. processSync(mapper);
  2645. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
  2646. }
  2647. TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
  2648. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2649. addConfigurationProperty("touch.deviceType", "touchScreen");
  2650. prepareButtons();
  2651. prepareAxes(POSITION);
  2652. addMapperAndConfigure(mapper);
  2653. NotifyMotionArgs args;
  2654. // Rotation 0.
  2655. prepareDisplay(DISPLAY_ORIENTATION_0);
  2656. processDown(mapper, toRawX(50), toRawY(75));
  2657. processSync(mapper);
  2658. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  2659. ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
  2660. ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
  2661. processUp(mapper);
  2662. processSync(mapper);
  2663. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
  2664. // Rotation 90.
  2665. prepareDisplay(DISPLAY_ORIENTATION_90);
  2666. processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
  2667. processSync(mapper);
  2668. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  2669. ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
  2670. ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
  2671. processUp(mapper);
  2672. processSync(mapper);
  2673. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
  2674. // Rotation 180.
  2675. prepareDisplay(DISPLAY_ORIENTATION_180);
  2676. processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
  2677. processSync(mapper);
  2678. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  2679. ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
  2680. ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
  2681. processUp(mapper);
  2682. processSync(mapper);
  2683. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
  2684. // Rotation 270.
  2685. prepareDisplay(DISPLAY_ORIENTATION_270);
  2686. processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
  2687. processSync(mapper);
  2688. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  2689. ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
  2690. ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
  2691. processUp(mapper);
  2692. processSync(mapper);
  2693. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
  2694. }
  2695. TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
  2696. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2697. addConfigurationProperty("touch.deviceType", "touchScreen");
  2698. prepareDisplay(DISPLAY_ORIENTATION_0);
  2699. prepareButtons();
  2700. prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
  2701. addMapperAndConfigure(mapper);
  2702. // These calculations are based on the input device calibration documentation.
  2703. int32_t rawX = 100;
  2704. int32_t rawY = 200;
  2705. int32_t rawPressure = 10;
  2706. int32_t rawToolMajor = 12;
  2707. int32_t rawDistance = 2;
  2708. int32_t rawTiltX = 30;
  2709. int32_t rawTiltY = 110;
  2710. float x = toDisplayX(rawX);
  2711. float y = toDisplayY(rawY);
  2712. float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
  2713. float size = float(rawToolMajor) / RAW_TOOL_MAX;
  2714. float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
  2715. float distance = float(rawDistance);
  2716. float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
  2717. float tiltScale = M_PI / 180;
  2718. float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
  2719. float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
  2720. float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
  2721. float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
  2722. processDown(mapper, rawX, rawY);
  2723. processPressure(mapper, rawPressure);
  2724. processToolMajor(mapper, rawToolMajor);
  2725. processDistance(mapper, rawDistance);
  2726. processTilt(mapper, rawTiltX, rawTiltY);
  2727. processSync(mapper);
  2728. NotifyMotionArgs args;
  2729. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  2730. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  2731. x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
  2732. ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
  2733. }
  2734. TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
  2735. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2736. addConfigurationProperty("touch.deviceType", "touchScreen");
  2737. prepareDisplay(DISPLAY_ORIENTATION_0);
  2738. prepareLocationCalibration();
  2739. prepareButtons();
  2740. prepareAxes(POSITION);
  2741. addMapperAndConfigure(mapper);
  2742. int32_t rawX = 100;
  2743. int32_t rawY = 200;
  2744. float x = toDisplayX(toCookedX(rawX, rawY));
  2745. float y = toDisplayY(toCookedY(rawX, rawY));
  2746. processDown(mapper, rawX, rawY);
  2747. processSync(mapper);
  2748. NotifyMotionArgs args;
  2749. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  2750. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  2751. x, y, 1, 0, 0, 0, 0, 0, 0, 0));
  2752. }
  2753. TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
  2754. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2755. addConfigurationProperty("touch.deviceType", "touchScreen");
  2756. prepareDisplay(DISPLAY_ORIENTATION_0);
  2757. prepareButtons();
  2758. prepareAxes(POSITION);
  2759. addMapperAndConfigure(mapper);
  2760. NotifyMotionArgs motionArgs;
  2761. NotifyKeyArgs keyArgs;
  2762. processDown(mapper, 100, 200);
  2763. processSync(mapper);
  2764. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2765. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  2766. ASSERT_EQ(0, motionArgs.buttonState);
  2767. // press BTN_LEFT, release BTN_LEFT
  2768. processKey(mapper, BTN_LEFT, 1);
  2769. processSync(mapper);
  2770. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2771. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2772. ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
  2773. processKey(mapper, BTN_LEFT, 0);
  2774. processSync(mapper);
  2775. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2776. ASSERT_EQ(0, motionArgs.buttonState);
  2777. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2778. // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
  2779. processKey(mapper, BTN_RIGHT, 1);
  2780. processKey(mapper, BTN_MIDDLE, 1);
  2781. processSync(mapper);
  2782. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2783. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2784. ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
  2785. motionArgs.buttonState);
  2786. processKey(mapper, BTN_RIGHT, 0);
  2787. processSync(mapper);
  2788. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2789. ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
  2790. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2791. processKey(mapper, BTN_MIDDLE, 0);
  2792. processSync(mapper);
  2793. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2794. ASSERT_EQ(0, motionArgs.buttonState);
  2795. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2796. // press BTN_BACK, release BTN_BACK
  2797. processKey(mapper, BTN_BACK, 1);
  2798. processSync(mapper);
  2799. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2800. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  2801. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  2802. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2803. ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
  2804. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2805. processKey(mapper, BTN_BACK, 0);
  2806. processSync(mapper);
  2807. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2808. ASSERT_EQ(0, motionArgs.buttonState);
  2809. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2810. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2811. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  2812. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  2813. // press BTN_SIDE, release BTN_SIDE
  2814. processKey(mapper, BTN_SIDE, 1);
  2815. processSync(mapper);
  2816. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2817. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  2818. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  2819. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2820. ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
  2821. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2822. processKey(mapper, BTN_SIDE, 0);
  2823. processSync(mapper);
  2824. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2825. ASSERT_EQ(0, motionArgs.buttonState);
  2826. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2827. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2828. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  2829. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  2830. // press BTN_FORWARD, release BTN_FORWARD
  2831. processKey(mapper, BTN_FORWARD, 1);
  2832. processSync(mapper);
  2833. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2834. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  2835. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  2836. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2837. ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
  2838. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2839. processKey(mapper, BTN_FORWARD, 0);
  2840. processSync(mapper);
  2841. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2842. ASSERT_EQ(0, motionArgs.buttonState);
  2843. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2844. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2845. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  2846. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  2847. // press BTN_EXTRA, release BTN_EXTRA
  2848. processKey(mapper, BTN_EXTRA, 1);
  2849. processSync(mapper);
  2850. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2851. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  2852. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  2853. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2854. ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
  2855. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2856. processKey(mapper, BTN_EXTRA, 0);
  2857. processSync(mapper);
  2858. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2859. ASSERT_EQ(0, motionArgs.buttonState);
  2860. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2861. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  2862. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  2863. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  2864. // press BTN_STYLUS, release BTN_STYLUS
  2865. processKey(mapper, BTN_STYLUS, 1);
  2866. processSync(mapper);
  2867. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2868. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2869. ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY, motionArgs.buttonState);
  2870. processKey(mapper, BTN_STYLUS, 0);
  2871. processSync(mapper);
  2872. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2873. ASSERT_EQ(0, motionArgs.buttonState);
  2874. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2875. // press BTN_STYLUS2, release BTN_STYLUS2
  2876. processKey(mapper, BTN_STYLUS2, 1);
  2877. processSync(mapper);
  2878. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2879. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2880. ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
  2881. processKey(mapper, BTN_STYLUS2, 0);
  2882. processSync(mapper);
  2883. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2884. ASSERT_EQ(0, motionArgs.buttonState);
  2885. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2886. // release touch
  2887. processUp(mapper);
  2888. processSync(mapper);
  2889. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2890. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  2891. ASSERT_EQ(0, motionArgs.buttonState);
  2892. }
  2893. TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
  2894. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  2895. addConfigurationProperty("touch.deviceType", "touchScreen");
  2896. prepareDisplay(DISPLAY_ORIENTATION_0);
  2897. prepareButtons();
  2898. prepareAxes(POSITION);
  2899. addMapperAndConfigure(mapper);
  2900. NotifyMotionArgs motionArgs;
  2901. // default tool type is finger
  2902. processDown(mapper, 100, 200);
  2903. processSync(mapper);
  2904. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2905. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  2906. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2907. // eraser
  2908. processKey(mapper, BTN_TOOL_RUBBER, 1);
  2909. processSync(mapper);
  2910. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2911. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2912. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
  2913. // stylus
  2914. processKey(mapper, BTN_TOOL_RUBBER, 0);
  2915. processKey(mapper, BTN_TOOL_PEN, 1);
  2916. processSync(mapper);
  2917. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2918. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2919. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  2920. // brush
  2921. processKey(mapper, BTN_TOOL_PEN, 0);
  2922. processKey(mapper, BTN_TOOL_BRUSH, 1);
  2923. processSync(mapper);
  2924. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2925. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2926. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  2927. // pencil
  2928. processKey(mapper, BTN_TOOL_BRUSH, 0);
  2929. processKey(mapper, BTN_TOOL_PENCIL, 1);
  2930. processSync(mapper);
  2931. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2932. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2933. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  2934. // airbrush
  2935. processKey(mapper, BTN_TOOL_PENCIL, 0);
  2936. processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
  2937. processSync(mapper);
  2938. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2939. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2940. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  2941. // mouse
  2942. processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
  2943. processKey(mapper, BTN_TOOL_MOUSE, 1);
  2944. processSync(mapper);
  2945. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2946. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2947. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
  2948. // lens
  2949. processKey(mapper, BTN_TOOL_MOUSE, 0);
  2950. processKey(mapper, BTN_TOOL_LENS, 1);
  2951. processSync(mapper);
  2952. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2953. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2954. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
  2955. // double-tap
  2956. processKey(mapper, BTN_TOOL_LENS, 0);
  2957. processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
  2958. processSync(mapper);
  2959. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2960. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2961. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2962. // triple-tap
  2963. processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
  2964. processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
  2965. processSync(mapper);
  2966. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2967. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2968. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2969. // quad-tap
  2970. processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
  2971. processKey(mapper, BTN_TOOL_QUADTAP, 1);
  2972. processSync(mapper);
  2973. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2974. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2975. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2976. // finger
  2977. processKey(mapper, BTN_TOOL_QUADTAP, 0);
  2978. processKey(mapper, BTN_TOOL_FINGER, 1);
  2979. processSync(mapper);
  2980. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2981. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2982. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  2983. // stylus trumps finger
  2984. processKey(mapper, BTN_TOOL_PEN, 1);
  2985. processSync(mapper);
  2986. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2987. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2988. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  2989. // eraser trumps stylus
  2990. processKey(mapper, BTN_TOOL_RUBBER, 1);
  2991. processSync(mapper);
  2992. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2993. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  2994. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
  2995. // mouse trumps eraser
  2996. processKey(mapper, BTN_TOOL_MOUSE, 1);
  2997. processSync(mapper);
  2998. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  2999. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3000. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
  3001. // back to default tool type
  3002. processKey(mapper, BTN_TOOL_MOUSE, 0);
  3003. processKey(mapper, BTN_TOOL_RUBBER, 0);
  3004. processKey(mapper, BTN_TOOL_PEN, 0);
  3005. processKey(mapper, BTN_TOOL_FINGER, 0);
  3006. processSync(mapper);
  3007. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3008. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3009. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3010. }
  3011. TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
  3012. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  3013. addConfigurationProperty("touch.deviceType", "touchScreen");
  3014. prepareDisplay(DISPLAY_ORIENTATION_0);
  3015. prepareButtons();
  3016. prepareAxes(POSITION);
  3017. mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
  3018. addMapperAndConfigure(mapper);
  3019. NotifyMotionArgs motionArgs;
  3020. // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
  3021. processKey(mapper, BTN_TOOL_FINGER, 1);
  3022. processMove(mapper, 100, 200);
  3023. processSync(mapper);
  3024. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3025. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
  3026. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3027. toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
  3028. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3029. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  3030. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3031. toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
  3032. // move a little
  3033. processMove(mapper, 150, 250);
  3034. processSync(mapper);
  3035. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3036. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  3037. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3038. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  3039. // down when BTN_TOUCH is pressed, pressure defaults to 1
  3040. processKey(mapper, BTN_TOUCH, 1);
  3041. processSync(mapper);
  3042. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3043. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
  3044. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3045. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  3046. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3047. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  3048. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3049. toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
  3050. // up when BTN_TOUCH is released, hover restored
  3051. processKey(mapper, BTN_TOUCH, 0);
  3052. processSync(mapper);
  3053. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3054. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  3055. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3056. toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
  3057. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3058. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
  3059. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3060. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  3061. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3062. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  3063. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3064. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  3065. // exit hover when pointer goes away
  3066. processKey(mapper, BTN_TOOL_FINGER, 0);
  3067. processSync(mapper);
  3068. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3069. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
  3070. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3071. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  3072. }
  3073. TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
  3074. SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
  3075. addConfigurationProperty("touch.deviceType", "touchScreen");
  3076. prepareDisplay(DISPLAY_ORIENTATION_0);
  3077. prepareButtons();
  3078. prepareAxes(POSITION | PRESSURE);
  3079. addMapperAndConfigure(mapper);
  3080. NotifyMotionArgs motionArgs;
  3081. // initially hovering because pressure is 0
  3082. processDown(mapper, 100, 200);
  3083. processPressure(mapper, 0);
  3084. processSync(mapper);
  3085. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3086. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
  3087. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3088. toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
  3089. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3090. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  3091. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3092. toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
  3093. // move a little
  3094. processMove(mapper, 150, 250);
  3095. processSync(mapper);
  3096. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3097. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  3098. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3099. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  3100. // down when pressure is non-zero
  3101. processPressure(mapper, RAW_PRESSURE_MAX);
  3102. processSync(mapper);
  3103. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3104. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
  3105. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3106. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  3107. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3108. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  3109. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3110. toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
  3111. // up when pressure becomes 0, hover restored
  3112. processPressure(mapper, 0);
  3113. processSync(mapper);
  3114. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3115. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  3116. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3117. toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
  3118. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3119. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
  3120. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3121. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  3122. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3123. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  3124. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3125. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  3126. // exit hover when pointer goes away
  3127. processUp(mapper);
  3128. processSync(mapper);
  3129. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3130. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
  3131. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3132. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  3133. }
  3134. // --- MultiTouchInputMapperTest ---
  3135. class MultiTouchInputMapperTest : public TouchInputMapperTest {
  3136. protected:
  3137. void prepareAxes(int axes);
  3138. void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
  3139. void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
  3140. void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
  3141. void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
  3142. void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
  3143. void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
  3144. void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
  3145. void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
  3146. void processId(MultiTouchInputMapper* mapper, int32_t id);
  3147. void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
  3148. void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
  3149. void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
  3150. void processMTSync(MultiTouchInputMapper* mapper);
  3151. void processSync(MultiTouchInputMapper* mapper);
  3152. };
  3153. void MultiTouchInputMapperTest::prepareAxes(int axes) {
  3154. if (axes & POSITION) {
  3155. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
  3156. RAW_X_MIN, RAW_X_MAX, 0, 0);
  3157. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
  3158. RAW_Y_MIN, RAW_Y_MAX, 0, 0);
  3159. }
  3160. if (axes & TOUCH) {
  3161. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
  3162. RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
  3163. if (axes & MINOR) {
  3164. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
  3165. RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
  3166. }
  3167. }
  3168. if (axes & TOOL) {
  3169. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
  3170. RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
  3171. if (axes & MINOR) {
  3172. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
  3173. RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
  3174. }
  3175. }
  3176. if (axes & ORIENTATION) {
  3177. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
  3178. RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
  3179. }
  3180. if (axes & PRESSURE) {
  3181. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
  3182. RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
  3183. }
  3184. if (axes & DISTANCE) {
  3185. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
  3186. RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
  3187. }
  3188. if (axes & ID) {
  3189. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
  3190. RAW_ID_MIN, RAW_ID_MAX, 0, 0);
  3191. }
  3192. if (axes & SLOT) {
  3193. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
  3194. RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
  3195. mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
  3196. }
  3197. if (axes & TOOL_TYPE) {
  3198. mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
  3199. 0, MT_TOOL_MAX, 0, 0);
  3200. }
  3201. }
  3202. void MultiTouchInputMapperTest::processPosition(
  3203. MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
  3204. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_X, x);
  3205. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_Y, y);
  3206. }
  3207. void MultiTouchInputMapperTest::processTouchMajor(
  3208. MultiTouchInputMapper* mapper, int32_t touchMajor) {
  3209. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
  3210. }
  3211. void MultiTouchInputMapperTest::processTouchMinor(
  3212. MultiTouchInputMapper* mapper, int32_t touchMinor) {
  3213. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
  3214. }
  3215. void MultiTouchInputMapperTest::processToolMajor(
  3216. MultiTouchInputMapper* mapper, int32_t toolMajor) {
  3217. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
  3218. }
  3219. void MultiTouchInputMapperTest::processToolMinor(
  3220. MultiTouchInputMapper* mapper, int32_t toolMinor) {
  3221. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
  3222. }
  3223. void MultiTouchInputMapperTest::processOrientation(
  3224. MultiTouchInputMapper* mapper, int32_t orientation) {
  3225. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_ORIENTATION, orientation);
  3226. }
  3227. void MultiTouchInputMapperTest::processPressure(
  3228. MultiTouchInputMapper* mapper, int32_t pressure) {
  3229. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_PRESSURE, pressure);
  3230. }
  3231. void MultiTouchInputMapperTest::processDistance(
  3232. MultiTouchInputMapper* mapper, int32_t distance) {
  3233. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_DISTANCE, distance);
  3234. }
  3235. void MultiTouchInputMapperTest::processId(
  3236. MultiTouchInputMapper* mapper, int32_t id) {
  3237. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TRACKING_ID, id);
  3238. }
  3239. void MultiTouchInputMapperTest::processSlot(
  3240. MultiTouchInputMapper* mapper, int32_t slot) {
  3241. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_SLOT, slot);
  3242. }
  3243. void MultiTouchInputMapperTest::processToolType(
  3244. MultiTouchInputMapper* mapper, int32_t toolType) {
  3245. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
  3246. }
  3247. void MultiTouchInputMapperTest::processKey(
  3248. MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
  3249. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, value);
  3250. }
  3251. void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
  3252. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_MT_REPORT, 0);
  3253. }
  3254. void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
  3255. process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
  3256. }
  3257. TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
  3258. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  3259. addConfigurationProperty("touch.deviceType", "touchScreen");
  3260. prepareDisplay(DISPLAY_ORIENTATION_0);
  3261. prepareAxes(POSITION);
  3262. prepareVirtualKeys();
  3263. addMapperAndConfigure(mapper);
  3264. mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
  3265. NotifyMotionArgs motionArgs;
  3266. // Two fingers down at once.
  3267. int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
  3268. processPosition(mapper, x1, y1);
  3269. processMTSync(mapper);
  3270. processPosition(mapper, x2, y2);
  3271. processMTSync(mapper);
  3272. processSync(mapper);
  3273. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3274. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  3275. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  3276. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  3277. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  3278. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  3279. ASSERT_EQ(0, motionArgs.flags);
  3280. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  3281. ASSERT_EQ(0, motionArgs.buttonState);
  3282. ASSERT_EQ(0, motionArgs.edgeFlags);
  3283. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3284. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3285. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3286. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3287. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3288. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  3289. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  3290. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  3291. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3292. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  3293. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  3294. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  3295. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  3296. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3297. motionArgs.action);
  3298. ASSERT_EQ(0, motionArgs.flags);
  3299. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  3300. ASSERT_EQ(0, motionArgs.buttonState);
  3301. ASSERT_EQ(0, motionArgs.edgeFlags);
  3302. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3303. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3304. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3305. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3306. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3307. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3308. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3309. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3310. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3311. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  3312. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  3313. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  3314. // Move.
  3315. x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
  3316. processPosition(mapper, x1, y1);
  3317. processMTSync(mapper);
  3318. processPosition(mapper, x2, y2);
  3319. processMTSync(mapper);
  3320. processSync(mapper);
  3321. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3322. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  3323. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  3324. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  3325. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  3326. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3327. ASSERT_EQ(0, motionArgs.flags);
  3328. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  3329. ASSERT_EQ(0, motionArgs.buttonState);
  3330. ASSERT_EQ(0, motionArgs.edgeFlags);
  3331. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3332. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3333. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3334. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3335. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3336. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3337. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3338. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3339. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3340. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  3341. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  3342. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  3343. // First finger up.
  3344. x2 += 15; y2 -= 20;
  3345. processPosition(mapper, x2, y2);
  3346. processMTSync(mapper);
  3347. processSync(mapper);
  3348. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3349. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  3350. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  3351. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  3352. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  3353. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3354. motionArgs.action);
  3355. ASSERT_EQ(0, motionArgs.flags);
  3356. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  3357. ASSERT_EQ(0, motionArgs.buttonState);
  3358. ASSERT_EQ(0, motionArgs.edgeFlags);
  3359. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3360. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3361. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3362. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3363. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3364. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3365. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3366. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3367. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3368. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  3369. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  3370. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  3371. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3372. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  3373. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  3374. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  3375. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  3376. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3377. ASSERT_EQ(0, motionArgs.flags);
  3378. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  3379. ASSERT_EQ(0, motionArgs.buttonState);
  3380. ASSERT_EQ(0, motionArgs.edgeFlags);
  3381. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3382. ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
  3383. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3384. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3385. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3386. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  3387. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  3388. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  3389. // Move.
  3390. x2 += 20; y2 -= 25;
  3391. processPosition(mapper, x2, y2);
  3392. processMTSync(mapper);
  3393. processSync(mapper);
  3394. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3395. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  3396. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  3397. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  3398. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  3399. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3400. ASSERT_EQ(0, motionArgs.flags);
  3401. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  3402. ASSERT_EQ(0, motionArgs.buttonState);
  3403. ASSERT_EQ(0, motionArgs.edgeFlags);
  3404. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3405. ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
  3406. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3407. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3408. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3409. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  3410. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  3411. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  3412. // New finger down.
  3413. int32_t x3 = 700, y3 = 300;
  3414. processPosition(mapper, x2, y2);
  3415. processMTSync(mapper);
  3416. processPosition(mapper, x3, y3);
  3417. processMTSync(mapper);
  3418. processSync(mapper);
  3419. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3420. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  3421. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  3422. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  3423. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  3424. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3425. motionArgs.action);
  3426. ASSERT_EQ(0, motionArgs.flags);
  3427. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  3428. ASSERT_EQ(0, motionArgs.buttonState);
  3429. ASSERT_EQ(0, motionArgs.edgeFlags);
  3430. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3431. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3432. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3433. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3434. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3435. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3436. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3437. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3438. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3439. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  3440. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  3441. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  3442. // Second finger up.
  3443. x3 += 30; y3 -= 20;
  3444. processPosition(mapper, x3, y3);
  3445. processMTSync(mapper);
  3446. processSync(mapper);
  3447. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3448. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  3449. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  3450. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  3451. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  3452. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3453. motionArgs.action);
  3454. ASSERT_EQ(0, motionArgs.flags);
  3455. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  3456. ASSERT_EQ(0, motionArgs.buttonState);
  3457. ASSERT_EQ(0, motionArgs.edgeFlags);
  3458. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3459. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3460. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3461. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3462. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3463. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3464. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3465. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3466. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3467. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  3468. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  3469. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  3470. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3471. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  3472. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  3473. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  3474. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  3475. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3476. ASSERT_EQ(0, motionArgs.flags);
  3477. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  3478. ASSERT_EQ(0, motionArgs.buttonState);
  3479. ASSERT_EQ(0, motionArgs.edgeFlags);
  3480. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3481. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3482. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3483. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3484. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3485. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  3486. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  3487. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  3488. // Last finger up.
  3489. processMTSync(mapper);
  3490. processSync(mapper);
  3491. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3492. ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
  3493. ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
  3494. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
  3495. ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
  3496. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  3497. ASSERT_EQ(0, motionArgs.flags);
  3498. ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
  3499. ASSERT_EQ(0, motionArgs.buttonState);
  3500. ASSERT_EQ(0, motionArgs.edgeFlags);
  3501. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3502. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3503. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3504. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3505. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3506. ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
  3507. ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
  3508. ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
  3509. // Should not have sent any more keys or motions.
  3510. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
  3511. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
  3512. }
  3513. TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
  3514. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  3515. addConfigurationProperty("touch.deviceType", "touchScreen");
  3516. prepareDisplay(DISPLAY_ORIENTATION_0);
  3517. prepareAxes(POSITION | ID);
  3518. prepareVirtualKeys();
  3519. addMapperAndConfigure(mapper);
  3520. mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
  3521. NotifyMotionArgs motionArgs;
  3522. // Two fingers down at once.
  3523. int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
  3524. processPosition(mapper, x1, y1);
  3525. processId(mapper, 1);
  3526. processMTSync(mapper);
  3527. processPosition(mapper, x2, y2);
  3528. processId(mapper, 2);
  3529. processMTSync(mapper);
  3530. processSync(mapper);
  3531. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3532. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  3533. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3534. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3535. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3536. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3537. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3538. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3539. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3540. motionArgs.action);
  3541. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3542. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3543. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3544. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3545. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3546. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3547. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3548. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3549. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3550. // Move.
  3551. x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
  3552. processPosition(mapper, x1, y1);
  3553. processId(mapper, 1);
  3554. processMTSync(mapper);
  3555. processPosition(mapper, x2, y2);
  3556. processId(mapper, 2);
  3557. processMTSync(mapper);
  3558. processSync(mapper);
  3559. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3560. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3561. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3562. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3563. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3564. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3565. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3566. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3567. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3568. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3569. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3570. // First finger up.
  3571. x2 += 15; y2 -= 20;
  3572. processPosition(mapper, x2, y2);
  3573. processId(mapper, 2);
  3574. processMTSync(mapper);
  3575. processSync(mapper);
  3576. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3577. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3578. motionArgs.action);
  3579. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3580. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3581. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3582. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3583. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3584. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3585. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3586. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3587. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3588. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3589. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3590. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3591. ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
  3592. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3593. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3594. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3595. // Move.
  3596. x2 += 20; y2 -= 25;
  3597. processPosition(mapper, x2, y2);
  3598. processId(mapper, 2);
  3599. processMTSync(mapper);
  3600. processSync(mapper);
  3601. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3602. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3603. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3604. ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
  3605. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3606. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3607. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3608. // New finger down.
  3609. int32_t x3 = 700, y3 = 300;
  3610. processPosition(mapper, x2, y2);
  3611. processId(mapper, 2);
  3612. processMTSync(mapper);
  3613. processPosition(mapper, x3, y3);
  3614. processId(mapper, 3);
  3615. processMTSync(mapper);
  3616. processSync(mapper);
  3617. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3618. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3619. motionArgs.action);
  3620. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3621. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3622. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3623. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3624. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3625. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3626. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3627. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3628. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3629. // Second finger up.
  3630. x3 += 30; y3 -= 20;
  3631. processPosition(mapper, x3, y3);
  3632. processId(mapper, 3);
  3633. processMTSync(mapper);
  3634. processSync(mapper);
  3635. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3636. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3637. motionArgs.action);
  3638. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3639. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3640. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3641. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3642. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3643. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3644. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3645. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3646. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3647. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3648. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3649. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3650. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3651. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3652. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3653. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3654. // Last finger up.
  3655. processMTSync(mapper);
  3656. processSync(mapper);
  3657. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3658. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  3659. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3660. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3661. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3662. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3663. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3664. // Should not have sent any more keys or motions.
  3665. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
  3666. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
  3667. }
  3668. TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
  3669. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  3670. addConfigurationProperty("touch.deviceType", "touchScreen");
  3671. prepareDisplay(DISPLAY_ORIENTATION_0);
  3672. prepareAxes(POSITION | ID | SLOT);
  3673. prepareVirtualKeys();
  3674. addMapperAndConfigure(mapper);
  3675. mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
  3676. NotifyMotionArgs motionArgs;
  3677. // Two fingers down at once.
  3678. int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
  3679. processPosition(mapper, x1, y1);
  3680. processId(mapper, 1);
  3681. processSlot(mapper, 1);
  3682. processPosition(mapper, x2, y2);
  3683. processId(mapper, 2);
  3684. processSync(mapper);
  3685. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3686. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  3687. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3688. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3689. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3690. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3691. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3692. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3693. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3694. motionArgs.action);
  3695. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3696. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3697. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3698. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3699. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3700. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3701. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3702. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3703. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3704. // Move.
  3705. x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
  3706. processSlot(mapper, 0);
  3707. processPosition(mapper, x1, y1);
  3708. processSlot(mapper, 1);
  3709. processPosition(mapper, x2, y2);
  3710. processSync(mapper);
  3711. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3712. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3713. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3714. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3715. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3716. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3717. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3718. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3719. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3720. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3721. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3722. // First finger up.
  3723. x2 += 15; y2 -= 20;
  3724. processSlot(mapper, 0);
  3725. processId(mapper, -1);
  3726. processSlot(mapper, 1);
  3727. processPosition(mapper, x2, y2);
  3728. processSync(mapper);
  3729. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3730. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3731. motionArgs.action);
  3732. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3733. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3734. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3735. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3736. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3737. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3738. toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
  3739. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3740. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3741. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3742. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3743. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3744. ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
  3745. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3746. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3747. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3748. // Move.
  3749. x2 += 20; y2 -= 25;
  3750. processPosition(mapper, x2, y2);
  3751. processSync(mapper);
  3752. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3753. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3754. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3755. ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
  3756. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3757. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3758. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3759. // New finger down.
  3760. int32_t x3 = 700, y3 = 300;
  3761. processPosition(mapper, x2, y2);
  3762. processSlot(mapper, 0);
  3763. processId(mapper, 3);
  3764. processPosition(mapper, x3, y3);
  3765. processSync(mapper);
  3766. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3767. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3768. motionArgs.action);
  3769. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3770. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3771. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3772. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3773. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3774. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3775. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3776. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3777. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3778. // Second finger up.
  3779. x3 += 30; y3 -= 20;
  3780. processSlot(mapper, 1);
  3781. processId(mapper, -1);
  3782. processSlot(mapper, 0);
  3783. processPosition(mapper, x3, y3);
  3784. processSync(mapper);
  3785. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3786. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3787. motionArgs.action);
  3788. ASSERT_EQ(size_t(2), motionArgs.pointerCount);
  3789. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3790. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3791. ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
  3792. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
  3793. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3794. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3795. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
  3796. toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
  3797. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3798. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  3799. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3800. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3801. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3802. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3803. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3804. // Last finger up.
  3805. processId(mapper, -1);
  3806. processSync(mapper);
  3807. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  3808. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  3809. ASSERT_EQ(size_t(1), motionArgs.pointerCount);
  3810. ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
  3811. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  3812. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  3813. toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
  3814. // Should not have sent any more keys or motions.
  3815. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
  3816. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
  3817. }
  3818. TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
  3819. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  3820. addConfigurationProperty("touch.deviceType", "touchScreen");
  3821. prepareDisplay(DISPLAY_ORIENTATION_0);
  3822. prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
  3823. addMapperAndConfigure(mapper);
  3824. // These calculations are based on the input device calibration documentation.
  3825. int32_t rawX = 100;
  3826. int32_t rawY = 200;
  3827. int32_t rawTouchMajor = 7;
  3828. int32_t rawTouchMinor = 6;
  3829. int32_t rawToolMajor = 9;
  3830. int32_t rawToolMinor = 8;
  3831. int32_t rawPressure = 11;
  3832. int32_t rawDistance = 0;
  3833. int32_t rawOrientation = 3;
  3834. int32_t id = 5;
  3835. float x = toDisplayX(rawX);
  3836. float y = toDisplayY(rawY);
  3837. float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
  3838. float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
  3839. float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
  3840. float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
  3841. float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
  3842. float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
  3843. float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
  3844. float distance = float(rawDistance);
  3845. processPosition(mapper, rawX, rawY);
  3846. processTouchMajor(mapper, rawTouchMajor);
  3847. processTouchMinor(mapper, rawTouchMinor);
  3848. processToolMajor(mapper, rawToolMajor);
  3849. processToolMinor(mapper, rawToolMinor);
  3850. processPressure(mapper, rawPressure);
  3851. processOrientation(mapper, rawOrientation);
  3852. processDistance(mapper, rawDistance);
  3853. processId(mapper, id);
  3854. processMTSync(mapper);
  3855. processSync(mapper);
  3856. NotifyMotionArgs args;
  3857. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  3858. ASSERT_EQ(0, args.pointerProperties[0].id);
  3859. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  3860. x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
  3861. orientation, distance));
  3862. }
  3863. TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
  3864. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  3865. addConfigurationProperty("touch.deviceType", "touchScreen");
  3866. prepareDisplay(DISPLAY_ORIENTATION_0);
  3867. prepareAxes(POSITION | TOUCH | TOOL | MINOR);
  3868. addConfigurationProperty("touch.size.calibration", "geometric");
  3869. addMapperAndConfigure(mapper);
  3870. // These calculations are based on the input device calibration documentation.
  3871. int32_t rawX = 100;
  3872. int32_t rawY = 200;
  3873. int32_t rawTouchMajor = 140;
  3874. int32_t rawTouchMinor = 120;
  3875. int32_t rawToolMajor = 180;
  3876. int32_t rawToolMinor = 160;
  3877. float x = toDisplayX(rawX);
  3878. float y = toDisplayY(rawY);
  3879. float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
  3880. float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
  3881. float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
  3882. float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
  3883. float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
  3884. processPosition(mapper, rawX, rawY);
  3885. processTouchMajor(mapper, rawTouchMajor);
  3886. processTouchMinor(mapper, rawTouchMinor);
  3887. processToolMajor(mapper, rawToolMajor);
  3888. processToolMinor(mapper, rawToolMinor);
  3889. processMTSync(mapper);
  3890. processSync(mapper);
  3891. NotifyMotionArgs args;
  3892. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  3893. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  3894. x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
  3895. }
  3896. TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
  3897. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  3898. addConfigurationProperty("touch.deviceType", "touchScreen");
  3899. prepareDisplay(DISPLAY_ORIENTATION_0);
  3900. prepareAxes(POSITION | TOUCH | TOOL);
  3901. addConfigurationProperty("touch.size.calibration", "diameter");
  3902. addConfigurationProperty("touch.size.scale", "10");
  3903. addConfigurationProperty("touch.size.bias", "160");
  3904. addConfigurationProperty("touch.size.isSummed", "1");
  3905. addMapperAndConfigure(mapper);
  3906. // These calculations are based on the input device calibration documentation.
  3907. // Note: We only provide a single common touch/tool value because the device is assumed
  3908. // not to emit separate values for each pointer (isSummed = 1).
  3909. int32_t rawX = 100;
  3910. int32_t rawY = 200;
  3911. int32_t rawX2 = 150;
  3912. int32_t rawY2 = 250;
  3913. int32_t rawTouchMajor = 5;
  3914. int32_t rawToolMajor = 8;
  3915. float x = toDisplayX(rawX);
  3916. float y = toDisplayY(rawY);
  3917. float x2 = toDisplayX(rawX2);
  3918. float y2 = toDisplayY(rawY2);
  3919. float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
  3920. float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
  3921. float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
  3922. processPosition(mapper, rawX, rawY);
  3923. processTouchMajor(mapper, rawTouchMajor);
  3924. processToolMajor(mapper, rawToolMajor);
  3925. processMTSync(mapper);
  3926. processPosition(mapper, rawX2, rawY2);
  3927. processTouchMajor(mapper, rawTouchMajor);
  3928. processToolMajor(mapper, rawToolMajor);
  3929. processMTSync(mapper);
  3930. processSync(mapper);
  3931. NotifyMotionArgs args;
  3932. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  3933. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
  3934. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  3935. ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  3936. args.action);
  3937. ASSERT_EQ(size_t(2), args.pointerCount);
  3938. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  3939. x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
  3940. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
  3941. x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
  3942. }
  3943. TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
  3944. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  3945. addConfigurationProperty("touch.deviceType", "touchScreen");
  3946. prepareDisplay(DISPLAY_ORIENTATION_0);
  3947. prepareAxes(POSITION | TOUCH | TOOL);
  3948. addConfigurationProperty("touch.size.calibration", "area");
  3949. addConfigurationProperty("touch.size.scale", "43");
  3950. addConfigurationProperty("touch.size.bias", "3");
  3951. addMapperAndConfigure(mapper);
  3952. // These calculations are based on the input device calibration documentation.
  3953. int32_t rawX = 100;
  3954. int32_t rawY = 200;
  3955. int32_t rawTouchMajor = 5;
  3956. int32_t rawToolMajor = 8;
  3957. float x = toDisplayX(rawX);
  3958. float y = toDisplayY(rawY);
  3959. float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
  3960. float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
  3961. float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
  3962. processPosition(mapper, rawX, rawY);
  3963. processTouchMajor(mapper, rawTouchMajor);
  3964. processToolMajor(mapper, rawToolMajor);
  3965. processMTSync(mapper);
  3966. processSync(mapper);
  3967. NotifyMotionArgs args;
  3968. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  3969. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  3970. x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
  3971. }
  3972. TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
  3973. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  3974. addConfigurationProperty("touch.deviceType", "touchScreen");
  3975. prepareDisplay(DISPLAY_ORIENTATION_0);
  3976. prepareAxes(POSITION | PRESSURE);
  3977. addConfigurationProperty("touch.pressure.calibration", "amplitude");
  3978. addConfigurationProperty("touch.pressure.scale", "0.01");
  3979. addMapperAndConfigure(mapper);
  3980. // These calculations are based on the input device calibration documentation.
  3981. int32_t rawX = 100;
  3982. int32_t rawY = 200;
  3983. int32_t rawPressure = 60;
  3984. float x = toDisplayX(rawX);
  3985. float y = toDisplayY(rawY);
  3986. float pressure = float(rawPressure) * 0.01f;
  3987. processPosition(mapper, rawX, rawY);
  3988. processPressure(mapper, rawPressure);
  3989. processMTSync(mapper);
  3990. processSync(mapper);
  3991. NotifyMotionArgs args;
  3992. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
  3993. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
  3994. x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
  3995. }
  3996. TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
  3997. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  3998. addConfigurationProperty("touch.deviceType", "touchScreen");
  3999. prepareDisplay(DISPLAY_ORIENTATION_0);
  4000. prepareAxes(POSITION | ID | SLOT);
  4001. addMapperAndConfigure(mapper);
  4002. NotifyMotionArgs motionArgs;
  4003. NotifyKeyArgs keyArgs;
  4004. processId(mapper, 1);
  4005. processPosition(mapper, 100, 200);
  4006. processSync(mapper);
  4007. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4008. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  4009. ASSERT_EQ(0, motionArgs.buttonState);
  4010. // press BTN_LEFT, release BTN_LEFT
  4011. processKey(mapper, BTN_LEFT, 1);
  4012. processSync(mapper);
  4013. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4014. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4015. ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
  4016. processKey(mapper, BTN_LEFT, 0);
  4017. processSync(mapper);
  4018. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4019. ASSERT_EQ(0, motionArgs.buttonState);
  4020. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4021. // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
  4022. processKey(mapper, BTN_RIGHT, 1);
  4023. processKey(mapper, BTN_MIDDLE, 1);
  4024. processSync(mapper);
  4025. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4026. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4027. ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
  4028. motionArgs.buttonState);
  4029. processKey(mapper, BTN_RIGHT, 0);
  4030. processSync(mapper);
  4031. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4032. ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
  4033. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4034. processKey(mapper, BTN_MIDDLE, 0);
  4035. processSync(mapper);
  4036. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4037. ASSERT_EQ(0, motionArgs.buttonState);
  4038. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4039. // press BTN_BACK, release BTN_BACK
  4040. processKey(mapper, BTN_BACK, 1);
  4041. processSync(mapper);
  4042. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  4043. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  4044. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  4045. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4046. ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
  4047. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4048. processKey(mapper, BTN_BACK, 0);
  4049. processSync(mapper);
  4050. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4051. ASSERT_EQ(0, motionArgs.buttonState);
  4052. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4053. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  4054. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  4055. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  4056. // press BTN_SIDE, release BTN_SIDE
  4057. processKey(mapper, BTN_SIDE, 1);
  4058. processSync(mapper);
  4059. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  4060. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  4061. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  4062. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4063. ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
  4064. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4065. processKey(mapper, BTN_SIDE, 0);
  4066. processSync(mapper);
  4067. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4068. ASSERT_EQ(0, motionArgs.buttonState);
  4069. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4070. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  4071. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  4072. ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
  4073. // press BTN_FORWARD, release BTN_FORWARD
  4074. processKey(mapper, BTN_FORWARD, 1);
  4075. processSync(mapper);
  4076. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  4077. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  4078. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  4079. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4080. ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
  4081. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4082. processKey(mapper, BTN_FORWARD, 0);
  4083. processSync(mapper);
  4084. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4085. ASSERT_EQ(0, motionArgs.buttonState);
  4086. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4087. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  4088. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  4089. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  4090. // press BTN_EXTRA, release BTN_EXTRA
  4091. processKey(mapper, BTN_EXTRA, 1);
  4092. processSync(mapper);
  4093. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  4094. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
  4095. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  4096. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4097. ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
  4098. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4099. processKey(mapper, BTN_EXTRA, 0);
  4100. processSync(mapper);
  4101. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4102. ASSERT_EQ(0, motionArgs.buttonState);
  4103. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4104. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
  4105. ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
  4106. ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
  4107. // press BTN_STYLUS, release BTN_STYLUS
  4108. processKey(mapper, BTN_STYLUS, 1);
  4109. processSync(mapper);
  4110. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4111. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4112. ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY, motionArgs.buttonState);
  4113. processKey(mapper, BTN_STYLUS, 0);
  4114. processSync(mapper);
  4115. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4116. ASSERT_EQ(0, motionArgs.buttonState);
  4117. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4118. // press BTN_STYLUS2, release BTN_STYLUS2
  4119. processKey(mapper, BTN_STYLUS2, 1);
  4120. processSync(mapper);
  4121. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4122. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4123. ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
  4124. processKey(mapper, BTN_STYLUS2, 0);
  4125. processSync(mapper);
  4126. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4127. ASSERT_EQ(0, motionArgs.buttonState);
  4128. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4129. // release touch
  4130. processId(mapper, -1);
  4131. processSync(mapper);
  4132. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4133. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  4134. ASSERT_EQ(0, motionArgs.buttonState);
  4135. }
  4136. TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
  4137. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  4138. addConfigurationProperty("touch.deviceType", "touchScreen");
  4139. prepareDisplay(DISPLAY_ORIENTATION_0);
  4140. prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
  4141. addMapperAndConfigure(mapper);
  4142. NotifyMotionArgs motionArgs;
  4143. // default tool type is finger
  4144. processId(mapper, 1);
  4145. processPosition(mapper, 100, 200);
  4146. processSync(mapper);
  4147. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4148. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  4149. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  4150. // eraser
  4151. processKey(mapper, BTN_TOOL_RUBBER, 1);
  4152. processSync(mapper);
  4153. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4154. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4155. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
  4156. // stylus
  4157. processKey(mapper, BTN_TOOL_RUBBER, 0);
  4158. processKey(mapper, BTN_TOOL_PEN, 1);
  4159. processSync(mapper);
  4160. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4161. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4162. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  4163. // brush
  4164. processKey(mapper, BTN_TOOL_PEN, 0);
  4165. processKey(mapper, BTN_TOOL_BRUSH, 1);
  4166. processSync(mapper);
  4167. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4168. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4169. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  4170. // pencil
  4171. processKey(mapper, BTN_TOOL_BRUSH, 0);
  4172. processKey(mapper, BTN_TOOL_PENCIL, 1);
  4173. processSync(mapper);
  4174. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4175. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4176. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  4177. // airbrush
  4178. processKey(mapper, BTN_TOOL_PENCIL, 0);
  4179. processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
  4180. processSync(mapper);
  4181. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4182. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4183. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  4184. // mouse
  4185. processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
  4186. processKey(mapper, BTN_TOOL_MOUSE, 1);
  4187. processSync(mapper);
  4188. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4189. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4190. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
  4191. // lens
  4192. processKey(mapper, BTN_TOOL_MOUSE, 0);
  4193. processKey(mapper, BTN_TOOL_LENS, 1);
  4194. processSync(mapper);
  4195. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4196. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4197. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
  4198. // double-tap
  4199. processKey(mapper, BTN_TOOL_LENS, 0);
  4200. processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
  4201. processSync(mapper);
  4202. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4203. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4204. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  4205. // triple-tap
  4206. processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
  4207. processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
  4208. processSync(mapper);
  4209. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4210. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4211. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  4212. // quad-tap
  4213. processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
  4214. processKey(mapper, BTN_TOOL_QUADTAP, 1);
  4215. processSync(mapper);
  4216. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4217. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4218. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  4219. // finger
  4220. processKey(mapper, BTN_TOOL_QUADTAP, 0);
  4221. processKey(mapper, BTN_TOOL_FINGER, 1);
  4222. processSync(mapper);
  4223. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4224. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4225. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  4226. // stylus trumps finger
  4227. processKey(mapper, BTN_TOOL_PEN, 1);
  4228. processSync(mapper);
  4229. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4230. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4231. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  4232. // eraser trumps stylus
  4233. processKey(mapper, BTN_TOOL_RUBBER, 1);
  4234. processSync(mapper);
  4235. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4236. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4237. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
  4238. // mouse trumps eraser
  4239. processKey(mapper, BTN_TOOL_MOUSE, 1);
  4240. processSync(mapper);
  4241. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4242. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4243. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
  4244. // MT tool type trumps BTN tool types: MT_TOOL_FINGER
  4245. processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
  4246. processSync(mapper);
  4247. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4248. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4249. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  4250. // MT tool type trumps BTN tool types: MT_TOOL_PEN
  4251. processToolType(mapper, MT_TOOL_PEN);
  4252. processSync(mapper);
  4253. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4254. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4255. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
  4256. // back to default tool type
  4257. processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
  4258. processKey(mapper, BTN_TOOL_MOUSE, 0);
  4259. processKey(mapper, BTN_TOOL_RUBBER, 0);
  4260. processKey(mapper, BTN_TOOL_PEN, 0);
  4261. processKey(mapper, BTN_TOOL_FINGER, 0);
  4262. processSync(mapper);
  4263. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4264. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
  4265. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
  4266. }
  4267. TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
  4268. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  4269. addConfigurationProperty("touch.deviceType", "touchScreen");
  4270. prepareDisplay(DISPLAY_ORIENTATION_0);
  4271. prepareAxes(POSITION | ID | SLOT);
  4272. mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
  4273. addMapperAndConfigure(mapper);
  4274. NotifyMotionArgs motionArgs;
  4275. // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
  4276. processId(mapper, 1);
  4277. processPosition(mapper, 100, 200);
  4278. processSync(mapper);
  4279. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4280. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
  4281. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4282. toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
  4283. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4284. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  4285. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4286. toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
  4287. // move a little
  4288. processPosition(mapper, 150, 250);
  4289. processSync(mapper);
  4290. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4291. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  4292. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4293. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  4294. // down when BTN_TOUCH is pressed, pressure defaults to 1
  4295. processKey(mapper, BTN_TOUCH, 1);
  4296. processSync(mapper);
  4297. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4298. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
  4299. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4300. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  4301. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4302. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  4303. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4304. toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
  4305. // up when BTN_TOUCH is released, hover restored
  4306. processKey(mapper, BTN_TOUCH, 0);
  4307. processSync(mapper);
  4308. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4309. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  4310. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4311. toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
  4312. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4313. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
  4314. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4315. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  4316. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4317. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  4318. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4319. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  4320. // exit hover when pointer goes away
  4321. processId(mapper, -1);
  4322. processSync(mapper);
  4323. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4324. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
  4325. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4326. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  4327. }
  4328. TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
  4329. MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
  4330. addConfigurationProperty("touch.deviceType", "touchScreen");
  4331. prepareDisplay(DISPLAY_ORIENTATION_0);
  4332. prepareAxes(POSITION | ID | SLOT | PRESSURE);
  4333. addMapperAndConfigure(mapper);
  4334. NotifyMotionArgs motionArgs;
  4335. // initially hovering because pressure is 0
  4336. processId(mapper, 1);
  4337. processPosition(mapper, 100, 200);
  4338. processPressure(mapper, 0);
  4339. processSync(mapper);
  4340. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4341. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
  4342. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4343. toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
  4344. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4345. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  4346. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4347. toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
  4348. // move a little
  4349. processPosition(mapper, 150, 250);
  4350. processSync(mapper);
  4351. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4352. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  4353. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4354. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  4355. // down when pressure becomes non-zero
  4356. processPressure(mapper, RAW_PRESSURE_MAX);
  4357. processSync(mapper);
  4358. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4359. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
  4360. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4361. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  4362. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4363. ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
  4364. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4365. toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
  4366. // up when pressure becomes 0, hover restored
  4367. processPressure(mapper, 0);
  4368. processSync(mapper);
  4369. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4370. ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
  4371. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4372. toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
  4373. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4374. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
  4375. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4376. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  4377. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4378. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
  4379. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4380. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  4381. // exit hover when pointer goes away
  4382. processId(mapper, -1);
  4383. processSync(mapper);
  4384. ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
  4385. ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
  4386. ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
  4387. toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
  4388. }
  4389. } // namespace android