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#include "ui/compositor/test/context_factories_for_test.h"
11#include "ui/views/test/views_test_helper.h"
12
13namespace views {
14
15ViewsTestBase::ViewsTestBase()
16    : setup_called_(false),
17      teardown_called_(false) {
18}
19
20ViewsTestBase::~ViewsTestBase() {
21  CHECK(setup_called_)
22      << "You have overridden SetUp but never called super class's SetUp";
23  CHECK(teardown_called_)
24      << "You have overridden TearDown but never called super class's TearDown";
25}
26
27void ViewsTestBase::SetUp() {
28  testing::Test::SetUp();
29  setup_called_ = true;
30  if (!views_delegate_.get())
31    views_delegate_.reset(new TestViewsDelegate());
32  // The ContextFactory must exist before any Compositors are created.
33  bool enable_pixel_output = false;
34  ui::ContextFactory* context_factory =
35      ui::InitializeContextFactoryForTests(enable_pixel_output);
36
37  test_helper_.reset(ViewsTestHelper::Create(&message_loop_, context_factory));
38  test_helper_->SetUp();
39  ui::InitializeInputMethodForTesting();
40}
41
42void ViewsTestBase::TearDown() {
43  ui::Clipboard::DestroyClipboardForCurrentThread();
44
45  // Flush the message loop because we have pending release tasks
46  // and these tasks if un-executed would upset Valgrind.
47  RunPendingMessages();
48  teardown_called_ = true;
49  views_delegate_.reset();
50  testing::Test::TearDown();
51  ui::ShutdownInputMethodForTesting();
52  test_helper_->TearDown();
53  ui::TerminateContextFactoryForTests();
54}
55
56void ViewsTestBase::RunPendingMessages() {
57  base::RunLoop run_loop;
58  run_loop.RunUntilIdle();
59}
60
61Widget::InitParams ViewsTestBase::CreateParams(
62    Widget::InitParams::Type type) {
63  Widget::InitParams params(type);
64  params.context = GetContext();
65  return params;
66}
67
68gfx::NativeWindow ViewsTestBase::GetContext() {
69  return test_helper_->GetContext();
70}
71
72}  // namespace views
73