bluetooth_remote_gatt_characteristic_unittest.cc 180 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
  5. #include <stdint.h>
  6. #include <functional>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/logging.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/run_loop.h"
  12. #include "base/test/bind.h"
  13. #include "build/build_config.h"
  14. #include "device/bluetooth/bluetooth_remote_gatt_service.h"
  15. #include "device/bluetooth/test/mock_bluetooth_adapter.h"
  16. #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. #if BUILDFLAG(IS_ANDROID)
  19. #include "device/bluetooth/test/bluetooth_test_android.h"
  20. #elif BUILDFLAG(IS_MAC)
  21. #include "device/bluetooth/test/bluetooth_test_mac.h"
  22. #elif BUILDFLAG(IS_WIN)
  23. #include "device/bluetooth/test/bluetooth_test_win.h"
  24. #elif defined(USE_CAST_BLUETOOTH_ADAPTER)
  25. #include "device/bluetooth/test/bluetooth_test_cast.h"
  26. #elif BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  27. #include "device/bluetooth/test/bluetooth_test_bluez.h"
  28. #elif BUILDFLAG(IS_FUCHSIA)
  29. #include "device/bluetooth/test/bluetooth_test_fuchsia.h"
  30. #endif
  31. using testing::_;
  32. using testing::Invoke;
  33. using WriteType = device::BluetoothRemoteGattCharacteristic::WriteType;
  34. namespace device {
  35. class BluetoothRemoteGattCharacteristicTest :
  36. #if BUILDFLAG(IS_WIN)
  37. public BluetoothTestWinrt {
  38. #else
  39. public BluetoothTest {
  40. #endif
  41. public:
  42. // Creates adapter_, device_, service_, characteristic1_, & characteristic2_.
  43. // |properties| will be used for each characteristic.
  44. void FakeCharacteristicBoilerplate(int properties = 0) {
  45. InitWithFakeAdapter();
  46. StartLowEnergyDiscoverySession();
  47. device_ = SimulateLowEnergyDevice(3);
  48. device_->CreateGattConnection(
  49. GetGattConnectionCallback(Call::EXPECTED, Result::SUCCESS));
  50. SimulateGattConnection(device_);
  51. base::RunLoop().RunUntilIdle();
  52. SimulateGattServicesDiscovered(
  53. device_, std::vector<std::string>({kTestUUIDGenericAccess}));
  54. base::RunLoop().RunUntilIdle();
  55. ASSERT_EQ(1u, device_->GetGattServices().size());
  56. service_ = device_->GetGattServices()[0];
  57. SimulateGattCharacteristic(service_, kTestUUIDDeviceName, properties);
  58. SimulateGattCharacteristic(service_, kTestUUIDDeviceName, properties);
  59. base::RunLoop().RunUntilIdle();
  60. ASSERT_EQ(2u, service_->GetCharacteristics().size());
  61. characteristic1_ = service_->GetCharacteristics()[0];
  62. characteristic2_ = service_->GetCharacteristics()[1];
  63. ResetEventCounts();
  64. }
  65. enum class StartNotifySetupError {
  66. CHARACTERISTIC_PROPERTIES,
  67. CONFIG_DESCRIPTOR_MISSING,
  68. CONFIG_DESCRIPTOR_DUPLICATE,
  69. SET_NOTIFY,
  70. WRITE_DESCRIPTOR,
  71. NONE
  72. };
  73. // Constructs characteristics with |properties|, calls StartNotifySession,
  74. // and verifies the appropriate |expected_config_descriptor_value| is written.
  75. // Error scenarios in this boilerplate are tested by setting |error| to the
  76. // setup stage to test.
  77. void StartNotifyBoilerplate(
  78. int properties,
  79. NotifyValueState notify_value_state,
  80. StartNotifySetupError error = StartNotifySetupError::NONE) {
  81. if (error == StartNotifySetupError::CHARACTERISTIC_PROPERTIES) {
  82. properties = 0;
  83. }
  84. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(properties));
  85. size_t expected_descriptors_count = 0;
  86. if (error != StartNotifySetupError::CONFIG_DESCRIPTOR_MISSING) {
  87. SimulateGattDescriptor(
  88. characteristic1_,
  89. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  90. .canonical_value());
  91. expected_descriptors_count++;
  92. }
  93. if (error == StartNotifySetupError::CONFIG_DESCRIPTOR_DUPLICATE) {
  94. SimulateGattDescriptor(
  95. characteristic1_,
  96. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  97. .canonical_value());
  98. expected_descriptors_count++;
  99. }
  100. base::RunLoop().RunUntilIdle();
  101. ASSERT_EQ(expected_descriptors_count,
  102. characteristic1_->GetDescriptors().size());
  103. if (error == StartNotifySetupError::SET_NOTIFY) {
  104. SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce(
  105. characteristic1_);
  106. }
  107. if (error == StartNotifySetupError::WRITE_DESCRIPTOR) {
  108. SimulateGattDescriptorWriteWillFailSynchronouslyOnce(
  109. characteristic1_->GetDescriptors()[0]);
  110. }
  111. if (error != StartNotifySetupError::NONE) {
  112. characteristic1_->StartNotifySession(
  113. GetNotifyCallback(Call::NOT_EXPECTED),
  114. GetGattErrorCallback(Call::EXPECTED));
  115. return;
  116. }
  117. characteristic1_->StartNotifySession(
  118. GetNotifyCallback(Call::EXPECTED),
  119. GetGattErrorCallback(Call::NOT_EXPECTED));
  120. EXPECT_EQ(0, callback_count_);
  121. SimulateGattNotifySessionStarted(characteristic1_);
  122. base::RunLoop().RunUntilIdle();
  123. ExpectedChangeNotifyValueAttempts(1);
  124. EXPECT_EQ(1, callback_count_);
  125. EXPECT_EQ(0, error_callback_count_);
  126. ASSERT_EQ(1u, notify_sessions_.size());
  127. ASSERT_TRUE(notify_sessions_[0]);
  128. EXPECT_EQ(characteristic1_->GetIdentifier(),
  129. notify_sessions_[0]->GetCharacteristicIdentifier());
  130. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  131. // Verify the Client Characteristic Configuration descriptor was written to.
  132. ExpectedNotifyValue(notify_value_state);
  133. }
  134. // A few tests below don't behave correctly on Classic Windows, but do for
  135. // WinRT. Since a #if BUILDFLAG(IS_WIN) guard is not sufficient to distinguish
  136. // these two cases, this small utility function is added.
  137. bool IsClassicWin() {
  138. #if BUILDFLAG(IS_WIN)
  139. return !UsesNewBleImplementation();
  140. #else
  141. return false;
  142. #endif
  143. }
  144. raw_ptr<BluetoothDevice> device_ = nullptr;
  145. raw_ptr<BluetoothRemoteGattService> service_ = nullptr;
  146. raw_ptr<BluetoothRemoteGattCharacteristic> characteristic1_ = nullptr;
  147. raw_ptr<BluetoothRemoteGattCharacteristic> characteristic2_ = nullptr;
  148. };
  149. #if BUILDFLAG(IS_WIN)
  150. using BluetoothRemoteGattCharacteristicTestWinrt =
  151. BluetoothRemoteGattCharacteristicTest;
  152. using BluetoothRemoteGattCharacteristicTestWin32Only =
  153. BluetoothRemoteGattCharacteristicTest;
  154. using BluetoothRemoteGattCharacteristicTestWinrtOnly =
  155. BluetoothRemoteGattCharacteristicTest;
  156. #endif
  157. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  158. #define MAYBE_GetIdentifier GetIdentifier
  159. #else
  160. #define MAYBE_GetIdentifier DISABLED_GetIdentifier
  161. #endif
  162. #if BUILDFLAG(IS_WIN)
  163. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, GetIdentifier) {
  164. #else
  165. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_GetIdentifier) {
  166. #endif
  167. if (!PlatformSupportsLowEnergy()) {
  168. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  169. return;
  170. }
  171. InitWithFakeAdapter();
  172. StartLowEnergyDiscoverySession();
  173. // 2 devices to verify unique IDs across them.
  174. BluetoothDevice* device1 = SimulateLowEnergyDevice(3);
  175. BluetoothDevice* device2 = SimulateLowEnergyDevice(4);
  176. device1->CreateGattConnection(
  177. GetGattConnectionCallback(Call::EXPECTED, Result::SUCCESS));
  178. device2->CreateGattConnection(
  179. GetGattConnectionCallback(Call::EXPECTED, Result::SUCCESS));
  180. SimulateGattConnection(device1);
  181. SimulateGattConnection(device2);
  182. base::RunLoop().RunUntilIdle();
  183. // 3 services (all with same UUID).
  184. // 1 on the first device (to test characteristic instances across devices).
  185. // 2 on the second device (to test same device, multiple service instances).
  186. SimulateGattServicesDiscovered(
  187. device1, std::vector<std::string>({kTestUUIDGenericAccess}));
  188. base::RunLoop().RunUntilIdle();
  189. SimulateGattServicesDiscovered(
  190. device2, std::vector<std::string>(
  191. {kTestUUIDGenericAccess, kTestUUIDGenericAccess}));
  192. base::RunLoop().RunUntilIdle();
  193. BluetoothRemoteGattService* service1 = device1->GetGattServices()[0];
  194. BluetoothRemoteGattService* service2 = device2->GetGattServices()[0];
  195. BluetoothRemoteGattService* service3 = device2->GetGattServices()[1];
  196. // 6 characteristics (same UUID), 2 on each service.
  197. SimulateGattCharacteristic(service1, kTestUUIDDeviceName, /* properties */ 0);
  198. SimulateGattCharacteristic(service1, kTestUUIDDeviceName, /* properties */ 0);
  199. SimulateGattCharacteristic(service2, kTestUUIDDeviceName, /* properties */ 0);
  200. SimulateGattCharacteristic(service2, kTestUUIDDeviceName, /* properties */ 0);
  201. SimulateGattCharacteristic(service3, kTestUUIDDeviceName, /* properties */ 0);
  202. SimulateGattCharacteristic(service3, kTestUUIDDeviceName, /* properties */ 0);
  203. base::RunLoop().RunUntilIdle();
  204. BluetoothRemoteGattCharacteristic* char1 = service1->GetCharacteristics()[0];
  205. BluetoothRemoteGattCharacteristic* char2 = service1->GetCharacteristics()[1];
  206. BluetoothRemoteGattCharacteristic* char3 = service2->GetCharacteristics()[0];
  207. BluetoothRemoteGattCharacteristic* char4 = service2->GetCharacteristics()[1];
  208. BluetoothRemoteGattCharacteristic* char5 = service3->GetCharacteristics()[0];
  209. BluetoothRemoteGattCharacteristic* char6 = service3->GetCharacteristics()[1];
  210. // All IDs are unique, even though they have the same UUID.
  211. EXPECT_NE(char1->GetIdentifier(), char2->GetIdentifier());
  212. EXPECT_NE(char1->GetIdentifier(), char3->GetIdentifier());
  213. EXPECT_NE(char1->GetIdentifier(), char4->GetIdentifier());
  214. EXPECT_NE(char1->GetIdentifier(), char5->GetIdentifier());
  215. EXPECT_NE(char1->GetIdentifier(), char6->GetIdentifier());
  216. EXPECT_NE(char2->GetIdentifier(), char3->GetIdentifier());
  217. EXPECT_NE(char2->GetIdentifier(), char4->GetIdentifier());
  218. EXPECT_NE(char2->GetIdentifier(), char5->GetIdentifier());
  219. EXPECT_NE(char2->GetIdentifier(), char6->GetIdentifier());
  220. EXPECT_NE(char3->GetIdentifier(), char4->GetIdentifier());
  221. EXPECT_NE(char3->GetIdentifier(), char5->GetIdentifier());
  222. EXPECT_NE(char3->GetIdentifier(), char6->GetIdentifier());
  223. EXPECT_NE(char4->GetIdentifier(), char5->GetIdentifier());
  224. EXPECT_NE(char4->GetIdentifier(), char6->GetIdentifier());
  225. EXPECT_NE(char5->GetIdentifier(), char6->GetIdentifier());
  226. }
  227. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  228. #define MAYBE_GetUUID GetUUID
  229. #else
  230. #define MAYBE_GetUUID DISABLED_GetUUID
  231. #endif
  232. #if BUILDFLAG(IS_WIN)
  233. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, GetUUID) {
  234. #else
  235. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_GetUUID) {
  236. #endif
  237. if (!PlatformSupportsLowEnergy()) {
  238. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  239. return;
  240. }
  241. InitWithFakeAdapter();
  242. StartLowEnergyDiscoverySession();
  243. BluetoothDevice* device = SimulateLowEnergyDevice(3);
  244. device->CreateGattConnection(
  245. GetGattConnectionCallback(Call::EXPECTED, Result::SUCCESS));
  246. SimulateGattConnection(device);
  247. base::RunLoop().RunUntilIdle();
  248. SimulateGattServicesDiscovered(
  249. device, std::vector<std::string>({kTestUUIDGenericAccess}));
  250. base::RunLoop().RunUntilIdle();
  251. BluetoothRemoteGattService* service = device->GetGattServices()[0];
  252. // Create 3 characteristics. Two of them are duplicates.
  253. BluetoothUUID uuid1(kTestUUIDDeviceName);
  254. BluetoothUUID uuid2(kTestUUIDAppearance);
  255. SimulateGattCharacteristic(service, kTestUUIDDeviceName, /* properties */ 0);
  256. SimulateGattCharacteristic(service, kTestUUIDAppearance, /* properties */ 0);
  257. SimulateGattCharacteristic(service, kTestUUIDAppearance, /* properties */ 0);
  258. base::RunLoop().RunUntilIdle();
  259. BluetoothRemoteGattCharacteristic* char1 = service->GetCharacteristics()[0];
  260. BluetoothRemoteGattCharacteristic* char2 = service->GetCharacteristics()[1];
  261. BluetoothRemoteGattCharacteristic* char3 = service->GetCharacteristics()[2];
  262. // Swap as needed to have char1 point to the the characteristic with uuid1.
  263. if (char2->GetUUID() == uuid1) {
  264. std::swap(char1, char2);
  265. } else if (char3->GetUUID() == uuid1) {
  266. std::swap(char1, char3);
  267. }
  268. EXPECT_EQ(uuid1, char1->GetUUID());
  269. EXPECT_EQ(uuid2, char2->GetUUID());
  270. EXPECT_EQ(uuid2, char3->GetUUID());
  271. }
  272. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  273. #define MAYBE_GetProperties GetProperties
  274. #else
  275. #define MAYBE_GetProperties DISABLED_GetProperties
  276. #endif
  277. #if BUILDFLAG(IS_WIN)
  278. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, GetProperties) {
  279. #else
  280. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_GetProperties) {
  281. #endif
  282. if (!PlatformSupportsLowEnergy()) {
  283. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  284. return;
  285. }
  286. InitWithFakeAdapter();
  287. StartLowEnergyDiscoverySession();
  288. BluetoothDevice* device = SimulateLowEnergyDevice(3);
  289. device->CreateGattConnection(
  290. GetGattConnectionCallback(Call::EXPECTED, Result::SUCCESS));
  291. SimulateGattConnection(device);
  292. base::RunLoop().RunUntilIdle();
  293. SimulateGattServicesDiscovered(
  294. device, std::vector<std::string>({kTestUUIDGenericAccess}));
  295. base::RunLoop().RunUntilIdle();
  296. BluetoothRemoteGattService* service = device->GetGattServices()[0];
  297. // Create two characteristics with different properties:
  298. SimulateGattCharacteristic(service, kTestUUIDDeviceName, /* properties */ 0);
  299. SimulateGattCharacteristic(service, kTestUUIDDeviceName, /* properties */ 7);
  300. base::RunLoop().RunUntilIdle();
  301. // Read the properties. Because ordering is unknown swap as necessary.
  302. int properties1 = service->GetCharacteristics()[0]->GetProperties();
  303. int properties2 = service->GetCharacteristics()[1]->GetProperties();
  304. if (properties2 == 0)
  305. std::swap(properties1, properties2);
  306. EXPECT_EQ(0, properties1);
  307. EXPECT_EQ(7, properties2);
  308. }
  309. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  310. #define MAYBE_GetService GetService
  311. #else
  312. #define MAYBE_GetService DISABLED_GetService
  313. #endif
  314. // Tests GetService.
  315. #if BUILDFLAG(IS_WIN)
  316. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, GetService) {
  317. #else
  318. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_GetService) {
  319. #endif
  320. if (!PlatformSupportsLowEnergy()) {
  321. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  322. return;
  323. }
  324. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
  325. EXPECT_EQ(service_, characteristic1_->GetService());
  326. EXPECT_EQ(service_, characteristic2_->GetService());
  327. }
  328. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  329. #define MAYBE_ReadRemoteCharacteristic_Empty ReadRemoteCharacteristic_Empty
  330. #else
  331. #define MAYBE_ReadRemoteCharacteristic_Empty \
  332. DISABLED_ReadRemoteCharacteristic_Empty
  333. #endif
  334. // Tests ReadRemoteCharacteristic and GetValue with empty value buffer.
  335. #if BUILDFLAG(IS_WIN)
  336. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  337. ReadRemoteCharacteristic_Empty) {
  338. #else
  339. TEST_F(BluetoothRemoteGattCharacteristicTest,
  340. MAYBE_ReadRemoteCharacteristic_Empty) {
  341. #endif
  342. if (!PlatformSupportsLowEnergy()) {
  343. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  344. return;
  345. }
  346. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  347. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  348. characteristic1_->ReadRemoteCharacteristic(
  349. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  350. std::vector<uint8_t> empty_vector;
  351. SimulateGattCharacteristicRead(characteristic1_, empty_vector);
  352. base::RunLoop().RunUntilIdle();
  353. // Duplicate read reported from OS shouldn't cause a problem:
  354. SimulateGattCharacteristicRead(characteristic1_, empty_vector);
  355. base::RunLoop().RunUntilIdle();
  356. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  357. EXPECT_EQ(empty_vector, last_read_value_);
  358. EXPECT_EQ(empty_vector, characteristic1_->GetValue());
  359. }
  360. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  361. #define MAYBE_WriteRemoteCharacteristic_Empty WriteRemoteCharacteristic_Empty
  362. #else
  363. #define MAYBE_WriteRemoteCharacteristic_Empty \
  364. DISABLED_WriteRemoteCharacteristic_Empty
  365. #endif
  366. // Tests WriteRemoteCharacteristic with empty value buffer.
  367. #if BUILDFLAG(IS_WIN)
  368. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  369. WriteRemoteCharacteristic_Empty) {
  370. #else
  371. TEST_F(BluetoothRemoteGattCharacteristicTest,
  372. MAYBE_WriteRemoteCharacteristic_Empty) {
  373. #endif
  374. if (!PlatformSupportsLowEnergy()) {
  375. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  376. return;
  377. }
  378. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  379. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  380. std::vector<uint8_t> empty_vector;
  381. base::RunLoop loop;
  382. characteristic1_->WriteRemoteCharacteristic(
  383. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  384. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  385. EXPECT_EQ(empty_vector, last_write_value_);
  386. loop.Quit();
  387. }),
  388. base::BindLambdaForTesting(
  389. [&](BluetoothGattService::GattErrorCode error_code) {
  390. ADD_FAILURE() << "unexpected error: " << error_code;
  391. loop.Quit();
  392. }));
  393. SimulateGattCharacteristicWrite(characteristic1_);
  394. loop.Run();
  395. // Duplicate write reported from OS shouldn't cause a problem:
  396. SimulateGattCharacteristicWrite(characteristic1_);
  397. base::RunLoop().RunUntilIdle();
  398. }
  399. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  400. #define MAYBE_DeprecatedWriteRemoteCharacteristic_Empty \
  401. DeprecatedWriteRemoteCharacteristic_Empty
  402. #else
  403. #define MAYBE_DeprecatedWriteRemoteCharacteristic_Empty \
  404. DISABLED_DeprecatedWriteRemoteCharacteristic_Empty
  405. #endif
  406. // Tests DeprecatedWriteRemoteCharacteristic with empty value buffer.
  407. #if BUILDFLAG(IS_WIN)
  408. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  409. DeprecatedWriteRemoteCharacteristic_Empty) {
  410. #else
  411. TEST_F(BluetoothRemoteGattCharacteristicTest,
  412. MAYBE_DeprecatedWriteRemoteCharacteristic_Empty) {
  413. #endif
  414. if (!PlatformSupportsLowEnergy()) {
  415. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  416. return;
  417. }
  418. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  419. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  420. std::vector<uint8_t> empty_vector;
  421. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  422. empty_vector, GetCallback(Call::EXPECTED),
  423. GetGattErrorCallback(Call::NOT_EXPECTED));
  424. SimulateGattCharacteristicWrite(characteristic1_);
  425. base::RunLoop().RunUntilIdle();
  426. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  427. // Duplicate write reported from OS shouldn't cause a problem:
  428. SimulateGattCharacteristicWrite(characteristic1_);
  429. base::RunLoop().RunUntilIdle();
  430. EXPECT_EQ(empty_vector, last_write_value_);
  431. }
  432. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  433. #define MAYBE_Retry_ReadRemoteCharacteristic_DuringDestruction_Fails \
  434. Retry_ReadRemoteCharacteristic_DuringDestruction_Fails
  435. #else
  436. #define MAYBE_Retry_ReadRemoteCharacteristic_DuringDestruction_Fails \
  437. DISABLED_Retry_ReadRemoteCharacteristic_DuringDestruction_Fails
  438. #endif
  439. #if BUILDFLAG(IS_WIN)
  440. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  441. Retry_ReadRemoteCharacteristic_DuringDestruction_Fails) {
  442. #else
  443. TEST_F(BluetoothRemoteGattCharacteristicTest,
  444. MAYBE_Retry_ReadRemoteCharacteristic_DuringDestruction_Fails) {
  445. #endif
  446. if (!PlatformSupportsLowEnergy()) {
  447. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  448. return;
  449. }
  450. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  451. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  452. bool read_characteristic_failed = false;
  453. characteristic1_->ReadRemoteCharacteristic(base::BindLambdaForTesting(
  454. [&](absl::optional<BluetoothGattService::GattErrorCode> error_code,
  455. const std::vector<uint8_t>&) {
  456. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  457. read_characteristic_failed = true;
  458. // Retrying Read should fail:
  459. characteristic1_->ReadRemoteCharacteristic(
  460. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  461. }));
  462. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  463. base::RunLoop().RunUntilIdle();
  464. EXPECT_TRUE(read_characteristic_failed);
  465. EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
  466. last_gatt_error_code_);
  467. }
  468. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  469. #define MAYBE_Retry_WriteRemoteCharacteristic_DuringDestruction_Fails \
  470. Retry_WriteRemoteCharacteristic_DuringDestruction_Fails
  471. #else
  472. #define MAYBE_Retry_WriteRemoteCharacteristic_DuringDestruction_Fails \
  473. DISABLED_Retry_WriteRemoteCharacteristic_DuringDestruction_Fails
  474. #endif
  475. #if BUILDFLAG(IS_WIN)
  476. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  477. Retry_WriteRemoteCharacteristic_DuringDestruction_Fails) {
  478. #else
  479. TEST_F(BluetoothRemoteGattCharacteristicTest,
  480. MAYBE_Retry_WriteRemoteCharacteristic_DuringDestruction_Fails) {
  481. #endif
  482. if (!PlatformSupportsLowEnergy()) {
  483. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  484. return;
  485. }
  486. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  487. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  488. base::RunLoop loop;
  489. characteristic1_->WriteRemoteCharacteristic(
  490. {} /* value */, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  491. ADD_FAILURE() << "unexpected success";
  492. loop.Quit();
  493. }),
  494. base::BindLambdaForTesting(
  495. [&](BluetoothGattService::GattErrorCode error_code) {
  496. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  497. // Retrying Write should fail:
  498. characteristic1_->WriteRemoteCharacteristic(
  499. {} /* value */, WriteType::kWithResponse,
  500. base::BindLambdaForTesting([&] {
  501. ADD_FAILURE() << "unexpected success";
  502. loop.Quit();
  503. }),
  504. base::BindLambdaForTesting(
  505. [&](BluetoothGattService::GattErrorCode error_code) {
  506. EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
  507. error_code);
  508. loop.Quit();
  509. }));
  510. }));
  511. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  512. loop.Run();
  513. }
  514. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  515. #define MAYBE_Retry_DeprecatedWriteRemoteCharacteristic_DuringDestruction_Fails \
  516. Retry_DeprecatedWriteRemoteCharacteristic_DuringDestruction_Fails
  517. #else
  518. #define MAYBE_Retry_DeprecatedWriteRemoteCharacteristic_DuringDestruction_Fails \
  519. DISABLED_Retry_DeprecatedWriteRemoteCharacteristic_DuringDestruction_Fails
  520. #endif
  521. #if BUILDFLAG(IS_WIN)
  522. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  523. Retry_DeprecatedWriteRemoteCharacteristic_DuringDestruction_Fails) {
  524. #else
  525. TEST_F(
  526. BluetoothRemoteGattCharacteristicTest,
  527. MAYBE_Retry_DeprecatedWriteRemoteCharacteristic_DuringDestruction_Fails) {
  528. #endif
  529. if (!PlatformSupportsLowEnergy()) {
  530. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  531. return;
  532. }
  533. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  534. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  535. bool write_error_callback_called = false;
  536. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  537. {} /* value */, GetCallback(Call::NOT_EXPECTED),
  538. base::BindLambdaForTesting(
  539. [&](BluetoothGattService::GattErrorCode error_code) {
  540. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  541. write_error_callback_called = true;
  542. // Retrying Write should fail:
  543. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  544. {} /* value */, GetCallback(Call::NOT_EXPECTED),
  545. GetGattErrorCallback(Call::EXPECTED));
  546. }));
  547. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  548. base::RunLoop().RunUntilIdle();
  549. EXPECT_TRUE(write_error_callback_called);
  550. EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
  551. last_gatt_error_code_);
  552. }
  553. #if BUILDFLAG(IS_ANDROID)
  554. #define MAYBE_ReadRemoteCharacteristic_AfterDeleted \
  555. ReadRemoteCharacteristic_AfterDeleted
  556. #else
  557. #define MAYBE_ReadRemoteCharacteristic_AfterDeleted \
  558. DISABLED_ReadRemoteCharacteristic_AfterDeleted
  559. #endif
  560. // Tests ReadRemoteCharacteristic completing after Chrome objects are deleted.
  561. // macOS: Not applicable: This can never happen if CBPeripheral delegate is set
  562. // to nil.
  563. // WinRT: Not applicable: Pending callbacks won't fire once the underlying
  564. // object is destroyed.
  565. #if BUILDFLAG(IS_WIN)
  566. TEST_P(BluetoothRemoteGattCharacteristicTestWin32Only,
  567. ReadRemoteCharacteristic_AfterDeleted) {
  568. #else
  569. TEST_F(BluetoothRemoteGattCharacteristicTest,
  570. MAYBE_ReadRemoteCharacteristic_AfterDeleted) {
  571. #endif
  572. if (!PlatformSupportsLowEnergy()) {
  573. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  574. return;
  575. }
  576. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  577. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  578. characteristic1_->ReadRemoteCharacteristic(
  579. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  580. RememberCharacteristicForSubsequentAction(characteristic1_);
  581. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  582. std::vector<uint8_t> empty_vector;
  583. SimulateGattCharacteristicRead(/* use remembered characteristic */ nullptr,
  584. empty_vector);
  585. base::RunLoop().RunUntilIdle();
  586. EXPECT_TRUE("Did not crash!");
  587. }
  588. // TODO(crbug.com/663131): Enable test on windows when disconnection is
  589. // implemented.
  590. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  591. #define MAYBE_ReadRemoteCharacteristic_Disconnected \
  592. ReadRemoteCharacteristic_Disconnected
  593. #else
  594. #define MAYBE_ReadRemoteCharacteristic_Disconnected \
  595. DISABLED_ReadRemoteCharacteristic_Disconnected
  596. #endif
  597. #if BUILDFLAG(IS_WIN)
  598. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  599. ReadRemoteCharacteristic_Disconnected) {
  600. #else
  601. TEST_F(BluetoothRemoteGattCharacteristicTest,
  602. MAYBE_ReadRemoteCharacteristic_Disconnected) {
  603. #endif
  604. if (!PlatformSupportsLowEnergy()) {
  605. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  606. return;
  607. }
  608. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  609. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  610. characteristic1_->ReadRemoteCharacteristic(
  611. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  612. // Set up for receiving a read response after disconnection.
  613. // On macOS or WinRT no events arrive after disconnection so there is no point
  614. // in building the infrastructure to test this behavior. FYI
  615. // the code CHECKs that responses arrive only when the device is connected.
  616. #if BUILDFLAG(IS_ANDROID)
  617. RememberCharacteristicForSubsequentAction(characteristic1_);
  618. #endif
  619. ASSERT_EQ(1u, adapter_->GetDevices().size());
  620. SimulateDeviceBreaksConnection(adapter_->GetDevices()[0]);
  621. base::RunLoop().RunUntilIdle();
  622. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  623. // Dispatch read response after disconnection. See above explanation for why
  624. // we don't do this in macOS on WinRT.
  625. #if BUILDFLAG(IS_ANDROID)
  626. std::vector<uint8_t> empty_vector;
  627. SimulateGattCharacteristicRead(nullptr /* use remembered characteristic */,
  628. empty_vector);
  629. base::RunLoop().RunUntilIdle();
  630. #endif
  631. }
  632. #if BUILDFLAG(IS_ANDROID)
  633. #define MAYBE_WriteRemoteCharacteristic_AfterDeleted \
  634. WriteRemoteCharacteristic_AfterDeleted
  635. #else
  636. #define MAYBE_WriteRemoteCharacteristic_AfterDeleted \
  637. DISABLED_WriteRemoteCharacteristic_AfterDeleted
  638. #endif
  639. // Tests WriteRemoteCharacteristic completing after Chrome objects are deleted.
  640. // macOS: Not applicable: This can never happen if CBPeripheral
  641. // delegate is set to nil.
  642. // WinRT: Not applicable: Pending callbacks won't fire once the underlying
  643. // object is destroyed.
  644. #if BUILDFLAG(IS_WIN)
  645. TEST_P(BluetoothRemoteGattCharacteristicTestWin32Only,
  646. WriteRemoteCharacteristic_AfterDeleted) {
  647. #else
  648. TEST_F(BluetoothRemoteGattCharacteristicTest,
  649. MAYBE_WriteRemoteCharacteristic_AfterDeleted) {
  650. #endif
  651. if (!PlatformSupportsLowEnergy()) {
  652. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  653. return;
  654. }
  655. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  656. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  657. std::vector<uint8_t> empty_vector;
  658. base::RunLoop loop;
  659. characteristic1_->WriteRemoteCharacteristic(
  660. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  661. ADD_FAILURE() << "unexpected success";
  662. loop.Quit();
  663. }),
  664. base::BindLambdaForTesting(
  665. [&](BluetoothGattService::GattErrorCode error_code) {
  666. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  667. loop.Quit();
  668. }));
  669. RememberCharacteristicForSubsequentAction(characteristic1_);
  670. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  671. SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr);
  672. loop.Run();
  673. }
  674. #if BUILDFLAG(IS_ANDROID)
  675. #define MAYBE_DeprecatedWriteRemoteCharacteristic_AfterDeleted \
  676. DeprecatedWriteRemoteCharacteristic_AfterDeleted
  677. #else
  678. #define MAYBE_DeprecatedWriteRemoteCharacteristic_AfterDeleted \
  679. DISABLED_DeprecatedWriteRemoteCharacteristic_AfterDeleted
  680. #endif
  681. // Tests DeprecatedWriteRemoteCharacteristic completing after Chrome objects are
  682. // deleted.
  683. // macOS: Not applicable: This can never happen if CBPeripheral
  684. // delegate is set to nil.
  685. // WinRT: Not applicable: Pending callbacks won't fire once the underlying
  686. // object is destroyed.
  687. #if BUILDFLAG(IS_WIN)
  688. TEST_P(BluetoothRemoteGattCharacteristicTestWin32Only,
  689. DeprecatedWriteRemoteCharacteristic_AfterDeleted) {
  690. #else
  691. TEST_F(BluetoothRemoteGattCharacteristicTest,
  692. MAYBE_DeprecatedWriteRemoteCharacteristic_AfterDeleted) {
  693. #endif
  694. if (!PlatformSupportsLowEnergy()) {
  695. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  696. return;
  697. }
  698. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  699. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  700. std::vector<uint8_t> empty_vector;
  701. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  702. empty_vector, GetCallback(Call::NOT_EXPECTED),
  703. GetGattErrorCallback(Call::EXPECTED));
  704. RememberCharacteristicForSubsequentAction(characteristic1_);
  705. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  706. SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr);
  707. base::RunLoop().RunUntilIdle();
  708. EXPECT_TRUE("Did not crash!");
  709. }
  710. // TODO(crbug.com/663131): Enable test on windows when disconnection is
  711. // implemented.
  712. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  713. #define MAYBE_WriteRemoteCharacteristic_Disconnected \
  714. WriteRemoteCharacteristic_Disconnected
  715. #else
  716. #define MAYBE_WriteRemoteCharacteristic_Disconnected \
  717. DISABLED_WriteRemoteCharacteristic_Disconnected
  718. #endif
  719. #if BUILDFLAG(IS_WIN)
  720. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  721. WriteRemoteCharacteristic_Disconnected) {
  722. #else
  723. TEST_F(BluetoothRemoteGattCharacteristicTest,
  724. MAYBE_WriteRemoteCharacteristic_Disconnected) {
  725. #endif
  726. if (!PlatformSupportsLowEnergy()) {
  727. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  728. return;
  729. }
  730. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  731. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  732. base::RunLoop loop;
  733. std::vector<uint8_t> empty_vector;
  734. characteristic1_->WriteRemoteCharacteristic(
  735. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  736. ADD_FAILURE() << "unexpected success";
  737. loop.Quit();
  738. }),
  739. base::BindLambdaForTesting(
  740. [&](BluetoothGattService::GattErrorCode error_code) {
  741. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  742. loop.Quit();
  743. }));
  744. // Set up for receiving a write response after disconnection.
  745. // On macOS and WinRT no events arrive after disconnection so there is no point
  746. // in building the infrastructure to test this behavior. FYI
  747. // the code CHECKs that responses arrive only when the device is connected.
  748. #if BUILDFLAG(IS_ANDROID)
  749. RememberCharacteristicForSubsequentAction(characteristic1_);
  750. #endif // BUILDFLAG(IS_ANDROID)
  751. ASSERT_EQ(1u, adapter_->GetDevices().size());
  752. SimulateDeviceBreaksConnection(adapter_->GetDevices()[0]);
  753. loop.Run();
  754. // Dispatch write response after disconnection. See above explanation for why
  755. // we don't do this in macOS and WinRT.
  756. #if BUILDFLAG(IS_ANDROID)
  757. SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr);
  758. base::RunLoop().RunUntilIdle();
  759. #endif // BUILDFLAG(IS_ANDROID)
  760. }
  761. // TODO(crbug.com/663131): Enable test on windows when disconnection is
  762. // implemented.
  763. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  764. #define MAYBE_DeprecatedWriteRemoteCharacteristic_Disconnected \
  765. DeprecatedWriteRemoteCharacteristic_Disconnected
  766. #else
  767. #define MAYBE_DeprecatedWriteRemoteCharacteristic_Disconnected \
  768. DISABLED_DeprecatedWriteRemoteCharacteristic_Disconnected
  769. #endif
  770. #if BUILDFLAG(IS_WIN)
  771. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  772. DeprecatedWriteRemoteCharacteristic_Disconnected) {
  773. #else
  774. TEST_F(BluetoothRemoteGattCharacteristicTest,
  775. MAYBE_DeprecatedWriteRemoteCharacteristic_Disconnected) {
  776. #endif
  777. if (!PlatformSupportsLowEnergy()) {
  778. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  779. return;
  780. }
  781. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  782. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  783. std::vector<uint8_t> empty_vector;
  784. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  785. empty_vector, GetCallback(Call::NOT_EXPECTED),
  786. GetGattErrorCallback(Call::EXPECTED));
  787. // Set up for receiving a write response after disconnection.
  788. // On macOS and WinRT no events arrive after disconnection so there is no point
  789. // in building the infrastructure to test this behavior. FYI
  790. // the code CHECKs that responses arrive only when the device is connected.
  791. #if BUILDFLAG(IS_ANDROID)
  792. RememberCharacteristicForSubsequentAction(characteristic1_);
  793. #endif // BUILDFLAG(IS_ANDROID)
  794. ASSERT_EQ(1u, adapter_->GetDevices().size());
  795. SimulateDeviceBreaksConnection(adapter_->GetDevices()[0]);
  796. base::RunLoop().RunUntilIdle();
  797. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  798. // Dispatch write response after disconnection. See above explanation for why
  799. // we don't do this in macOS and WinRT.
  800. #if BUILDFLAG(IS_ANDROID)
  801. SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr);
  802. base::RunLoop().RunUntilIdle();
  803. #endif // BUILDFLAG(IS_ANDROID)
  804. }
  805. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  806. #define MAYBE_ReadRemoteCharacteristic ReadRemoteCharacteristic
  807. #else
  808. #define MAYBE_ReadRemoteCharacteristic DISABLED_ReadRemoteCharacteristic
  809. #endif
  810. // Tests ReadRemoteCharacteristic and GetValue with non-empty value buffer.
  811. #if BUILDFLAG(IS_WIN)
  812. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, ReadRemoteCharacteristic) {
  813. #else
  814. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_ReadRemoteCharacteristic) {
  815. #endif
  816. if (!PlatformSupportsLowEnergy()) {
  817. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  818. return;
  819. }
  820. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  821. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  822. characteristic1_->ReadRemoteCharacteristic(
  823. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  824. std::vector<uint8_t> test_vector = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
  825. SimulateGattCharacteristicRead(characteristic1_, test_vector);
  826. base::RunLoop().RunUntilIdle();
  827. // Duplicate read reported from OS shouldn't cause a problem:
  828. std::vector<uint8_t> empty_vector;
  829. SimulateGattCharacteristicRead(characteristic1_, empty_vector);
  830. base::RunLoop().RunUntilIdle();
  831. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  832. EXPECT_EQ(test_vector, last_read_value_);
  833. EXPECT_EQ(test_vector, characteristic1_->GetValue());
  834. }
  835. // Callback that make sure GattCharacteristicValueChanged has been called
  836. // before the callback runs.
  837. static void TestCallback(
  838. BluetoothRemoteGattCharacteristic::ValueCallback callback,
  839. const TestBluetoothAdapterObserver& callback_observer,
  840. absl::optional<BluetoothGattService::GattErrorCode> error_code,
  841. const std::vector<uint8_t>& value) {
  842. EXPECT_EQ(0, callback_observer.gatt_characteristic_value_changed_count());
  843. std::move(callback).Run(error_code, value);
  844. }
  845. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  846. #define MAYBE_ReadRemoteCharacteristic_GattCharacteristicValueChangedNotCalled \
  847. ReadRemoteCharacteristic_GattCharacteristicValueChangedNotCalled
  848. #else
  849. #define MAYBE_ReadRemoteCharacteristic_GattCharacteristicValueChangedNotCalled \
  850. DISABLED_ReadRemoteCharacteristic_GattCharacteristicValueChangedNotCalled
  851. #endif
  852. // Tests that ReadRemoteCharacteristic doesn't result in a
  853. // GattCharacteristicValueChanged call.
  854. #if BUILDFLAG(IS_WIN)
  855. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  856. ReadRemoteCharacteristic_GattCharacteristicValueChangedNotCalled) {
  857. #else
  858. TEST_F(BluetoothRemoteGattCharacteristicTest,
  859. MAYBE_ReadRemoteCharacteristic_GattCharacteristicValueChangedNotCalled) {
  860. #endif
  861. if (!PlatformSupportsLowEnergy()) {
  862. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  863. return;
  864. }
  865. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  866. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  867. TestBluetoothAdapterObserver observer(adapter_);
  868. characteristic1_->ReadRemoteCharacteristic(base::BindOnce(
  869. TestCallback, GetReadValueCallback(Call::EXPECTED, Result::SUCCESS),
  870. std::cref(observer)));
  871. std::vector<uint8_t> test_vector = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
  872. SimulateGattCharacteristicRead(characteristic1_, test_vector);
  873. base::RunLoop().RunUntilIdle();
  874. EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
  875. // TODO(https://crbug.com/699694): Remove this #if once the bug on Windows is
  876. // fixed.
  877. if (IsClassicWin()) {
  878. EXPECT_FALSE(observer.last_gatt_characteristic_id().empty());
  879. EXPECT_TRUE(observer.last_gatt_characteristic_uuid().IsValid());
  880. } else {
  881. EXPECT_TRUE(observer.last_gatt_characteristic_id().empty());
  882. EXPECT_FALSE(observer.last_gatt_characteristic_uuid().IsValid());
  883. }
  884. EXPECT_TRUE(observer.last_changed_characteristic_value().empty());
  885. }
  886. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  887. #define MAYBE_WriteRemoteCharacteristic WriteRemoteCharacteristic
  888. #else
  889. #define MAYBE_WriteRemoteCharacteristic DISABLED_WriteRemoteCharacteristic
  890. #endif
  891. // Tests WriteRemoteCharacteristic with non-empty value buffer.
  892. #if BUILDFLAG(IS_WIN)
  893. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, WriteRemoteCharacteristic) {
  894. #else
  895. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_WriteRemoteCharacteristic) {
  896. #endif
  897. if (!PlatformSupportsLowEnergy()) {
  898. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  899. return;
  900. }
  901. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  902. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  903. TestBluetoothAdapterObserver observer(adapter_);
  904. base::RunLoop loop;
  905. uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
  906. std::vector<uint8_t> test_vector(values, values + std::size(values));
  907. characteristic1_->WriteRemoteCharacteristic(
  908. test_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  909. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  910. // TODO(crbug.com/653291): remove this if once the bug on windows is
  911. // fixed.
  912. if (!IsClassicWin())
  913. EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
  914. EXPECT_EQ(test_vector, last_write_value_);
  915. loop.Quit();
  916. }),
  917. base::BindLambdaForTesting(
  918. [&](BluetoothGattService::GattErrorCode error_code) {
  919. ADD_FAILURE() << "unexpected error: " << error_code;
  920. loop.Quit();
  921. }));
  922. SimulateGattCharacteristicWrite(characteristic1_);
  923. loop.Run();
  924. }
  925. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  926. #define MAYBE_DeprecatedWriteRemoteCharacteristic \
  927. DeprecatedWriteRemoteCharacteristic
  928. #else
  929. #define MAYBE_DeprecatedWriteRemoteCharacteristic \
  930. DISABLED_DeprecatedWriteRemoteCharacteristic
  931. #endif
  932. // Tests DeprecatedWriteRemoteCharacteristic with non-empty value buffer.
  933. #if BUILDFLAG(IS_WIN)
  934. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  935. DeprecatedWriteRemoteCharacteristic) {
  936. #else
  937. TEST_F(BluetoothRemoteGattCharacteristicTest,
  938. MAYBE_DeprecatedWriteRemoteCharacteristic) {
  939. #endif
  940. if (!PlatformSupportsLowEnergy()) {
  941. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  942. return;
  943. }
  944. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  945. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  946. TestBluetoothAdapterObserver observer(adapter_);
  947. uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
  948. std::vector<uint8_t> test_vector(values, values + std::size(values));
  949. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  950. test_vector, GetCallback(Call::EXPECTED),
  951. GetGattErrorCallback(Call::NOT_EXPECTED));
  952. SimulateGattCharacteristicWrite(characteristic1_);
  953. base::RunLoop().RunUntilIdle();
  954. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  955. // TODO(crbug.com/653291): remove this if once the bug on windows is fixed.
  956. if (!IsClassicWin())
  957. EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
  958. EXPECT_EQ(test_vector, last_write_value_);
  959. }
  960. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  961. #define MAYBE_ReadRemoteCharacteristic_Twice ReadRemoteCharacteristic_Twice
  962. #else
  963. #define MAYBE_ReadRemoteCharacteristic_Twice \
  964. DISABLED_ReadRemoteCharacteristic_Twice
  965. #endif
  966. // Tests ReadRemoteCharacteristic and GetValue multiple times.
  967. #if BUILDFLAG(IS_WIN)
  968. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  969. ReadRemoteCharacteristic_Twice) {
  970. #else
  971. TEST_F(BluetoothRemoteGattCharacteristicTest,
  972. MAYBE_ReadRemoteCharacteristic_Twice) {
  973. #endif
  974. if (!PlatformSupportsLowEnergy()) {
  975. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  976. return;
  977. }
  978. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  979. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  980. characteristic1_->ReadRemoteCharacteristic(
  981. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  982. uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
  983. std::vector<uint8_t> test_vector(values, values + std::size(values));
  984. SimulateGattCharacteristicRead(characteristic1_, test_vector);
  985. base::RunLoop().RunUntilIdle();
  986. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  987. EXPECT_EQ(1, callback_count_);
  988. EXPECT_EQ(0, error_callback_count_);
  989. EXPECT_EQ(test_vector, last_read_value_);
  990. EXPECT_EQ(test_vector, characteristic1_->GetValue());
  991. // Read again, with different value:
  992. ResetEventCounts();
  993. characteristic1_->ReadRemoteCharacteristic(
  994. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  995. std::vector<uint8_t> empty_vector;
  996. SimulateGattCharacteristicRead(characteristic1_, empty_vector);
  997. base::RunLoop().RunUntilIdle();
  998. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  999. EXPECT_EQ(1, callback_count_);
  1000. EXPECT_EQ(0, error_callback_count_);
  1001. EXPECT_EQ(empty_vector, last_read_value_);
  1002. EXPECT_EQ(empty_vector, characteristic1_->GetValue());
  1003. }
  1004. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1005. #define MAYBE_WriteRemoteCharacteristic_Twice WriteRemoteCharacteristic_Twice
  1006. #else
  1007. #define MAYBE_WriteRemoteCharacteristic_Twice \
  1008. DISABLED_WriteRemoteCharacteristic_Twice
  1009. #endif
  1010. // Tests WriteRemoteCharacteristic multiple times.
  1011. #if BUILDFLAG(IS_WIN)
  1012. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1013. WriteRemoteCharacteristic_Twice) {
  1014. #else
  1015. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1016. MAYBE_WriteRemoteCharacteristic_Twice) {
  1017. #endif
  1018. if (!PlatformSupportsLowEnergy()) {
  1019. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1020. return;
  1021. }
  1022. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1023. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1024. base::RunLoop loop1;
  1025. uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
  1026. std::vector<uint8_t> test_vector(values, values + std::size(values));
  1027. characteristic1_->WriteRemoteCharacteristic(
  1028. test_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1029. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1030. EXPECT_EQ(test_vector, last_write_value_);
  1031. loop1.Quit();
  1032. }),
  1033. base::BindLambdaForTesting(
  1034. [&](BluetoothGattService::GattErrorCode error_code) {
  1035. ADD_FAILURE() << "unexpected error" << error_code;
  1036. loop1.Quit();
  1037. }));
  1038. SimulateGattCharacteristicWrite(characteristic1_);
  1039. loop1.Run();
  1040. // Write again, with different value:
  1041. ResetEventCounts();
  1042. base::RunLoop loop2;
  1043. std::vector<uint8_t> empty_vector;
  1044. characteristic1_->WriteRemoteCharacteristic(
  1045. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1046. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1047. EXPECT_EQ(empty_vector, last_write_value_);
  1048. loop2.Quit();
  1049. }),
  1050. base::BindLambdaForTesting(
  1051. [&](BluetoothGattService::GattErrorCode error_code) {
  1052. ADD_FAILURE() << "unexpected error" << error_code;
  1053. loop2.Quit();
  1054. }));
  1055. SimulateGattCharacteristicWrite(characteristic1_);
  1056. loop2.Run();
  1057. }
  1058. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1059. #define MAYBE_DeprecatedWriteRemoteCharacteristic_Twice \
  1060. DeprecatedWriteRemoteCharacteristic_Twice
  1061. #else
  1062. #define MAYBE_DeprecatedWriteRemoteCharacteristic_Twice \
  1063. DISABLED_DeprecatedWriteRemoteCharacteristic_Twice
  1064. #endif
  1065. // Tests DeprecatedWriteRemoteCharacteristic multiple times.
  1066. #if BUILDFLAG(IS_WIN)
  1067. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1068. DeprecatedWriteRemoteCharacteristic_Twice) {
  1069. #else
  1070. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1071. MAYBE_DeprecatedWriteRemoteCharacteristic_Twice) {
  1072. #endif
  1073. if (!PlatformSupportsLowEnergy()) {
  1074. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1075. return;
  1076. }
  1077. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1078. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1079. uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff};
  1080. std::vector<uint8_t> test_vector(values, values + std::size(values));
  1081. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1082. test_vector, GetCallback(Call::EXPECTED),
  1083. GetGattErrorCallback(Call::NOT_EXPECTED));
  1084. SimulateGattCharacteristicWrite(characteristic1_);
  1085. base::RunLoop().RunUntilIdle();
  1086. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1087. EXPECT_EQ(1, callback_count_);
  1088. EXPECT_EQ(0, error_callback_count_);
  1089. EXPECT_EQ(test_vector, last_write_value_);
  1090. // Write again, with different value:
  1091. ResetEventCounts();
  1092. std::vector<uint8_t> empty_vector;
  1093. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1094. empty_vector, GetCallback(Call::EXPECTED),
  1095. GetGattErrorCallback(Call::NOT_EXPECTED));
  1096. SimulateGattCharacteristicWrite(characteristic1_);
  1097. base::RunLoop().RunUntilIdle();
  1098. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1099. EXPECT_EQ(1, callback_count_);
  1100. EXPECT_EQ(0, error_callback_count_);
  1101. EXPECT_EQ(empty_vector, last_write_value_);
  1102. }
  1103. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1104. #define MAYBE_ReadRemoteCharacteristic_MultipleCharacteristics \
  1105. ReadRemoteCharacteristic_MultipleCharacteristics
  1106. #else
  1107. #define MAYBE_ReadRemoteCharacteristic_MultipleCharacteristics \
  1108. DISABLED_ReadRemoteCharacteristic_MultipleCharacteristics
  1109. #endif
  1110. // Tests ReadRemoteCharacteristic on two characteristics.
  1111. #if BUILDFLAG(IS_WIN)
  1112. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1113. ReadRemoteCharacteristic_MultipleCharacteristics) {
  1114. #else
  1115. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1116. MAYBE_ReadRemoteCharacteristic_MultipleCharacteristics) {
  1117. #endif
  1118. if (!PlatformSupportsLowEnergy()) {
  1119. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1120. return;
  1121. }
  1122. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1123. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  1124. characteristic1_->ReadRemoteCharacteristic(
  1125. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  1126. characteristic2_->ReadRemoteCharacteristic(
  1127. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  1128. EXPECT_EQ(0, callback_count_);
  1129. EXPECT_EQ(0, error_callback_count_);
  1130. std::vector<uint8_t> test_vector1;
  1131. test_vector1.push_back(111);
  1132. SimulateGattCharacteristicRead(characteristic1_, test_vector1);
  1133. base::RunLoop().RunUntilIdle();
  1134. EXPECT_EQ(test_vector1, last_read_value_);
  1135. std::vector<uint8_t> test_vector2;
  1136. test_vector2.push_back(222);
  1137. SimulateGattCharacteristicRead(characteristic2_, test_vector2);
  1138. base::RunLoop().RunUntilIdle();
  1139. EXPECT_EQ(test_vector2, last_read_value_);
  1140. EXPECT_EQ(2, gatt_read_characteristic_attempts_);
  1141. EXPECT_EQ(2, callback_count_);
  1142. EXPECT_EQ(0, error_callback_count_);
  1143. EXPECT_EQ(test_vector1, characteristic1_->GetValue());
  1144. EXPECT_EQ(test_vector2, characteristic2_->GetValue());
  1145. }
  1146. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1147. #define MAYBE_WriteRemoteCharacteristic_MultipleCharacteristics \
  1148. WriteRemoteCharacteristic_MultipleCharacteristics
  1149. #else
  1150. #define MAYBE_WriteRemoteCharacteristic_MultipleCharacteristics \
  1151. DISABLED_WriteRemoteCharacteristic_MultipleCharacteristics
  1152. #endif
  1153. // Tests WriteRemoteCharacteristic on two characteristics.
  1154. #if BUILDFLAG(IS_WIN)
  1155. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1156. WriteRemoteCharacteristic_MultipleCharacteristics) {
  1157. #else
  1158. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1159. MAYBE_WriteRemoteCharacteristic_MultipleCharacteristics) {
  1160. #endif
  1161. if (!PlatformSupportsLowEnergy()) {
  1162. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1163. return;
  1164. }
  1165. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1166. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1167. base::RunLoop loop1;
  1168. std::vector<uint8_t> test_vector1;
  1169. test_vector1.push_back(111);
  1170. characteristic1_->WriteRemoteCharacteristic(
  1171. test_vector1, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1172. if (IsClassicWin()) {
  1173. EXPECT_EQ(test_vector1, last_write_value_);
  1174. }
  1175. loop1.Quit();
  1176. }),
  1177. base::BindLambdaForTesting(
  1178. [&](BluetoothGattService::GattErrorCode error_code) {
  1179. ADD_FAILURE() << "unexpected error" << error_code;
  1180. loop1.Quit();
  1181. }));
  1182. if (!IsClassicWin()) {
  1183. EXPECT_EQ(test_vector1, last_write_value_);
  1184. }
  1185. base::RunLoop loop2;
  1186. std::vector<uint8_t> test_vector2;
  1187. test_vector2.push_back(222);
  1188. characteristic2_->WriteRemoteCharacteristic(
  1189. test_vector2, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1190. if (IsClassicWin()) {
  1191. EXPECT_EQ(test_vector2, last_write_value_);
  1192. }
  1193. loop2.Quit();
  1194. }),
  1195. base::BindLambdaForTesting(
  1196. [&](BluetoothGattService::GattErrorCode error_code) {
  1197. ADD_FAILURE() << "unexpected error" << error_code;
  1198. loop2.Quit();
  1199. }));
  1200. if (!IsClassicWin()) {
  1201. EXPECT_EQ(test_vector2, last_write_value_);
  1202. }
  1203. SimulateGattCharacteristicWrite(characteristic1_);
  1204. loop1.Run();
  1205. SimulateGattCharacteristicWrite(characteristic2_);
  1206. loop2.Run();
  1207. }
  1208. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1209. #define MAYBE_DeprecatedWriteRemoteCharacteristic_MultipleCharacteristics \
  1210. DeprecatedWriteRemoteCharacteristic_MultipleCharacteristics
  1211. #else
  1212. #define MAYBE_DeprecatedWriteRemoteCharacteristic_MultipleCharacteristics \
  1213. DISABLED_DeprecatedWriteRemoteCharacteristic_MultipleCharacteristics
  1214. #endif
  1215. // Tests DeprecatedWriteRemoteCharacteristic on two characteristics.
  1216. #if BUILDFLAG(IS_WIN)
  1217. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1218. DeprecatedWriteRemoteCharacteristic_MultipleCharacteristics) {
  1219. #else
  1220. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1221. MAYBE_DeprecatedWriteRemoteCharacteristic_MultipleCharacteristics) {
  1222. #endif
  1223. if (!PlatformSupportsLowEnergy()) {
  1224. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1225. return;
  1226. }
  1227. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1228. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1229. std::vector<uint8_t> test_vector1;
  1230. test_vector1.push_back(111);
  1231. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1232. test_vector1, GetCallback(Call::EXPECTED),
  1233. GetGattErrorCallback(Call::NOT_EXPECTED));
  1234. if (!IsClassicWin())
  1235. EXPECT_EQ(test_vector1, last_write_value_);
  1236. std::vector<uint8_t> test_vector2;
  1237. test_vector2.push_back(222);
  1238. characteristic2_->DeprecatedWriteRemoteCharacteristic(
  1239. test_vector2, GetCallback(Call::EXPECTED),
  1240. GetGattErrorCallback(Call::NOT_EXPECTED));
  1241. if (!IsClassicWin())
  1242. EXPECT_EQ(test_vector2, last_write_value_);
  1243. EXPECT_EQ(0, callback_count_);
  1244. EXPECT_EQ(0, error_callback_count_);
  1245. SimulateGattCharacteristicWrite(characteristic1_);
  1246. base::RunLoop().RunUntilIdle();
  1247. if (IsClassicWin())
  1248. EXPECT_EQ(test_vector1, last_write_value_);
  1249. SimulateGattCharacteristicWrite(characteristic2_);
  1250. base::RunLoop().RunUntilIdle();
  1251. if (IsClassicWin())
  1252. EXPECT_EQ(test_vector2, last_write_value_);
  1253. EXPECT_EQ(2, gatt_write_characteristic_attempts_);
  1254. EXPECT_EQ(2, callback_count_);
  1255. EXPECT_EQ(0, error_callback_count_);
  1256. // TODO(591740): Remove if define for OS_ANDROID in this test.
  1257. }
  1258. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1259. #define MAYBE_RemoteCharacteristic_Nested_Read_Read \
  1260. RemoteCharacteristic_Nested_Read_Read
  1261. #else
  1262. #define MAYBE_RemoteCharacteristic_Nested_Read_Read \
  1263. DISABLED_RemoteCharacteristic_Nested_Read_Read
  1264. #endif
  1265. // Tests a nested ReadRemoteCharacteristic from within another
  1266. // ReadRemoteCharacteristic.
  1267. #if BUILDFLAG(IS_WIN)
  1268. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1269. RemoteCharacteristic_Nested_Read_Read) {
  1270. #else
  1271. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1272. MAYBE_RemoteCharacteristic_Nested_Read_Read) {
  1273. #endif
  1274. if (!PlatformSupportsLowEnergy()) {
  1275. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1276. return;
  1277. }
  1278. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1279. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  1280. std::vector<uint8_t> test_vector_1 = {0, 1, 2, 3, 4};
  1281. std::vector<uint8_t> test_vector_2 = {0xf, 0xf0, 0xff};
  1282. characteristic1_->ReadRemoteCharacteristic(base::BindLambdaForTesting(
  1283. [&](absl::optional<BluetoothGattService::GattErrorCode> error_code,
  1284. const std::vector<uint8_t>& data) {
  1285. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS)
  1286. .Run(error_code, data);
  1287. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  1288. EXPECT_EQ(1, callback_count_);
  1289. EXPECT_EQ(0, error_callback_count_);
  1290. EXPECT_EQ(test_vector_1, last_read_value_);
  1291. EXPECT_EQ(test_vector_1, characteristic1_->GetValue());
  1292. characteristic1_->ReadRemoteCharacteristic(
  1293. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  1294. SimulateGattCharacteristicRead(characteristic1_, test_vector_2);
  1295. }));
  1296. SimulateGattCharacteristicRead(characteristic1_, test_vector_1);
  1297. base::RunLoop().RunUntilIdle();
  1298. EXPECT_EQ(2, gatt_read_characteristic_attempts_);
  1299. EXPECT_EQ(2, callback_count_);
  1300. EXPECT_EQ(0, error_callback_count_);
  1301. EXPECT_EQ(test_vector_2, last_read_value_);
  1302. EXPECT_EQ(test_vector_2, characteristic1_->GetValue());
  1303. }
  1304. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1305. #define MAYBE_RemoteCharacteristic_Nested_Write_Write \
  1306. RemoteCharacteristic_Nested_Write_Write
  1307. #else
  1308. #define MAYBE_RemoteCharacteristic_Nested_Write_Write \
  1309. DISABLED_RemoteCharacteristic_Nested_Write_Write
  1310. #endif
  1311. // Tests a nested WriteRemoteCharacteristic from within another
  1312. // WriteRemoteCharacteristic.
  1313. #if BUILDFLAG(IS_WIN)
  1314. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1315. RemoteCharacteristic_Nested_Write_Write) {
  1316. #else
  1317. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1318. MAYBE_RemoteCharacteristic_Nested_Write_Write) {
  1319. #endif
  1320. if (!PlatformSupportsLowEnergy()) {
  1321. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1322. return;
  1323. }
  1324. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1325. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1326. base::RunLoop loop;
  1327. std::vector<uint8_t> test_vector_1 = {0, 1, 2, 3, 4};
  1328. std::vector<uint8_t> test_vector_2 = {0xf, 0xf0, 0xff};
  1329. characteristic1_->WriteRemoteCharacteristic(
  1330. test_vector_1, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1331. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1332. EXPECT_EQ(test_vector_1, last_write_value_);
  1333. characteristic1_->WriteRemoteCharacteristic(
  1334. test_vector_2, WriteType::kWithResponse,
  1335. base::BindLambdaForTesting([&] {
  1336. EXPECT_EQ(2, gatt_write_characteristic_attempts_);
  1337. EXPECT_EQ(test_vector_2, last_write_value_);
  1338. loop.Quit();
  1339. }),
  1340. base::BindLambdaForTesting(
  1341. [&](BluetoothGattService::GattErrorCode error_code) {
  1342. ADD_FAILURE() << "unexpected error: " << error_code;
  1343. loop.Quit();
  1344. }));
  1345. SimulateGattCharacteristicWrite(characteristic1_);
  1346. }),
  1347. base::BindLambdaForTesting(
  1348. [&](BluetoothGattService::GattErrorCode error_code) {
  1349. ADD_FAILURE() << "unexpected error: " << error_code;
  1350. loop.Quit();
  1351. }));
  1352. SimulateGattCharacteristicWrite(characteristic1_);
  1353. loop.Run();
  1354. }
  1355. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1356. #define MAYBE_RemoteCharacteristic_Nested_DeprecatedWrite_DeprecatedWrite \
  1357. RemoteCharacteristic_Nested_DeprecatedWrite_DeprecatedWrite
  1358. #else
  1359. #define MAYBE_RemoteCharacteristic_Nested_DeprecatedWrite_DeprecatedWrite \
  1360. DISABLED_RemoteCharacteristic_Nested_DeprecatedWrite_DeprecatedWrite
  1361. #endif
  1362. // Tests a nested DeprecatedWriteRemoteCharacteristic from within another
  1363. // DeprecatedWriteRemoteCharacteristic.
  1364. #if BUILDFLAG(IS_WIN)
  1365. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1366. RemoteCharacteristic_Nested_DeprecatedWrite_DeprecatedWrite) {
  1367. #else
  1368. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1369. MAYBE_RemoteCharacteristic_Nested_DeprecatedWrite_DeprecatedWrite) {
  1370. #endif
  1371. if (!PlatformSupportsLowEnergy()) {
  1372. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1373. return;
  1374. }
  1375. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1376. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1377. std::vector<uint8_t> test_vector_1 = {0, 1, 2, 3, 4};
  1378. std::vector<uint8_t> test_vector_2 = {0xf, 0xf0, 0xff};
  1379. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1380. test_vector_1, base::BindLambdaForTesting([&] {
  1381. GetCallback(Call::EXPECTED).Run();
  1382. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1383. EXPECT_EQ(1, callback_count_);
  1384. EXPECT_EQ(0, error_callback_count_);
  1385. EXPECT_EQ(test_vector_1, last_write_value_);
  1386. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1387. test_vector_2, GetCallback(Call::EXPECTED),
  1388. GetGattErrorCallback(Call::NOT_EXPECTED));
  1389. SimulateGattCharacteristicWrite(characteristic1_);
  1390. }),
  1391. GetGattErrorCallback(Call::NOT_EXPECTED));
  1392. SimulateGattCharacteristicWrite(characteristic1_);
  1393. base::RunLoop().RunUntilIdle();
  1394. EXPECT_EQ(2, gatt_write_characteristic_attempts_);
  1395. EXPECT_EQ(2, callback_count_);
  1396. EXPECT_EQ(0, error_callback_count_);
  1397. EXPECT_EQ(test_vector_2, last_write_value_);
  1398. }
  1399. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1400. #define MAYBE_RemoteCharacteristic_Nested_Read_Write \
  1401. RemoteCharacteristic_Nested_Read_Write
  1402. #else
  1403. #define MAYBE_RemoteCharacteristic_Nested_Read_Write \
  1404. DISABLED_RemoteCharacteristic_Nested_Read_Write
  1405. #endif
  1406. // Tests a nested WriteRemoteCharacteristic from within a
  1407. // ReadRemoteCharacteristic.
  1408. #if BUILDFLAG(IS_WIN)
  1409. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1410. RemoteCharacteristic_Nested_Read_Write) {
  1411. #else
  1412. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1413. MAYBE_RemoteCharacteristic_Nested_Read_Write) {
  1414. #endif
  1415. if (!PlatformSupportsLowEnergy()) {
  1416. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1417. return;
  1418. }
  1419. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1420. BluetoothRemoteGattCharacteristic::PROPERTY_READ |
  1421. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1422. base::RunLoop loop;
  1423. std::vector<uint8_t> test_vector_1 = {0, 1, 2, 3, 4};
  1424. std::vector<uint8_t> test_vector_2 = {0xf, 0xf0, 0xff};
  1425. characteristic1_->ReadRemoteCharacteristic(base::BindLambdaForTesting(
  1426. [&](absl::optional<BluetoothGattService::GattErrorCode> error_code,
  1427. const std::vector<uint8_t>& data) {
  1428. ASSERT_FALSE(error_code.has_value())
  1429. << "unexpected error: " << error_code.value();
  1430. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  1431. EXPECT_EQ(0, gatt_write_characteristic_attempts_);
  1432. EXPECT_EQ(test_vector_1, data);
  1433. EXPECT_EQ(test_vector_1, characteristic1_->GetValue());
  1434. characteristic1_->WriteRemoteCharacteristic(
  1435. test_vector_2, WriteType::kWithResponse,
  1436. base::BindLambdaForTesting([&] {
  1437. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  1438. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1439. EXPECT_EQ(test_vector_2, last_write_value_);
  1440. loop.Quit();
  1441. }),
  1442. base::BindLambdaForTesting(
  1443. [&](BluetoothGattService::GattErrorCode error_code) {
  1444. ADD_FAILURE() << "unexpected error: " << error_code;
  1445. loop.Quit();
  1446. }));
  1447. SimulateGattCharacteristicWrite(characteristic1_);
  1448. }));
  1449. SimulateGattCharacteristicRead(characteristic1_, test_vector_1);
  1450. loop.Run();
  1451. }
  1452. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1453. #define MAYBE_RemoteCharacteristic_Nested_Read_DeprecatedWrite \
  1454. RemoteCharacteristic_Nested_Read_DeprecatedWrite
  1455. #else
  1456. #define MAYBE_RemoteCharacteristic_Nested_Read_DeprecatedWrite \
  1457. DISABLED_RemoteCharacteristic_Nested_Read_DeprecatedWrite
  1458. #endif
  1459. // Tests a nested DeprecatedWriteRemoteCharacteristic from within a
  1460. // ReadRemoteCharacteristic.
  1461. #if BUILDFLAG(IS_WIN)
  1462. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1463. RemoteCharacteristic_Nested_Read_DeprecatedWrite) {
  1464. #else
  1465. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1466. MAYBE_RemoteCharacteristic_Nested_Read_DeprecatedWrite) {
  1467. #endif
  1468. if (!PlatformSupportsLowEnergy()) {
  1469. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1470. return;
  1471. }
  1472. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1473. BluetoothRemoteGattCharacteristic::PROPERTY_READ |
  1474. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1475. std::vector<uint8_t> test_vector_1 = {0, 1, 2, 3, 4};
  1476. std::vector<uint8_t> test_vector_2 = {0xf, 0xf0, 0xff};
  1477. characteristic1_->ReadRemoteCharacteristic(base::BindLambdaForTesting(
  1478. [&](absl::optional<BluetoothGattService::GattErrorCode> error_code,
  1479. const std::vector<uint8_t>& data) {
  1480. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS)
  1481. .Run(error_code, data);
  1482. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  1483. EXPECT_EQ(0, gatt_write_characteristic_attempts_);
  1484. EXPECT_EQ(1, callback_count_);
  1485. EXPECT_EQ(0, error_callback_count_);
  1486. EXPECT_EQ(test_vector_1, last_read_value_);
  1487. EXPECT_EQ(test_vector_1, characteristic1_->GetValue());
  1488. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1489. test_vector_2, GetCallback(Call::EXPECTED),
  1490. GetGattErrorCallback(Call::NOT_EXPECTED));
  1491. SimulateGattCharacteristicWrite(characteristic1_);
  1492. }));
  1493. SimulateGattCharacteristicRead(characteristic1_, test_vector_1);
  1494. base::RunLoop().RunUntilIdle();
  1495. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  1496. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1497. EXPECT_EQ(2, callback_count_);
  1498. EXPECT_EQ(0, error_callback_count_);
  1499. EXPECT_EQ(test_vector_2, last_write_value_);
  1500. }
  1501. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1502. #define MAYBE_RemoteCharacteristic_Nested_Write_Read \
  1503. RemoteCharacteristic_Nested_Write_Read
  1504. #else
  1505. #define MAYBE_RemoteCharacteristic_Nested_Write_Read \
  1506. DISABLED_RemoteCharacteristic_Nested_Write_Read
  1507. #endif
  1508. // Tests a nested ReadRemoteCharacteristic from within a
  1509. // WriteRemoteCharacteristic.
  1510. #if BUILDFLAG(IS_WIN)
  1511. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1512. RemoteCharacteristic_Nested_Write_Read) {
  1513. #else
  1514. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1515. MAYBE_RemoteCharacteristic_Nested_Write_Read) {
  1516. #endif
  1517. if (!PlatformSupportsLowEnergy()) {
  1518. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1519. return;
  1520. }
  1521. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1522. BluetoothRemoteGattCharacteristic::PROPERTY_READ |
  1523. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1524. base::RunLoop loop;
  1525. std::vector<uint8_t> test_vector_1 = {0, 1, 2, 3, 4};
  1526. std::vector<uint8_t> test_vector_2 = {0xf, 0xf0, 0xff};
  1527. characteristic1_->WriteRemoteCharacteristic(
  1528. test_vector_1, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1529. EXPECT_EQ(0, gatt_read_characteristic_attempts_);
  1530. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1531. EXPECT_EQ(test_vector_1, last_write_value_);
  1532. characteristic1_->ReadRemoteCharacteristic(base::BindLambdaForTesting(
  1533. [&](absl::optional<BluetoothGattService::GattErrorCode> error_code,
  1534. const std::vector<uint8_t>& data) {
  1535. EXPECT_EQ(error_code, absl::nullopt);
  1536. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  1537. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1538. EXPECT_EQ(test_vector_2, data);
  1539. EXPECT_EQ(test_vector_2, characteristic1_->GetValue());
  1540. loop.Quit();
  1541. }));
  1542. SimulateGattCharacteristicRead(characteristic1_, test_vector_2);
  1543. }),
  1544. base::BindLambdaForTesting(
  1545. [&](BluetoothGattService::GattErrorCode error_code) {
  1546. ADD_FAILURE() << "unexpected error: " << error_code;
  1547. loop.Quit();
  1548. }));
  1549. SimulateGattCharacteristicWrite(characteristic1_);
  1550. loop.Run();
  1551. }
  1552. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1553. #define MAYBE_RemoteCharacteristic_Nested_DeprecatedWrite_Read \
  1554. RemoteCharacteristic_Nested_DeprecatedWrite_Read
  1555. #else
  1556. #define MAYBE_RemoteCharacteristic_Nested_DeprecatedWrite_Read \
  1557. DISABLED_RemoteCharacteristic_Nested_DeprecatedWrite_Read
  1558. #endif
  1559. // Tests a nested ReadRemoteCharacteristic from within a
  1560. // DeprecatedWriteRemoteCharacteristic.
  1561. #if BUILDFLAG(IS_WIN)
  1562. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1563. RemoteCharacteristic_Nested_DeprecatedWrite_Read) {
  1564. #else
  1565. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1566. MAYBE_RemoteCharacteristic_Nested_DeprecatedWrite_Read) {
  1567. #endif
  1568. if (!PlatformSupportsLowEnergy()) {
  1569. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1570. return;
  1571. }
  1572. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1573. BluetoothRemoteGattCharacteristic::PROPERTY_READ |
  1574. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1575. std::vector<uint8_t> test_vector_1 = {0, 1, 2, 3, 4};
  1576. std::vector<uint8_t> test_vector_2 = {0xf, 0xf0, 0xff};
  1577. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1578. test_vector_1, base::BindLambdaForTesting([&] {
  1579. GetCallback(Call::EXPECTED).Run();
  1580. EXPECT_EQ(0, gatt_read_characteristic_attempts_);
  1581. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1582. EXPECT_EQ(1, callback_count_);
  1583. EXPECT_EQ(0, error_callback_count_);
  1584. EXPECT_EQ(test_vector_1, last_write_value_);
  1585. characteristic1_->ReadRemoteCharacteristic(
  1586. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  1587. SimulateGattCharacteristicRead(characteristic1_, test_vector_2);
  1588. }),
  1589. GetGattErrorCallback(Call::NOT_EXPECTED));
  1590. SimulateGattCharacteristicWrite(characteristic1_);
  1591. base::RunLoop().RunUntilIdle();
  1592. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  1593. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1594. EXPECT_EQ(2, callback_count_);
  1595. EXPECT_EQ(0, error_callback_count_);
  1596. EXPECT_EQ(test_vector_2, last_read_value_);
  1597. EXPECT_EQ(test_vector_2, characteristic1_->GetValue());
  1598. }
  1599. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1600. #define MAYBE_ReadError ReadError
  1601. #else
  1602. #define MAYBE_ReadError DISABLED_ReadError
  1603. #endif
  1604. // Tests ReadRemoteCharacteristic asynchronous error.
  1605. #if BUILDFLAG(IS_WIN)
  1606. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, ReadError) {
  1607. #else
  1608. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_ReadError) {
  1609. #endif
  1610. if (!PlatformSupportsLowEnergy()) {
  1611. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1612. return;
  1613. }
  1614. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1615. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  1616. TestBluetoothAdapterObserver observer(adapter_);
  1617. characteristic1_->ReadRemoteCharacteristic(
  1618. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  1619. SimulateGattCharacteristicReadError(
  1620. characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH);
  1621. SimulateGattCharacteristicReadError(characteristic1_,
  1622. BluetoothGattService::GATT_ERROR_FAILED);
  1623. base::RunLoop().RunUntilIdle();
  1624. EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH,
  1625. last_gatt_error_code_);
  1626. EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
  1627. }
  1628. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1629. #define MAYBE_WriteError WriteError
  1630. #else
  1631. #define MAYBE_WriteError DISABLED_WriteError
  1632. #endif
  1633. // Tests WriteRemoteCharacteristic asynchronous error.
  1634. #if BUILDFLAG(IS_WIN)
  1635. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, WriteError) {
  1636. #else
  1637. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_WriteError) {
  1638. #endif
  1639. if (!PlatformSupportsLowEnergy()) {
  1640. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1641. return;
  1642. }
  1643. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1644. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1645. base::RunLoop loop;
  1646. std::vector<uint8_t> empty_vector;
  1647. characteristic1_->WriteRemoteCharacteristic(
  1648. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1649. ADD_FAILURE() << "unexpected success";
  1650. loop.Quit();
  1651. }),
  1652. base::BindLambdaForTesting(
  1653. [&](BluetoothGattService::GattErrorCode error_code) {
  1654. EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH,
  1655. error_code);
  1656. loop.Quit();
  1657. }));
  1658. SimulateGattCharacteristicWriteError(
  1659. characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH);
  1660. // Only the error above should be reported to the caller.
  1661. SimulateGattCharacteristicWriteError(characteristic1_,
  1662. BluetoothGattService::GATT_ERROR_FAILED);
  1663. loop.Run();
  1664. }
  1665. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1666. #define MAYBE_DeprecatedWriteError DeprecatedWriteError
  1667. #else
  1668. #define MAYBE_DeprecatedWriteError DISABLED_DeprecatedWriteError
  1669. #endif
  1670. // Tests DeprecatedWriteRemoteCharacteristic asynchronous error.
  1671. #if BUILDFLAG(IS_WIN)
  1672. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, DeprecatedWriteError) {
  1673. #else
  1674. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_DeprecatedWriteError) {
  1675. #endif
  1676. if (!PlatformSupportsLowEnergy()) {
  1677. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1678. return;
  1679. }
  1680. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1681. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1682. std::vector<uint8_t> empty_vector;
  1683. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1684. empty_vector, GetCallback(Call::NOT_EXPECTED),
  1685. GetGattErrorCallback(Call::EXPECTED));
  1686. SimulateGattCharacteristicWriteError(
  1687. characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH);
  1688. SimulateGattCharacteristicWriteError(characteristic1_,
  1689. BluetoothGattService::GATT_ERROR_FAILED);
  1690. base::RunLoop().RunUntilIdle();
  1691. EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH,
  1692. last_gatt_error_code_);
  1693. }
  1694. #if BUILDFLAG(IS_ANDROID)
  1695. #define MAYBE_ReadSynchronousError ReadSynchronousError
  1696. #else
  1697. #define MAYBE_ReadSynchronousError DISABLED_ReadSynchronousError
  1698. #endif
  1699. // Tests ReadRemoteCharacteristic synchronous error.
  1700. // Test not relevant for macOS and WinRT since characteristic read cannot
  1701. // generate synchronous error.
  1702. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_ReadSynchronousError) {
  1703. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
  1704. SimulateGattCharacteristicReadWillFailSynchronouslyOnce(characteristic1_);
  1705. characteristic1_->ReadRemoteCharacteristic(
  1706. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  1707. EXPECT_EQ(0, gatt_read_characteristic_attempts_);
  1708. base::RunLoop().RunUntilIdle();
  1709. EXPECT_EQ(0, callback_count_);
  1710. EXPECT_EQ(1, error_callback_count_);
  1711. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  1712. // After failing once, can succeed:
  1713. ResetEventCounts();
  1714. characteristic1_->ReadRemoteCharacteristic(
  1715. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  1716. EXPECT_EQ(1, gatt_read_characteristic_attempts_);
  1717. std::vector<uint8_t> empty_vector;
  1718. SimulateGattCharacteristicRead(characteristic1_, empty_vector);
  1719. base::RunLoop().RunUntilIdle();
  1720. EXPECT_EQ(1, callback_count_);
  1721. EXPECT_EQ(0, error_callback_count_);
  1722. }
  1723. #if BUILDFLAG(IS_ANDROID)
  1724. #define MAYBE_WriteSynchronousError WriteSynchronousError
  1725. #else
  1726. #define MAYBE_WriteSynchronousError DISABLED_WriteSynchronousError
  1727. #endif
  1728. // Tests WriteRemoteCharacteristic synchronous error.
  1729. // This test doesn't apply to macOS and WinRT since a synchronous API does not
  1730. // exist.
  1731. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_WriteSynchronousError) {
  1732. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
  1733. SimulateGattCharacteristicWriteWillFailSynchronouslyOnce(characteristic1_);
  1734. base::RunLoop loop1;
  1735. std::vector<uint8_t> empty_vector;
  1736. characteristic1_->WriteRemoteCharacteristic(
  1737. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1738. ADD_FAILURE() << "unexpected success";
  1739. loop1.Quit();
  1740. }),
  1741. base::BindLambdaForTesting(
  1742. [&](BluetoothGattService::GattErrorCode error_code) {
  1743. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  1744. loop1.Quit();
  1745. }));
  1746. loop1.Run();
  1747. // After failing once, can succeed:
  1748. ResetEventCounts();
  1749. base::RunLoop loop2;
  1750. characteristic1_->WriteRemoteCharacteristic(
  1751. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1752. SUCCEED();
  1753. loop2.Quit();
  1754. }),
  1755. base::BindLambdaForTesting(
  1756. [&](BluetoothGattService::GattErrorCode error_code) {
  1757. ADD_FAILURE() << "unexpected error: " << error_code;
  1758. loop2.Quit();
  1759. }));
  1760. SimulateGattCharacteristicWrite(characteristic1_);
  1761. loop2.Run();
  1762. }
  1763. #if BUILDFLAG(IS_ANDROID)
  1764. #define MAYBE_DeprecatedWriteSynchronousError DeprecatedWriteSynchronousError
  1765. #else
  1766. #define MAYBE_DeprecatedWriteSynchronousError \
  1767. DISABLED_DeprecatedWriteSynchronousError
  1768. #endif
  1769. // Tests DeprecatedWriteRemoteCharacteristic synchronous error.
  1770. // This test doesn't apply to macOS and WinRT since a synchronous API does not
  1771. // exist.
  1772. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1773. MAYBE_DeprecatedWriteSynchronousError) {
  1774. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
  1775. SimulateGattCharacteristicWriteWillFailSynchronouslyOnce(characteristic1_);
  1776. std::vector<uint8_t> empty_vector;
  1777. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1778. empty_vector, GetCallback(Call::NOT_EXPECTED),
  1779. GetGattErrorCallback(Call::EXPECTED));
  1780. EXPECT_EQ(0, gatt_write_characteristic_attempts_);
  1781. base::RunLoop().RunUntilIdle();
  1782. EXPECT_EQ(0, callback_count_);
  1783. EXPECT_EQ(1, error_callback_count_);
  1784. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  1785. // After failing once, can succeed:
  1786. ResetEventCounts();
  1787. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1788. empty_vector, GetCallback(Call::EXPECTED),
  1789. GetGattErrorCallback(Call::NOT_EXPECTED));
  1790. EXPECT_EQ(1, gatt_write_characteristic_attempts_);
  1791. SimulateGattCharacteristicWrite(characteristic1_);
  1792. base::RunLoop().RunUntilIdle();
  1793. EXPECT_EQ(1, callback_count_);
  1794. EXPECT_EQ(0, error_callback_count_);
  1795. }
  1796. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1797. #define MAYBE_ReadRemoteCharacteristic_ReadPending \
  1798. ReadRemoteCharacteristic_ReadPending
  1799. #else
  1800. #define MAYBE_ReadRemoteCharacteristic_ReadPending \
  1801. DISABLED_ReadRemoteCharacteristic_ReadPending
  1802. #endif
  1803. // Tests ReadRemoteCharacteristic error with a pending read operation.
  1804. #if BUILDFLAG(IS_WIN)
  1805. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1806. ReadRemoteCharacteristic_ReadPending) {
  1807. #else
  1808. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1809. MAYBE_ReadRemoteCharacteristic_ReadPending) {
  1810. #endif
  1811. if (!PlatformSupportsLowEnergy()) {
  1812. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1813. return;
  1814. }
  1815. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1816. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  1817. characteristic1_->ReadRemoteCharacteristic(
  1818. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  1819. characteristic1_->ReadRemoteCharacteristic(
  1820. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  1821. base::RunLoop().RunUntilIdle();
  1822. EXPECT_EQ(0, callback_count_);
  1823. EXPECT_EQ(1, error_callback_count_);
  1824. EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
  1825. last_gatt_error_code_);
  1826. // Initial read should still succeed:
  1827. ResetEventCounts();
  1828. std::vector<uint8_t> empty_vector;
  1829. SimulateGattCharacteristicRead(characteristic1_, empty_vector);
  1830. base::RunLoop().RunUntilIdle();
  1831. EXPECT_EQ(1, callback_count_);
  1832. EXPECT_EQ(0, error_callback_count_);
  1833. }
  1834. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1835. #define MAYBE_WriteRemoteCharacteristic_WritePending \
  1836. WriteRemoteCharacteristic_WritePending
  1837. #else
  1838. #define MAYBE_WriteRemoteCharacteristic_WritePending \
  1839. DISABLED_WriteRemoteCharacteristic_WritePending
  1840. #endif
  1841. // Tests WriteRemoteCharacteristic error with a pending write
  1842. // operation.
  1843. #if BUILDFLAG(IS_WIN)
  1844. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1845. WriteRemoteCharacteristic_WritePending) {
  1846. #else
  1847. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1848. MAYBE_WriteRemoteCharacteristic_WritePending) {
  1849. #endif
  1850. if (!PlatformSupportsLowEnergy()) {
  1851. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1852. return;
  1853. }
  1854. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1855. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1856. base::RunLoop loop1;
  1857. std::vector<uint8_t> empty_vector;
  1858. characteristic1_->WriteRemoteCharacteristic(
  1859. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1860. SUCCEED();
  1861. loop1.Quit();
  1862. }),
  1863. base::BindLambdaForTesting(
  1864. [&](BluetoothGattService::GattErrorCode error_code) {
  1865. ADD_FAILURE() << "unexpected error: " << error_code;
  1866. loop1.Quit();
  1867. }));
  1868. base::RunLoop loop2;
  1869. characteristic1_->WriteRemoteCharacteristic(
  1870. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1871. ADD_FAILURE() << "unexpected success";
  1872. loop2.Quit();
  1873. }),
  1874. base::BindLambdaForTesting(
  1875. [&](BluetoothGattService::GattErrorCode error_code) {
  1876. EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, error_code);
  1877. loop2.Quit();
  1878. }));
  1879. loop2.Run();
  1880. // Initial write should still succeed:
  1881. ResetEventCounts();
  1882. SimulateGattCharacteristicWrite(characteristic1_);
  1883. loop1.Run();
  1884. }
  1885. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1886. #define MAYBE_DeprecatedWriteRemoteCharacteristic_WritePending \
  1887. DeprecatedWriteRemoteCharacteristic_WritePending
  1888. #else
  1889. #define MAYBE_DeprecatedWriteRemoteCharacteristic_WritePending \
  1890. DISABLED_DeprecatedWriteRemoteCharacteristic_WritePending
  1891. #endif
  1892. // Tests DeprecatedWriteRemoteCharacteristic error with a pending write
  1893. // operation.
  1894. #if BUILDFLAG(IS_WIN)
  1895. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1896. DeprecatedWriteRemoteCharacteristic_WritePending) {
  1897. #else
  1898. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1899. MAYBE_DeprecatedWriteRemoteCharacteristic_WritePending) {
  1900. #endif
  1901. if (!PlatformSupportsLowEnergy()) {
  1902. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1903. return;
  1904. }
  1905. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1906. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1907. std::vector<uint8_t> empty_vector;
  1908. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1909. empty_vector, GetCallback(Call::EXPECTED),
  1910. GetGattErrorCallback(Call::NOT_EXPECTED));
  1911. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1912. empty_vector, GetCallback(Call::NOT_EXPECTED),
  1913. GetGattErrorCallback(Call::EXPECTED));
  1914. base::RunLoop().RunUntilIdle();
  1915. EXPECT_EQ(0, callback_count_);
  1916. EXPECT_EQ(1, error_callback_count_);
  1917. EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
  1918. last_gatt_error_code_);
  1919. // Initial write should still succeed:
  1920. ResetEventCounts();
  1921. SimulateGattCharacteristicWrite(characteristic1_);
  1922. base::RunLoop().RunUntilIdle();
  1923. EXPECT_EQ(1, callback_count_);
  1924. EXPECT_EQ(0, error_callback_count_);
  1925. }
  1926. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1927. #define MAYBE_ReadRemoteCharacteristic_WritePending \
  1928. ReadRemoteCharacteristic_WritePending
  1929. #else
  1930. #define MAYBE_ReadRemoteCharacteristic_WritePending \
  1931. DISABLED_ReadRemoteCharacteristic_WritePending
  1932. #endif
  1933. // Tests ReadRemoteCharacteristic error with a pending write operation.
  1934. #if BUILDFLAG(IS_WIN)
  1935. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1936. ReadRemoteCharacteristic_WritePending) {
  1937. #else
  1938. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1939. MAYBE_ReadRemoteCharacteristic_WritePending) {
  1940. #endif
  1941. if (!PlatformSupportsLowEnergy()) {
  1942. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1943. return;
  1944. }
  1945. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1946. BluetoothRemoteGattCharacteristic::PROPERTY_READ |
  1947. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1948. base::RunLoop loop1;
  1949. base::RunLoop loop2;
  1950. std::vector<uint8_t> empty_vector;
  1951. characteristic1_->WriteRemoteCharacteristic(
  1952. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  1953. SUCCEED();
  1954. loop1.Quit();
  1955. }),
  1956. base::BindLambdaForTesting(
  1957. [&](BluetoothGattService::GattErrorCode error_code) {
  1958. ADD_FAILURE() << "unexpected error: " << error_code;
  1959. loop1.Quit();
  1960. }));
  1961. characteristic1_->ReadRemoteCharacteristic(base::BindLambdaForTesting(
  1962. [&](absl::optional<BluetoothGattService::GattErrorCode> error_code,
  1963. const std::vector<uint8_t>& data) {
  1964. EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, error_code);
  1965. loop2.Quit();
  1966. }));
  1967. loop2.Run();
  1968. // Initial write should still succeed:
  1969. ResetEventCounts();
  1970. SimulateGattCharacteristicWrite(characteristic1_);
  1971. loop1.Run();
  1972. }
  1973. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  1974. #define MAYBE_ReadRemoteCharacteristic_DeprecatedWritePending \
  1975. ReadRemoteCharacteristic_DeprecatedWritePending
  1976. #else
  1977. #define MAYBE_ReadRemoteCharacteristic_DeprecatedWritePending \
  1978. DISABLED_ReadRemoteCharacteristic_DeprecatedWritePending
  1979. #endif
  1980. // Tests ReadRemoteCharacteristic error with a pending write operation.
  1981. #if BUILDFLAG(IS_WIN)
  1982. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  1983. ReadRemoteCharacteristic_DeprecatedWritePending) {
  1984. #else
  1985. TEST_F(BluetoothRemoteGattCharacteristicTest,
  1986. MAYBE_ReadRemoteCharacteristic_DeprecatedWritePending) {
  1987. #endif
  1988. if (!PlatformSupportsLowEnergy()) {
  1989. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  1990. return;
  1991. }
  1992. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  1993. BluetoothRemoteGattCharacteristic::PROPERTY_READ |
  1994. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  1995. std::vector<uint8_t> empty_vector;
  1996. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  1997. empty_vector, GetCallback(Call::EXPECTED),
  1998. GetGattErrorCallback(Call::NOT_EXPECTED));
  1999. characteristic1_->ReadRemoteCharacteristic(
  2000. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  2001. base::RunLoop().RunUntilIdle();
  2002. EXPECT_EQ(0, callback_count_);
  2003. EXPECT_EQ(1, error_callback_count_);
  2004. EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
  2005. last_gatt_error_code_);
  2006. // Initial write should still succeed:
  2007. ResetEventCounts();
  2008. SimulateGattCharacteristicWrite(characteristic1_);
  2009. base::RunLoop().RunUntilIdle();
  2010. EXPECT_EQ(1, callback_count_);
  2011. EXPECT_EQ(0, error_callback_count_);
  2012. }
  2013. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2014. #define MAYBE_WriteRemoteCharacteristic_ReadPending \
  2015. WriteRemoteCharacteristic_ReadPending
  2016. #else
  2017. #define MAYBE_WriteRemoteCharacteristic_ReadPending \
  2018. DISABLED_WriteRemoteCharacteristic_ReadPending
  2019. #endif
  2020. // Tests WriteRemoteCharacteristic error with a pending Read operation.
  2021. #if BUILDFLAG(IS_WIN)
  2022. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2023. WriteRemoteCharacteristic_ReadPending) {
  2024. #else
  2025. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2026. MAYBE_WriteRemoteCharacteristic_ReadPending) {
  2027. #endif
  2028. if (!PlatformSupportsLowEnergy()) {
  2029. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2030. return;
  2031. }
  2032. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  2033. BluetoothRemoteGattCharacteristic::PROPERTY_READ |
  2034. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  2035. base::RunLoop loop1;
  2036. base::RunLoop loop2;
  2037. std::vector<uint8_t> empty_vector;
  2038. characteristic1_->ReadRemoteCharacteristic(base::BindLambdaForTesting(
  2039. [&](absl::optional<BluetoothGattService::GattErrorCode> error_code,
  2040. const std::vector<uint8_t>& data) {
  2041. EXPECT_FALSE(error_code.has_value()) << "unexpected failure";
  2042. loop1.Quit();
  2043. }));
  2044. characteristic1_->WriteRemoteCharacteristic(
  2045. empty_vector, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  2046. ADD_FAILURE() << "unexpected success";
  2047. loop2.Quit();
  2048. }),
  2049. base::BindLambdaForTesting(
  2050. [&](BluetoothGattService::GattErrorCode error_code) {
  2051. EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, error_code);
  2052. loop2.Quit();
  2053. }));
  2054. loop2.Run();
  2055. // Initial read should still succeed:
  2056. ResetEventCounts();
  2057. SimulateGattCharacteristicRead(characteristic1_, empty_vector);
  2058. loop1.Run();
  2059. }
  2060. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2061. #define MAYBE_DeprecatedWriteRemoteCharacteristic_ReadPending \
  2062. DeprecatedWriteRemoteCharacteristic_ReadPending
  2063. #else
  2064. #define MAYBE_DeprecatedWriteRemoteCharacteristic_ReadPending \
  2065. DISABLED_DeprecatedWriteRemoteCharacteristic_ReadPending
  2066. #endif
  2067. // Tests DeprecatedWriteRemoteCharacteristic error with a pending Read
  2068. // operation.
  2069. #if BUILDFLAG(IS_WIN)
  2070. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2071. DeprecatedWriteRemoteCharacteristic_ReadPending) {
  2072. #else
  2073. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2074. MAYBE_DeprecatedWriteRemoteCharacteristic_ReadPending) {
  2075. #endif
  2076. if (!PlatformSupportsLowEnergy()) {
  2077. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2078. return;
  2079. }
  2080. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  2081. BluetoothRemoteGattCharacteristic::PROPERTY_READ |
  2082. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  2083. std::vector<uint8_t> empty_vector;
  2084. characteristic1_->ReadRemoteCharacteristic(
  2085. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  2086. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  2087. empty_vector, GetCallback(Call::NOT_EXPECTED),
  2088. GetGattErrorCallback(Call::EXPECTED));
  2089. base::RunLoop().RunUntilIdle();
  2090. EXPECT_EQ(0, callback_count_);
  2091. EXPECT_EQ(1, error_callback_count_);
  2092. EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS,
  2093. last_gatt_error_code_);
  2094. // Initial read should still succeed:
  2095. ResetEventCounts();
  2096. SimulateGattCharacteristicRead(characteristic1_, empty_vector);
  2097. base::RunLoop().RunUntilIdle();
  2098. EXPECT_EQ(1, callback_count_);
  2099. EXPECT_EQ(0, error_callback_count_);
  2100. }
  2101. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2102. #define MAYBE_Notification_During_ReadRemoteCharacteristic \
  2103. Notification_During_ReadRemoteCharacteristic
  2104. #else
  2105. #define MAYBE_Notification_During_ReadRemoteCharacteristic \
  2106. DISABLED_Notification_During_ReadRemoteCharacteristic
  2107. #endif
  2108. // TODO(crbug.com/713991): Enable on windows once it better matches
  2109. // how other platforms set global variables.
  2110. // Tests that a notification arriving during a pending read doesn't
  2111. // cause a crash.
  2112. #if BUILDFLAG(IS_WIN)
  2113. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  2114. Notification_During_ReadRemoteCharacteristic) {
  2115. #else
  2116. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2117. MAYBE_Notification_During_ReadRemoteCharacteristic) {
  2118. #endif
  2119. if (!PlatformSupportsLowEnergy()) {
  2120. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2121. return;
  2122. }
  2123. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2124. BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY |
  2125. BluetoothRemoteGattCharacteristic::PROPERTY_READ,
  2126. NotifyValueState::NOTIFY));
  2127. TestBluetoothAdapterObserver observer(adapter_);
  2128. characteristic1_->ReadRemoteCharacteristic(
  2129. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  2130. std::vector<uint8_t> notification_value = {111};
  2131. SimulateGattCharacteristicChanged(characteristic1_, notification_value);
  2132. base::RunLoop().RunUntilIdle();
  2133. #if BUILDFLAG(IS_MAC)
  2134. // Because macOS uses the same event for notifications and read value
  2135. // responses, we can't know what the event was for. Because there is a pending
  2136. // read request we assume is a read request on macOS.
  2137. EXPECT_EQ(notification_value, last_read_value_);
  2138. EXPECT_EQ(notification_value, characteristic1_->GetValue());
  2139. EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
  2140. #else // !BUILDFLAG(IS_MAC)
  2141. EXPECT_EQ(std::vector<uint8_t>(), last_read_value_);
  2142. EXPECT_EQ(notification_value, characteristic1_->GetValue());
  2143. EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
  2144. #endif // BUILDFLAG(IS_MAC)
  2145. observer.Reset();
  2146. std::vector<uint8_t> read_value = {222};
  2147. SimulateGattCharacteristicRead(characteristic1_, read_value);
  2148. base::RunLoop().RunUntilIdle();
  2149. #if BUILDFLAG(IS_MAC)
  2150. // There are no pending read requests anymore so we assume the event
  2151. // was a notification.
  2152. EXPECT_EQ(notification_value, last_read_value_);
  2153. EXPECT_EQ(read_value, characteristic1_->GetValue());
  2154. EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
  2155. #else // !BUILDFLAG(IS_MAC)
  2156. EXPECT_EQ(read_value, last_read_value_);
  2157. EXPECT_EQ(read_value, characteristic1_->GetValue());
  2158. EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
  2159. #endif // BUILDFLAG(IS_MAC)
  2160. }
  2161. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2162. #define MAYBE_Notification_During_WriteRemoteCharacteristic \
  2163. Notification_During_WriteRemoteCharacteristic
  2164. #else
  2165. #define MAYBE_Notification_During_WriteRemoteCharacteristic \
  2166. DISABLED_Notification_During_WriteRemoteCharacteristic
  2167. #endif
  2168. // Tests that a notification arriving during a pending write doesn't
  2169. // cause a crash.
  2170. #if BUILDFLAG(IS_WIN)
  2171. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2172. Notification_During_WriteRemoteCharacteristic) {
  2173. #else
  2174. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2175. MAYBE_Notification_During_WriteRemoteCharacteristic) {
  2176. #endif
  2177. if (!PlatformSupportsLowEnergy()) {
  2178. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2179. return;
  2180. }
  2181. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2182. BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY |
  2183. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE,
  2184. NotifyValueState::NOTIFY));
  2185. TestBluetoothAdapterObserver observer(adapter_);
  2186. base::RunLoop loop;
  2187. std::vector<uint8_t> write_value = {111};
  2188. characteristic1_->WriteRemoteCharacteristic(
  2189. write_value, WriteType::kWithResponse, base::BindLambdaForTesting([&] {
  2190. EXPECT_EQ(write_value, last_write_value_);
  2191. loop.Quit();
  2192. }),
  2193. base::BindLambdaForTesting(
  2194. [&](BluetoothGattService::GattErrorCode error_code) {
  2195. ADD_FAILURE() << "unexpected error: " << error_code;
  2196. loop.Quit();
  2197. }));
  2198. std::vector<uint8_t> notification_value = {222};
  2199. SimulateGattCharacteristicChanged(characteristic1_, notification_value);
  2200. base::RunLoop().RunUntilIdle();
  2201. EXPECT_EQ(notification_value, characteristic1_->GetValue());
  2202. EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
  2203. observer.Reset();
  2204. SimulateGattCharacteristicWrite(characteristic1_);
  2205. loop.Run();
  2206. }
  2207. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2208. #define MAYBE_Notification_During_DeprecatedWriteRemoteCharacteristic \
  2209. Notification_During_DeprecatedWriteRemoteCharacteristic
  2210. #else
  2211. #define MAYBE_Notification_During_DeprecatedWriteRemoteCharacteristic \
  2212. DISABLED_Notification_During_DeprecatedWriteRemoteCharacteristic
  2213. #endif
  2214. // Tests that a notification arriving during a pending write doesn't
  2215. // cause a crash.
  2216. #if BUILDFLAG(IS_WIN)
  2217. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2218. Notification_During_DeprecatedWriteRemoteCharacteristic) {
  2219. #else
  2220. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2221. MAYBE_Notification_During_DeprecatedWriteRemoteCharacteristic) {
  2222. #endif
  2223. if (!PlatformSupportsLowEnergy()) {
  2224. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2225. return;
  2226. }
  2227. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2228. BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY |
  2229. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE,
  2230. NotifyValueState::NOTIFY));
  2231. TestBluetoothAdapterObserver observer(adapter_);
  2232. std::vector<uint8_t> write_value = {111};
  2233. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  2234. write_value, GetCallback(Call::EXPECTED),
  2235. GetGattErrorCallback(Call::NOT_EXPECTED));
  2236. std::vector<uint8_t> notification_value = {222};
  2237. SimulateGattCharacteristicChanged(characteristic1_, notification_value);
  2238. base::RunLoop().RunUntilIdle();
  2239. EXPECT_EQ(notification_value, characteristic1_->GetValue());
  2240. EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
  2241. observer.Reset();
  2242. SimulateGattCharacteristicWrite(characteristic1_);
  2243. base::RunLoop().RunUntilIdle();
  2244. EXPECT_EQ(write_value, last_write_value_);
  2245. }
  2246. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2247. #define MAYBE_StartNotifySession_NoNotifyOrIndicate \
  2248. StartNotifySession_NoNotifyOrIndicate
  2249. #else
  2250. #define MAYBE_StartNotifySession_NoNotifyOrIndicate \
  2251. DISABLED_StartNotifySession_NoNotifyOrIndicate
  2252. #endif
  2253. // StartNotifySession fails if characteristic doesn't have Notify or Indicate
  2254. // property.
  2255. #if BUILDFLAG(IS_WIN)
  2256. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2257. StartNotifySession_NoNotifyOrIndicate) {
  2258. #else
  2259. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2260. MAYBE_StartNotifySession_NoNotifyOrIndicate) {
  2261. #endif
  2262. if (!PlatformSupportsLowEnergy()) {
  2263. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2264. return;
  2265. }
  2266. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2267. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY,
  2268. StartNotifySetupError::CHARACTERISTIC_PROPERTIES));
  2269. ExpectedChangeNotifyValueAttempts(0);
  2270. // The expected error callback is asynchronous:
  2271. EXPECT_EQ(0, error_callback_count_);
  2272. base::RunLoop().RunUntilIdle();
  2273. EXPECT_EQ(1, error_callback_count_);
  2274. EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED,
  2275. last_gatt_error_code_);
  2276. }
  2277. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2278. #define MAYBE_StartNotifySession_NoConfigDescriptor \
  2279. StartNotifySession_NoConfigDescriptor
  2280. #else
  2281. #define MAYBE_StartNotifySession_NoConfigDescriptor \
  2282. DISABLED_StartNotifySession_NoConfigDescriptor
  2283. #endif
  2284. // StartNotifySession fails if the characteristic is missing the Client
  2285. // Characteristic Configuration descriptor.
  2286. #if BUILDFLAG(IS_WIN)
  2287. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2288. StartNotifySession_NoConfigDescriptor) {
  2289. #else
  2290. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2291. MAYBE_StartNotifySession_NoConfigDescriptor) {
  2292. #endif
  2293. if (!PlatformSupportsLowEnergy()) {
  2294. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2295. return;
  2296. }
  2297. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2298. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY,
  2299. StartNotifySetupError::CONFIG_DESCRIPTOR_MISSING));
  2300. ExpectedChangeNotifyValueAttempts(0);
  2301. // The expected error callback is asynchronous:
  2302. EXPECT_EQ(0, error_callback_count_);
  2303. base::RunLoop().RunUntilIdle();
  2304. EXPECT_EQ(1, error_callback_count_);
  2305. EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED,
  2306. last_gatt_error_code_);
  2307. }
  2308. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2309. #define MAYBE_StartNotifySession_MultipleConfigDescriptor \
  2310. StartNotifySession_MultipleConfigDescriptor
  2311. #else
  2312. #define MAYBE_StartNotifySession_MultipleConfigDescriptor \
  2313. DISABLED_StartNotifySession_MultipleConfigDescriptor
  2314. #endif
  2315. // StartNotifySession fails if the characteristic has multiple Client
  2316. // Characteristic Configuration descriptors.
  2317. #if BUILDFLAG(IS_WIN)
  2318. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2319. StartNotifySession_MultipleConfigDescriptor) {
  2320. #else
  2321. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2322. MAYBE_StartNotifySession_MultipleConfigDescriptor) {
  2323. #endif
  2324. if (!PlatformSupportsLowEnergy()) {
  2325. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2326. return;
  2327. }
  2328. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2329. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY,
  2330. StartNotifySetupError::CONFIG_DESCRIPTOR_DUPLICATE));
  2331. ExpectedChangeNotifyValueAttempts(0);
  2332. // The expected error callback is asynchronous:
  2333. EXPECT_EQ(0, error_callback_count_);
  2334. base::RunLoop().RunUntilIdle();
  2335. EXPECT_EQ(1, error_callback_count_);
  2336. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  2337. }
  2338. #if BUILDFLAG(IS_ANDROID)
  2339. #define MAYBE_StartNotifySession_FailToSetCharacteristicNotification \
  2340. StartNotifySession_FailToSetCharacteristicNotification
  2341. #else
  2342. #define MAYBE_StartNotifySession_FailToSetCharacteristicNotification \
  2343. DISABLED_StartNotifySession_FailToSetCharacteristicNotification
  2344. #endif
  2345. // StartNotifySession fails synchronously when failing to set a characteristic
  2346. // to enable notifications.
  2347. // Android: This is mBluetoothGatt.setCharacteristicNotification failing.
  2348. // macOS: Not applicable: CoreBluetooth doesn't support synchronous API.
  2349. // Windows: Synchronous Test Not Applicable: OS calls are all made
  2350. // asynchronously from BluetoothTaskManagerWin.
  2351. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2352. MAYBE_StartNotifySession_FailToSetCharacteristicNotification) {
  2353. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2354. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY,
  2355. StartNotifySetupError::SET_NOTIFY));
  2356. // The expected error callback is asynchronous:
  2357. EXPECT_EQ(0, error_callback_count_);
  2358. base::RunLoop().RunUntilIdle();
  2359. EXPECT_EQ(1, error_callback_count_);
  2360. ExpectedChangeNotifyValueAttempts(0);
  2361. ASSERT_EQ(0u, notify_sessions_.size());
  2362. }
  2363. #if BUILDFLAG(IS_ANDROID)
  2364. #define MAYBE_StartNotifySession_WriteDescriptorSynchronousError \
  2365. StartNotifySession_WriteDescriptorSynchronousError
  2366. #else
  2367. #define MAYBE_StartNotifySession_WriteDescriptorSynchronousError \
  2368. DISABLED_StartNotifySession_WriteDescriptorSynchronousError
  2369. #endif
  2370. // Tests StartNotifySession descriptor write synchronous failure.
  2371. // macOS: Not applicable: No need to write to the descriptor manually.
  2372. // -[CBPeripheral setNotifyValue:forCharacteristic:] takes care of it.
  2373. // Windows: Synchronous Test Not Applicable: OS calls are all made
  2374. // asynchronously from BluetoothTaskManagerWin.
  2375. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2376. MAYBE_StartNotifySession_WriteDescriptorSynchronousError) {
  2377. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2378. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY,
  2379. StartNotifySetupError::WRITE_DESCRIPTOR));
  2380. // The expected error callback is asynchronous:
  2381. EXPECT_EQ(0, error_callback_count_);
  2382. base::RunLoop().RunUntilIdle();
  2383. EXPECT_EQ(1, error_callback_count_);
  2384. EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
  2385. EXPECT_EQ(0, gatt_write_descriptor_attempts_);
  2386. ASSERT_EQ(0u, last_write_value_.size());
  2387. ASSERT_EQ(0u, notify_sessions_.size());
  2388. }
  2389. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2390. #define MAYBE_StartNotifySession StartNotifySession
  2391. #else
  2392. #define MAYBE_StartNotifySession DISABLED_StartNotifySession
  2393. #endif
  2394. // Tests StartNotifySession success on a characteristic that enabled Notify.
  2395. #if BUILDFLAG(IS_WIN)
  2396. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, StartNotifySession) {
  2397. #else
  2398. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_StartNotifySession) {
  2399. #endif
  2400. if (!PlatformSupportsLowEnergy()) {
  2401. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2402. return;
  2403. }
  2404. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2405. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  2406. }
  2407. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2408. #define MAYBE_StartNotifySession_OnIndicate StartNotifySession_OnIndicate
  2409. #else
  2410. #define MAYBE_StartNotifySession_OnIndicate \
  2411. DISABLED_StartNotifySession_OnIndicate
  2412. #endif
  2413. // Tests StartNotifySession success on a characteristic that enabled Indicate.
  2414. #if BUILDFLAG(IS_WIN)
  2415. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2416. StartNotifySession_OnIndicate) {
  2417. #else
  2418. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2419. MAYBE_StartNotifySession_OnIndicate) {
  2420. #endif
  2421. if (!PlatformSupportsLowEnergy()) {
  2422. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2423. return;
  2424. }
  2425. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2426. /* properties: INDICATE */ 0x20, NotifyValueState::INDICATE));
  2427. }
  2428. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2429. #define MAYBE_StartNotifySession_OnNotifyAndIndicate \
  2430. StartNotifySession_OnNotifyAndIndicate
  2431. #else
  2432. #define MAYBE_StartNotifySession_OnNotifyAndIndicate \
  2433. DISABLED_StartNotifySession_OnNotifyAndIndicate
  2434. #endif
  2435. // Tests StartNotifySession success on a characteristic that enabled Notify &
  2436. // Indicate.
  2437. #if BUILDFLAG(IS_WIN)
  2438. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2439. StartNotifySession_OnNotifyAndIndicate) {
  2440. #else
  2441. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2442. MAYBE_StartNotifySession_OnNotifyAndIndicate) {
  2443. #endif
  2444. if (!PlatformSupportsLowEnergy()) {
  2445. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2446. return;
  2447. }
  2448. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2449. /* properties: NOTIFY and INDICATE bits set */ 0x30,
  2450. NotifyValueState::NOTIFY));
  2451. }
  2452. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2453. #define MAYBE_StartNotifySession_Multiple StartNotifySession_Multiple
  2454. #else
  2455. #define MAYBE_StartNotifySession_Multiple DISABLED_StartNotifySession_Multiple
  2456. #endif
  2457. // Tests multiple StartNotifySession success.
  2458. #if BUILDFLAG(IS_WIN)
  2459. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2460. StartNotifySession_Multiple) {
  2461. #else
  2462. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2463. MAYBE_StartNotifySession_Multiple) {
  2464. #endif
  2465. if (!PlatformSupportsLowEnergy()) {
  2466. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2467. return;
  2468. }
  2469. ASSERT_NO_FATAL_FAILURE(
  2470. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  2471. SimulateGattDescriptor(
  2472. characteristic1_,
  2473. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  2474. .canonical_value());
  2475. base::RunLoop().RunUntilIdle();
  2476. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  2477. characteristic1_->StartNotifySession(
  2478. GetNotifyCallback(Call::EXPECTED),
  2479. GetGattErrorCallback(Call::NOT_EXPECTED));
  2480. characteristic1_->StartNotifySession(
  2481. GetNotifyCallback(Call::EXPECTED),
  2482. GetGattErrorCallback(Call::NOT_EXPECTED));
  2483. EXPECT_EQ(0, callback_count_);
  2484. SimulateGattNotifySessionStarted(characteristic1_);
  2485. base::RunLoop().RunUntilIdle();
  2486. ExpectedChangeNotifyValueAttempts(1);
  2487. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  2488. EXPECT_EQ(2, callback_count_);
  2489. EXPECT_EQ(0, error_callback_count_);
  2490. ASSERT_EQ(2u, notify_sessions_.size());
  2491. ASSERT_TRUE(notify_sessions_[0]);
  2492. ASSERT_TRUE(notify_sessions_[1]);
  2493. EXPECT_EQ(characteristic1_->GetIdentifier(),
  2494. notify_sessions_[0]->GetCharacteristicIdentifier());
  2495. EXPECT_EQ(characteristic1_->GetIdentifier(),
  2496. notify_sessions_[1]->GetCharacteristicIdentifier());
  2497. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  2498. EXPECT_TRUE(notify_sessions_[1]->IsActive());
  2499. }
  2500. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2501. #define MAYBE_StartNotifySessionError_Multiple StartNotifySessionError_Multiple
  2502. #else
  2503. #define MAYBE_StartNotifySessionError_Multiple \
  2504. DISABLED_StartNotifySessionError_Multiple
  2505. #endif
  2506. // Tests multiple StartNotifySessions pending and then an error.
  2507. #if BUILDFLAG(IS_WIN)
  2508. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  2509. StartNotifySessionError_Multiple) {
  2510. #else
  2511. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2512. MAYBE_StartNotifySessionError_Multiple) {
  2513. #endif
  2514. if (!PlatformSupportsLowEnergy()) {
  2515. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2516. return;
  2517. }
  2518. ASSERT_NO_FATAL_FAILURE(
  2519. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  2520. SimulateGattDescriptor(
  2521. characteristic1_,
  2522. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  2523. .canonical_value());
  2524. base::RunLoop().RunUntilIdle();
  2525. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  2526. characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
  2527. GetGattErrorCallback(Call::EXPECTED));
  2528. characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
  2529. GetGattErrorCallback(Call::EXPECTED));
  2530. ExpectedChangeNotifyValueAttempts(1);
  2531. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  2532. EXPECT_EQ(0, callback_count_);
  2533. SimulateGattNotifySessionStartError(characteristic1_,
  2534. BluetoothGattService::GATT_ERROR_FAILED);
  2535. base::RunLoop().RunUntilIdle();
  2536. EXPECT_EQ(0, callback_count_);
  2537. EXPECT_EQ(2, error_callback_count_);
  2538. ASSERT_EQ(0u, notify_sessions_.size());
  2539. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  2540. }
  2541. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2542. #define MAYBE_StartNotifySessionDisconnectOnError \
  2543. StartNotifySessionDisconnectOnError
  2544. #else
  2545. #define MAYBE_StartNotifySessionDisconnectOnError \
  2546. DISABLED_StartNotifySessionDisconnectOnError
  2547. #endif
  2548. // Test that a GATT disconnect in a StartNotifications error callback will
  2549. // behave correctly. Regression test for crbug.com/1107577.
  2550. #if BUILDFLAG(IS_WIN)
  2551. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  2552. StartNotifySessionDisconnectOnError) {
  2553. #else
  2554. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2555. MAYBE_StartNotifySessionDisconnectOnError) {
  2556. #endif
  2557. if (!PlatformSupportsLowEnergy()) {
  2558. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2559. return;
  2560. }
  2561. ASSERT_NO_FATAL_FAILURE(
  2562. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  2563. base::RunLoop start_notifications_loop;
  2564. characteristic1_->StartNotifySession(
  2565. base::BindLambdaForTesting(
  2566. [&start_notifications_loop](
  2567. std::unique_ptr<BluetoothGattNotifySession> notify_session) {
  2568. ADD_FAILURE() << "Unexpected startNotifications success.";
  2569. start_notifications_loop.Quit();
  2570. }),
  2571. base::BindLambdaForTesting(
  2572. [&](BluetoothGattService::GattErrorCode error_code) {
  2573. start_notifications_loop.Quit();
  2574. ASSERT_FALSE(gatt_connections_.empty());
  2575. gatt_connections_[0]->Disconnect();
  2576. }));
  2577. start_notifications_loop.Run();
  2578. }
  2579. #if BUILDFLAG(IS_ANDROID)
  2580. #define MAYBE_StartNotifySession_AfterDeleted StartNotifySession_AfterDeleted
  2581. #else
  2582. #define MAYBE_StartNotifySession_AfterDeleted \
  2583. DISABLED_StartNotifySession_AfterDeleted
  2584. #endif
  2585. // Tests StartNotifySession completing after chrome objects are deleted.
  2586. // macOS: Not applicable: This can never happen if CBPeripheral delegate is set
  2587. // to nil.
  2588. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2589. MAYBE_StartNotifySession_AfterDeleted) {
  2590. ASSERT_NO_FATAL_FAILURE(
  2591. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  2592. SimulateGattDescriptor(
  2593. characteristic1_,
  2594. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  2595. .canonical_value());
  2596. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  2597. characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
  2598. GetGattErrorCallback(Call::EXPECTED));
  2599. ExpectedChangeNotifyValueAttempts(1);
  2600. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  2601. EXPECT_EQ(0, callback_count_);
  2602. RememberCharacteristicForSubsequentAction(characteristic1_);
  2603. RememberCCCDescriptorForSubsequentAction(characteristic1_);
  2604. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  2605. SimulateGattNotifySessionStarted(/* use remembered characteristic */ nullptr);
  2606. base::RunLoop().RunUntilIdle();
  2607. EXPECT_EQ(0, callback_count_);
  2608. EXPECT_EQ(1, error_callback_count_);
  2609. ASSERT_EQ(0u, notify_sessions_.size());
  2610. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  2611. }
  2612. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2613. #define MAYBE_StartNotifySession_BeforeDeleted StartNotifySession_BeforeDeleted
  2614. #else
  2615. #define MAYBE_StartNotifySession_BeforeDeleted \
  2616. DISABLED_StartNotifySession_BeforeDeleted
  2617. #endif
  2618. // Tests StartNotifySession completing before chrome objects are deleted.
  2619. #if BUILDFLAG(IS_WIN)
  2620. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  2621. StartNotifySession_BeforeDeleted) {
  2622. #else
  2623. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2624. MAYBE_StartNotifySession_BeforeDeleted) {
  2625. #endif
  2626. if (!PlatformSupportsLowEnergy()) {
  2627. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2628. return;
  2629. }
  2630. ASSERT_NO_FATAL_FAILURE(
  2631. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  2632. SimulateGattDescriptor(
  2633. characteristic1_,
  2634. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  2635. .canonical_value());
  2636. base::RunLoop().RunUntilIdle();
  2637. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  2638. characteristic1_->StartNotifySession(
  2639. GetNotifyCallback(Call::EXPECTED),
  2640. GetGattErrorCallback(Call::NOT_EXPECTED));
  2641. ExpectedChangeNotifyValueAttempts(1);
  2642. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  2643. EXPECT_EQ(0, callback_count_);
  2644. SimulateGattNotifySessionStarted(characteristic1_);
  2645. base::RunLoop().RunUntilIdle();
  2646. ASSERT_EQ(1u, notify_sessions_.size());
  2647. std::string characteristic_identifier = characteristic1_->GetIdentifier();
  2648. EXPECT_EQ(characteristic_identifier,
  2649. notify_sessions_[0]->GetCharacteristicIdentifier());
  2650. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  2651. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  2652. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  2653. base::RunLoop().RunUntilIdle();
  2654. EXPECT_TRUE("Did not crash!");
  2655. ASSERT_TRUE(notify_sessions_[0]);
  2656. EXPECT_EQ(characteristic_identifier,
  2657. notify_sessions_[0]->GetCharacteristicIdentifier());
  2658. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  2659. }
  2660. #if BUILDFLAG(IS_MAC)
  2661. #define MAYBE_StartNotifySession_Reentrant_Success_Success \
  2662. StartNotifySession_Reentrant_Success_Success
  2663. #else
  2664. #define MAYBE_StartNotifySession_Reentrant_Success_Success \
  2665. DISABLED_StartNotifySession_Reentrant_Success_Success
  2666. #endif
  2667. // Tests StartNotifySession reentrant in start notify session success callback
  2668. // and the reentrant start notify session success.
  2669. #if BUILDFLAG(IS_WIN)
  2670. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2671. StartNotifySession_Reentrant_Success_Success) {
  2672. #else
  2673. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2674. MAYBE_StartNotifySession_Reentrant_Success_Success) {
  2675. #endif
  2676. if (!PlatformSupportsLowEnergy()) {
  2677. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2678. return;
  2679. }
  2680. ASSERT_NO_FATAL_FAILURE(
  2681. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  2682. SimulateGattDescriptor(
  2683. characteristic1_,
  2684. BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()
  2685. .canonical_value());
  2686. base::RunLoop().RunUntilIdle();
  2687. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  2688. characteristic1_->StartNotifySession(
  2689. GetReentrantStartNotifySessionSuccessCallback(Call::EXPECTED,
  2690. characteristic1_),
  2691. GetReentrantStartNotifySessionErrorCallback(
  2692. Call::NOT_EXPECTED, characteristic1_,
  2693. false /* error_in_reentrant */));
  2694. EXPECT_EQ(0, callback_count_);
  2695. SimulateGattNotifySessionStarted(characteristic1_);
  2696. base::RunLoop().RunUntilIdle();
  2697. ExpectedChangeNotifyValueAttempts(1);
  2698. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  2699. // Simulate reentrant StartNotifySession request from
  2700. // BluetoothTestBase::ReentrantStartNotifySessionSuccessCallback.
  2701. base::RunLoop().RunUntilIdle();
  2702. ExpectedChangeNotifyValueAttempts(1);
  2703. EXPECT_EQ(2, callback_count_);
  2704. EXPECT_EQ(0, error_callback_count_);
  2705. ASSERT_EQ(2u, notify_sessions_.size());
  2706. for (unsigned int i = 0; i < notify_sessions_.size(); i++) {
  2707. ASSERT_TRUE(notify_sessions_[i]);
  2708. EXPECT_EQ(characteristic1_->GetIdentifier(),
  2709. notify_sessions_[i]->GetCharacteristicIdentifier());
  2710. EXPECT_TRUE(notify_sessions_[i]->IsActive());
  2711. }
  2712. }
  2713. #if BUILDFLAG(IS_WIN)
  2714. // Tests StartNotifySession reentrant in start notify session error callback
  2715. // and the reentrant start notify session error.
  2716. TEST_P(BluetoothRemoteGattCharacteristicTestWin32Only,
  2717. StartNotifySession_Reentrant_Error_Error) {
  2718. ASSERT_NO_FATAL_FAILURE(
  2719. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  2720. SimulateGattDescriptor(
  2721. characteristic1_,
  2722. BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid().value());
  2723. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  2724. SimulateGattNotifySessionStartError(characteristic1_,
  2725. BluetoothGattService::GATT_ERROR_UNKNOWN);
  2726. characteristic1_->StartNotifySession(
  2727. GetReentrantStartNotifySessionSuccessCallback(Call::NOT_EXPECTED,
  2728. characteristic1_),
  2729. GetReentrantStartNotifySessionErrorCallback(
  2730. Call::EXPECTED, characteristic1_, true /* error_in_reentrant */));
  2731. EXPECT_EQ(0, callback_count_);
  2732. SimulateGattNotifySessionStarted(characteristic1_);
  2733. base::RunLoop().RunUntilIdle();
  2734. ExpectedChangeNotifyValueAttempts(0);
  2735. // Simulate reentrant StartNotifySession request from
  2736. // BluetoothTestBase::ReentrantStartNotifySessionErrorCallback.
  2737. SimulateGattNotifySessionStarted(characteristic1_);
  2738. base::RunLoop().RunUntilIdle();
  2739. ExpectedChangeNotifyValueAttempts(0);
  2740. EXPECT_EQ(0, callback_count_);
  2741. EXPECT_EQ(2, error_callback_count_);
  2742. ASSERT_EQ(0u, notify_sessions_.size());
  2743. }
  2744. #endif
  2745. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2746. #define MAYBE_StopNotifySession StopNotifySession
  2747. #else
  2748. #define MAYBE_StopNotifySession DISABLED_StopNotifySession
  2749. #endif
  2750. // Tests StopNotifySession success on a characteristic that enabled Notify.
  2751. #if BUILDFLAG(IS_WIN)
  2752. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly, StopNotifySession) {
  2753. #else
  2754. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_StopNotifySession) {
  2755. #endif
  2756. if (!PlatformSupportsLowEnergy()) {
  2757. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2758. return;
  2759. }
  2760. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2761. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  2762. ExpectedChangeNotifyValueAttempts(1);
  2763. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  2764. notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
  2765. SimulateGattNotifySessionStopped(characteristic1_);
  2766. base::RunLoop().RunUntilIdle();
  2767. ExpectedChangeNotifyValueAttempts(2);
  2768. ExpectedNotifyValue(NotifyValueState::NONE);
  2769. // Check that the notify session is inactive.
  2770. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  2771. EXPECT_FALSE(characteristic1_->IsNotifying());
  2772. }
  2773. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2774. #define MAYBE_StopNotifySession_SessionDeleted StopNotifySession_SessionDeleted
  2775. #else
  2776. #define MAYBE_StopNotifySession_SessionDeleted \
  2777. DISABLED_StopNotifySession_SessionDeleted
  2778. #endif
  2779. // Tests that deleted sessions are stopped.
  2780. #if BUILDFLAG(IS_WIN)
  2781. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  2782. StopNotifySession_SessionDeleted) {
  2783. #else
  2784. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2785. MAYBE_StopNotifySession_SessionDeleted) {
  2786. #endif
  2787. if (!PlatformSupportsLowEnergy()) {
  2788. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2789. return;
  2790. }
  2791. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2792. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  2793. ExpectedChangeNotifyValueAttempts(1);
  2794. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  2795. notify_sessions_.clear();
  2796. SimulateGattNotifySessionStopped(characteristic1_);
  2797. base::RunLoop().RunUntilIdle();
  2798. ExpectedChangeNotifyValueAttempts(2);
  2799. ExpectedNotifyValue(NotifyValueState::NONE);
  2800. // Check that the notify session is inactive.
  2801. EXPECT_FALSE(characteristic1_->IsNotifying());
  2802. }
  2803. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2804. #define MAYBE_StopNotifySession_SessionDeleted2 \
  2805. StopNotifySession_SessionDeleted2
  2806. #else
  2807. #define MAYBE_StopNotifySession_SessionDeleted2 \
  2808. DISABLED_StopNotifySession_SessionDeleted2
  2809. #endif
  2810. // Tests that deleting the sessions before the stop callbacks have been
  2811. // invoked does not cause problems.
  2812. #if BUILDFLAG(IS_WIN)
  2813. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  2814. StopNotifySession_SessionDeleted2) {
  2815. #else
  2816. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2817. MAYBE_StopNotifySession_SessionDeleted2) {
  2818. #endif
  2819. if (!PlatformSupportsLowEnergy()) {
  2820. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2821. return;
  2822. }
  2823. ASSERT_NO_FATAL_FAILURE(
  2824. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  2825. SimulateGattDescriptor(
  2826. characteristic1_,
  2827. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  2828. .canonical_value());
  2829. base::RunLoop().RunUntilIdle();
  2830. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  2831. // Start notify sessions.
  2832. characteristic1_->StartNotifySession(
  2833. GetNotifyCheckForPrecedingCalls(0),
  2834. GetGattErrorCallback(Call::NOT_EXPECTED));
  2835. characteristic1_->StartNotifySession(
  2836. GetNotifyCheckForPrecedingCalls(1),
  2837. GetGattErrorCallback(Call::NOT_EXPECTED));
  2838. EXPECT_EQ(0, callback_count_);
  2839. SimulateGattNotifySessionStarted(characteristic1_);
  2840. base::RunLoop().RunUntilIdle();
  2841. ExpectedChangeNotifyValueAttempts(1);
  2842. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  2843. EXPECT_EQ(2, callback_count_);
  2844. EXPECT_EQ(0, error_callback_count_);
  2845. ASSERT_EQ(2u, notify_sessions_.size());
  2846. ASSERT_TRUE(notify_sessions_[0]);
  2847. ASSERT_TRUE(notify_sessions_[1]);
  2848. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  2849. EXPECT_EQ(characteristic1_, notify_sessions_[1]->GetCharacteristic());
  2850. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  2851. EXPECT_TRUE(notify_sessions_[1]->IsActive());
  2852. EXPECT_TRUE(characteristic1_->IsNotifying());
  2853. // Queue up stop events.
  2854. notify_sessions_[1]->Stop(GetStopNotifyCallback(Call::EXPECTED));
  2855. notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
  2856. // Delete the notify sessions.
  2857. notify_sessions_.clear();
  2858. // Run the stop events.
  2859. base::RunLoop().RunUntilIdle();
  2860. SimulateGattNotifySessionStopped(characteristic1_);
  2861. base::RunLoop().RunUntilIdle();
  2862. // Check that the state is correct.
  2863. EXPECT_TRUE("Did not crash!");
  2864. EXPECT_FALSE(characteristic1_->IsNotifying());
  2865. }
  2866. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2867. #define MAYBE_StopNotifySession_Cancelled StopNotifySession_Cancelled
  2868. #else
  2869. #define MAYBE_StopNotifySession_Cancelled DISABLED_StopNotifySession_Cancelled
  2870. #endif
  2871. // Tests that cancelling StopNotifySession works.
  2872. #if BUILDFLAG(IS_WIN)
  2873. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2874. StopNotifySession_Cancelled) {
  2875. #else
  2876. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2877. MAYBE_StopNotifySession_Cancelled) {
  2878. #endif
  2879. if (!PlatformSupportsLowEnergy()) {
  2880. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2881. return;
  2882. }
  2883. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2884. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  2885. // Check that the session is correctly setup.
  2886. std::string characteristic_identifier = characteristic1_->GetIdentifier();
  2887. EXPECT_EQ(characteristic_identifier,
  2888. notify_sessions_[0]->GetCharacteristicIdentifier());
  2889. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  2890. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  2891. // Queue a Stop request.
  2892. notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
  2893. // Cancel Stop by deleting the device before Stop finishes.
  2894. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  2895. }
  2896. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2897. #define MAYBE_StopNotifySession_AfterDeleted StopNotifySession_AfterDeleted
  2898. #else
  2899. #define MAYBE_StopNotifySession_AfterDeleted \
  2900. DISABLED_StopNotifySession_AfterDeleted
  2901. #endif
  2902. // Tests that deleted sessions are stopped.
  2903. #if BUILDFLAG(IS_WIN)
  2904. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  2905. StopNotifySession_AfterDeleted) {
  2906. #else
  2907. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2908. MAYBE_StopNotifySession_AfterDeleted) {
  2909. #endif
  2910. if (!PlatformSupportsLowEnergy()) {
  2911. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2912. return;
  2913. }
  2914. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2915. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  2916. // Check that the session is correctly setup
  2917. std::string characteristic_identifier = characteristic1_->GetIdentifier();
  2918. EXPECT_EQ(characteristic_identifier,
  2919. notify_sessions_[0]->GetCharacteristicIdentifier());
  2920. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  2921. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  2922. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  2923. ResetEventCounts();
  2924. notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
  2925. // Check that the callback does not arrive synchronously.
  2926. EXPECT_EQ(0, callback_count_);
  2927. // Trigger the stop callback
  2928. base::RunLoop().RunUntilIdle();
  2929. EXPECT_EQ(1, callback_count_);
  2930. EXPECT_TRUE("Did not crash!");
  2931. ASSERT_TRUE(notify_sessions_[0]);
  2932. EXPECT_EQ(characteristic_identifier,
  2933. notify_sessions_[0]->GetCharacteristicIdentifier());
  2934. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  2935. }
  2936. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2937. #define MAYBE_StopNotifySession_OnIndicate StopNotifySession_OnIndicate
  2938. #else
  2939. #define MAYBE_StopNotifySession_OnIndicate DISABLED_StopNotifySession_OnIndicate
  2940. #endif
  2941. // Tests StopNotifySession success on a characteristic that enabled Indicate.
  2942. #if BUILDFLAG(IS_WIN)
  2943. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  2944. StopNotifySession_OnIndicate) {
  2945. #else
  2946. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2947. MAYBE_StopNotifySession_OnIndicate) {
  2948. #endif
  2949. if (!PlatformSupportsLowEnergy()) {
  2950. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2951. return;
  2952. }
  2953. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2954. /* properties: INDICATE */ 0x20, NotifyValueState::INDICATE));
  2955. ExpectedChangeNotifyValueAttempts(1);
  2956. ExpectedNotifyValue(NotifyValueState::INDICATE);
  2957. notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
  2958. SimulateGattNotifySessionStopped(characteristic1_);
  2959. base::RunLoop().RunUntilIdle();
  2960. ExpectedChangeNotifyValueAttempts(2);
  2961. ExpectedNotifyValue(NotifyValueState::NONE);
  2962. // Check that the notify session is inactive.
  2963. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  2964. EXPECT_FALSE(characteristic1_->IsNotifying());
  2965. }
  2966. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  2967. #define MAYBE_StopNotifySession_OnNotifyAndIndicate \
  2968. StopNotifySession_OnNotifyAndIndicate
  2969. #else
  2970. #define MAYBE_StopNotifySession_OnNotifyAndIndicate \
  2971. DISABLED_StopNotifySession_OnNotifyAndIndicate
  2972. #endif
  2973. // Tests StopNotifySession success on a characteristic that enabled Notify &
  2974. // Indicate.
  2975. #if BUILDFLAG(IS_WIN)
  2976. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  2977. StopNotifySession_OnNotifyAndIndicate) {
  2978. #else
  2979. TEST_F(BluetoothRemoteGattCharacteristicTest,
  2980. MAYBE_StopNotifySession_OnNotifyAndIndicate) {
  2981. #endif
  2982. if (!PlatformSupportsLowEnergy()) {
  2983. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  2984. return;
  2985. }
  2986. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  2987. /* properties: NOTIFY and INDICATE bits set */ 0x30,
  2988. NotifyValueState::NOTIFY));
  2989. ExpectedChangeNotifyValueAttempts(1);
  2990. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  2991. notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
  2992. SimulateGattNotifySessionStopped(characteristic1_);
  2993. base::RunLoop().RunUntilIdle();
  2994. ExpectedChangeNotifyValueAttempts(2);
  2995. ExpectedNotifyValue(NotifyValueState::NONE);
  2996. // Check that the notify session is inactive.
  2997. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  2998. EXPECT_FALSE(characteristic1_->IsNotifying());
  2999. }
  3000. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3001. #define MAYBE_StopNotifySession_Error StopNotifySession_Error
  3002. #else
  3003. #define MAYBE_StopNotifySession_Error DISABLED_StopNotifySession_Error
  3004. #endif
  3005. // Tests StopNotifySession error
  3006. #if BUILDFLAG(IS_WIN)
  3007. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3008. StopNotifySession_Error) {
  3009. #else
  3010. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_StopNotifySession_Error) {
  3011. #endif
  3012. if (!PlatformSupportsLowEnergy()) {
  3013. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3014. return;
  3015. }
  3016. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  3017. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  3018. // Check that the notify session is active.
  3019. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  3020. EXPECT_EQ(characteristic1_->GetIdentifier(),
  3021. notify_sessions_[0]->GetCharacteristicIdentifier());
  3022. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  3023. EXPECT_TRUE(characteristic1_->IsNotifying());
  3024. notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
  3025. SimulateGattNotifySessionStopError(characteristic1_,
  3026. BluetoothGattService::GATT_ERROR_UNKNOWN);
  3027. base::RunLoop().RunUntilIdle();
  3028. // Check that the notify session is inactive.
  3029. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3030. EXPECT_FALSE(characteristic1_->IsNotifying());
  3031. }
  3032. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3033. #define MAYBE_StopNotifySession_Multiple1 StopNotifySession_Multiple1
  3034. #else
  3035. #define MAYBE_StopNotifySession_Multiple1 DISABLED_StopNotifySession_Multiple1
  3036. #endif
  3037. // Tests multiple StopNotifySession calls for a single session.
  3038. #if BUILDFLAG(IS_WIN)
  3039. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3040. StopNotifySession_Multiple1) {
  3041. #else
  3042. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3043. MAYBE_StopNotifySession_Multiple1) {
  3044. #endif
  3045. if (!PlatformSupportsLowEnergy()) {
  3046. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3047. return;
  3048. }
  3049. ASSERT_NO_FATAL_FAILURE(
  3050. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  3051. SimulateGattDescriptor(
  3052. characteristic1_,
  3053. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  3054. .canonical_value());
  3055. base::RunLoop().RunUntilIdle();
  3056. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  3057. // Start notify session
  3058. characteristic1_->StartNotifySession(
  3059. GetNotifyCallback(Call::EXPECTED),
  3060. GetGattErrorCallback(Call::NOT_EXPECTED));
  3061. EXPECT_EQ(0, callback_count_);
  3062. SimulateGattNotifySessionStarted(characteristic1_);
  3063. base::RunLoop().RunUntilIdle();
  3064. ExpectedChangeNotifyValueAttempts(1);
  3065. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  3066. EXPECT_EQ(1, callback_count_);
  3067. EXPECT_EQ(0, error_callback_count_);
  3068. ASSERT_EQ(1u, notify_sessions_.size());
  3069. ASSERT_TRUE(notify_sessions_[0]);
  3070. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  3071. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  3072. EXPECT_TRUE(characteristic1_->IsNotifying());
  3073. // Stop the notify session twice
  3074. ResetEventCounts();
  3075. notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(0));
  3076. notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(1));
  3077. SimulateGattNotifySessionStopped(characteristic1_);
  3078. base::RunLoop().RunUntilIdle();
  3079. // Check that the notify session is inactive.
  3080. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3081. EXPECT_FALSE(characteristic1_->IsNotifying());
  3082. }
  3083. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3084. #define MAYBE_StopNotifySession_Multiple2 StopNotifySession_Multiple2
  3085. #else
  3086. #define MAYBE_StopNotifySession_Multiple2 DISABLED_StopNotifySession_Multiple2
  3087. #endif
  3088. // Tests multiple StartNotifySession calls and multiple StopNotifySession calls.
  3089. #if BUILDFLAG(IS_WIN)
  3090. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3091. StopNotifySession_Multiple2) {
  3092. #else
  3093. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3094. MAYBE_StopNotifySession_Multiple2) {
  3095. #endif
  3096. if (!PlatformSupportsLowEnergy()) {
  3097. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3098. return;
  3099. }
  3100. ASSERT_NO_FATAL_FAILURE(
  3101. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  3102. SimulateGattDescriptor(
  3103. characteristic1_,
  3104. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  3105. .canonical_value());
  3106. base::RunLoop().RunUntilIdle();
  3107. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  3108. // Start notify sessions
  3109. characteristic1_->StartNotifySession(
  3110. GetNotifyCheckForPrecedingCalls(0),
  3111. GetGattErrorCallback(Call::NOT_EXPECTED));
  3112. characteristic1_->StartNotifySession(
  3113. GetNotifyCheckForPrecedingCalls(1),
  3114. GetGattErrorCallback(Call::NOT_EXPECTED));
  3115. EXPECT_EQ(0, callback_count_);
  3116. SimulateGattNotifySessionStarted(characteristic1_);
  3117. base::RunLoop().RunUntilIdle();
  3118. ExpectedChangeNotifyValueAttempts(1);
  3119. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  3120. EXPECT_EQ(2, callback_count_);
  3121. EXPECT_EQ(0, error_callback_count_);
  3122. ASSERT_EQ(2u, notify_sessions_.size());
  3123. ASSERT_TRUE(notify_sessions_[0]);
  3124. ASSERT_TRUE(notify_sessions_[1]);
  3125. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  3126. EXPECT_EQ(characteristic1_, notify_sessions_[1]->GetCharacteristic());
  3127. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  3128. EXPECT_TRUE(notify_sessions_[1]->IsActive());
  3129. EXPECT_TRUE(characteristic1_->IsNotifying());
  3130. // Stop the notify sessions
  3131. notify_sessions_[1]->Stop(GetStopNotifyCheckForPrecedingCalls(2));
  3132. base::RunLoop().RunUntilIdle();
  3133. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  3134. EXPECT_FALSE(notify_sessions_[1]->IsActive());
  3135. EXPECT_TRUE(characteristic1_->IsNotifying());
  3136. notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(3));
  3137. SimulateGattNotifySessionStopped(characteristic1_);
  3138. base::RunLoop().RunUntilIdle();
  3139. // Check that the notify sessions is inactive.
  3140. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3141. EXPECT_FALSE(notify_sessions_[1]->IsActive());
  3142. EXPECT_FALSE(characteristic1_->IsNotifying());
  3143. }
  3144. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3145. #define MAYBE_StopNotifySession_StopStart StopNotifySession_StopStart
  3146. #else
  3147. #define MAYBE_StopNotifySession_StopStart DISABLED_StopNotifySession_StopStart
  3148. #endif
  3149. // Tests starting a new notify session before the previous stop request
  3150. // resolves.
  3151. #if BUILDFLAG(IS_WIN)
  3152. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3153. StopNotifySession_StopStart) {
  3154. #else
  3155. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3156. MAYBE_StopNotifySession_StopStart) {
  3157. #endif
  3158. if (!PlatformSupportsLowEnergy()) {
  3159. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3160. return;
  3161. }
  3162. ASSERT_NO_FATAL_FAILURE(
  3163. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  3164. SimulateGattDescriptor(
  3165. characteristic1_,
  3166. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  3167. .canonical_value());
  3168. base::RunLoop().RunUntilIdle();
  3169. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  3170. // Start notify session
  3171. ResetEventCounts();
  3172. characteristic1_->StartNotifySession(
  3173. GetNotifyCheckForPrecedingCalls(0),
  3174. GetGattErrorCallback(Call::NOT_EXPECTED));
  3175. SimulateGattNotifySessionStarted(characteristic1_);
  3176. base::RunLoop().RunUntilIdle();
  3177. ASSERT_EQ(1u, notify_sessions_.size());
  3178. ASSERT_TRUE(notify_sessions_[0]);
  3179. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  3180. // Stop the notify session
  3181. notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(1));
  3182. // Start another notify session
  3183. characteristic1_->StartNotifySession(
  3184. GetNotifyCheckForPrecedingCalls(2),
  3185. GetGattErrorCallback(Call::NOT_EXPECTED));
  3186. SimulateGattNotifySessionStopped(characteristic1_);
  3187. base::RunLoop().RunUntilIdle();
  3188. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3189. SimulateGattNotifySessionStarted(characteristic1_);
  3190. base::RunLoop().RunUntilIdle();
  3191. ASSERT_EQ(2u, notify_sessions_.size());
  3192. ASSERT_TRUE(notify_sessions_[0]);
  3193. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3194. ASSERT_TRUE(notify_sessions_[1]);
  3195. EXPECT_TRUE(notify_sessions_[1]->IsActive());
  3196. EXPECT_TRUE(characteristic1_->IsNotifying());
  3197. }
  3198. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3199. #define MAYBE_StopNotifySession_StartStopStart StopNotifySession_StartStopStart
  3200. #else
  3201. #define MAYBE_StopNotifySession_StartStopStart \
  3202. DISABLED_StopNotifySession_StartStopStart
  3203. #endif
  3204. #if BUILDFLAG(IS_WIN)
  3205. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3206. StopNotifySession_StartStopStart) {
  3207. #else
  3208. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3209. MAYBE_StopNotifySession_StartStopStart) {
  3210. #endif
  3211. if (!PlatformSupportsLowEnergy()) {
  3212. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3213. return;
  3214. }
  3215. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  3216. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  3217. // Check that the initial notify session is active.
  3218. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  3219. EXPECT_EQ(characteristic1_->GetIdentifier(),
  3220. notify_sessions_[0]->GetCharacteristicIdentifier());
  3221. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  3222. EXPECT_TRUE(characteristic1_->IsNotifying());
  3223. // Queue up the first event.
  3224. ResetEventCounts();
  3225. characteristic1_->StartNotifySession(
  3226. GetNotifyCheckForPrecedingCalls(0),
  3227. GetGattErrorCallback(Call::NOT_EXPECTED));
  3228. // Queue up the second event.
  3229. notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(1));
  3230. // Queue up the third event.
  3231. characteristic1_->StartNotifySession(
  3232. GetNotifyCheckForPrecedingCalls(2),
  3233. GetGattErrorCallback(Call::NOT_EXPECTED));
  3234. // Run the event loop to resolve all three events.
  3235. base::RunLoop().RunUntilIdle();
  3236. // Check the state of all the sessions.
  3237. ASSERT_EQ(3u, notify_sessions_.size());
  3238. EXPECT_EQ(characteristic1_->GetIdentifier(),
  3239. notify_sessions_[0]->GetCharacteristicIdentifier());
  3240. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  3241. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3242. EXPECT_EQ(characteristic1_->GetIdentifier(),
  3243. notify_sessions_[1]->GetCharacteristicIdentifier());
  3244. EXPECT_EQ(characteristic1_, notify_sessions_[1]->GetCharacteristic());
  3245. EXPECT_TRUE(notify_sessions_[1]->IsActive());
  3246. EXPECT_EQ(characteristic1_->GetIdentifier(),
  3247. notify_sessions_[2]->GetCharacteristicIdentifier());
  3248. EXPECT_EQ(characteristic1_, notify_sessions_[2]->GetCharacteristic());
  3249. EXPECT_TRUE(notify_sessions_[2]->IsActive());
  3250. EXPECT_TRUE(characteristic1_->IsNotifying());
  3251. }
  3252. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3253. #define MAYBE_StopNotifySession_StopStopStart StopNotifySession_StopStopStart
  3254. #else
  3255. #define MAYBE_StopNotifySession_StopStopStart \
  3256. DISABLED_StopNotifySession_StopStopStart
  3257. #endif
  3258. // Tests starting a new notify session before the previous stop requests
  3259. // resolve.
  3260. #if BUILDFLAG(IS_WIN)
  3261. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3262. StopNotifySession_StopStopStart) {
  3263. #else
  3264. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3265. MAYBE_StopNotifySession_StopStopStart) {
  3266. #endif
  3267. if (!PlatformSupportsLowEnergy()) {
  3268. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3269. return;
  3270. }
  3271. ASSERT_NO_FATAL_FAILURE(
  3272. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  3273. SimulateGattDescriptor(
  3274. characteristic1_,
  3275. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  3276. .canonical_value());
  3277. base::RunLoop().RunUntilIdle();
  3278. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  3279. // Start notify session
  3280. ResetEventCounts();
  3281. characteristic1_->StartNotifySession(
  3282. GetNotifyCheckForPrecedingCalls(0),
  3283. GetGattErrorCallback(Call::NOT_EXPECTED));
  3284. SimulateGattNotifySessionStarted(characteristic1_);
  3285. base::RunLoop().RunUntilIdle();
  3286. ASSERT_EQ(1u, notify_sessions_.size());
  3287. ASSERT_TRUE(notify_sessions_[0]);
  3288. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  3289. ExpectedChangeNotifyValueAttempts(1);
  3290. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  3291. // Stop the notify session twice
  3292. notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(1));
  3293. notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(2));
  3294. ExpectedChangeNotifyValueAttempts(2);
  3295. ExpectedNotifyValue(NotifyValueState::NONE);
  3296. // Start another notify session
  3297. characteristic1_->StartNotifySession(
  3298. GetNotifyCheckForPrecedingCalls(3),
  3299. GetGattErrorCallback(Call::NOT_EXPECTED));
  3300. ExpectedChangeNotifyValueAttempts(2);
  3301. ExpectedNotifyValue(NotifyValueState::NONE);
  3302. SimulateGattNotifySessionStopped(characteristic1_);
  3303. base::RunLoop().RunUntilIdle();
  3304. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3305. SimulateGattNotifySessionStarted(characteristic1_);
  3306. base::RunLoop().RunUntilIdle();
  3307. ExpectedChangeNotifyValueAttempts(3);
  3308. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  3309. // Check the notify state
  3310. ASSERT_EQ(2u, notify_sessions_.size());
  3311. ASSERT_TRUE(notify_sessions_[0]);
  3312. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3313. ASSERT_TRUE(notify_sessions_[1]);
  3314. EXPECT_TRUE(notify_sessions_[1]->IsActive());
  3315. EXPECT_TRUE(characteristic1_->IsNotifying());
  3316. }
  3317. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3318. #define MAYBE_StopNotifySession_Reentrant_Success_Stop \
  3319. StopNotifySession_Reentrant_Success_Stop
  3320. #else
  3321. #define MAYBE_StopNotifySession_Reentrant_Success_Stop \
  3322. DISABLED_StopNotifySession_Reentrant_Success_Stop
  3323. #endif
  3324. #if BUILDFLAG(IS_WIN)
  3325. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3326. StopNotifySession_Reentrant_Success_Stop) {
  3327. #else
  3328. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3329. MAYBE_StopNotifySession_Reentrant_Success_Stop) {
  3330. #endif
  3331. if (!PlatformSupportsLowEnergy()) {
  3332. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3333. return;
  3334. }
  3335. ASSERT_NO_FATAL_FAILURE(
  3336. FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
  3337. SimulateGattDescriptor(
  3338. characteristic1_,
  3339. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  3340. .canonical_value());
  3341. base::RunLoop().RunUntilIdle();
  3342. ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
  3343. // Start notify session
  3344. characteristic1_->StartNotifySession(
  3345. base::BindOnce(
  3346. [](BluetoothRemoteGattCharacteristic::NotifySessionCallback
  3347. notify_callback,
  3348. base::OnceClosure stop_notify_callback,
  3349. std::unique_ptr<BluetoothGattNotifySession> session) {
  3350. BluetoothGattNotifySession* s = session.get();
  3351. std::move(notify_callback).Run(std::move(session));
  3352. s->Stop(std::move(stop_notify_callback));
  3353. },
  3354. GetNotifyCallback(Call::EXPECTED),
  3355. GetStopNotifyCallback(Call::EXPECTED)),
  3356. GetGattErrorCallback(Call::NOT_EXPECTED));
  3357. SimulateGattNotifySessionStarted(characteristic1_);
  3358. base::RunLoop().RunUntilIdle();
  3359. ASSERT_EQ(1u, notify_sessions_.size());
  3360. ASSERT_TRUE(notify_sessions_[0]);
  3361. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3362. EXPECT_TRUE(characteristic1_->IsNotifying());
  3363. SimulateGattNotifySessionStopped(characteristic1_);
  3364. base::RunLoop().RunUntilIdle();
  3365. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3366. EXPECT_FALSE(characteristic1_->IsNotifying());
  3367. }
  3368. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3369. #define MAYBE_StopNotifySession_Reentrant_Stop_StartSuccess \
  3370. StopNotifySession_Reentrant_Stop_StartSuccess
  3371. #else
  3372. #define MAYBE_StopNotifySession_Reentrant_Stop_StartSuccess \
  3373. DISABLED_StopNotifySession_Reentrant_Stop_StartSuccess
  3374. #endif
  3375. #if BUILDFLAG(IS_WIN)
  3376. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3377. StopNotifySession_Reentrant_Stop_StartSuccess) {
  3378. #else
  3379. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3380. MAYBE_StopNotifySession_Reentrant_Stop_StartSuccess) {
  3381. #endif
  3382. if (!PlatformSupportsLowEnergy()) {
  3383. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3384. return;
  3385. }
  3386. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  3387. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  3388. // Check that the notify session is active.
  3389. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  3390. EXPECT_EQ(characteristic1_->GetIdentifier(),
  3391. notify_sessions_[0]->GetCharacteristicIdentifier());
  3392. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  3393. EXPECT_TRUE(characteristic1_->IsNotifying());
  3394. notify_sessions_[0]->Stop(base::BindOnce(
  3395. [](BluetoothRemoteGattCharacteristic* characteristic,
  3396. BluetoothRemoteGattCharacteristic::NotifySessionCallback
  3397. notify_callback,
  3398. BluetoothRemoteGattCharacteristic::ErrorCallback error_callback) {
  3399. characteristic->StartNotifySession(std::move(notify_callback),
  3400. std::move(error_callback));
  3401. },
  3402. characteristic1_, GetNotifyCallback(Call::EXPECTED),
  3403. GetGattErrorCallback(Call::NOT_EXPECTED)));
  3404. SimulateGattNotifySessionStopped(characteristic1_);
  3405. base::RunLoop().RunUntilIdle();
  3406. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3407. EXPECT_FALSE(characteristic1_->IsNotifying());
  3408. SimulateGattNotifySessionStarted(characteristic1_);
  3409. base::RunLoop().RunUntilIdle();
  3410. ASSERT_EQ(2u, notify_sessions_.size());
  3411. ASSERT_TRUE(notify_sessions_[0]);
  3412. ASSERT_TRUE(notify_sessions_[1]);
  3413. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  3414. EXPECT_EQ(characteristic1_, notify_sessions_[1]->GetCharacteristic());
  3415. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3416. EXPECT_TRUE(notify_sessions_[1]->IsActive());
  3417. EXPECT_TRUE(characteristic1_->IsNotifying());
  3418. }
  3419. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3420. #define MAYBE_StopNotifySession_Reentrant_Stop_StartError \
  3421. StopNotifySession_Reentrant_Stop_StartError
  3422. #else
  3423. #define MAYBE_StopNotifySession_Reentrant_Stop_StartError \
  3424. DISABLED_StopNotifySession_Reentrant_Stop_StartError
  3425. #endif
  3426. #if BUILDFLAG(IS_WIN)
  3427. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3428. StopNotifySession_Reentrant_Stop_StartError) {
  3429. #else
  3430. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3431. MAYBE_StopNotifySession_Reentrant_Stop_StartError) {
  3432. #endif
  3433. if (!PlatformSupportsLowEnergy()) {
  3434. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3435. return;
  3436. }
  3437. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  3438. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  3439. // Check that the notify session is active.
  3440. EXPECT_TRUE(notify_sessions_[0]->IsActive());
  3441. EXPECT_EQ(characteristic1_->GetIdentifier(),
  3442. notify_sessions_[0]->GetCharacteristicIdentifier());
  3443. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  3444. EXPECT_TRUE(characteristic1_->IsNotifying());
  3445. notify_sessions_[0]->Stop(base::BindOnce(
  3446. [](BluetoothRemoteGattCharacteristic* characteristic,
  3447. BluetoothRemoteGattCharacteristic::NotifySessionCallback
  3448. notify_callback,
  3449. BluetoothRemoteGattCharacteristic::ErrorCallback error_callback) {
  3450. characteristic->StartNotifySession(std::move(notify_callback),
  3451. std::move(error_callback));
  3452. },
  3453. characteristic1_, GetNotifyCallback(Call::NOT_EXPECTED),
  3454. GetGattErrorCallback(Call::EXPECTED)));
  3455. SimulateGattNotifySessionStopped(characteristic1_);
  3456. base::RunLoop().RunUntilIdle();
  3457. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3458. EXPECT_FALSE(characteristic1_->IsNotifying());
  3459. SimulateGattNotifySessionStartError(characteristic1_,
  3460. BluetoothGattService::GATT_ERROR_FAILED);
  3461. base::RunLoop().RunUntilIdle();
  3462. ASSERT_EQ(1u, notify_sessions_.size());
  3463. ASSERT_TRUE(notify_sessions_[0]);
  3464. EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
  3465. EXPECT_FALSE(notify_sessions_[0]->IsActive());
  3466. EXPECT_FALSE(characteristic1_->IsNotifying());
  3467. }
  3468. #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  3469. #define MAYBE_GattCharacteristicAdded GattCharacteristicAdded
  3470. #else
  3471. #define MAYBE_GattCharacteristicAdded DISABLED_GattCharacteristicAdded
  3472. #endif
  3473. // TODO(786473) Android should report that services are discovered when a
  3474. // characteristic is added, but currently does not.
  3475. #if BUILDFLAG(IS_WIN)
  3476. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, GattCharacteristicAdded) {
  3477. #else
  3478. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_GattCharacteristicAdded) {
  3479. #endif
  3480. if (!PlatformSupportsLowEnergy()) {
  3481. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3482. return;
  3483. }
  3484. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
  3485. TestBluetoothAdapterObserver observer(adapter_);
  3486. SimulateGattCharacteristic(service_, kTestUUIDDeviceName, 0);
  3487. base::RunLoop().RunUntilIdle();
  3488. EXPECT_EQ(1, observer.gatt_services_discovered_count());
  3489. }
  3490. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3491. #define MAYBE_GattCharacteristicValueChanged GattCharacteristicValueChanged
  3492. #else
  3493. #define MAYBE_GattCharacteristicValueChanged \
  3494. DISABLED_GattCharacteristicValueChanged
  3495. #endif
  3496. // Tests Characteristic Value changes during a Notify Session.
  3497. #if BUILDFLAG(IS_WIN)
  3498. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  3499. GattCharacteristicValueChanged) {
  3500. #else
  3501. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3502. MAYBE_GattCharacteristicValueChanged) {
  3503. #endif
  3504. if (!PlatformSupportsLowEnergy()) {
  3505. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3506. return;
  3507. }
  3508. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  3509. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  3510. TestBluetoothAdapterObserver observer(adapter_);
  3511. std::vector<uint8_t> test_vector1, test_vector2;
  3512. test_vector1.push_back(111);
  3513. test_vector2.push_back(222);
  3514. SimulateGattCharacteristicChanged(characteristic1_, test_vector1);
  3515. base::RunLoop().RunUntilIdle();
  3516. EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count());
  3517. EXPECT_EQ(test_vector1, characteristic1_->GetValue());
  3518. SimulateGattCharacteristicChanged(characteristic1_, test_vector2);
  3519. base::RunLoop().RunUntilIdle();
  3520. EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count());
  3521. EXPECT_EQ(test_vector2, characteristic1_->GetValue());
  3522. }
  3523. #if BUILDFLAG(IS_ANDROID)
  3524. #define MAYBE_TwoGattCharacteristicValueChanges \
  3525. TwoGattCharacteristicValueChanges
  3526. #else
  3527. #define MAYBE_TwoGattCharacteristicValueChanges \
  3528. DISABLED_TwoGattCharacteristicValueChanges
  3529. #endif
  3530. // Tests that Characteristic value changes arriving consecutively result in
  3531. // two notifications with correct values.
  3532. // macOS: Does not apply. All events arrive on the UI Thread.
  3533. // TODO(crbug.com/694102): Enable this test on Windows.
  3534. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3535. MAYBE_TwoGattCharacteristicValueChanges) {
  3536. if (!PlatformSupportsLowEnergy()) {
  3537. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3538. return;
  3539. }
  3540. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  3541. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  3542. TestBluetoothAdapterObserver observer(adapter_);
  3543. std::vector<uint8_t> test_vector1({111});
  3544. std::vector<uint8_t> test_vector2({222});
  3545. SimulateGattCharacteristicChanged(characteristic1_, test_vector1);
  3546. SimulateGattCharacteristicChanged(characteristic1_, test_vector2);
  3547. base::RunLoop().RunUntilIdle();
  3548. EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count());
  3549. EXPECT_EQ(test_vector2, characteristic1_->GetValue());
  3550. EXPECT_EQ(std::vector<std::vector<uint8_t>>({test_vector1, test_vector2}),
  3551. observer.previous_characteristic_value_changed_values());
  3552. }
  3553. #if BUILDFLAG(IS_ANDROID)
  3554. #define MAYBE_GattCharacteristicValueChanged_AfterDeleted \
  3555. GattCharacteristicValueChanged_AfterDeleted
  3556. #else
  3557. #define MAYBE_GattCharacteristicValueChanged_AfterDeleted \
  3558. DISABLED_GattCharacteristicValueChanged_AfterDeleted
  3559. #endif
  3560. // Tests Characteristic Value changing after a Notify Session and objects being
  3561. // destroyed.
  3562. // macOS: Not applicable: This can never happen if CBPeripheral delegate is set
  3563. // to nil.
  3564. #if BUILDFLAG(IS_WIN)
  3565. TEST_P(BluetoothRemoteGattCharacteristicTestWin32Only,
  3566. GattCharacteristicValueChanged_AfterDeleted) {
  3567. #else
  3568. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3569. MAYBE_GattCharacteristicValueChanged_AfterDeleted) {
  3570. #endif
  3571. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  3572. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  3573. TestBluetoothAdapterObserver observer(adapter_);
  3574. RememberCharacteristicForSubsequentAction(characteristic1_);
  3575. DeleteDevice(device_); // TODO(576906) delete only the characteristic.
  3576. std::vector<uint8_t> empty_vector;
  3577. SimulateGattCharacteristicChanged(/* use remembered characteristic */ nullptr,
  3578. empty_vector);
  3579. base::RunLoop().RunUntilIdle();
  3580. EXPECT_TRUE("Did not crash!");
  3581. EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count());
  3582. }
  3583. // Tests that closing the GATT connection during a characteristic
  3584. // value notification is safe.
  3585. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3586. #define MAYBE_GattCharacteristicValueChanged_DisconnectDuring \
  3587. GattCharacteristicValueChanged_DisconnectDuring
  3588. #else
  3589. #define MAYBE_GattCharacteristicValueChanged_DisconnectDuring \
  3590. DISABLED_GattCharacteristicValueChanged_DisconnectDuring
  3591. #endif
  3592. #if BUILDFLAG(IS_WIN)
  3593. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3594. GattCharacteristicValueChanged_DisconnectDuring) {
  3595. #else
  3596. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3597. MAYBE_GattCharacteristicValueChanged_DisconnectDuring) {
  3598. #endif
  3599. if (!PlatformSupportsLowEnergy()) {
  3600. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3601. return;
  3602. }
  3603. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  3604. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  3605. MockBluetoothAdapter::Observer observer1(adapter_);
  3606. MockBluetoothAdapter::Observer observer2(adapter_);
  3607. // |observer1| will be notified first and close the GATT connection which
  3608. // may prevent |observer2| from being notified if |characteristic1_| has been
  3609. // freed.
  3610. base::RunLoop loop;
  3611. EXPECT_CALL(observer1, GattCharacteristicValueChanged(
  3612. adapter_.get(), characteristic1_.get(), _))
  3613. .WillOnce(
  3614. Invoke([&](BluetoothAdapter*, BluetoothRemoteGattCharacteristic*,
  3615. const std::vector<uint8_t>& value) {
  3616. gatt_connections_[0]->Disconnect();
  3617. loop.Quit();
  3618. }));
  3619. EXPECT_CALL(observer2, GattCharacteristicValueChanged(
  3620. adapter_.get(), characteristic1_.get(), _))
  3621. .Times(testing::AtMost(1))
  3622. .WillRepeatedly(
  3623. Invoke([&](BluetoothAdapter*,
  3624. BluetoothRemoteGattCharacteristic* characteristic,
  3625. const std::vector<uint8_t>& value) {
  3626. // Call a method on |characteristic| to check the pointer is still
  3627. // valid.
  3628. EXPECT_EQ(value, characteristic->GetValue());
  3629. }));
  3630. std::vector<uint8_t> empty_vector;
  3631. SimulateGattCharacteristicChanged(characteristic1_, empty_vector);
  3632. loop.Run();
  3633. }
  3634. #if BUILDFLAG(IS_ANDROID)
  3635. #define MAYBE_GetDescriptors_FindNone GetDescriptors_FindNone
  3636. #else
  3637. #define MAYBE_GetDescriptors_FindNone DISABLED_GetDescriptors_FindNone
  3638. #endif
  3639. #if BUILDFLAG(IS_WIN)
  3640. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, GetDescriptors_FindNone) {
  3641. #else
  3642. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_GetDescriptors_FindNone) {
  3643. #endif
  3644. if (!PlatformSupportsLowEnergy()) {
  3645. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3646. return;
  3647. }
  3648. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
  3649. EXPECT_EQ(0u, characteristic1_->GetDescriptors().size());
  3650. }
  3651. #if BUILDFLAG(IS_ANDROID)
  3652. #define MAYBE_GetDescriptors_and_GetDescriptor GetDescriptors_and_GetDescriptor
  3653. #else
  3654. #define MAYBE_GetDescriptors_and_GetDescriptor \
  3655. DISABLED_GetDescriptors_and_GetDescriptor
  3656. #endif
  3657. #if BUILDFLAG(IS_WIN)
  3658. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt,
  3659. GetDescriptors_and_GetDescriptor) {
  3660. #else
  3661. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3662. MAYBE_GetDescriptors_and_GetDescriptor) {
  3663. #endif
  3664. if (!PlatformSupportsLowEnergy()) {
  3665. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3666. return;
  3667. }
  3668. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
  3669. // Add several Descriptors:
  3670. BluetoothUUID uuid1(kTestUUIDCharacteristicUserDescription);
  3671. BluetoothUUID uuid2(kTestUUIDClientCharacteristicConfiguration);
  3672. BluetoothUUID uuid3(kTestUUIDServerCharacteristicConfiguration);
  3673. BluetoothUUID uuid4(kTestUUIDCharacteristicPresentationFormat);
  3674. SimulateGattDescriptor(characteristic1_, uuid1.canonical_value());
  3675. SimulateGattDescriptor(characteristic1_, uuid2.canonical_value());
  3676. SimulateGattDescriptor(characteristic2_, uuid3.canonical_value());
  3677. SimulateGattDescriptor(characteristic2_, uuid4.canonical_value());
  3678. base::RunLoop().RunUntilIdle();
  3679. // Verify that GetDescriptor can retrieve descriptors again by ID,
  3680. // and that the same Descriptor is returned when searched by ID.
  3681. EXPECT_EQ(2u, characteristic1_->GetDescriptors().size());
  3682. EXPECT_EQ(2u, characteristic2_->GetDescriptors().size());
  3683. std::string c1_id1 = characteristic1_->GetDescriptors()[0]->GetIdentifier();
  3684. std::string c1_id2 = characteristic1_->GetDescriptors()[1]->GetIdentifier();
  3685. std::string c2_id1 = characteristic2_->GetDescriptors()[0]->GetIdentifier();
  3686. std::string c2_id2 = characteristic2_->GetDescriptors()[1]->GetIdentifier();
  3687. BluetoothUUID c1_uuid1 = characteristic1_->GetDescriptors()[0]->GetUUID();
  3688. BluetoothUUID c1_uuid2 = characteristic1_->GetDescriptors()[1]->GetUUID();
  3689. BluetoothUUID c2_uuid1 = characteristic2_->GetDescriptors()[0]->GetUUID();
  3690. BluetoothUUID c2_uuid2 = characteristic2_->GetDescriptors()[1]->GetUUID();
  3691. EXPECT_EQ(c1_uuid1, characteristic1_->GetDescriptor(c1_id1)->GetUUID());
  3692. EXPECT_EQ(c1_uuid2, characteristic1_->GetDescriptor(c1_id2)->GetUUID());
  3693. EXPECT_EQ(c2_uuid1, characteristic2_->GetDescriptor(c2_id1)->GetUUID());
  3694. EXPECT_EQ(c2_uuid2, characteristic2_->GetDescriptor(c2_id2)->GetUUID());
  3695. // GetDescriptors & GetDescriptor return the same object for the same ID:
  3696. EXPECT_EQ(characteristic1_->GetDescriptors()[0],
  3697. characteristic1_->GetDescriptor(c1_id1));
  3698. EXPECT_EQ(characteristic1_->GetDescriptor(c1_id1),
  3699. characteristic1_->GetDescriptor(c1_id1));
  3700. // Characteristic 1 has descriptor uuids 1 and 2 (we don't know the order).
  3701. EXPECT_TRUE(c1_uuid1 == uuid1 || c1_uuid2 == uuid1);
  3702. EXPECT_TRUE(c1_uuid1 == uuid2 || c1_uuid2 == uuid2);
  3703. // ... but not uuid 3
  3704. EXPECT_FALSE(c1_uuid1 == uuid3 || c1_uuid2 == uuid3);
  3705. }
  3706. #if BUILDFLAG(IS_ANDROID)
  3707. #define MAYBE_GetDescriptorsByUUID GetDescriptorsByUUID
  3708. #else
  3709. #define MAYBE_GetDescriptorsByUUID DISABLED_GetDescriptorsByUUID
  3710. #endif
  3711. #if BUILDFLAG(IS_WIN)
  3712. TEST_P(BluetoothRemoteGattCharacteristicTestWinrt, GetDescriptorsByUUID) {
  3713. #else
  3714. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_GetDescriptorsByUUID) {
  3715. #endif
  3716. if (!PlatformSupportsLowEnergy()) {
  3717. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3718. return;
  3719. }
  3720. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate());
  3721. // Add several Descriptors:
  3722. BluetoothUUID id1(kTestUUIDCharacteristicUserDescription);
  3723. BluetoothUUID id2(kTestUUIDClientCharacteristicConfiguration);
  3724. BluetoothUUID id3(kTestUUIDServerCharacteristicConfiguration);
  3725. SimulateGattDescriptor(characteristic1_, id1.canonical_value());
  3726. SimulateGattDescriptor(characteristic1_, id2.canonical_value());
  3727. SimulateGattDescriptor(characteristic2_, id3.canonical_value());
  3728. SimulateGattDescriptor(characteristic2_, id3.canonical_value());
  3729. base::RunLoop().RunUntilIdle();
  3730. EXPECT_NE(characteristic2_->GetDescriptorsByUUID(id3).at(0)->GetIdentifier(),
  3731. characteristic2_->GetDescriptorsByUUID(id3).at(1)->GetIdentifier());
  3732. EXPECT_EQ(id1, characteristic1_->GetDescriptorsByUUID(id1).at(0)->GetUUID());
  3733. EXPECT_EQ(id2, characteristic1_->GetDescriptorsByUUID(id2).at(0)->GetUUID());
  3734. EXPECT_EQ(id3, characteristic2_->GetDescriptorsByUUID(id3).at(0)->GetUUID());
  3735. EXPECT_EQ(id3, characteristic2_->GetDescriptorsByUUID(id3).at(1)->GetUUID());
  3736. EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id1).size());
  3737. EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id2).size());
  3738. EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size());
  3739. EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size());
  3740. EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size());
  3741. EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size());
  3742. }
  3743. #if BUILDFLAG(IS_ANDROID)
  3744. #define MAYBE_ReadDuringDisconnect ReadDuringDisconnect
  3745. #else
  3746. #define MAYBE_ReadDuringDisconnect DISABLED_ReadDuringDisconnect
  3747. #endif
  3748. // Tests that read requests after a device disconnects but before the disconnect
  3749. // task has a chance to run result in an error.
  3750. // macOS: Does not apply. All events arrive on the UI Thread.
  3751. // TODO(crbug.com/694102): Enable this test on Windows.
  3752. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_ReadDuringDisconnect) {
  3753. if (!PlatformSupportsLowEnergy()) {
  3754. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3755. return;
  3756. }
  3757. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  3758. BluetoothRemoteGattCharacteristic::PROPERTY_READ));
  3759. SimulateGattDisconnection(device_);
  3760. // Do not yet call RunUntilIdle() to process the disconnect task.
  3761. characteristic1_->ReadRemoteCharacteristic(
  3762. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  3763. base::RunLoop().RunUntilIdle();
  3764. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  3765. }
  3766. #if BUILDFLAG(IS_ANDROID)
  3767. #define MAYBE_WriteDuringDisconnect WriteDuringDisconnect
  3768. #else
  3769. #define MAYBE_WriteDuringDisconnect DISABLED_WriteDuringDisconnect
  3770. #endif
  3771. // Tests that write requests after a device disconnects but before the
  3772. // disconnect task runs result in an error.
  3773. // macOS: Does not apply. All events arrive on the UI Thread.
  3774. // TODO(crbug.com/694102): Enable this test on Windows.
  3775. TEST_F(BluetoothRemoteGattCharacteristicTest, MAYBE_WriteDuringDisconnect) {
  3776. if (!PlatformSupportsLowEnergy()) {
  3777. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3778. return;
  3779. }
  3780. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  3781. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  3782. base::RunLoop loop;
  3783. SimulateGattDisconnection(device_);
  3784. // Do not yet call loop.Run() to process the disconnect task.
  3785. characteristic1_->WriteRemoteCharacteristic(
  3786. std::vector<uint8_t>(), WriteType::kWithResponse,
  3787. base::BindLambdaForTesting([&] {
  3788. ADD_FAILURE() << "unexpected success";
  3789. loop.Quit();
  3790. }),
  3791. base::BindLambdaForTesting(
  3792. [&](BluetoothGattService::GattErrorCode error_code) {
  3793. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  3794. loop.Quit();
  3795. }));
  3796. loop.Run();
  3797. }
  3798. #if BUILDFLAG(IS_ANDROID)
  3799. #define MAYBE_DeprecatedWriteDuringDisconnect DeprecatedWriteDuringDisconnect
  3800. #else
  3801. #define MAYBE_DeprecatedWriteDuringDisconnect \
  3802. DISABLED_DeprecatedWriteDuringDisconnect
  3803. #endif
  3804. // Tests that write requests after a device disconnects but before the
  3805. // disconnect task runs result in an error.
  3806. // macOS: Does not apply. All events arrive on the UI Thread.
  3807. // TODO(crbug.com/694102): Enable this test on Windows.
  3808. TEST_F(BluetoothRemoteGattCharacteristicTest,
  3809. MAYBE_DeprecatedWriteDuringDisconnect) {
  3810. if (!PlatformSupportsLowEnergy()) {
  3811. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3812. return;
  3813. }
  3814. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  3815. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  3816. SimulateGattDisconnection(device_);
  3817. // Do not yet call RunUntilIdle() to process the disconnect task.
  3818. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  3819. std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED),
  3820. GetGattErrorCallback(Call::EXPECTED));
  3821. base::RunLoop().RunUntilIdle();
  3822. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  3823. }
  3824. #if BUILDFLAG(IS_MAC)
  3825. #define MAYBE_WriteWithoutResponseOnlyCharacteristic_WriteRemoteCharacteristicDuringDisconnect \
  3826. WriteWithoutResponseOnlyCharacteristic_WriteRemoteCharacteristicDuringDisconnect
  3827. #else
  3828. #define MAYBE_WriteWithoutResponseOnlyCharacteristic_WriteRemoteCharacteristicDuringDisconnect \
  3829. DISABLED_WriteWithoutResponseOnlyCharacteristic_WriteRemoteCharacteristicDuringDisconnect
  3830. #endif
  3831. // Tests that writing without response during a disconnect results in an error.
  3832. // Only applies to macOS and WinRT whose events arrive all on the UI thread. See
  3833. // other *DuringDisconnect tests for Android and Windows whose events arrive on
  3834. // a different thread.
  3835. #if BUILDFLAG(IS_WIN)
  3836. TEST_P(
  3837. BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3838. WriteWithoutResponseOnlyCharacteristic_WriteRemoteCharacteristicDuringDisconnect) {
  3839. #else
  3840. TEST_F(
  3841. BluetoothRemoteGattCharacteristicTest,
  3842. MAYBE_WriteWithoutResponseOnlyCharacteristic_WriteRemoteCharacteristicDuringDisconnect) {
  3843. #endif
  3844. if (!PlatformSupportsLowEnergy()) {
  3845. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3846. return;
  3847. }
  3848. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  3849. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE));
  3850. base::RunLoop loop;
  3851. characteristic1_->WriteRemoteCharacteristic(
  3852. std::vector<uint8_t>(), WriteType::kWithoutResponse,
  3853. base::BindLambdaForTesting([&] {
  3854. ADD_FAILURE() << "unexpected success";
  3855. loop.Quit();
  3856. }),
  3857. base::BindLambdaForTesting(
  3858. [&](BluetoothGattService::GattErrorCode error_code) {
  3859. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  3860. loop.Quit();
  3861. }));
  3862. SimulateDeviceBreaksConnection(adapter_->GetDevices()[0]);
  3863. loop.Run();
  3864. }
  3865. #if BUILDFLAG(IS_MAC)
  3866. #define MAYBE_WriteWithoutResponseOnlyCharacteristic_DeprecatedWriteRemoteCharacteristicDuringDisconnect \
  3867. WriteWithoutResponseOnlyCharacteristic_DeprecatedWriteRemoteCharacteristicDuringDisconnect
  3868. #else
  3869. #define MAYBE_WriteWithoutResponseOnlyCharacteristic_DeprecatedWriteRemoteCharacteristicDuringDisconnect \
  3870. DISABLED_WriteWithoutResponseOnlyCharacteristic_DeprecatedWriteRemoteCharacteristicDuringDisconnect
  3871. #endif
  3872. // Tests that writing without response during a disconnect results in an error.
  3873. // Only applies to macOS and WinRT whose events arrive all on the UI thread. See
  3874. // other *DuringDisconnect tests for Android and Windows whose events arrive on
  3875. // a different thread.
  3876. #if BUILDFLAG(IS_WIN)
  3877. TEST_P(
  3878. BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3879. WriteWithoutResponseOnlyCharacteristic_DeprecatedWriteRemoteCharacteristicDuringDisconnect) {
  3880. #else
  3881. TEST_F(
  3882. BluetoothRemoteGattCharacteristicTest,
  3883. MAYBE_WriteWithoutResponseOnlyCharacteristic_DeprecatedWriteRemoteCharacteristicDuringDisconnect) {
  3884. #endif
  3885. if (!PlatformSupportsLowEnergy()) {
  3886. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3887. return;
  3888. }
  3889. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  3890. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE));
  3891. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  3892. std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED),
  3893. GetGattErrorCallback(Call::EXPECTED));
  3894. SimulateDeviceBreaksConnection(adapter_->GetDevices()[0]);
  3895. base::RunLoop().RunUntilIdle();
  3896. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  3897. }
  3898. // Tests that closing the GATT connection when a characteristic value write
  3899. // fails due to a disconnect is safe.
  3900. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3901. #define MAYBE_WriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect \
  3902. WriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect
  3903. #else
  3904. #define MAYBE_WriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect \
  3905. DISABLED_WriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect
  3906. #endif
  3907. #if BUILDFLAG(IS_WIN)
  3908. TEST_P(BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3909. WriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect) {
  3910. #else
  3911. TEST_F(
  3912. BluetoothRemoteGattCharacteristicTest,
  3913. MAYBE_WriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect) {
  3914. #endif
  3915. if (!PlatformSupportsLowEnergy()) {
  3916. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3917. return;
  3918. }
  3919. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  3920. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE));
  3921. base::RunLoop loop;
  3922. characteristic1_->WriteRemoteCharacteristic(
  3923. std::vector<uint8_t>(), WriteType::kWithoutResponse,
  3924. base::BindLambdaForTesting([&] {
  3925. ADD_FAILURE() << "unexpected success";
  3926. loop.Quit();
  3927. }),
  3928. base::BindLambdaForTesting(
  3929. [&](BluetoothGattService::GattErrorCode error_code) {
  3930. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  3931. gatt_connections_[0]->Disconnect();
  3932. loop.Quit();
  3933. }));
  3934. SimulateDeviceBreaksConnection(adapter_->GetDevices()[0]);
  3935. loop.Run();
  3936. }
  3937. // Tests that closing the GATT connection when a characteristic value write
  3938. // fails due to a disconnect is safe.
  3939. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
  3940. #define MAYBE_DeprecatedWriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect \
  3941. DeprecatedWriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect
  3942. #else
  3943. #define MAYBE_DeprecatedWriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect \
  3944. DISABLED_DeprecatedWriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect
  3945. #endif
  3946. #if BUILDFLAG(IS_WIN)
  3947. TEST_P(
  3948. BluetoothRemoteGattCharacteristicTestWinrtOnly,
  3949. DeprecatedWriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect) {
  3950. #else
  3951. TEST_F(
  3952. BluetoothRemoteGattCharacteristicTest,
  3953. MAYBE_DeprecatedWriteWithoutResponseOnlyCharacteristic_CloseConnectionDuringDisconnect) {
  3954. #endif
  3955. if (!PlatformSupportsLowEnergy()) {
  3956. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3957. return;
  3958. }
  3959. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  3960. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE));
  3961. base::RunLoop loop;
  3962. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  3963. std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED),
  3964. base::BindLambdaForTesting(
  3965. [&](BluetoothGattService::GattErrorCode error_code) {
  3966. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  3967. gatt_connections_[0]->Disconnect();
  3968. loop.Quit();
  3969. }));
  3970. SimulateDeviceBreaksConnection(adapter_->GetDevices()[0]);
  3971. loop.Run();
  3972. }
  3973. #if BUILDFLAG(IS_MAC)
  3974. #define MAYBE_WriteWithoutResponseOnlyCharacteristic_DisconnectCalledDuringWriteRemoteCharacteristic \
  3975. WriteWithoutResponseOnlyCharacteristic_DisconnectCalledDuringWriteRemoteCharacteristic
  3976. #else
  3977. #define MAYBE_WriteWithoutResponseOnlyCharacteristic_DisconnectCalledDuringWriteRemoteCharacteristic \
  3978. DISABLED_WriteWithoutResponseOnlyCharacteristic_DisconnectCalledDuringWriteRemoteCharacteristic
  3979. #endif
  3980. // Tests that disconnecting right after a write without response results in an
  3981. // error.
  3982. // TODO(crbug.com/726534): Enable on other platforms depending on the resolution
  3983. // of crbug.com/726534.
  3984. TEST_F(
  3985. BluetoothRemoteGattCharacteristicTest,
  3986. MAYBE_WriteWithoutResponseOnlyCharacteristic_DisconnectCalledDuringWriteRemoteCharacteristic) {
  3987. if (!PlatformSupportsLowEnergy()) {
  3988. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  3989. return;
  3990. }
  3991. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  3992. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE));
  3993. base::RunLoop loop;
  3994. characteristic1_->WriteRemoteCharacteristic(
  3995. std::vector<uint8_t>(), WriteType::kWithoutResponse,
  3996. base::BindLambdaForTesting([&] {
  3997. ADD_FAILURE() << "unexpected success";
  3998. loop.Quit();
  3999. }),
  4000. base::BindLambdaForTesting(
  4001. [&](BluetoothGattService::GattErrorCode error_code) {
  4002. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  4003. loop.Quit();
  4004. }));
  4005. gatt_connections_[0]->Disconnect();
  4006. loop.Run();
  4007. SimulateGattDisconnection(device_);
  4008. base::RunLoop().RunUntilIdle();
  4009. }
  4010. #if BUILDFLAG(IS_MAC)
  4011. #define MAYBE_DeprecatedWriteWithoutResponseOnlyCharacteristic_DisconnectCalledDuringWriteRemoteCharacteristic \
  4012. DeprecatedWriteWithoutResponseOnlyCharacteristic_DisconnectCalledDuringWriteRemoteCharacteristic
  4013. #else
  4014. #define MAYBE_DeprecatedWriteWithoutResponseOnlyCharacteristic_DisconnectCalledDuringWriteRemoteCharacteristic \
  4015. DISABLED_DeprecatedWriteWithoutResponseOnlyCharacteristic_DisconnectCalledDuringWriteRemoteCharacteristic
  4016. #endif
  4017. // Tests that disconnecting right after a write without response results in an
  4018. // error.
  4019. // TODO(crbug.com/726534): Enable on other platforms depending on the resolution
  4020. // of crbug.com/726534.
  4021. TEST_F(
  4022. BluetoothRemoteGattCharacteristicTest,
  4023. MAYBE_DeprecatedWriteWithoutResponseOnlyCharacteristic_DisconnectCalledDuringWriteRemoteCharacteristic) {
  4024. if (!PlatformSupportsLowEnergy()) {
  4025. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  4026. return;
  4027. }
  4028. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  4029. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE));
  4030. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  4031. std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED),
  4032. GetGattErrorCallback(Call::EXPECTED));
  4033. gatt_connections_[0]->Disconnect();
  4034. base::RunLoop().RunUntilIdle();
  4035. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  4036. SimulateGattDisconnection(device_);
  4037. base::RunLoop().RunUntilIdle();
  4038. }
  4039. #if BUILDFLAG(IS_MAC)
  4040. #define MAYBE_WriteWithoutResponseOnlyCharacteristic_DisconnectCalledBeforeWriteRemoteCharacteristic \
  4041. WriteWithoutResponseOnlyCharacteristic_DisconnectCalledBeforeWriteRemoteCharacteristic
  4042. #else
  4043. #define MAYBE_WriteWithoutResponseOnlyCharacteristic_DisconnectCalledBeforeWriteRemoteCharacteristic \
  4044. DISABLED_WriteWithoutResponseOnlyCharacteristic_DisconnectCalledBeforeWriteRemoteCharacteristic
  4045. #endif
  4046. // Tests that disconnecting right before a write without response results in an
  4047. // error.
  4048. // TODO(crbug.com/726534): Enable on other platforms depending on the resolution
  4049. // of crbug.com/726534.
  4050. TEST_F(
  4051. BluetoothRemoteGattCharacteristicTest,
  4052. MAYBE_WriteWithoutResponseOnlyCharacteristic_DisconnectCalledBeforeWriteRemoteCharacteristic) {
  4053. if (!PlatformSupportsLowEnergy()) {
  4054. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  4055. return;
  4056. }
  4057. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  4058. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE));
  4059. base::RunLoop loop;
  4060. gatt_connections_[0]->Disconnect();
  4061. characteristic1_->WriteRemoteCharacteristic(
  4062. std::vector<uint8_t>(), WriteType::kWithoutResponse,
  4063. base::BindLambdaForTesting([&] {
  4064. ADD_FAILURE() << "unexpected success";
  4065. loop.Quit();
  4066. }),
  4067. base::BindLambdaForTesting(
  4068. [&](BluetoothGattService::GattErrorCode error_code) {
  4069. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, error_code);
  4070. loop.Quit();
  4071. }));
  4072. loop.Run();
  4073. SimulateGattDisconnection(device_);
  4074. base::RunLoop().RunUntilIdle();
  4075. }
  4076. #if BUILDFLAG(IS_MAC)
  4077. #define MAYBE_DeprecatedWriteWithoutResponseOnlyCharacteristic_DisconnectCalledBeforeWriteRemoteCharacteristic \
  4078. DeprecatedWriteWithoutResponseOnlyCharacteristic_DisconnectCalledBeforeWriteRemoteCharacteristic
  4079. #else
  4080. #define MAYBE_DeprecatedWriteWithoutResponseOnlyCharacteristic_DisconnectCalledBeforeWriteRemoteCharacteristic \
  4081. DISABLED_DeprecatedWriteWithoutResponseOnlyCharacteristic_DisconnectCalledBeforeWriteRemoteCharacteristic
  4082. #endif
  4083. // Tests that disconnecting right before a write without response results in an
  4084. // error.
  4085. // TODO(crbug.com/726534): Enable on other platforms depending on the resolution
  4086. // of crbug.com/726534.
  4087. TEST_F(
  4088. BluetoothRemoteGattCharacteristicTest,
  4089. MAYBE_DeprecatedWriteWithoutResponseOnlyCharacteristic_DisconnectCalledBeforeWriteRemoteCharacteristic) {
  4090. if (!PlatformSupportsLowEnergy()) {
  4091. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  4092. return;
  4093. }
  4094. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  4095. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE));
  4096. gatt_connections_[0]->Disconnect();
  4097. characteristic1_->DeprecatedWriteRemoteCharacteristic(
  4098. std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED),
  4099. GetGattErrorCallback(Call::EXPECTED));
  4100. base::RunLoop().RunUntilIdle();
  4101. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  4102. SimulateGattDisconnection(device_);
  4103. base::RunLoop().RunUntilIdle();
  4104. }
  4105. #if BUILDFLAG(IS_ANDROID)
  4106. #define MAYBE_StartNotifySessionDuringDisconnect \
  4107. StartNotifySessionDuringDisconnect
  4108. #else
  4109. #define MAYBE_StartNotifySessionDuringDisconnect \
  4110. DISABLED_StartNotifySessionDuringDisconnect
  4111. #endif
  4112. // Tests that start notifications requests after a device disconnects but before
  4113. // the disconnect task runs result in an error.
  4114. // macOS: Does not apply. All events arrive on the UI Thread.
  4115. // TODO(crbug.com/694102): Enable this test on Windows.
  4116. TEST_F(BluetoothRemoteGattCharacteristicTest,
  4117. MAYBE_StartNotifySessionDuringDisconnect) {
  4118. if (!PlatformSupportsLowEnergy()) {
  4119. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  4120. return;
  4121. }
  4122. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  4123. BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY));
  4124. SimulateGattDescriptor(
  4125. characteristic1_,
  4126. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  4127. .canonical_value());
  4128. SimulateGattDisconnection(device_);
  4129. // Don't run the disconnect task.
  4130. characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
  4131. GetGattErrorCallback(Call::EXPECTED));
  4132. base::RunLoop().RunUntilIdle();
  4133. EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_);
  4134. }
  4135. #if BUILDFLAG(IS_ANDROID)
  4136. #define MAYBE_StopNotifySessionDuringDisconnect \
  4137. StopNotifySessionDuringDisconnect
  4138. #else
  4139. #define MAYBE_StopNotifySessionDuringDisconnect \
  4140. DISABLED_StopNotifySessionDuringDisconnect
  4141. #endif
  4142. // Tests that stop notifications requests after a device disconnects but before
  4143. // the disconnect task runs do not result in a crash.
  4144. // macOS: Does not apply. All events arrive on the UI Thread.
  4145. // TODO(crbug.com/694102): Enable this test on Windows.
  4146. TEST_F(BluetoothRemoteGattCharacteristicTest,
  4147. MAYBE_StopNotifySessionDuringDisconnect) {
  4148. if (!PlatformSupportsLowEnergy()) {
  4149. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  4150. return;
  4151. }
  4152. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  4153. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  4154. SimulateGattDisconnection(device_);
  4155. // Don't run the disconnect task.
  4156. notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
  4157. base::RunLoop().RunUntilIdle();
  4158. }
  4159. #if BUILDFLAG(IS_ANDROID)
  4160. #define MAYBE_DeleteNotifySessionDuringDisconnect \
  4161. DeleteNotifySessionDuringDisconnect
  4162. #else
  4163. #define MAYBE_DeleteNotifySessionDuringDisconnect \
  4164. DISABLED_DeleteNotifySessionDuringDisconnect
  4165. #endif
  4166. // Tests that deleting notify sessions after a device disconnects but before the
  4167. // disconnect task runs do not result in a crash.
  4168. // macOS: Does not apply. All events arrive on the UI Thread.
  4169. // TODO(crbug.com/694102): Enable this test on Windows.
  4170. TEST_F(BluetoothRemoteGattCharacteristicTest,
  4171. MAYBE_DeleteNotifySessionDuringDisconnect) {
  4172. if (!PlatformSupportsLowEnergy()) {
  4173. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  4174. return;
  4175. }
  4176. ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
  4177. /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY));
  4178. SimulateGattDisconnection(device_);
  4179. // Don't run the disconnect task.
  4180. notify_sessions_.clear();
  4181. base::RunLoop().RunUntilIdle();
  4182. }
  4183. #if BUILDFLAG(IS_MAC)
  4184. // Tests to receive a services changed notification from macOS, while
  4185. // discovering descriptors. This test simulate having 2 descriptor scan at the
  4186. // same time. Only once both descriptor scanning is done, the gatt device is
  4187. // ready.
  4188. // Android: This test doesn't apply to Android because there is no services
  4189. // changed event that could arrive during a discovery procedure.
  4190. TEST_F(BluetoothRemoteGattCharacteristicTest,
  4191. SimulateDeviceModificationWhileDiscoveringDescriptors) {
  4192. if (!PlatformSupportsLowEnergy()) {
  4193. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  4194. return;
  4195. }
  4196. InitWithFakeAdapter();
  4197. StartLowEnergyDiscoverySession();
  4198. BluetoothDevice* device = SimulateLowEnergyDevice(3);
  4199. device->CreateGattConnection(
  4200. GetGattConnectionCallback(Call::EXPECTED, Result::SUCCESS));
  4201. TestBluetoothAdapterObserver observer(adapter_);
  4202. // Starts first discovery process.
  4203. SimulateGattConnection(device);
  4204. EXPECT_EQ(1, observer.device_changed_count());
  4205. AddServicesToDeviceMac(device, {kTestUUIDHeartRate});
  4206. SimulateDidDiscoverServicesMac(device);
  4207. EXPECT_EQ(1u, device->GetGattServices().size());
  4208. BluetoothRemoteGattService* service = device->GetGattServices()[0];
  4209. std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb";
  4210. AddCharacteristicToServiceMac(service, characteristic_uuid,
  4211. /* properties */ 0);
  4212. SimulateDidDiscoverCharacteristicsMac(service);
  4213. EXPECT_EQ(1u, service->GetCharacteristics().size());
  4214. BluetoothRemoteGattCharacteristic* characteristic =
  4215. service->GetCharacteristics()[0];
  4216. std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb";
  4217. AddDescriptorToCharacteristicMac(characteristic, descriptor_uuid);
  4218. // Now waiting for descriptor discovery.
  4219. // Starts second discovery process.
  4220. SimulateGattServicesChanged(device);
  4221. EXPECT_EQ(2, observer.device_changed_count());
  4222. SimulateDidDiscoverServicesMac(device);
  4223. SimulateDidDiscoverCharacteristicsMac(service);
  4224. // Now waiting for a second descriptor discovery.
  4225. // Finish discovery process.
  4226. // First system call to -[id<CBPeripheralDelegate>
  4227. // peripheral:didDiscoverDescriptorsForCharacteristic:error:]
  4228. SimulateDidDiscoverDescriptorsMac(characteristic);
  4229. EXPECT_EQ(0, observer.gatt_service_changed_count());
  4230. EXPECT_EQ(1u, service->GetCharacteristics().size());
  4231. EXPECT_EQ(1u, characteristic->GetDescriptors().size());
  4232. EXPECT_EQ(2, observer.device_changed_count());
  4233. // Second system call to -[id<CBPeripheralDelegate>
  4234. // peripheral:didDiscoverDescriptorsForCharacteristic:error:]
  4235. // Finish second discovery process.
  4236. observer.Reset();
  4237. SimulateDidDiscoverDescriptorsMac(characteristic);
  4238. EXPECT_EQ(1, observer.gatt_service_changed_count());
  4239. EXPECT_EQ(1, observer.device_changed_count());
  4240. }
  4241. #endif // BUILDFLAG(IS_MAC)
  4242. #if BUILDFLAG(IS_MAC)
  4243. // Simulates to receive an extra discovery descriptor notifications from macOS.
  4244. // Those notifications should be ignored.
  4245. // Android: This test doesn't apply to Android because there is no services
  4246. // changed event that could arrive during a discovery procedure.
  4247. TEST_F(BluetoothRemoteGattCharacteristicTest, ExtraDidDiscoverDescriptorsCall) {
  4248. if (!PlatformSupportsLowEnergy()) {
  4249. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  4250. return;
  4251. }
  4252. InitWithFakeAdapter();
  4253. StartLowEnergyDiscoverySession();
  4254. BluetoothDevice* device = SimulateLowEnergyDevice(3);
  4255. device->CreateGattConnection(
  4256. GetGattConnectionCallback(Call::EXPECTED, Result::SUCCESS));
  4257. TestBluetoothAdapterObserver observer(adapter_);
  4258. // Starts first discovery process.
  4259. SimulateGattConnection(device);
  4260. AddServicesToDeviceMac(device, {kTestUUIDHeartRate});
  4261. SimulateDidDiscoverServicesMac(device);
  4262. EXPECT_EQ(1u, device->GetGattServices().size());
  4263. BluetoothRemoteGattService* service = device->GetGattServices()[0];
  4264. std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb";
  4265. AddCharacteristicToServiceMac(service, characteristic_uuid,
  4266. /* properties */ 0);
  4267. SimulateDidDiscoverCharacteristicsMac(service);
  4268. EXPECT_EQ(1u, service->GetCharacteristics().size());
  4269. BluetoothRemoteGattCharacteristic* characteristic =
  4270. service->GetCharacteristics()[0];
  4271. std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb";
  4272. AddDescriptorToCharacteristicMac(characteristic, descriptor_uuid);
  4273. SimulateDidDiscoverDescriptorsMac(characteristic);
  4274. EXPECT_EQ(1, observer.gatt_service_changed_count());
  4275. EXPECT_EQ(1u, service->GetCharacteristics().size());
  4276. EXPECT_EQ(1u, characteristic->GetDescriptors().size());
  4277. observer.Reset();
  4278. SimulateDidDiscoverDescriptorsMac(characteristic); // Extra system call.
  4279. SimulateGattServicesChanged(device);
  4280. SimulateDidDiscoverDescriptorsMac(characteristic); // Extra system call.
  4281. SimulateDidDiscoverServicesMac(device);
  4282. SimulateDidDiscoverDescriptorsMac(characteristic); // Extra system call.
  4283. SimulateDidDiscoverCharacteristicsMac(service);
  4284. SimulateDidDiscoverDescriptorsMac(characteristic);
  4285. SimulateDidDiscoverDescriptorsMac(characteristic); // Extra system call.
  4286. EXPECT_EQ(2, observer.device_changed_count());
  4287. }
  4288. #endif // BUILDFLAG(IS_MAC)
  4289. #if BUILDFLAG(IS_WIN)
  4290. INSTANTIATE_TEST_SUITE_P(All,
  4291. BluetoothRemoteGattCharacteristicTestWinrt,
  4292. ::testing::ValuesIn(kBluetoothTestWinrtParamAll));
  4293. INSTANTIATE_TEST_SUITE_P(
  4294. All,
  4295. BluetoothRemoteGattCharacteristicTestWin32Only,
  4296. ::testing::ValuesIn(kBluetoothTestWinrtParamWin32Only));
  4297. INSTANTIATE_TEST_SUITE_P(
  4298. All,
  4299. BluetoothRemoteGattCharacteristicTestWinrtOnly,
  4300. ::testing::ValuesIn(kBluetoothTestWinrtParamWinrtOnly));
  4301. #endif // BUILDFLAG(IS_WIN)
  4302. } // namespace device