workspace_window_resizer_unittest.cc 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746
  1. // Copyright (c) 2012 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/wm/workspace/workspace_window_resizer.h"
  5. #include "ash/public/cpp/shell_window_ids.h"
  6. #include "ash/public/cpp/window_properties.h"
  7. #include "ash/screen_util.h"
  8. #include "ash/shelf/shelf.h"
  9. #include "ash/shell.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "ash/wm/window_positioning_utils.h"
  12. #include "ash/wm/window_state.h"
  13. #include "ash/wm/window_util.h"
  14. #include "ash/wm/wm_event.h"
  15. #include "ash/wm/work_area_insets.h"
  16. #include "ash/wm/workspace/phantom_window_controller.h"
  17. #include "ash/wm/workspace_controller.h"
  18. #include "base/containers/adapters.h"
  19. #include "base/strings/stringprintf.h"
  20. #include "base/test/metrics/histogram_tester.h"
  21. #include "base/test/scoped_feature_list.h"
  22. #include "base/time/time.h"
  23. #include "base/timer/timer.h"
  24. #include "chromeos/ui/wm/features.h"
  25. #include "ui/aura/client/aura_constants.h"
  26. #include "ui/aura/test/test_window_delegate.h"
  27. #include "ui/aura/test/test_windows.h"
  28. #include "ui/aura/window.h"
  29. #include "ui/aura/window_event_dispatcher.h"
  30. #include "ui/base/hit_test.h"
  31. #include "ui/compositor/layer.h"
  32. #include "ui/compositor/test/test_utils.h"
  33. #include "ui/display/display_layout.h"
  34. #include "ui/display/manager/display_manager.h"
  35. #include "ui/display/screen.h"
  36. #include "ui/display/test/display_manager_test_api.h"
  37. #include "ui/events/gesture_detection/gesture_configuration.h"
  38. #include "ui/events/test/event_generator.h"
  39. #include "ui/gfx/geometry/insets.h"
  40. #include "ui/views/widget/widget.h"
  41. #include "ui/views/widget/widget_observer.h"
  42. namespace ash {
  43. namespace {
  44. using chromeos::WindowStateType;
  45. constexpr int kRootHeight = 600;
  46. gfx::PointF CalculateDragPoint(const WindowResizer& resizer,
  47. int delta_x,
  48. int delta_y) {
  49. gfx::PointF location = resizer.GetInitialLocation();
  50. location.set_x(location.x() + delta_x);
  51. location.set_y(location.y() + delta_y);
  52. return location;
  53. }
  54. void AllowSnap(aura::Window* window) {
  55. window->SetProperty(aura::client::kResizeBehaviorKey,
  56. aura::client::kResizeBehaviorCanResize |
  57. aura::client::kResizeBehaviorCanMaximize);
  58. }
  59. // A simple window delegate that returns the specified min size.
  60. class TestWindowDelegate : public aura::test::TestWindowDelegate {
  61. public:
  62. TestWindowDelegate() = default;
  63. TestWindowDelegate(const TestWindowDelegate&) = delete;
  64. TestWindowDelegate& operator=(const TestWindowDelegate&) = delete;
  65. ~TestWindowDelegate() override = default;
  66. void set_min_size(const gfx::Size& size) { min_size_ = size; }
  67. void set_max_size(const gfx::Size& size) { max_size_ = size; }
  68. private:
  69. // aura::Test::TestWindowDelegate:
  70. gfx::Size GetMinimumSize() const override { return min_size_; }
  71. gfx::Size GetMaximumSize() const override { return max_size_; }
  72. gfx::Size min_size_;
  73. gfx::Size max_size_;
  74. };
  75. } // namespace
  76. class WorkspaceWindowResizerTest : public AshTestBase {
  77. public:
  78. WorkspaceWindowResizerTest() = default;
  79. WorkspaceWindowResizerTest(const WorkspaceWindowResizerTest&) = delete;
  80. WorkspaceWindowResizerTest& operator=(const WorkspaceWindowResizerTest&) =
  81. delete;
  82. ~WorkspaceWindowResizerTest() override = default;
  83. void SetUp() override {
  84. AshTestBase::SetUp();
  85. UpdateDisplay(base::StringPrintf("800x%d", kRootHeight));
  86. // Ignore the touch slop region.
  87. ui::GestureConfiguration::GetInstance()
  88. ->set_max_touch_move_in_pixels_for_click(0);
  89. aura::Window* root = Shell::GetPrimaryRootWindow();
  90. gfx::Rect root_bounds(root->bounds());
  91. EXPECT_EQ(800, root_bounds.width());
  92. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  93. root, gfx::Rect(), gfx::Insets(), gfx::Insets());
  94. window_ = std::make_unique<aura::Window>(&delegate_);
  95. window_->SetType(aura::client::WINDOW_TYPE_NORMAL);
  96. window_->Init(ui::LAYER_NOT_DRAWN);
  97. ParentWindowInPrimaryRootWindow(window_.get());
  98. window_->SetId(1);
  99. window2_ = std::make_unique<aura::Window>(&delegate2_);
  100. window2_->SetType(aura::client::WINDOW_TYPE_NORMAL);
  101. window2_->Init(ui::LAYER_NOT_DRAWN);
  102. ParentWindowInPrimaryRootWindow(window2_.get());
  103. window2_->SetId(2);
  104. window3_ = std::make_unique<aura::Window>(&delegate3_);
  105. window3_->SetType(aura::client::WINDOW_TYPE_NORMAL);
  106. window3_->Init(ui::LAYER_NOT_DRAWN);
  107. ParentWindowInPrimaryRootWindow(window3_.get());
  108. window3_->SetId(3);
  109. window4_ = std::make_unique<aura::Window>(&delegate4_);
  110. window4_->SetType(aura::client::WINDOW_TYPE_NORMAL);
  111. window4_->Init(ui::LAYER_NOT_DRAWN);
  112. ParentWindowInPrimaryRootWindow(window4_.get());
  113. window4_->SetId(4);
  114. }
  115. void TearDown() override {
  116. window_.reset();
  117. window2_.reset();
  118. window3_.reset();
  119. window4_.reset();
  120. touch_resize_window_.reset();
  121. AshTestBase::TearDown();
  122. }
  123. // Returns a string identifying the z-order of each of the known child windows
  124. // of |parent|. The returned string constains the id of the known windows and
  125. // is ordered from topmost to bottomost windows.
  126. std::vector<int> WindowOrderAsIntVector(aura::Window* parent) const {
  127. std::vector<int> result;
  128. const aura::Window::Windows& windows = parent->children();
  129. for (aura::Window* window : base::Reversed(windows)) {
  130. if (window == window_.get() || window == window2_.get() ||
  131. window == window3_.get()) {
  132. result.push_back(window->GetId());
  133. }
  134. }
  135. return result;
  136. }
  137. protected:
  138. std::unique_ptr<WindowResizer> CreateResizerForTest(
  139. aura::Window* window,
  140. const gfx::Point& point_in_parent = gfx::Point(),
  141. int window_component = HTCAPTION) {
  142. auto resizer =
  143. CreateWindowResizer(window, gfx::PointF(point_in_parent),
  144. window_component, ::wm::WINDOW_MOVE_SOURCE_MOUSE);
  145. workspace_resizer_ = WorkspaceWindowResizer::GetInstanceForTest();
  146. return resizer;
  147. }
  148. std::unique_ptr<WorkspaceWindowResizer> CreateWorkspaceResizerForTest(
  149. aura::Window* window,
  150. int window_component,
  151. const std::vector<aura::Window*>& attached_windows) {
  152. return CreateWorkspaceResizerForTest(window, gfx::Point(), window_component,
  153. ::wm::WINDOW_MOVE_SOURCE_MOUSE,
  154. attached_windows);
  155. }
  156. std::unique_ptr<WorkspaceWindowResizer> CreateWorkspaceResizerForTest(
  157. aura::Window* window,
  158. const gfx::Point& point_in_parent,
  159. int window_component,
  160. ::wm::WindowMoveSource source,
  161. const std::vector<aura::Window*>& attached_windows) {
  162. WindowState* window_state = WindowState::Get(window);
  163. window_state->CreateDragDetails(gfx::PointF(point_in_parent),
  164. window_component, source);
  165. return WorkspaceWindowResizer::Create(window_state, attached_windows);
  166. }
  167. void DragToMaximize(aura::Window* window) {
  168. window->SetBounds(gfx::Rect(200, 200));
  169. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window);
  170. resizer->Drag(gfx::PointF(400.f, 400.f), 0);
  171. resizer->Drag(gfx::PointF(400.f, 2.f), 0);
  172. DwellCountdownTimerFireNow();
  173. resizer->CompleteDrag();
  174. ASSERT_TRUE(WindowState::Get(window)->IsMaximized());
  175. ASSERT_TRUE(WindowState::Get(window)->HasRestoreBounds());
  176. resizer.reset();
  177. }
  178. void DragToRestore(aura::Window* window) {
  179. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window);
  180. resizer->Drag(gfx::PointF(200.f, 200.f), 0);
  181. resizer->CompleteDrag();
  182. ASSERT_FALSE(WindowState::Get(window)->IsMaximized());
  183. resizer.reset();
  184. }
  185. PhantomWindowController* snap_phantom_window_controller() const {
  186. return workspace_resizer_->snap_phantom_window_controller_.get();
  187. }
  188. void InitTouchResizeWindow(const gfx::Rect& bounds, int window_component) {
  189. touch_resize_delegate_.set_window_component(window_component);
  190. touch_resize_window_.reset(CreateTestWindowInShellWithDelegate(
  191. &touch_resize_delegate_, 0, bounds));
  192. }
  193. bool IsDwellCountdownTimerRunning() {
  194. return workspace_resizer_->dwell_countdown_timer_.IsRunning();
  195. }
  196. void DwellCountdownTimerFireNow() {
  197. workspace_resizer_->dwell_countdown_timer_.FireNow();
  198. }
  199. void DragToMaximizeBehaviorCheckCountdownTimerFireNow(aura::Window* window) {
  200. WindowState::Get(window)->drag_to_maximize_mis_trigger_timer_.FireNow();
  201. }
  202. TestWindowDelegate delegate_;
  203. TestWindowDelegate delegate2_;
  204. TestWindowDelegate delegate3_;
  205. TestWindowDelegate delegate4_;
  206. std::unique_ptr<aura::Window> window_;
  207. std::unique_ptr<aura::Window> window2_;
  208. std::unique_ptr<aura::Window> window3_;
  209. std::unique_ptr<aura::Window> window4_;
  210. TestWindowDelegate touch_resize_delegate_;
  211. std::unique_ptr<aura::Window> touch_resize_window_;
  212. WorkspaceWindowResizer* workspace_resizer_ = nullptr;
  213. };
  214. // Assertions around attached window resize dragging from the right with 2
  215. // windows.
  216. TEST_F(WorkspaceWindowResizerTest, AttachedResize_RIGHT_2) {
  217. window_->SetBounds(gfx::Rect(0, 300, 400, 300));
  218. window2_->SetBounds(gfx::Rect(400, 200, 100, 200));
  219. std::unique_ptr<WorkspaceWindowResizer> resizer =
  220. CreateWorkspaceResizerForTest(window_.get(), HTRIGHT, {window2_.get()});
  221. ASSERT_TRUE(resizer.get());
  222. // Move it 100 to the right, which should expand w1 and push w2.
  223. resizer->Drag(CalculateDragPoint(*resizer, 100, 10), 0);
  224. EXPECT_EQ("0,300 500x300", window_->bounds().ToString());
  225. EXPECT_EQ("500,200 100x200", window2_->bounds().ToString());
  226. // Push off the screen, w2 should be resized to its min.
  227. delegate2_.set_min_size(gfx::Size(20, 20));
  228. resizer->Drag(CalculateDragPoint(*resizer, 800, 20), 0);
  229. EXPECT_EQ("0,300 780x300", window_->bounds().ToString());
  230. EXPECT_EQ("780,200 20x200", window2_->bounds().ToString());
  231. // Move back to 100 and verify w2 gets its original size.
  232. resizer->Drag(CalculateDragPoint(*resizer, 100, 10), 0);
  233. EXPECT_EQ("0,300 500x300", window_->bounds().ToString());
  234. EXPECT_EQ("500,200 100x200", window2_->bounds().ToString());
  235. // Revert and make sure everything moves back.
  236. resizer->Drag(CalculateDragPoint(*resizer, 800, 20), 0);
  237. resizer->RevertDrag();
  238. EXPECT_EQ("0,300 400x300", window_->bounds().ToString());
  239. EXPECT_EQ("400,200 100x200", window2_->bounds().ToString());
  240. }
  241. // Assertions around collapsing and expanding.
  242. TEST_F(WorkspaceWindowResizerTest, AttachedResize_RIGHT_Compress) {
  243. window_->SetBounds(gfx::Rect(0, 300, 400, 300));
  244. window2_->SetBounds(gfx::Rect(400, 200, 100, 200));
  245. std::unique_ptr<WorkspaceWindowResizer> resizer =
  246. CreateWorkspaceResizerForTest(window_.get(), HTRIGHT, {window2_.get()});
  247. ASSERT_TRUE(resizer.get());
  248. // Move it 100 to the left, which should expand w2 and collapse w1.
  249. resizer->Drag(CalculateDragPoint(*resizer, -100, 10), 0);
  250. EXPECT_EQ("0,300 300x300", window_->bounds().ToString());
  251. EXPECT_EQ("300,200 200x200", window2_->bounds().ToString());
  252. // Collapse all the way to w1's min.
  253. delegate_.set_min_size(gfx::Size(25, 25));
  254. resizer->Drag(CalculateDragPoint(*resizer, -800, 25), 0);
  255. EXPECT_EQ("0,300 25x300", window_->bounds().ToString());
  256. EXPECT_EQ("25,200 475x200", window2_->bounds().ToString());
  257. // But should keep minimum visible width;
  258. resizer->Drag(CalculateDragPoint(*resizer, -800, 20), 0);
  259. EXPECT_EQ("0,300 25x300", window_->bounds().ToString());
  260. EXPECT_EQ("25,200 475x200", window2_->bounds().ToString());
  261. // Move 100 to the left.
  262. resizer->Drag(CalculateDragPoint(*resizer, 100, 10), 0);
  263. EXPECT_EQ("0,300 500x300", window_->bounds().ToString());
  264. EXPECT_EQ("500,200 100x200", window2_->bounds().ToString());
  265. // Back to -100.
  266. resizer->Drag(CalculateDragPoint(*resizer, -100, 20), 0);
  267. EXPECT_EQ("0,300 300x300", window_->bounds().ToString());
  268. EXPECT_EQ("300,200 200x200", window2_->bounds().ToString());
  269. }
  270. // Assertions around attached window resize dragging from the right with 3
  271. // windows.
  272. TEST_F(WorkspaceWindowResizerTest, AttachedResize_RIGHT_3) {
  273. window_->SetBounds(gfx::Rect(100, 300, 200, 300));
  274. window2_->SetBounds(gfx::Rect(300, 300, 150, 200));
  275. window3_->SetBounds(gfx::Rect(450, 300, 100, 200));
  276. delegate2_.set_min_size(gfx::Size(52, 50));
  277. delegate3_.set_min_size(gfx::Size(38, 50));
  278. std::unique_ptr<WorkspaceWindowResizer> resizer =
  279. CreateWorkspaceResizerForTest(window_.get(), HTRIGHT,
  280. {window2_.get(), window3_.get()});
  281. ASSERT_TRUE(resizer.get());
  282. // Move it 100 to the right, which should expand w1 and push w2 and w3.
  283. resizer->Drag(CalculateDragPoint(*resizer, 100, -10), 0);
  284. EXPECT_EQ("100,300 300x300", window_->bounds().ToString());
  285. EXPECT_EQ("400,300 150x200", window2_->bounds().ToString());
  286. EXPECT_EQ("550,300 100x200", window3_->bounds().ToString());
  287. // Move it 300, things should compress.
  288. resizer->Drag(CalculateDragPoint(*resizer, 300, -10), 0);
  289. EXPECT_EQ("100,300 500x300", window_->bounds().ToString());
  290. EXPECT_EQ("600,300 120x200", window2_->bounds().ToString());
  291. EXPECT_EQ("720,300 80x200", window3_->bounds().ToString());
  292. // Move it so much the last two end up at their min.
  293. resizer->Drag(CalculateDragPoint(*resizer, 800, 50), 0);
  294. EXPECT_EQ("100,300 610x300", window_->bounds().ToString());
  295. EXPECT_EQ("710,300 52x200", window2_->bounds().ToString());
  296. EXPECT_EQ("762,300 38x200", window3_->bounds().ToString());
  297. // Revert and make sure everything moves back.
  298. resizer->RevertDrag();
  299. EXPECT_EQ("100,300 200x300", window_->bounds().ToString());
  300. EXPECT_EQ("300,300 150x200", window2_->bounds().ToString());
  301. EXPECT_EQ("450,300 100x200", window3_->bounds().ToString());
  302. }
  303. // Assertions around attached window resizing (collapsing and expanding) with
  304. // 3 windows.
  305. TEST_F(WorkspaceWindowResizerTest, AttachedResize_RIGHT_3_Compress) {
  306. window_->SetBounds(gfx::Rect(100, 300, 200, 300));
  307. window2_->SetBounds(gfx::Rect(300, 300, 200, 200));
  308. window3_->SetBounds(gfx::Rect(450, 300, 100, 200));
  309. delegate2_.set_min_size(gfx::Size(52, 50));
  310. delegate3_.set_min_size(gfx::Size(38, 50));
  311. std::unique_ptr<WorkspaceWindowResizer> resizer =
  312. CreateWorkspaceResizerForTest(window_.get(), HTRIGHT,
  313. {window2_.get(), window3_.get()});
  314. ASSERT_TRUE(resizer.get());
  315. // Move it -100 to the right, which should collapse w1 and expand w2 and w3.
  316. resizer->Drag(CalculateDragPoint(*resizer, -100, -10), 0);
  317. EXPECT_EQ("100,300 100x300", window_->bounds().ToString());
  318. EXPECT_EQ("200,300 266x200", window2_->bounds().ToString());
  319. EXPECT_EQ("466,300 134x200", window3_->bounds().ToString());
  320. // Move it 100 to the right.
  321. resizer->Drag(CalculateDragPoint(*resizer, 100, -10), 0);
  322. EXPECT_EQ("100,300 300x300", window_->bounds().ToString());
  323. EXPECT_EQ("400,300 200x200", window2_->bounds().ToString());
  324. EXPECT_EQ("600,300 100x200", window3_->bounds().ToString());
  325. // 100 to the left again.
  326. resizer->Drag(CalculateDragPoint(*resizer, -100, -10), 0);
  327. EXPECT_EQ("100,300 100x300", window_->bounds().ToString());
  328. EXPECT_EQ("200,300 266x200", window2_->bounds().ToString());
  329. EXPECT_EQ("466,300 134x200", window3_->bounds().ToString());
  330. }
  331. // Assertions around collapsing and expanding from the bottom.
  332. TEST_F(WorkspaceWindowResizerTest, AttachedResize_BOTTOM_Compress) {
  333. window_->SetBounds(gfx::Rect(0, 100, 400, 300));
  334. window2_->SetBounds(gfx::Rect(400, 400, 100, 200));
  335. std::unique_ptr<WorkspaceWindowResizer> resizer =
  336. CreateWorkspaceResizerForTest(window_.get(), HTBOTTOM, {window2_.get()});
  337. ASSERT_TRUE(resizer.get());
  338. // Move it up 100, which should expand w2 and collapse w1.
  339. resizer->Drag(CalculateDragPoint(*resizer, 10, -100), 0);
  340. EXPECT_EQ("0,100 400x200", window_->bounds().ToString());
  341. EXPECT_EQ("400,300 100x300", window2_->bounds().ToString());
  342. // Collapse all the way to w1's min.
  343. delegate_.set_min_size(gfx::Size(20, 20));
  344. resizer->Drag(CalculateDragPoint(*resizer, 20, -800), 0);
  345. EXPECT_EQ("0,100 400x20", window_->bounds().ToString());
  346. EXPECT_EQ("400,120 100x480", window2_->bounds().ToString());
  347. // Move 100 down.
  348. resizer->Drag(CalculateDragPoint(*resizer, 10, 100), 0);
  349. EXPECT_EQ("0,100 400x400", window_->bounds().ToString());
  350. EXPECT_EQ("400,500 100x100", window2_->bounds().ToString());
  351. // Back to -100.
  352. resizer->Drag(CalculateDragPoint(*resizer, 20, -100), 0);
  353. EXPECT_EQ("0,100 400x200", window_->bounds().ToString());
  354. EXPECT_EQ("400,300 100x300", window2_->bounds().ToString());
  355. }
  356. // Assertions around attached window resize dragging from the bottom with 2
  357. // windows.
  358. TEST_F(WorkspaceWindowResizerTest, AttachedResize_BOTTOM_2) {
  359. window_->SetBounds(gfx::Rect(0, 50, 400, 200));
  360. window2_->SetBounds(gfx::Rect(0, 250, 200, 100));
  361. std::unique_ptr<WorkspaceWindowResizer> resizer =
  362. CreateWorkspaceResizerForTest(window_.get(), HTBOTTOM, {window2_.get()});
  363. ASSERT_TRUE(resizer.get());
  364. // Move it 100 to the bottom, which should expand w1 and push w2.
  365. resizer->Drag(CalculateDragPoint(*resizer, 10, 100), 0);
  366. EXPECT_EQ("0,50 400x300", window_->bounds().ToString());
  367. EXPECT_EQ("0,350 200x100", window2_->bounds().ToString());
  368. // Push off the screen, w2 should be resized to its min.
  369. delegate2_.set_min_size(gfx::Size(20, 20));
  370. resizer->Drag(CalculateDragPoint(*resizer, 50, 820), 0);
  371. EXPECT_EQ("0,50 400x530", window_->bounds().ToString());
  372. EXPECT_EQ("0,580 200x20", window2_->bounds().ToString());
  373. // Move back to 100 and verify w2 gets its original size.
  374. resizer->Drag(CalculateDragPoint(*resizer, 10, 100), 0);
  375. EXPECT_EQ("0,50 400x300", window_->bounds().ToString());
  376. EXPECT_EQ("0,350 200x100", window2_->bounds().ToString());
  377. // Revert and make sure everything moves back.
  378. resizer->Drag(CalculateDragPoint(*resizer, 800, 20), 0);
  379. resizer->RevertDrag();
  380. EXPECT_EQ("0,50 400x200", window_->bounds().ToString());
  381. EXPECT_EQ("0,250 200x100", window2_->bounds().ToString());
  382. }
  383. // Assertions around attached window resize dragging from the bottom with 3
  384. // windows.
  385. TEST_F(WorkspaceWindowResizerTest, AttachedResize_BOTTOM_3) {
  386. UpdateDisplay("600x800");
  387. aura::Window* root = Shell::GetPrimaryRootWindow();
  388. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  389. root, gfx::Rect(), gfx::Insets(), gfx::Insets());
  390. window_->SetBounds(gfx::Rect(300, 100, 300, 200));
  391. window2_->SetBounds(gfx::Rect(300, 300, 200, 150));
  392. window3_->SetBounds(gfx::Rect(300, 450, 200, 100));
  393. delegate2_.set_min_size(gfx::Size(50, 52));
  394. delegate3_.set_min_size(gfx::Size(50, 38));
  395. std::unique_ptr<WorkspaceWindowResizer> resizer =
  396. CreateWorkspaceResizerForTest(window_.get(), HTBOTTOM,
  397. {window2_.get(), window3_.get()});
  398. ASSERT_TRUE(resizer.get());
  399. // Move it 100 down, which should expand w1 and push w2 and w3.
  400. resizer->Drag(CalculateDragPoint(*resizer, -10, 100), 0);
  401. EXPECT_EQ("300,100 300x300", window_->bounds().ToString());
  402. EXPECT_EQ("300,400 200x150", window2_->bounds().ToString());
  403. EXPECT_EQ("300,550 200x100", window3_->bounds().ToString());
  404. // Move it 296 things should compress.
  405. resizer->Drag(CalculateDragPoint(*resizer, -10, 296), 0);
  406. EXPECT_EQ("300,100 300x496", window_->bounds().ToString());
  407. EXPECT_EQ("300,596 200x123", window2_->bounds().ToString());
  408. EXPECT_EQ("300,719 200x81", window3_->bounds().ToString());
  409. // Move it so much everything ends up at its min.
  410. resizer->Drag(CalculateDragPoint(*resizer, 50, 798), 0);
  411. EXPECT_EQ("300,100 300x610", window_->bounds().ToString());
  412. EXPECT_EQ("300,710 200x52", window2_->bounds().ToString());
  413. EXPECT_EQ("300,762 200x38", window3_->bounds().ToString());
  414. // Revert and make sure everything moves back.
  415. resizer->RevertDrag();
  416. EXPECT_EQ("300,100 300x200", window_->bounds().ToString());
  417. EXPECT_EQ("300,300 200x150", window2_->bounds().ToString());
  418. EXPECT_EQ("300,450 200x100", window3_->bounds().ToString());
  419. }
  420. // Assertions around attached window resizing (collapsing and expanding) with
  421. // 3 windows.
  422. TEST_F(WorkspaceWindowResizerTest, AttachedResize_BOTTOM_3_Compress) {
  423. window_->SetBounds(gfx::Rect(0, 0, 200, 200));
  424. window2_->SetBounds(gfx::Rect(10, 200, 200, 200));
  425. window3_->SetBounds(gfx::Rect(20, 400, 100, 100));
  426. delegate2_.set_min_size(gfx::Size(52, 50));
  427. delegate3_.set_min_size(gfx::Size(38, 50));
  428. std::unique_ptr<WorkspaceWindowResizer> resizer =
  429. CreateWorkspaceResizerForTest(window_.get(), HTBOTTOM,
  430. {window2_.get(), window3_.get()});
  431. ASSERT_TRUE(resizer.get());
  432. // Move it 100 up, which should collapse w1 and expand w2 and w3.
  433. resizer->Drag(CalculateDragPoint(*resizer, -10, -100), 0);
  434. EXPECT_EQ("0,0 200x100", window_->bounds().ToString());
  435. EXPECT_EQ("10,100 200x266", window2_->bounds().ToString());
  436. EXPECT_EQ("20,366 100x134", window3_->bounds().ToString());
  437. // Move it 100 down.
  438. resizer->Drag(CalculateDragPoint(*resizer, 10, 100), 0);
  439. EXPECT_EQ("0,0 200x300", window_->bounds().ToString());
  440. EXPECT_EQ("10,300 200x200", window2_->bounds().ToString());
  441. EXPECT_EQ("20,500 100x100", window3_->bounds().ToString());
  442. // 100 up again.
  443. resizer->Drag(CalculateDragPoint(*resizer, -10, -100), 0);
  444. EXPECT_EQ("0,0 200x100", window_->bounds().ToString());
  445. EXPECT_EQ("10,100 200x266", window2_->bounds().ToString());
  446. EXPECT_EQ("20,366 100x134", window3_->bounds().ToString());
  447. }
  448. // Tests that touch-dragging a window does not lock the mouse cursor
  449. // and therefore shows the cursor on a mousemove.
  450. TEST_F(WorkspaceWindowResizerTest, MouseMoveWithTouchDrag) {
  451. // Shell hides the cursor by default; show it for this tests.
  452. Shell::Get()->cursor_manager()->ShowCursor();
  453. window_->SetBounds(gfx::Rect(0, 300, 400, 300));
  454. window2_->SetBounds(gfx::Rect(400, 200, 100, 200));
  455. Shell* shell = Shell::Get();
  456. ui::test::EventGenerator generator(window_->GetRootWindow());
  457. // The cursor should not be locked initially.
  458. EXPECT_FALSE(shell->cursor_manager()->IsCursorLocked());
  459. std::vector<aura::Window*> windows;
  460. windows.push_back(window2_.get());
  461. std::unique_ptr<WorkspaceWindowResizer> resizer =
  462. CreateWorkspaceResizerForTest(window_.get(), gfx::Point(), HTRIGHT,
  463. ::wm::WINDOW_MOVE_SOURCE_TOUCH, windows);
  464. ASSERT_TRUE(resizer.get());
  465. // Creating a WorkspaceWindowResizer should not lock the cursor.
  466. EXPECT_FALSE(shell->cursor_manager()->IsCursorLocked());
  467. // The cursor should be hidden after touching the screen and
  468. // starting a drag.
  469. EXPECT_TRUE(shell->cursor_manager()->IsCursorVisible());
  470. generator.PressTouch();
  471. resizer->Drag(CalculateDragPoint(*resizer, 100, 10), 0);
  472. EXPECT_FALSE(shell->cursor_manager()->IsCursorVisible());
  473. EXPECT_FALSE(shell->cursor_manager()->IsCursorLocked());
  474. // Moving the mouse should show the cursor.
  475. generator.MoveMouseBy(1, 1);
  476. EXPECT_TRUE(shell->cursor_manager()->IsCursorVisible());
  477. EXPECT_FALSE(shell->cursor_manager()->IsCursorLocked());
  478. resizer->RevertDrag();
  479. }
  480. // Assertions around dragging near the left/right edge of the display.
  481. TEST_F(WorkspaceWindowResizerTest, Edge) {
  482. // Resize host window to force insets update.
  483. UpdateDisplay("800x700");
  484. // TODO(varkha): Insets are reset after every drag because of
  485. // http://crbug.com/292238.
  486. window_->SetBounds(gfx::Rect(20, 30, 400, 60));
  487. AllowSnap(window_.get());
  488. WindowState* window_state = WindowState::Get(window_.get());
  489. {
  490. gfx::Rect expected_bounds_in_parent(GetDefaultSnappedWindowBoundsInParent(
  491. window_.get(), SnapViewType::kPrimary));
  492. std::unique_ptr<WindowResizer> resizer =
  493. CreateResizerForTest(window_.get());
  494. ASSERT_TRUE(resizer.get());
  495. // Test tolerance of 32px for snapping.
  496. resizer->Drag(CalculateDragPoint(*resizer, 32, 10), 0);
  497. resizer->CompleteDrag();
  498. EXPECT_EQ(expected_bounds_in_parent, window_->bounds());
  499. ASSERT_TRUE(window_state->HasRestoreBounds());
  500. EXPECT_EQ(gfx::Rect(20, 30, 400, 60),
  501. window_state->GetRestoreBoundsInScreen());
  502. }
  503. // Try the same with the right side.
  504. {
  505. gfx::Rect expected_bounds_in_parent(GetDefaultSnappedWindowBoundsInParent(
  506. window_.get(), SnapViewType::kSecondary));
  507. std::unique_ptr<WindowResizer> resizer =
  508. CreateResizerForTest(window_.get());
  509. ASSERT_TRUE(resizer.get());
  510. // Test tolerance of 32px for snapping (measured from 799, not from 800).
  511. resizer->Drag(CalculateDragPoint(*resizer, 767, 10), 0);
  512. resizer->CompleteDrag();
  513. EXPECT_EQ(expected_bounds_in_parent, window_->bounds());
  514. ASSERT_TRUE(window_state->HasRestoreBounds());
  515. EXPECT_EQ(gfx::Rect(20, 30, 400, 60),
  516. window_state->GetRestoreBoundsInScreen());
  517. }
  518. }
  519. // Check that non resizable windows will not get resized.
  520. TEST_F(WorkspaceWindowResizerTest, NonResizableWindows) {
  521. window_->SetBounds(gfx::Rect(20, 30, 50, 60));
  522. window_->SetProperty(aura::client::kResizeBehaviorKey,
  523. aura::client::kResizeBehaviorNone);
  524. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  525. ASSERT_TRUE(resizer.get());
  526. resizer->Drag(CalculateDragPoint(*resizer, -20, 0), 0);
  527. resizer->CompleteDrag();
  528. EXPECT_EQ("0,30 50x60", window_->bounds().ToString());
  529. }
  530. TEST_F(WorkspaceWindowResizerTest, MultiDisplaySnapPhantom) {
  531. UpdateDisplay("800x600,800x600");
  532. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  533. ASSERT_EQ(2U, root_windows.size());
  534. window_->SetBoundsInScreen(gfx::Rect(0, 0, 50, 60),
  535. display::Screen::GetScreen()->GetPrimaryDisplay());
  536. // Make the window snappable.
  537. AllowSnap(window_.get());
  538. EXPECT_EQ(root_windows[0], window_->GetRootWindow());
  539. EXPECT_FLOAT_EQ(1.0f, window_->layer()->opacity());
  540. {
  541. std::unique_ptr<WindowResizer> resizer =
  542. CreateResizerForTest(window_.get());
  543. ASSERT_TRUE(resizer.get());
  544. EXPECT_FALSE(snap_phantom_window_controller());
  545. // The pointer is on the edge but not shared. The snap phantom window
  546. // controller should be non-NULL.
  547. resizer->Drag(CalculateDragPoint(*resizer, 799, 0), 0);
  548. EXPECT_TRUE(snap_phantom_window_controller());
  549. // Move the cursor across the edge. Now the snap phantom window controller
  550. // should still be non-null.
  551. resizer->Drag(CalculateDragPoint(*resizer, 800, 0), 0);
  552. EXPECT_TRUE(snap_phantom_window_controller());
  553. }
  554. }
  555. // Verifies that dragging a snapped window unsnaps it.
  556. TEST_F(WorkspaceWindowResizerTest, DragSnapped) {
  557. WindowState* window_state = WindowState::Get(window_.get());
  558. const gfx::Rect kInitialBounds(100, 100, 100, 100);
  559. window_->SetBounds(kInitialBounds);
  560. window_->Show();
  561. AllowSnap(window_.get());
  562. const WindowSnapWMEvent snap_event(WM_EVENT_SNAP_PRIMARY);
  563. window_state->OnWMEvent(&snap_event);
  564. EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state->GetStateType());
  565. gfx::Rect snapped_bounds = window_->bounds();
  566. EXPECT_NE(snapped_bounds.ToString(), kInitialBounds.ToString());
  567. EXPECT_EQ(kInitialBounds, window_state->GetRestoreBoundsInParent());
  568. // Dragging a side snapped window should unsnap it.
  569. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  570. resizer->Drag(CalculateDragPoint(*resizer, 33, 0), 0);
  571. resizer->CompleteDrag();
  572. EXPECT_EQ(WindowStateType::kNormal, window_state->GetStateType());
  573. EXPECT_EQ("33,0 100x100", window_->bounds().ToString());
  574. EXPECT_FALSE(window_state->HasRestoreBounds());
  575. }
  576. // Verifies the behavior of resizing a side snapped window.
  577. TEST_F(WorkspaceWindowResizerTest, ResizeSnapped) {
  578. WindowState* window_state = WindowState::Get(window_.get());
  579. AllowSnap(window_.get());
  580. const gfx::Rect kInitialBounds(100, 100, 100, 100);
  581. window_->SetBounds(kInitialBounds);
  582. window_->Show();
  583. const WindowSnapWMEvent snap_event(WM_EVENT_SNAP_PRIMARY);
  584. window_state->OnWMEvent(&snap_event);
  585. EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state->GetStateType());
  586. gfx::Rect snapped_bounds = window_->bounds();
  587. EXPECT_NE(snapped_bounds.ToString(), kInitialBounds.ToString());
  588. EXPECT_EQ(kInitialBounds, window_state->GetRestoreBoundsInParent());
  589. {
  590. // 1) Resizing a side snapped window to make it wider should not unsnap the
  591. // window.
  592. std::unique_ptr<WindowResizer> resizer =
  593. CreateResizerForTest(window_.get(), gfx::Point(), HTRIGHT);
  594. resizer->Drag(CalculateDragPoint(*resizer, 10, 0), 0);
  595. resizer->CompleteDrag();
  596. EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state->GetStateType());
  597. snapped_bounds.Inset(gfx::Insets::TLBR(0, 0, 0, -10));
  598. EXPECT_EQ(snapped_bounds.ToString(), window_->bounds().ToString());
  599. EXPECT_EQ(kInitialBounds, window_state->GetRestoreBoundsInParent());
  600. }
  601. {
  602. // 2) Resizing a side snapped window vertically and then undoing the change
  603. // should not unsnap.
  604. std::unique_ptr<WindowResizer> resizer =
  605. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOM);
  606. resizer->Drag(CalculateDragPoint(*resizer, 0, -30), 0);
  607. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  608. resizer->CompleteDrag();
  609. EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state->GetStateType());
  610. EXPECT_EQ(snapped_bounds.ToString(), window_->bounds().ToString());
  611. EXPECT_EQ(kInitialBounds, window_state->GetRestoreBoundsInParent());
  612. }
  613. {
  614. // 3) Resizing a side snapped window vertically and then not undoing the
  615. // change should unsnap.
  616. std::unique_ptr<WindowResizer> resizer =
  617. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOM);
  618. resizer->Drag(CalculateDragPoint(*resizer, 0, -10), 0);
  619. resizer->CompleteDrag();
  620. EXPECT_EQ(WindowStateType::kNormal, window_state->GetStateType());
  621. gfx::Rect expected_bounds(snapped_bounds);
  622. expected_bounds.Inset(gfx::Insets::TLBR(0, 0, 10, 0));
  623. EXPECT_EQ(expected_bounds.ToString(), window_->bounds().ToString());
  624. EXPECT_FALSE(window_state->HasRestoreBounds());
  625. }
  626. }
  627. // Verifies the behavior of resizing and restoring a side snapped window.
  628. TEST_F(WorkspaceWindowResizerTest, ResizeRestoreSnappedWindow) {
  629. WindowState* window_state = WindowState::Get(window_.get());
  630. AllowSnap(window_.get());
  631. const gfx::Rect kInitialBounds(100, 100, 100, 100);
  632. window_->SetBounds(kInitialBounds);
  633. window_->Show();
  634. // Snap the window to the left.
  635. const WindowSnapWMEvent snap_event(WM_EVENT_SNAP_PRIMARY);
  636. window_state->OnWMEvent(&snap_event);
  637. gfx::Rect snapped_bounds = window_->bounds();
  638. // Resize the snapped window to make it wider.
  639. std::unique_ptr<WindowResizer> resizer =
  640. CreateResizerForTest(window_.get(), gfx::Point(), HTRIGHT);
  641. resizer->Drag(CalculateDragPoint(*resizer, 10, 0), 0);
  642. resizer->CompleteDrag();
  643. EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state->GetStateType());
  644. snapped_bounds.Inset(gfx::Insets::TLBR(0, 0, 0, -10));
  645. EXPECT_EQ(snapped_bounds.ToString(), window_->bounds().ToString());
  646. EXPECT_EQ(kInitialBounds, window_state->GetRestoreBoundsInParent());
  647. // Minimize then restore the window and expect the resized snapped bounds to
  648. // be restored.
  649. window_state->Minimize();
  650. window_state->Restore();
  651. EXPECT_EQ(snapped_bounds.ToString(), window_->bounds().ToString());
  652. }
  653. // Verifies windows are correctly restacked when reordering multiple windows.
  654. TEST_F(WorkspaceWindowResizerTest, RestackAttached) {
  655. window_->SetBounds(gfx::Rect(0, 0, 200, 300));
  656. window2_->SetBounds(gfx::Rect(200, 0, 100, 200));
  657. window3_->SetBounds(gfx::Rect(300, 0, 100, 100));
  658. {
  659. std::unique_ptr<WorkspaceWindowResizer> resizer =
  660. CreateWorkspaceResizerForTest(window_.get(), HTRIGHT, {window2_.get()});
  661. ASSERT_TRUE(resizer.get());
  662. // Move it 100 to the right, which should expand w1 and push w2 and w3.
  663. resizer->Drag(CalculateDragPoint(*resizer, 100, -10), 0);
  664. // 2 should be topmost since it's initially the highest in the stack.
  665. const std::vector<int> expected_order = {2, 1, 3};
  666. EXPECT_EQ(expected_order, WindowOrderAsIntVector(window_->parent()));
  667. }
  668. {
  669. std::unique_ptr<WorkspaceWindowResizer> resizer =
  670. CreateWorkspaceResizerForTest(window2_.get(), HTRIGHT,
  671. {window3_.get()});
  672. ASSERT_TRUE(resizer.get());
  673. // Move it 100 to the right, which should expand w1 and push w2 and w3.
  674. resizer->Drag(CalculateDragPoint(*resizer, 100, -10), 0);
  675. // 2 should be topmost since it's initially the highest in the stack.
  676. const std::vector<int> expected_order = {2, 3, 1};
  677. EXPECT_EQ(expected_order, WindowOrderAsIntVector(window_->parent()));
  678. }
  679. }
  680. // Makes sure we don't allow dragging below the work area.
  681. TEST_F(WorkspaceWindowResizerTest, DontDragOffBottom) {
  682. aura::Window* root = Shell::GetPrimaryRootWindow();
  683. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  684. root, gfx::Rect(), gfx::Insets::TLBR(0, 0, 10, 0),
  685. gfx::Insets::TLBR(0, 0, 10, 0));
  686. ASSERT_EQ(1, display::Screen::GetScreen()->GetNumDisplays());
  687. window_->SetBounds(gfx::Rect(100, 200, 300, 400));
  688. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  689. ASSERT_TRUE(resizer.get());
  690. resizer->Drag(CalculateDragPoint(*resizer, 0, 600), 0);
  691. int expected_y =
  692. kRootHeight - WorkspaceWindowResizer::kMinOnscreenHeight - 10;
  693. EXPECT_EQ(gfx::Rect(100, expected_y, 300, 400), window_->bounds());
  694. }
  695. // Makes sure we don't allow dragging on the work area with multidisplay.
  696. TEST_F(WorkspaceWindowResizerTest, DontDragOffBottomWithMultiDisplay) {
  697. UpdateDisplay("800x600,800x600");
  698. ASSERT_EQ(2, display::Screen::GetScreen()->GetNumDisplays());
  699. aura::Window* root = Shell::GetPrimaryRootWindow();
  700. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  701. root, gfx::Rect(), gfx::Insets::TLBR(0, 0, 10, 0),
  702. gfx::Insets::TLBR(0, 0, 10, 0));
  703. // Positions the secondary display at the bottom the primary display.
  704. Shell::Get()->display_manager()->SetLayoutForCurrentDisplays(
  705. display::test::CreateDisplayLayout(display_manager(),
  706. display::DisplayPlacement::BOTTOM, 0));
  707. {
  708. window_->SetBounds(gfx::Rect(100, 200, 300, 20));
  709. DCHECK_LT(window_->bounds().height(),
  710. WorkspaceWindowResizer::kMinOnscreenHeight);
  711. // Drag down avoiding dragging along the edge as that would side-snap.
  712. std::unique_ptr<WindowResizer> resizer =
  713. CreateResizerForTest(window_.get(), gfx::Point(10, 0), HTCAPTION);
  714. ASSERT_TRUE(resizer.get());
  715. resizer->Drag(CalculateDragPoint(*resizer, 0, 400), 0);
  716. int expected_y = kRootHeight - window_->bounds().height() - 10;
  717. // When the mouse cursor is in the primary display, the window cannot move
  718. // on non-work area but can get all the way towards the bottom,
  719. // restricted only by the window height.
  720. EXPECT_EQ(gfx::Rect(100, expected_y, 300, 20), window_->bounds());
  721. // Revert the drag in order to not remember the restore bounds.
  722. resizer->RevertDrag();
  723. }
  724. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  725. root, gfx::Rect(), gfx::Insets::TLBR(0, 0, 10, 0),
  726. gfx::Insets::TLBR(0, 0, 10, 0));
  727. {
  728. window_->SetBounds(gfx::Rect(100, 200, 300, 400));
  729. std::unique_ptr<WindowResizer> resizer =
  730. CreateResizerForTest(window_.get(), gfx::Point(10, 0), HTCAPTION);
  731. ASSERT_TRUE(resizer.get());
  732. // Drag down avoiding dragging along the edge as that would side-snap.
  733. resizer->Drag(CalculateDragPoint(*resizer, 0, 400), 0);
  734. int expected_y =
  735. kRootHeight - WorkspaceWindowResizer::kMinOnscreenHeight - 10;
  736. // When the mouse cursor is in the primary display, the window cannot move
  737. // on non-work area with kMinOnscreenHeight margin.
  738. EXPECT_EQ(gfx::Rect(100, expected_y, 300, 400), window_->bounds());
  739. resizer->CompleteDrag();
  740. }
  741. {
  742. window_->SetBounds(gfx::Rect(100, 200, 300, 400));
  743. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(
  744. window_.get(), window_->bounds().origin(), HTCAPTION);
  745. ASSERT_TRUE(resizer.get());
  746. // Drag down avoiding getting stuck against the shelf on the bottom screen.
  747. resizer->Drag(CalculateDragPoint(*resizer, 0, 500), 0);
  748. // The window can move to the secondary display beyond non-work area of
  749. // the primary display.
  750. EXPECT_EQ("100,700 300x400", window_->bounds().ToString());
  751. resizer->CompleteDrag();
  752. }
  753. }
  754. // Makes sure we don't allow dragging off the top of the work area.
  755. TEST_F(WorkspaceWindowResizerTest, DontDragOffTop) {
  756. aura::Window* root = Shell::GetPrimaryRootWindow();
  757. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  758. root, gfx::Rect(), gfx::Insets::TLBR(10, 0, 0, 0),
  759. gfx::Insets::TLBR(10, 0, 0, 0));
  760. window_->SetBounds(gfx::Rect(100, 200, 300, 400));
  761. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  762. ASSERT_TRUE(resizer.get());
  763. resizer->Drag(CalculateDragPoint(*resizer, 0, -600), 0);
  764. EXPECT_EQ("100,10 300x400", window_->bounds().ToString());
  765. }
  766. TEST_F(WorkspaceWindowResizerTest, ResizeBottomOutsideWorkArea) {
  767. aura::Window* root = Shell::GetPrimaryRootWindow();
  768. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  769. root, gfx::Rect(), gfx::Insets::TLBR(0, 0, 50, 0),
  770. gfx::Insets::TLBR(0, 0, 50, 0));
  771. window_->SetBounds(gfx::Rect(100, 200, 300, 380));
  772. std::unique_ptr<WindowResizer> resizer =
  773. CreateResizerForTest(window_.get(), gfx::Point(), HTTOP);
  774. ASSERT_TRUE(resizer.get());
  775. resizer->Drag(CalculateDragPoint(*resizer, 8, 0), 0);
  776. EXPECT_EQ("100,200 300x380", window_->bounds().ToString());
  777. }
  778. TEST_F(WorkspaceWindowResizerTest, ResizeWindowOutsideLeftWorkArea) {
  779. aura::Window* root = Shell::GetPrimaryRootWindow();
  780. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  781. root, gfx::Rect(), gfx::Insets::TLBR(0, 0, 50, 0),
  782. gfx::Insets::TLBR(0, 0, 50, 0));
  783. int left = screen_util::GetDisplayWorkAreaBoundsInParent(window_.get()).x();
  784. int pixels_to_left_border = 50;
  785. int window_width = 300;
  786. int window_x = left - window_width + pixels_to_left_border;
  787. window_->SetBounds(gfx::Rect(window_x, 100, window_width, 380));
  788. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(
  789. window_.get(), gfx::Point(pixels_to_left_border, 0), HTRIGHT);
  790. ASSERT_TRUE(resizer.get());
  791. resizer->Drag(CalculateDragPoint(*resizer, -window_width, 0), 0);
  792. EXPECT_EQ(gfx::Rect(window_x, 100, kMinimumOnScreenArea - window_x, 380),
  793. window_->bounds());
  794. }
  795. TEST_F(WorkspaceWindowResizerTest, ResizeWindowOutsideRightWorkArea) {
  796. aura::Window* root = Shell::GetPrimaryRootWindow();
  797. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  798. root, gfx::Rect(), gfx::Insets::TLBR(0, 0, 50, 0),
  799. gfx::Insets::TLBR(0, 0, 50, 0));
  800. int right =
  801. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get()).right();
  802. int pixels_to_right_border = 50;
  803. int window_width = 300;
  804. int window_x = right - pixels_to_right_border;
  805. window_->SetBounds(gfx::Rect(window_x, 100, window_width, 380));
  806. std::unique_ptr<WindowResizer> resizer =
  807. CreateResizerForTest(window_.get(), gfx::Point(window_x, 0), HTLEFT);
  808. ASSERT_TRUE(resizer.get());
  809. resizer->Drag(CalculateDragPoint(*resizer, window_width, 0), 0);
  810. EXPECT_EQ(
  811. gfx::Rect(right - kMinimumOnScreenArea, 100,
  812. window_width - pixels_to_right_border + kMinimumOnScreenArea,
  813. 380),
  814. window_->bounds());
  815. }
  816. TEST_F(WorkspaceWindowResizerTest, ResizeWindowOutsideBottomWorkArea) {
  817. aura::Window* root = Shell::GetPrimaryRootWindow();
  818. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  819. root, gfx::Rect(), gfx::Insets::TLBR(0, 0, 50, 0),
  820. gfx::Insets::TLBR(0, 0, 50, 0));
  821. int bottom =
  822. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get()).bottom();
  823. int delta_to_bottom = 50;
  824. int height = 380;
  825. window_->SetBounds(gfx::Rect(100, bottom - delta_to_bottom, 300, height));
  826. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(
  827. window_.get(), gfx::Point(0, bottom - delta_to_bottom), HTTOP);
  828. ASSERT_TRUE(resizer.get());
  829. resizer->Drag(CalculateDragPoint(*resizer, 0, bottom), 0);
  830. EXPECT_EQ(gfx::Rect(100, bottom - kMinimumOnScreenArea, 300,
  831. height - (delta_to_bottom - kMinimumOnScreenArea)),
  832. window_->bounds());
  833. }
  834. // Verifies that 'outside' check of the resizer take into account the extended
  835. // desktop in case of repositions.
  836. TEST_F(WorkspaceWindowResizerTest, DragWindowOutsideRightToSecondaryDisplay) {
  837. // Only primary display. Changes the window position to fit within the
  838. // display.
  839. aura::Window* root = Shell::GetPrimaryRootWindow();
  840. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  841. root, gfx::Rect(), gfx::Insets::TLBR(0, 0, 50, 0),
  842. gfx::Insets::TLBR(0, 0, 50, 0));
  843. int right =
  844. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get()).right();
  845. int pixels_to_right_border = 50;
  846. int window_width = 300;
  847. int window_x = right - pixels_to_right_border;
  848. window_->SetBounds(gfx::Rect(window_x, 100, window_width, 380));
  849. std::unique_ptr<WindowResizer> resizer =
  850. CreateResizerForTest(window_.get(), gfx::Point(window_x, 0), HTCAPTION);
  851. ASSERT_TRUE(resizer.get());
  852. resizer->Drag(CalculateDragPoint(*resizer, window_width, 0), 0);
  853. EXPECT_EQ(gfx::Rect(right - kMinimumOnScreenArea, 100, window_width, 380),
  854. window_->bounds());
  855. // With secondary display. Operation itself is same but doesn't change
  856. // the position because the window is still within the secondary display.
  857. UpdateDisplay("1000x600,600x400");
  858. root = Shell::GetPrimaryRootWindow();
  859. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  860. root, gfx::Rect(), gfx::Insets::TLBR(0, 0, 50, 0),
  861. gfx::Insets::TLBR(0, 0, 50, 0));
  862. window_->SetBounds(gfx::Rect(window_x, 100, window_width, 380));
  863. resizer->Drag(CalculateDragPoint(*resizer, window_width, 0), 0);
  864. EXPECT_EQ(gfx::Rect(window_x + window_width, 100, window_width, 380),
  865. window_->bounds());
  866. }
  867. // Verifies snapping to edges works.
  868. TEST_F(WorkspaceWindowResizerTest, SnapToEdge) {
  869. GetPrimaryShelf()->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlways);
  870. window_->SetBounds(gfx::Rect(96, 112, 320, 160));
  871. // Click 50px to the right so that the mouse pointer does not leave the
  872. // workspace ensuring sticky behavior.
  873. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(
  874. window_.get(), window_->bounds().origin() + gfx::Vector2d(50, 0),
  875. HTCAPTION);
  876. ASSERT_TRUE(resizer.get());
  877. int distance_to_left = window_->bounds().x();
  878. int distance_to_right =
  879. 800 - window_->bounds().width() - window_->bounds().x();
  880. int distance_to_bottom =
  881. 600 - window_->bounds().height() - window_->bounds().y();
  882. int distance_to_top = window_->bounds().y();
  883. // Test left side.
  884. // Move to an x-coordinate of 15, which should not snap.
  885. resizer->Drag(CalculateDragPoint(*resizer, 15 - distance_to_left, 0), 0);
  886. // An x-coordinate of 7 should snap.
  887. resizer->Drag(CalculateDragPoint(*resizer, 7 - distance_to_left, 0), 0);
  888. EXPECT_EQ("0,112 320x160", window_->bounds().ToString());
  889. // Move to -15, should still snap to 0.
  890. resizer->Drag(CalculateDragPoint(*resizer, -15 - distance_to_left, 0), 0);
  891. EXPECT_EQ("0,112 320x160", window_->bounds().ToString());
  892. // At -32 should move past snap points.
  893. resizer->Drag(CalculateDragPoint(*resizer, -32 - distance_to_left, 0), 0);
  894. EXPECT_EQ("-32,112 320x160", window_->bounds().ToString());
  895. resizer->Drag(CalculateDragPoint(*resizer, -33 - distance_to_left, 0), 0);
  896. EXPECT_EQ("-33,112 320x160", window_->bounds().ToString());
  897. // Right side should similarly snap.
  898. resizer->Drag(CalculateDragPoint(*resizer, distance_to_right - 15, 0), 0);
  899. EXPECT_EQ("465,112 320x160", window_->bounds().ToString());
  900. resizer->Drag(CalculateDragPoint(*resizer, distance_to_right - 7, 0), 0);
  901. EXPECT_EQ("480,112 320x160", window_->bounds().ToString());
  902. resizer->Drag(CalculateDragPoint(*resizer, distance_to_right + 15, 0), 0);
  903. EXPECT_EQ("480,112 320x160", window_->bounds().ToString());
  904. resizer->Drag(CalculateDragPoint(*resizer, distance_to_right + 32, 0), 0);
  905. EXPECT_EQ("512,112 320x160", window_->bounds().ToString());
  906. resizer->Drag(CalculateDragPoint(*resizer, distance_to_right + 33, 0), 0);
  907. EXPECT_EQ("513,112 320x160", window_->bounds().ToString());
  908. // And the bottom should snap too.
  909. resizer->Drag(CalculateDragPoint(*resizer, 0, distance_to_bottom - 7), 0);
  910. EXPECT_EQ(gfx::Rect(96, 440, 320, 160), window_->bounds());
  911. resizer->Drag(CalculateDragPoint(*resizer, 0, distance_to_bottom + 15), 0);
  912. EXPECT_EQ(gfx::Rect(96, 440, 320, 160), window_->bounds());
  913. resizer->Drag(CalculateDragPoint(*resizer, 0, distance_to_bottom - 2 + 32),
  914. 0);
  915. EXPECT_EQ("96,470 320x160", window_->bounds().ToString());
  916. resizer->Drag(CalculateDragPoint(*resizer, 0, distance_to_bottom - 2 + 33),
  917. 0);
  918. EXPECT_EQ("96,471 320x160", window_->bounds().ToString());
  919. // And the top should snap too.
  920. resizer->Drag(CalculateDragPoint(*resizer, 0, -distance_to_top + 20), 0);
  921. EXPECT_EQ("96,20 320x160", window_->bounds().ToString());
  922. resizer->Drag(CalculateDragPoint(*resizer, 0, -distance_to_top + 7), 0);
  923. EXPECT_EQ("96,0 320x160", window_->bounds().ToString());
  924. // And bottom/left should snap too.
  925. resizer->Drag(CalculateDragPoint(*resizer, 7 - distance_to_left,
  926. distance_to_bottom - 7),
  927. 0);
  928. EXPECT_EQ(gfx::Rect(0, 440, 320, 160), window_->bounds());
  929. resizer->Drag(CalculateDragPoint(*resizer, -15 - distance_to_left,
  930. distance_to_bottom + 15),
  931. 0);
  932. EXPECT_EQ(gfx::Rect(0, 440, 320, 160), window_->bounds());
  933. // should move past snap points.
  934. resizer->Drag(CalculateDragPoint(*resizer, -32 - distance_to_left,
  935. distance_to_bottom - 2 + 32),
  936. 0);
  937. EXPECT_EQ("-32,470 320x160", window_->bounds().ToString());
  938. resizer->Drag(CalculateDragPoint(*resizer, -33 - distance_to_left,
  939. distance_to_bottom - 2 + 33),
  940. 0);
  941. EXPECT_EQ("-33,471 320x160", window_->bounds().ToString());
  942. // No need to test dragging < 0 as we force that to 0.
  943. }
  944. // Verifies a resize snap when dragging TOPLEFT.
  945. TEST_F(WorkspaceWindowResizerTest, SnapToWorkArea_TOPLEFT) {
  946. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  947. std::unique_ptr<WindowResizer> resizer =
  948. CreateResizerForTest(window_.get(), gfx::Point(), HTTOPLEFT);
  949. ASSERT_TRUE(resizer.get());
  950. resizer->Drag(CalculateDragPoint(*resizer, -98, -199), 0);
  951. EXPECT_EQ("0,0 120x230", window_->bounds().ToString());
  952. }
  953. // Verifies a resize snap when dragging TOPRIGHT.
  954. TEST_F(WorkspaceWindowResizerTest, SnapToWorkArea_TOPRIGHT) {
  955. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  956. gfx::Rect work_area(
  957. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get()));
  958. std::unique_ptr<WindowResizer> resizer =
  959. CreateResizerForTest(window_.get(), gfx::Point(), HTTOPRIGHT);
  960. ASSERT_TRUE(resizer.get());
  961. resizer->Drag(CalculateDragPoint(*resizer, work_area.right() - 120 - 1, -199),
  962. 0);
  963. EXPECT_EQ(100, window_->bounds().x());
  964. EXPECT_EQ(work_area.y(), window_->bounds().y());
  965. EXPECT_EQ(work_area.right() - 100, window_->bounds().width());
  966. EXPECT_EQ(230, window_->bounds().height());
  967. }
  968. // Verifies a resize snap when dragging BOTTOMRIGHT.
  969. TEST_F(WorkspaceWindowResizerTest, SnapToWorkArea_BOTTOMRIGHT) {
  970. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  971. gfx::Rect work_area(
  972. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get()));
  973. std::unique_ptr<WindowResizer> resizer =
  974. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOMRIGHT);
  975. ASSERT_TRUE(resizer.get());
  976. resizer->Drag(CalculateDragPoint(*resizer, work_area.right() - 120 - 1,
  977. work_area.bottom() - 220 - 2),
  978. 0);
  979. EXPECT_EQ(100, window_->bounds().x());
  980. EXPECT_EQ(200, window_->bounds().y());
  981. EXPECT_EQ(work_area.right() - 100, window_->bounds().width());
  982. EXPECT_EQ(work_area.bottom() - 200, window_->bounds().height());
  983. }
  984. // Verifies a resize snap when dragging BOTTOMLEFT.
  985. TEST_F(WorkspaceWindowResizerTest, SnapToWorkArea_BOTTOMLEFT) {
  986. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  987. gfx::Rect work_area(
  988. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get()));
  989. std::unique_ptr<WindowResizer> resizer =
  990. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOMLEFT);
  991. ASSERT_TRUE(resizer.get());
  992. resizer->Drag(CalculateDragPoint(*resizer, -98, work_area.bottom() - 220 - 2),
  993. 0);
  994. EXPECT_EQ(0, window_->bounds().x());
  995. EXPECT_EQ(200, window_->bounds().y());
  996. EXPECT_EQ(120, window_->bounds().width());
  997. EXPECT_EQ(work_area.bottom() - 200, window_->bounds().height());
  998. }
  999. // Verifies window sticks to both window and work area.
  1000. TEST_F(WorkspaceWindowResizerTest, StickToBothEdgeAndWindow) {
  1001. window_->SetBounds(gfx::Rect(10, 10, 20, 50));
  1002. window_->Show();
  1003. window2_->SetBounds(gfx::Rect(150, 160, 25, 1000));
  1004. window2_->Show();
  1005. std::unique_ptr<WindowResizer> resizer =
  1006. CreateResizerForTest(window_.get(), gfx::Point(10, 10), HTCAPTION);
  1007. ASSERT_TRUE(resizer.get());
  1008. // Move |window| one pixel to the left of |window2|. Should snap to right.
  1009. resizer->Drag(CalculateDragPoint(*resizer, 119, 145), 0);
  1010. gfx::Rect expected(130, 160, 20, 50);
  1011. EXPECT_EQ(expected.ToString(), window_->bounds().ToString());
  1012. gfx::Rect work_area(
  1013. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get()));
  1014. // The initial y position of |window_|.
  1015. int initial_y = 10;
  1016. // The drag position where the window is exactly attached to the bottom.
  1017. int attach_y = work_area.bottom() - window_->bounds().height() - initial_y;
  1018. // Dragging 10px above should not attach to the bottom.
  1019. resizer->Drag(CalculateDragPoint(*resizer, 119, attach_y - 10), 0);
  1020. expected.set_y(attach_y + initial_y - 10);
  1021. EXPECT_EQ(expected.ToString(), window_->bounds().ToString());
  1022. // Stick to the work area.
  1023. resizer->Drag(CalculateDragPoint(*resizer, 119, attach_y - 1), 0);
  1024. expected.set_y(attach_y + initial_y);
  1025. EXPECT_EQ(expected.ToString(), window_->bounds().ToString());
  1026. resizer->Drag(CalculateDragPoint(*resizer, 119, attach_y), 0);
  1027. expected.set_y(attach_y + initial_y);
  1028. EXPECT_EQ(expected.ToString(), window_->bounds().ToString());
  1029. resizer->Drag(CalculateDragPoint(*resizer, 119, attach_y + 1), 0);
  1030. expected.set_y(attach_y + initial_y);
  1031. EXPECT_EQ(expected.ToString(), window_->bounds().ToString());
  1032. // Moving down further should move the window.
  1033. resizer->Drag(CalculateDragPoint(*resizer, 119, attach_y + 18), 0);
  1034. expected.set_y(attach_y + initial_y + 18);
  1035. EXPECT_EQ(expected.ToString(), window_->bounds().ToString());
  1036. }
  1037. TEST_F(WorkspaceWindowResizerTest, CtrlDragResizeToExactPosition) {
  1038. window_->SetBounds(gfx::Rect(96, 112, 320, 160));
  1039. std::unique_ptr<WindowResizer> resizer =
  1040. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOMRIGHT);
  1041. ASSERT_TRUE(resizer.get());
  1042. // Resize the right bottom to add 10 in width, 12 in height.
  1043. resizer->Drag(CalculateDragPoint(*resizer, 10, 12), ui::EF_CONTROL_DOWN);
  1044. // Both bottom and right sides to resize to exact size requested.
  1045. EXPECT_EQ("96,112 330x172", window_->bounds().ToString());
  1046. }
  1047. // Verifies that a dragged, non-snapped window will clear restore bounds.
  1048. TEST_F(WorkspaceWindowResizerTest, RestoreClearedOnResize) {
  1049. window_->SetBounds(gfx::Rect(10, 10, 100, 100));
  1050. WindowState* window_state = WindowState::Get(window_.get());
  1051. window_state->SetRestoreBoundsInScreen(gfx::Rect(50, 50, 50, 50));
  1052. std::unique_ptr<WindowResizer> resizer =
  1053. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOMRIGHT);
  1054. ASSERT_TRUE(resizer.get());
  1055. // Drag the window to new position by adding (20, 30) to original point,
  1056. // the original restore bound should be cleared.
  1057. resizer->Drag(CalculateDragPoint(*resizer, 20, 30), 0);
  1058. resizer->CompleteDrag();
  1059. EXPECT_EQ("10,10 120x130", window_->bounds().ToString());
  1060. EXPECT_FALSE(window_state->HasRestoreBounds());
  1061. }
  1062. // Verifies that a dragged window will restore to its pre-maximized size.
  1063. TEST_F(WorkspaceWindowResizerTest, RestoreToPreMaximizeCoordinates) {
  1064. window_->SetBounds(gfx::Rect(0, 0, 1000, 1000));
  1065. WindowState* window_state = WindowState::Get(window_.get());
  1066. window_state->SetRestoreBoundsInScreen(gfx::Rect(96, 112, 320, 160));
  1067. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  1068. ASSERT_TRUE(resizer.get());
  1069. // Drag the window to new position by adding (33, 33) to original point,
  1070. // the window should get restored.
  1071. resizer->Drag(CalculateDragPoint(*resizer, 33, 33), 0);
  1072. resizer->CompleteDrag();
  1073. EXPECT_EQ("33,33 320x160", window_->bounds().ToString());
  1074. // The restore rectangle should get cleared as well.
  1075. EXPECT_FALSE(window_state->HasRestoreBounds());
  1076. }
  1077. // Verifies that a dragged window will restore to its pre-maximized size.
  1078. TEST_F(WorkspaceWindowResizerTest, RevertResizeOperation) {
  1079. const gfx::Rect initial_bounds(0, 0, 200, 400);
  1080. window_->SetBounds(initial_bounds);
  1081. WindowState* window_state = WindowState::Get(window_.get());
  1082. window_state->SetRestoreBoundsInScreen(gfx::Rect(96, 112, 320, 160));
  1083. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  1084. ASSERT_TRUE(resizer.get());
  1085. // Drag the window to new poistion by adding (180, 16) to original point,
  1086. // the window should get restored.
  1087. resizer->Drag(CalculateDragPoint(*resizer, 180, 16), 0);
  1088. resizer->RevertDrag();
  1089. EXPECT_EQ(initial_bounds.ToString(), window_->bounds().ToString());
  1090. EXPECT_EQ(gfx::Rect(96, 112, 320, 160),
  1091. window_state->GetRestoreBoundsInScreen());
  1092. }
  1093. // Check that only usable sizes get returned by the resizer.
  1094. TEST_F(WorkspaceWindowResizerTest, MagneticallyAttach) {
  1095. window_->SetBounds(gfx::Rect(10, 10, 20, 30));
  1096. window2_->SetBounds(gfx::Rect(150, 160, 25, 20));
  1097. window2_->Show();
  1098. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  1099. ASSERT_TRUE(resizer.get());
  1100. // Move |window| one pixel to the left of |window2|. Should snap to right and
  1101. // top.
  1102. resizer->Drag(CalculateDragPoint(*resizer, 119, 145), 0);
  1103. EXPECT_EQ("130,160 20x30", window_->bounds().ToString());
  1104. // Move |window| one pixel to the right of |window2|. Should snap to left and
  1105. // top.
  1106. resizer->Drag(CalculateDragPoint(*resizer, 164, 145), 0);
  1107. EXPECT_EQ("175,160 20x30", window_->bounds().ToString());
  1108. // Move |window| one pixel above |window2|. Should snap to top and left.
  1109. resizer->Drag(CalculateDragPoint(*resizer, 142, 119), 0);
  1110. EXPECT_EQ("150,130 20x30", window_->bounds().ToString());
  1111. // Move |window| one pixel above the bottom of |window2|. Should snap to
  1112. // bottom and left.
  1113. resizer->Drag(CalculateDragPoint(*resizer, 142, 169), 0);
  1114. EXPECT_EQ("150,180 20x30", window_->bounds().ToString());
  1115. }
  1116. // The following variants verify magnetic snapping during resize when dragging a
  1117. // particular edge.
  1118. TEST_F(WorkspaceWindowResizerTest, MagneticallyResize_TOP) {
  1119. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  1120. window2_->SetBounds(gfx::Rect(99, 179, 10, 20));
  1121. window2_->Show();
  1122. std::unique_ptr<WindowResizer> resizer =
  1123. CreateResizerForTest(window_.get(), gfx::Point(), HTTOP);
  1124. ASSERT_TRUE(resizer.get());
  1125. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1126. EXPECT_EQ("100,199 20x31", window_->bounds().ToString());
  1127. }
  1128. // Resize window to the top edge of display should not trigger maximize nor
  1129. // the maximize dwell timer (crbug.com/1251859)
  1130. TEST_F(WorkspaceWindowResizerTest, ResizeTopShouldNotTriggerMaximize) {
  1131. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  1132. AllowSnap(window_.get());
  1133. std::unique_ptr<WindowResizer> resizer =
  1134. CreateResizerForTest(window_.get(), gfx::Point(), HTTOP);
  1135. ASSERT_TRUE(resizer.get());
  1136. resizer->Drag(CalculateDragPoint(*resizer, 50, -195), 0);
  1137. resizer->Drag(CalculateDragPoint(*resizer, 50, -200), 0);
  1138. ASSERT_TRUE(WindowState::Get(window_.get())->IsNormalStateType());
  1139. EXPECT_EQ("100,0 20x230", window_->bounds().ToString());
  1140. EXPECT_FALSE(snap_phantom_window_controller());
  1141. EXPECT_FALSE(IsDwellCountdownTimerRunning());
  1142. }
  1143. TEST_F(WorkspaceWindowResizerTest, MagneticallyResize_TOPLEFT) {
  1144. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  1145. window2_->SetBounds(gfx::Rect(99, 179, 10, 20));
  1146. window2_->Show();
  1147. {
  1148. std::unique_ptr<WindowResizer> resizer =
  1149. CreateResizerForTest(window_.get(), gfx::Point(), HTTOPLEFT);
  1150. ASSERT_TRUE(resizer.get());
  1151. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1152. EXPECT_EQ("99,199 21x31", window_->bounds().ToString());
  1153. resizer->RevertDrag();
  1154. }
  1155. {
  1156. window2_->SetBounds(gfx::Rect(88, 201, 10, 20));
  1157. std::unique_ptr<WindowResizer> resizer =
  1158. CreateResizerForTest(window_.get(), gfx::Point(), HTTOPLEFT);
  1159. ASSERT_TRUE(resizer.get());
  1160. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1161. EXPECT_EQ("98,201 22x29", window_->bounds().ToString());
  1162. resizer->RevertDrag();
  1163. }
  1164. }
  1165. TEST_F(WorkspaceWindowResizerTest, MagneticallyResize_TOPRIGHT) {
  1166. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  1167. window2_->Show();
  1168. {
  1169. window2_->SetBounds(gfx::Rect(111, 179, 10, 20));
  1170. std::unique_ptr<WindowResizer> resizer =
  1171. CreateResizerForTest(window_.get(), gfx::Point(), HTTOPRIGHT);
  1172. ASSERT_TRUE(resizer.get());
  1173. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1174. EXPECT_EQ("100,199 21x31", window_->bounds().ToString());
  1175. resizer->RevertDrag();
  1176. }
  1177. {
  1178. window2_->SetBounds(gfx::Rect(121, 199, 10, 20));
  1179. std::unique_ptr<WindowResizer> resizer =
  1180. CreateResizerForTest(window_.get(), gfx::Point(), HTTOPRIGHT);
  1181. ASSERT_TRUE(resizer.get());
  1182. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1183. EXPECT_EQ("100,199 21x31", window_->bounds().ToString());
  1184. resizer->RevertDrag();
  1185. }
  1186. }
  1187. TEST_F(WorkspaceWindowResizerTest, MagneticallyResize_RIGHT) {
  1188. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  1189. window2_->SetBounds(gfx::Rect(121, 199, 10, 20));
  1190. window2_->Show();
  1191. std::unique_ptr<WindowResizer> resizer =
  1192. CreateResizerForTest(window_.get(), gfx::Point(), HTRIGHT);
  1193. ASSERT_TRUE(resizer.get());
  1194. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1195. EXPECT_EQ("100,200 21x30", window_->bounds().ToString());
  1196. }
  1197. TEST_F(WorkspaceWindowResizerTest, MagneticallyResize_BOTTOMRIGHT) {
  1198. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  1199. window2_->Show();
  1200. {
  1201. window2_->SetBounds(gfx::Rect(122, 212, 10, 20));
  1202. std::unique_ptr<WindowResizer> resizer =
  1203. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOMRIGHT);
  1204. ASSERT_TRUE(resizer.get());
  1205. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1206. EXPECT_EQ("100,200 22x32", window_->bounds().ToString());
  1207. resizer->RevertDrag();
  1208. }
  1209. {
  1210. window2_->SetBounds(gfx::Rect(111, 233, 10, 20));
  1211. std::unique_ptr<WindowResizer> resizer =
  1212. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOMRIGHT);
  1213. ASSERT_TRUE(resizer.get());
  1214. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1215. EXPECT_EQ("100,200 21x33", window_->bounds().ToString());
  1216. resizer->RevertDrag();
  1217. }
  1218. }
  1219. TEST_F(WorkspaceWindowResizerTest, MagneticallyResize_BOTTOM) {
  1220. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  1221. window2_->SetBounds(gfx::Rect(111, 233, 10, 20));
  1222. window2_->Show();
  1223. std::unique_ptr<WindowResizer> resizer =
  1224. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOM);
  1225. ASSERT_TRUE(resizer.get());
  1226. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1227. EXPECT_EQ("100,200 20x33", window_->bounds().ToString());
  1228. }
  1229. TEST_F(WorkspaceWindowResizerTest, MagneticallyResize_BOTTOMLEFT) {
  1230. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  1231. window2_->Show();
  1232. {
  1233. window2_->SetBounds(gfx::Rect(99, 231, 10, 20));
  1234. std::unique_ptr<WindowResizer> resizer =
  1235. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOMLEFT);
  1236. ASSERT_TRUE(resizer.get());
  1237. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1238. EXPECT_EQ("99,200 21x31", window_->bounds().ToString());
  1239. resizer->RevertDrag();
  1240. }
  1241. {
  1242. window2_->SetBounds(gfx::Rect(89, 209, 10, 20));
  1243. std::unique_ptr<WindowResizer> resizer =
  1244. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOMLEFT);
  1245. ASSERT_TRUE(resizer.get());
  1246. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1247. EXPECT_EQ("99,200 21x29", window_->bounds().ToString());
  1248. resizer->RevertDrag();
  1249. }
  1250. }
  1251. TEST_F(WorkspaceWindowResizerTest, MagneticallyResize_LEFT) {
  1252. window2_->SetBounds(gfx::Rect(89, 209, 10, 20));
  1253. window_->SetBounds(gfx::Rect(100, 200, 20, 30));
  1254. window2_->Show();
  1255. std::unique_ptr<WindowResizer> resizer =
  1256. CreateResizerForTest(window_.get(), gfx::Point(), HTLEFT);
  1257. ASSERT_TRUE(resizer.get());
  1258. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  1259. EXPECT_EQ("99,200 21x30", window_->bounds().ToString());
  1260. }
  1261. // Test that the user moved window flag is getting properly set.
  1262. TEST_F(WorkspaceWindowResizerTest, CheckUserWindowManagedFlags) {
  1263. window_->SetBounds(gfx::Rect(0, 50, 400, 200));
  1264. window_->SetProperty(aura::client::kResizeBehaviorKey,
  1265. aura::client::kResizeBehaviorCanMaximize);
  1266. std::vector<aura::Window*> no_attached_windows;
  1267. // Check that an abort doesn't change anything.
  1268. {
  1269. std::unique_ptr<WindowResizer> resizer =
  1270. CreateResizerForTest(window_.get());
  1271. ASSERT_TRUE(resizer.get());
  1272. // Move it 100 to the bottom.
  1273. resizer->Drag(CalculateDragPoint(*resizer, 0, 100), 0);
  1274. EXPECT_EQ("0,150 400x200", window_->bounds().ToString());
  1275. resizer->RevertDrag();
  1276. EXPECT_FALSE(WindowState::Get(window_.get())->bounds_changed_by_user());
  1277. }
  1278. // Check that a completed move / size does change the user coordinates.
  1279. {
  1280. std::unique_ptr<WindowResizer> resizer =
  1281. CreateResizerForTest(window_.get());
  1282. ASSERT_TRUE(resizer.get());
  1283. // Move it 100 to the bottom.
  1284. resizer->Drag(CalculateDragPoint(*resizer, 0, 100), 0);
  1285. EXPECT_EQ("0,150 400x200", window_->bounds().ToString());
  1286. resizer->CompleteDrag();
  1287. EXPECT_TRUE(WindowState::Get(window_.get())->bounds_changed_by_user());
  1288. }
  1289. }
  1290. // Test that a window with a specified max size doesn't exceed it when dragged.
  1291. TEST_F(WorkspaceWindowResizerTest, TestMaxSizeEnforced) {
  1292. window_->SetBounds(gfx::Rect(0, 0, 400, 300));
  1293. delegate_.set_max_size(gfx::Size(401, 301));
  1294. std::unique_ptr<WindowResizer> resizer =
  1295. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOMRIGHT);
  1296. resizer->Drag(CalculateDragPoint(*resizer, 2, 2), 0);
  1297. EXPECT_EQ(401, window_->bounds().width());
  1298. EXPECT_EQ(301, window_->bounds().height());
  1299. }
  1300. // Test that a window with a specified max width doesn't restrict its height.
  1301. TEST_F(WorkspaceWindowResizerTest, TestPartialMaxSizeEnforced) {
  1302. window_->SetBounds(gfx::Rect(0, 0, 400, 300));
  1303. delegate_.set_max_size(gfx::Size(401, 0));
  1304. std::unique_ptr<WindowResizer> resizer =
  1305. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOMRIGHT);
  1306. resizer->Drag(CalculateDragPoint(*resizer, 2, 2), 0);
  1307. EXPECT_EQ(401, window_->bounds().width());
  1308. EXPECT_EQ(302, window_->bounds().height());
  1309. }
  1310. // Test that a window with a specified max size can't be snapped.
  1311. TEST_F(WorkspaceWindowResizerTest, PhantomSnapNonMaximizable) {
  1312. // Make the window snappable.
  1313. AllowSnap(window_.get());
  1314. {
  1315. // With max size not set we get a phantom window controller for dragging off
  1316. // the right hand side.
  1317. std::unique_ptr<WindowResizer> resizer =
  1318. CreateResizerForTest(window_.get());
  1319. EXPECT_FALSE(snap_phantom_window_controller());
  1320. resizer->Drag(CalculateDragPoint(*resizer, 801, 0), 0);
  1321. EXPECT_TRUE(snap_phantom_window_controller());
  1322. resizer->RevertDrag();
  1323. }
  1324. {
  1325. // When it can't be maximzied, we get no phantom window for snapping.
  1326. window_->SetBounds(gfx::Rect(0, 0, 400, 200));
  1327. window_->SetProperty(
  1328. aura::client::kResizeBehaviorKey,
  1329. window_->GetProperty(aura::client::kResizeBehaviorKey) ^
  1330. aura::client::kResizeBehaviorCanMaximize);
  1331. std::unique_ptr<WindowResizer> resizer =
  1332. CreateResizerForTest(window_.get());
  1333. resizer->Drag(CalculateDragPoint(*resizer, 801, 0), 0);
  1334. EXPECT_FALSE(snap_phantom_window_controller());
  1335. resizer->RevertDrag();
  1336. }
  1337. }
  1338. TEST_F(WorkspaceWindowResizerTest, DontRewardRightmostWindowForOverflows) {
  1339. UpdateDisplay("600x800");
  1340. aura::Window* root = Shell::GetPrimaryRootWindow();
  1341. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  1342. root, gfx::Rect(), gfx::Insets(), gfx::Insets());
  1343. // Four 100x100 windows flush against eachother, starting at 100,100.
  1344. window_->SetBounds(gfx::Rect(100, 100, 100, 100));
  1345. window2_->SetBounds(gfx::Rect(200, 100, 100, 100));
  1346. window3_->SetBounds(gfx::Rect(300, 100, 100, 100));
  1347. window4_->SetBounds(gfx::Rect(400, 100, 100, 100));
  1348. delegate2_.set_max_size(gfx::Size(101, 0));
  1349. std::vector<aura::Window*> windows;
  1350. windows.push_back(window2_.get());
  1351. windows.push_back(window3_.get());
  1352. windows.push_back(window4_.get());
  1353. std::unique_ptr<WorkspaceWindowResizer> resizer =
  1354. CreateWorkspaceResizerForTest(window_.get(), gfx::Point(), HTRIGHT,
  1355. ::wm::WINDOW_MOVE_SOURCE_MOUSE, windows);
  1356. ASSERT_TRUE(resizer.get());
  1357. // Move it 51 to the left, which should contract w1 and expand w2-4.
  1358. // w2 will hit its max size straight away, and in doing so will leave extra
  1359. // pixels that a naive implementation may award to the rightmost window. A
  1360. // fair implementation will give 25 pixels to each of the other windows.
  1361. resizer->Drag(CalculateDragPoint(*resizer, -51, 0), 0);
  1362. EXPECT_EQ("100,100 49x100", window_->bounds().ToString());
  1363. EXPECT_EQ("149,100 101x100", window2_->bounds().ToString());
  1364. EXPECT_EQ("250,100 125x100", window3_->bounds().ToString());
  1365. EXPECT_EQ("375,100 125x100", window4_->bounds().ToString());
  1366. }
  1367. TEST_F(WorkspaceWindowResizerTest, DontExceedMaxWidth) {
  1368. UpdateDisplay("600x800");
  1369. aura::Window* root = Shell::GetPrimaryRootWindow();
  1370. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  1371. root, gfx::Rect(), gfx::Insets(), gfx::Insets());
  1372. // Four 100x100 windows flush against eachother, starting at 100,100.
  1373. window_->SetBounds(gfx::Rect(100, 100, 100, 100));
  1374. window2_->SetBounds(gfx::Rect(200, 100, 100, 100));
  1375. window3_->SetBounds(gfx::Rect(300, 100, 100, 100));
  1376. window4_->SetBounds(gfx::Rect(400, 100, 100, 100));
  1377. delegate2_.set_max_size(gfx::Size(101, 0));
  1378. delegate3_.set_max_size(gfx::Size(101, 0));
  1379. std::unique_ptr<WorkspaceWindowResizer> resizer =
  1380. CreateWorkspaceResizerForTest(
  1381. window_.get(), HTRIGHT,
  1382. {window2_.get(), window3_.get(), window4_.get()});
  1383. ASSERT_TRUE(resizer.get());
  1384. // Move it 52 to the left, which should contract w1 and expand w2-4.
  1385. resizer->Drag(CalculateDragPoint(*resizer, -52, 0), 0);
  1386. EXPECT_EQ("100,100 48x100", window_->bounds().ToString());
  1387. EXPECT_EQ("148,100 101x100", window2_->bounds().ToString());
  1388. EXPECT_EQ("249,100 101x100", window3_->bounds().ToString());
  1389. EXPECT_EQ("350,100 150x100", window4_->bounds().ToString());
  1390. }
  1391. TEST_F(WorkspaceWindowResizerTest, DontExceedMaxHeight) {
  1392. UpdateDisplay("600x800");
  1393. aura::Window* root = Shell::GetPrimaryRootWindow();
  1394. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  1395. root, gfx::Rect(), gfx::Insets(), gfx::Insets());
  1396. // Four 100x100 windows flush against eachother, starting at 100,100.
  1397. window_->SetBounds(gfx::Rect(100, 100, 100, 100));
  1398. window2_->SetBounds(gfx::Rect(100, 200, 100, 100));
  1399. window3_->SetBounds(gfx::Rect(100, 300, 100, 100));
  1400. window4_->SetBounds(gfx::Rect(100, 400, 100, 100));
  1401. delegate2_.set_max_size(gfx::Size(0, 101));
  1402. delegate3_.set_max_size(gfx::Size(0, 101));
  1403. std::unique_ptr<WorkspaceWindowResizer> resizer =
  1404. CreateWorkspaceResizerForTest(
  1405. window_.get(), HTBOTTOM,
  1406. {window2_.get(), window3_.get(), window4_.get()});
  1407. ASSERT_TRUE(resizer.get());
  1408. // Move it 52 up, which should contract w1 and expand w2-4.
  1409. resizer->Drag(CalculateDragPoint(*resizer, 0, -52), 0);
  1410. EXPECT_EQ("100,100 100x48", window_->bounds().ToString());
  1411. EXPECT_EQ("100,148 100x101", window2_->bounds().ToString());
  1412. EXPECT_EQ("100,249 100x101", window3_->bounds().ToString());
  1413. EXPECT_EQ("100,350 100x150", window4_->bounds().ToString());
  1414. }
  1415. TEST_F(WorkspaceWindowResizerTest, DontExceedMinHeight) {
  1416. UpdateDisplay("600x500");
  1417. aura::Window* root = Shell::GetPrimaryRootWindow();
  1418. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  1419. root, gfx::Rect(), gfx::Insets(), gfx::Insets());
  1420. // Four 100x100 windows flush against eachother, starting at 100,100.
  1421. window_->SetBounds(gfx::Rect(100, 100, 100, 100));
  1422. window2_->SetBounds(gfx::Rect(100, 200, 100, 100));
  1423. window3_->SetBounds(gfx::Rect(100, 300, 100, 100));
  1424. window4_->SetBounds(gfx::Rect(100, 400, 100, 100));
  1425. delegate2_.set_min_size(gfx::Size(0, 99));
  1426. delegate3_.set_min_size(gfx::Size(0, 99));
  1427. std::unique_ptr<WorkspaceWindowResizer> resizer =
  1428. CreateWorkspaceResizerForTest(
  1429. window_.get(), HTBOTTOM,
  1430. {window2_.get(), window3_.get(), window4_.get()});
  1431. ASSERT_TRUE(resizer.get());
  1432. // Move it 52 down, which should expand w1 and contract w2-4.
  1433. resizer->Drag(CalculateDragPoint(*resizer, 0, 52), 0);
  1434. EXPECT_EQ("100,100 100x152", window_->bounds().ToString());
  1435. EXPECT_EQ("100,252 100x99", window2_->bounds().ToString());
  1436. EXPECT_EQ("100,351 100x99", window3_->bounds().ToString());
  1437. EXPECT_EQ("100,450 100x50", window4_->bounds().ToString());
  1438. }
  1439. TEST_F(WorkspaceWindowResizerTest, DontExpandRightmostPastMaxWidth) {
  1440. UpdateDisplay("600x800");
  1441. aura::Window* root = Shell::GetPrimaryRootWindow();
  1442. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  1443. root, gfx::Rect(), gfx::Insets(), gfx::Insets());
  1444. // Three 100x100 windows flush against eachother, starting at 100,100.
  1445. window_->SetBounds(gfx::Rect(100, 100, 100, 100));
  1446. window2_->SetBounds(gfx::Rect(200, 100, 100, 100));
  1447. window3_->SetBounds(gfx::Rect(300, 100, 100, 100));
  1448. delegate3_.set_max_size(gfx::Size(101, 0));
  1449. std::unique_ptr<WorkspaceWindowResizer> resizer =
  1450. CreateWorkspaceResizerForTest(
  1451. window_.get(), HTRIGHT,
  1452. {window2_.get(), window3_.get(), window4_.get()});
  1453. ASSERT_TRUE(resizer.get());
  1454. // Move it 51 to the left, which should contract w1 and expand w2-3.
  1455. resizer->Drag(CalculateDragPoint(*resizer, -51, 0), 0);
  1456. EXPECT_EQ("100,100 49x100", window_->bounds().ToString());
  1457. EXPECT_EQ("149,100 150x100", window2_->bounds().ToString());
  1458. EXPECT_EQ("299,100 101x100", window3_->bounds().ToString());
  1459. }
  1460. TEST_F(WorkspaceWindowResizerTest, MoveAttachedWhenGrownToMaxSize) {
  1461. UpdateDisplay("600x800");
  1462. aura::Window* root = Shell::GetPrimaryRootWindow();
  1463. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  1464. root, gfx::Rect(), gfx::Insets(), gfx::Insets());
  1465. // Three 100x100 windows flush against eachother, starting at 100,100.
  1466. window_->SetBounds(gfx::Rect(100, 100, 100, 100));
  1467. window2_->SetBounds(gfx::Rect(200, 100, 100, 100));
  1468. window3_->SetBounds(gfx::Rect(300, 100, 100, 100));
  1469. delegate2_.set_max_size(gfx::Size(101, 0));
  1470. delegate3_.set_max_size(gfx::Size(101, 0));
  1471. std::unique_ptr<WorkspaceWindowResizer> resizer =
  1472. CreateWorkspaceResizerForTest(
  1473. window_.get(), HTRIGHT,
  1474. {window2_.get(), window3_.get(), window4_.get()});
  1475. ASSERT_TRUE(resizer.get());
  1476. // Move it 52 to the left, which should contract w1 and expand and move w2-3.
  1477. resizer->Drag(CalculateDragPoint(*resizer, -52, 0), 0);
  1478. EXPECT_EQ("100,100 48x100", window_->bounds().ToString());
  1479. EXPECT_EQ("148,100 101x100", window2_->bounds().ToString());
  1480. EXPECT_EQ("249,100 101x100", window3_->bounds().ToString());
  1481. }
  1482. TEST_F(WorkspaceWindowResizerTest, MainWindowHonoursMaxWidth) {
  1483. UpdateDisplay("400x800");
  1484. aura::Window* root = Shell::GetPrimaryRootWindow();
  1485. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  1486. root, gfx::Rect(), gfx::Insets(), gfx::Insets());
  1487. // Three 100x100 windows flush against eachother, starting at 100,100.
  1488. window_->SetBounds(gfx::Rect(100, 100, 100, 100));
  1489. window2_->SetBounds(gfx::Rect(200, 100, 100, 100));
  1490. window3_->SetBounds(gfx::Rect(300, 100, 100, 100));
  1491. delegate_.set_max_size(gfx::Size(102, 0));
  1492. std::unique_ptr<WorkspaceWindowResizer> resizer =
  1493. CreateWorkspaceResizerForTest(
  1494. window_.get(), HTRIGHT,
  1495. {window2_.get(), window3_.get(), window4_.get()});
  1496. ASSERT_TRUE(resizer.get());
  1497. // Move it 50 to the right, which should expand w1 and contract w2-3, as they
  1498. // won't fit in the root window in their original sizes.
  1499. resizer->Drag(CalculateDragPoint(*resizer, 50, 0), 0);
  1500. EXPECT_EQ("100,100 102x100", window_->bounds().ToString());
  1501. EXPECT_EQ("202,100 99x100", window2_->bounds().ToString());
  1502. EXPECT_EQ("301,100 99x100", window3_->bounds().ToString());
  1503. }
  1504. TEST_F(WorkspaceWindowResizerTest, MainWindowHonoursMinWidth) {
  1505. UpdateDisplay("400x800");
  1506. aura::Window* root = Shell::GetPrimaryRootWindow();
  1507. WorkAreaInsets::ForWindow(root)->UpdateWorkAreaInsetsForTest(
  1508. root, gfx::Rect(), gfx::Insets(), gfx::Insets());
  1509. // Three 100x100 windows flush against eachother, starting at 100,100.
  1510. window_->SetBounds(gfx::Rect(100, 100, 100, 100));
  1511. window2_->SetBounds(gfx::Rect(200, 100, 100, 100));
  1512. window3_->SetBounds(gfx::Rect(300, 100, 100, 100));
  1513. delegate_.set_min_size(gfx::Size(98, 0));
  1514. std::unique_ptr<WorkspaceWindowResizer> resizer =
  1515. CreateWorkspaceResizerForTest(window_.get(), HTRIGHT,
  1516. {window2_.get(), window3_.get()});
  1517. ASSERT_TRUE(resizer.get());
  1518. // Move it 50 to the left, which should contract w1 and expand w2-3.
  1519. resizer->Drag(CalculateDragPoint(*resizer, -50, 0), 0);
  1520. EXPECT_EQ("100,100 98x100", window_->bounds().ToString());
  1521. EXPECT_EQ("198,100 101x100", window2_->bounds().ToString());
  1522. EXPECT_EQ("299,100 101x100", window3_->bounds().ToString());
  1523. }
  1524. // The following variants test that windows are resized correctly to the edges
  1525. // of the screen using touch, when touch point is off of the window border.
  1526. TEST_F(WorkspaceWindowResizerTest, TouchResizeToEdge_RIGHT) {
  1527. GetPrimaryShelf()->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlwaysHidden);
  1528. InitTouchResizeWindow(gfx::Rect(100, 100, 600, kRootHeight - 200), HTRIGHT);
  1529. EXPECT_EQ(gfx::Rect(100, 100, 600, kRootHeight - 200),
  1530. touch_resize_window_->bounds());
  1531. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
  1532. touch_resize_window_.get());
  1533. // Drag out of the right border a bit and check if the border is aligned with
  1534. // the touch point.
  1535. generator.GestureScrollSequence(gfx::Point(715, kRootHeight / 2),
  1536. gfx::Point(725, kRootHeight / 2),
  1537. base::Milliseconds(10), 5);
  1538. EXPECT_EQ(gfx::Rect(100, 100, 625, kRootHeight - 200),
  1539. touch_resize_window_->bounds());
  1540. // Drag more, but stop before being snapped to the edge.
  1541. generator.GestureScrollSequence(gfx::Point(725, kRootHeight / 2),
  1542. gfx::Point(760, kRootHeight / 2),
  1543. base::Milliseconds(10), 5);
  1544. EXPECT_EQ(gfx::Rect(100, 100, 660, kRootHeight - 200),
  1545. touch_resize_window_->bounds());
  1546. // Drag even more to snap to the edge.
  1547. generator.GestureScrollSequence(gfx::Point(760, kRootHeight / 2),
  1548. gfx::Point(775, kRootHeight / 2),
  1549. base::Milliseconds(10), 5);
  1550. EXPECT_EQ(gfx::Rect(100, 100, 700, kRootHeight - 200),
  1551. touch_resize_window_->bounds());
  1552. }
  1553. TEST_F(WorkspaceWindowResizerTest, TouchResizeToEdge_LEFT) {
  1554. GetPrimaryShelf()->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlwaysHidden);
  1555. InitTouchResizeWindow(gfx::Rect(100, 100, 600, kRootHeight - 200), HTLEFT);
  1556. EXPECT_EQ(gfx::Rect(100, 100, 600, kRootHeight - 200),
  1557. touch_resize_window_->bounds());
  1558. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
  1559. touch_resize_window_.get());
  1560. // Drag out of the left border a bit and check if the border is aligned with
  1561. // the touch point.
  1562. generator.GestureScrollSequence(gfx::Point(85, kRootHeight / 2),
  1563. gfx::Point(75, kRootHeight / 2),
  1564. base::Milliseconds(10), 5);
  1565. EXPECT_EQ(gfx::Rect(75, 100, 625, kRootHeight - 200),
  1566. touch_resize_window_->bounds());
  1567. // Drag more, but stop before being snapped to the edge.
  1568. generator.GestureScrollSequence(gfx::Point(75, kRootHeight / 2),
  1569. gfx::Point(40, kRootHeight / 2),
  1570. base::Milliseconds(10), 5);
  1571. EXPECT_EQ(gfx::Rect(40, 100, 660, kRootHeight - 200),
  1572. touch_resize_window_->bounds());
  1573. // Drag even more to snap to the edge.
  1574. generator.GestureScrollSequence(gfx::Point(40, kRootHeight / 2),
  1575. gfx::Point(25, kRootHeight / 2),
  1576. base::Milliseconds(10), 5);
  1577. EXPECT_EQ(gfx::Rect(0, 100, 700, kRootHeight - 200),
  1578. touch_resize_window_->bounds());
  1579. }
  1580. TEST_F(WorkspaceWindowResizerTest, TouchResizeToEdge_TOP) {
  1581. GetPrimaryShelf()->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlwaysHidden);
  1582. InitTouchResizeWindow(gfx::Rect(100, 100, 600, kRootHeight - 200), HTTOP);
  1583. EXPECT_EQ(gfx::Rect(100, 100, 600, kRootHeight - 200),
  1584. touch_resize_window_->bounds());
  1585. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
  1586. touch_resize_window_.get());
  1587. // Drag out of the top border a bit and check if the border is aligned with
  1588. // the touch point.
  1589. generator.GestureScrollSequence(gfx::Point(400, 85), gfx::Point(400, 75),
  1590. base::Milliseconds(10), 5);
  1591. EXPECT_EQ(gfx::Rect(100, 75, 600, kRootHeight - 175),
  1592. touch_resize_window_->bounds());
  1593. // Drag more, but stop before being snapped to the edge.
  1594. generator.GestureScrollSequence(gfx::Point(400, 75), gfx::Point(400, 40),
  1595. base::Milliseconds(10), 5);
  1596. EXPECT_EQ(gfx::Rect(100, 40, 600, kRootHeight - 140),
  1597. touch_resize_window_->bounds());
  1598. // Drag even more to snap to the edge.
  1599. generator.GestureScrollSequence(gfx::Point(400, 40), gfx::Point(400, 25),
  1600. base::Milliseconds(10), 5);
  1601. EXPECT_EQ(gfx::Rect(100, 0, 600, kRootHeight - 100),
  1602. touch_resize_window_->bounds());
  1603. }
  1604. TEST_F(WorkspaceWindowResizerTest, TouchResizeToEdge_BOTTOM) {
  1605. GetPrimaryShelf()->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlwaysHidden);
  1606. InitTouchResizeWindow(gfx::Rect(100, 100, 600, kRootHeight - 200), HTBOTTOM);
  1607. EXPECT_EQ(gfx::Rect(100, 100, 600, kRootHeight - 200),
  1608. touch_resize_window_->bounds());
  1609. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
  1610. touch_resize_window_.get());
  1611. // Drag out of the bottom border a bit and check if the border is aligned with
  1612. // the touch point.
  1613. generator.GestureScrollSequence(gfx::Point(400, kRootHeight - 85),
  1614. gfx::Point(400, kRootHeight - 75),
  1615. base::Milliseconds(10), 5);
  1616. EXPECT_EQ(gfx::Rect(100, 100, 600, kRootHeight - 175),
  1617. touch_resize_window_->bounds());
  1618. // Drag more, but stop before being snapped to the edge.
  1619. generator.GestureScrollSequence(gfx::Point(400, kRootHeight - 75),
  1620. gfx::Point(400, kRootHeight - 40),
  1621. base::Milliseconds(10), 5);
  1622. EXPECT_EQ(gfx::Rect(100, 100, 600, kRootHeight - 140),
  1623. touch_resize_window_->bounds());
  1624. // Drag even more to snap to the edge.
  1625. generator.GestureScrollSequence(gfx::Point(400, kRootHeight - 40),
  1626. gfx::Point(400, kRootHeight - 25),
  1627. base::Milliseconds(10), 5);
  1628. EXPECT_EQ(gfx::Rect(100, 100, 600, kRootHeight - 100),
  1629. touch_resize_window_->bounds());
  1630. }
  1631. TEST_F(WorkspaceWindowResizerTest, ResizeHistogram) {
  1632. base::HistogramTester histograms;
  1633. window_->SetBounds(gfx::Rect(20, 30, 400, 60));
  1634. std::unique_ptr<WindowResizer> resizer =
  1635. CreateResizerForTest(window_.get(), gfx::Point(), HTRIGHT);
  1636. ASSERT_TRUE(resizer.get());
  1637. resizer->Drag(gfx::PointF(50, 50), 0);
  1638. // A resize should generate a histogram.
  1639. EXPECT_NE(gfx::Size(400, 60), window_->bounds().size());
  1640. EXPECT_TRUE(
  1641. ui::WaitForNextFrameToBePresented(window_->GetHost()->compositor()));
  1642. histograms.ExpectTotalCount("Ash.InteractiveWindowResize.TimeToPresent", 1);
  1643. // Completing the drag should not generate another histogram.
  1644. resizer->CompleteDrag();
  1645. // Flush pending draws until there is no frame presented for 100ms (6 frames
  1646. // worth time) and check that histogram is not updated.
  1647. while (ui::WaitForNextFrameToBePresented(window_->GetHost()->compositor(),
  1648. base::Milliseconds(100)))
  1649. ;
  1650. histograms.ExpectTotalCount("Ash.InteractiveWindowResize.TimeToPresent", 1);
  1651. }
  1652. // Tests that windows that are snapped and maximized have to be dragged a
  1653. // certain amount before the window bounds change.
  1654. TEST_F(WorkspaceWindowResizerTest, DraggingThresholdForSnappedAndMaximized) {
  1655. UpdateDisplay("800x648");
  1656. const gfx::Rect restore_bounds(30, 30, 300, 300);
  1657. window_->SetBounds(restore_bounds);
  1658. AllowSnap(window_.get());
  1659. // Test that on a normal window, there is no minimal drag amount.
  1660. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  1661. resizer->Drag(gfx::PointF(1.f, 1.f), 0);
  1662. EXPECT_EQ(gfx::Rect(31, 31, 300, 300), window_->bounds());
  1663. // End the drag in a snap region, and verify the window has snapped.
  1664. resizer->Drag(gfx::PointF(2.f, 2.f), 0);
  1665. resizer->CompleteDrag();
  1666. auto* window_state = WindowState::Get(window_.get());
  1667. ASSERT_TRUE(window_state->IsSnapped());
  1668. const gfx::Rect snapped_bounds(400, 600);
  1669. EXPECT_EQ(snapped_bounds, window_->bounds());
  1670. resizer.reset();
  1671. resizer = CreateResizerForTest(window_.get());
  1672. // Tests that small drags do not change the window bounds.
  1673. resizer->Drag(gfx::PointF(1.f, 1.f), 0);
  1674. EXPECT_EQ(snapped_bounds, window_->bounds());
  1675. // A large enough drag will change the window bounds to its restore bounds.
  1676. resizer->Drag(gfx::PointF(10.f, 10.f), 0);
  1677. EXPECT_EQ(restore_bounds.size(), window_->bounds().size());
  1678. // Tests that on drag end, the window will get restored. Make sure the drag
  1679. // does not end in a snap region.
  1680. resizer->Drag(gfx::PointF(200.f, 200.f), 0);
  1681. resizer->CompleteDrag();
  1682. EXPECT_EQ(restore_bounds.size(), window_->bounds().size());
  1683. EXPECT_TRUE(window_state->IsNormalStateType());
  1684. // Tests the same things as the snapped window case for the maximized window
  1685. // case.
  1686. window_state->Maximize();
  1687. const gfx::Rect maximized_bounds(800, 600);
  1688. EXPECT_EQ(maximized_bounds, window_->bounds());
  1689. resizer.reset();
  1690. resizer = CreateResizerForTest(window_.get());
  1691. resizer->Drag(gfx::PointF(1.f, 1.f), 0);
  1692. EXPECT_EQ(maximized_bounds, window_->bounds());
  1693. resizer->Drag(gfx::PointF(10.f, 10.f), 0);
  1694. EXPECT_EQ(restore_bounds.size(), window_->bounds().size());
  1695. resizer->Drag(gfx::PointF(200.f, 200.f), 0);
  1696. resizer->CompleteDrag();
  1697. EXPECT_EQ(restore_bounds.size(), window_->bounds().size());
  1698. EXPECT_TRUE(window_state->IsNormalStateType());
  1699. }
  1700. // Tests dragging a window and snapping it to maximized state.
  1701. TEST_F(WorkspaceWindowResizerTest, DragToSnapMaximize) {
  1702. UpdateDisplay("800x648");
  1703. const gfx::Rect restore_bounds(30, 30, 300, 300);
  1704. window_->SetBounds(restore_bounds);
  1705. AllowSnap(window_.get());
  1706. // Drag to a top region of the display and release. The window should be
  1707. // maximized.
  1708. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  1709. resizer->Drag(gfx::PointF(400.f, 400.f), 0);
  1710. resizer->Drag(gfx::PointF(400.f, 2.f), 0);
  1711. DwellCountdownTimerFireNow();
  1712. resizer->CompleteDrag();
  1713. auto* window_state = WindowState::Get(window_.get());
  1714. ASSERT_TRUE(window_state->IsMaximized());
  1715. // Tests that dragging a maximized window and snapping it back to maximized
  1716. // state works as expected.
  1717. resizer.reset();
  1718. resizer = CreateResizerForTest(window_.get());
  1719. // First drag to "unmaximize". The window is still maximized at this point,
  1720. // but the bounds have shrunk to make it look restored.
  1721. resizer->Drag(gfx::PointF(200.f, 200.f), 0);
  1722. EXPECT_TRUE(window_state->IsMaximized());
  1723. EXPECT_EQ(gfx::Size(300, 300), window_->bounds().size());
  1724. // End the drag in the snap to maximize region. The window should be maximized
  1725. // and sized to fit the whole work area.
  1726. resizer->Drag(gfx::PointF(200.f, 2.f), 0);
  1727. DwellCountdownTimerFireNow();
  1728. EXPECT_TRUE(!snap_phantom_window_controller()->GetMaximizeCueForTesting());
  1729. resizer->CompleteDrag();
  1730. EXPECT_TRUE(window_state->IsMaximized());
  1731. EXPECT_EQ(gfx::Rect(800, 600), window_->bounds());
  1732. }
  1733. TEST_F(WorkspaceWindowResizerTest, DragToMaximizeStartingInSnapRegion) {
  1734. UpdateDisplay("800x648");
  1735. // Drag starting in the snap to maximize region. If we do not leave it, on
  1736. // drag release the window will not get maximized.
  1737. window_->SetBounds(gfx::Rect(200, 200));
  1738. AllowSnap(window_.get());
  1739. std::unique_ptr<WindowResizer> resizer =
  1740. CreateResizerForTest(window_.get(), gfx::Point(400.f, 1.f));
  1741. resizer->Drag(gfx::PointF(400.f, 5.f), 0);
  1742. resizer->CompleteDrag();
  1743. ASSERT_FALSE(WindowState::Get(window_.get())->IsMaximized());
  1744. // This time exit the snap region. On returning and completing the drag, the
  1745. // window should be maximized.
  1746. WindowState::Get(window_.get())->Maximize();
  1747. resizer.reset();
  1748. resizer = CreateResizerForTest(window_.get(), gfx::Point(400.f, 1.f));
  1749. resizer->Drag(gfx::PointF(400.f, 400.f), 0);
  1750. resizer->Drag(gfx::PointF(400.f, 5.f), 0);
  1751. DwellCountdownTimerFireNow();
  1752. resizer->CompleteDrag();
  1753. EXPECT_TRUE(WindowState::Get(window_.get())->IsMaximized());
  1754. }
  1755. TEST_F(WorkspaceWindowResizerTest, DragToMaximizeNumOfMisTriggersMetric) {
  1756. base::HistogramTester histogram_tester;
  1757. auto* window = window_.get();
  1758. auto* window2 = window2_.get();
  1759. AllowSnap(window);
  1760. AllowSnap(window2);
  1761. // Drag to maximize `window_` again.
  1762. DragToMaximize(window);
  1763. // Immediately restore it.
  1764. DragToRestore(window);
  1765. // Drag to maximize again immediately after it's restored.
  1766. DragToMaximize(window);
  1767. // Immediately restore it again.
  1768. DragToRestore(window);
  1769. // Wait for the drag to maximize behavior to be checked.
  1770. DragToMaximizeBehaviorCheckCountdownTimerFireNow(window);
  1771. window_.reset();
  1772. // Verify that during the lifetime of `window_`, there're 2 drag to maximize
  1773. // mis-triggers on it.
  1774. histogram_tester.ExpectBucketCount(
  1775. "Ash.Window.DragMaximized.NumberOfMisTriggers", 2, 1);
  1776. // Drag to maximize `window2_`.
  1777. DragToMaximize(window2);
  1778. // Immediately restore it.
  1779. DragToRestore(window2);
  1780. DragToMaximizeBehaviorCheckCountdownTimerFireNow(window2);
  1781. window2_.reset();
  1782. // Verify that during the lifetime of `window2_`, there is one drag to
  1783. // maximize mis-trigger on it.
  1784. histogram_tester.ExpectBucketCount(
  1785. "Ash.Window.DragMaximized.NumberOfMisTriggers", 1, 1);
  1786. window3_.reset();
  1787. // Verify that the number of mis-triggers is not recorded for `window3`, since
  1788. // it's never been dragged to maximized.
  1789. histogram_tester.ExpectBucketCount(
  1790. "Ash.Window.DragMaximized.NumberOfMisTriggers", 0, 0);
  1791. }
  1792. TEST_F(WorkspaceWindowResizerTest, DragToMaximizeValidMetric) {
  1793. base::HistogramTester histogram_tester;
  1794. auto* window = window_.get();
  1795. AllowSnap(window);
  1796. // Drag to maximize `window_`.
  1797. DragToMaximize(window);
  1798. // Now immediately drag to restore `window_`.
  1799. DragToRestore(window);
  1800. // Wait for the drag to maximize behavior to be checked.
  1801. DragToMaximizeBehaviorCheckCountdownTimerFireNow(window);
  1802. // Verify that since `window_` is restored immediately after is dragged to
  1803. // maximized, drag to maximize behavior should be considered as invalid.
  1804. histogram_tester.ExpectBucketCount("Ash.Window.DragMaximized.Valid", true, 0);
  1805. histogram_tester.ExpectBucketCount("Ash.Window.DragMaximized.Valid", false,
  1806. 1);
  1807. // Drag to maximize `window_` again.
  1808. DragToMaximize(window);
  1809. // Don't restore `window_` and wait for the drag to maximize behavior to be
  1810. // checked.
  1811. DragToMaximizeBehaviorCheckCountdownTimerFireNow(window);
  1812. DragToRestore(window);
  1813. // Verify this time the drag to maximize behavior should be considered as
  1814. // valid since the window is restored after 5 seconds.
  1815. histogram_tester.ExpectBucketCount("Ash.Window.DragMaximized.Valid", true, 1);
  1816. histogram_tester.ExpectBucketCount("Ash.Window.DragMaximized.Valid", false,
  1817. 1);
  1818. // Drag to maximize `window_` again.
  1819. DragToMaximize(window);
  1820. // Immediately restore it.
  1821. DragToRestore(window);
  1822. // Drag to maximize again immediately after it's restored.
  1823. DragToMaximize(window);
  1824. // Verify that the first drag to maximize should be considered as invalid.
  1825. histogram_tester.ExpectBucketCount("Ash.Window.DragMaximized.Valid", false,
  1826. 2);
  1827. // Immediately restore it again.
  1828. DragToRestore(window);
  1829. // Wait for the drag to maximize behavior to be checked.
  1830. DragToMaximizeBehaviorCheckCountdownTimerFireNow(window);
  1831. // Verify that the second drag to maximize should also be considered as
  1832. // invalid.'
  1833. histogram_tester.ExpectBucketCount("Ash.Window.DragMaximized.Valid", false,
  1834. 3);
  1835. }
  1836. // Makes sure that we are not creating any resizer in kiosk mode.
  1837. TEST_F(WorkspaceWindowResizerTest, DoesNotWorkInAppMode) {
  1838. GetSessionControllerClient()->SetIsRunningInAppMode(true);
  1839. EXPECT_FALSE(CreateResizerForTest(window_.get()));
  1840. }
  1841. TEST_F(WorkspaceWindowResizerTest, DoNotCreateResizerIfNotActiveSession) {
  1842. GetSessionControllerClient()->SetSessionState(
  1843. session_manager::SessionState::OOBE);
  1844. EXPECT_FALSE(CreateResizerForTest(window_.get()));
  1845. GetSessionControllerClient()->SetSessionState(
  1846. session_manager::SessionState::LOCKED);
  1847. EXPECT_FALSE(CreateResizerForTest(window_.get()));
  1848. GetSessionControllerClient()->SetSessionState(
  1849. session_manager::SessionState::LOGIN_PRIMARY);
  1850. EXPECT_FALSE(CreateResizerForTest(window_.get()));
  1851. GetSessionControllerClient()->SetSessionState(
  1852. session_manager::SessionState::ACTIVE);
  1853. EXPECT_TRUE(CreateResizerForTest(window_.get()));
  1854. }
  1855. // Tests that windows dragged across multiple displays have their restore bounds
  1856. // updated.
  1857. TEST_F(WorkspaceWindowResizerTest, MultiDisplayRestoreBounds) {
  1858. UpdateDisplay("800x700,800x700");
  1859. // Create a window and maximize it on the primary display.
  1860. window_->SetBounds(gfx::Rect(200, 200));
  1861. AllowSnap(window_.get());
  1862. auto* window_state = WindowState::Get(window_.get());
  1863. window_state->Maximize();
  1864. ASSERT_TRUE(window_state->HasRestoreBounds());
  1865. ASSERT_EQ(gfx::Rect(200, 200), window_state->GetRestoreBoundsInScreen());
  1866. // Drag the window to the secondary display and end it in a snap to maximize
  1867. // region. Drag() doesn't update the display, so manually do it in the test.
  1868. // Also we need to first drag the window out of the maximized snap zone,
  1869. // otherwise it won't snap on drag completed.
  1870. std::unique_ptr<WindowResizer> resizer =
  1871. CreateResizerForTest(window_.get(), gfx::Point(400.f, 1.f), HTCAPTION);
  1872. Shell::Get()->cursor_manager()->SetDisplay(
  1873. display::Screen::GetScreen()->GetDisplayNearestPoint(
  1874. gfx::Point(1200, 200)));
  1875. resizer->Drag(gfx::PointF(1200.f, 200.f), 0);
  1876. resizer->Drag(gfx::PointF(1200.f, 5.f), 0);
  1877. DwellCountdownTimerFireNow();
  1878. resizer->CompleteDrag();
  1879. ASSERT_TRUE(window_state->IsMaximized());
  1880. // Tests that the window and its restore bounds on on the secondary display.
  1881. ASSERT_EQ(2, display::Screen::GetScreen()->GetNumDisplays());
  1882. EXPECT_EQ(display::Screen::GetScreen()->GetAllDisplays()[1].id(),
  1883. window_state->GetDisplay().id());
  1884. EXPECT_EQ(gfx::Rect(800, 0, 200, 200),
  1885. window_state->GetRestoreBoundsInScreen());
  1886. }
  1887. // Tests that after dragging and flinging a maximized or snapped window,
  1888. // restoring it will have the same size it used to before it was maximized or
  1889. // snapped.
  1890. TEST_F(WorkspaceWindowResizerTest, FlingRestoreSize) {
  1891. // Init the window with |window_size| before maximizing it. This is the
  1892. // expected size after flinging the maximized window and restoring it.
  1893. gfx::Size window_size(300, 300);
  1894. InitTouchResizeWindow(gfx::Rect(gfx::Point(100, 100), window_size),
  1895. HTCAPTION);
  1896. auto* window_state = WindowState::Get(touch_resize_window_.get());
  1897. window_state->Maximize();
  1898. ASSERT_TRUE(window_state->IsMaximized());
  1899. // Create a fling sequence on |touch_resize_window_|. Verify that this results
  1900. // in the window being minimized.
  1901. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
  1902. touch_resize_window_.get());
  1903. generator.GestureScrollSequence(gfx::Point(400, 10), gfx::Point(400, 210),
  1904. base::Milliseconds(10), 10);
  1905. ASSERT_TRUE(window_state->IsMinimized());
  1906. // After unminimzing, the window bounds are the size they were before
  1907. // maximizing.
  1908. window_state->Unminimize();
  1909. EXPECT_TRUE(window_state->IsNormalStateType());
  1910. EXPECT_EQ(window_size, touch_resize_window_->bounds().size());
  1911. // Snap a window and do the same test.
  1912. const WindowSnapWMEvent snap_event(WM_EVENT_SNAP_PRIMARY);
  1913. window_state->OnWMEvent(&snap_event);
  1914. ASSERT_TRUE(window_state->IsSnapped());
  1915. const gfx::Rect snapped_bounds = window_state->window()->bounds();
  1916. generator.GestureScrollSequence(gfx::Point(10, 10), gfx::Point(10, 210),
  1917. base::Milliseconds(10), 10);
  1918. ASSERT_TRUE(window_state->IsMinimized());
  1919. // After unminimzing, the window bounds are the size they were before
  1920. // minimizing.
  1921. window_state->Unminimize();
  1922. EXPECT_TRUE(window_state->IsSnapped());
  1923. EXPECT_EQ(snapped_bounds, touch_resize_window_->bounds());
  1924. }
  1925. // Tests that fling to maximize does not crash or DCHECK if the window's restore
  1926. // bounds is on another display.
  1927. TEST_F(WorkspaceWindowResizerTest,
  1928. FlingMaximizeRestoreBoundsOnDifferentDisplay) {
  1929. UpdateDisplay("800x600,500x400");
  1930. // Prepare `touch_resize_window_` in the 2nd display.
  1931. gfx::Size window_size(300, 300);
  1932. InitTouchResizeWindow(gfx::Rect(gfx::Point(800, 100), window_size),
  1933. HTCAPTION);
  1934. // Speculatively simulate how a window could get a restore bounds in another
  1935. // display. What actually happens in the field is still a mystery.
  1936. // 1. Maximize to set a restore bounds in the 2nd display.
  1937. auto* window_state = WindowState::Get(touch_resize_window_.get());
  1938. window_state->Maximize();
  1939. ASSERT_TRUE(window_state->IsMaximized());
  1940. // 2. SetBoundsInScreen to move the window to the primary display.
  1941. touch_resize_window_->SetBoundsInScreen(
  1942. gfx::Rect(100, 100, 300, 30),
  1943. display::Screen::GetScreen()->GetPrimaryDisplay());
  1944. // Ensures that the restore bounds is not in the same display of window.
  1945. gfx::Rect restore_bounds = window_state->GetRestoreBoundsInScreen();
  1946. ASSERT_FALSE(
  1947. touch_resize_window_->GetRootWindow()->GetBoundsInScreen().Contains(
  1948. restore_bounds));
  1949. // Fling up.
  1950. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
  1951. touch_resize_window_.get());
  1952. generator.GestureScrollSequence(gfx::Point(250, 110), gfx::Point(250, 10),
  1953. base::Milliseconds(10), 10);
  1954. ASSERT_TRUE(window_state->IsMaximized());
  1955. // No crash, no DCHECK, and the window stays in the primary display.
  1956. EXPECT_TRUE(
  1957. Shell::GetPrimaryRootWindow()->Contains(touch_resize_window_.get()));
  1958. }
  1959. using MultiDisplayWorkspaceWindowResizerTest = AshTestBase;
  1960. // Makes sure that window drag magnetism still works when a window is dragged
  1961. // between different displays.
  1962. TEST_F(MultiDisplayWorkspaceWindowResizerTest, Magnetism) {
  1963. UpdateDisplay("800x600,600x500");
  1964. auto roots = Shell::GetAllRootWindows();
  1965. ASSERT_EQ(2u, roots.size());
  1966. // Create two windows one on the first display, and the other on the second.
  1967. auto win1 = CreateToplevelTestWindow(gfx::Rect(10, 10, 100, 100), /*id=*/1);
  1968. auto win2 = CreateToplevelTestWindow(gfx::Rect(1250, 100, 50, 50), /*id=*/2);
  1969. EXPECT_EQ(win1->GetRootWindow(), roots[0]);
  1970. EXPECT_EQ(win2->GetRootWindow(), roots[1]);
  1971. std::unique_ptr<WindowResizer> resizer = CreateWindowResizer(
  1972. win1.get(), gfx::PointF(), HTCAPTION, ::wm::WINDOW_MOVE_SOURCE_MOUSE);
  1973. ASSERT_TRUE(resizer.get());
  1974. // Drag `win1` such that its right edge is 5 pixels from the left edge of
  1975. // `win2`. Expect that `win1` will snap to `win2` on its left edge.
  1976. resizer->Drag(CalculateDragPoint(*resizer, 1135, 0), /*event_flags=*/0);
  1977. EXPECT_EQ(gfx::Rect(1150, 10, 100, 100), win1->GetBoundsInScreen());
  1978. }
  1979. // Make sure metrics is recorded during tab dragging.
  1980. TEST_F(WorkspaceWindowResizerTest, TabDraggingHistogram) {
  1981. UpdateDisplay("800x600,800x600");
  1982. ASSERT_EQ(2, display::Screen::GetScreen()->GetNumDisplays());
  1983. struct {
  1984. bool is_dragging_tab;
  1985. gfx::PointF drag_to_point;
  1986. int expected_latency_count;
  1987. int expected_max_latency_count;
  1988. } kTestCases[] = {// A tab dragging should generate a histogram.
  1989. {/*is_dragging_tab*/ true, gfx::PointF(200, 200), 1, 1},
  1990. // A window dragging should not generate a histogram.
  1991. {/*is_dragging_tab*/ false, gfx::PointF(200, 200), 0, 0},
  1992. // A tab dragging should not generate a histogram when
  1993. // the drag touches a different display.
  1994. {/*is_dragging_tab*/ true, gfx::PointF(850, 200), 0, 0}};
  1995. for (const auto& test : kTestCases) {
  1996. SCOPED_TRACE(testing::Message()
  1997. << "is_dragging_tab=" << test.is_dragging_tab);
  1998. base::HistogramTester histogram_tester;
  1999. window_->SetBounds(gfx::Rect(100, 100, 100, 100));
  2000. window_->SetProperty(ash::kIsDraggingTabsKey, test.is_dragging_tab);
  2001. std::unique_ptr<WindowResizer> resizer =
  2002. CreateWindowResizer(window_.get(), gfx::PointF(), HTCAPTION,
  2003. ::wm::WINDOW_MOVE_SOURCE_MOUSE);
  2004. ASSERT_TRUE(resizer.get());
  2005. resizer->Drag(test.drag_to_point, 0);
  2006. EXPECT_TRUE(
  2007. ui::WaitForNextFrameToBePresented(window_->GetHost()->compositor()));
  2008. resizer->CompleteDrag();
  2009. resizer.reset(nullptr);
  2010. histogram_tester.ExpectTotalCount(
  2011. "Ash.TabDrag.PresentationTime.ClamshellMode",
  2012. test.expected_latency_count);
  2013. histogram_tester.ExpectTotalCount(
  2014. "Ash.TabDrag.PresentationTime.MaxLatency.ClamshellMode",
  2015. test.expected_max_latency_count);
  2016. }
  2017. }
  2018. // Test dwell time before snap to maximize.
  2019. TEST_F(WorkspaceWindowResizerTest, SnapMaximizeDwellTime) {
  2020. UpdateDisplay("800x648");
  2021. window_->SetBounds(gfx::Rect(10, 10, 100, 100));
  2022. AllowSnap(window_.get());
  2023. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  2024. // Ensure the timer is not running.
  2025. EXPECT_FALSE(IsDwellCountdownTimerRunning());
  2026. // Test when dwell timer finished countdown window is maximized.
  2027. resizer->Drag(gfx::PointF(400.f, 400.f), 0);
  2028. resizer->Drag(gfx::PointF(100.f, 3.f), 0);
  2029. // Timer is triggered.
  2030. EXPECT_TRUE(IsDwellCountdownTimerRunning());
  2031. DwellCountdownTimerFireNow();
  2032. resizer->CompleteDrag();
  2033. auto* window_state = WindowState::Get(window_.get());
  2034. base::RunLoop().RunUntilIdle();
  2035. EXPECT_TRUE(window_state->IsMaximized());
  2036. // If dwell timer doesn't finish countdown,
  2037. // Window will not be maximized.
  2038. resizer.reset();
  2039. resizer = CreateResizerForTest(window_.get());
  2040. resizer->Drag(gfx::PointF(400.f, 400.f), 0);
  2041. resizer->Drag(gfx::PointF(100.f, 3.f), 0);
  2042. // Timer is triggered.
  2043. EXPECT_TRUE(IsDwellCountdownTimerRunning());
  2044. resizer->CompleteDrag();
  2045. window_state = WindowState::Get(window_.get());
  2046. EXPECT_FALSE(window_state->IsMaximized());
  2047. // Once dwell timer starts, drag away the window
  2048. // will not maximize the window if move more than
  2049. // kSnapDragDwellTimeResetThreshold.
  2050. resizer.reset();
  2051. resizer = CreateResizerForTest(window_.get());
  2052. resizer->Drag(gfx::PointF(400.f, 400.f), 0);
  2053. resizer->Drag(gfx::PointF(100.f, 3.f), 0);
  2054. DwellCountdownTimerFireNow();
  2055. resizer->Drag(gfx::PointF(200.f, 3.f), 0);
  2056. // Timer is triggered.
  2057. EXPECT_TRUE(IsDwellCountdownTimerRunning());
  2058. resizer->CompleteDrag();
  2059. window_state = WindowState::Get(window_.get());
  2060. EXPECT_FALSE(window_state->IsMaximized());
  2061. // Once dwell timer starts, drag away the window
  2062. // can still maximize the window if move less than
  2063. // kSnapDragDwellTimeResetThreshold.
  2064. resizer.reset();
  2065. resizer = CreateResizerForTest(window_.get());
  2066. resizer->Drag(gfx::PointF(400.f, 400.f), 0);
  2067. resizer->Drag(gfx::PointF(100.f, 3.f), 0);
  2068. DwellCountdownTimerFireNow();
  2069. resizer->Drag(gfx::PointF(101.f, 3.f), 0);
  2070. // Timer is triggered.
  2071. EXPECT_TRUE(IsDwellCountdownTimerRunning());
  2072. resizer->CompleteDrag();
  2073. window_state = WindowState::Get(window_.get());
  2074. EXPECT_TRUE(window_state->IsMaximized());
  2075. }
  2076. // Test horizontal move won't trigger snap.
  2077. TEST_F(WorkspaceWindowResizerTest, HorizontalMoveNotTriggerSnap) {
  2078. UpdateDisplay("800x648");
  2079. window_->SetBounds(gfx::Rect(10, 10, 100, 100));
  2080. AllowSnap(window_.get());
  2081. std::unique_ptr<WindowResizer> resizer =
  2082. CreateResizerForTest(window_.get(), gfx::Point(400.f, 67.f));
  2083. // Check if a horizontal move more than threshold will trigger snap.
  2084. resizer->Drag(gfx::PointF(400.f, 67.f), 0);
  2085. resizer->Drag(gfx::PointF(400.f, 2.f), 0);
  2086. DwellCountdownTimerFireNow();
  2087. resizer->CompleteDrag();
  2088. auto* window_state = WindowState::Get(window_.get());
  2089. base::RunLoop().RunUntilIdle();
  2090. EXPECT_TRUE(window_state->IsMaximized());
  2091. // Check if a horizontal move less than threshold won't trigger snap.
  2092. resizer.reset();
  2093. resizer = CreateResizerForTest(window_.get());
  2094. resizer->Drag(gfx::PointF(1.f, 1.f), 0);
  2095. resizer->Drag(gfx::PointF(100.f, 1.f), 0);
  2096. resizer->CompleteDrag();
  2097. window_state = WindowState::Get(window_.get());
  2098. EXPECT_FALSE(window_state->IsMaximized());
  2099. }
  2100. class PortraitWorkspaceWindowResizerTest : public WorkspaceWindowResizerTest {
  2101. public:
  2102. PortraitWorkspaceWindowResizerTest() = default;
  2103. PortraitWorkspaceWindowResizerTest(
  2104. const PortraitWorkspaceWindowResizerTest&) = delete;
  2105. PortraitWorkspaceWindowResizerTest& operator=(
  2106. const PortraitWorkspaceWindowResizerTest&) = delete;
  2107. ~PortraitWorkspaceWindowResizerTest() override = default;
  2108. // WorkspaceWindowResizerTest:
  2109. void SetUp() override {
  2110. WorkspaceWindowResizerTest::SetUp();
  2111. UpdateDisplay("600x800");
  2112. // Make the window snappable.
  2113. AllowSnap(window_.get());
  2114. }
  2115. };
  2116. // Tests that dragging to an external portrait display updates phantom snap to
  2117. // top/bottom phantom windows instead of left/right.
  2118. TEST_F(PortraitWorkspaceWindowResizerTest, MultiDisplaySnapPhantom) {
  2119. UpdateDisplay("800x600,600x800");
  2120. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  2121. ASSERT_EQ(2U, root_windows.size());
  2122. window_->SetBoundsInScreen(gfx::Rect(0, 0, 50, 60),
  2123. display::Screen::GetScreen()->GetPrimaryDisplay());
  2124. gfx::Rect work_area(
  2125. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get()));
  2126. EXPECT_EQ(root_windows[0], window_->GetRootWindow());
  2127. EXPECT_FLOAT_EQ(1.0f, window_->layer()->opacity());
  2128. std::unique_ptr<WindowResizer> resizer = CreateResizerForTest(window_.get());
  2129. ASSERT_TRUE(resizer.get());
  2130. EXPECT_FALSE(snap_phantom_window_controller());
  2131. // Drag to snap left in landscape display should show left phantom window.
  2132. resizer->Drag(CalculateDragPoint(*resizer, 10, 0), 0);
  2133. EXPECT_TRUE(snap_phantom_window_controller());
  2134. EXPECT_EQ(gfx::Rect(0, 0, work_area.width() / 2, work_area.height()),
  2135. snap_phantom_window_controller()->GetTargetWindowBounds());
  2136. // Drag to snap right in landscape display should show right phantom window.
  2137. resizer->Drag(CalculateDragPoint(*resizer, 799, 0), 0);
  2138. EXPECT_TRUE(snap_phantom_window_controller());
  2139. EXPECT_EQ(gfx::Rect(work_area.width() / 2, 0, work_area.width() / 2,
  2140. work_area.height()),
  2141. snap_phantom_window_controller()->GetTargetWindowBounds());
  2142. display::Display display =
  2143. display::Screen::GetScreen()->GetDisplayNearestPoint(gfx::Point(810, 0));
  2144. Shell::Get()->cursor_manager()->SetDisplay(display);
  2145. work_area = display.work_area();
  2146. // Move the window to the portrait display to snap top with vertical movement
  2147. // more than |kSnapTriggerVerticalMoveThreshold| to show top phantom window.
  2148. resizer->Drag(CalculateDragPoint(*resizer, 1100, 67), 0);
  2149. resizer->Drag(CalculateDragPoint(*resizer, 1100, 2), 0);
  2150. resizer->Drag(CalculateDragPoint(*resizer, 1100, 1), 0);
  2151. EXPECT_TRUE(snap_phantom_window_controller());
  2152. EXPECT_EQ(gfx::Rect(800, 0, work_area.width(), work_area.height() / 2),
  2153. snap_phantom_window_controller()->GetTargetWindowBounds());
  2154. // Move the window to the portrait display. Now the snap bottom should show
  2155. // bottom phantom window.
  2156. resizer->Drag(CalculateDragPoint(*resizer, 1100, 780), 0);
  2157. resizer->Drag(CalculateDragPoint(*resizer, 1100, 781), 0);
  2158. EXPECT_TRUE(snap_phantom_window_controller());
  2159. EXPECT_EQ(gfx::Rect(800, work_area.height() / 2, work_area.width(),
  2160. work_area.height() / 2),
  2161. snap_phantom_window_controller()->GetTargetWindowBounds());
  2162. }
  2163. // Tests that dragging window to top triggers top snap.
  2164. TEST_F(PortraitWorkspaceWindowResizerTest, SnapTop) {
  2165. const gfx::Rect restore_bounds(50, 50, 100, 100);
  2166. window_->SetBounds(restore_bounds);
  2167. const gfx::Rect work_area =
  2168. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get());
  2169. const float work_area_center_x = work_area.CenterPoint().x();
  2170. constexpr int kScreenEdgeInsetForSnappingTop = 8;
  2171. std::unique_ptr<WindowResizer> resizer(
  2172. CreateResizerForTest(window_.get(), gfx::Point(0, 100), HTCAPTION));
  2173. // Drag to a top-snap region should snap top.
  2174. resizer->Drag(
  2175. gfx::PointF(work_area_center_x, kScreenEdgeInsetForSnappingTop + 1), 0);
  2176. EXPECT_FALSE(snap_phantom_window_controller());
  2177. resizer->Drag(gfx::PointF(work_area_center_x, kScreenEdgeInsetForSnappingTop),
  2178. 0);
  2179. auto* phantom_controller = snap_phantom_window_controller();
  2180. ASSERT_TRUE(phantom_controller);
  2181. const gfx::Rect expected_snapped_bounds(work_area.width(),
  2182. work_area.height() / 2);
  2183. EXPECT_EQ(expected_snapped_bounds,
  2184. phantom_controller->GetTargetWindowBounds());
  2185. EXPECT_TRUE(!!snap_phantom_window_controller()->GetMaximizeCueForTesting());
  2186. resizer->CompleteDrag();
  2187. EXPECT_TRUE(WindowState::Get(window_.get())->IsSnapped());
  2188. EXPECT_EQ(expected_snapped_bounds, window_->bounds());
  2189. }
  2190. // Tests that dragging window to bottom area trigger bottom snap.
  2191. TEST_F(PortraitWorkspaceWindowResizerTest, SnapBottom) {
  2192. const gfx::Rect restore_bounds(50, 50, 100, 100);
  2193. window_->SetBounds(restore_bounds);
  2194. const gfx::Rect work_area =
  2195. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get());
  2196. const float work_area_center_x = work_area.CenterPoint().x();
  2197. std::unique_ptr<WindowResizer> resizer(
  2198. CreateResizerForTest(window_.get(), gfx::Point(), HTCAPTION));
  2199. constexpr int kScreenEdgeInsetForSnappingSides = 32;
  2200. EXPECT_FALSE(snap_phantom_window_controller());
  2201. // Drag to a bottom-snap region should snap bottom. Bottom area should
  2202. // be with
  2203. resizer->Drag(
  2204. gfx::PointF(work_area_center_x,
  2205. work_area.bottom() - kScreenEdgeInsetForSnappingSides - 2),
  2206. 0);
  2207. EXPECT_FALSE(snap_phantom_window_controller());
  2208. resizer->Drag(
  2209. gfx::PointF(work_area_center_x,
  2210. work_area.bottom() - kScreenEdgeInsetForSnappingSides - 1),
  2211. 0);
  2212. ASSERT_TRUE(snap_phantom_window_controller());
  2213. const gfx::Rect expected_snapped_bounds(
  2214. 0, work_area.bottom() / 2, work_area.width(), work_area.height() / 2);
  2215. resizer->CompleteDrag();
  2216. EXPECT_TRUE(WindowState::Get(window_.get())->IsSnapped());
  2217. EXPECT_EQ(expected_snapped_bounds, window_->bounds());
  2218. }
  2219. // Tests that in portrait display, holding a window at top position longer can
  2220. // transform top-snap phantom to maximize phantom window. Moreover, top snap
  2221. // phantom displays the maximize cue widget and hides the cue as soon as it
  2222. // transforms to maximize phantom.
  2223. TEST_F(PortraitWorkspaceWindowResizerTest, SnapTopTransitionToMaximize) {
  2224. const gfx::Rect restore_bounds(50, 50, 100, 100);
  2225. window_->SetBounds(restore_bounds);
  2226. const gfx::Rect work_area =
  2227. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get());
  2228. const float work_area_center_x = work_area.CenterPoint().x();
  2229. constexpr int kScreenEdgeInsetForSnappingTop = 8;
  2230. std::unique_ptr<WindowResizer> resizer(
  2231. CreateResizerForTest(window_.get(), gfx::Point(0, 100), HTCAPTION));
  2232. // Drag to a top-snap region.
  2233. resizer->Drag(
  2234. gfx::PointF(work_area_center_x, kScreenEdgeInsetForSnappingTop + 1), 0);
  2235. EXPECT_FALSE(snap_phantom_window_controller());
  2236. resizer->Drag(gfx::PointF(work_area_center_x, kScreenEdgeInsetForSnappingTop),
  2237. 0);
  2238. auto* phantom_controller = snap_phantom_window_controller();
  2239. ASSERT_TRUE(phantom_controller);
  2240. // During dragging to snap top, the top phantom window should show up along
  2241. // with the maximize cue widget.
  2242. const gfx::Rect expected_top_snapped_bounds(work_area.width(),
  2243. work_area.height() / 2);
  2244. EXPECT_EQ(expected_top_snapped_bounds,
  2245. phantom_controller->GetTargetWindowBounds());
  2246. auto* maximize_cue_widget = phantom_controller->GetMaximizeCueForTesting();
  2247. EXPECT_TRUE(!!maximize_cue_widget);
  2248. EXPECT_TRUE(maximize_cue_widget->IsVisible());
  2249. EXPECT_EQ(1.f, maximize_cue_widget->GetLayer()->opacity());
  2250. EXPECT_TRUE(IsDwellCountdownTimerRunning());
  2251. // Once the count down ends, the maximize cue widget is hidden from the view
  2252. // and the top-snap phantom turns into maximize phantom window.
  2253. DwellCountdownTimerFireNow();
  2254. EXPECT_EQ(0.f, maximize_cue_widget->GetLayer()->opacity());
  2255. EXPECT_EQ(work_area, phantom_controller->GetTargetWindowBounds());
  2256. resizer->CompleteDrag();
  2257. EXPECT_TRUE(WindowState::Get(window_.get())->IsMaximized());
  2258. EXPECT_EQ(work_area, window_->bounds());
  2259. }
  2260. // Verifies the behavior of resizing a vertically snapped window.
  2261. TEST_F(PortraitWorkspaceWindowResizerTest, ResizeSnapped) {
  2262. WindowState* window_state = WindowState::Get(window_.get());
  2263. AllowSnap(window_.get());
  2264. const gfx::Rect kInitialBounds(100, 100, 100, 100);
  2265. window_->SetBounds(kInitialBounds);
  2266. window_->Show();
  2267. const gfx::Rect work_area =
  2268. screen_util::GetDisplayWorkAreaBoundsInParent(window_.get());
  2269. const WindowSnapWMEvent snap_top(WM_EVENT_SNAP_PRIMARY);
  2270. window_state->OnWMEvent(&snap_top);
  2271. EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state->GetStateType());
  2272. gfx::Rect expected_snap_bounds =
  2273. gfx::Rect(work_area.width(), work_area.height() / 2);
  2274. EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state->GetStateType());
  2275. EXPECT_EQ(expected_snap_bounds, window_->bounds());
  2276. EXPECT_EQ(kInitialBounds, window_state->GetRestoreBoundsInParent());
  2277. {
  2278. // 1) Resizing a vertically snapped window to make it higher should not
  2279. // unsnap the window.
  2280. std::unique_ptr<WindowResizer> resizer =
  2281. CreateResizerForTest(window_.get(), gfx::Point(), HTBOTTOM);
  2282. resizer->Drag(CalculateDragPoint(*resizer, 0, 30), 0);
  2283. resizer->CompleteDrag();
  2284. EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state->GetStateType());
  2285. expected_snap_bounds.Inset(gfx::Insets::TLBR(0, 0, -30, 0));
  2286. EXPECT_EQ(expected_snap_bounds, window_->bounds());
  2287. EXPECT_EQ(kInitialBounds, window_state->GetRestoreBoundsInParent());
  2288. }
  2289. {
  2290. // 2) Resizing a vertically snapped window horizontally and then undoing
  2291. // the change should not unsnap.
  2292. std::unique_ptr<WindowResizer> resizer =
  2293. CreateResizerForTest(window_.get(), gfx::Point(), HTLEFT);
  2294. resizer->Drag(CalculateDragPoint(*resizer, 30, 0), 0);
  2295. resizer->Drag(CalculateDragPoint(*resizer, 0, 0), 0);
  2296. resizer->CompleteDrag();
  2297. EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state->GetStateType());
  2298. EXPECT_EQ(expected_snap_bounds, window_->bounds());
  2299. EXPECT_EQ(kInitialBounds, window_state->GetRestoreBoundsInParent());
  2300. }
  2301. {
  2302. // 3) Resizing a vertically snapped window horizontally should unsnap.
  2303. std::unique_ptr<WindowResizer> resizer =
  2304. CreateResizerForTest(window_.get(), gfx::Point(), HTLEFT);
  2305. resizer->Drag(CalculateDragPoint(*resizer, 30, 0), 0);
  2306. resizer->CompleteDrag();
  2307. EXPECT_EQ(WindowStateType::kNormal, window_state->GetStateType());
  2308. expected_snap_bounds.Inset(gfx::Insets::TLBR(0, 30, 0, 0));
  2309. EXPECT_EQ(expected_snap_bounds, window_->bounds());
  2310. EXPECT_FALSE(window_state->HasRestoreBounds());
  2311. }
  2312. }
  2313. using MultiOrientationDisplayWorkspaceWindowResizerTest =
  2314. WorkspaceWindowResizerTest;
  2315. // Test WorkspaceWindowResizer functionalities for two displays with different
  2316. // orientation: landscape and portrait. Assertions around dragging near the four
  2317. // edges of the display.
  2318. TEST_F(MultiOrientationDisplayWorkspaceWindowResizerTest, Edge) {
  2319. UpdateDisplay("800x600,500x600");
  2320. // Make the window snappable.
  2321. AllowSnap(window_.get());
  2322. window_->SetBounds(gfx::Rect(20, 30, 400, 60));
  2323. WindowState* window_state = WindowState::Get(window_.get());
  2324. // Test dragging to another display and snapping there.
  2325. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  2326. const gfx::Rect display2_work_area =
  2327. display::Screen::GetScreen()
  2328. ->GetDisplayNearestWindow(root_windows[1])
  2329. .work_area();
  2330. {
  2331. EXPECT_EQ(gfx::Rect(20, 30, 400, 60), window_->GetBoundsInScreen());
  2332. std::unique_ptr<WindowResizer> resizer =
  2333. CreateResizerForTest(window_.get());
  2334. ASSERT_TRUE(resizer.get());
  2335. // TODO(crbug.com/990589): Unit tests should be able to simulate mouse input
  2336. // without having to call |CursorManager::SetDisplay|.
  2337. // Move to the second display.
  2338. // Drag to bottom right area of the second display to trigger the bottom
  2339. // snap if vertical snap is enabled or the right snap otherwise.
  2340. Shell::Get()->cursor_manager()->SetDisplay(
  2341. display::Screen::GetScreen()->GetDisplayNearestWindow(root_windows[1]));
  2342. resizer->Drag(CalculateDragPoint(*resizer, display2_work_area.right(),
  2343. display2_work_area.bottom()),
  2344. 0);
  2345. EXPECT_EQ(root_windows[0], window_->GetRootWindow());
  2346. resizer->CompleteDrag();
  2347. EXPECT_EQ(root_windows[1], window_->GetRootWindow());
  2348. const gfx::Rect secondary_snap_bounds =
  2349. gfx::Rect(0, display2_work_area.height() / 2,
  2350. display2_work_area.width(), display2_work_area.height() / 2);
  2351. EXPECT_EQ(secondary_snap_bounds, window_->bounds());
  2352. EXPECT_EQ(gfx::Rect(820, 30, 400, 60),
  2353. window_state->GetRestoreBoundsInScreen());
  2354. }
  2355. // Restore the window to clear snapped state.
  2356. window_state->Restore();
  2357. {
  2358. // Test dragging from a secondary display and snapping on the same display.
  2359. EXPECT_EQ(gfx::Rect(820, 30, 400, 60), window_->GetBoundsInScreen());
  2360. std::unique_ptr<WindowResizer> resizer =
  2361. CreateResizerForTest(window_.get(), gfx::Point(0, 100));
  2362. ASSERT_TRUE(resizer.get());
  2363. // TODO(crbug.com/990589): Unit tests should be able to simulate mouse input
  2364. // without having to call |CursorManager::SetDisplay|.
  2365. // Drag to top left area of the second display to trigger the top snap
  2366. // if vertical snap is enabled or the bottom snap otherwise.
  2367. Shell::Get()->cursor_manager()->SetDisplay(
  2368. display::Screen::GetScreen()->GetDisplayNearestWindow(root_windows[1]));
  2369. resizer->Drag(CalculateDragPoint(*resizer, 0, -95), 0);
  2370. resizer->Drag(CalculateDragPoint(*resizer, 0, -100), 0);
  2371. resizer->CompleteDrag();
  2372. const gfx::Rect primary_snap_bounds =
  2373. gfx::Rect(display2_work_area.width(), display2_work_area.height() / 2);
  2374. EXPECT_EQ(primary_snap_bounds, window_->bounds());
  2375. EXPECT_EQ(gfx::Rect(820, 30, 400, 60),
  2376. window_state->GetRestoreBoundsInScreen());
  2377. }
  2378. }
  2379. } // namespace ash