m65 merge: cros: fix cannot minimize window after leaving tablet mode

changes:
When leaving tablet mode, we will restore old state, which will finally
call BaseState::UpdateMinimizedState. If a window is minimized at this
time, it will set |aura::client::kPreMinimizedShowStateKey| to
minimized show state. It doesn't make sense to set pre-minimized show
state to minimized show state, which could leave us in a bad window state.

TBR=jamescook@chromium.org

(cherry picked from commit b6f3f2bea1a5b9af6694611cc55c304de265956b)

Bug: 783310, 810857
Test: emulator test and added test coverage
Change-Id: I0c084774a838c0c0b9f3b516011ff31679ae770b
Reviewed-on: https://chromium-review.googlesource.com/914981
Commit-Queue: Qiang Xu <warx@google.com>
Reviewed-by: James Cook <jamescook@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#536285}
Reviewed-on: https://chromium-review.googlesource.com/927357
Reviewed-by: Qiang Xu <warx@google.com>
Cr-Commit-Position: refs/branch-heads/3325@{#517}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/ash/wm/base_state.cc b/ash/wm/base_state.cc
index ba9fedc..1929c42 100644
--- a/ash/wm/base_state.cc
+++ b/ash/wm/base_state.cc
@@ -93,10 +93,12 @@
     mojom::WindowStateType previous_state_type) {
   aura::Window* window = window_state->window();
   if (window_state->IsMinimized()) {
-    // Save the previous show state so that we can correctly restore it after
-    // exiting the minimized mode.
-    window->SetProperty(aura::client::kPreMinimizedShowStateKey,
-                        ToWindowShowState(previous_state_type));
+    // Save the previous show state when it is not minimized so that we can
+    // correctly restore it after exiting the minimized mode.
+    if (previous_state_type != mojom::WindowStateType::MINIMIZED) {
+      window->SetProperty(aura::client::kPreMinimizedShowStateKey,
+                          ToWindowShowState(previous_state_type));
+    }
     ::wm::SetWindowVisibilityAnimationType(
         window, WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE);
 
diff --git a/ash/wm/tablet_mode/tablet_mode_window_manager_unittest.cc b/ash/wm/tablet_mode/tablet_mode_window_manager_unittest.cc
index 918e493..f387196 100644
--- a/ash/wm/tablet_mode/tablet_mode_window_manager_unittest.cc
+++ b/ash/wm/tablet_mode/tablet_mode_window_manager_unittest.cc
@@ -881,6 +881,26 @@
   EXPECT_TRUE(window->IsVisible());
 }
 
+// Tests that minimized window can restore to pre-minimized show state after
+// entering and leaving tablet mode (https://crbug.com/783310).
+TEST_F(TabletModeWindowManagerTest, MinimizedEnterAndLeaveTabletMode) {
+  gfx::Rect rect(10, 10, 100, 100);
+  std::unique_ptr<aura::Window> window(
+      CreateWindow(aura::client::WINDOW_TYPE_NORMAL, rect));
+  wm::WindowState* window_state = wm::GetWindowState(window.get());
+  window_state->Minimize();
+  EXPECT_TRUE(window_state->IsMinimized());
+  Shell::Get()->tablet_mode_controller()->EnableTabletModeWindowManager(true);
+  EXPECT_TRUE(window_state->IsMinimized());
+  Shell::Get()->tablet_mode_controller()->EnableTabletModeWindowManager(false);
+  EXPECT_TRUE(window_state->IsMinimized());
+
+  window_state->Unminimize();
+  EXPECT_FALSE(window_state->IsMinimized());
+  window_state->Minimize();
+  EXPECT_TRUE(window_state->IsMinimized());
+}
+
 // Check that a full screen window remains full screen upon entering maximize
 // mode. Furthermore, checks that this window is not full screen upon exiting
 // tablet mode if it was un-full-screened while in tablet mode.