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 "ui/views/test/views_test_base.h"
      6 
      7 #include "base/run_loop.h"
      8 #include "ui/base/clipboard/clipboard.h"
      9 #include "ui/base/ime/input_method_initializer.h"
     10 
     11 #if defined(USE_AURA)
     12 #include "ui/aura/env.h"
     13 #include "ui/aura/root_window.h"
     14 #include "ui/aura/test/aura_test_helper.h"
     15 #include "ui/views/corewm/capture_controller.h"
     16 #include "ui/views/corewm/transient_window_stacking_client.h"
     17 #endif
     18 
     19 namespace views {
     20 
     21 ViewsTestBase::ViewsTestBase()
     22     : setup_called_(false),
     23       teardown_called_(false) {
     24 }
     25 
     26 ViewsTestBase::~ViewsTestBase() {
     27   CHECK(setup_called_)
     28       << "You have overridden SetUp but never called super class's SetUp";
     29   CHECK(teardown_called_)
     30       << "You have overrideen TearDown but never called super class's TearDown";
     31 }
     32 
     33 void ViewsTestBase::SetUp() {
     34   testing::Test::SetUp();
     35   setup_called_ = true;
     36   if (!views_delegate_.get())
     37     views_delegate_.reset(new TestViewsDelegate());
     38 #if defined(USE_AURA)
     39   aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
     40   aura_test_helper_->SetUp();
     41   // SetWindowStackingClient() takes ownership of TransientWindowStackingClient.
     42   aura::client::SetWindowStackingClient(
     43       new corewm::TransientWindowStackingClient);
     44 #endif  // USE_AURA
     45   ui::InitializeInputMethodForTesting();
     46 }
     47 
     48 void ViewsTestBase::TearDown() {
     49   ui::Clipboard::DestroyClipboardForCurrentThread();
     50 
     51   // Flush the message loop because we have pending release tasks
     52   // and these tasks if un-executed would upset Valgrind.
     53   RunPendingMessages();
     54   teardown_called_ = true;
     55   views_delegate_.reset();
     56   testing::Test::TearDown();
     57   ui::ShutdownInputMethodForTesting();
     58 #if defined(USE_AURA)
     59   aura_test_helper_->TearDown();
     60   aura::client::SetWindowStackingClient(NULL);
     61   CHECK(!corewm::ScopedCaptureClient::IsActive());
     62 #endif  // USE_AURA
     63 }
     64 
     65 void ViewsTestBase::RunPendingMessages() {
     66   base::RunLoop run_loop;
     67 #if defined(USE_AURA)
     68   run_loop.set_dispatcher(aura::Env::GetInstance()->GetDispatcher());
     69 #endif
     70   run_loop.RunUntilIdle();
     71 }
     72 
     73 Widget::InitParams ViewsTestBase::CreateParams(
     74     Widget::InitParams::Type type) {
     75   Widget::InitParams params(type);
     76 #if defined(USE_AURA)
     77   params.context = aura_test_helper_->root_window();
     78 #endif
     79   return params;
     80 }
     81 
     82 gfx::NativeView ViewsTestBase::GetContext() {
     83 #if defined(USE_AURA)
     84   return aura_test_helper_->root_window();
     85 #else
     86   return NULL;
     87 #endif
     88 }
     89 
     90 }  // namespace views
     91