Set Ash material design mode in tests properly

Currently, in Ash tests, material design mode is set after call to
AshTestBase::SetUp(). This is not right as some setup in AshTestBase
might depend on material mode. This CL allows setting material mode
before calling AshTestBase::SetUp().

BUG=620093

Review-Url: https://codereview.chromium.org/2186363002
Cr-Commit-Position: refs/heads/master@{#408837}
diff --git a/ash/shelf/shelf_view_unittest.cc b/ash/shelf/shelf_view_unittest.cc
index 17ebe18..6d88368b 100644
--- a/ash/shelf/shelf_view_unittest.cc
+++ b/ash/shelf/shelf_view_unittest.cc
@@ -2116,6 +2116,8 @@
 
 }  // namespace
 
+// Test fixture that forces material design mode in order to test ink drop
+// ripples on shelf.
 class ShelfViewInkDropTest : public ShelfViewTest {
  public:
   ShelfViewInkDropTest() {}
@@ -2125,20 +2127,9 @@
     shell_delegate_ = new TestAppListShellDelegate;
     ash_test_helper()->set_test_shell_delegate(shell_delegate_);
 
+    set_material_mode(ash::MaterialDesignController::MATERIAL_EXPERIMENTAL);
+
     ShelfViewTest::SetUp();
-
-    // TODO(mohsen): Ideally, we would want to set material mode before calling
-    // ShelfViewTest::SetUp() so that everything is set up with the correct
-    // material mode. Currently, this is not possible as it expects material
-    // mode be UNINITIALIZED. (See https://crbug.com/620093)
-    ash_md_controller_.reset(new ash::test::MaterialDesignControllerTestAPI(
-        ash::MaterialDesignController::MATERIAL_EXPERIMENTAL));
-  }
-
-  void TearDown() override {
-    ash_md_controller_.reset();
-
-    ShelfViewTest::TearDown();
   }
 
  protected:
@@ -2179,9 +2170,6 @@
     shell_delegate_->app_list_presenter()->FinishVisibilityChange();
   }
 
-  std::unique_ptr<ash::test::MaterialDesignControllerTestAPI>
-      ash_md_controller_;
-
   TestAppListShellDelegate* shell_delegate_ = nullptr;  // Owned by Shell.
 
   AppListButton* app_list_button_ = nullptr;
diff --git a/ash/test/ash_md_test_base.cc b/ash/test/ash_md_test_base.cc
index a0edbd35..fb8f24686 100644
--- a/ash/test/ash_md_test_base.cc
+++ b/ash/test/ash_md_test_base.cc
@@ -15,28 +15,31 @@
 AshMDTestBase::~AshMDTestBase() {}
 
 void AshMDTestBase::SetUp() {
-  AshTestBase::SetUp();
+  int non_md_shelf_size = 0;
+  int non_md_auto_hide_shelf_size = 0;
+  int md_shelf_size = 0;
+  int md_auto_hide_shelf_size = 0;
 
-  material_design_state_.reset(new test::MaterialDesignControllerTestAPI(
-      MaterialDesignController::Mode::NON_MATERIAL));
-  const int non_md_shelf_size = GetShelfConstant(SHELF_SIZE);
-  const int non_md_auto_hide_shelf_size =
-      GetShelfConstant(SHELF_INSETS_FOR_AUTO_HIDE);
+  {
+    test::MaterialDesignControllerTestAPI md_state(
+        MaterialDesignController::Mode::NON_MATERIAL);
+    non_md_shelf_size = GetShelfConstant(SHELF_SIZE);
+    non_md_auto_hide_shelf_size = GetShelfConstant(SHELF_INSETS_FOR_AUTO_HIDE);
+  }
 
-  material_design_state_.reset(
-      new test::MaterialDesignControllerTestAPI(GetParam()));
-  const int md_state_shelf_size = GetShelfConstant(SHELF_SIZE);
-  const int md_state_auto_hide_shelf_size =
-      GetShelfConstant(SHELF_INSETS_FOR_AUTO_HIDE);
+  {
+    test::MaterialDesignControllerTestAPI md_state(GetParam());
+    md_shelf_size = GetShelfConstant(SHELF_SIZE);
+    md_auto_hide_shelf_size = GetShelfConstant(SHELF_INSETS_FOR_AUTO_HIDE);
+  }
 
-  md_maximized_window_height_offset_ = non_md_shelf_size - md_state_shelf_size;
+  md_maximized_window_height_offset_ = non_md_shelf_size - md_shelf_size;
   md_auto_hidden_shelf_height_offset_ =
-      non_md_auto_hide_shelf_size - md_state_auto_hide_shelf_size;
-}
+      non_md_auto_hide_shelf_size - md_auto_hide_shelf_size;
 
-void AshMDTestBase::TearDown() {
-  material_design_state_.reset();
-  AshTestBase::TearDown();
+  set_material_mode(GetParam());
+
+  AshTestBase::SetUp();
 }
 
 int AshMDTestBase::GetMdMaximizedWindowHeightOffset() {
diff --git a/ash/test/ash_md_test_base.h b/ash/test/ash_md_test_base.h
index 1e34a40..e6cf9c46 100644
--- a/ash/test/ash_md_test_base.h
+++ b/ash/test/ash_md_test_base.h
@@ -21,14 +21,11 @@
 
   // AshTestBase:
   void SetUp() override;
-  void TearDown() override;
 
   int GetMdMaximizedWindowHeightOffset();
   int GetMdAutoHiddenShelfHeightOffset();
 
  private:
-  std::unique_ptr<MaterialDesignControllerTestAPI> material_design_state_;
-
   // The material design shelf is taller (by 1px) so use this offset to
   // adjust the expected height of a maximized window.
   int md_maximized_window_height_offset_ = 0;
diff --git a/ash/test/ash_test_base.cc b/ash/test/ash_test_base.cc
index c5c301b9..e7ee5ccd9 100644
--- a/ash/test/ash_test_base.cc
+++ b/ash/test/ash_test_base.cc
@@ -91,7 +91,10 @@
 /////////////////////////////////////////////////////////////////////////////
 
 AshTestBase::AshTestBase()
-    : setup_called_(false), teardown_called_(false), start_session_(true) {
+    : setup_called_(false),
+      teardown_called_(false),
+      start_session_(true),
+      material_mode_(MaterialDesignController::Mode::UNINITIALIZED) {
 #if defined(USE_X11)
   // This is needed for tests which use this base class but are run in browser
   // test binaries so don't get the default initialization in the unit test
@@ -130,7 +133,8 @@
 #if defined(OS_WIN)
   ui::test::SetUsePopupAsRootWindowForTest(true);
 #endif
-  ash_test_helper_->SetUp(start_session_);
+
+  ash_test_helper_->SetUp(start_session_, material_mode_);
 
   Shell::GetPrimaryRootWindow()->Show();
   Shell::GetPrimaryRootWindow()->GetHost()->Show();
diff --git a/ash/test/ash_test_base.h b/ash/test/ash_test_base.h
index 288dbb5..43bdb17 100644
--- a/ash/test/ash_test_base.h
+++ b/ash/test/ash_test_base.h
@@ -10,6 +10,7 @@
 #include <memory>
 #include <string>
 
+#include "ash/common/material_design/material_design_controller.h"
 #include "base/compiler_specific.h"
 #include "base/macros.h"
 #include "base/message_loop/message_loop.h"
@@ -138,6 +139,13 @@
 
   void set_start_session(bool start_session) { start_session_ = start_session; }
 
+  // Sets material mode for the test. This will override material mode set via
+  // command line switches.
+  void set_material_mode(MaterialDesignController::Mode material_mode) {
+    CHECK(!setup_called_);
+    material_mode_ = material_mode;
+  }
+
   AshTestHelper* ash_test_helper() { return ash_test_helper_.get(); }
 
   void RunAllPendingInMessageLoop();
@@ -170,6 +178,7 @@
   bool teardown_called_;
   // |SetUp()| doesn't activate session if this is set to false.
   bool start_session_;
+  MaterialDesignController::Mode material_mode_;
   std::unique_ptr<content::TestBrowserThreadBundle> thread_bundle_;
   std::unique_ptr<AshTestHelper> ash_test_helper_;
   std::unique_ptr<ui::test::EventGenerator> event_generator_;
diff --git a/ash/test/ash_test_helper.cc b/ash/test/ash_test_helper.cc
index 50d4475e..bd25987c 100644
--- a/ash/test/ash_test_helper.cc
+++ b/ash/test/ash_test_helper.cc
@@ -7,7 +7,6 @@
 #include "ash/accelerators/accelerator_controller_delegate_aura.h"
 #include "ash/common/ash_switches.h"
 #include "ash/common/display/display_info.h"
-#include "ash/common/material_design/material_design_controller.h"
 #include "ash/common/wm_shell.h"
 #include "ash/shell.h"
 #include "ash/shell_init_params.h"
@@ -71,7 +70,8 @@
 
 AshTestHelper::~AshTestHelper() {}
 
-void AshTestHelper::SetUp(bool start_session) {
+void AshTestHelper::SetUp(bool start_session,
+                          MaterialDesignController::Mode material_mode) {
   ResetDisplayIdForTest();
   views_delegate_.reset(new AshTestViewsDelegate);
 
@@ -127,6 +127,11 @@
   ui::test::MaterialDesignControllerTestAPI::Uninitialize();
   ui::MaterialDesignController::Initialize();
   ash::MaterialDesignController::Initialize();
+  if (material_mode == MaterialDesignController::Mode::UNINITIALIZED)
+    material_mode = MaterialDesignController::GetMode();
+  material_design_state_.reset(
+      new test::MaterialDesignControllerTestAPI(material_mode));
+
   ShellInitParams init_params;
   init_params.delegate = test_shell_delegate_;
   init_params.context_factory = context_factory;
@@ -152,6 +157,7 @@
 void AshTestHelper::TearDown() {
   // Tear down the shell.
   Shell::DeleteInstance();
+  material_design_state_.reset();
   test::MaterialDesignControllerTestAPI::Uninitialize();
   ShellContentState::DestroyInstance();
 
diff --git a/ash/test/ash_test_helper.h b/ash/test/ash_test_helper.h
index 345d8273..4c0978f 100644
--- a/ash/test/ash_test_helper.h
+++ b/ash/test/ash_test_helper.h
@@ -7,6 +7,8 @@
 
 #include <memory>
 
+#include "ash/common/material_design/material_design_controller.h"
+#include "ash/test/material_design_controller_test_api.h"
 #include "base/compiler_specific.h"
 #include "base/macros.h"
 
@@ -42,10 +44,12 @@
   explicit AshTestHelper(base::MessageLoopForUI* message_loop);
   ~AshTestHelper();
 
-  // Creates the ash::Shell and performs associated initialization.
-  // Set |start_session| to true if the user should log in before
-  // the test is run.
-  void SetUp(bool start_session);
+  // Creates the ash::Shell and performs associated initialization.  Set
+  // |start_session| to true if the user should log in before the test is run.
+  // |material_mode| determines the material design mode to be used for the
+  // tests. If |material_mode| is UNINITIALIZED, the value from command line
+  // switches is used.
+  void SetUp(bool start_session, MaterialDesignController::Mode material_mode);
 
   // Destroys the ash::Shell and performs associated cleanup.
   void TearDown();
@@ -107,6 +111,8 @@
   bool bluez_dbus_manager_initialized_;
 #endif
 
+  std::unique_ptr<test::MaterialDesignControllerTestAPI> material_design_state_;
+
   DISALLOW_COPY_AND_ASSIGN(AshTestHelper);
 };
 
diff --git a/ash/test/ash_test_helper_unittest.cc b/ash/test/ash_test_helper_unittest.cc
index 3d0a257..23db8de 100644
--- a/ash/test/ash_test_helper_unittest.cc
+++ b/ash/test/ash_test_helper_unittest.cc
@@ -22,7 +22,8 @@
   void SetUp() override {
     testing::Test::SetUp();
     ash_test_helper_.reset(new ash::test::AshTestHelper(&message_loop_));
-    ash_test_helper_->SetUp(true);
+    ash_test_helper_->SetUp(true,
+                            ash::MaterialDesignController::Mode::UNINITIALIZED);
   }
 
   void TearDown() override {
diff --git a/ash/wm/overview/window_selector_unittest.cc b/ash/wm/overview/window_selector_unittest.cc
index b30fd27a..eede2ea 100644
--- a/ash/wm/overview/window_selector_unittest.cc
+++ b/ash/wm/overview/window_selector_unittest.cc
@@ -113,9 +113,8 @@
   ~WindowSelectorTest() override {}
 
   void SetUp() override {
+    set_material_mode(GetParam());
     test::AshTestBase::SetUp();
-    material_design_state_.reset(
-        new test::MaterialDesignControllerTestAPI(GetParam()));
     if (!ash::MaterialDesignController::IsOverviewMaterial()) {
       base::CommandLine::ForCurrentProcess()->AppendSwitch(
           switches::kAshEnableStableOverviewOrder);
@@ -128,11 +127,6 @@
     ScopedTransformOverviewWindow::SetImmediateCloseForTests();
   }
 
-  void TearDown() override {
-    material_design_state_.reset();
-    test::AshTestBase::TearDown();
-  }
-
   aura::Window* CreateWindow(const gfx::Rect& bounds) {
     return CreateTestWindowInShellWithDelegate(&delegate_, -1, bounds);
   }
@@ -339,7 +333,6 @@
   aura::test::TestWindowDelegate delegate_;
   NonActivatableActivationDelegate non_activatable_activation_delegate_;
   std::unique_ptr<test::ShelfViewTestAPI> shelf_view_test_;
-  std::unique_ptr<test::MaterialDesignControllerTestAPI> material_design_state_;
 
   DISALLOW_COPY_AND_ASSIGN(WindowSelectorTest);
 };
diff --git a/chrome/test/base/browser_with_test_window_test.cc b/chrome/test/base/browser_with_test_window_test.cc
index cfb71ad..967d987 100644
--- a/chrome/test/base/browser_with_test_window_test.cc
+++ b/chrome/test/base/browser_with_test_window_test.cc
@@ -4,6 +4,7 @@
 
 #include "chrome/test/base/browser_with_test_window_test.h"
 
+#include "ash/common/material_design/material_design_controller.h"
 #include "base/location.h"
 #include "base/run_loop.h"
 #include "base/single_thread_task_runner.h"
@@ -55,7 +56,8 @@
   // perhaps by AshTestHelper owning an AuraTestHelper.
   ash_test_helper_.reset(new ash::test::AshTestHelper(
       base::MessageLoopForUI::current()));
-  ash_test_helper_->SetUp(true);
+  ash_test_helper_->SetUp(true,
+                          ash::MaterialDesignController::Mode::UNINITIALIZED);
 #elif defined(TOOLKIT_VIEWS)
   views_test_helper_.reset(new views::ScopedViewsTestHelper());
 #endif