Home | History | Annotate | Download | only in test
      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 
      5 #include "ash/test/ash_test_base.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     10 #include "ash/ash_switches.h"
     11 #include "ash/display/display_controller.h"
     12 #include "ash/screen_ash.h"
     13 #include "ash/shell.h"
     14 #include "ash/shell/toplevel_window.h"
     15 #include "ash/test/ash_test_helper.h"
     16 #include "ash/test/display_manager_test_api.h"
     17 #include "ash/test/test_session_state_delegate.h"
     18 #include "ash/test/test_shell_delegate.h"
     19 #include "ash/test/test_system_tray_delegate.h"
     20 #include "ash/wm/coordinate_conversion.h"
     21 #include "ash/wm/window_positioner.h"
     22 #include "base/command_line.h"
     23 #include "content/public/test/web_contents_tester.h"
     24 #include "ui/aura/client/aura_constants.h"
     25 #include "ui/aura/client/screen_position_client.h"
     26 #include "ui/aura/client/window_tree_client.h"
     27 #include "ui/aura/root_window.h"
     28 #include "ui/aura/test/event_generator.h"
     29 #include "ui/aura/test/test_window_delegate.h"
     30 #include "ui/aura/window.h"
     31 #include "ui/aura/window_delegate.h"
     32 #include "ui/base/ime/input_method_initializer.h"
     33 #include "ui/gfx/display.h"
     34 #include "ui/gfx/point.h"
     35 #include "ui/gfx/screen.h"
     36 
     37 #if defined(OS_CHROMEOS)
     38 #include "ash/system/chromeos/tray_display.h"
     39 #endif
     40 
     41 #if defined(OS_WIN)
     42 #include "ash/test/test_metro_viewer_process_host.h"
     43 #include "base/test/test_process_killer_win.h"
     44 #include "base/win/metro.h"
     45 #include "base/win/windows_version.h"
     46 #include "ui/aura/remote_root_window_host_win.h"
     47 #include "ui/aura/root_window_host_win.h"
     48 #include "win8/test/test_registrar_constants.h"
     49 #endif
     50 
     51 namespace ash {
     52 namespace test {
     53 namespace {
     54 
     55 class AshEventGeneratorDelegate : public aura::test::EventGeneratorDelegate {
     56  public:
     57   AshEventGeneratorDelegate() {}
     58   virtual ~AshEventGeneratorDelegate() {}
     59 
     60   // aura::test::EventGeneratorDelegate overrides:
     61   virtual aura::RootWindow* GetRootWindowAt(
     62       const gfx::Point& point_in_screen) const OVERRIDE {
     63     gfx::Screen* screen = Shell::GetScreen();
     64     gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
     65     return Shell::GetInstance()->display_controller()->
     66         GetRootWindowForDisplayId(display.id())->GetDispatcher();
     67   }
     68 
     69   virtual aura::client::ScreenPositionClient* GetScreenPositionClient(
     70       const aura::Window* window) const OVERRIDE {
     71     return aura::client::GetScreenPositionClient(window->GetRootWindow());
     72   }
     73 
     74  private:
     75   DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
     76 };
     77 
     78 }  // namespace
     79 
     80 content::WebContents* AshTestViewsDelegate::CreateWebContents(
     81     content::BrowserContext* browser_context,
     82     content::SiteInstance* site_instance) {
     83   return content::WebContentsTester::CreateTestWebContents(browser_context,
     84                                                            site_instance);
     85 }
     86 
     87 /////////////////////////////////////////////////////////////////////////////
     88 
     89 AshTestBase::AshTestBase()
     90     : setup_called_(false),
     91       teardown_called_(false),
     92       start_session_(true) {
     93   // Must initialize |ash_test_helper_| here because some tests rely on
     94   // AshTestBase methods before they call AshTestBase::SetUp().
     95   ash_test_helper_.reset(new AshTestHelper(base::MessageLoopForUI::current()));
     96 }
     97 
     98 AshTestBase::~AshTestBase() {
     99   CHECK(setup_called_)
    100       << "You have overridden SetUp but never called AshTestBase::SetUp";
    101   CHECK(teardown_called_)
    102       << "You have overridden TearDown but never called AshTestBase::TearDown";
    103 }
    104 
    105 void AshTestBase::SetUp() {
    106   setup_called_ = true;
    107 
    108   // Clears the saved state so that test doesn't use on the wrong
    109   // default state.
    110   shell::ToplevelWindow::ClearSavedStateForTest();
    111 
    112   // TODO(jamescook): Can we do this without changing command line?
    113   // Use the origin (1,1) so that it doesn't over
    114   // lap with the native mouse cursor.
    115   CommandLine* command_line = CommandLine::ForCurrentProcess();
    116   if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) {
    117     command_line->AppendSwitchASCII(
    118         switches::kAshHostWindowBounds, "1+1-800x600");
    119   }
    120 #if defined(OS_WIN)
    121   aura::test::SetUsePopupAsRootWindowForTest(true);
    122 #endif
    123   ash_test_helper_->SetUp(start_session_);
    124 
    125   Shell::GetPrimaryRootWindow()->Show();
    126   Shell::GetPrimaryRootWindow()->GetDispatcher()->host()->Show();
    127   // Move the mouse cursor to far away so that native events doesn't
    128   // interfere test expectations.
    129   Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
    130   ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
    131 
    132 #if defined(OS_WIN)
    133   if (!command_line->HasSwitch(ash::switches::kForceAshToDesktop)) {
    134     if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
    135       ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread"));
    136       base::Thread::Options options;
    137       options.message_loop_type = base::MessageLoop::TYPE_IO;
    138       ipc_thread_->StartWithOptions(options);
    139       metro_viewer_host_.reset(
    140           new TestMetroViewerProcessHost(ipc_thread_->message_loop_proxy()));
    141       CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection(
    142           win8::test::kDefaultTestAppUserModelId));
    143       aura::RemoteRootWindowHostWin* root_window_host =
    144           aura::RemoteRootWindowHostWin::Instance();
    145       CHECK(root_window_host != NULL);
    146     }
    147     ash::WindowPositioner::SetMaximizeFirstWindow(true);
    148   }
    149 #endif
    150 }
    151 
    152 void AshTestBase::TearDown() {
    153   teardown_called_ = true;
    154   // Flush the message loop to finish pending release tasks.
    155   RunAllPendingInMessageLoop();
    156 
    157 #if defined(OS_WIN)
    158   if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
    159       !CommandLine::ForCurrentProcess()->HasSwitch(
    160           ash::switches::kForceAshToDesktop)) {
    161     // Check that our viewer connection is still established.
    162     CHECK(!metro_viewer_host_->closed_unexpectedly());
    163   }
    164 #endif
    165 
    166   ash_test_helper_->TearDown();
    167 #if defined(OS_WIN)
    168   aura::test::SetUsePopupAsRootWindowForTest(false);
    169   // Kill the viewer process if we spun one up.
    170   metro_viewer_host_.reset();
    171 
    172   // Clean up any dangling viewer processes as the metro APIs sometimes leave
    173   // zombies behind. A default browser process in metro will have the
    174   // following command line arg so use that to avoid killing all processes named
    175   // win8::test::kDefaultTestExePath.
    176   const wchar_t kViewerProcessArgument[] = L"DefaultBrowserServer";
    177   base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath,
    178                                           kViewerProcessArgument);
    179 #endif
    180 
    181   event_generator_.reset();
    182   // Some tests set an internal display id,
    183   // reset it here, so other tests will continue in a clean environment.
    184   gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
    185 }
    186 
    187 aura::test::EventGenerator& AshTestBase::GetEventGenerator() {
    188   if (!event_generator_) {
    189     event_generator_.reset(
    190         new aura::test::EventGenerator(new AshEventGeneratorDelegate()));
    191   }
    192   return *event_generator_.get();
    193 }
    194 
    195 // static
    196 bool AshTestBase::SupportsMultipleDisplays() {
    197 #if defined(OS_WIN)
    198   return base::win::GetVersion() < base::win::VERSION_WIN8;
    199 #else
    200   return true;
    201 #endif
    202 }
    203 
    204 // static
    205 bool AshTestBase::SupportsHostWindowResize() {
    206 #if defined(OS_WIN)
    207   return base::win::GetVersion() < base::win::VERSION_WIN8;
    208 #else
    209   return true;
    210 #endif
    211 }
    212 
    213 void AshTestBase::UpdateDisplay(const std::string& display_specs) {
    214   DisplayManagerTestApi display_manager_test_api(
    215       Shell::GetInstance()->display_manager());
    216   display_manager_test_api.UpdateDisplay(display_specs);
    217 }
    218 
    219 aura::Window* AshTestBase::CurrentContext() {
    220   return ash_test_helper_->CurrentContext();
    221 }
    222 
    223 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
    224   return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
    225 }
    226 
    227 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
    228     const gfx::Rect& bounds) {
    229   return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
    230 }
    231 
    232 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
    233                                                    int id,
    234                                                    const gfx::Rect& bounds) {
    235   return CreateTestWindowInShellWithDelegate(
    236       new aura::test::ColorTestWindowDelegate(color), id, bounds);
    237 }
    238 
    239 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
    240     aura::WindowDelegate* delegate,
    241     int id,
    242     const gfx::Rect& bounds) {
    243   return CreateTestWindowInShellWithDelegateAndType(
    244       delegate,
    245       aura::client::WINDOW_TYPE_NORMAL,
    246       id,
    247       bounds);
    248 }
    249 
    250 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
    251     aura::WindowDelegate* delegate,
    252     aura::client::WindowType type,
    253     int id,
    254     const gfx::Rect& bounds) {
    255   aura::Window* window = new aura::Window(delegate);
    256   window->set_id(id);
    257   window->SetType(type);
    258   window->Init(ui::LAYER_TEXTURED);
    259   window->Show();
    260 
    261   if (bounds.IsEmpty()) {
    262     ParentWindowInPrimaryRootWindow(window);
    263   } else {
    264     gfx::Display display =
    265         Shell::GetScreen()->GetDisplayMatching(bounds);
    266     aura::Window* root = ash::Shell::GetInstance()->display_controller()->
    267         GetRootWindowForDisplayId(display.id());
    268     gfx::Point origin = bounds.origin();
    269     wm::ConvertPointFromScreen(root, &origin);
    270     window->SetBounds(gfx::Rect(origin, bounds.size()));
    271     aura::client::ParentWindowWithContext(window, root, bounds);
    272   }
    273   window->SetProperty(aura::client::kCanMaximizeKey, true);
    274   return window;
    275 }
    276 
    277 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
    278   aura::client::ParentWindowWithContext(
    279       window, Shell::GetPrimaryRootWindow(), gfx::Rect());
    280 }
    281 
    282 void AshTestBase::RunAllPendingInMessageLoop() {
    283   ash_test_helper_->RunAllPendingInMessageLoop();
    284 }
    285 
    286 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
    287   return ash_test_helper_->test_screenshot_delegate();
    288 }
    289 
    290 TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() {
    291   return static_cast<TestSystemTrayDelegate*>(
    292       Shell::GetInstance()->system_tray_delegate());
    293 }
    294 
    295 void AshTestBase::SetSessionStarted(bool session_started) {
    296   ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
    297       SetActiveUserSessionStarted(session_started);
    298 }
    299 
    300 void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
    301   ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
    302       SetHasActiveUser(user_logged_in);
    303 }
    304 
    305 void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
    306   ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
    307       SetCanLockScreen(can_lock_screen);
    308 }
    309 
    310 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
    311   ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
    312       SetShouldLockScreenBeforeSuspending(should_lock);
    313 }
    314 
    315 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
    316   ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
    317       SetUserAddingScreenRunning(user_adding_screen_running);
    318 }
    319 
    320 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
    321   switch (block_reason) {
    322     case BLOCKED_BY_LOCK_SCREEN:
    323       SetSessionStarted(true);
    324       SetUserAddingScreenRunning(false);
    325       Shell::GetInstance()->session_state_delegate()->LockScreen();
    326       break;
    327     case BLOCKED_BY_LOGIN_SCREEN:
    328       SetUserAddingScreenRunning(false);
    329       SetSessionStarted(false);
    330       break;
    331     case BLOCKED_BY_USER_ADDING_SCREEN:
    332       SetUserAddingScreenRunning(true);
    333       SetSessionStarted(true);
    334       break;
    335     default:
    336       NOTREACHED();
    337       break;
    338   }
    339 }
    340 
    341 void AshTestBase::UnblockUserSession() {
    342   Shell::GetInstance()->session_state_delegate()->UnlockScreen();
    343   SetSessionStarted(true);
    344   SetUserAddingScreenRunning(false);
    345 }
    346 
    347 
    348 }  // namespace test
    349 }  // namespace ash
    350