Home | History | Annotate | Download | only in test
      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 
      5 #include "ash/test/ash_test_helper.h"
      6 
      7 #include "ash/ash_switches.h"
      8 #include "ash/shell.h"
      9 #include "ash/test/display_manager_test_api.h"
     10 #include "ash/test/shell_test_api.h"
     11 #include "ash/test/test_session_state_delegate.h"
     12 #include "ash/test/test_shell_delegate.h"
     13 #include "base/run_loop.h"
     14 #include "ui/aura/env.h"
     15 #include "ui/base/ime/input_method_initializer.h"
     16 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
     17 #include "ui/message_center/message_center.h"
     18 
     19 #if defined(OS_CHROMEOS)
     20 #include "chromeos/audio/cras_audio_handler.h"
     21 #endif
     22 
     23 #if defined(USE_X11)
     24 #include "ui/aura/root_window_host_x11.h"
     25 #endif
     26 
     27 namespace ash {
     28 namespace test {
     29 
     30 AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop)
     31     : message_loop_(message_loop),
     32       test_shell_delegate_(NULL) {
     33   CHECK(message_loop_);
     34 #if defined(USE_X11)
     35   aura::test::SetUseOverrideRedirectWindowByDefault(true);
     36 #endif
     37 }
     38 
     39 AshTestHelper::~AshTestHelper() {
     40 }
     41 
     42 void AshTestHelper::SetUp(bool start_session) {
     43   // Disable animations during tests.
     44   zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
     45       ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
     46   ui::InitializeInputMethodForTesting();
     47 
     48   // Creates Shell and hook with Desktop.
     49   test_shell_delegate_ = new TestShellDelegate;
     50 
     51   // Creates MessageCenter since g_browser_process is not created in AshTestBase
     52   // tests.
     53   message_center::MessageCenter::Initialize();
     54 
     55 #if defined(OS_CHROMEOS)
     56   // Create CrasAudioHandler for testing since g_browser_process is not
     57   // created in AshTestBase tests.
     58   chromeos::CrasAudioHandler::InitializeForTesting();
     59 #endif
     60 
     61   ash::Shell::CreateInstance(test_shell_delegate_);
     62   Shell* shell = Shell::GetInstance();
     63   if (start_session) {
     64     test_shell_delegate_->test_session_state_delegate()->
     65         SetActiveUserSessionStarted(true);
     66     test_shell_delegate_->test_session_state_delegate()->
     67         SetHasActiveUser(true);
     68   }
     69 
     70   test::DisplayManagerTestApi(shell->display_manager()).
     71       DisableChangeDisplayUponHostResize();
     72   ShellTestApi(shell).DisableOutputConfiguratorAnimation();
     73 }
     74 
     75 void AshTestHelper::TearDown() {
     76   // Tear down the shell.
     77   Shell::DeleteInstance();
     78 
     79   // Remove global message center state.
     80   message_center::MessageCenter::Shutdown();
     81 
     82 #if defined(OS_CHROMEOS)
     83   chromeos::CrasAudioHandler::Shutdown();
     84 #endif
     85 
     86   aura::Env::DeleteInstance();
     87 
     88   ui::ShutdownInputMethodForTesting();
     89   zero_duration_mode_.reset();
     90 }
     91 
     92 void AshTestHelper::RunAllPendingInMessageLoop() {
     93 #if !defined(OS_MACOSX)
     94   DCHECK(base::MessageLoopForUI::current() == message_loop_);
     95   base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher());
     96   run_loop.RunUntilIdle();
     97 #endif
     98 }
     99 
    100 aura::RootWindow* AshTestHelper::CurrentContext() {
    101   aura::RootWindow* root_window = Shell::GetActiveRootWindow();
    102   if (!root_window)
    103     root_window = Shell::GetPrimaryRootWindow();
    104   DCHECK(root_window);
    105   return root_window;
    106 }
    107 
    108 }  // namespace test
    109 }  // namespace ash
    110