workspace_controller_unittest.cc 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. // Copyright 2013 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_controller.h"
  5. #include <map>
  6. #include "ash/public/cpp/shell_window_ids.h"
  7. #include "ash/screen_util.h"
  8. #include "ash/session/session_controller_impl.h"
  9. #include "ash/shelf/shelf.h"
  10. #include "ash/shelf/shelf_layout_manager.h"
  11. #include "ash/shelf/shelf_widget.h"
  12. #include "ash/shell.h"
  13. #include "ash/system/status_area_widget.h"
  14. #include "ash/test/ash_test_base.h"
  15. #include "ash/wm/desks/desks_util.h"
  16. #include "ash/wm/window_state.h"
  17. #include "ash/wm/window_util.h"
  18. #include "ash/wm/wm_event.h"
  19. #include "ash/wm/work_area_insets.h"
  20. #include "ash/wm/workspace/workspace_window_resizer.h"
  21. #include "base/strings/string_number_conversions.h"
  22. #include "ui/aura/client/aura_constants.h"
  23. #include "ui/aura/test/test_window_delegate.h"
  24. #include "ui/aura/test/test_windows.h"
  25. #include "ui/aura/window.h"
  26. #include "ui/base/hit_test.h"
  27. #include "ui/compositor/layer.h"
  28. #include "ui/compositor/layer_animator.h"
  29. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  30. #include "ui/display/screen.h"
  31. #include "ui/events/event_utils.h"
  32. #include "ui/events/test/event_generator.h"
  33. #include "ui/gfx/geometry/insets.h"
  34. #include "ui/gfx/geometry/rect.h"
  35. #include "ui/views/widget/widget.h"
  36. #include "ui/wm/core/window_util.h"
  37. using aura::Window;
  38. namespace ash {
  39. // Returns a string containing the names of all the children of |window| (in
  40. // order). Each entry is separated by a space.
  41. std::string GetWindowNames(const aura::Window* window) {
  42. std::string result;
  43. for (size_t i = 0; i < window->children().size(); ++i) {
  44. if (i != 0)
  45. result += " ";
  46. result += window->children()[i]->GetName();
  47. }
  48. return result;
  49. }
  50. // Returns a string containing the names of windows corresponding to each of the
  51. // child layers of |window|'s layer. Any layers that don't correspond to a child
  52. // Window of |window| are ignored. The result is ordered based on the layer
  53. // ordering.
  54. std::string GetLayerNames(const aura::Window* window) {
  55. typedef std::map<const ui::Layer*, std::string> LayerToWindowNameMap;
  56. LayerToWindowNameMap window_names;
  57. for (size_t i = 0; i < window->children().size(); ++i) {
  58. window_names[window->children()[i]->layer()] =
  59. window->children()[i]->GetName();
  60. }
  61. std::string result;
  62. const std::vector<ui::Layer*>& layers(window->layer()->children());
  63. for (size_t i = 0; i < layers.size(); ++i) {
  64. LayerToWindowNameMap::iterator layer_i = window_names.find(layers[i]);
  65. if (layer_i != window_names.end()) {
  66. if (!result.empty())
  67. result += " ";
  68. result += layer_i->second;
  69. }
  70. }
  71. return result;
  72. }
  73. class WorkspaceControllerTest : public AshTestBase {
  74. public:
  75. WorkspaceControllerTest() = default;
  76. WorkspaceControllerTest(const WorkspaceControllerTest&) = delete;
  77. WorkspaceControllerTest& operator=(const WorkspaceControllerTest&) = delete;
  78. ~WorkspaceControllerTest() override = default;
  79. aura::Window* CreateTestWindowUnparented() {
  80. aura::Window* window = new aura::Window(nullptr);
  81. window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  82. window->SetType(aura::client::WINDOW_TYPE_NORMAL);
  83. window->Init(ui::LAYER_TEXTURED);
  84. return window;
  85. }
  86. aura::Window* CreateTestWindow() {
  87. aura::Window* window = new aura::Window(nullptr);
  88. window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  89. window->SetType(aura::client::WINDOW_TYPE_NORMAL);
  90. window->Init(ui::LAYER_TEXTURED);
  91. ParentWindowInPrimaryRootWindow(window);
  92. return window;
  93. }
  94. aura::Window* CreateBrowserLikeWindow(const gfx::Rect& bounds) {
  95. aura::Window* window = CreateTestWindow();
  96. window->SetBounds(bounds);
  97. WindowState* window_state = WindowState::Get(window);
  98. window_state->SetWindowPositionManaged(true);
  99. window->Show();
  100. return window;
  101. }
  102. aura::Window* CreatePopupLikeWindow(const gfx::Rect& bounds) {
  103. aura::Window* window = CreateTestWindowInShellWithBounds(bounds);
  104. window->Show();
  105. return window;
  106. }
  107. aura::Window* GetDesktop() {
  108. return Shell::GetContainer(Shell::GetPrimaryRootWindow(),
  109. desks_util::GetActiveDeskContainerId());
  110. }
  111. gfx::Rect GetFullscreenBounds(aura::Window* window) {
  112. return display::Screen::GetScreen()
  113. ->GetDisplayNearestWindow(window)
  114. .bounds();
  115. }
  116. ShelfWidget* shelf_widget() { return GetPrimaryShelf()->shelf_widget(); }
  117. ShelfLayoutManager* shelf_layout_manager() {
  118. return GetPrimaryShelf()->shelf_layout_manager();
  119. }
  120. };
  121. // Assertions around adding a normal window.
  122. TEST_F(WorkspaceControllerTest, AddNormalWindowWhenEmpty) {
  123. std::unique_ptr<Window> w1(CreateTestWindow());
  124. w1->SetBounds(gfx::Rect(0, 0, 250, 251));
  125. WindowState* window_state = WindowState::Get(w1.get());
  126. EXPECT_FALSE(window_state->HasRestoreBounds());
  127. w1->Show();
  128. EXPECT_FALSE(window_state->HasRestoreBounds());
  129. ASSERT_TRUE(w1->layer() != NULL);
  130. EXPECT_TRUE(w1->layer()->visible());
  131. EXPECT_EQ("0,0 250x251", w1->bounds().ToString());
  132. EXPECT_EQ(w1.get(), GetDesktop()->children()[0]);
  133. }
  134. // Assertions around maximizing/unmaximizing.
  135. TEST_F(WorkspaceControllerTest, SingleMaximizeWindow) {
  136. std::unique_ptr<Window> w1(CreateTestWindow());
  137. w1->SetBounds(gfx::Rect(0, 0, 250, 251));
  138. w1->Show();
  139. wm::ActivateWindow(w1.get());
  140. EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
  141. ASSERT_TRUE(w1->layer() != NULL);
  142. EXPECT_TRUE(w1->layer()->visible());
  143. EXPECT_EQ("0,0 250x251", w1->bounds().ToString());
  144. // Maximize the window.
  145. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
  146. EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
  147. EXPECT_EQ(w1.get(), GetDesktop()->children()[0]);
  148. EXPECT_EQ(screen_util::GetMaximizedWindowBoundsInParent(w1.get()).width(),
  149. w1->bounds().width());
  150. EXPECT_EQ(screen_util::GetMaximizedWindowBoundsInParent(w1.get()).height(),
  151. w1->bounds().height());
  152. // Restore the window.
  153. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  154. EXPECT_EQ(w1.get(), GetDesktop()->children()[0]);
  155. EXPECT_EQ("0,0 250x251", w1->bounds().ToString());
  156. }
  157. // Assertions around two windows and toggling one to be fullscreen.
  158. TEST_F(WorkspaceControllerTest, FullscreenWithNormalWindow) {
  159. std::unique_ptr<Window> w1(CreateTestWindow());
  160. std::unique_ptr<Window> w2(CreateTestWindow());
  161. w1->SetBounds(gfx::Rect(0, 0, 250, 251));
  162. w1->Show();
  163. ASSERT_TRUE(w1->layer() != NULL);
  164. EXPECT_TRUE(w1->layer()->visible());
  165. w2->SetBounds(gfx::Rect(0, 0, 50, 51));
  166. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
  167. w2->Show();
  168. wm::ActivateWindow(w2.get());
  169. // Both windows should be in the same workspace.
  170. EXPECT_EQ(w1.get(), GetDesktop()->children()[0]);
  171. EXPECT_EQ(w2.get(), GetDesktop()->children()[1]);
  172. gfx::Rect work_area(screen_util::GetMaximizedWindowBoundsInParent(w1.get()));
  173. EXPECT_EQ(work_area.width(), w2->bounds().width());
  174. EXPECT_EQ(work_area.height(), w2->bounds().height());
  175. // Restore w2, which should then go back to one workspace.
  176. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  177. EXPECT_EQ(50, w2->bounds().width());
  178. EXPECT_EQ(51, w2->bounds().height());
  179. EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
  180. }
  181. // Makes sure requests to change the bounds of a normal window go through.
  182. TEST_F(WorkspaceControllerTest, ChangeBoundsOfNormalWindow) {
  183. std::unique_ptr<Window> w1(CreateTestWindow());
  184. w1->Show();
  185. // Setting the bounds should go through since the window is in the normal
  186. // workspace.
  187. w1->SetBounds(gfx::Rect(0, 0, 200, 500));
  188. EXPECT_EQ(200, w1->bounds().width());
  189. EXPECT_EQ(500, w1->bounds().height());
  190. }
  191. // Verifies the bounds is not altered when showing and grid is enabled.
  192. TEST_F(WorkspaceControllerTest, SnapToGrid) {
  193. std::unique_ptr<Window> w1(CreateTestWindowUnparented());
  194. w1->SetBounds(gfx::Rect(1, 6, 25, 30));
  195. ParentWindowInPrimaryRootWindow(w1.get());
  196. // We are not aligning this anymore this way. When the window gets shown
  197. // the window is expected to be handled differently, but this cannot be
  198. // tested with this test. So the result of this test should be that the
  199. // bounds are exactly as passed in.
  200. EXPECT_EQ("1,6 25x30", w1->bounds().ToString());
  201. }
  202. // Assertions around a fullscreen window.
  203. TEST_F(WorkspaceControllerTest, SingleFullscreenWindow) {
  204. std::unique_ptr<Window> w1(CreateTestWindow());
  205. w1->SetBounds(gfx::Rect(0, 0, 250, 251));
  206. // Make the window fullscreen.
  207. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
  208. w1->Show();
  209. wm::ActivateWindow(w1.get());
  210. EXPECT_EQ(w1.get(), GetDesktop()->children()[0]);
  211. EXPECT_EQ(GetFullscreenBounds(w1.get()).width(), w1->bounds().width());
  212. EXPECT_EQ(GetFullscreenBounds(w1.get()).height(), w1->bounds().height());
  213. // Restore the window. Use SHOW_STATE_DEFAULT as that is what we'll end up
  214. // with when using views::Widget.
  215. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_DEFAULT);
  216. EXPECT_EQ("0,0 250x251", w1->bounds().ToString());
  217. EXPECT_EQ(w1.get(), GetDesktop()->children()[0]);
  218. EXPECT_EQ(250, w1->bounds().width());
  219. EXPECT_EQ(251, w1->bounds().height());
  220. // Back to fullscreen.
  221. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
  222. EXPECT_EQ(w1.get(), GetDesktop()->children()[0]);
  223. EXPECT_EQ(GetFullscreenBounds(w1.get()).width(), w1->bounds().width());
  224. EXPECT_EQ(GetFullscreenBounds(w1.get()).height(), w1->bounds().height());
  225. WindowState* window_state = WindowState::Get(w1.get());
  226. ASSERT_TRUE(window_state->HasRestoreBounds());
  227. EXPECT_EQ("0,0 250x251", window_state->GetRestoreBoundsInScreen().ToString());
  228. }
  229. // Assertions around minimizing a single window.
  230. TEST_F(WorkspaceControllerTest, MinimizeSingleWindow) {
  231. std::unique_ptr<Window> w1(CreateTestWindow());
  232. w1->Show();
  233. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
  234. EXPECT_FALSE(w1->layer()->IsDrawn());
  235. EXPECT_TRUE(w1->layer()->GetTargetTransform().IsIdentity());
  236. // Show the window.
  237. w1->Show();
  238. EXPECT_TRUE(WindowState::Get(w1.get())->IsNormalStateType());
  239. EXPECT_TRUE(w1->layer()->IsDrawn());
  240. }
  241. // Assertions around minimizing a fullscreen window.
  242. TEST_F(WorkspaceControllerTest, MinimizeFullscreenWindow) {
  243. // Two windows, w1 normal, w2 fullscreen.
  244. std::unique_ptr<Window> w1(CreateTestWindow());
  245. std::unique_ptr<Window> w2(CreateTestWindow());
  246. w1->Show();
  247. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
  248. w2->Show();
  249. WindowState* w1_state = WindowState::Get(w1.get());
  250. WindowState* w2_state = WindowState::Get(w2.get());
  251. w2_state->Activate();
  252. // Minimize w2.
  253. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
  254. EXPECT_TRUE(w1->layer()->IsDrawn());
  255. EXPECT_FALSE(w2->layer()->IsDrawn());
  256. // Show the window, which should trigger unminimizing.
  257. w2->Show();
  258. w2_state->Activate();
  259. EXPECT_TRUE(w2_state->IsFullscreen());
  260. EXPECT_TRUE(w1->layer()->IsDrawn());
  261. EXPECT_TRUE(w2->layer()->IsDrawn());
  262. // Minimize the window, which should hide the window.
  263. EXPECT_TRUE(w2_state->IsActive());
  264. w2_state->Minimize();
  265. EXPECT_FALSE(w2_state->IsActive());
  266. EXPECT_FALSE(w2->layer()->IsDrawn());
  267. EXPECT_TRUE(w1_state->IsActive());
  268. EXPECT_EQ(w2.get(), GetDesktop()->children()[0]);
  269. EXPECT_EQ(w1.get(), GetDesktop()->children()[1]);
  270. // Make the window normal.
  271. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  272. // Setting back to normal doesn't change the activation.
  273. EXPECT_FALSE(w2_state->IsActive());
  274. EXPECT_TRUE(w1_state->IsActive());
  275. EXPECT_EQ(w2.get(), GetDesktop()->children()[0]);
  276. EXPECT_EQ(w1.get(), GetDesktop()->children()[1]);
  277. EXPECT_TRUE(w2->layer()->IsDrawn());
  278. }
  279. // Verifies ShelfLayoutManager's visibility/auto-hide state is correctly
  280. // updated.
  281. TEST_F(WorkspaceControllerTest, ShelfStateUpdated) {
  282. // Since ShelfLayoutManager queries for mouse location, move the mouse so
  283. // it isn't over the shelf.
  284. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
  285. gfx::Point());
  286. generator.MoveMouseTo(0, 0);
  287. std::unique_ptr<Window> w1(CreateTestWindow());
  288. const gfx::Rect w1_bounds(0, 1, 101, 102);
  289. Shelf* shelf = GetPrimaryShelf();
  290. shelf->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlways);
  291. const gfx::Rect touches_shelf_bounds(
  292. 0, shelf_layout_manager()->GetIdealBounds().y() - 10, 101, 102);
  293. // Move |w1| to overlap the shelf.
  294. w1->SetBounds(touches_shelf_bounds);
  295. w1->Show();
  296. wm::ActivateWindow(w1.get());
  297. w1->SetBounds(w1_bounds);
  298. w1->Show();
  299. wm::ActivateWindow(w1.get());
  300. EXPECT_EQ(SHELF_AUTO_HIDE, shelf->GetVisibilityState());
  301. // Maximize the window.
  302. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
  303. EXPECT_EQ(SHELF_AUTO_HIDE, shelf->GetVisibilityState());
  304. EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->GetAutoHideState());
  305. // Restore.
  306. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  307. EXPECT_EQ(SHELF_AUTO_HIDE, shelf->GetVisibilityState());
  308. EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
  309. // Fullscreen.
  310. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
  311. EXPECT_EQ(SHELF_HIDDEN, shelf->GetVisibilityState());
  312. // Normal.
  313. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  314. EXPECT_EQ(SHELF_AUTO_HIDE, shelf->GetVisibilityState());
  315. EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
  316. w1->SetBounds(w1_bounds);
  317. // Maximize again.
  318. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
  319. EXPECT_EQ(SHELF_AUTO_HIDE, shelf->GetVisibilityState());
  320. EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->GetAutoHideState());
  321. // Minimize.
  322. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
  323. EXPECT_EQ(SHELF_AUTO_HIDE, shelf->GetVisibilityState());
  324. // Since the restore from minimize will restore to the pre-minimize
  325. // state (tested elsewhere), we abandon the current size and restore
  326. // rect and set them to the window.
  327. WindowState* window_state = WindowState::Get(w1.get());
  328. gfx::Rect restore = window_state->GetRestoreBoundsInScreen();
  329. EXPECT_EQ(gfx::Rect(0, 0, 800, 600).ToString(), w1->bounds().ToString());
  330. EXPECT_EQ("0,1 101x102", restore.ToString());
  331. window_state->ClearRestoreBounds();
  332. w1->SetBounds(restore);
  333. // Restore.
  334. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  335. EXPECT_EQ(SHELF_AUTO_HIDE, shelf->GetVisibilityState());
  336. EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
  337. // Create another window, maximized.
  338. std::unique_ptr<Window> w2(CreateTestWindow());
  339. w2->SetBounds(gfx::Rect(10, 11, 250, 251));
  340. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
  341. w2->Show();
  342. wm::ActivateWindow(w2.get());
  343. EXPECT_EQ(SHELF_AUTO_HIDE, shelf->GetVisibilityState());
  344. EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->GetAutoHideState());
  345. EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
  346. EXPECT_EQ(
  347. screen_util::GetMaximizedWindowBoundsInParent(w2->parent()).ToString(),
  348. w2->bounds().ToString());
  349. // Switch to w1.
  350. wm::ActivateWindow(w1.get());
  351. EXPECT_EQ(SHELF_AUTO_HIDE, shelf->GetVisibilityState());
  352. EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
  353. EXPECT_EQ(
  354. screen_util::GetMaximizedWindowBoundsInParent(w2->parent()).ToString(),
  355. w2->bounds().ToString());
  356. // Switch to w2.
  357. wm::ActivateWindow(w2.get());
  358. EXPECT_EQ(SHELF_AUTO_HIDE, shelf->GetVisibilityState());
  359. EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->GetAutoHideState());
  360. EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
  361. EXPECT_EQ(screen_util::GetMaximizedWindowBoundsInParent(w2.get()).ToString(),
  362. w2->bounds().ToString());
  363. }
  364. // Verifies going from maximized to minimized sets the right state for painting
  365. // the background of the launcher.
  366. TEST_F(WorkspaceControllerTest, MinimizeResetsVisibility) {
  367. // TODO(bruthig|xiyuan): Move SessionState setup into AshTestBase or
  368. // AshTestHelper.
  369. SessionInfo info;
  370. info.state = session_manager::SessionState::ACTIVE;
  371. Shell::Get()->session_controller()->SetSessionInfo(info);
  372. std::unique_ptr<Window> w1(CreateTestWindow());
  373. w1->Show();
  374. wm::ActivateWindow(w1.get());
  375. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
  376. EXPECT_EQ(ShelfBackgroundType::kMaximized,
  377. shelf_widget()->GetBackgroundType());
  378. w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
  379. EXPECT_EQ(SHELF_VISIBLE, GetPrimaryShelf()->GetVisibilityState());
  380. EXPECT_EQ(ShelfBackgroundType::kDefaultBg,
  381. shelf_widget()->GetBackgroundType());
  382. }
  383. // Verifies window visibility during various workspace changes.
  384. TEST_F(WorkspaceControllerTest, VisibilityTests) {
  385. std::unique_ptr<Window> w1(CreateTestWindow());
  386. w1->Show();
  387. EXPECT_TRUE(w1->IsVisible());
  388. EXPECT_EQ(1.0f, w1->layer()->GetCombinedOpacity());
  389. // Create another window, activate it and make it fullscreen.
  390. std::unique_ptr<Window> w2(CreateTestWindow());
  391. w2->Show();
  392. wm::ActivateWindow(w2.get());
  393. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
  394. EXPECT_TRUE(w2->IsVisible());
  395. EXPECT_EQ(1.0f, w2->layer()->GetCombinedOpacity());
  396. EXPECT_TRUE(w1->IsVisible());
  397. // Switch to w1. |w1| should be visible on top of |w2|.
  398. wm::ActivateWindow(w1.get());
  399. EXPECT_TRUE(w1->IsVisible());
  400. EXPECT_EQ(1.0f, w1->layer()->GetCombinedOpacity());
  401. EXPECT_TRUE(w2->IsVisible());
  402. // Switch back to |w2|.
  403. wm::ActivateWindow(w2.get());
  404. EXPECT_TRUE(w2->IsVisible());
  405. EXPECT_EQ(1.0f, w2->layer()->GetCombinedOpacity());
  406. EXPECT_TRUE(w1->IsVisible());
  407. // Restore |w2|, both windows should be visible.
  408. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  409. EXPECT_TRUE(w1->IsVisible());
  410. EXPECT_EQ(1.0f, w1->layer()->GetCombinedOpacity());
  411. EXPECT_TRUE(w2->IsVisible());
  412. EXPECT_EQ(1.0f, w2->layer()->GetCombinedOpacity());
  413. // Make |w2| fullscreen again, then close it.
  414. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
  415. w2->Hide();
  416. EXPECT_FALSE(w2->IsVisible());
  417. EXPECT_EQ(1.0f, w1->layer()->GetCombinedOpacity());
  418. EXPECT_TRUE(w1->IsVisible());
  419. // Create |w2| and maximize it.
  420. w2.reset(CreateTestWindow());
  421. w2->Show();
  422. wm::ActivateWindow(w2.get());
  423. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
  424. EXPECT_TRUE(w2->IsVisible());
  425. EXPECT_EQ(1.0f, w2->layer()->GetCombinedOpacity());
  426. EXPECT_TRUE(w1->IsVisible());
  427. // Close |w2|.
  428. w2.reset();
  429. EXPECT_EQ(1.0f, w1->layer()->GetCombinedOpacity());
  430. EXPECT_TRUE(w1->IsVisible());
  431. }
  432. // Verifies windows that are offscreen don't move when switching workspaces.
  433. TEST_F(WorkspaceControllerTest, DontMoveOnSwitch) {
  434. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
  435. gfx::Point());
  436. generator.MoveMouseTo(0, 0);
  437. std::unique_ptr<Window> w1(CreateTestWindow());
  438. const gfx::Rect touches_shelf_bounds(
  439. 0, shelf_layout_manager()->GetIdealBounds().y() - 10, 101, 102);
  440. // Move |w1| to overlap the shelf.
  441. w1->SetBounds(touches_shelf_bounds);
  442. w1->Show();
  443. wm::ActivateWindow(w1.get());
  444. // Create another window and maximize it.
  445. std::unique_ptr<Window> w2(CreateTestWindow());
  446. w2->SetBounds(gfx::Rect(10, 11, 250, 251));
  447. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
  448. w2->Show();
  449. wm::ActivateWindow(w2.get());
  450. // Switch to w1.
  451. wm::ActivateWindow(w1.get());
  452. EXPECT_EQ(touches_shelf_bounds.ToString(), w1->bounds().ToString());
  453. }
  454. // Verifies that windows that are completely offscreen move when switching
  455. // workspaces.
  456. TEST_F(WorkspaceControllerTest, MoveOnSwitch) {
  457. aura::Window* root = Shell::GetPrimaryRootWindow();
  458. ui::test::EventGenerator generator(root, gfx::Point());
  459. generator.MoveMouseTo(0, 0);
  460. std::unique_ptr<Window> w1(CreateTestWindow());
  461. const gfx::Rect w1_bounds(0, shelf_layout_manager()->GetIdealBounds().y(),
  462. 100, 200);
  463. // Move |w1| so that the top edge is the same as the top edge of the shelf.
  464. w1->SetBounds(w1_bounds);
  465. w1->Show();
  466. wm::ActivateWindow(w1.get());
  467. EXPECT_EQ(w1_bounds.ToString(), w1->bounds().ToString());
  468. // Create another window and maximize it.
  469. std::unique_ptr<Window> w2(CreateTestWindow());
  470. w2->SetBounds(gfx::Rect(10, 11, 250, 251));
  471. w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
  472. w2->Show();
  473. wm::ActivateWindow(w2.get());
  474. // Increase the size of the WorkAreaInsets. This would make |w1| fall
  475. // completely out of the display work area.
  476. WorkAreaInsets* work_area_insets = WorkAreaInsets::ForWindow(root);
  477. gfx::Insets insets = work_area_insets->in_session_user_work_area_insets();
  478. insets = gfx::Insets::TLBR(0, 0, insets.bottom() + 30, 0);
  479. work_area_insets->UpdateWorkAreaInsetsForTest(root, gfx::Rect(), insets,
  480. insets);
  481. // Switch to w1. The window should have moved.
  482. wm::ActivateWindow(w1.get());
  483. EXPECT_NE(w1_bounds.ToString(), w1->bounds().ToString());
  484. }
  485. namespace {
  486. // WindowDelegate used by DontCrashOnChangeAndActivate.
  487. class DontCrashOnChangeAndActivateDelegate
  488. : public aura::test::TestWindowDelegate {
  489. public:
  490. DontCrashOnChangeAndActivateDelegate() : window_(NULL) {}
  491. DontCrashOnChangeAndActivateDelegate(
  492. const DontCrashOnChangeAndActivateDelegate&) = delete;
  493. DontCrashOnChangeAndActivateDelegate& operator=(
  494. const DontCrashOnChangeAndActivateDelegate&) = delete;
  495. void set_window(aura::Window* window) { window_ = window; }
  496. // WindowDelegate overrides:
  497. void OnBoundsChanged(const gfx::Rect& old_bounds,
  498. const gfx::Rect& new_bounds) override {
  499. if (window_) {
  500. wm::ActivateWindow(window_);
  501. window_ = NULL;
  502. }
  503. }
  504. private:
  505. aura::Window* window_;
  506. };
  507. } // namespace
  508. // Exercises possible crash in W2. Here's the sequence:
  509. // . minimize a maximized window.
  510. // . remove the window (which happens when switching displays).
  511. // . add the window back.
  512. // . show the window and during the bounds change activate it.
  513. TEST_F(WorkspaceControllerTest, DontCrashOnChangeAndActivate) {
  514. // Force the shelf
  515. Shelf* shelf = GetPrimaryShelf();
  516. shelf->SetAutoHideBehavior(ShelfAutoHideBehavior::kNever);
  517. DontCrashOnChangeAndActivateDelegate delegate;
  518. std::unique_ptr<Window> w1(CreateTestWindowInShellWithDelegate(
  519. &delegate, 1000, gfx::Rect(10, 11, 250, 251)));
  520. w1->Show();
  521. WindowState* w1_state = WindowState::Get(w1.get());
  522. w1_state->Activate();
  523. w1_state->Maximize();
  524. w1_state->Minimize();
  525. w1->parent()->RemoveChild(w1.get());
  526. // Do this so that when we Show() the window a resize occurs and we make the
  527. // window active.
  528. shelf->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlways);
  529. ParentWindowInPrimaryRootWindow(w1.get());
  530. delegate.set_window(w1.get());
  531. w1->Show();
  532. }
  533. // Verifies a window with a transient parent not managed by workspace works.
  534. TEST_F(WorkspaceControllerTest, TransientParent) {
  535. std::unique_ptr<Window> w1(CreateTestWindow());
  536. w1->SetBounds(gfx::Rect(0, 0, 100, 100));
  537. w1->Show();
  538. // Normal window with no transient parent.
  539. std::unique_ptr<Window> w3(CreateTestWindow());
  540. w3->SetBounds(gfx::Rect(10, 11, 250, 251));
  541. w3->Show();
  542. wm::ActivateWindow(w3.get());
  543. // Window with a transient parent.
  544. std::unique_ptr<Window> w2(CreateTestWindowUnparented());
  545. ::wm::AddTransientChild(w1.get(), w2.get());
  546. w2->SetBounds(gfx::Rect(10, 11, 250, 251));
  547. ParentWindowInPrimaryRootWindow(w2.get());
  548. w2->Show();
  549. wm::ActivateWindow(w2.get());
  550. // The window with the transient parent should get added to the same parent as
  551. // the normal window.
  552. EXPECT_EQ(w3->parent(), w2->parent());
  553. }
  554. // Test the placement of newly created windows.
  555. TEST_F(WorkspaceControllerTest, BasicAutoPlacingOnCreate) {
  556. UpdateDisplay("1600x1200");
  557. // Creating a popup handler here to make sure it does not interfere with the
  558. // existing windows.
  559. gfx::Rect source_browser_bounds(16, 32, 640, 320);
  560. std::unique_ptr<aura::Window> browser_window(
  561. CreateBrowserLikeWindow(source_browser_bounds));
  562. // Creating a popup to make sure it does not interfere with the positioning.
  563. std::unique_ptr<aura::Window> browser_popup(
  564. CreatePopupLikeWindow(gfx::Rect(16, 32, 128, 256)));
  565. browser_window->Show();
  566. browser_popup->Show();
  567. { // With a shown window it's size should get returned.
  568. std::unique_ptr<aura::Window> new_browser_window(
  569. CreateBrowserLikeWindow(source_browser_bounds));
  570. // The position should be right flush.
  571. EXPECT_EQ("960,32 640x320", new_browser_window->bounds().ToString());
  572. }
  573. { // With the window shown - but more on the right side then on the left
  574. // side (and partially out of the screen), it should default to the other
  575. // side and inside the screen.
  576. gfx::Rect source_browser_bounds(gfx::Rect(1000, 600, 640, 320));
  577. browser_window->SetBounds(source_browser_bounds);
  578. std::unique_ptr<aura::Window> new_browser_window(
  579. CreateBrowserLikeWindow(source_browser_bounds));
  580. // The position should be left & bottom flush.
  581. EXPECT_EQ("0,600 640x320", new_browser_window->bounds().ToString());
  582. // If the other window was already beyond the point to get right flush
  583. // it will remain where it is.
  584. EXPECT_EQ("1000,600 640x320", browser_window->bounds().ToString());
  585. }
  586. { // Make sure that popups do not get changed.
  587. std::unique_ptr<aura::Window> new_popup_window(
  588. CreatePopupLikeWindow(gfx::Rect(50, 100, 300, 150)));
  589. EXPECT_EQ("50,100 300x150", new_popup_window->bounds().ToString());
  590. }
  591. browser_window->Hide();
  592. { // If a window is there but not shown the default should be centered.
  593. std::unique_ptr<aura::Window> new_browser_window(
  594. CreateBrowserLikeWindow(gfx::Rect(50, 100, 300, 150)));
  595. EXPECT_EQ("650,100 300x150", new_browser_window->bounds().ToString());
  596. }
  597. }
  598. // Test that adding a second window shifts both the first window and its
  599. // transient child.
  600. TEST_F(WorkspaceControllerTest, AutoPlacingMovesTransientChild) {
  601. // Create an auto-positioned window.
  602. std::unique_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
  603. gfx::Rect desktop_area = window1->parent()->bounds();
  604. WindowState::Get(window1.get())->SetWindowPositionManaged(true);
  605. // Hide and then show |window1| to trigger auto-positioning logic.
  606. window1->Hide();
  607. window1->SetBounds(gfx::Rect(16, 32, 300, 300));
  608. window1->Show();
  609. // |window1| should be horizontally centered.
  610. int x_window1 = (desktop_area.width() - 300) / 2;
  611. EXPECT_EQ(base::NumberToString(x_window1) + ",32 300x300",
  612. window1->bounds().ToString());
  613. // Create a |child| window and make it a transient child of |window1|.
  614. std::unique_ptr<Window> child(CreateTestWindowUnparented());
  615. ::wm::AddTransientChild(window1.get(), child.get());
  616. const int x_child = x_window1 + 50;
  617. child->SetBounds(gfx::Rect(x_child, 20, 200, 200));
  618. ParentWindowInPrimaryRootWindow(child.get());
  619. child->Show();
  620. wm::ActivateWindow(child.get());
  621. // The |child| should be where it was created.
  622. EXPECT_EQ(base::NumberToString(x_child) + ",20 200x200",
  623. child->bounds().ToString());
  624. // Create and show a second window forcing the first window and its child to
  625. // move.
  626. std::unique_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
  627. WindowState::Get(window2.get())->SetWindowPositionManaged(true);
  628. // Hide and then show |window2| to trigger auto-positioning logic.
  629. window2->Hide();
  630. window2->SetBounds(gfx::Rect(32, 48, 250, 250));
  631. window2->Show();
  632. // Check that both |window1| and |child| have moved left.
  633. EXPECT_EQ("0,32 300x300", window1->bounds().ToString());
  634. int x = x_child - x_window1;
  635. EXPECT_EQ(base::NumberToString(x) + ",20 200x200",
  636. child->bounds().ToString());
  637. // Check that |window2| has moved right.
  638. x = desktop_area.width() - window2->bounds().width();
  639. EXPECT_EQ(base::NumberToString(x) + ",48 250x250",
  640. window2->bounds().ToString());
  641. }
  642. // Test the basic auto placement of one and or two windows in a "simulated
  643. // session" of sequential window operations.
  644. TEST_F(WorkspaceControllerTest, BasicAutoPlacingOnShowHide) {
  645. // Test 1: In case there is no manageable window, no window should shift.
  646. std::unique_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
  647. window1->SetBounds(gfx::Rect(16, 32, 640, 320));
  648. gfx::Rect desktop_area = window1->parent()->bounds();
  649. std::unique_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
  650. // Trigger the auto window placement function by making it visible.
  651. // Note that the bounds are getting changed while it is invisible.
  652. window2->Hide();
  653. window2->SetBounds(gfx::Rect(32, 48, 256, 512));
  654. window2->Show();
  655. // Check the initial position of the windows is unchanged.
  656. EXPECT_EQ("16,32 640x320", window1->bounds().ToString());
  657. EXPECT_EQ("32,48 256x512", window2->bounds().ToString());
  658. // Remove the second window and make sure that the first window
  659. // does NOT get centered.
  660. window2.reset();
  661. EXPECT_EQ("16,32 640x320", window1->bounds().ToString());
  662. WindowState* window1_state = WindowState::Get(window1.get());
  663. // Test 2: Set up two managed windows and check their auto positioning.
  664. window1_state->SetWindowPositionManaged(true);
  665. std::unique_ptr<aura::Window> window3(CreateTestWindowInShellWithId(2));
  666. WindowState::Get(window3.get())->SetWindowPositionManaged(true);
  667. // To avoid any auto window manager changes due to SetBounds, the window
  668. // gets first hidden and then shown again.
  669. window3->Hide();
  670. window3->SetBounds(gfx::Rect(32, 48, 256, 512));
  671. window3->Show();
  672. // |window1| should be flush left and |window3| flush right.
  673. EXPECT_EQ("0,32 640x320", window1->bounds().ToString());
  674. EXPECT_EQ(
  675. base::NumberToString(desktop_area.width() - window3->bounds().width()) +
  676. ",48 256x512",
  677. window3->bounds().ToString());
  678. // After removing |window3|, |window1| should be centered again.
  679. window3.reset();
  680. EXPECT_EQ(base::NumberToString(
  681. (desktop_area.width() - window1->bounds().width()) / 2) +
  682. ",32 640x320",
  683. window1->bounds().ToString());
  684. // Test 3: Set up a manageable and a non manageable window and check
  685. // positioning.
  686. std::unique_ptr<aura::Window> window4(CreateTestWindowInShellWithId(3));
  687. // To avoid any auto window manager changes due to SetBounds, the window
  688. // gets first hidden and then shown again.
  689. window1->Hide();
  690. window1->SetBounds(gfx::Rect(16, 32, 640, 320));
  691. window4->SetBounds(gfx::Rect(32, 48, 256, 512));
  692. window1->Show();
  693. // |window1| should be centered and |window4| untouched.
  694. EXPECT_EQ(base::NumberToString(
  695. (desktop_area.width() - window1->bounds().width()) / 2) +
  696. ",32 640x320",
  697. window1->bounds().ToString());
  698. EXPECT_EQ("32,48 256x512", window4->bounds().ToString());
  699. // Test4: A single manageable window should get centered.
  700. window4.reset();
  701. window1_state->set_bounds_changed_by_user(false);
  702. // Trigger the auto window placement function by showing (and hiding) it.
  703. window1->Hide();
  704. window1->Show();
  705. // |window1| should be centered.
  706. EXPECT_EQ(base::NumberToString(
  707. (desktop_area.width() - window1->bounds().width()) / 2) +
  708. ",32 640x320",
  709. window1->bounds().ToString());
  710. }
  711. // Test the proper usage of user window movement interaction.
  712. TEST_F(WorkspaceControllerTest, TestUserMovedWindowRepositioning) {
  713. std::unique_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
  714. window1->SetBounds(gfx::Rect(16, 32, 640, 320));
  715. gfx::Rect desktop_area = window1->parent()->bounds();
  716. std::unique_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
  717. window2->SetBounds(gfx::Rect(32, 48, 256, 512));
  718. window1->Hide();
  719. window2->Hide();
  720. WindowState* window1_state = WindowState::Get(window1.get());
  721. WindowState* window2_state = WindowState::Get(window2.get());
  722. window1_state->SetWindowPositionManaged(true);
  723. window2_state->SetWindowPositionManaged(true);
  724. EXPECT_FALSE(window1_state->bounds_changed_by_user());
  725. EXPECT_FALSE(window2_state->bounds_changed_by_user());
  726. // Check that the current location gets preserved if the user has
  727. // positioned it previously.
  728. window1_state->set_bounds_changed_by_user(true);
  729. window1->Show();
  730. EXPECT_EQ("16,32 640x320", window1->bounds().ToString());
  731. // Flag should be still set.
  732. EXPECT_TRUE(window1_state->bounds_changed_by_user());
  733. EXPECT_FALSE(window2_state->bounds_changed_by_user());
  734. // Turn on the second window and make sure that both windows are now
  735. // positionable again (user movement cleared).
  736. window2->Show();
  737. // |window1| should be flush left and |window2| flush right.
  738. EXPECT_EQ("0,32 640x320", window1->bounds().ToString());
  739. EXPECT_EQ(
  740. base::NumberToString(desktop_area.width() - window2->bounds().width()) +
  741. ",48 256x512",
  742. window2->bounds().ToString());
  743. // FLag should now be reset.
  744. EXPECT_FALSE(window1_state->bounds_changed_by_user());
  745. EXPECT_FALSE(window2_state->bounds_changed_by_user());
  746. // Going back to one shown window should keep the state.
  747. window1_state->set_bounds_changed_by_user(true);
  748. window2->Hide();
  749. EXPECT_EQ("0,32 640x320", window1->bounds().ToString());
  750. EXPECT_TRUE(window1_state->bounds_changed_by_user());
  751. }
  752. // Test if the single window will be restored at original position.
  753. TEST_F(WorkspaceControllerTest, TestSingleWindowsRestoredBounds) {
  754. std::unique_ptr<aura::Window> window1(
  755. CreateTestWindowInShellWithBounds(gfx::Rect(100, 100, 100, 100)));
  756. std::unique_ptr<aura::Window> window2(
  757. CreateTestWindowInShellWithBounds(gfx::Rect(110, 110, 100, 100)));
  758. std::unique_ptr<aura::Window> window3(
  759. CreateTestWindowInShellWithBounds(gfx::Rect(120, 120, 100, 100)));
  760. window1->Hide();
  761. window2->Hide();
  762. window3->Hide();
  763. WindowState::Get(window1.get())->SetWindowPositionManaged(true);
  764. WindowState::Get(window2.get())->SetWindowPositionManaged(true);
  765. WindowState::Get(window3.get())->SetWindowPositionManaged(true);
  766. window1->Show();
  767. wm::ActivateWindow(window1.get());
  768. window2->Show();
  769. wm::ActivateWindow(window2.get());
  770. window3->Show();
  771. wm::ActivateWindow(window3.get());
  772. EXPECT_EQ(0, window1->bounds().x());
  773. EXPECT_EQ(window2->GetRootWindow()->bounds().right(),
  774. window2->bounds().right());
  775. EXPECT_EQ(0, window3->bounds().x());
  776. window1->Hide();
  777. EXPECT_EQ(window2->GetRootWindow()->bounds().right(),
  778. window2->bounds().right());
  779. EXPECT_EQ(0, window3->bounds().x());
  780. // Being a single window will retore the original location.
  781. window3->Hide();
  782. wm::ActivateWindow(window2.get());
  783. EXPECT_EQ("110,110 100x100", window2->bounds().ToString());
  784. // Showing the 3rd will push the 2nd window left.
  785. window3->Show();
  786. wm::ActivateWindow(window3.get());
  787. EXPECT_EQ(0, window2->bounds().x());
  788. EXPECT_EQ(window3->GetRootWindow()->bounds().right(),
  789. window3->bounds().right());
  790. // Being a single window will retore the original location.
  791. window2->Hide();
  792. EXPECT_EQ("120,120 100x100", window3->bounds().ToString());
  793. }
  794. // Test that user placed windows go back to their user placement after the user
  795. // closes all other windows.
  796. TEST_F(WorkspaceControllerTest, TestUserHandledWindowRestore) {
  797. std::unique_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
  798. gfx::Rect user_pos = gfx::Rect(16, 42, 640, 320);
  799. window1->SetBounds(user_pos);
  800. WindowState* window1_state = WindowState::Get(window1.get());
  801. window1_state->SetPreAutoManageWindowBounds(user_pos);
  802. gfx::Rect desktop_area = window1->parent()->bounds();
  803. // Create a second window to let the auto manager kick in.
  804. std::unique_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
  805. window2->SetBounds(gfx::Rect(32, 48, 256, 512));
  806. window1->Hide();
  807. window2->Hide();
  808. WindowState::Get(window1.get())->SetWindowPositionManaged(true);
  809. WindowState::Get(window2.get())->SetWindowPositionManaged(true);
  810. window1->Show();
  811. EXPECT_EQ(user_pos.ToString(), window1->bounds().ToString());
  812. window2->Show();
  813. // |window1| should be flush left and |window2| flush right.
  814. EXPECT_EQ("0," + base::NumberToString(user_pos.y()) + " 640x320",
  815. window1->bounds().ToString());
  816. EXPECT_EQ(
  817. base::NumberToString(desktop_area.width() - window2->bounds().width()) +
  818. ",48 256x512",
  819. window2->bounds().ToString());
  820. window2->Hide();
  821. // After the other window get hidden the window has to move back to the
  822. // previous position and the bounds should still be set and unchanged.
  823. EXPECT_EQ(user_pos.ToString(), window1->bounds().ToString());
  824. ASSERT_TRUE(window1_state->pre_auto_manage_window_bounds());
  825. EXPECT_EQ(user_pos.ToString(),
  826. window1_state->pre_auto_manage_window_bounds()->ToString());
  827. }
  828. // Solo window should be restored to the bounds where a user moved to.
  829. TEST_F(WorkspaceControllerTest, TestRestoreToUserModifiedBounds) {
  830. UpdateDisplay("400x300");
  831. gfx::Rect default_bounds(10, 0, 100, 100);
  832. std::unique_ptr<aura::Window> window1(
  833. CreateTestWindowInShellWithBounds(default_bounds));
  834. WindowState* window1_state = WindowState::Get(window1.get());
  835. window1->Hide();
  836. window1_state->SetWindowPositionManaged(true);
  837. window1->Show();
  838. // First window is centered.
  839. EXPECT_EQ("150,0 100x100", window1->bounds().ToString());
  840. std::unique_ptr<aura::Window> window2(
  841. CreateTestWindowInShellWithBounds(default_bounds));
  842. WindowState* window2_state = WindowState::Get(window2.get());
  843. window2->Hide();
  844. window2_state->SetWindowPositionManaged(true);
  845. window2->Show();
  846. // Auto positioning pushes windows to each sides.
  847. EXPECT_EQ("0,0 100x100", window1->bounds().ToString());
  848. EXPECT_EQ("300,0 100x100", window2->bounds().ToString());
  849. window2->Hide();
  850. // Restores to the center.
  851. EXPECT_EQ("150,0 100x100", window1->bounds().ToString());
  852. // A user moved the window.
  853. std::unique_ptr<WindowResizer> resizer(
  854. CreateWindowResizer(window1.get(), gfx::PointF(), HTCAPTION,
  855. ::wm::WINDOW_MOVE_SOURCE_MOUSE)
  856. .release());
  857. gfx::PointF location = resizer->GetInitialLocation();
  858. location.Offset(-50, 0);
  859. resizer->Drag(location, 0);
  860. resizer->CompleteDrag();
  861. window1_state->set_bounds_changed_by_user(true);
  862. window1->SetBounds(gfx::Rect(100, 0, 100, 100));
  863. window2->Show();
  864. EXPECT_EQ("0,0 100x100", window1->bounds().ToString());
  865. EXPECT_EQ("300,0 100x100", window2->bounds().ToString());
  866. // Window 1 should be restored to the user modified bounds.
  867. window2->Hide();
  868. EXPECT_EQ("100,0 100x100", window1->bounds().ToString());
  869. }
  870. // Test that a window from normal to minimize will repos the remaining.
  871. TEST_F(WorkspaceControllerTest, ToMinimizeRepositionsRemaining) {
  872. std::unique_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
  873. WindowState* window1_state = WindowState::Get(window1.get());
  874. window1_state->SetWindowPositionManaged(true);
  875. window1->SetBounds(gfx::Rect(16, 32, 640, 320));
  876. gfx::Rect desktop_area = window1->parent()->bounds();
  877. std::unique_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
  878. WindowState* window2_state = WindowState::Get(window2.get());
  879. window2_state->SetWindowPositionManaged(true);
  880. window2->SetBounds(gfx::Rect(32, 48, 256, 512));
  881. window1_state->Minimize();
  882. // |window2| should be centered now.
  883. EXPECT_TRUE(window2->IsVisible());
  884. EXPECT_TRUE(window2_state->IsNormalStateType());
  885. EXPECT_EQ(base::NumberToString(
  886. (desktop_area.width() - window2->bounds().width()) / 2) +
  887. ",48 256x512",
  888. window2->bounds().ToString());
  889. window1_state->Restore();
  890. // |window1| should be flush right and |window3| flush left.
  891. EXPECT_EQ(
  892. base::NumberToString(desktop_area.width() - window1->bounds().width()) +
  893. ",32 640x320",
  894. window1->bounds().ToString());
  895. EXPECT_EQ("0,48 256x512", window2->bounds().ToString());
  896. }
  897. // Test that minimizing an initially maximized window will repos the remaining.
  898. TEST_F(WorkspaceControllerTest, MaxToMinRepositionsRemaining) {
  899. std::unique_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
  900. WindowState* window1_state = WindowState::Get(window1.get());
  901. window1_state->SetWindowPositionManaged(true);
  902. gfx::Rect desktop_area = window1->parent()->bounds();
  903. std::unique_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
  904. WindowState* window2_state = WindowState::Get(window2.get());
  905. window2_state->SetWindowPositionManaged(true);
  906. window2->SetBounds(gfx::Rect(32, 48, 256, 512));
  907. window1_state->Maximize();
  908. window1_state->Minimize();
  909. // |window2| should be centered now.
  910. EXPECT_TRUE(window2->IsVisible());
  911. EXPECT_TRUE(window2_state->IsNormalStateType());
  912. EXPECT_EQ(base::NumberToString(
  913. (desktop_area.width() - window2->bounds().width()) / 2) +
  914. ",48 256x512",
  915. window2->bounds().ToString());
  916. }
  917. // Test that nomral, maximize, minimizing will repos the remaining.
  918. TEST_F(WorkspaceControllerTest, NormToMaxToMinRepositionsRemaining) {
  919. std::unique_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
  920. window1->SetBounds(gfx::Rect(16, 32, 640, 320));
  921. WindowState* window1_state = WindowState::Get(window1.get());
  922. window1_state->SetWindowPositionManaged(true);
  923. gfx::Rect desktop_area = window1->parent()->bounds();
  924. std::unique_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
  925. WindowState* window2_state = WindowState::Get(window2.get());
  926. window2_state->SetWindowPositionManaged(true);
  927. window2->SetBounds(gfx::Rect(32, 40, 256, 512));
  928. // Trigger the auto window placement function by showing (and hiding) it.
  929. window1->Hide();
  930. window1->Show();
  931. // |window1| should be flush right and |window3| flush left.
  932. EXPECT_EQ(
  933. base::NumberToString(desktop_area.width() - window1->bounds().width()) +
  934. ",32 640x320",
  935. window1->bounds().ToString());
  936. EXPECT_EQ("0,40 256x512", window2->bounds().ToString());
  937. window1_state->Maximize();
  938. window1_state->Minimize();
  939. // |window2| should be centered now.
  940. EXPECT_TRUE(window2->IsVisible());
  941. EXPECT_TRUE(window2_state->IsNormalStateType());
  942. EXPECT_EQ(base::NumberToString(
  943. (desktop_area.width() - window2->bounds().width()) / 2) +
  944. ",40 256x512",
  945. window2->bounds().ToString());
  946. }
  947. // Test that nomral, maximize, normal will repos the remaining.
  948. TEST_F(WorkspaceControllerTest, NormToMaxToNormRepositionsRemaining) {
  949. std::unique_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
  950. window1->SetBounds(gfx::Rect(16, 32, 640, 320));
  951. WindowState* window1_state = WindowState::Get(window1.get());
  952. window1_state->SetWindowPositionManaged(true);
  953. gfx::Rect desktop_area = window1->parent()->bounds();
  954. std::unique_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
  955. WindowState::Get(window2.get())->SetWindowPositionManaged(true);
  956. window2->SetBounds(gfx::Rect(32, 40, 256, 512));
  957. // Trigger the auto window placement function by showing (and hiding) it.
  958. window1->Hide();
  959. window1->Show();
  960. // |window1| should be flush right and |window3| flush left.
  961. EXPECT_EQ(
  962. base::NumberToString(desktop_area.width() - window1->bounds().width()) +
  963. ",32 640x320",
  964. window1->bounds().ToString());
  965. EXPECT_EQ("0,40 256x512", window2->bounds().ToString());
  966. window1_state->Maximize();
  967. window1_state->Restore();
  968. // |window1| should be flush right and |window2| flush left.
  969. EXPECT_EQ(
  970. base::NumberToString(desktop_area.width() - window1->bounds().width()) +
  971. ",32 640x320",
  972. window1->bounds().ToString());
  973. EXPECT_EQ("0,40 256x512", window2->bounds().ToString());
  974. }
  975. // Test that animations are triggered.
  976. TEST_F(WorkspaceControllerTest, AnimatedNormToMaxToNormRepositionsRemaining) {
  977. ui::ScopedAnimationDurationScaleMode test_duration_mode(
  978. ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
  979. std::unique_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
  980. window1->Hide();
  981. window1->SetBounds(gfx::Rect(16, 32, 640, 320));
  982. gfx::Rect desktop_area = window1->parent()->bounds();
  983. std::unique_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
  984. window2->Hide();
  985. window2->SetBounds(gfx::Rect(32, 48, 256, 512));
  986. WindowState::Get(window1.get())->SetWindowPositionManaged(true);
  987. WindowState::Get(window2.get())->SetWindowPositionManaged(true);
  988. // Make sure nothing is animating.
  989. window1->layer()->GetAnimator()->StopAnimating();
  990. window2->layer()->GetAnimator()->StopAnimating();
  991. window2->Show();
  992. // The second window should now animate.
  993. EXPECT_FALSE(window1->layer()->GetAnimator()->is_animating());
  994. EXPECT_TRUE(window2->layer()->GetAnimator()->is_animating());
  995. window2->layer()->GetAnimator()->StopAnimating();
  996. window1->Show();
  997. EXPECT_TRUE(window1->layer()->GetAnimator()->is_animating());
  998. EXPECT_TRUE(window2->layer()->GetAnimator()->is_animating());
  999. window1->layer()->GetAnimator()->StopAnimating();
  1000. window2->layer()->GetAnimator()->StopAnimating();
  1001. // |window1| should be flush right and |window2| flush left.
  1002. EXPECT_EQ(
  1003. base::NumberToString(desktop_area.width() - window1->bounds().width()) +
  1004. ",32 640x320",
  1005. window1->bounds().ToString());
  1006. EXPECT_EQ("0,48 256x512", window2->bounds().ToString());
  1007. }
  1008. // This tests simulates a browser and an app and verifies the ordering of the
  1009. // windows and layers doesn't get out of sync as various operations occur. Its
  1010. // really testing code in FocusController, but easier to simulate here. Just as
  1011. // with a real browser the browser here has a transient child window
  1012. // (corresponds to the status bubble).
  1013. TEST_F(WorkspaceControllerTest, VerifyLayerOrdering) {
  1014. std::unique_ptr<Window> browser(aura::test::CreateTestWindowWithDelegate(
  1015. NULL, aura::client::WINDOW_TYPE_NORMAL, gfx::Rect(5, 6, 7, 8), NULL));
  1016. browser->SetName("browser");
  1017. ParentWindowInPrimaryRootWindow(browser.get());
  1018. browser->Show();
  1019. wm::ActivateWindow(browser.get());
  1020. // |status_bubble| is made a transient child of |browser| and as a result
  1021. // owned by |browser|.
  1022. aura::test::TestWindowDelegate* status_bubble_delegate =
  1023. aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate();
  1024. status_bubble_delegate->set_can_focus(false);
  1025. Window* status_bubble = aura::test::CreateTestWindowWithDelegate(
  1026. status_bubble_delegate, aura::client::WINDOW_TYPE_POPUP,
  1027. gfx::Rect(5, 6, 7, 8), NULL);
  1028. ::wm::AddTransientChild(browser.get(), status_bubble);
  1029. ParentWindowInPrimaryRootWindow(status_bubble);
  1030. status_bubble->SetName("status_bubble");
  1031. std::unique_ptr<Window> app(aura::test::CreateTestWindowWithDelegate(
  1032. NULL, aura::client::WINDOW_TYPE_NORMAL, gfx::Rect(5, 6, 7, 8), NULL));
  1033. app->SetName("app");
  1034. ParentWindowInPrimaryRootWindow(app.get());
  1035. aura::Window* parent = browser->parent();
  1036. app->Show();
  1037. wm::ActivateWindow(app.get());
  1038. EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
  1039. // Minimize the app, focus should go the browser.
  1040. app->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
  1041. EXPECT_TRUE(wm::IsActiveWindow(browser.get()));
  1042. EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
  1043. // Minimize the browser (neither windows are focused).
  1044. browser->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
  1045. EXPECT_FALSE(wm::IsActiveWindow(browser.get()));
  1046. EXPECT_FALSE(wm::IsActiveWindow(app.get()));
  1047. EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
  1048. // Show the browser (which should restore it).
  1049. browser->Show();
  1050. EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
  1051. // Activate the browser.
  1052. wm::ActivateWindow(browser.get());
  1053. EXPECT_TRUE(wm::IsActiveWindow(browser.get()));
  1054. EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
  1055. // Restore the app. This differs from above code for |browser| as internally
  1056. // the app code does this. Restoring this way or using Show() should not make
  1057. // a difference.
  1058. app->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  1059. EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
  1060. // Activate the app.
  1061. wm::ActivateWindow(app.get());
  1062. EXPECT_TRUE(wm::IsActiveWindow(app.get()));
  1063. EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
  1064. }
  1065. // Test that minimizing and restoring a snapped window should restore to the
  1066. // snapped bounds. When a window is created and snapped, it must be a
  1067. // user-initiated operation, no need to do rearrangement for restoring this
  1068. // window (crbug.com/692175).
  1069. TEST_F(WorkspaceControllerTest, RestoreMinimizedSnappedWindow) {
  1070. // Create an auto-positioned window.
  1071. std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
  1072. WindowState* window_state = WindowState::Get(window.get());
  1073. window_state->SetWindowPositionManaged(true);
  1074. window->SetBounds(gfx::Rect(10, 20, 100, 200));
  1075. window->Show();
  1076. // Left snap |window|.
  1077. EXPECT_FALSE(window_state->bounds_changed_by_user());
  1078. const WindowSnapWMEvent snap_left(WM_EVENT_SNAP_PRIMARY);
  1079. window_state->OnWMEvent(&snap_left);
  1080. const gfx::Rect work_area =
  1081. display::Screen::GetScreen()
  1082. ->GetDisplayNearestPoint(window->bounds().origin())
  1083. .work_area();
  1084. gfx::Rect snapped_bounds(work_area.x(), work_area.y(), work_area.width() / 2,
  1085. work_area.height());
  1086. EXPECT_EQ(snapped_bounds, window->bounds());
  1087. EXPECT_TRUE(window_state->bounds_changed_by_user());
  1088. // Minimize and Restore |window|, the restored bounds should be equal to the
  1089. // bounds of left snapped state.
  1090. window_state->Minimize();
  1091. window_state->Restore();
  1092. EXPECT_EQ(snapped_bounds, window->bounds());
  1093. }
  1094. // Verifies that a new maximized window becomes visible after its activation
  1095. // is requested, even though it does not become activated because a system
  1096. // modal window is active.
  1097. TEST_F(WorkspaceControllerTest, SwitchFromModal) {
  1098. std::unique_ptr<Window> modal_window(CreateTestWindowUnparented());
  1099. modal_window->SetBounds(gfx::Rect(10, 11, 21, 22));
  1100. modal_window->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_SYSTEM);
  1101. ParentWindowInPrimaryRootWindow(modal_window.get());
  1102. modal_window->Show();
  1103. wm::ActivateWindow(modal_window.get());
  1104. std::unique_ptr<Window> maximized_window(CreateTestWindow());
  1105. maximized_window->SetProperty(aura::client::kShowStateKey,
  1106. ui::SHOW_STATE_MAXIMIZED);
  1107. maximized_window->Show();
  1108. wm::ActivateWindow(maximized_window.get());
  1109. EXPECT_TRUE(maximized_window->IsVisible());
  1110. }
  1111. // Verifies that when dragging a window autohidden shelf stays hidden during
  1112. // and after the drag.
  1113. TEST_F(WorkspaceControllerTest, DragWindowKeepsShelfAutohidden) {
  1114. aura::test::TestWindowDelegate delegate;
  1115. delegate.set_window_component(HTCAPTION);
  1116. std::unique_ptr<Window> w1(aura::test::CreateTestWindowWithDelegate(
  1117. &delegate, aura::client::WINDOW_TYPE_NORMAL, gfx::Rect(5, 5, 100, 50),
  1118. NULL));
  1119. ParentWindowInPrimaryRootWindow(w1.get());
  1120. Shelf* shelf = GetPrimaryShelf();
  1121. shelf->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlways);
  1122. EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->GetAutoHideState());
  1123. // Drag very little.
  1124. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
  1125. gfx::Point());
  1126. generator.MoveMouseTo(10, 10);
  1127. generator.PressLeftButton();
  1128. generator.MoveMouseTo(12, 12);
  1129. // Shelf should be hidden during and after the drag.
  1130. EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->GetAutoHideState());
  1131. generator.ReleaseLeftButton();
  1132. EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->GetAutoHideState());
  1133. }
  1134. // Verifies that events are targeted properly just outside the window edges.
  1135. TEST_F(WorkspaceControllerTest, WindowEdgeHitTest) {
  1136. aura::test::TestWindowDelegate d_first, d_second;
  1137. std::unique_ptr<Window> first(aura::test::CreateTestWindowWithDelegate(
  1138. &d_first, 123, gfx::Rect(20, 10, 100, 50), NULL));
  1139. ParentWindowInPrimaryRootWindow(first.get());
  1140. first->Show();
  1141. std::unique_ptr<Window> second(aura::test::CreateTestWindowWithDelegate(
  1142. &d_second, 234, gfx::Rect(30, 40, 40, 10), NULL));
  1143. ParentWindowInPrimaryRootWindow(second.get());
  1144. second->Show();
  1145. aura::Window* root = first->GetRootWindow();
  1146. ui::EventTargeter* targeter =
  1147. root->GetHost()->dispatcher()->GetDefaultEventTargeter();
  1148. // The windows overlap, and |second| is on top of |first|. Events targeted
  1149. // slightly outside the edges of the |second| window should still be targeted
  1150. // to |second| to allow resizing the windows easily.
  1151. const int kNumPoints = 4;
  1152. struct {
  1153. const char* direction;
  1154. gfx::Point location;
  1155. } points[kNumPoints] = {
  1156. {"left", gfx::Point(28, 45)}, // outside the left edge.
  1157. {"top", gfx::Point(50, 38)}, // outside the top edge.
  1158. {"right", gfx::Point(72, 45)}, // outside the right edge.
  1159. {"bottom", gfx::Point(50, 52)}, // outside the bottom edge.
  1160. };
  1161. // Do two iterations, first without any transform on |second|, and the second
  1162. // time after applying some transform on |second| so that it doesn't get
  1163. // targeted.
  1164. for (int times = 0; times < 2; ++times) {
  1165. SCOPED_TRACE(times == 0 ? "Without transform" : "With transform");
  1166. aura::Window* expected_target = times == 0 ? second.get() : first.get();
  1167. for (int i = 0; i < kNumPoints; ++i) {
  1168. SCOPED_TRACE(points[i].direction);
  1169. const gfx::Point& location = points[i].location;
  1170. ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, location, location,
  1171. ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
  1172. ui::EventTarget* target = targeter->FindTargetForEvent(root, &mouse);
  1173. EXPECT_EQ(expected_target, target);
  1174. ui::TouchEvent touch(ui::ET_TOUCH_PRESSED, location,
  1175. ui::EventTimeForNow(),
  1176. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  1177. target = targeter->FindTargetForEvent(root, &touch);
  1178. EXPECT_EQ(expected_target, target);
  1179. }
  1180. // Apply a transform on |second|. After the transform is applied, the window
  1181. // should no longer be targeted.
  1182. gfx::Transform transform;
  1183. transform.Translate(70, 40);
  1184. second->SetTransform(transform);
  1185. }
  1186. }
  1187. } // namespace ash