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 #ifndef UI_AURA_TEST_AURA_TEST_BASE_H_
6 #define UI_AURA_TEST_AURA_TEST_BASE_H_
7 
8 #include <memory>
9 
10 #include "base/compiler_specific.h"
11 #include "base/macros.h"
12 #include "base/test/task_environment.h"
13 #include "build/build_config.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/aura/env.h"
16 #include "ui/aura/test/aura_test_helper.h"
17 #include "ui/aura/window_tree_host.h"
18 
19 #if defined(OS_WIN)
20 #include "base/win/scoped_com_initializer.h"
21 #endif
22 
23 namespace aura {
24 class Window;
25 class WindowDelegate;
26 
27 namespace client {
28 class FocusClient;
29 }
30 
31 namespace test {
32 
33 // A base class for aura unit tests.
34 // TODO(beng): Instances of this test will create and own a RootWindow.
35 class AuraTestBase : public testing::Test {
36  public:
37   AuraTestBase();
38   ~AuraTestBase() override;
39 
40   // testing::Test:
41   void SetUp() override;
42   void TearDown() override;
43 
44   // Creates a normal window parented to |parent|.
45   aura::Window* CreateNormalWindow(int id, Window* parent,
46                                    aura::WindowDelegate* delegate);
47 
48  protected:
49   void RunAllPendingInMessageLoop();
50 
51   void ParentWindow(Window* window);
52 
53   // A convenience function for dispatching an event to |dispatcher()|.
54   // Returns whether |event| was handled.
55   bool DispatchEventUsingWindowDispatcher(ui::Event* event);
56 
root_window()57   Window* root_window() { return helper_->GetContext(); }
host()58   WindowTreeHost* host() { return helper_->GetHost(); }
event_sink()59   ui::EventSink* event_sink() { return host()->event_sink(); }
test_screen()60   TestScreen* test_screen() { return helper_->GetTestScreen(); }
focus_client()61   client::FocusClient* focus_client() { return helper_->GetFocusClient(); }
62 
63  private:
64   base::test::TaskEnvironment task_environment_;
65 
66 #if defined(OS_WIN)
67   base::win::ScopedCOMInitializer com_initializer_;
68 #endif
69 
70   bool setup_called_ = false;
71   bool teardown_called_ = false;
72   std::unique_ptr<AuraTestHelper> helper_;
73 
74   DISALLOW_COPY_AND_ASSIGN(AuraTestBase);
75 };
76 
77 }  // namespace test
78 }  // namespace aura
79 
80 #endif  // UI_AURA_TEST_AURA_TEST_BASE_H_
81