summaryrefslogtreecommitdiffstats
path: root/ui/views/test/views_test_helper_aura.cc
blob: f3705b6d0830e6e8174526248be9538ef962637c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/test/views_test_helper_aura.h"

#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/test/aura_test_helper.h"
#include "ui/wm/core/capture_controller.h"
#include "ui/wm/core/default_activation_client.h"
#include "ui/wm/core/default_screen_position_client.h"
#include "ui/wm/core/wm_state.h"

namespace views {

// static
ViewsTestHelper* ViewsTestHelper::Create(base::MessageLoopForUI* message_loop,
                                         ui::ContextFactory* context_factory) {
  return new ViewsTestHelperAura(message_loop, context_factory);
}

ViewsTestHelperAura::ViewsTestHelperAura(base::MessageLoopForUI* message_loop,
                                         ui::ContextFactory* context_factory)
    : context_factory_(context_factory) {
  aura_test_helper_.reset(new aura::test::AuraTestHelper(message_loop));
}

ViewsTestHelperAura::~ViewsTestHelperAura() {
}

void ViewsTestHelperAura::SetUp() {
  aura_test_helper_->SetUp(context_factory_);
  gfx::NativeWindow root_window = GetContext();
  new wm::DefaultActivationClient(root_window);
  wm_state_.reset(new wm::WMState);

  if (!aura::client::GetScreenPositionClient(root_window)) {
    screen_position_client_.reset(new wm::DefaultScreenPositionClient);
    aura::client::SetScreenPositionClient(root_window,
                                          screen_position_client_.get());
  }
}

void ViewsTestHelperAura::TearDown() {
  // Ensure all Widgets (and windows) are closed in unit tests. This is done
  // automatically when the RootWindow is torn down, but is an error on
  // platforms that must ensure no Compositors are alive when the ContextFactory
  // is torn down.
  // So, although it's optional, check the root window to detect failures before
  // they hit the CQ on other platforms.
  DCHECK(aura_test_helper_->root_window()->children().empty())
      << "Not all windows were closed.";

  if (screen_position_client_.get() ==
      aura::client::GetScreenPositionClient(GetContext()))
    aura::client::SetScreenPositionClient(GetContext(), nullptr);

  aura_test_helper_->TearDown();
  wm_state_.reset();
  CHECK(!wm::ScopedCaptureClient::IsActive());
}

gfx::NativeWindow ViewsTestHelperAura::GetContext() {
  return aura_test_helper_->root_window();
}

}  // namespace views