app_list_view_unittest.cc 156 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061
  1. // Copyright 2014 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 "ash/app_list/views/app_list_view.h"
  5. #include <stddef.h>
  6. #include <algorithm>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "ash/app_list/app_list_test_view_delegate.h"
  13. #include "ash/app_list/model/app_list_test_model.h"
  14. #include "ash/app_list/model/search/test_search_result.h"
  15. #include "ash/app_list/views/app_list_folder_view.h"
  16. #include "ash/app_list/views/app_list_item_view.h"
  17. #include "ash/app_list/views/app_list_main_view.h"
  18. #include "ash/app_list/views/apps_container_view.h"
  19. #include "ash/app_list/views/apps_grid_view.h"
  20. #include "ash/app_list/views/apps_grid_view_test_api.h"
  21. #include "ash/app_list/views/contents_view.h"
  22. #include "ash/app_list/views/expand_arrow_view.h"
  23. #include "ash/app_list/views/folder_background_view.h"
  24. #include "ash/app_list/views/folder_header_view.h"
  25. #include "ash/app_list/views/page_switcher.h"
  26. #include "ash/app_list/views/paged_apps_grid_view.h"
  27. #include "ash/app_list/views/recent_apps_view.h"
  28. #include "ash/app_list/views/result_selection_controller.h"
  29. #include "ash/app_list/views/search_box_view.h"
  30. #include "ash/app_list/views/search_result_container_view.h"
  31. #include "ash/app_list/views/search_result_list_view.h"
  32. #include "ash/app_list/views/search_result_page_view.h"
  33. #include "ash/app_list/views/search_result_tile_item_list_view.h"
  34. #include "ash/app_list/views/search_result_tile_item_view.h"
  35. #include "ash/app_list/views/search_result_view.h"
  36. #include "ash/app_list/views/suggestion_chip_container_view.h"
  37. #include "ash/constants/ash_features.h"
  38. #include "ash/keyboard/ui/keyboard_ui_controller.h"
  39. #include "ash/public/cpp/app_list/app_list_config.h"
  40. #include "ash/public/cpp/app_list/app_list_types.h"
  41. #include "ash/public/cpp/pagination/pagination_model.h"
  42. #include "ash/public/cpp/test/test_app_list_color_provider.h"
  43. #include "ash/search_box/search_box_constants.h"
  44. #include "ash/style/ash_color_provider.h"
  45. #include "base/run_loop.h"
  46. #include "base/strings/string_number_conversions.h"
  47. #include "base/strings/utf_string_conversions.h"
  48. #include "base/test/icu_test_util.h"
  49. #include "base/test/metrics/histogram_tester.h"
  50. #include "base/test/scoped_feature_list.h"
  51. #include "build/build_config.h"
  52. #include "testing/gtest/include/gtest/gtest.h"
  53. #include "ui/compositor/layer.h"
  54. #include "ui/compositor/layer_animator.h"
  55. #include "ui/compositor/presentation_time_recorder.h"
  56. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  57. #include "ui/display/display.h"
  58. #include "ui/display/screen.h"
  59. #include "ui/events/event_utils.h"
  60. #include "ui/events/keycodes/keyboard_codes.h"
  61. #include "ui/views/controls/textfield/textfield.h"
  62. #include "ui/views/test/views_test_base.h"
  63. #include "ui/views/view_model.h"
  64. namespace ash {
  65. namespace {
  66. using test::AppListTestModel;
  67. using test::AppListTestViewDelegate;
  68. using test::AppsGridViewTestApi;
  69. constexpr int kInitialItems = 34;
  70. constexpr int kMaxItemsPerFolderPage = AppListFolderView::kMaxFolderColumns *
  71. AppListFolderView::kMaxPagedFolderRows;
  72. // Constants used for for testing app list layout in fullscreen state:
  73. // The total height of search box and suggestion chips views, including the
  74. // vertical margin between them.
  75. constexpr int kSearchBoxAndSuggestionChipsHeightDefault =
  76. 48 /* search box height */ +
  77. 16 /* margin between search box and suggestion chips */ +
  78. 32 /* suggestion chips container height */;
  79. constexpr int kSearchBoxAndSuggestionChipsHeightDense =
  80. 40 /* search box height */ +
  81. 16 /* margin between search box and suggestion chips */ +
  82. 32 /* suggestion chips container height */;
  83. // The app list grid vertical inset - the height of the view fadeout area.
  84. constexpr int kGridVerticalInset = 16;
  85. // Vertical margin for apps grid view (in addition to the grid insets).
  86. constexpr int kGridVerticalMargin = 8;
  87. // The horizontal spacing between apps grid view and the page switcher.
  88. constexpr int kPageSwitcherSpacing = 8;
  89. // The maximum allowed margin between items in apps item grid.
  90. constexpr int kMaxItemMargin = 96;
  91. // The min margins for contents within the fullscreen productivity launcher.
  92. constexpr int kMinProductivityLauncherMargin = 24;
  93. // The min horizontal margin for apps grid in fullscreen productivity launcher.
  94. // In addition to min productivity launcher margin, reserves 32 dip for page
  95. // switcher UI.
  96. constexpr int kMinProductivityLauncherGridHorizontalMargin =
  97. kMinProductivityLauncherMargin + 32;
  98. // The amount of the screen height that should be taken up by the vertical
  99. // margin for the apps container view.
  100. constexpr float kProductivityLauncherVerticalMarginRatio = 1.0f / 24.0f;
  101. constexpr float kProductivityLauncherVerticalMarginRatioLargeHeight =
  102. 1.0f / 16.0f;
  103. // `is_large_height` should be set depending on whether the screen height is
  104. // expected to be greater than 800. This is set so that the correct vertical
  105. // margin ratio is used depending on the expected screen height.
  106. int GetExpectedScreenSizeForProductivityLauncher(int row_count,
  107. int tile_height,
  108. int tile_margins,
  109. bool is_large_height) {
  110. // The vertical margins are calculated as a ratio of the total screen height.
  111. // Below we solve for the screen_height where vertical AppsContainerView
  112. // margins are defined as margin_ratio * screen_height.
  113. // Given the following values:
  114. // h = screen_height
  115. // r = margin_ratio
  116. // The screen_height can be calculated with the following:
  117. // h = 2 * r * h + grid_size + space_above_grid + shelf_size
  118. // Solving for h results in the following, which is used below:
  119. // h = (grid_size + space_above_grid + shelf_size) / (1 - 2 * r)
  120. float margin_ratio = kProductivityLauncherVerticalMarginRatio;
  121. if (is_large_height)
  122. margin_ratio = kProductivityLauncherVerticalMarginRatioLargeHeight;
  123. const float shelf_size = 56;
  124. const float grid_size =
  125. row_count * tile_height + (row_count - 1) * tile_margins;
  126. const float search_box_height = 48;
  127. const float space_above_grid =
  128. kGridVerticalMargin + kGridVerticalInset + search_box_height;
  129. DCHECK_NE(margin_ratio, 0.5f);
  130. const float screen_height = (grid_size + space_above_grid + shelf_size) /
  131. (1.0f - 2.0f * margin_ratio);
  132. // If the margins would be less than the minimum allowed margin, then add back
  133. // the necessary amount to account for the minimum margin.
  134. if (screen_height * margin_ratio < kMinProductivityLauncherMargin) {
  135. return screen_height + 2 * (kMinProductivityLauncherMargin -
  136. (screen_height * margin_ratio));
  137. }
  138. return screen_height;
  139. }
  140. SearchModel* GetSearchModel() {
  141. return AppListModelProvider::Get()->search_model();
  142. }
  143. void AddRecentApps(int num_apps) {
  144. for (int i = 0; i < num_apps; i++) {
  145. auto result = std::make_unique<TestSearchResult>();
  146. // Use the same "Item #" convention as AppListTestModel uses. The search
  147. // result IDs must match app item IDs in the app list data model.
  148. result->set_result_id(test::AppListTestModel::GetItemName(i));
  149. result->set_result_type(AppListSearchResultType::kInstalledApp);
  150. result->set_display_type(SearchResultDisplayType::kRecentApps);
  151. GetSearchModel()->results()->Add(std::move(result));
  152. }
  153. }
  154. int GridItemSizeWithMargins(int grid_size, int item_size, int item_count) {
  155. int margin = (grid_size - item_size * item_count) / (2 * (item_count - 1));
  156. return item_size + 2 * margin;
  157. }
  158. // Calculates the apps item grid size with maximum allowed margin between items.
  159. int GetItemGridSizeWithMaxItemMargins(int item_size, int item_count) {
  160. return item_size * item_count + (item_count - 1) * kMaxItemMargin;
  161. }
  162. template <class T>
  163. size_t GetVisibleViews(const std::vector<T*>& tiles) {
  164. size_t count = 0;
  165. for (const auto& tile : tiles) {
  166. if (tile->GetVisible())
  167. count++;
  168. }
  169. return count;
  170. }
  171. // A standard set of checks on a view, e.g., ensuring it is drawn and visible.
  172. void CheckView(views::View* subview) {
  173. ASSERT_TRUE(subview);
  174. EXPECT_TRUE(subview->parent());
  175. EXPECT_TRUE(subview->GetVisible());
  176. EXPECT_TRUE(subview->IsDrawn());
  177. EXPECT_FALSE(subview->bounds().IsEmpty());
  178. }
  179. bool IsViewVisibleOnScreen(views::View* view) {
  180. if (!view->IsDrawn())
  181. return false;
  182. if (view->layer() && !view->layer()->IsDrawn())
  183. return false;
  184. if (view->layer() && view->layer()->opacity() == 0.0f)
  185. return false;
  186. return display::Screen::GetScreen()
  187. ->GetPrimaryDisplay()
  188. .work_area()
  189. .Intersects(view->GetBoundsInScreen());
  190. }
  191. class TestStartPageSearchResult : public TestSearchResult {
  192. public:
  193. TestStartPageSearchResult() {
  194. set_display_type(ash::SearchResultDisplayType::kChip);
  195. set_is_recommendation(true);
  196. }
  197. TestStartPageSearchResult(const TestStartPageSearchResult&) = delete;
  198. TestStartPageSearchResult& operator=(const TestStartPageSearchResult&) =
  199. delete;
  200. ~TestStartPageSearchResult() override = default;
  201. };
  202. class AppListViewTest : public views::ViewsTestBase {
  203. public:
  204. AppListViewTest() = default;
  205. AppListViewTest(const AppListViewTest&) = delete;
  206. AppListViewTest& operator=(const AppListViewTest&) = delete;
  207. ~AppListViewTest() override = default;
  208. void SetUp() override {
  209. views::ViewsTestBase::SetUp();
  210. zero_duration_mode_ =
  211. std::make_unique<ui::ScopedAnimationDurationScaleMode>(
  212. ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
  213. ui::PresentationTimeRecorder::SetReportPresentationTimeImmediatelyForTest(
  214. true);
  215. }
  216. void TearDown() override {
  217. ui::PresentationTimeRecorder::SetReportPresentationTimeImmediatelyForTest(
  218. false);
  219. view_->GetWidget()->Close();
  220. zero_duration_mode_.reset();
  221. views::ViewsTestBase::TearDown();
  222. }
  223. protected:
  224. void Show(bool is_side_shelf = false) {
  225. view_->Show(AppListViewState::kPeeking, is_side_shelf);
  226. }
  227. void Initialize(bool is_tablet_mode) {
  228. delegate_ = std::make_unique<AppListTestViewDelegate>();
  229. delegate_->SetIsTabletModeEnabled(is_tablet_mode);
  230. view_ = new AppListView(delegate_.get());
  231. view_->InitView(GetContext());
  232. test_api_ = std::make_unique<AppsGridViewTestApi>(apps_grid_view());
  233. EXPECT_FALSE(view_->GetWidget()->IsVisible());
  234. }
  235. // Switches the launcher to |state| and lays out to ensure all launcher pages
  236. // are in the correct position. Checks that the state is where it should be
  237. // and returns false on failure.
  238. bool SetAppListState(ash::AppListState state) {
  239. ContentsView* contents_view = view_->app_list_main_view()->contents_view();
  240. // The default method of changing the state to |kStateSearchResults| is via
  241. // |ShowSearchResults|
  242. if (state == ash::AppListState::kStateSearchResults)
  243. contents_view->ShowSearchResults(true);
  244. else
  245. contents_view->SetActiveState(state);
  246. contents_view->Layout();
  247. return IsStateShown(state);
  248. }
  249. // Returns true if all of the pages are in their correct position for |state|.
  250. bool IsStateShown(ash::AppListState state) {
  251. ContentsView* contents_view = view_->app_list_main_view()->contents_view();
  252. bool success = true;
  253. for (int i = 0; i < contents_view->NumLauncherPages(); ++i) {
  254. const gfx::Rect expected_bounds =
  255. contents_view->GetPageView(i)->GetPageBoundsForState(
  256. state, contents_view->GetContentsBounds(),
  257. contents_view->GetSearchBoxBounds(state));
  258. const views::View* page_view = contents_view->GetPageView(i);
  259. if (page_view->bounds() != expected_bounds) {
  260. ADD_FAILURE() << "Page " << i << " bounds do not match "
  261. << "expected: " << expected_bounds.ToString()
  262. << " actual: " << page_view->bounds().ToString();
  263. success = false;
  264. }
  265. if (contents_view->GetPageIndexForState(state) == i &&
  266. !page_view->GetVisible()) {
  267. ADD_FAILURE() << "Target page not visible " << i;
  268. success = false;
  269. }
  270. }
  271. if (state != delegate_->GetCurrentAppListPage()) {
  272. ADD_FAILURE() << "Model state does not match state "
  273. << static_cast<int>(state);
  274. success = false;
  275. }
  276. return success;
  277. }
  278. // Checks the search box widget is at |expected| in the contents view's
  279. // coordinate space.
  280. bool CheckSearchBoxWidget(const gfx::Rect& expected) {
  281. ContentsView* contents_view = view_->app_list_main_view()->contents_view();
  282. // Adjust for the search box view's shadow.
  283. gfx::Rect expected_with_shadow =
  284. view_->app_list_main_view()
  285. ->search_box_view()
  286. ->GetViewBoundsForSearchBoxContentsBounds(expected);
  287. gfx::Point point = expected_with_shadow.origin();
  288. views::View::ConvertPointToScreen(contents_view, &point);
  289. return gfx::Rect(point, expected_with_shadow.size()) ==
  290. view_->search_box_view()->GetWidget()->GetWindowBoundsInScreen();
  291. }
  292. void SetTextInSearchBox(const std::u16string& text) {
  293. views::Textfield* search_box =
  294. view_->app_list_main_view()->search_box_view()->search_box();
  295. // Set new text as if it is typed by a user.
  296. search_box->SetText(std::u16string());
  297. search_box->InsertText(
  298. text,
  299. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  300. }
  301. int ShelfSize() const { return delegate_->GetShelfSize(); }
  302. // Gets the PaginationModel owned by |view_|.
  303. ash::PaginationModel* GetPaginationModel() const {
  304. return view_->GetAppsPaginationModel();
  305. }
  306. SearchBoxView* search_box_view() {
  307. return view_->app_list_main_view()->search_box_view();
  308. }
  309. ContentsView* contents_view() {
  310. return view_->app_list_main_view()->contents_view();
  311. }
  312. views::View* scrollable_container() {
  313. return contents_view()
  314. ->apps_container_view()
  315. ->scrollable_container_for_test();
  316. }
  317. PagedAppsGridView* apps_grid_view() {
  318. return contents_view()->apps_container_view()->apps_grid_view();
  319. }
  320. PageSwitcher* page_switcher_view() {
  321. return contents_view()->apps_container_view()->page_switcher();
  322. }
  323. RecentAppsView* recent_apps() {
  324. return contents_view()->apps_container_view()->GetRecentAppsView();
  325. }
  326. views::View* assistant_page_view() {
  327. const int assistant_page_index = contents_view()->GetPageIndexForState(
  328. ash::AppListState::kStateEmbeddedAssistant);
  329. return contents_view()->GetPageView(assistant_page_index);
  330. }
  331. gfx::Point GetPointBetweenTwoApps() {
  332. const views::ViewModelT<AppListItemView>* view_model =
  333. apps_grid_view()->view_model();
  334. const gfx::Rect bounds_1 = view_model->view_at(0)->GetBoundsInScreen();
  335. const gfx::Rect bounds_2 = view_model->view_at(1)->GetBoundsInScreen();
  336. return gfx::Point(bounds_1.right() + (bounds_2.x() - bounds_1.right()) / 2,
  337. bounds_1.y());
  338. }
  339. int show_wallpaper_context_menu_count() {
  340. return delegate_->show_wallpaper_context_menu_count();
  341. }
  342. void VerifyAppsContainerLayoutForProductivityLauncher(
  343. const gfx::Size& container_size,
  344. int row_count,
  345. int expected_horizontal_margin,
  346. const gfx::Size& expected_item_size,
  347. bool has_recent_apps) {
  348. const int column_count = 5;
  349. ASSERT_EQ(column_count, apps_grid_view()->cols());
  350. ASSERT_EQ(row_count, apps_grid_view()->GetFirstPageRowsForTesting());
  351. const float ratio =
  352. (container_size.height() > 800)
  353. ? kProductivityLauncherVerticalMarginRatioLargeHeight
  354. : kProductivityLauncherVerticalMarginRatio;
  355. const int expected_vertical_margin =
  356. std::max(static_cast<int>(container_size.height() * ratio),
  357. kMinProductivityLauncherMargin);
  358. const int expected_grid_width =
  359. container_size.width() - 2 * expected_horizontal_margin;
  360. // Verify scrollable container bounds.
  361. const int expected_scrollable_container_top = expected_vertical_margin +
  362. 48 /*search box height*/ +
  363. kGridVerticalMargin;
  364. const int expected_scrollable_container_height =
  365. container_size.height() - expected_scrollable_container_top -
  366. expected_vertical_margin - ShelfSize();
  367. EXPECT_EQ(
  368. gfx::Rect(expected_horizontal_margin, expected_scrollable_container_top,
  369. expected_grid_width, expected_scrollable_container_height),
  370. scrollable_container()->bounds());
  371. // Verify apps grid bounds.
  372. gfx::Point grid_origin_in_scrollable_container;
  373. views::View::ConvertPointToTarget(apps_grid_view(), scrollable_container(),
  374. &grid_origin_in_scrollable_container);
  375. EXPECT_EQ(gfx::Point(0, kGridVerticalInset),
  376. grid_origin_in_scrollable_container);
  377. const int expected_grid_height =
  378. expected_scrollable_container_height - kGridVerticalInset;
  379. EXPECT_EQ(gfx::Size(expected_grid_width, expected_grid_height),
  380. apps_grid_view()->size());
  381. // Verify page switcher bounds.
  382. EXPECT_EQ(gfx::Rect(expected_grid_width + expected_horizontal_margin +
  383. kPageSwitcherSpacing,
  384. expected_scrollable_container_top,
  385. 2 * PageSwitcher::kMaxButtonRadiusForRootGrid,
  386. expected_grid_height),
  387. page_switcher_view()->bounds());
  388. // Verify recent apps view visibility and bounds (when visible).
  389. EXPECT_EQ(has_recent_apps, recent_apps()->GetVisible());
  390. if (has_recent_apps) {
  391. gfx::Point origin_in_scrollable_container;
  392. views::View::ConvertPointToTarget(recent_apps(), scrollable_container(),
  393. &origin_in_scrollable_container);
  394. EXPECT_EQ(gfx::Point(0, kGridVerticalInset),
  395. origin_in_scrollable_container);
  396. EXPECT_EQ(expected_grid_width, recent_apps()->width());
  397. EXPECT_EQ(expected_item_size.height(), recent_apps()->height());
  398. }
  399. // Horizontal offset between app list item views, which includes tile width
  400. // and horizontal margin.
  401. const int horizontal_item_offset = GridItemSizeWithMargins(
  402. expected_grid_width, expected_item_size.width(), column_count);
  403. EXPECT_LE(horizontal_item_offset - expected_item_size.width(), 128);
  404. // Calculate space reserved for separator, which is only shown if suggested
  405. // content (e.g. recent apps) exists.
  406. int separator_size = 0;
  407. if (has_recent_apps) {
  408. const int separator_margin = expected_item_size.height() > 88 ? 16 : 8;
  409. separator_size = 2 * separator_margin + 1 /*actual separator height*/;
  410. }
  411. // Vertical offset between app list item views, which includes tile height
  412. // and vertical margin.
  413. const int vertical_item_offset = GridItemSizeWithMargins(
  414. expected_grid_height - separator_size, expected_item_size.height(),
  415. row_count + (has_recent_apps ? 1 : 0));
  416. EXPECT_GE(vertical_item_offset - expected_item_size.height(), 8);
  417. EXPECT_LE(vertical_item_offset - expected_item_size.height(), 96);
  418. // If recent apps are shown, the items on the first page are offset by the
  419. // recent apps container height, a separator and vertical margin between
  420. // tiles.
  421. const int base_vertical_offset =
  422. has_recent_apps ? vertical_item_offset + separator_size : 0;
  423. // Verify expected bounds for the first row:
  424. for (int i = 0; i < column_count; ++i) {
  425. EXPECT_EQ(gfx::Rect(gfx::Point(i * horizontal_item_offset,
  426. base_vertical_offset),
  427. expected_item_size),
  428. test_api_->GetItemTileRectAtVisualIndex(0, i))
  429. << "Item " << i << " bounds";
  430. }
  431. // Verify expected bounds for the first column:
  432. for (int j = 1; j < row_count; ++j) {
  433. EXPECT_EQ(gfx::Rect(gfx::Point(0, base_vertical_offset +
  434. j * vertical_item_offset),
  435. expected_item_size),
  436. test_api_->GetItemTileRectAtVisualIndex(0, j * column_count))
  437. << "Item " << j * column_count << " bounds";
  438. }
  439. // The last item in the page (bottom right):
  440. EXPECT_EQ(gfx::Rect(gfx::Point((column_count - 1) * horizontal_item_offset,
  441. base_vertical_offset +
  442. (row_count - 1) * vertical_item_offset),
  443. expected_item_size),
  444. test_api_->GetItemTileRectAtVisualIndex(
  445. 0, row_count * column_count - 1));
  446. // Verify that search box top is at the expected apps container vertical
  447. // margin, both in apps, and search results state.
  448. std::vector<ash::AppListState> available_app_list_states = {
  449. ash::AppListState::kStateApps, ash::AppListState::kStateSearchResults};
  450. for (auto app_list_state : available_app_list_states) {
  451. const gfx::Rect search_box_bounds =
  452. contents_view()->GetSearchBoxBounds(app_list_state);
  453. EXPECT_EQ(expected_vertical_margin, search_box_bounds.y())
  454. << "App list state: " << static_cast<int>(app_list_state);
  455. }
  456. }
  457. // Verifies fullscreen apps container bounds and layout.
  458. void VerifyAppsContainerLayout(const gfx::Size& container_size,
  459. int column_count,
  460. int row_count,
  461. int expected_horizontal_margin,
  462. int expected_vertical_margin,
  463. int expected_item_size) {
  464. const int kExpectedGridWidth =
  465. container_size.width() - 2 * expected_horizontal_margin;
  466. const int search_box_and_suggestion_chip_height =
  467. container_size.height() < 600 + ShelfSize()
  468. ? kSearchBoxAndSuggestionChipsHeightDense
  469. : kSearchBoxAndSuggestionChipsHeightDefault;
  470. const int kExpectedGridTop = expected_vertical_margin +
  471. search_box_and_suggestion_chip_height +
  472. kGridVerticalMargin;
  473. const int kExpectedGridHeight =
  474. container_size.height() - kExpectedGridTop -
  475. (expected_vertical_margin - kGridVerticalInset) - ShelfSize();
  476. EXPECT_EQ(gfx::Rect(0, 0, kExpectedGridWidth, kExpectedGridHeight),
  477. apps_grid_view()->bounds());
  478. EXPECT_EQ(gfx::Rect(expected_horizontal_margin, kExpectedGridTop,
  479. kExpectedGridWidth, kExpectedGridHeight),
  480. scrollable_container()->bounds());
  481. EXPECT_EQ(gfx::Rect(kExpectedGridWidth + expected_horizontal_margin +
  482. kPageSwitcherSpacing,
  483. kExpectedGridTop,
  484. 2 * PageSwitcher::kMaxButtonRadiusForRootGrid,
  485. kExpectedGridHeight),
  486. page_switcher_view()->bounds());
  487. // Horizontal offset between app list item views.
  488. const int kHorizontalOffset = GridItemSizeWithMargins(
  489. kExpectedGridWidth, expected_item_size, column_count);
  490. // Verify expected bounds for the first row:
  491. for (int i = 0; i < column_count; ++i) {
  492. EXPECT_EQ(gfx::Rect(i * kHorizontalOffset, kGridVerticalInset,
  493. expected_item_size, expected_item_size),
  494. test_api_->GetItemTileRectAtVisualIndex(0, i))
  495. << "Item " << i << " bounds";
  496. }
  497. // Vertical offset between app list item views.
  498. const int kVerticalOffset =
  499. GridItemSizeWithMargins(kExpectedGridHeight - 2 * kGridVerticalInset,
  500. expected_item_size, row_count);
  501. // Verify expected bounds for the first column:
  502. for (int j = 1; j < row_count; ++j) {
  503. EXPECT_EQ(gfx::Rect(0, kGridVerticalInset + j * kVerticalOffset,
  504. expected_item_size, expected_item_size),
  505. test_api_->GetItemTileRectAtVisualIndex(0, j * column_count))
  506. << "Item " << j * column_count << " bounds";
  507. }
  508. // The last item in the page (bottom right):
  509. EXPECT_EQ(gfx::Rect((column_count - 1) * kHorizontalOffset,
  510. kGridVerticalInset + (row_count - 1) * kVerticalOffset,
  511. expected_item_size, expected_item_size),
  512. test_api_->GetItemTileRectAtVisualIndex(0, 19));
  513. // Verify that search box top is at the expected apps container vertical
  514. // margin, both in apps, and search results state.
  515. std::vector<ash::AppListState> available_app_list_states = {
  516. ash::AppListState::kStateApps, ash::AppListState::kStateSearchResults};
  517. for (auto app_list_state : available_app_list_states) {
  518. const gfx::Rect search_box_bounds =
  519. contents_view()->GetSearchBoxBounds(app_list_state);
  520. EXPECT_EQ(expected_vertical_margin, search_box_bounds.y())
  521. << "App list state: " << static_cast<int>(app_list_state);
  522. }
  523. }
  524. // Sets animation durations to zero.
  525. std::unique_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
  526. TestAppListColorProvider app_list_color_provider_; // Needed by AppListView.
  527. // Needed by ProductivityLauncher AppsContainerView::ContinueContainer.
  528. AshColorProvider ash_color_provider_;
  529. AppListView* view_ = nullptr; // Owned by native widget.
  530. std::unique_ptr<AppListTestViewDelegate> delegate_;
  531. std::unique_ptr<AppsGridViewTestApi> test_api_;
  532. // Used by AppListFolderView::UpdatePreferredBounds.
  533. keyboard::KeyboardUIController keyboard_ui_controller_;
  534. };
  535. // Tests for the legacy "peeking" clamshell launcher. These can be deleted when
  536. // ProductivityLauncher is the default.
  537. class AppListViewPeekingTest : public AppListViewTest {
  538. public:
  539. AppListViewPeekingTest() {
  540. feature_list_.InitAndDisableFeature(features::kProductivityLauncher);
  541. }
  542. base::test::ScopedFeatureList feature_list_;
  543. };
  544. // Tests for tablet mode. Parameterized by ProductivityLauncher.
  545. class AppListViewTabletTest : public AppListViewTest,
  546. public testing::WithParamInterface<bool> {
  547. public:
  548. AppListViewTabletTest() {
  549. const bool enable_productivity_launcher = GetParam();
  550. feature_list_.InitWithFeatureState(features::kProductivityLauncher,
  551. enable_productivity_launcher);
  552. }
  553. base::test::ScopedFeatureList feature_list_;
  554. };
  555. INSTANTIATE_TEST_SUITE_P(ProductivityLauncher,
  556. AppListViewTabletTest,
  557. testing::Bool());
  558. // Tests app list view layout for different screen sizes.
  559. class AppListViewScalableLayoutTest : public AppListViewTest {
  560. public:
  561. explicit AppListViewScalableLayoutTest(bool enable_productivity_launcher) {
  562. if (enable_productivity_launcher) {
  563. scoped_feature_list_.InitWithFeatures(
  564. {ash::features::kEnableBackgroundBlur,
  565. ash::features::kProductivityLauncher},
  566. {});
  567. } else {
  568. scoped_feature_list_.InitWithFeatures(
  569. {ash::features::kEnableBackgroundBlur},
  570. {ash::features::kProductivityLauncher});
  571. }
  572. }
  573. ~AppListViewScalableLayoutTest() override = default;
  574. void SetUp() override {
  575. // Clear configs created in previous tests.
  576. AppListConfigProvider::Get().ResetForTesting();
  577. AppListViewTest::SetUp();
  578. }
  579. void TearDown() override {
  580. AppListViewTest::TearDown();
  581. AppListConfigProvider::Get().ResetForTesting();
  582. }
  583. protected:
  584. base::test::ScopedFeatureList scoped_feature_list_;
  585. };
  586. class ProductivityLauncherAppListViewLayoutTest
  587. : public AppListViewScalableLayoutTest {
  588. public:
  589. ProductivityLauncherAppListViewLayoutTest()
  590. : AppListViewScalableLayoutTest(/*enable_productivity_launcher=*/true) {}
  591. void InitializeAppList() {
  592. Initialize(true /*is_tablet_mode*/);
  593. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  594. Show();
  595. }
  596. };
  597. class LegacyLauncherAppListViewLayoutTest
  598. : public AppListViewScalableLayoutTest {
  599. public:
  600. LegacyLauncherAppListViewLayoutTest()
  601. : AppListViewScalableLayoutTest(/*enable_productivity_launcher=*/false) {}
  602. };
  603. // Tests of focus, optionally parameterized by RTL.
  604. class AppListViewFocusTest : public views::ViewsTestBase,
  605. public testing::WithParamInterface<bool> {
  606. public:
  607. AppListViewFocusTest() = default;
  608. AppListViewFocusTest(const AppListViewFocusTest&) = delete;
  609. AppListViewFocusTest& operator=(const AppListViewFocusTest&) = delete;
  610. ~AppListViewFocusTest() override = default;
  611. // testing::Test
  612. void SetUp() override {
  613. if (testing::UnitTest::GetInstance()->current_test_info()->value_param()) {
  614. // Setup right to left environment if necessary.
  615. is_rtl_ = GetParam();
  616. if (is_rtl_)
  617. base::i18n::SetICUDefaultLocale("he");
  618. }
  619. views::ViewsTestBase::SetUp();
  620. // Initialize app list view.
  621. delegate_ = std::make_unique<AppListTestViewDelegate>();
  622. view_ = new AppListView(delegate_.get());
  623. view_->InitView(GetContext());
  624. Show();
  625. test_api_ = std::make_unique<AppsGridViewTestApi>(apps_grid_view());
  626. // May be null for ProductivityLauncher, which does not use chips.
  627. suggestions_container_ = contents_view()
  628. ->apps_container_view()
  629. ->suggestion_chip_container_view_for_test();
  630. expand_arrow_view_ = contents_view()->expand_arrow_view();
  631. // Add suggestion apps, a folder with apps and other app list items.
  632. const int kSuggestionAppNum = 3;
  633. const int kItemNumInFolder = 25;
  634. const int kAppListItemNum =
  635. SharedAppListConfig::instance().GetMaxNumOfItemsPerPage() + 1;
  636. AppListTestModel* model = delegate_->GetTestModel();
  637. SearchModel* search_model = GetSearchModel();
  638. for (size_t i = 0; i < kSuggestionAppNum; i++) {
  639. search_model->results()->Add(
  640. std::make_unique<TestStartPageSearchResult>());
  641. }
  642. AppListFolderItem* folder_item =
  643. model->CreateAndPopulateFolderWithApps(kItemNumInFolder);
  644. model->PopulateApps(kAppListItemNum);
  645. if (suggestions_container_)
  646. suggestions_container_->Update();
  647. EXPECT_EQ(static_cast<size_t>(kAppListItemNum + 1),
  648. model->top_level_item_list()->item_count());
  649. EXPECT_EQ(folder_item->id(),
  650. model->top_level_item_list()->item_at(0)->id());
  651. // Disable animation timer.
  652. view_->GetWidget()->GetLayer()->GetAnimator()->set_disable_timer_for_test(
  653. true);
  654. }
  655. void TearDown() override {
  656. view_->GetWidget()->Close();
  657. views::ViewsTestBase::TearDown();
  658. }
  659. void SetAppListState(ash::AppListViewState state) {
  660. if (state == ash::AppListViewState::kClosed) {
  661. view_->Dismiss();
  662. return;
  663. }
  664. view_->SetState(state);
  665. }
  666. void Show() {
  667. view_->Show(AppListViewState::kPeeking, /*is_side_shelf=*/false);
  668. }
  669. SearchResultTileItemListView* GetSearchResultTileItemListView() {
  670. return contents_view()
  671. ->search_result_page_view()
  672. ->GetSearchResultTileItemListViewForTest();
  673. }
  674. SearchResultListView* GetSearchResultListView() {
  675. return contents_view()
  676. ->search_result_page_view()
  677. ->GetSearchResultListViewForTest();
  678. }
  679. AppsGridViewTestApi* test_api() { return test_api_.get(); }
  680. void SimulateKeyPress(ui::KeyboardCode key_code,
  681. bool shift_down,
  682. bool ctrl_down = false) {
  683. ui::KeyEvent key_event(ui::ET_KEY_PRESSED, key_code,
  684. shift_down
  685. ? ui::EF_SHIFT_DOWN
  686. : ctrl_down ? ui::EF_CONTROL_DOWN : ui::EF_NONE);
  687. view_->GetWidget()->OnKeyEvent(&key_event);
  688. }
  689. // Add search results for test on focus movement.
  690. void SetUpSearchResults(int tile_results_num, int list_results_num) {
  691. std::vector<std::pair<SearchResult::DisplayType, int>> result_types;
  692. result_types.emplace_back(SearchResultDisplayType::kTile, tile_results_num);
  693. result_types.emplace_back(SearchResultDisplayType::kList, list_results_num);
  694. SearchModel::SearchResults* results = GetSearchModel()->results();
  695. results->DeleteAll();
  696. double display_score = result_types.size();
  697. for (const auto& data : result_types) {
  698. // Set the display score of the results in each group in decreasing order
  699. // (so the earlier groups have higher display score, and therefore appear
  700. // first).
  701. display_score -= 0.5;
  702. for (int i = 0; i < data.second; ++i) {
  703. std::unique_ptr<TestSearchResult> result =
  704. std::make_unique<TestSearchResult>();
  705. result->set_display_type(data.first);
  706. result->set_display_score(display_score);
  707. result->SetTitle(u"Test");
  708. result->set_best_match(true);
  709. results->Add(std::move(result));
  710. }
  711. }
  712. // Adding results will schedule Update().
  713. base::RunLoop().RunUntilIdle();
  714. }
  715. // Add search results for test on embedded Assistant UI.
  716. void SetUpSearchResultsForAssistantUI(int list_results_num,
  717. int index_open_assistant_ui) {
  718. SearchModel::SearchResults* results = GetSearchModel()->results();
  719. results->DeleteAll();
  720. double display_score = list_results_num;
  721. for (int i = 0; i < list_results_num; ++i) {
  722. // Set the display score of the results in decreasing order
  723. // (so the earlier groups have higher display score, and therefore appear
  724. // first).
  725. display_score -= 1;
  726. std::unique_ptr<TestSearchResult> result =
  727. std::make_unique<TestSearchResult>();
  728. result->set_display_type(ash::SearchResultDisplayType::kList);
  729. result->set_display_score(display_score);
  730. result->SetTitle(u"Test" + base::NumberToString16(i));
  731. result->set_result_id("Test" + base::NumberToString(i));
  732. if (i == index_open_assistant_ui)
  733. result->set_is_omnibox_search(true);
  734. results->Add(std::move(result));
  735. }
  736. // Adding results will schedule Update().
  737. base::RunLoop().RunUntilIdle();
  738. }
  739. void ClearSearchResults() { GetSearchModel()->results()->DeleteAll(); }
  740. void AddSearchResultWithTitleAndScore(const base::StringPiece& title,
  741. double score) {
  742. std::unique_ptr<TestSearchResult> result =
  743. std::make_unique<TestSearchResult>();
  744. result->set_display_type(ash::SearchResultDisplayType::kList);
  745. result->set_display_score(score);
  746. result->SetTitle(ASCIIToUTF16(title));
  747. result->set_best_match(true);
  748. GetSearchModel()->results()->Add(std::move(result));
  749. base::RunLoop().RunUntilIdle();
  750. }
  751. int GetOpenFirstSearchResultCount() {
  752. std::map<size_t, int>& counts = delegate_->open_search_result_counts();
  753. if (counts.size() == 0)
  754. return 0;
  755. return counts[0];
  756. }
  757. int GetTotalOpenSearchResultCount() {
  758. return delegate_->open_search_result_count();
  759. }
  760. int GetTotalOpenAssistantUICount() {
  761. return delegate_->open_assistant_ui_count();
  762. }
  763. // Test focus traversal across all the views in |view_list|. The initial focus
  764. // is expected to be on the first view in |view_list|. The final focus is
  765. // expected to be on the last view in |view_list| after |view_list.size()-1|
  766. // key events are pressed.
  767. void TestFocusTraversal(const std::vector<views::View*>& view_list,
  768. ui::KeyboardCode key_code,
  769. bool shift_down) {
  770. EXPECT_EQ(view_list[0], focused_view());
  771. for (size_t i = 1; i < view_list.size(); ++i) {
  772. SimulateKeyPress(key_code, shift_down);
  773. EXPECT_EQ(view_list[i], focused_view());
  774. }
  775. }
  776. void TestSelectionTraversal(const std::vector<views::View*>& view_list,
  777. ui::KeyboardCode key_code,
  778. bool shift_down) {
  779. ResultSelectionController* selection_controller =
  780. contents_view()
  781. ->search_result_page_view()
  782. ->result_selection_controller();
  783. EXPECT_EQ(view_list[0], selection_controller->selected_result());
  784. for (size_t i = 1; i < view_list.size(); ++i) {
  785. SimulateKeyPress(key_code, shift_down);
  786. EXPECT_EQ(view_list[i], selection_controller->selected_result());
  787. }
  788. }
  789. // Test the behavior triggered by left and right key when focus is on the
  790. // |textfield|. Does not insert text.
  791. void TestLeftAndRightKeyTraversalOnTextfield(views::Textfield* textfield) {
  792. EXPECT_TRUE(textfield->GetText().empty());
  793. EXPECT_EQ(textfield, focused_view());
  794. views::View* next_view =
  795. view_->GetWidget()->GetFocusManager()->GetNextFocusableView(
  796. textfield, view_->GetWidget(), false, false);
  797. views::View* prev_view =
  798. view_->GetWidget()->GetFocusManager()->GetNextFocusableView(
  799. textfield, view_->GetWidget(), true, false);
  800. // Only need to hit left or right key once to move focus outside the
  801. // textfield when it is empty.
  802. SimulateKeyPress(ui::VKEY_RIGHT, false);
  803. EXPECT_EQ(is_rtl_ ? prev_view : next_view, focused_view());
  804. SimulateKeyPress(ui::VKEY_LEFT, false);
  805. EXPECT_EQ(textfield, focused_view());
  806. SimulateKeyPress(ui::VKEY_LEFT, false);
  807. EXPECT_EQ(is_rtl_ ? next_view : prev_view, focused_view());
  808. SimulateKeyPress(ui::VKEY_RIGHT, false);
  809. EXPECT_EQ(textfield, focused_view());
  810. }
  811. // Test the behavior triggered by left and right key when focus is on the
  812. // |textfield|. This includes typing text into the field.
  813. void TestLeftAndRightKeyOnTextfieldWithText(views::Textfield* textfield,
  814. bool text_rtl) {
  815. // Test initial traversal
  816. TestLeftAndRightKeyTraversalOnTextfield(textfield);
  817. // Type something in textfield.
  818. std::u16string text = text_rtl ? u"اختبار" : u"test";
  819. textfield->InsertText(
  820. text,
  821. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  822. views::View* next_view = next_view =
  823. view_->GetWidget()->GetFocusManager()->GetNextFocusableView(
  824. textfield, view_->GetWidget(), false, false);
  825. views::View* prev_view = prev_view =
  826. view_->GetWidget()->GetFocusManager()->GetNextFocusableView(
  827. textfield, view_->GetWidget(), true, false);
  828. EXPECT_EQ(text.length(), textfield->GetCursorPosition());
  829. EXPECT_FALSE(textfield->HasSelection());
  830. EXPECT_EQ(textfield, focused_view());
  831. const ui::KeyboardCode backward_key =
  832. text_rtl ? ui::VKEY_RIGHT : ui::VKEY_LEFT;
  833. const ui::KeyboardCode forward_key =
  834. text_rtl ? ui::VKEY_LEFT : ui::VKEY_RIGHT;
  835. // Move cursor backward.
  836. SimulateKeyPress(backward_key, false);
  837. EXPECT_EQ(text.length() - 1, textfield->GetCursorPosition());
  838. EXPECT_EQ(textfield, focused_view());
  839. // Move cursor forward.
  840. SimulateKeyPress(forward_key, false);
  841. EXPECT_EQ(text.length(), textfield->GetCursorPosition());
  842. EXPECT_EQ(textfield, focused_view());
  843. // Hit forward key to move focus outside the textfield.
  844. SimulateKeyPress(forward_key, false);
  845. EXPECT_EQ((!is_rtl_ && !text_rtl) || (is_rtl_ && text_rtl) ? next_view
  846. : prev_view,
  847. focused_view());
  848. // Hit backward key to move focus back to textfield and select all text.
  849. SimulateKeyPress(backward_key, false);
  850. EXPECT_EQ(text, textfield->GetSelectedText());
  851. EXPECT_EQ(textfield, focused_view());
  852. // Hit backward key to move cursor to the beginning.
  853. SimulateKeyPress(backward_key, false);
  854. EXPECT_EQ(0U, textfield->GetCursorPosition());
  855. EXPECT_FALSE(textfield->HasSelection());
  856. EXPECT_EQ(textfield, focused_view());
  857. // Hit backward key to move focus outside the textfield.
  858. SimulateKeyPress(backward_key, false);
  859. EXPECT_EQ((!is_rtl_ && !text_rtl) || (is_rtl_ && text_rtl) ? prev_view
  860. : next_view,
  861. focused_view());
  862. // Hit forward key to move focus back to textfield and select all text.
  863. SimulateKeyPress(forward_key, false);
  864. EXPECT_EQ(text, textfield->GetSelectedText());
  865. EXPECT_EQ(textfield, focused_view());
  866. // Hit forward key to move cursor to the end.
  867. SimulateKeyPress(forward_key, false);
  868. EXPECT_EQ(text.length(), textfield->GetCursorPosition());
  869. EXPECT_FALSE(textfield->HasSelection());
  870. EXPECT_EQ(textfield, focused_view());
  871. // Hitt forward key to move focus outside the textfield.
  872. SimulateKeyPress(forward_key, false);
  873. EXPECT_EQ((!is_rtl_ && !text_rtl) || (is_rtl_ && text_rtl) ? next_view
  874. : prev_view,
  875. focused_view());
  876. // Clean up
  877. textfield->RequestFocus();
  878. textfield->SetText(u"");
  879. }
  880. AppListView* app_list_view() { return view_; }
  881. AppListMainView* main_view() { return view_->app_list_main_view(); }
  882. ContentsView* contents_view() {
  883. return view_->app_list_main_view()->contents_view();
  884. }
  885. PagedAppsGridView* apps_grid_view() {
  886. return main_view()
  887. ->contents_view()
  888. ->apps_container_view()
  889. ->apps_grid_view();
  890. }
  891. AppListFolderView* app_list_folder_view() {
  892. return main_view()
  893. ->contents_view()
  894. ->apps_container_view()
  895. ->app_list_folder_view();
  896. }
  897. SearchResultContainerView* suggestions_container() {
  898. return suggestions_container_;
  899. }
  900. std::vector<views::View*> GetAllSuggestions() {
  901. // ProductivityLauncher does not use suggestion chips.
  902. DCHECK(!features::IsProductivityLauncherEnabled());
  903. const auto& children = suggestions_container()->children();
  904. std::vector<views::View*> suggestions;
  905. std::copy_if(children.cbegin(), children.cend(),
  906. std::back_inserter(suggestions),
  907. [](const auto* v) { return v->GetVisible(); });
  908. return suggestions;
  909. }
  910. SearchBoxView* search_box_view() { return main_view()->search_box_view(); }
  911. AppListItemView* folder_item_view() {
  912. return apps_grid_view()->view_model()->view_at(0);
  913. }
  914. views::View* focused_view() {
  915. return view_->GetWidget()->GetFocusManager()->GetFocusedView();
  916. }
  917. ExpandArrowView* expand_arrow_view() { return expand_arrow_view_; }
  918. protected:
  919. bool is_rtl_ = false;
  920. base::test::ScopedFeatureList scoped_feature_list_;
  921. private:
  922. TestAppListColorProvider app_list_color_provider_; // Needed by AppListView.
  923. AshColorProvider ash_color_provider_; // Needed by ProductivityLauncher.
  924. AppListView* view_ = nullptr; // Owned by native widget.
  925. // Owned by view hierarchy. May be null for ProductivityLauncher, which does
  926. // not use suggestion chips.
  927. SearchResultContainerView* suggestions_container_ = nullptr;
  928. ExpandArrowView* expand_arrow_view_ = nullptr; // Owned by view hierarchy.
  929. std::unique_ptr<AppListTestViewDelegate> delegate_;
  930. std::unique_ptr<AppsGridViewTestApi> test_api_;
  931. // Restores the locale to default when destructor is called.
  932. base::test::ScopedRestoreICUDefaultLocale restore_locale_;
  933. // Used by AppListFolderView::UpdatePreferredBounds.
  934. keyboard::KeyboardUIController keyboard_ui_controller_;
  935. };
  936. INSTANTIATE_TEST_SUITE_P(Rtl, AppListViewFocusTest, testing::Bool());
  937. // Tests for the legacy "peeking" clamshell launcher. These can be deleted when
  938. // ProductivityLauncher is the default.
  939. class AppListViewPeekingFocusTest : public AppListViewFocusTest {
  940. public:
  941. AppListViewPeekingFocusTest() {
  942. feature_list_.InitAndDisableFeature(features::kProductivityLauncher);
  943. }
  944. base::test::ScopedFeatureList feature_list_;
  945. };
  946. INSTANTIATE_TEST_SUITE_P(Rtl, AppListViewPeekingFocusTest, testing::Bool());
  947. // Tests that the initial focus is on search box.
  948. TEST_F(AppListViewFocusTest, InitialFocus) {
  949. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  950. }
  951. // Tests the linear focus traversal in PEEKING state.
  952. TEST_P(AppListViewPeekingFocusTest, LinearFocusTraversalInPeekingState) {
  953. Show();
  954. SetAppListState(ash::AppListViewState::kPeeking);
  955. std::vector<views::View*> forward_view_list;
  956. forward_view_list.push_back(search_box_view()->search_box());
  957. for (auto* v : GetAllSuggestions())
  958. forward_view_list.push_back(v);
  959. forward_view_list.push_back(expand_arrow_view());
  960. forward_view_list.push_back(search_box_view()->search_box());
  961. std::vector<views::View*> backward_view_list = forward_view_list;
  962. std::reverse(backward_view_list.begin(), backward_view_list.end());
  963. // Test traversal triggered by tab.
  964. TestFocusTraversal(forward_view_list, ui::VKEY_TAB, false);
  965. // Test traversal triggered by shift+tab.
  966. TestFocusTraversal(backward_view_list, ui::VKEY_TAB, true);
  967. // Test traversal triggered by right.
  968. TestFocusTraversal(is_rtl_ ? backward_view_list : forward_view_list,
  969. ui::VKEY_RIGHT, false);
  970. // Test traversal triggered by left.
  971. TestFocusTraversal(is_rtl_ ? forward_view_list : backward_view_list,
  972. ui::VKEY_LEFT, false);
  973. }
  974. // Tests the linear focus traversal in FULLSCREEN_ALL_APPS state.
  975. TEST_P(AppListViewFocusTest, LinearFocusTraversalInFullscreenAllAppsState) {
  976. // TODO(https://crbug.com/1284992): Fix for ProductivityLauncher, which
  977. // does not use suggestion chips.
  978. if (features::IsProductivityLauncherEnabled())
  979. return;
  980. Show();
  981. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  982. std::vector<views::View*> forward_view_list;
  983. forward_view_list.push_back(search_box_view()->search_box());
  984. for (auto* v : GetAllSuggestions())
  985. forward_view_list.push_back(v);
  986. const views::ViewModelT<AppListItemView>* view_model =
  987. apps_grid_view()->view_model();
  988. for (const auto& entry : view_model->entries())
  989. forward_view_list.push_back(entry.view);
  990. forward_view_list.push_back(search_box_view()->search_box());
  991. std::vector<views::View*> backward_view_list = forward_view_list;
  992. std::reverse(backward_view_list.begin(), backward_view_list.end());
  993. // Test traversal triggered by tab.
  994. TestFocusTraversal(forward_view_list, ui::VKEY_TAB, false);
  995. // Test traversal triggered by shift+tab.
  996. TestFocusTraversal(backward_view_list, ui::VKEY_TAB, true);
  997. // Test traversal triggered by right.
  998. TestFocusTraversal(is_rtl_ ? backward_view_list : forward_view_list,
  999. ui::VKEY_RIGHT, false);
  1000. // Test traversal triggered by left.
  1001. TestFocusTraversal(is_rtl_ ? forward_view_list : backward_view_list,
  1002. ui::VKEY_LEFT, false);
  1003. }
  1004. // Tests focus traversal in HALF state with opened search box using |VKEY_TAB|.
  1005. TEST_F(AppListViewPeekingFocusTest, TabFocusTraversalInHalfState) {
  1006. Show();
  1007. // Type something in search box to transition to HALF state and populate
  1008. // fake search results.
  1009. search_box_view()->search_box()->InsertText(
  1010. u"test",
  1011. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1012. EXPECT_EQ(app_list_view()->app_list_state(), ash::AppListViewState::kHalf);
  1013. constexpr int kTileResults = 3;
  1014. constexpr int kListResults = 2;
  1015. SetUpSearchResults(kTileResults, kListResults);
  1016. std::vector<views::View*> forward_view_list;
  1017. const std::vector<SearchResultTileItemView*>& tile_views =
  1018. GetSearchResultTileItemListView()->tile_views_for_test();
  1019. for (int i = 0; i < kTileResults; ++i)
  1020. forward_view_list.push_back(tile_views[i]);
  1021. SearchResultListView* list_view = GetSearchResultListView();
  1022. for (int i = 0; i < kListResults; ++i)
  1023. forward_view_list.push_back(list_view->GetResultViewAt(i));
  1024. // The selected view will always be a result when using
  1025. // |result_selection_controller|
  1026. forward_view_list.push_back(nullptr);
  1027. std::vector<views::View*> backward_view_list = forward_view_list;
  1028. std::reverse(backward_view_list.begin(), backward_view_list.end());
  1029. // Test traversal triggered by tab.
  1030. EXPECT_TRUE(search_box_view()->search_box()->HasFocus());
  1031. TestSelectionTraversal(forward_view_list, ui::VKEY_TAB, false);
  1032. EXPECT_TRUE(search_box_view()->close_button()->HasFocus());
  1033. // Focus cycles from the close button to the first result.
  1034. TestSelectionTraversal({nullptr, forward_view_list[0]}, ui::VKEY_TAB, false);
  1035. EXPECT_TRUE(search_box_view()->search_box()->HasFocus());
  1036. // The shift+tab key should move focus back to the close button.
  1037. TestSelectionTraversal({forward_view_list[0], nullptr}, ui::VKEY_TAB, true);
  1038. EXPECT_TRUE(search_box_view()->close_button()->HasFocus());
  1039. // Test traversal triggered by shift+tab.
  1040. TestSelectionTraversal(backward_view_list, ui::VKEY_TAB, true);
  1041. EXPECT_TRUE(search_box_view()->search_box()->HasFocus());
  1042. }
  1043. // Tests return key with search box close button focused (with app list view in
  1044. // half state):
  1045. // * search box text is cleared
  1046. // * search box gets focus, but it's not active
  1047. // * subsequent tab keys move focus to app list folder view.
  1048. TEST_F(AppListViewPeekingFocusTest, CloseButtonClearsSearchOnEnter) {
  1049. Show();
  1050. // Type something in search box to transition to HALF state and populate
  1051. // fake search results.
  1052. search_box_view()->search_box()->InsertText(
  1053. u"test",
  1054. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1055. EXPECT_EQ(app_list_view()->app_list_state(), ash::AppListViewState::kHalf);
  1056. constexpr int kTileResults = 3;
  1057. constexpr int kListResults = 2;
  1058. SetUpSearchResults(kTileResults, kListResults);
  1059. const std::vector<SearchResultTileItemView*>& tile_views =
  1060. GetSearchResultTileItemListView()->tile_views_for_test();
  1061. ASSERT_FALSE(tile_views.empty());
  1062. views::View* first_result_view = tile_views[0];
  1063. // Shift+Tab to focus close button.
  1064. TestSelectionTraversal({first_result_view, nullptr}, ui::VKEY_TAB, true);
  1065. EXPECT_TRUE(search_box_view()->close_button()->HasFocus());
  1066. // Enter - it should clear the search box.
  1067. SimulateKeyPress(ui::VKEY_RETURN, false /*shift_down*/);
  1068. EXPECT_TRUE(search_box_view()->search_box()->HasFocus());
  1069. EXPECT_EQ(std::u16string(), search_box_view()->search_box()->GetText());
  1070. EXPECT_FALSE(search_box_view()->is_search_box_active());
  1071. EXPECT_FALSE(contents_view()->search_result_page_view()->GetVisible());
  1072. ResultSelectionController* selection_controller =
  1073. contents_view()->search_result_page_view()->result_selection_controller();
  1074. EXPECT_EQ(nullptr, selection_controller->selected_result());
  1075. // Tab traversal continues with app list folder items.
  1076. std::vector<views::View*> forward_view_list;
  1077. forward_view_list.push_back(search_box_view()->search_box());
  1078. const views::ViewModelT<AppListItemView>* view_model =
  1079. app_list_folder_view()->items_grid_view()->view_model();
  1080. for (const auto& entry : view_model->entries())
  1081. forward_view_list.push_back(entry.view);
  1082. TestFocusTraversal(forward_view_list, ui::VKEY_TAB, false);
  1083. }
  1084. // Tests focus traversal in HALF state with opened search box using |VKEY_LEFT|
  1085. // and |VKEY_RIGHT|.
  1086. TEST_P(AppListViewPeekingFocusTest, LeftRightFocusTraversalInHalfState) {
  1087. Show();
  1088. // Type something in search box to transition to HALF state and populate
  1089. // fake search results.
  1090. // Type something in textfield.
  1091. std::u16string text = is_rtl_ ? u"اختبار" : u"test";
  1092. search_box_view()->search_box()->InsertText(
  1093. text,
  1094. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1095. EXPECT_EQ(app_list_view()->app_list_state(), ash::AppListViewState::kHalf);
  1096. constexpr int kTileResults = 6;
  1097. SetUpSearchResults(kTileResults, 0);
  1098. std::vector<views::View*> forward_view_list;
  1099. const std::vector<SearchResultTileItemView*>& tile_views =
  1100. GetSearchResultTileItemListView()->tile_views_for_test();
  1101. forward_view_list.push_back(tile_views[0]);
  1102. for (int i = 1; i < kTileResults; ++i)
  1103. forward_view_list.push_back(tile_views[i]);
  1104. forward_view_list.push_back(tile_views[0]);
  1105. TestSelectionTraversal(forward_view_list,
  1106. is_rtl_ ? ui::VKEY_LEFT : ui::VKEY_RIGHT, false);
  1107. std::vector<views::View*> backward_view_list = forward_view_list;
  1108. std::reverse(backward_view_list.begin(), backward_view_list.end());
  1109. TestSelectionTraversal(backward_view_list,
  1110. is_rtl_ ? ui::VKEY_RIGHT : ui::VKEY_LEFT, false);
  1111. }
  1112. // Tests the linear focus traversal in FULLSCREEN_ALL_APPS state within folder.
  1113. TEST_P(AppListViewFocusTest, LinearFocusTraversalInFolder) {
  1114. Show();
  1115. // Transition to FULLSCREEN_ALL_APPS state and open the folder.
  1116. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  1117. folder_item_view()->RequestFocus();
  1118. SimulateKeyPress(ui::VKEY_RETURN, false);
  1119. EXPECT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  1120. std::vector<views::View*> forward_view_list;
  1121. const views::ViewModelT<AppListItemView>* view_model =
  1122. app_list_folder_view()->items_grid_view()->view_model();
  1123. for (const auto& entry : view_model->entries())
  1124. forward_view_list.push_back(entry.view);
  1125. forward_view_list.push_back(
  1126. app_list_folder_view()->folder_header_view()->GetFolderNameViewForTest());
  1127. forward_view_list.push_back(search_box_view()->search_box());
  1128. forward_view_list.push_back(view_model->view_at(0));
  1129. std::vector<views::View*> backward_view_list = forward_view_list;
  1130. std::reverse(backward_view_list.begin(), backward_view_list.end());
  1131. // Test traversal triggered by tab.
  1132. TestFocusTraversal(forward_view_list, ui::VKEY_TAB, false);
  1133. // Test traversal triggered by shift+tab.
  1134. TestFocusTraversal(backward_view_list, ui::VKEY_TAB, true);
  1135. // Test traversal triggered by right.
  1136. TestFocusTraversal(is_rtl_ ? backward_view_list : forward_view_list,
  1137. ui::VKEY_RIGHT, false);
  1138. // Test traversal triggered by left.
  1139. TestFocusTraversal(is_rtl_ ? forward_view_list : backward_view_list,
  1140. ui::VKEY_LEFT, false);
  1141. }
  1142. // Tests the vertical focus traversal by in PEEKING state.
  1143. TEST_P(AppListViewPeekingFocusTest, VerticalFocusTraversalInPeekingState) {
  1144. Show();
  1145. SetAppListState(ash::AppListViewState::kPeeking);
  1146. std::vector<views::View*> forward_view_list;
  1147. forward_view_list.push_back(search_box_view()->search_box());
  1148. const std::vector<views::View*> suggestions = GetAllSuggestions();
  1149. forward_view_list.push_back(suggestions[0]);
  1150. forward_view_list.push_back(expand_arrow_view());
  1151. forward_view_list.push_back(search_box_view()->search_box());
  1152. // Test traversal triggered by down.
  1153. TestFocusTraversal(forward_view_list, ui::VKEY_DOWN, false);
  1154. std::vector<views::View*> backward_view_list;
  1155. backward_view_list.push_back(search_box_view()->search_box());
  1156. backward_view_list.push_back(expand_arrow_view());
  1157. backward_view_list.push_back(suggestions.back());
  1158. backward_view_list.push_back(search_box_view()->search_box());
  1159. // Test traversal triggered by up.
  1160. TestFocusTraversal(backward_view_list, ui::VKEY_UP, false);
  1161. }
  1162. // Tests the vertical focus traversal in FULLSCREEN_ALL_APPS state.
  1163. TEST_P(AppListViewFocusTest, VerticalFocusTraversalInFullscreenAllAppsState) {
  1164. // TODO(https://crbug.com/1284992): Fix for ProductivityLauncher, which
  1165. // does not use suggestion chips.
  1166. if (features::IsProductivityLauncherEnabled())
  1167. return;
  1168. Show();
  1169. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  1170. std::vector<views::View*> forward_view_list;
  1171. forward_view_list.push_back(search_box_view()->search_box());
  1172. const std::vector<views::View*> suggestions = GetAllSuggestions();
  1173. forward_view_list.push_back(suggestions[0]);
  1174. const views::ViewModelT<AppListItemView>* view_model =
  1175. apps_grid_view()->view_model();
  1176. for (size_t i = 0; i < view_model->view_size(); i += apps_grid_view()->cols())
  1177. forward_view_list.push_back(view_model->view_at(i));
  1178. forward_view_list.push_back(search_box_view()->search_box());
  1179. // Test traversal triggered by down.
  1180. TestFocusTraversal(forward_view_list, ui::VKEY_DOWN, false);
  1181. std::vector<views::View*> backward_view_list;
  1182. backward_view_list.push_back(search_box_view()->search_box());
  1183. for (size_t i = view_model->view_size() - 1; i < view_model->view_size();
  1184. i -= apps_grid_view()->cols())
  1185. backward_view_list.push_back(view_model->view_at(i));
  1186. // Up key will always move focus to the last suggestion chip from first row
  1187. // apps.
  1188. const int index = suggestions.size() - 1;
  1189. backward_view_list.push_back(suggestions[index]);
  1190. backward_view_list.push_back(search_box_view()->search_box());
  1191. // Test traversal triggered by up.
  1192. TestFocusTraversal(backward_view_list, ui::VKEY_UP, false);
  1193. }
  1194. // Tests the vertical focus traversal in HALF state with opened search box.
  1195. TEST_F(AppListViewPeekingFocusTest, VerticalFocusTraversalInHalfState) {
  1196. Show();
  1197. // Type something in search box to transition to HALF state and populate
  1198. // fake search results.
  1199. search_box_view()->search_box()->InsertText(
  1200. u"test",
  1201. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1202. EXPECT_EQ(app_list_view()->app_list_state(), ash::AppListViewState::kHalf);
  1203. constexpr int kTileResults = 3;
  1204. constexpr int kListResults = 2;
  1205. SetUpSearchResults(kTileResults, kListResults);
  1206. std::vector<views::View*> forward_view_list;
  1207. const std::vector<SearchResultTileItemView*>& tile_views =
  1208. GetSearchResultTileItemListView()->tile_views_for_test();
  1209. forward_view_list.push_back(tile_views[0]);
  1210. SearchResultListView* list_view = GetSearchResultListView();
  1211. for (int i = 0; i < kListResults; ++i)
  1212. forward_view_list.push_back(list_view->GetResultViewAt(i));
  1213. contents_view()
  1214. ->search_result_page_view()
  1215. ->result_selection_controller()
  1216. ->ResetSelection(nullptr, false);
  1217. // Test traversal triggered by down.
  1218. TestSelectionTraversal(forward_view_list, ui::VKEY_DOWN, false);
  1219. std::vector<views::View*> backward_view_list;
  1220. for (int i = kListResults - 1; i >= 0; --i)
  1221. backward_view_list.push_back(list_view->GetResultViewAt(i));
  1222. backward_view_list.push_back(tile_views[kTileResults - 1]);
  1223. // Test traversal triggered by up.
  1224. TestSelectionTraversal(backward_view_list, ui::VKEY_UP, false);
  1225. }
  1226. // Tests the vertical focus traversal in FULLSCREEN_ALL_APPS state in the first
  1227. // page within folder.
  1228. TEST_F(AppListViewFocusTest, VerticalFocusTraversalInFirstPageOfFolder) {
  1229. Show();
  1230. // Transition to FULLSCREEN_ALL_APPS state and open the folder.
  1231. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  1232. folder_item_view()->RequestFocus();
  1233. SimulateKeyPress(ui::VKEY_RETURN, false);
  1234. EXPECT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  1235. std::vector<views::View*> forward_view_list;
  1236. const views::ViewModelT<AppListItemView>* view_model =
  1237. app_list_folder_view()->items_grid_view()->view_model();
  1238. for (size_t i = 0; i < view_model->view_size();
  1239. i += app_list_folder_view()->items_grid_view()->cols()) {
  1240. forward_view_list.push_back(view_model->view_at(i));
  1241. }
  1242. forward_view_list.push_back(
  1243. app_list_folder_view()->folder_header_view()->GetFolderNameViewForTest());
  1244. forward_view_list.push_back(search_box_view()->search_box());
  1245. forward_view_list.push_back(view_model->view_at(0));
  1246. // Test traversal triggered by down.
  1247. TestFocusTraversal(forward_view_list, ui::VKEY_DOWN, false);
  1248. std::vector<views::View*> backward_view_list;
  1249. backward_view_list.push_back(view_model->view_at(0));
  1250. backward_view_list.push_back(search_box_view()->search_box());
  1251. backward_view_list.push_back(
  1252. app_list_folder_view()->folder_header_view()->GetFolderNameViewForTest());
  1253. for (size_t i = view_model->view_size() - 1; i < view_model->view_size();
  1254. i -= app_list_folder_view()->items_grid_view()->cols()) {
  1255. backward_view_list.push_back(view_model->view_at(i));
  1256. }
  1257. backward_view_list.push_back(search_box_view()->search_box());
  1258. // Test traversal triggered by up.
  1259. TestFocusTraversal(backward_view_list, ui::VKEY_UP, false);
  1260. }
  1261. // Tests the vertical focus traversal in FULLSCREEN_ALL_APPS state in the second
  1262. // page within folder. ProductivityLauncher does not use pages for folders.
  1263. TEST_F(AppListViewPeekingFocusTest,
  1264. VerticalFocusTraversalInSecondPageOfFolder) {
  1265. Show();
  1266. // Transition to FULLSCREEN_ALL_APPS state and open the folder.
  1267. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  1268. folder_item_view()->RequestFocus();
  1269. SimulateKeyPress(ui::VKEY_RETURN, false);
  1270. EXPECT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  1271. // Select the second page.
  1272. ASSERT_FALSE(features::IsProductivityLauncherEnabled());
  1273. static_cast<PagedAppsGridView*>(app_list_folder_view()->items_grid_view())
  1274. ->pagination_model()
  1275. ->SelectPage(1, false /* animate */);
  1276. std::vector<views::View*> forward_view_list;
  1277. const views::ViewModelT<AppListItemView>* view_model =
  1278. app_list_folder_view()->items_grid_view()->view_model();
  1279. for (size_t i = kMaxItemsPerFolderPage; i < view_model->view_size();
  1280. i += app_list_folder_view()->items_grid_view()->cols()) {
  1281. forward_view_list.push_back(view_model->view_at(i));
  1282. }
  1283. forward_view_list.push_back(
  1284. app_list_folder_view()->folder_header_view()->GetFolderNameViewForTest());
  1285. forward_view_list.push_back(search_box_view()->search_box());
  1286. forward_view_list.push_back(view_model->view_at(0));
  1287. // Test traversal triggered by down.
  1288. TestFocusTraversal(forward_view_list, ui::VKEY_DOWN, false);
  1289. std::vector<views::View*> backward_view_list;
  1290. backward_view_list.push_back(view_model->view_at(0));
  1291. backward_view_list.push_back(search_box_view()->search_box());
  1292. backward_view_list.push_back(
  1293. app_list_folder_view()->folder_header_view()->GetFolderNameViewForTest());
  1294. for (size_t i = view_model->view_size() - 1; i < view_model->view_size();
  1295. i -= app_list_folder_view()->items_grid_view()->cols()) {
  1296. backward_view_list.push_back(view_model->view_at(i));
  1297. }
  1298. backward_view_list.push_back(search_box_view()->search_box());
  1299. // Test traversal triggered by up.
  1300. TestFocusTraversal(backward_view_list, ui::VKEY_UP, false);
  1301. }
  1302. // Tests that the focus is set back onto search box after all state transitions
  1303. // besides those going to/from an activated folder.
  1304. TEST_F(AppListViewPeekingFocusTest, FocusResetAfterStateTransition) {
  1305. Show();
  1306. // Type something in search box to transition to HALF state and populate
  1307. // fake search results.
  1308. search_box_view()->search_box()->InsertText(
  1309. u"test",
  1310. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1311. const int kTileResults = 3;
  1312. const int kListResults = 2;
  1313. SetUpSearchResults(kTileResults, kListResults);
  1314. EXPECT_EQ(app_list_view()->app_list_state(), ash::AppListViewState::kHalf);
  1315. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  1316. // Move focus to the first search result, then transition to PEEKING state.
  1317. SimulateKeyPress(ui::VKEY_TAB, false);
  1318. SimulateKeyPress(ui::VKEY_TAB, false);
  1319. SetAppListState(ash::AppListViewState::kPeeking);
  1320. EXPECT_EQ(app_list_view()->app_list_state(), ash::AppListViewState::kPeeking);
  1321. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  1322. // Move focus to the first suggestion app, then transition to
  1323. // FULLSCREEN_ALL_APPS state.
  1324. SimulateKeyPress(ui::VKEY_TAB, false);
  1325. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  1326. EXPECT_EQ(app_list_view()->app_list_state(),
  1327. ash::AppListViewState::kFullscreenAllApps);
  1328. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  1329. // Move focus to the folder and open it.
  1330. folder_item_view()->RequestFocus();
  1331. SimulateKeyPress(ui::VKEY_RETURN, false);
  1332. // Test that the first item in the folder is focused.
  1333. EXPECT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  1334. EXPECT_EQ(app_list_folder_view()->items_grid_view()->view_model()->view_at(0),
  1335. focused_view());
  1336. // Close the folder.
  1337. SimulateKeyPress(ui::VKEY_ESCAPE, false);
  1338. // Test that focus is on the previously activated folder item
  1339. EXPECT_EQ(folder_item_view(), focused_view());
  1340. // Transition to PEEKING state.
  1341. SetAppListState(ash::AppListViewState::kPeeking);
  1342. // Test that the searchbox is focused.
  1343. EXPECT_EQ(app_list_view()->app_list_state(), ash::AppListViewState::kPeeking);
  1344. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  1345. }
  1346. // Tests that key event which is not handled by focused view will be redirected
  1347. // to search box when search box view is active (but not focused).
  1348. TEST_F(AppListViewFocusTest, RedirectFocusToSearchBox) {
  1349. // TODO(https://crbug.com/1284992): Fix for ProductivityLauncher, which
  1350. // does not support this behavior and also does not use suggestion chips.
  1351. if (features::IsProductivityLauncherEnabled())
  1352. return;
  1353. Show();
  1354. // Set focus to first suggestion app and type a character.
  1355. GetAllSuggestions()[0]->RequestFocus();
  1356. SimulateKeyPress(ui::VKEY_A, false);
  1357. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  1358. EXPECT_EQ(search_box_view()->search_box()->GetText(), u"a");
  1359. EXPECT_FALSE(search_box_view()->search_box()->HasSelection());
  1360. // Set focus to close button and type a character.
  1361. search_box_view()->close_button()->RequestFocus();
  1362. EXPECT_NE(search_box_view()->search_box(), focused_view());
  1363. SimulateKeyPress(ui::VKEY_B, false);
  1364. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  1365. EXPECT_EQ(search_box_view()->search_box()->GetText(), u"ab");
  1366. EXPECT_FALSE(search_box_view()->search_box()->HasSelection());
  1367. // Set focus to close button and hitting backspace.
  1368. search_box_view()->close_button()->RequestFocus();
  1369. SimulateKeyPress(ui::VKEY_BACK, false);
  1370. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  1371. EXPECT_EQ(search_box_view()->search_box()->GetText(), u"a");
  1372. EXPECT_FALSE(search_box_view()->search_box()->HasSelection());
  1373. }
  1374. // Tests that focus changes update the search box text.
  1375. TEST_F(AppListViewFocusTest, SearchBoxTextUpdatesOnResultFocus) {
  1376. Show();
  1377. views::Textfield* search_box = search_box_view()->search_box();
  1378. search_box->InsertText(
  1379. u"TestText",
  1380. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1381. // Set up test results with unique titles
  1382. ClearSearchResults();
  1383. AddSearchResultWithTitleAndScore("TestResult1", 3);
  1384. AddSearchResultWithTitleAndScore("TestResult2", 2);
  1385. AddSearchResultWithTitleAndScore("TestResult3", 1);
  1386. // Change focus to the next result
  1387. SimulateKeyPress(ui::VKEY_TAB, false);
  1388. EXPECT_EQ(search_box->GetText(), u"TestResult2");
  1389. SimulateKeyPress(ui::VKEY_TAB, true);
  1390. EXPECT_EQ(search_box->GetText(), u"TestResult1");
  1391. SimulateKeyPress(ui::VKEY_TAB, false);
  1392. // Change focus to the final result
  1393. SimulateKeyPress(ui::VKEY_TAB, false);
  1394. EXPECT_EQ(search_box->GetText(), u"TestResult3");
  1395. }
  1396. // Tests that ctrl-A selects all text in the searchbox when the SearchBoxView is
  1397. // not focused.
  1398. TEST_F(AppListViewFocusTest, CtrlASelectsAllTextInSearchbox) {
  1399. Show();
  1400. search_box_view()->search_box()->InsertText(
  1401. u"test",
  1402. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1403. EXPECT_EQ(app_list_view()->app_list_state(), ash::AppListViewState::kHalf);
  1404. constexpr int kTileResults = 3;
  1405. constexpr int kListResults = 2;
  1406. SetUpSearchResults(kTileResults, kListResults);
  1407. // Move focus to the first search result.
  1408. SimulateKeyPress(ui::VKEY_TAB, false);
  1409. SimulateKeyPress(ui::VKEY_TAB, false);
  1410. // Focus left the searchbox, so the selected range should be at the end of the
  1411. // search text.
  1412. EXPECT_FALSE(search_box_view()->search_box()->HasSelection());
  1413. EXPECT_EQ(gfx::Range(4, 4),
  1414. search_box_view()->search_box()->GetSelectedRange());
  1415. // Press Ctrl-A, everything should be selected and the selected range should
  1416. // include the whole text.
  1417. SimulateKeyPress(ui::VKEY_A, false, true);
  1418. EXPECT_TRUE(search_box_view()->search_box()->HasSelection());
  1419. EXPECT_EQ(gfx::Range(0, 4),
  1420. search_box_view()->search_box()->GetSelectedRange());
  1421. // Advance focus, Focus should leave the searchbox, and the selected range
  1422. // should be at the end of the search text.
  1423. SimulateKeyPress(ui::VKEY_TAB, false);
  1424. EXPECT_FALSE(search_box_view()->search_box()->HasSelection());
  1425. EXPECT_EQ(gfx::Range(4, 4),
  1426. search_box_view()->search_box()->GetSelectedRange());
  1427. }
  1428. // Tests that the first search result's view is selected after search results
  1429. // are updated when the focus is on search box.
  1430. // crbug.com/1242053: flaky on chromeos
  1431. #if BUILDFLAG(IS_CHROMEOS)
  1432. #define MAYBE_FirstResultSelectedAfterSearchResultsUpdated \
  1433. DISABLED_FirstResultSelectedAfterSearchResultsUpdated
  1434. #else
  1435. #define MAYBE_FirstResultSelectedAfterSearchResultsUpdated \
  1436. FirstResultSelectedAfterSearchResultsUpdated
  1437. #endif
  1438. TEST_F(AppListViewFocusTest,
  1439. MAYBE_FirstResultSelectedAfterSearchResultsUpdated) {
  1440. Show();
  1441. // Type something in search box to transition to HALF state and populate
  1442. // fake list results.
  1443. search_box_view()->search_box()->InsertText(
  1444. u"test",
  1445. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1446. const int kListResults = 2;
  1447. SetUpSearchResults(0, kListResults);
  1448. SearchResultListView* list_view = GetSearchResultListView();
  1449. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  1450. EXPECT_EQ(list_view->GetResultViewAt(0),
  1451. contents_view()->search_result_page_view()->first_result_view());
  1452. EXPECT_TRUE(list_view->GetResultViewAt(0)->selected());
  1453. // Populate both fake list results and tile results.
  1454. const int kTileResults = 3;
  1455. SetUpSearchResults(kTileResults, kListResults);
  1456. const std::vector<SearchResultTileItemView*>& tile_views =
  1457. GetSearchResultTileItemListView()->tile_views_for_test();
  1458. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  1459. EXPECT_EQ(tile_views[0],
  1460. contents_view()->search_result_page_view()->first_result_view());
  1461. EXPECT_TRUE(tile_views[0]->selected());
  1462. ResultSelectionController* selection_controller =
  1463. contents_view()->search_result_page_view()->result_selection_controller();
  1464. // Ensures the |ResultSelectionController| selects the correct result
  1465. EXPECT_EQ(selection_controller->selected_result(), tile_views[0]);
  1466. // Ensure current highlighted result loses highlight on transition
  1467. SimulateKeyPress(ui::VKEY_TAB, false);
  1468. EXPECT_FALSE(tile_views[0]->selected());
  1469. // Clear up all search results.
  1470. SetUpSearchResults(0, 0);
  1471. EXPECT_EQ(search_box_view()->search_box(), focused_view());
  1472. EXPECT_EQ(nullptr,
  1473. contents_view()->search_result_page_view()->first_result_view());
  1474. }
  1475. // Tests hitting Enter key when focus is on search box.
  1476. // There are two behaviors:
  1477. // 1. Activate the search box when it is inactive.
  1478. // 2. Open the first result when query exists.
  1479. TEST_F(AppListViewFocusTest, HittingEnterWhenFocusOnSearchBox) {
  1480. Show();
  1481. // Initially the search box is inactive, hitting Enter to activate it.
  1482. EXPECT_FALSE(search_box_view()->is_search_box_active());
  1483. SimulateKeyPress(ui::VKEY_RETURN, false);
  1484. EXPECT_TRUE(search_box_view()->is_search_box_active());
  1485. // Type something in search box to transition to HALF state and populate
  1486. // fake list results. Then hit Enter key.
  1487. search_box_view()->search_box()->InsertText(
  1488. u"test",
  1489. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1490. const int kListResults = 2;
  1491. SetUpSearchResults(0, kListResults);
  1492. SimulateKeyPress(ui::VKEY_RETURN, false);
  1493. EXPECT_EQ(1, GetOpenFirstSearchResultCount());
  1494. EXPECT_EQ(1, GetTotalOpenSearchResultCount());
  1495. // Populate both fake list results and tile results. Then hit Enter key.
  1496. const int kTileResults = 3;
  1497. SetUpSearchResults(kTileResults, kListResults);
  1498. SimulateKeyPress(ui::VKEY_RETURN, false);
  1499. EXPECT_EQ(2, GetOpenFirstSearchResultCount());
  1500. EXPECT_EQ(2, GetTotalOpenSearchResultCount());
  1501. // Clear up all search results. Then hit Enter key.
  1502. SetUpSearchResults(0, 0);
  1503. SimulateKeyPress(ui::VKEY_RETURN, false);
  1504. EXPECT_EQ(2, GetOpenFirstSearchResultCount());
  1505. EXPECT_EQ(2, GetTotalOpenSearchResultCount());
  1506. }
  1507. // Tests that search box becomes focused when it is activated.
  1508. TEST_F(AppListViewFocusTest, SetFocusOnSearchboxWhenActivated) {
  1509. Show();
  1510. // Press tab several times to move focus out of the search box.
  1511. SimulateKeyPress(ui::VKEY_TAB, false);
  1512. SimulateKeyPress(ui::VKEY_TAB, false);
  1513. SimulateKeyPress(ui::VKEY_TAB, false);
  1514. EXPECT_FALSE(search_box_view()->search_box()->HasFocus());
  1515. // Activate the search box.
  1516. search_box_view()->SetSearchBoxActive(true, ui::ET_MOUSE_PRESSED);
  1517. EXPECT_TRUE(search_box_view()->search_box()->HasFocus());
  1518. // Deactivate the search box won't move focus away.
  1519. search_box_view()->SetSearchBoxActive(false, ui::ET_MOUSE_PRESSED);
  1520. EXPECT_TRUE(search_box_view()->search_box()->HasFocus());
  1521. }
  1522. // Tests the left and right key when focus is on the textfield.
  1523. TEST_P(AppListViewFocusTest, HittingLeftRightWhenFocusOnTextfield) {
  1524. Show();
  1525. // Transition to FULLSCREEN_ALL_APPS state and open the folder.
  1526. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  1527. folder_item_view()->RequestFocus();
  1528. SimulateKeyPress(ui::VKEY_RETURN, false);
  1529. // Set focus on the folder name.
  1530. views::Textfield* folder_name_view = static_cast<views::Textfield*>(
  1531. app_list_folder_view()->folder_header_view()->GetFolderNameViewForTest());
  1532. folder_name_view->RequestFocus();
  1533. // Test folder name.
  1534. TestLeftAndRightKeyOnTextfieldWithText(folder_name_view, false);
  1535. TestLeftAndRightKeyOnTextfieldWithText(folder_name_view, true);
  1536. // Set focus on the search box.
  1537. search_box_view()->search_box()->RequestFocus();
  1538. // Test search box. Active traversal has been tested at this point. This will
  1539. // specifically test inactive traversal with no search results set up.
  1540. TestLeftAndRightKeyTraversalOnTextfield(search_box_view()->search_box());
  1541. }
  1542. // Tests that the focus is reset and the folder does not exit after hitting
  1543. // enter/escape on folder name.
  1544. TEST_P(AppListViewFocusTest, FocusResetAfterHittingEnterOrEscapeOnFolderName) {
  1545. Show();
  1546. // Transition to FULLSCREEN_ALL_APPS state and open the folder.
  1547. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  1548. folder_item_view()->RequestFocus();
  1549. SimulateKeyPress(ui::VKEY_RETURN, false);
  1550. EXPECT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  1551. // Set focus on the folder name.
  1552. views::View* folder_name_view =
  1553. app_list_folder_view()->folder_header_view()->GetFolderNameViewForTest();
  1554. folder_name_view->RequestFocus();
  1555. // Hit enter key.
  1556. SimulateKeyPress(ui::VKEY_RETURN, false);
  1557. EXPECT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  1558. EXPECT_FALSE(folder_name_view->HasFocus());
  1559. // Refocus and hit escape key.
  1560. folder_name_view->RequestFocus();
  1561. SimulateKeyPress(ui::VKEY_ESCAPE, false);
  1562. EXPECT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  1563. EXPECT_FALSE(folder_name_view->HasFocus());
  1564. }
  1565. // Tests that the selection highlight follows the page change.
  1566. TEST_F(AppListViewFocusTest, SelectionHighlightFollowsChangingPage) {
  1567. // Move the focus to the first app in the grid.
  1568. Show();
  1569. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  1570. const views::ViewModelT<AppListItemView>* view_model =
  1571. apps_grid_view()->view_model();
  1572. AppListItemView* first_item_view = view_model->view_at(0);
  1573. first_item_view->RequestFocus();
  1574. ASSERT_EQ(0, apps_grid_view()->pagination_model()->selected_page());
  1575. // Select the second page.
  1576. apps_grid_view()->pagination_model()->SelectPage(1, false);
  1577. // Test that focus followed to the next page.
  1578. EXPECT_EQ(view_model->view_at(test_api()->TilesPerPage(0)),
  1579. apps_grid_view()->selected_view());
  1580. // Select the first page.
  1581. apps_grid_view()->pagination_model()->SelectPage(0, false);
  1582. // Test that focus followed.
  1583. EXPECT_EQ(view_model->view_at(test_api()->TilesPerPage(0) - 1),
  1584. apps_grid_view()->selected_view());
  1585. }
  1586. // Tests that the selection highlight only shows up inside a folder if the
  1587. // selection highlight existed on the folder before it opened.
  1588. TEST_F(AppListViewFocusTest, SelectionDoesNotShowInFolderIfNotSelected) {
  1589. // Open a folder without making the view selected.
  1590. Show();
  1591. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  1592. const gfx::Point folder_item_view_bounds =
  1593. folder_item_view()->bounds().CenterPoint();
  1594. ui::GestureEvent tap(folder_item_view_bounds.x(), folder_item_view_bounds.y(),
  1595. 0, base::TimeTicks(),
  1596. ui::GestureEventDetails(ui::ET_GESTURE_TAP));
  1597. folder_item_view()->OnGestureEvent(&tap);
  1598. ASSERT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  1599. // Test that there is no selected view in the folders grid view, but the first
  1600. // item is focused.
  1601. AppsGridView* items_grid_view = app_list_folder_view()->items_grid_view();
  1602. EXPECT_FALSE(items_grid_view->has_selected_view());
  1603. EXPECT_EQ(items_grid_view->view_model()->view_at(0), focused_view());
  1604. // Hide the folder, expect that the folder is not selected, but is focused.
  1605. app_list_view()->AcceleratorPressed(
  1606. ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  1607. EXPECT_FALSE(apps_grid_view()->has_selected_view());
  1608. EXPECT_EQ(folder_item_view(), focused_view());
  1609. }
  1610. // Tests that the selection highlight only shows on the activated folder if it
  1611. // existed within the folder.
  1612. TEST_F(AppListViewFocusTest, SelectionGoesIntoFolderIfSelected) {
  1613. // Open a folder without making the view selected.
  1614. Show();
  1615. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  1616. folder_item_view()->RequestFocus();
  1617. ASSERT_TRUE(apps_grid_view()->IsSelectedView(folder_item_view()));
  1618. // Show the folder.
  1619. const gfx::Point folder_item_view_bounds =
  1620. folder_item_view()->bounds().CenterPoint();
  1621. ui::GestureEvent tap(folder_item_view_bounds.x(), folder_item_view_bounds.y(),
  1622. 0, base::TimeTicks(),
  1623. ui::GestureEventDetails(ui::ET_GESTURE_TAP));
  1624. folder_item_view()->OnGestureEvent(&tap);
  1625. ASSERT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  1626. // Test that the focused view is also selected.
  1627. AppsGridView* items_grid_view = app_list_folder_view()->items_grid_view();
  1628. EXPECT_EQ(items_grid_view->selected_view(), focused_view());
  1629. EXPECT_EQ(items_grid_view->view_model()->view_at(0), focused_view());
  1630. // Hide the folder, expect that the folder is selected and focused.
  1631. app_list_view()->AcceleratorPressed(
  1632. ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  1633. EXPECT_TRUE(apps_grid_view()->IsSelectedView(folder_item_view()));
  1634. EXPECT_EQ(folder_item_view(), focused_view());
  1635. }
  1636. // Tests that opening the app list opens in peeking mode by default.
  1637. TEST_F(AppListViewPeekingTest, ShowPeekingByDefault) {
  1638. Initialize(false /*is_tablet_mode*/);
  1639. Show();
  1640. ASSERT_EQ(ash::AppListViewState::kPeeking, view_->app_list_state());
  1641. }
  1642. // Tests that in side shelf mode, the app list opens in fullscreen by default
  1643. // and verifies that the top rounded corners of the app list background are
  1644. // hidden (see https://crbug.com/920082). ProductivityLauncher does not change
  1645. // shelf corners.
  1646. TEST_F(AppListViewPeekingTest, ShowFullscreenWhenInSideShelfMode) {
  1647. Initialize(false /*is_tablet_mode*/);
  1648. Show(true /*is_side_shelf*/);
  1649. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1650. // The rounded corners should be off screen in side shelf.
  1651. gfx::Transform translation;
  1652. translation.Translate(0, -(delegate_->GetShelfSize() / 2));
  1653. // The rounded corners should be off screen in side shelf.
  1654. EXPECT_EQ(translation,
  1655. view_->GetAppListBackgroundShieldForTest()->GetTransform());
  1656. }
  1657. // Tests that in tablet mode, the app list opens in fullscreen by default.
  1658. TEST_F(AppListViewTest, ShowFullscreenWhenInTabletMode) {
  1659. Initialize(true /*is_tablet_mode*/);
  1660. Show();
  1661. ASSERT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1662. }
  1663. // Tests that setting empty text in the search box does not change the state.
  1664. TEST_F(AppListViewPeekingTest, EmptySearchTextStillPeeking) {
  1665. Initialize(false /*is_tablet_mode*/);
  1666. views::Textfield* search_box =
  1667. view_->app_list_main_view()->search_box_view()->search_box();
  1668. Show();
  1669. search_box->SetText(std::u16string());
  1670. ASSERT_EQ(ash::AppListViewState::kPeeking, view_->app_list_state());
  1671. }
  1672. TEST_F(AppListViewPeekingTest, UpwardMouseWheelScrollTransitionsToFullscreen) {
  1673. base::HistogramTester histogram_tester;
  1674. Initialize(false /*is_tablet_mode*/);
  1675. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  1676. Show();
  1677. EXPECT_EQ(AppListViewState::kPeeking, view_->app_list_state());
  1678. view_->HandleScroll(gfx::Point(0, 0), gfx::Vector2d(0, 30),
  1679. ui::ET_MOUSEWHEEL);
  1680. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1681. // This should use animation instead of drag.
  1682. // TODO(oshima): Test AnimationSmoothness.
  1683. histogram_tester.ExpectTotalCount(
  1684. "Apps.StateTransition.Drag.PresentationTime.ClamshellMode", 0);
  1685. auto grid_bounds = gfx::RectF(apps_grid_view()->bounds());
  1686. views::View::ConvertRectToTarget(apps_grid_view(), view_, &grid_bounds);
  1687. view_->HandleScroll(gfx::ToRoundedPoint(grid_bounds.CenterPoint()),
  1688. gfx::Vector2d(0, 30), ui::ET_MOUSEWHEEL);
  1689. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1690. // Scrolls inside or to the side of the apps grid should not dismiss.
  1691. view_->HandleScroll(
  1692. gfx::ToRoundedPoint(grid_bounds.left_center()) + gfx::Vector2d(-20, 0),
  1693. gfx::Vector2d(0, -30), ui::ET_MOUSEWHEEL);
  1694. ASSERT_EQ(0, delegate_->dismiss_count());
  1695. // Scroll above the app list should dismiss.
  1696. view_->HandleScroll(gfx::Point(0, 0), gfx::Vector2d(0, -30),
  1697. ui::ET_MOUSEWHEEL);
  1698. ASSERT_EQ(1, delegate_->dismiss_count());
  1699. }
  1700. TEST_F(AppListViewPeekingTest,
  1701. DownwardMouseWheelScrollDismissesPeekingLauncher) {
  1702. Initialize(false /*is_tablet_mode*/);
  1703. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  1704. Show();
  1705. EXPECT_EQ(ash::AppListViewState::kPeeking, view_->app_list_state());
  1706. EXPECT_EQ(0, delegate_->dismiss_count());
  1707. view_->HandleScroll(gfx::Point(0, 0), gfx::Vector2d(0, -30),
  1708. ui::ET_MOUSEWHEEL);
  1709. EXPECT_EQ(1, delegate_->dismiss_count());
  1710. }
  1711. TEST_F(AppListViewPeekingTest, UpwardGestureScrollTransitionsToFullscreen) {
  1712. base::HistogramTester histogram_tester;
  1713. Initialize(false /*is_tablet_mode*/);
  1714. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  1715. Show();
  1716. EXPECT_EQ(AppListViewState::kPeeking, view_->app_list_state());
  1717. view_->HandleScroll(gfx::Point(0, 0), gfx::Vector2d(0, 30), ui::ET_SCROLL);
  1718. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1719. // This should use animation instead of drag.
  1720. // TODO(oshima): Test AnimationSmoothness.
  1721. histogram_tester.ExpectTotalCount(
  1722. "Apps.StateTransition.Drag.PresentationTime.ClamshellMode", 0);
  1723. }
  1724. TEST_F(AppListViewPeekingTest, DownwardGestureScrollDismissesPeekingLauncher) {
  1725. Initialize(false /*is_tablet_mode*/);
  1726. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  1727. Show();
  1728. EXPECT_EQ(ash::AppListViewState::kPeeking, view_->app_list_state());
  1729. EXPECT_EQ(0, delegate_->dismiss_count());
  1730. view_->HandleScroll(gfx::Point(0, 0), gfx::Vector2d(0, -30), ui::ET_SCROLL);
  1731. EXPECT_EQ(1, delegate_->dismiss_count());
  1732. }
  1733. // Tests that typing text after opening transitions from peeking to half.
  1734. TEST_F(AppListViewPeekingTest, TypingPeekingToHalf) {
  1735. Initialize(false /*is_tablet_mode*/);
  1736. views::Textfield* search_box =
  1737. view_->app_list_main_view()->search_box_view()->search_box();
  1738. Show();
  1739. search_box->SetText(std::u16string());
  1740. search_box->InsertText(
  1741. u"nice",
  1742. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1743. ASSERT_EQ(ash::AppListViewState::kHalf, view_->app_list_state());
  1744. }
  1745. // Tests that typing when in fullscreen changes the state to fullscreen search.
  1746. TEST_F(AppListViewPeekingTest, TypingFullscreenToFullscreenSearch) {
  1747. Initialize(false /*is_tablet_mode*/);
  1748. Show();
  1749. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  1750. views::Textfield* search_box =
  1751. view_->app_list_main_view()->search_box_view()->search_box();
  1752. search_box->SetText(std::u16string());
  1753. search_box->InsertText(
  1754. u"https://youtu.be/dQw4w9WgXcQ",
  1755. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1756. ASSERT_EQ(ash::AppListViewState::kFullscreenSearch, view_->app_list_state());
  1757. }
  1758. // Tests that in tablet mode, typing changes the state to fullscreen search.
  1759. TEST_F(AppListViewTest, TypingTabletModeFullscreenSearch) {
  1760. Initialize(true /*is_tablet_mode*/);
  1761. views::Textfield* search_box =
  1762. view_->app_list_main_view()->search_box_view()->search_box();
  1763. Show();
  1764. search_box->SetText(std::u16string());
  1765. search_box->InsertText(
  1766. u"cool!",
  1767. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  1768. ASSERT_EQ(ash::AppListViewState::kFullscreenSearch, view_->app_list_state());
  1769. }
  1770. // Tests that pressing escape when in peeking closes the app list.
  1771. TEST_F(AppListViewPeekingTest, EscapeKeyPeekingToClosed) {
  1772. Initialize(false /*is_tablet_mode*/);
  1773. Show();
  1774. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  1775. ASSERT_EQ(1, delegate_->dismiss_count());
  1776. }
  1777. // Tests that pressing escape when in half screen changes the state to peeking.
  1778. TEST_F(AppListViewPeekingTest, EscapeKeyHalfToPeeking) {
  1779. Initialize(false /*is_tablet_mode*/);
  1780. Show();
  1781. SetTextInSearchBox(u"doggie");
  1782. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  1783. ASSERT_EQ(ash::AppListViewState::kPeeking, view_->app_list_state());
  1784. }
  1785. // Tests that pressing escape when in fullscreen changes the state to closed.
  1786. TEST_F(AppListViewPeekingTest, EscapeKeyFullscreenToClosed) {
  1787. Initialize(false /*is_tablet_mode*/);
  1788. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  1789. Show();
  1790. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  1791. ASSERT_EQ(1, delegate_->dismiss_count());
  1792. }
  1793. // Tests that pressing escape when in fullscreen side-shelf closes the app list.
  1794. TEST_F(AppListViewPeekingTest, EscapeKeySideShelfFullscreenToClosed) {
  1795. // Put into fullscreen by using side-shelf.
  1796. Initialize(false /*is_tablet_mode*/);
  1797. Show(/*is_side_shelf=*/true);
  1798. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  1799. ASSERT_EQ(1, delegate_->dismiss_count());
  1800. }
  1801. // Tests that pressing escape when in tablet mode keeps app list in fullscreen.
  1802. TEST_P(AppListViewTabletTest, EscapeKeyTabletModeStayFullscreen) {
  1803. // Put into fullscreen by using tablet mode.
  1804. Initialize(/*is_tablet_mode=*/true);
  1805. Show();
  1806. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  1807. ASSERT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1808. }
  1809. // Tests that pressing escape when in fullscreen search changes to fullscreen.
  1810. TEST_F(AppListViewPeekingTest, EscapeKeyFullscreenSearchToFullscreen) {
  1811. Initialize(false /*is_tablet_mode*/);
  1812. Show();
  1813. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  1814. SetTextInSearchBox(u"https://youtu.be/dQw4w9WgXcQ");
  1815. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  1816. ASSERT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1817. }
  1818. // Tests that pressing escape when in sideshelf search changes to fullscreen.
  1819. TEST_F(AppListViewPeekingTest, EscapeKeySideShelfSearchToFullscreen) {
  1820. // Put into fullscreen using side-shelf.
  1821. Initialize(false /*is_tablet_mode*/);
  1822. Show(true /*is_side_shelf*/);
  1823. SetTextInSearchBox(u"kitty");
  1824. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  1825. ASSERT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1826. }
  1827. // Tests that in fullscreen, the app list has multiple pages with enough apps.
  1828. TEST_F(AppListViewTest, PopulateAppsCreatesAnotherPage) {
  1829. Initialize(/*is_tablet_mode=*/true);
  1830. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  1831. Show();
  1832. ASSERT_EQ(2, GetPaginationModel()->total_pages());
  1833. }
  1834. // Tests that pressing escape when in tablet search changes to fullscreen.
  1835. TEST_F(AppListViewTest, EscapeKeyTabletModeSearchToFullscreen) {
  1836. // Put into fullscreen using tablet mode.
  1837. Initialize(true /*is_tablet_mode*/);
  1838. Show();
  1839. SetTextInSearchBox(u"yay");
  1840. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  1841. ASSERT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1842. }
  1843. // Tests that opening in peeking mode sets the correct height.
  1844. TEST_F(AppListViewPeekingTest, OpenInPeekingCorrectHeight) {
  1845. Initialize(false /*is_tablet_mode*/);
  1846. Show();
  1847. view_->SetState(AppListViewState::kPeeking);
  1848. ASSERT_EQ(view_->GetHeightForState(AppListViewState::kPeeking),
  1849. view_->GetCurrentAppListHeight());
  1850. }
  1851. // Tests that opening in fullscreen mode sets the correct height.
  1852. TEST_F(AppListViewPeekingTest, OpenInFullscreenCorrectHeight) {
  1853. Initialize(false /*is_tablet_mode*/);
  1854. Show();
  1855. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  1856. const int y = view_->GetWidget()->GetWindowBoundsInScreen().y();
  1857. ASSERT_EQ(0, y);
  1858. }
  1859. // Tests that AppListView::SetState succeeds when the state has been set to
  1860. // CLOSED.
  1861. TEST_F(AppListViewPeekingTest, SetStateFailsWhenClosing) {
  1862. Initialize(false /*is_tablet_mode*/);
  1863. Show();
  1864. view_->SetState(ash::AppListViewState::kClosed);
  1865. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  1866. ASSERT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1867. }
  1868. TEST_F(AppListViewPeekingTest, AppsGridViewVisibilityOnReopening) {
  1869. Initialize(false /*is_tablet_mode*/);
  1870. Show();
  1871. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  1872. EXPECT_TRUE(IsViewVisibleOnScreen(apps_grid_view()));
  1873. view_->SetState(ash::AppListViewState::kFullscreenSearch);
  1874. SetAppListState(ash::AppListState::kStateSearchResults);
  1875. EXPECT_TRUE(IsViewVisibleOnScreen(apps_grid_view()));
  1876. // Close the app-list and re-show to fullscreen all apps.
  1877. view_->SetState(ash::AppListViewState::kClosed);
  1878. Show();
  1879. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  1880. EXPECT_TRUE(IsViewVisibleOnScreen(apps_grid_view()));
  1881. }
  1882. TEST_F(AppListViewPeekingTest, AppsGridViewExpandHintingOnReopening) {
  1883. ui::ScopedAnimationDurationScaleMode non_zero_duration(
  1884. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
  1885. Initialize(false /*is_tablet_mode*/);
  1886. Show();
  1887. view_->SetState(ash::AppListViewState::kPeeking);
  1888. EXPECT_TRUE(
  1889. contents_view()->expand_arrow_view()->IsHintingAnimationRunningForTest());
  1890. view_->SetState(ash::AppListViewState::kClosed);
  1891. EXPECT_FALSE(
  1892. contents_view()->expand_arrow_view()->IsHintingAnimationRunningForTest());
  1893. Show();
  1894. view_->SetState(ash::AppListViewState::kPeeking);
  1895. EXPECT_TRUE(
  1896. contents_view()->expand_arrow_view()->IsHintingAnimationRunningForTest());
  1897. }
  1898. // Tests that going into a folder view, then setting the AppListState to PEEKING
  1899. // hides the folder view.
  1900. TEST_F(AppListViewPeekingTest, FolderViewToPeeking) {
  1901. Initialize(false /*is_tablet_mode*/);
  1902. AppListTestModel* model = delegate_->GetTestModel();
  1903. model->PopulateApps(kInitialItems);
  1904. const std::string folder_id =
  1905. model->MergeItems(model->top_level_item_list()->item_at(0)->id(),
  1906. model->top_level_item_list()->item_at(1)->id());
  1907. model->FindFolderItem(folder_id);
  1908. Show();
  1909. AppsGridViewTestApi test_api(view_->app_list_main_view()
  1910. ->contents_view()
  1911. ->apps_container_view()
  1912. ->apps_grid_view());
  1913. test_api.PressItemAt(0);
  1914. EXPECT_TRUE(view_->app_list_main_view()
  1915. ->contents_view()
  1916. ->apps_container_view()
  1917. ->IsInFolderView());
  1918. view_->SetState(ash::AppListViewState::kPeeking);
  1919. EXPECT_FALSE(view_->app_list_main_view()
  1920. ->contents_view()
  1921. ->apps_container_view()
  1922. ->IsInFolderView());
  1923. }
  1924. // Tests that a tap or click in an empty region of the AppsGridView closes the
  1925. // AppList. ProductivityLauncher does not have this behavior.
  1926. TEST_F(AppListViewPeekingTest, TapAndClickWithinAppsGridView) {
  1927. Initialize(false /*is_tablet_mode*/);
  1928. // Populate the AppList with a small number of apps so there is an empty
  1929. // region to click.
  1930. delegate_->GetTestModel()->PopulateApps(6);
  1931. Show();
  1932. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  1933. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1934. AppsGridView* apps_grid_view = view_->app_list_main_view()
  1935. ->contents_view()
  1936. ->apps_container_view()
  1937. ->apps_grid_view();
  1938. AppsGridViewTestApi test_api(apps_grid_view);
  1939. // Get the point of the first empty region (where app #7 would be) and tap on
  1940. // it, the AppList should close.
  1941. const gfx::Point empty_region =
  1942. test_api.GetItemTileRectOnCurrentPageAt(2, 2).CenterPoint();
  1943. ui::GestureEvent tap(empty_region.x(), empty_region.y(), 0, base::TimeTicks(),
  1944. ui::GestureEventDetails(ui::ET_GESTURE_TAP));
  1945. ui::Event::DispatcherApi tap_dispatcher_api(static_cast<ui::Event*>(&tap));
  1946. tap_dispatcher_api.set_target(view_);
  1947. view_->OnGestureEvent(&tap);
  1948. ASSERT_EQ(1, delegate_->dismiss_count());
  1949. Show();
  1950. // Tap on the same empty region, the AppList should close again.
  1951. ui::MouseEvent mouse_click(ui::ET_MOUSE_PRESSED, empty_region, empty_region,
  1952. base::TimeTicks(), 0, 0);
  1953. auto mouse_click_dispatcher_api = std::make_unique<ui::Event::DispatcherApi>(
  1954. static_cast<ui::Event*>(&mouse_click));
  1955. mouse_click_dispatcher_api->set_target(view_);
  1956. view_->OnMouseEvent(&mouse_click);
  1957. ui::MouseEvent mouse_release(ui::ET_MOUSE_RELEASED, empty_region,
  1958. empty_region, base::TimeTicks(), 0, 0);
  1959. mouse_click_dispatcher_api =
  1960. std::make_unique<ui::Event::DispatcherApi>(&mouse_release);
  1961. mouse_click_dispatcher_api->set_target(view_);
  1962. view_->OnMouseEvent(&mouse_release);
  1963. ASSERT_EQ(2, delegate_->dismiss_count());
  1964. }
  1965. // Tests that search box should not become a rectangle during drag.
  1966. TEST_F(AppListViewPeekingTest, SearchBoxCornerRadiusDuringDragging) {
  1967. base::HistogramTester histogram_tester;
  1968. Initialize(false /*is_tablet_mode*/);
  1969. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  1970. Show();
  1971. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  1972. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  1973. histogram_tester.ExpectTotalCount(
  1974. "Apps.StateTransition.Drag.PresentationTime.ClamshellMode", 0);
  1975. // Send SCROLL_START and SCROLL_UPDATE events, simulating dragging the
  1976. // launcher.
  1977. base::TimeTicks timestamp = base::TimeTicks::Now();
  1978. gfx::Point start = view_->GetWidget()->GetWindowBoundsInScreen().top_right();
  1979. int delta_y = 1;
  1980. ui::GestureEvent start_event = ui::GestureEvent(
  1981. start.x(), start.y(), ui::EF_NONE, timestamp,
  1982. ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN, 0, delta_y));
  1983. view_->OnGestureEvent(&start_event);
  1984. histogram_tester.ExpectTotalCount(
  1985. "Apps.StateTransition.Drag.PresentationTime.ClamshellMode", 0);
  1986. // Drag down the launcher.
  1987. timestamp += base::Milliseconds(25);
  1988. delta_y += 10;
  1989. start.Offset(0, 1);
  1990. ui::GestureEvent update_event = ui::GestureEvent(
  1991. start.x(), start.y(), ui::EF_NONE, timestamp,
  1992. ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_UPDATE, 0, delta_y));
  1993. view_->OnGestureEvent(&update_event);
  1994. EXPECT_TRUE(IsStateShown(ash::AppListState::kStateApps));
  1995. EXPECT_EQ(kSearchBoxBorderCornerRadius,
  1996. search_box_view()->GetSearchBoxBorderCornerRadiusForState(
  1997. ash::AppListState::kStateApps));
  1998. // Search box should keep |kSearchBoxCornerRadiusFullscreen| corner radius
  1999. // during drag.
  2000. EXPECT_TRUE(SetAppListState(ash::AppListState::kStateSearchResults));
  2001. EXPECT_TRUE(view_->is_in_drag());
  2002. EXPECT_EQ(kSearchBoxBorderCornerRadius,
  2003. search_box_view()->GetSearchBoxBorderCornerRadiusForState(
  2004. ash::AppListState::kStateSearchResults));
  2005. histogram_tester.ExpectTotalCount(
  2006. "Apps.StateTransition.Drag.PresentationTime.ClamshellMode", 1);
  2007. histogram_tester.ExpectTotalCount(
  2008. "Apps.StateTransition.Drag.PresentationTime.MaxLatency.ClamshellMode", 0);
  2009. // Ends to drag the launcher.
  2010. EXPECT_TRUE(SetAppListState(ash::AppListState::kStateApps));
  2011. timestamp += base::Milliseconds(25);
  2012. start.Offset(0, 1);
  2013. ui::GestureEvent end_event =
  2014. ui::GestureEvent(start.x(), start.y() + delta_y, ui::EF_NONE, timestamp,
  2015. ui::GestureEventDetails(ui::ET_GESTURE_END));
  2016. view_->OnGestureEvent(&end_event);
  2017. // Search box should keep |kSearchBoxCornerRadiusFullscreen| corner radius
  2018. // if launcher drag finished.
  2019. EXPECT_FALSE(view_->is_in_drag());
  2020. EXPECT_EQ(kSearchBoxBorderCornerRadius,
  2021. search_box_view()->GetSearchBoxBorderCornerRadiusForState(
  2022. ash::AppListState::kStateApps));
  2023. histogram_tester.ExpectTotalCount(
  2024. "Apps.StateTransition.Drag.PresentationTime.ClamshellMode", 1);
  2025. histogram_tester.ExpectTotalCount(
  2026. "Apps.StateTransition.Drag.PresentationTime.MaxLatency.ClamshellMode", 1);
  2027. }
  2028. // Tests displaying the app list and performs a standard set of checks on its
  2029. // top level views.
  2030. TEST_F(AppListViewPeekingTest, DisplayTest) {
  2031. Initialize(/*is_tablet_mode=*/false);
  2032. EXPECT_EQ(-1, GetPaginationModel()->total_pages());
  2033. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  2034. Show();
  2035. // |view_| bounds equal to the root window's size.
  2036. EXPECT_EQ("800x600", view_->bounds().size().ToString());
  2037. EXPECT_EQ(2, GetPaginationModel()->total_pages());
  2038. EXPECT_EQ(0, GetPaginationModel()->selected_page());
  2039. // Checks on the main view.
  2040. AppListMainView* main_view = view_->app_list_main_view();
  2041. EXPECT_NO_FATAL_FAILURE(CheckView(main_view));
  2042. EXPECT_NO_FATAL_FAILURE(CheckView(main_view->contents_view()));
  2043. ash::AppListState expected = ash::AppListState::kStateApps;
  2044. EXPECT_TRUE(main_view->contents_view()->IsStateActive(expected));
  2045. EXPECT_EQ(expected, delegate_->GetCurrentAppListPage());
  2046. }
  2047. // As above above, but tests tablet mode with and without ProductivityLauncher.
  2048. TEST_P(AppListViewTabletTest, DisplayTest) {
  2049. Initialize(/*is_tablet_mode=*/true);
  2050. EXPECT_EQ(-1, GetPaginationModel()->total_pages());
  2051. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  2052. Show();
  2053. // |view_| bounds equal to the root window's size.
  2054. EXPECT_EQ("800x600", view_->bounds().size().ToString());
  2055. EXPECT_EQ(2, GetPaginationModel()->total_pages());
  2056. EXPECT_EQ(0, GetPaginationModel()->selected_page());
  2057. // Checks on the main view.
  2058. AppListMainView* main_view = view_->app_list_main_view();
  2059. EXPECT_NO_FATAL_FAILURE(CheckView(main_view));
  2060. EXPECT_NO_FATAL_FAILURE(CheckView(main_view->contents_view()));
  2061. ash::AppListState expected = ash::AppListState::kStateApps;
  2062. EXPECT_TRUE(main_view->contents_view()->IsStateActive(expected));
  2063. EXPECT_EQ(expected, delegate_->GetCurrentAppListPage());
  2064. }
  2065. // Tests switching rapidly between multiple pages of the launcher.
  2066. // ProductivityLauncher has tests of animated page transitions in the tests for
  2067. // AppListBubbleView and AppListBubbleAppsPage.
  2068. TEST_F(AppListViewPeekingTest, PageSwitchingAnimationTest) {
  2069. ui::ScopedAnimationDurationScaleMode non_zero_duration(
  2070. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
  2071. Initialize(/*is_tablet_mode=*/false);
  2072. Show();
  2073. AppListMainView* main_view = view_->app_list_main_view();
  2074. // Checks on the main view.
  2075. EXPECT_NO_FATAL_FAILURE(CheckView(main_view));
  2076. EXPECT_NO_FATAL_FAILURE(CheckView(main_view->contents_view()));
  2077. ContentsView* contents_view = main_view->contents_view();
  2078. contents_view->SetActiveState(ash::AppListState::kStateApps);
  2079. contents_view->Layout();
  2080. EXPECT_TRUE(IsStateShown(ash::AppListState::kStateApps));
  2081. // Change pages. Animation start triggers layout, and updates the page UI.
  2082. contents_view->ShowSearchResults(true);
  2083. EXPECT_TRUE(IsStateShown(ash::AppListState::kStateSearchResults));
  2084. // Change back to the apps container.
  2085. contents_view->SetActiveState(ash::AppListState::kStateApps);
  2086. EXPECT_TRUE(IsStateShown(ash::AppListState::kStateApps));
  2087. // Verify that search results are shown when going back to search results
  2088. // page.
  2089. contents_view->ShowSearchResults(true);
  2090. EXPECT_TRUE(IsStateShown(ash::AppListState::kStateSearchResults));
  2091. }
  2092. // Tests that the correct views are displayed for showing search results.
  2093. TEST_F(AppListViewTest, DISABLED_SearchResultsTest) {
  2094. Initialize(false /*is_tablet_mode*/);
  2095. // TODO(newcomer): this test needs to be reevaluated for the fullscreen app
  2096. // list (http://crbug.com/759779).
  2097. EXPECT_FALSE(view_->GetWidget()->IsVisible());
  2098. EXPECT_EQ(-1, GetPaginationModel()->total_pages());
  2099. AppListTestModel* model = delegate_->GetTestModel();
  2100. model->PopulateApps(3);
  2101. Show();
  2102. AppListMainView* main_view = view_->app_list_main_view();
  2103. ContentsView* contents_view = main_view->contents_view();
  2104. EXPECT_TRUE(SetAppListState(ash::AppListState::kStateApps));
  2105. // Show the search results.
  2106. contents_view->ShowSearchResults(true);
  2107. contents_view->Layout();
  2108. EXPECT_TRUE(
  2109. contents_view->IsStateActive(ash::AppListState::kStateSearchResults));
  2110. EXPECT_TRUE(IsStateShown(ash::AppListState::kStateSearchResults));
  2111. // Hide the search results.
  2112. contents_view->ShowSearchResults(false);
  2113. contents_view->Layout();
  2114. // Check that we return to the page that we were on before the search.
  2115. EXPECT_TRUE(IsStateShown(ash::AppListState::kStateApps));
  2116. view_->Layout();
  2117. EXPECT_TRUE(IsStateShown(ash::AppListState::kStateApps));
  2118. std::u16string search_text = u"test";
  2119. main_view->search_box_view()->search_box()->SetText(std::u16string());
  2120. main_view->search_box_view()->search_box()->InsertText(
  2121. search_text,
  2122. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  2123. // Check that the current search is using |search_text|.
  2124. EXPECT_EQ(search_text, main_view->search_box_view()->search_box()->GetText());
  2125. EXPECT_EQ(search_text, main_view->search_box_view()->current_query());
  2126. contents_view->Layout();
  2127. EXPECT_TRUE(
  2128. contents_view->IsStateActive(ash::AppListState::kStateSearchResults));
  2129. EXPECT_TRUE(CheckSearchBoxWidget(contents_view->GetSearchBoxBounds(
  2130. ash::AppListState::kStateSearchResults)));
  2131. // Check that typing into the search box triggers the search page.
  2132. EXPECT_TRUE(SetAppListState(ash::AppListState::kStateApps));
  2133. contents_view->Layout();
  2134. EXPECT_TRUE(IsStateShown(ash::AppListState::kStateApps));
  2135. EXPECT_TRUE(CheckSearchBoxWidget(
  2136. contents_view->GetSearchBoxBounds(ash::AppListState::kStateApps)));
  2137. std::u16string new_search_text = u"apple";
  2138. main_view->search_box_view()->search_box()->SetText(std::u16string());
  2139. main_view->search_box_view()->search_box()->InsertText(
  2140. new_search_text,
  2141. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  2142. // Check that the current search is using |new_search_text|.
  2143. EXPECT_EQ(new_search_text,
  2144. main_view->search_box_view()->search_box()->GetText());
  2145. EXPECT_EQ(search_text, main_view->search_box_view()->current_query());
  2146. contents_view->Layout();
  2147. EXPECT_TRUE(IsStateShown(ash::AppListState::kStateSearchResults));
  2148. EXPECT_TRUE(CheckSearchBoxWidget(contents_view->GetSearchBoxBounds(
  2149. ash::AppListState::kStateSearchResults)));
  2150. }
  2151. // Tests that a context menu can be shown between app icons in tablet mode.
  2152. TEST_P(AppListViewTabletTest, ShowContextMenuBetweenAppsInTabletMode) {
  2153. Initialize(true /*is_tablet_mode*/);
  2154. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  2155. Show();
  2156. // Tap between two apps in tablet mode.
  2157. const gfx::Point middle = GetPointBetweenTwoApps();
  2158. ui::GestureEvent tap(middle.x(), middle.y(), 0, base::TimeTicks(),
  2159. ui::GestureEventDetails(ui::ET_GESTURE_TWO_FINGER_TAP));
  2160. view_->OnGestureEvent(&tap);
  2161. // The wallpaper context menu should show.
  2162. EXPECT_EQ(1, show_wallpaper_context_menu_count());
  2163. EXPECT_TRUE(view_->GetWidget()->IsVisible());
  2164. // Click between two apps in tablet mode.
  2165. ui::MouseEvent click_mouse_event(
  2166. ui::ET_MOUSE_PRESSED, middle, middle, ui::EventTimeForNow(),
  2167. ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON);
  2168. view_->OnMouseEvent(&click_mouse_event);
  2169. ui::MouseEvent release_mouse_event(
  2170. ui::ET_MOUSE_RELEASED, middle, middle, ui::EventTimeForNow(),
  2171. ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON);
  2172. view_->OnMouseEvent(&release_mouse_event);
  2173. // The wallpaper context menu should show.
  2174. EXPECT_EQ(2, show_wallpaper_context_menu_count());
  2175. EXPECT_TRUE(view_->GetWidget()->IsVisible());
  2176. }
  2177. // Tests that context menus are not shown between app icons in clamshell mode.
  2178. TEST_F(AppListViewPeekingTest, DontShowContextMenuBetweenAppsInClamshellMode) {
  2179. Initialize(false /* disable tablet mode */);
  2180. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  2181. Show();
  2182. // Tap between two apps in clamshell mode.
  2183. const gfx::Point middle = GetPointBetweenTwoApps();
  2184. ui::GestureEvent tap(middle.x(), middle.y(), 0, base::TimeTicks(),
  2185. ui::GestureEventDetails(ui::ET_GESTURE_TWO_FINGER_TAP));
  2186. view_->OnGestureEvent(&tap);
  2187. // The wallpaper menu should not show.
  2188. EXPECT_EQ(0, show_wallpaper_context_menu_count());
  2189. EXPECT_TRUE(view_->GetWidget()->IsVisible());
  2190. // Right click between two apps in clamshell mode.
  2191. ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, middle, middle,
  2192. ui::EventTimeForNow(), ui::EF_RIGHT_MOUSE_BUTTON,
  2193. ui::EF_RIGHT_MOUSE_BUTTON);
  2194. view_->OnMouseEvent(&mouse_event);
  2195. // The wallpaper menu should not show.
  2196. EXPECT_EQ(0, show_wallpaper_context_menu_count());
  2197. EXPECT_TRUE(view_->GetWidget()->IsVisible());
  2198. }
  2199. // Tests the back action in home launcher.
  2200. TEST_F(AppListViewTest, BackAction) {
  2201. // Put into fullscreen using tablet mode.
  2202. Initialize(true /*is_tablet_mode*/);
  2203. // Populate apps to fill up the first page and add a folder in the second
  2204. // page.
  2205. AppListTestModel* model = delegate_->GetTestModel();
  2206. const int kAppListItemNum =
  2207. SharedAppListConfig::instance().GetMaxNumOfItemsPerPage();
  2208. const int kItemNumInFolder = 5;
  2209. model->PopulateApps(kAppListItemNum);
  2210. model->CreateAndPopulateFolderWithApps(kItemNumInFolder);
  2211. // Show the app list
  2212. Show();
  2213. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  2214. EXPECT_EQ(2, apps_grid_view()->pagination_model()->total_pages());
  2215. // Select the second page and open the folder.
  2216. apps_grid_view()->pagination_model()->SelectPage(1, false);
  2217. test_api_->PressItemAt(kAppListItemNum);
  2218. EXPECT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  2219. EXPECT_EQ(1, apps_grid_view()->pagination_model()->selected_page());
  2220. // Back action will first close the folder.
  2221. contents_view()->Back();
  2222. EXPECT_FALSE(contents_view()->apps_container_view()->IsInFolderView());
  2223. EXPECT_EQ(1, apps_grid_view()->pagination_model()->selected_page());
  2224. // Back action will then select the first page.
  2225. contents_view()->Back();
  2226. EXPECT_FALSE(contents_view()->apps_container_view()->IsInFolderView());
  2227. EXPECT_EQ(0, apps_grid_view()->pagination_model()->selected_page());
  2228. // Select the second page and open search results page.
  2229. apps_grid_view()->pagination_model()->SelectPage(1, false);
  2230. search_box_view()->search_box()->InsertText(
  2231. u"A",
  2232. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  2233. EXPECT_EQ(ash::AppListViewState::kFullscreenSearch, view_->app_list_state());
  2234. EXPECT_EQ(1, apps_grid_view()->pagination_model()->selected_page());
  2235. // Back action will first close the search results page.
  2236. contents_view()->Back();
  2237. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  2238. EXPECT_EQ(1, apps_grid_view()->pagination_model()->selected_page());
  2239. // Back action will then select the first page.
  2240. contents_view()->Back();
  2241. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  2242. EXPECT_EQ(0, apps_grid_view()->pagination_model()->selected_page());
  2243. }
  2244. TEST_F(AppListViewTest, CloseFolderByClickingBackground) {
  2245. Initialize(/*is_tablet_mode=*/false);
  2246. AppListTestModel* model = delegate_->GetTestModel();
  2247. model->CreateAndPopulateFolderWithApps(3);
  2248. EXPECT_EQ(1u, model->top_level_item_list()->item_count());
  2249. EXPECT_EQ(AppListFolderItem::kItemType,
  2250. model->top_level_item_list()->item_at(0)->GetItemType());
  2251. // Open the folder.
  2252. Show();
  2253. test_api_->PressItemAt(0);
  2254. AppsContainerView* apps_container_view =
  2255. contents_view()->apps_container_view();
  2256. EXPECT_TRUE(apps_container_view->IsInFolderView());
  2257. // Simulate mouse press on folder background to close the folder.
  2258. ui::MouseEvent event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
  2259. ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
  2260. ui::EF_LEFT_MOUSE_BUTTON);
  2261. apps_container_view->folder_background_view()->OnMouseEvent(&event);
  2262. EXPECT_FALSE(apps_container_view->IsInFolderView());
  2263. }
  2264. // Tests that, in clamshell mode, the current app list page resets to the
  2265. // initial page when app list is closed and re-opened.
  2266. TEST_F(AppListViewTest, InitialPageResetClamshellModeTest) {
  2267. Initialize(false /*is_tablet_mode*/);
  2268. AppListTestModel* model = delegate_->GetTestModel();
  2269. const int kAppListItemNum =
  2270. SharedAppListConfig::instance().GetMaxNumOfItemsPerPage() + 1;
  2271. model->PopulateApps(kAppListItemNum);
  2272. Show();
  2273. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  2274. apps_grid_view()->pagination_model()->SelectPage(1, false /* animate */);
  2275. // Close and re-open the app list to ensure the current page doesn't persist.
  2276. view_->SetState(ash::AppListViewState::kClosed);
  2277. Show();
  2278. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  2279. EXPECT_EQ(0, apps_grid_view()->pagination_model()->selected_page());
  2280. }
  2281. // Tests that, in tablet mode, the current app list page doesn't immediately
  2282. // reset to the initial page when app list is closed and re-opened.
  2283. TEST_P(AppListViewTabletTest, PagePersistanceTabletModeTest) {
  2284. Initialize(true /*is_tablet_mode*/);
  2285. AppListTestModel* model = delegate_->GetTestModel();
  2286. const int kAppListItemNum =
  2287. SharedAppListConfig::instance().GetMaxNumOfItemsPerPage() + 1;
  2288. model->PopulateApps(kAppListItemNum);
  2289. Show();
  2290. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  2291. apps_grid_view()->pagination_model()->SelectPage(1, false /* animate */);
  2292. // Close and re-open the app list to ensure the current page persists.
  2293. view_->SetState(ash::AppListViewState::kClosed);
  2294. Show();
  2295. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  2296. // The current page should not be reset for the tablet mode app list.
  2297. EXPECT_EQ(1, apps_grid_view()->pagination_model()->selected_page());
  2298. }
  2299. // Tests selecting search result to show embedded Assistant UI.
  2300. // TODO(https://crbug.com/1280300): Figure out if ProductivityLauncher needs a
  2301. // version of this test. ProductivityLauncherSearchView has its own test suite.
  2302. TEST_F(AppListViewPeekingFocusTest, ShowEmbeddedAssistantUI) {
  2303. Show();
  2304. // Initially the search box is inactive, hitting Enter to activate it.
  2305. EXPECT_FALSE(search_box_view()->is_search_box_active());
  2306. SimulateKeyPress(ui::VKEY_RETURN, false);
  2307. EXPECT_TRUE(search_box_view()->is_search_box_active());
  2308. // Type something in search box to transition to HALF state and populate
  2309. // fake list results. Then hit Enter key.
  2310. search_box_view()->search_box()->InsertText(
  2311. u"test",
  2312. ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
  2313. const int kListResults = 2;
  2314. const int kIndexOpenAssistantUi = 1;
  2315. SetUpSearchResultsForAssistantUI(kListResults, kIndexOpenAssistantUi);
  2316. SimulateKeyPress(ui::VKEY_RETURN, false);
  2317. EXPECT_EQ(1, GetOpenFirstSearchResultCount());
  2318. EXPECT_EQ(1, GetTotalOpenSearchResultCount());
  2319. EXPECT_EQ(0, GetTotalOpenAssistantUICount());
  2320. SearchResultListView* list_view = GetSearchResultListView();
  2321. ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE);
  2322. list_view->GetResultViewAt(kIndexOpenAssistantUi)->OnKeyEvent(&key_event);
  2323. EXPECT_EQ(1, GetOpenFirstSearchResultCount());
  2324. EXPECT_EQ(2, GetTotalOpenSearchResultCount());
  2325. EXPECT_EQ(1, GetTotalOpenAssistantUICount());
  2326. }
  2327. // Tests that the correct contents is visible in the contents_view upon
  2328. // reshowing. See b/142069648 for the details.
  2329. TEST_F(AppListViewPeekingTest, AppsGridVisibilityOnResetForShow) {
  2330. Initialize(true /*is_tablet_mode*/);
  2331. Show();
  2332. contents_view()->ShowEmbeddedAssistantUI(true);
  2333. EXPECT_FALSE(contents_view()->apps_container_view()->GetVisible());
  2334. EXPECT_FALSE(contents_view()->search_result_page_view()->GetVisible());
  2335. EXPECT_TRUE(assistant_page_view()->GetVisible());
  2336. view_->OnTabletModeChanged(false);
  2337. Show();
  2338. EXPECT_TRUE(contents_view()->apps_container_view()->GetVisible());
  2339. EXPECT_FALSE(contents_view()->search_result_page_view()->GetVisible());
  2340. EXPECT_FALSE(assistant_page_view()->GetVisible());
  2341. }
  2342. // Tests that pressing escape in embedded Assistant UI returns to fullscreen
  2343. // if the Assistant UI was launched from fullscreen app list.
  2344. TEST_F(AppListViewTest, EscapeKeyInEmbeddedAssistantUIReturnsToAppList) {
  2345. Initialize(false /*is_tablet_mode*/);
  2346. Show();
  2347. // First we're in the fullscreen app list
  2348. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  2349. // Then we go to search by entering text
  2350. SetTextInSearchBox(u"search query");
  2351. // From there we launch the Assistant UI
  2352. contents_view()->ShowEmbeddedAssistantUI(true);
  2353. // We press escape to leave the Assistant UI
  2354. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  2355. // And we should be back in the fullscreen app list
  2356. EXPECT_FALSE(contents_view()->IsShowingSearchResults());
  2357. EXPECT_EQ(ash::AppListViewState::kFullscreenAllApps, view_->app_list_state());
  2358. }
  2359. // Tests that pressing escape in embedded Assistant UI returns to peeking
  2360. // if the Assistant UI was launched from half screen.
  2361. TEST_F(AppListViewPeekingTest, EscapeKeyInEmbeddedAssistantUIReturnsToPeeking) {
  2362. Initialize(false /*is_tablet_mode*/);
  2363. Show();
  2364. // Enter half screen search by entering text
  2365. SetTextInSearchBox(u"search query");
  2366. // From there we launch the Assistant UI
  2367. contents_view()->ShowEmbeddedAssistantUI(true);
  2368. // We press escape to leave the Assistant UI
  2369. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  2370. // And we should be back in the peeking state
  2371. EXPECT_FALSE(contents_view()->IsShowingSearchResults());
  2372. EXPECT_EQ(ash::AppListViewState::kPeeking, view_->app_list_state());
  2373. }
  2374. // Tests that clicking empty region in AppListview when showing Assistant UI
  2375. // should go back to peeking state.
  2376. TEST_F(AppListViewPeekingTest, ClickOutsideEmbeddedAssistantUIToPeeking) {
  2377. Initialize(false /*is_tablet_mode*/);
  2378. Show();
  2379. // Set search_box_view active.
  2380. ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE);
  2381. view_->GetWidget()->OnKeyEvent(&key_event);
  2382. contents_view()->ShowEmbeddedAssistantUI(true);
  2383. EXPECT_TRUE(contents_view()->IsShowingEmbeddedAssistantUI());
  2384. // Click on the same empty region, the AppList should be peeking state.
  2385. const gfx::Point empty_region = view_->GetBoundsInScreen().origin();
  2386. ui::MouseEvent mouse_click(ui::ET_MOUSE_PRESSED, empty_region, empty_region,
  2387. base::TimeTicks(), 0, 0);
  2388. auto mouse_click_dispatcher_api =
  2389. std::make_unique<ui::Event::DispatcherApi>(&mouse_click);
  2390. mouse_click_dispatcher_api->set_target(view_);
  2391. view_->OnMouseEvent(&mouse_click);
  2392. ui::MouseEvent mouse_release(ui::ET_MOUSE_RELEASED, empty_region,
  2393. empty_region, base::TimeTicks(), 0, 0);
  2394. mouse_click_dispatcher_api =
  2395. std::make_unique<ui::Event::DispatcherApi>(&mouse_release);
  2396. mouse_click_dispatcher_api->set_target(view_);
  2397. view_->OnMouseEvent(&mouse_release);
  2398. EXPECT_EQ(ash::AppListViewState::kPeeking, view_->app_list_state());
  2399. }
  2400. // Tests that expand arrow is not visible when showing embedded Assistant UI.
  2401. // ProductivityLauncher does not have an expand arrow.
  2402. TEST_F(AppListViewPeekingTest, ExpandArrowNotVisibleInEmbeddedAssistantUI) {
  2403. Initialize(false /*is_tablet_mode*/);
  2404. Show();
  2405. // Set search_box_view active.
  2406. ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE);
  2407. view_->GetWidget()->OnKeyEvent(&key_event);
  2408. contents_view()->ShowEmbeddedAssistantUI(true);
  2409. EXPECT_TRUE(contents_view()->IsShowingEmbeddedAssistantUI());
  2410. EXPECT_EQ(0.0f,
  2411. contents_view()->expand_arrow_view()->layer()->GetTargetOpacity());
  2412. }
  2413. // Tests the expand arrow view opacity updates correctly when transitioning
  2414. // between various app list view states. ProductivityLauncher does not have an
  2415. // expand arrow.
  2416. TEST_F(AppListViewPeekingTest, ExpandArrowViewVisibilityTest) {
  2417. Initialize(false /*is_tablet_mode*/);
  2418. Show();
  2419. view_->SetState(ash::AppListViewState::kClosed);
  2420. // Expand arrow should not be visible when app list view state is closed.
  2421. ASSERT_EQ(contents_view()->expand_arrow_view()->layer()->opacity(), 0.0f);
  2422. // Expand arrow view should be visible for peeking launcher.
  2423. view_->SetState(ash::AppListViewState::kPeeking);
  2424. ASSERT_EQ(contents_view()->expand_arrow_view()->layer()->opacity(), 1.0f);
  2425. // Expand arrow view should not be visible for half launcher when showing
  2426. // embedded assistant.
  2427. contents_view()->ShowEmbeddedAssistantUI(true);
  2428. ASSERT_EQ(contents_view()->expand_arrow_view()->layer()->opacity(), 0.0f);
  2429. // Expand arrow should become visible when hiding the assistant view.
  2430. contents_view()->ShowEmbeddedAssistantUI(false);
  2431. EXPECT_TRUE(contents_view()->expand_arrow_view()->GetVisible());
  2432. ASSERT_EQ(contents_view()->expand_arrow_view()->layer()->opacity(), 1.0f);
  2433. // Typing text in the search box should hide the expand arrow view.
  2434. SetTextInSearchBox(u"https://youtu.be/dQw4w9WgXcQ");
  2435. ASSERT_EQ(contents_view()->expand_arrow_view()->layer()->opacity(), 0.0f);
  2436. // Pressing escape should show the expand arrow view again.
  2437. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  2438. ASSERT_EQ(contents_view()->expand_arrow_view()->layer()->opacity(), 1.0f);
  2439. }
  2440. // Tests the expand arrow view opacity updates correctly when transitioning
  2441. // between various app list view states with app list state animations enabled.
  2442. // ProductivityLauncher does not have an expand arrow.
  2443. TEST_F(AppListViewPeekingTest,
  2444. ExpandArrowViewVisibilityWithStateAnimationsTest) {
  2445. Initialize(false /*is_tablet_mode*/);
  2446. Show();
  2447. ui::ScopedAnimationDurationScaleMode non_zero_duration_mode(
  2448. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
  2449. // Expand arrow view should be visible for peeking launcher.
  2450. EXPECT_EQ(1.0f,
  2451. contents_view()->expand_arrow_view()->layer()->GetTargetOpacity());
  2452. // Expand arrow view should not be visible for half launcher when showing
  2453. // embedded assistant.
  2454. contents_view()->ShowEmbeddedAssistantUI(true);
  2455. EXPECT_EQ(0.0f,
  2456. contents_view()->expand_arrow_view()->layer()->GetTargetOpacity());
  2457. // Expand arrow should become visible when hiding the assistant view.
  2458. contents_view()->ShowEmbeddedAssistantUI(false);
  2459. EXPECT_TRUE(contents_view()->expand_arrow_view()->GetVisible());
  2460. EXPECT_EQ(1.0f,
  2461. contents_view()->expand_arrow_view()->layer()->GetTargetOpacity());
  2462. // Typing text in the search box should hide the expand arrow view.
  2463. SetTextInSearchBox(u"https://youtu.be/dQw4w9WgXcQ");
  2464. EXPECT_EQ(0.0f,
  2465. contents_view()->expand_arrow_view()->layer()->GetTargetOpacity());
  2466. // Pressing escape should show the expand arrow view again.
  2467. view_->AcceleratorPressed(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
  2468. EXPECT_EQ(1.0f,
  2469. contents_view()->expand_arrow_view()->layer()->GetTargetOpacity());
  2470. }
  2471. // Tests that search box is not visible when showing embedded Assistant UI.
  2472. // ProductivityLauncher has tests for this in AppListBubbleViewTest.
  2473. TEST_F(AppListViewPeekingTest, SearchBoxViewNotVisibleInEmbeddedAssistantUI) {
  2474. Initialize(false /*is_tablet_mode*/);
  2475. Show();
  2476. EXPECT_TRUE(search_box_view()->GetWidget()->IsVisible());
  2477. contents_view()->ShowEmbeddedAssistantUI(true);
  2478. EXPECT_TRUE(contents_view()->IsShowingEmbeddedAssistantUI());
  2479. EXPECT_FALSE(search_box_view()->GetWidget()->IsVisible());
  2480. }
  2481. // Tests that the expand arrow cannot be seen when opening the app list with
  2482. // side shelf enabled. ProductivityLauncher does not have an expand arrow.
  2483. TEST_F(AppListViewPeekingTest, ExpandArrowNotVisibleWithSideShelf) {
  2484. Initialize(false /*is_tablet_mode*/);
  2485. Show(true /*is_side_shelf*/);
  2486. EXPECT_EQ(0.0f, contents_view()->expand_arrow_view()->layer()->opacity());
  2487. }
  2488. TEST_F(ProductivityLauncherAppListViewLayoutTest, RegularLandscapeScreen) {
  2489. const gfx::Size window_size = gfx::Size(1000, 800);
  2490. GetContext()->SetBounds(gfx::Rect(window_size));
  2491. InitializeAppList();
  2492. const int expected_horizontal_margin =
  2493. kMinProductivityLauncherGridHorizontalMargin;
  2494. const gfx::Size expected_item_size(96, 120);
  2495. {
  2496. SCOPED_TRACE("Only apps grid");
  2497. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2498. VerifyAppsContainerLayoutForProductivityLauncher(
  2499. window_size, /*row_count=*/4, expected_horizontal_margin,
  2500. expected_item_size, /*has_recent_apps=*/false);
  2501. }
  2502. AddRecentApps(4);
  2503. contents_view()->ResetForShow();
  2504. {
  2505. SCOPED_TRACE("With recent apps");
  2506. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2507. VerifyAppsContainerLayoutForProductivityLauncher(
  2508. window_size, 3 /*row_count*/, expected_horizontal_margin,
  2509. expected_item_size, /*has_recent_apps=*/true);
  2510. }
  2511. }
  2512. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2513. RegularLandscapeScreenAtMinPreferredVerticalMargin) {
  2514. const int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2515. /*row_count=*/4, /*tile_height=*/120, /*tile_margins=*/8,
  2516. /*is_large_height=*/false);
  2517. EXPECT_EQ(689, window_height);
  2518. const gfx::Size window_size = gfx::Size(800, window_height);
  2519. GetContext()->SetBounds(gfx::Rect(window_size));
  2520. InitializeAppList();
  2521. const int expected_horizontal_margin =
  2522. kMinProductivityLauncherGridHorizontalMargin;
  2523. const gfx::Size expected_item_size(96, 120);
  2524. {
  2525. SCOPED_TRACE("Only apps grid");
  2526. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2527. VerifyAppsContainerLayoutForProductivityLauncher(
  2528. window_size, /*row_count=*/4, expected_horizontal_margin,
  2529. expected_item_size, /*has_recent_apps=*/false);
  2530. }
  2531. AddRecentApps(4);
  2532. contents_view()->ResetForShow();
  2533. {
  2534. SCOPED_TRACE("With recent apps");
  2535. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2536. VerifyAppsContainerLayoutForProductivityLauncher(
  2537. window_size, 2 /*row_count*/, expected_horizontal_margin,
  2538. expected_item_size, /*has_recent_apps=*/true);
  2539. }
  2540. }
  2541. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2542. RegularLandscapeScreenWithRemovedRows) {
  2543. const int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2544. /*row_count=*/4, /*tile_height=*/120,
  2545. /*tile_margins=*/8, /*is_large_height=*/false) -
  2546. 4;
  2547. EXPECT_EQ(685, window_height);
  2548. const gfx::Size window_size = gfx::Size(800, window_height);
  2549. GetContext()->SetBounds(gfx::Rect(window_size));
  2550. InitializeAppList();
  2551. const int expected_horizontal_margin =
  2552. kMinProductivityLauncherGridHorizontalMargin;
  2553. const gfx::Size expected_item_size(96, 120);
  2554. {
  2555. SCOPED_TRACE("Only apps grid");
  2556. EXPECT_EQ(3, apps_grid_view()->GetRowsForTesting());
  2557. VerifyAppsContainerLayoutForProductivityLauncher(
  2558. window_size, 3 /*row_count*/, expected_horizontal_margin,
  2559. expected_item_size, /*has_recent_apps=*/false);
  2560. }
  2561. AddRecentApps(4);
  2562. contents_view()->ResetForShow();
  2563. {
  2564. SCOPED_TRACE("With recent apps");
  2565. EXPECT_EQ(3, apps_grid_view()->GetRowsForTesting());
  2566. VerifyAppsContainerLayoutForProductivityLauncher(
  2567. window_size, 2 /*row_count*/, expected_horizontal_margin,
  2568. expected_item_size, /*has_recent_apps=*/true);
  2569. }
  2570. }
  2571. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2572. RegularLandscapeScreenAtMaxPreferredVerticalMargin) {
  2573. const int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2574. /*row_count=*/4, /*tile_height=*/120, /*tile_margins=*/96,
  2575. /*is_large_height=*/true);
  2576. EXPECT_EQ(1024, window_height);
  2577. const gfx::Size window_size = gfx::Size(1100, window_height);
  2578. GetContext()->SetBounds(gfx::Rect(window_size));
  2579. InitializeAppList();
  2580. const int expected_horizontal_margin =
  2581. kMinProductivityLauncherGridHorizontalMargin;
  2582. const gfx::Size expected_item_size(96, 120);
  2583. {
  2584. SCOPED_TRACE("Only apps grid");
  2585. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2586. VerifyAppsContainerLayoutForProductivityLauncher(
  2587. window_size, /*row_count=*/4, expected_horizontal_margin,
  2588. expected_item_size, /*has_recent_apps=*/false);
  2589. }
  2590. AddRecentApps(4);
  2591. contents_view()->ResetForShow();
  2592. {
  2593. SCOPED_TRACE("With recent apps");
  2594. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2595. VerifyAppsContainerLayoutForProductivityLauncher(
  2596. window_size, 4 /*row_count*/, expected_horizontal_margin,
  2597. expected_item_size, /*has_recent_apps=*/true);
  2598. }
  2599. }
  2600. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2601. RegularLandscapeScreenWithAddedRows) {
  2602. const int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2603. /*row_count=*/4, /*tile_height=*/120,
  2604. /*tile_margins=*/96, /*is_large_height=*/true) +
  2605. 6;
  2606. EXPECT_EQ(1030, window_height);
  2607. const gfx::Size window_size = gfx::Size(1100, window_height);
  2608. GetContext()->SetBounds(gfx::Rect(window_size));
  2609. InitializeAppList();
  2610. const int expected_horizontal_margin =
  2611. kMinProductivityLauncherGridHorizontalMargin;
  2612. const gfx::Size expected_item_size(96, 120);
  2613. {
  2614. SCOPED_TRACE("Only apps grid");
  2615. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2616. VerifyAppsContainerLayoutForProductivityLauncher(
  2617. window_size, /*row_count=*/5, expected_horizontal_margin,
  2618. expected_item_size, /*has_recent_apps=*/false);
  2619. }
  2620. AddRecentApps(4);
  2621. contents_view()->ResetForShow();
  2622. {
  2623. SCOPED_TRACE("With recent apps");
  2624. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2625. VerifyAppsContainerLayoutForProductivityLauncher(
  2626. window_size, 4 /*row_count*/, expected_horizontal_margin,
  2627. expected_item_size, /*has_recent_apps=*/true);
  2628. }
  2629. }
  2630. TEST_F(ProductivityLauncherAppListViewLayoutTest, RegularPortraitScreen) {
  2631. const gfx::Size window_size = gfx::Size(800, 1000);
  2632. GetContext()->SetBounds(gfx::Rect(window_size));
  2633. InitializeAppList();
  2634. const int expected_horizontal_margin =
  2635. kMinProductivityLauncherGridHorizontalMargin;
  2636. const gfx::Size expected_item_size(96, 120);
  2637. {
  2638. SCOPED_TRACE("Only apps grid");
  2639. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2640. VerifyAppsContainerLayoutForProductivityLauncher(
  2641. window_size, /*row_count=*/5, expected_horizontal_margin,
  2642. expected_item_size, /*has_recent_apps=*/false);
  2643. }
  2644. AddRecentApps(4);
  2645. contents_view()->ResetForShow();
  2646. {
  2647. SCOPED_TRACE("With recent apps");
  2648. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2649. VerifyAppsContainerLayoutForProductivityLauncher(
  2650. window_size, /*row_count=*/4, expected_horizontal_margin,
  2651. expected_item_size, /*has_recent_apps=*/true);
  2652. }
  2653. }
  2654. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2655. RegularPortraitScreenAtMinPreferredVerticalMargin) {
  2656. int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2657. /*row_count=*/5, /*tile_height=*/120, /*tile_margins=*/8,
  2658. /*is_large_height=*/true);
  2659. // window_height = 860;
  2660. EXPECT_EQ(868, window_height);
  2661. const gfx::Size window_size = gfx::Size(700, window_height);
  2662. GetContext()->SetBounds(gfx::Rect(window_size));
  2663. InitializeAppList();
  2664. const int expected_horizontal_margin =
  2665. kMinProductivityLauncherGridHorizontalMargin;
  2666. const gfx::Size expected_item_size(96, 120);
  2667. {
  2668. SCOPED_TRACE("Only apps grid");
  2669. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2670. VerifyAppsContainerLayoutForProductivityLauncher(
  2671. window_size, /*row_count=*/5, expected_horizontal_margin,
  2672. expected_item_size, /*has_recent_apps=*/false);
  2673. }
  2674. AddRecentApps(4);
  2675. contents_view()->ResetForShow();
  2676. {
  2677. SCOPED_TRACE("With recent apps");
  2678. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2679. VerifyAppsContainerLayoutForProductivityLauncher(
  2680. window_size, 3 /*row_count*/, expected_horizontal_margin,
  2681. expected_item_size, /*has_recent_apps=*/true);
  2682. }
  2683. }
  2684. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2685. RegularPortraitScreenWithRemovedRows) {
  2686. const int window_height =
  2687. GetExpectedScreenSizeForProductivityLauncher(
  2688. /*row_count=*/5, /*tile_height=*/120, /*tile_margins=*/8,
  2689. /*is_large_height=*/true) -
  2690. 8;
  2691. EXPECT_EQ(860, window_height);
  2692. const gfx::Size window_size = gfx::Size(700, window_height);
  2693. GetContext()->SetBounds(gfx::Rect(window_size));
  2694. InitializeAppList();
  2695. const int expected_horizontal_margin =
  2696. kMinProductivityLauncherGridHorizontalMargin;
  2697. const gfx::Size expected_item_size(96, 120);
  2698. {
  2699. SCOPED_TRACE("Only apps grid");
  2700. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2701. VerifyAppsContainerLayoutForProductivityLauncher(
  2702. window_size, /*row_count=*/4, expected_horizontal_margin,
  2703. expected_item_size, /*has_recent_apps=*/false);
  2704. }
  2705. AddRecentApps(4);
  2706. contents_view()->ResetForShow();
  2707. {
  2708. SCOPED_TRACE("With recent apps");
  2709. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2710. VerifyAppsContainerLayoutForProductivityLauncher(
  2711. window_size, 3 /*row_count*/, expected_horizontal_margin,
  2712. expected_item_size, /*has_recent_apps=*/true);
  2713. }
  2714. }
  2715. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2716. RegularPortraitScreenAtMaxPreferredVerticalMargin) {
  2717. const int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2718. /*row_count=*/5, /*tile_height=*/120, /*tile_margins=*/96,
  2719. /*is_large_height=*/true);
  2720. EXPECT_EQ(1270, window_height);
  2721. const gfx::Size window_size = gfx::Size(1200, window_height);
  2722. GetContext()->SetBounds(gfx::Rect(window_size));
  2723. InitializeAppList();
  2724. const int expected_horizontal_margin = 104;
  2725. const gfx::Size expected_item_size(96, 120);
  2726. {
  2727. SCOPED_TRACE("Only apps grid");
  2728. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2729. VerifyAppsContainerLayoutForProductivityLauncher(
  2730. window_size, /*row_count=*/5, expected_horizontal_margin,
  2731. expected_item_size, /*has_recent_apps=*/false);
  2732. }
  2733. AddRecentApps(4);
  2734. contents_view()->ResetForShow();
  2735. {
  2736. SCOPED_TRACE("With recent apps");
  2737. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2738. VerifyAppsContainerLayoutForProductivityLauncher(
  2739. window_size, /*row_count=*/4, expected_horizontal_margin,
  2740. expected_item_size, /*has_recent_apps=*/true);
  2741. }
  2742. }
  2743. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2744. RegularPortraitScreenWithExtraRows) {
  2745. const int window_height =
  2746. GetExpectedScreenSizeForProductivityLauncher(
  2747. /*row_count=*/5, /*tile_height=*/120, /*tile_margins=*/96,
  2748. /*is_large_height=*/true) +
  2749. 4;
  2750. EXPECT_EQ(1274, window_height);
  2751. const gfx::Size window_size = gfx::Size(1200, window_height);
  2752. GetContext()->SetBounds(gfx::Rect(window_size));
  2753. InitializeAppList();
  2754. const int expected_horizontal_margin = 104;
  2755. const gfx::Size expected_item_size(96, 120);
  2756. {
  2757. SCOPED_TRACE("Only apps grid");
  2758. EXPECT_EQ(6, apps_grid_view()->GetRowsForTesting());
  2759. VerifyAppsContainerLayoutForProductivityLauncher(
  2760. window_size, /*row_count=*/6, expected_horizontal_margin,
  2761. expected_item_size, /*has_recent_apps=*/false);
  2762. }
  2763. AddRecentApps(4);
  2764. contents_view()->ResetForShow();
  2765. {
  2766. SCOPED_TRACE("With recent apps");
  2767. EXPECT_EQ(6, apps_grid_view()->GetRowsForTesting());
  2768. VerifyAppsContainerLayoutForProductivityLauncher(
  2769. window_size, /*row_count=*/4, expected_horizontal_margin,
  2770. expected_item_size, /*has_recent_apps=*/true);
  2771. }
  2772. }
  2773. TEST_F(ProductivityLauncherAppListViewLayoutTest, DenseLandscapeScreen) {
  2774. const gfx::Size window_size = gfx::Size(800, 600);
  2775. GetContext()->SetBounds(gfx::Rect(window_size));
  2776. InitializeAppList();
  2777. const int expected_horizontal_margin =
  2778. kMinProductivityLauncherGridHorizontalMargin;
  2779. const gfx::Size expected_item_size(80, 88);
  2780. {
  2781. SCOPED_TRACE("Only apps grid");
  2782. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2783. VerifyAppsContainerLayoutForProductivityLauncher(
  2784. window_size, /*row_count=*/4, expected_horizontal_margin,
  2785. expected_item_size, /*has_recent_apps=*/false);
  2786. }
  2787. AddRecentApps(4);
  2788. contents_view()->ResetForShow();
  2789. {
  2790. SCOPED_TRACE("With recent apps");
  2791. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2792. VerifyAppsContainerLayoutForProductivityLauncher(
  2793. window_size, 3 /*row_count*/, expected_horizontal_margin,
  2794. expected_item_size, /*has_recent_apps=*/true);
  2795. }
  2796. }
  2797. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2798. DenseLandscapeScreenAtMinPreferredVerticalMargin) {
  2799. const int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2800. /*row_count=*/4, /*tile_height=*/88, /*tile_margins=*/8,
  2801. /*is_large_height=*/false);
  2802. EXPECT_EQ(552, window_height);
  2803. const gfx::Size window_size = gfx::Size(800, window_height);
  2804. GetContext()->SetBounds(gfx::Rect(window_size));
  2805. InitializeAppList();
  2806. const int expected_horizontal_margin =
  2807. kMinProductivityLauncherGridHorizontalMargin;
  2808. const gfx::Size expected_item_size(80, 88);
  2809. {
  2810. SCOPED_TRACE("Only apps grid");
  2811. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2812. VerifyAppsContainerLayoutForProductivityLauncher(
  2813. window_size, /*row_count=*/4, expected_horizontal_margin,
  2814. expected_item_size, /*has_recent_apps=*/false);
  2815. }
  2816. AddRecentApps(4);
  2817. contents_view()->ResetForShow();
  2818. {
  2819. SCOPED_TRACE("With recent apps");
  2820. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2821. VerifyAppsContainerLayoutForProductivityLauncher(
  2822. window_size, 2 /*row_count*/, expected_horizontal_margin,
  2823. expected_item_size, /*has_recent_apps=*/true);
  2824. }
  2825. }
  2826. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2827. DenseLandscapeScreenWithRemovedRows) {
  2828. const int window_height =
  2829. GetExpectedScreenSizeForProductivityLauncher(
  2830. /*row_count=*/4, /*tile_height=*/88, /*tile_margins=*/8,
  2831. /*large_height*/ false) -
  2832. 4;
  2833. EXPECT_EQ(548, window_height);
  2834. const gfx::Size window_size = gfx::Size(800, window_height);
  2835. GetContext()->SetBounds(gfx::Rect(window_size));
  2836. InitializeAppList();
  2837. const int expected_horizontal_margin =
  2838. kMinProductivityLauncherGridHorizontalMargin;
  2839. const gfx::Size expected_item_size(80, 88);
  2840. {
  2841. SCOPED_TRACE("Only apps grid");
  2842. EXPECT_EQ(3, apps_grid_view()->GetRowsForTesting());
  2843. VerifyAppsContainerLayoutForProductivityLauncher(
  2844. window_size, 3 /*row_count*/, expected_horizontal_margin,
  2845. expected_item_size, /*has_recent_apps=*/false);
  2846. }
  2847. AddRecentApps(4);
  2848. contents_view()->ResetForShow();
  2849. {
  2850. SCOPED_TRACE("With recent apps");
  2851. EXPECT_EQ(3, apps_grid_view()->GetRowsForTesting());
  2852. VerifyAppsContainerLayoutForProductivityLauncher(
  2853. window_size, 2 /*row_count*/, expected_horizontal_margin,
  2854. expected_item_size, /*has_recent_apps=*/true);
  2855. }
  2856. }
  2857. TEST_F(ProductivityLauncherAppListViewLayoutTest, DensePortraitScreen) {
  2858. const gfx::Size window_size = gfx::Size(600, 800);
  2859. GetContext()->SetBounds(gfx::Rect(window_size));
  2860. InitializeAppList();
  2861. const int expected_horizontal_margin =
  2862. kMinProductivityLauncherGridHorizontalMargin;
  2863. const gfx::Size expected_item_size(80, 88);
  2864. {
  2865. SCOPED_TRACE("Only apps grid");
  2866. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2867. VerifyAppsContainerLayoutForProductivityLauncher(
  2868. window_size, /*row_count=*/5, expected_horizontal_margin,
  2869. expected_item_size, /*has_recent_apps=*/false);
  2870. }
  2871. AddRecentApps(4);
  2872. contents_view()->ResetForShow();
  2873. {
  2874. SCOPED_TRACE("With recent apps");
  2875. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2876. VerifyAppsContainerLayoutForProductivityLauncher(
  2877. window_size, /*row_count=*/4, expected_horizontal_margin,
  2878. expected_item_size, /*has_recent_apps=*/true);
  2879. }
  2880. }
  2881. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2882. DensePortraitScreenAtMinPreferredVerticalMargin) {
  2883. const int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2884. /*row_count=*/5, /*tile_height=*/88, /*tile_margins=*/8,
  2885. /*large_height*/ false);
  2886. EXPECT_EQ(654, window_height);
  2887. const gfx::Size window_size = gfx::Size(600, window_height);
  2888. GetContext()->SetBounds(gfx::Rect(window_size));
  2889. InitializeAppList();
  2890. const int expected_horizontal_margin =
  2891. kMinProductivityLauncherGridHorizontalMargin;
  2892. const gfx::Size expected_item_size(80, 88);
  2893. {
  2894. SCOPED_TRACE("Only apps grid");
  2895. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2896. VerifyAppsContainerLayoutForProductivityLauncher(
  2897. window_size, /*row_count=*/5, expected_horizontal_margin,
  2898. expected_item_size, /*has_recent_apps=*/false);
  2899. }
  2900. AddRecentApps(4);
  2901. contents_view()->ResetForShow();
  2902. {
  2903. SCOPED_TRACE("With recent apps");
  2904. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2905. VerifyAppsContainerLayoutForProductivityLauncher(
  2906. window_size, 3 /*row_count*/, expected_horizontal_margin,
  2907. expected_item_size, /*has_recent_apps=*/true);
  2908. }
  2909. }
  2910. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2911. DensePortraitScreenWithRemovedRows) {
  2912. const int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2913. /*row_count=*/5, /*tile_height=*/88,
  2914. /*tile_margins=*/8, /*large_height*/ false) -
  2915. 8;
  2916. EXPECT_EQ(646, window_height);
  2917. const gfx::Size window_size = gfx::Size(540, window_height);
  2918. GetContext()->SetBounds(gfx::Rect(window_size));
  2919. InitializeAppList();
  2920. const int expected_horizontal_margin =
  2921. kMinProductivityLauncherGridHorizontalMargin;
  2922. const gfx::Size expected_item_size(80, 88);
  2923. {
  2924. SCOPED_TRACE("Only apps grid");
  2925. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2926. VerifyAppsContainerLayoutForProductivityLauncher(
  2927. window_size, /*row_count=*/4, expected_horizontal_margin,
  2928. expected_item_size, /*has_recent_apps=*/false);
  2929. }
  2930. AddRecentApps(4);
  2931. contents_view()->ResetForShow();
  2932. {
  2933. SCOPED_TRACE("With recent apps");
  2934. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  2935. VerifyAppsContainerLayoutForProductivityLauncher(
  2936. window_size, 3 /*row_count*/, expected_horizontal_margin,
  2937. expected_item_size, /*has_recent_apps=*/true);
  2938. }
  2939. }
  2940. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2941. DensePortraitScreenAtMaxPreferredVerticalMargin) {
  2942. const int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2943. /*row_count=*/5, /*tile_height=*/88, /*tile_margins=*/96,
  2944. /*large_height*/ true);
  2945. EXPECT_EQ(1088, window_height);
  2946. const gfx::Size window_size = gfx::Size(601, window_height);
  2947. GetContext()->SetBounds(gfx::Rect(window_size));
  2948. InitializeAppList();
  2949. const int expected_horizontal_margin =
  2950. kMinProductivityLauncherGridHorizontalMargin;
  2951. const gfx::Size expected_item_size(80, 88);
  2952. {
  2953. SCOPED_TRACE("Only apps grid");
  2954. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2955. VerifyAppsContainerLayoutForProductivityLauncher(
  2956. window_size, /*row_count=*/5, expected_horizontal_margin,
  2957. expected_item_size, /*has_recent_apps=*/false);
  2958. }
  2959. AddRecentApps(4);
  2960. contents_view()->ResetForShow();
  2961. {
  2962. SCOPED_TRACE("With recent apps");
  2963. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  2964. VerifyAppsContainerLayoutForProductivityLauncher(
  2965. window_size, /*row_count=*/4, expected_horizontal_margin,
  2966. expected_item_size, /*has_recent_apps=*/true);
  2967. }
  2968. }
  2969. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  2970. DensePortraitScreenWithExtraRows) {
  2971. const int window_height = GetExpectedScreenSizeForProductivityLauncher(
  2972. /*row_count=*/5, /*tile_height=*/88,
  2973. /*tile_margins=*/96, /*large_height*/ true) +
  2974. 4;
  2975. EXPECT_EQ(1092, window_height);
  2976. const gfx::Size window_size = gfx::Size(601, window_height);
  2977. GetContext()->SetBounds(gfx::Rect(window_size));
  2978. InitializeAppList();
  2979. const int expected_horizontal_margin =
  2980. kMinProductivityLauncherGridHorizontalMargin;
  2981. const gfx::Size expected_item_size(80, 88);
  2982. {
  2983. SCOPED_TRACE("Only apps grid");
  2984. EXPECT_EQ(6, apps_grid_view()->GetRowsForTesting());
  2985. VerifyAppsContainerLayoutForProductivityLauncher(
  2986. window_size, /*row_count=*/6, expected_horizontal_margin,
  2987. expected_item_size, /*has_recent_apps=*/false);
  2988. }
  2989. AddRecentApps(4);
  2990. contents_view()->ResetForShow();
  2991. {
  2992. SCOPED_TRACE("With recent apps");
  2993. EXPECT_EQ(6, apps_grid_view()->GetRowsForTesting());
  2994. VerifyAppsContainerLayoutForProductivityLauncher(
  2995. window_size, /*row_count=*/4, expected_horizontal_margin,
  2996. expected_item_size, /*has_recent_apps=*/true);
  2997. }
  2998. }
  2999. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  3000. DenseAppsGridPaddingScaledDownToMakeRoomForPageSwitcher) {
  3001. // Select window width so using non-zero horizontal padding would result in
  3002. // lack of space for the page switcher.
  3003. const gfx::Size window_size = gfx::Size(512, 800);
  3004. GetContext()->SetBounds(gfx::Rect(window_size));
  3005. InitializeAppList();
  3006. const int expected_horizontal_margin =
  3007. kMinProductivityLauncherGridHorizontalMargin;
  3008. const gfx::Size expected_item_size(80, 88);
  3009. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  3010. VerifyAppsContainerLayoutForProductivityLauncher(
  3011. window_size, /*row_count=*/5, expected_horizontal_margin,
  3012. expected_item_size, /*has_recent_apps=*/false);
  3013. }
  3014. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  3015. DenseAppsGridScaledDownToMakeRoomForPageSwitcher) {
  3016. // Select window width so using default icon width would result in lack of
  3017. // space for the page switcher.
  3018. const gfx::Size window_size = gfx::Size(442, 800);
  3019. GetContext()->SetBounds(gfx::Rect(window_size));
  3020. InitializeAppList();
  3021. const int expected_horizontal_margin =
  3022. kMinProductivityLauncherGridHorizontalMargin;
  3023. const gfx::Size expected_item_size(66, 88);
  3024. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  3025. VerifyAppsContainerLayoutForProductivityLauncher(
  3026. window_size, /*row_count=*/5, expected_horizontal_margin,
  3027. expected_item_size, /*has_recent_apps=*/false);
  3028. }
  3029. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  3030. DenseAppsGridWithMaxHorizontalItemMargins) {
  3031. // Select window width that results in apps grid layout with max allowed
  3032. // horizontal margin (128): 2 * 56 (min horizontal margin) + 4 * 128 + 5 * 80
  3033. const gfx::Size window_size = gfx::Size(984, 600);
  3034. GetContext()->SetBounds(gfx::Rect(window_size));
  3035. InitializeAppList();
  3036. const int expected_horizontal_margin =
  3037. kMinProductivityLauncherGridHorizontalMargin;
  3038. const gfx::Size expected_item_size(80, 88);
  3039. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  3040. VerifyAppsContainerLayoutForProductivityLauncher(
  3041. window_size, /*row_count=*/4, expected_horizontal_margin,
  3042. expected_item_size, /*has_recent_apps=*/false);
  3043. }
  3044. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  3045. DenseAppsGridHorizontalItemMarginsBounded) {
  3046. // Select window width that results in apps grid layout with max allowed
  3047. // horizontal margin (128), i.e. larger than
  3048. // 2 * 56 (min horizontal margin) + 4 * 128 + 5 * 80
  3049. const gfx::Size window_size = gfx::Size(1040, 600);
  3050. GetContext()->SetBounds(gfx::Rect(window_size));
  3051. InitializeAppList();
  3052. const int expected_horizontal_margin = 64;
  3053. const gfx::Size expected_item_size(80, 88);
  3054. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  3055. VerifyAppsContainerLayoutForProductivityLauncher(
  3056. window_size, /*row_count=*/4, expected_horizontal_margin,
  3057. expected_item_size, /*has_recent_apps=*/false);
  3058. }
  3059. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  3060. RegularAppsGridWithMaxHorizontalItemMargins) {
  3061. // Select window width that results in apps grid layout with max allowed
  3062. // horizontal margin (128):
  3063. // 2 * 56 (min horizontal margin) + 4 * 128 + 5 * 96
  3064. const gfx::Size window_size = gfx::Size(1104, 1200);
  3065. GetContext()->SetBounds(gfx::Rect(window_size));
  3066. InitializeAppList();
  3067. const int expected_horizontal_margin =
  3068. kMinProductivityLauncherGridHorizontalMargin;
  3069. const gfx::Size expected_item_size(96, 120);
  3070. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  3071. VerifyAppsContainerLayoutForProductivityLauncher(
  3072. window_size, /*row_count=*/5, expected_horizontal_margin,
  3073. expected_item_size, /*has_recent_apps=*/false);
  3074. }
  3075. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  3076. RegularAppsGridHorizontalItemMarginsBounded) {
  3077. // Select window width that results in apps grid layout with max allowed
  3078. // horizontal margin (128), i.e. larger than
  3079. // 2 * 56 (min horizontal margin) + 4 * 128 + 5 * 96
  3080. const gfx::Size window_size = gfx::Size(1116, 1200);
  3081. GetContext()->SetBounds(gfx::Rect(window_size));
  3082. InitializeAppList();
  3083. const int expected_horizontal_margin = 62;
  3084. const gfx::Size expected_item_size(96, 120);
  3085. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  3086. VerifyAppsContainerLayoutForProductivityLauncher(
  3087. window_size, /*row_count=*/5, expected_horizontal_margin,
  3088. expected_item_size, /*has_recent_apps=*/false);
  3089. }
  3090. TEST_F(ProductivityLauncherAppListViewLayoutTest, LayoutAfterConfigChange) {
  3091. const gfx::Size window_size = gfx::Size(600, 800);
  3092. GetContext()->SetBounds(gfx::Rect(window_size));
  3093. InitializeAppList();
  3094. const int expected_horizontal_margin =
  3095. kMinProductivityLauncherGridHorizontalMargin;
  3096. const gfx::Size expected_item_size(80, 88);
  3097. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  3098. VerifyAppsContainerLayoutForProductivityLauncher(
  3099. window_size, /*row_count=*/5, expected_horizontal_margin,
  3100. expected_item_size, /*has_recent_apps=*/false);
  3101. const gfx::Size updated_window_size = gfx::Size(1000, 800);
  3102. GetContext()->SetBounds(gfx::Rect(updated_window_size));
  3103. view_->OnParentWindowBoundsChanged();
  3104. const gfx::Size expected_updated_item_size(96, 120);
  3105. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  3106. VerifyAppsContainerLayoutForProductivityLauncher(
  3107. updated_window_size, /*row_count=*/4, expected_horizontal_margin,
  3108. expected_updated_item_size, /*has_recent_apps=*/false);
  3109. }
  3110. TEST_F(ProductivityLauncherAppListViewLayoutTest,
  3111. LayoutAfterConfigChangeWithRecentApps) {
  3112. const gfx::Size window_size = gfx::Size(600, 800);
  3113. GetContext()->SetBounds(gfx::Rect(window_size));
  3114. InitializeAppList();
  3115. AddRecentApps(4);
  3116. contents_view()->ResetForShow();
  3117. const int expected_horizontal_margin =
  3118. kMinProductivityLauncherGridHorizontalMargin;
  3119. const gfx::Size expected_item_size(80, 88);
  3120. EXPECT_EQ(5, apps_grid_view()->GetRowsForTesting());
  3121. VerifyAppsContainerLayoutForProductivityLauncher(
  3122. window_size, /*row_count=*/4, expected_horizontal_margin,
  3123. expected_item_size, /*has_recent_apps=*/true);
  3124. const gfx::Size updated_window_size = gfx::Size(1000, 800);
  3125. GetContext()->SetBounds(gfx::Rect(updated_window_size));
  3126. view_->OnParentWindowBoundsChanged();
  3127. const gfx::Size expected_updated_item_size(96, 120);
  3128. EXPECT_EQ(4, apps_grid_view()->GetRowsForTesting());
  3129. VerifyAppsContainerLayoutForProductivityLauncher(
  3130. updated_window_size, /*row_count=*/3, expected_horizontal_margin,
  3131. expected_updated_item_size, /*has_recent_apps=*/true);
  3132. }
  3133. // Tests fullscreen apps grid sizing and layout for small screens (width < 960)
  3134. // in landscape layout.
  3135. TEST_F(LegacyLauncherAppListViewLayoutTest,
  3136. AppListViewLayoutForSmallLandscapeScreen) {
  3137. const gfx::Size window_size = gfx::Size(800, 600);
  3138. gfx::NativeView parent = GetContext();
  3139. parent->SetBounds(gfx::Rect(window_size));
  3140. Initialize(false /*is_tablet_mode*/);
  3141. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  3142. Show();
  3143. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  3144. const int expected_vertical_margin =
  3145. (window_size.height() - ShelfSize()) / 16;
  3146. VerifyAppsContainerLayout(
  3147. window_size, 5 /*column_count*/, 4 /*row_count*/,
  3148. window_size.width() / 12 /*expected_horizontal_margin*/,
  3149. expected_vertical_margin, 80 /*expected_item_size*/);
  3150. }
  3151. // Tests fullscreen apps grid sizing and layout for small screens (width < 600)
  3152. // in portrait layout.
  3153. TEST_F(LegacyLauncherAppListViewLayoutTest,
  3154. AppListViewLayoutForSmallPortraitScreen) {
  3155. const gfx::Size window_size = gfx::Size(500, 800);
  3156. gfx::NativeView parent = GetContext();
  3157. parent->SetBounds(gfx::Rect(window_size));
  3158. Initialize(false /*is_tablet_mode*/);
  3159. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  3160. Show();
  3161. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  3162. const int expected_vertical_margin =
  3163. (window_size.height() - ShelfSize()) / 16;
  3164. VerifyAppsContainerLayout(window_size, 4 /*column_count*/, 5 /*row_count*/,
  3165. 56 /*expected_horizontal_margin*/,
  3166. expected_vertical_margin,
  3167. 80 /*expected_item_size*/);
  3168. }
  3169. // Tests fullscreen apps grid sizing and layout for medium sized screens
  3170. // (width < 1200) in lanscape layout.
  3171. TEST_F(LegacyLauncherAppListViewLayoutTest,
  3172. AppListViewLayoutForMediumLandscapeScreen) {
  3173. const gfx::Size window_size = gfx::Size(960, 800);
  3174. gfx::NativeView parent = GetContext();
  3175. parent->SetBounds(gfx::Rect(window_size));
  3176. Initialize(false /*is_tablet_mode*/);
  3177. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  3178. Show();
  3179. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  3180. // Horizontal margin should be set so apps grid doesn't go over the max size.
  3181. const int expected_horizontal_margin =
  3182. (window_size.width() - GetItemGridSizeWithMaxItemMargins(88, 5)) / 2;
  3183. const int expected_vertical_margin =
  3184. (window_size.height() - ShelfSize()) / 16;
  3185. VerifyAppsContainerLayout(window_size, 5 /*column_count*/, 4 /*row_count*/,
  3186. expected_horizontal_margin,
  3187. expected_vertical_margin,
  3188. 88 /*expected_item_size*/);
  3189. }
  3190. // Tests fullscreen apps grid sizing and layout for medium sized screens
  3191. // (width < 768) in portrait layout.
  3192. TEST_F(LegacyLauncherAppListViewLayoutTest,
  3193. AppListViewLayoutForMediumPortraitScreen) {
  3194. const gfx::Size window_size = gfx::Size(700, 800);
  3195. gfx::NativeView parent = GetContext();
  3196. parent->SetBounds(gfx::Rect(window_size));
  3197. Initialize(false /*is_tablet_mode*/);
  3198. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  3199. Show();
  3200. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  3201. const int expected_vertical_margin =
  3202. (window_size.height() - ShelfSize()) / 16;
  3203. VerifyAppsContainerLayout(
  3204. window_size, 4 /*column_count*/, 5 /*row_count*/,
  3205. window_size.width() / 12 /*expected_horizontal_margin*/,
  3206. expected_vertical_margin, 88 /*expected_item_size*/);
  3207. }
  3208. // Tests fullscreen apps grid sizing and layout for large screens
  3209. // (width >= 1200) in landscape layout.
  3210. TEST_F(LegacyLauncherAppListViewLayoutTest,
  3211. AppListViewLayoutForLargeLandscapeScreen) {
  3212. const gfx::Size window_size = gfx::Size(1200, 960);
  3213. gfx::NativeView parent = GetContext();
  3214. parent->SetBounds(gfx::Rect(window_size));
  3215. Initialize(false /*is_tablet_mode*/);
  3216. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  3217. Show();
  3218. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  3219. // Horizontal margin should be set so apps grid doesn't go over the max size.
  3220. const int expected_horizontal_margin =
  3221. (window_size.width() - GetItemGridSizeWithMaxItemMargins(120, 5)) / 2;
  3222. const int expected_vertical_margin =
  3223. (window_size.height() - ShelfSize()) / 16;
  3224. VerifyAppsContainerLayout(window_size, 5 /*column_count*/, 4 /*row_count*/,
  3225. expected_horizontal_margin,
  3226. expected_vertical_margin,
  3227. 120 /*expected_item_size*/);
  3228. }
  3229. // Tests fullscreen apps grid sizing and layout for large screens (width >= 768)
  3230. // in portrait layout.
  3231. TEST_F(LegacyLauncherAppListViewLayoutTest,
  3232. AppListViewLayoutForLargePortraitScreen) {
  3233. const gfx::Size window_size = gfx::Size(800, 1200);
  3234. gfx::NativeView parent = GetContext();
  3235. parent->SetBounds(gfx::Rect(window_size));
  3236. Initialize(false /*is_tablet_mode*/);
  3237. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  3238. Show();
  3239. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  3240. const int expected_vertical_margin =
  3241. (window_size.height() - ShelfSize()) / 16;
  3242. VerifyAppsContainerLayout(
  3243. window_size, 4 /*column_count*/, 5 /*row_count*/,
  3244. window_size.width() / 12 /*expected_horizontal_margin*/,
  3245. expected_vertical_margin, 120 /*expected_item_size*/);
  3246. }
  3247. // Tests that apps grid horizontal margin have minimum that ensures the page
  3248. // switcher view can fit next to the apps grid.
  3249. TEST_F(LegacyLauncherAppListViewLayoutTest,
  3250. EnsurePageSwitcherFitsAppsGridMargin) {
  3251. const gfx::Size window_size = gfx::Size(440, 800);
  3252. gfx::NativeView parent = GetContext();
  3253. parent->SetBounds(gfx::Rect(window_size));
  3254. Initialize(false /*is_tablet_mode*/);
  3255. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  3256. Show();
  3257. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  3258. const int expected_vertical_margin =
  3259. (window_size.height() - ShelfSize()) / 16;
  3260. // The horizontal margin is selected so the page switcher fits the margin
  3261. // space (note that 440 / 12, which is how the margin is normally calculated
  3262. // is smaller than the width required by page switcher).
  3263. VerifyAppsContainerLayout(window_size, 4 /*column_count*/, 5 /*row_count*/,
  3264. 56 /*expected_horizontal_margin*/,
  3265. expected_vertical_margin,
  3266. 80 /*expected_item_size*/);
  3267. }
  3268. // Verifies that the vertical spacing between items in apps grid has an upper
  3269. // limit, and that the apps grid is centered in the available space if item
  3270. // spacing hits that limit.
  3271. TEST_F(LegacyLauncherAppListViewLayoutTest,
  3272. VerticalAppsGridItemSpacingIsBounded) {
  3273. const gfx::Size window_size = gfx::Size(960, 1600);
  3274. gfx::NativeView parent = GetContext();
  3275. parent->SetBounds(gfx::Rect(window_size));
  3276. Initialize(false /*is_tablet_mode*/);
  3277. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  3278. Show();
  3279. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  3280. // Horizontal margin should be set so apps grid doesn't go over the max size.
  3281. const int expected_horizontal_margin =
  3282. (window_size.width() - GetItemGridSizeWithMaxItemMargins(120, 4)) / 2;
  3283. const int expected_vertical_margin =
  3284. (window_size.height() - ShelfSize() - kGridVerticalInset -
  3285. kSearchBoxAndSuggestionChipsHeightDefault - kGridVerticalMargin -
  3286. GetItemGridSizeWithMaxItemMargins(120, 5)) /
  3287. 2;
  3288. VerifyAppsContainerLayout(window_size, 4 /*column_count*/, 5 /*row_count*/,
  3289. expected_horizontal_margin,
  3290. expected_vertical_margin,
  3291. 120 /*expected_item_size*/);
  3292. }
  3293. // Verifies that the vertical apps container margin is big enough to fit the
  3294. // apps grid fadeout area.
  3295. TEST_F(LegacyLauncherAppListViewLayoutTest,
  3296. VerticalAppsContainerMarginFitFadeoutArea) {
  3297. const gfx::Size window_size(650, 536);
  3298. gfx::NativeView parent = GetContext();
  3299. parent->SetBounds(gfx::Rect(window_size));
  3300. Initialize(false /*is_tablet_mode*/);
  3301. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  3302. Show();
  3303. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  3304. // The horizontal margin is selected so the page switcher fits the margin
  3305. // space (note that 650 / 12, which is how the margin is normally calculated
  3306. // is smaller than the width required by page switcher).
  3307. VerifyAppsContainerLayout(
  3308. window_size, 5 /*column_count*/, 4 /*row_count*/,
  3309. 56 /*expected_horizontal_margin*/,
  3310. kGridVerticalInset + kGridVerticalMargin /*expected_vertical_margin*/,
  3311. 80 /*expected_item_size*/);
  3312. }
  3313. // Tests fullscreen apps grid sizing and layout gets updated to correct bounds
  3314. // when app list config changes.
  3315. TEST_F(LegacyLauncherAppListViewLayoutTest,
  3316. AppListViewLayoutAfterConfigChange) {
  3317. const gfx::Size window_size = gfx::Size(500, 800);
  3318. gfx::NativeView parent = GetContext();
  3319. parent->SetBounds(gfx::Rect(window_size));
  3320. Initialize(false /*is_tablet_mode*/);
  3321. delegate_->GetTestModel()->PopulateApps(kInitialItems);
  3322. Show();
  3323. view_->SetState(ash::AppListViewState::kFullscreenAllApps);
  3324. int expected_vertical_margin = (window_size.height() - ShelfSize()) / 16;
  3325. VerifyAppsContainerLayout(window_size, 4 /*column_count*/, 5 /*row_count*/,
  3326. 56 /*expected_horizontal_margin*/,
  3327. expected_vertical_margin,
  3328. 80 /*expected_item_size*/);
  3329. const gfx::Size updated_window_size = gfx::Size(800, 1200);
  3330. parent->SetBounds(gfx::Rect(updated_window_size));
  3331. view_->OnParentWindowBoundsChanged();
  3332. expected_vertical_margin = (updated_window_size.height() - ShelfSize()) / 16;
  3333. VerifyAppsContainerLayout(
  3334. updated_window_size, 4 /*column_count*/, 5 /*row_count*/,
  3335. updated_window_size.width() / 12 /*expected_horizontal_margin*/,
  3336. expected_vertical_margin, 120 /*expected_item_size*/);
  3337. }
  3338. // Tests that page switching in folder doesn't record AppListPageSwitcherSource
  3339. // metric. ProductivityLauncher does not use pages in folders.
  3340. TEST_F(AppListViewPeekingFocusTest, PageSwitchingNotRecordingMetric) {
  3341. base::HistogramTester histogram_tester;
  3342. Show();
  3343. histogram_tester.ExpectTotalCount("Apps.AppListPageSwitcherSource", 0);
  3344. // Transition to kFullscreenAllApps state and open the folder.
  3345. SetAppListState(ash::AppListViewState::kFullscreenAllApps);
  3346. folder_item_view()->RequestFocus();
  3347. SimulateKeyPress(ui::VKEY_RETURN, false);
  3348. ASSERT_TRUE(contents_view()->apps_container_view()->IsInFolderView());
  3349. // Create a fling to the left so the folder view changes page.
  3350. constexpr float kFlingVelocityForChangingPage = 850.0f;
  3351. gfx::Point location = app_list_folder_view()->bounds().CenterPoint();
  3352. ui::GestureEventDetails details = ui::GestureEventDetails(
  3353. ui::ET_SCROLL_FLING_START, -kFlingVelocityForChangingPage, 0);
  3354. ui::GestureEvent event = ui::GestureEvent(
  3355. location.x(), location.y(), ui::EF_NONE, base::TimeTicks::Now(), details);
  3356. app_list_folder_view()->items_grid_view()->OnGestureEvent(&event);
  3357. ASSERT_TRUE(event.handled());
  3358. histogram_tester.ExpectTotalCount("Apps.AppListPageSwitcherSource", 0);
  3359. }
  3360. } // namespace
  3361. } // namespace ash