• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ASH_TEST_ASH_TEST_BASE_H_
6 #define ASH_TEST_ASH_TEST_BASE_H_
7 
8 #include <string>
9 
10 #include "base/compiler_specific.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/threading/thread.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/skia/include/core/SkColor.h"
16 #include "ui/wm/public/window_types.h"
17 
18 #if defined(OS_WIN)
19 #include "ui/base/win/scoped_ole_initializer.h"
20 #endif
21 
22 namespace gfx {
23 class Rect;
24 }
25 
26 namespace aura {
27 class RootWindow;
28 class Window;
29 class WindowDelegate;
30 
31 namespace test {
32 class EventGenerator;
33 }  // namespace test
34 }  // namespace aura
35 
36 namespace ash {
37 class DisplayManager;
38 
39 namespace test {
40 
41 class AshTestHelper;
42 class TestScreenshotDelegate;
43 class TestSystemTrayDelegate;
44 #if defined(OS_WIN)
45 class TestMetroViewerProcessHost;
46 #endif
47 
48 class AshTestBase : public testing::Test {
49  public:
50   AshTestBase();
51   virtual ~AshTestBase();
52 
53   // testing::Test:
54   virtual void SetUp() OVERRIDE;
55   virtual void TearDown() OVERRIDE;
56 
57   // Update the display configuration as given in |display_specs|.
58   // See ash::test::DisplayManagerTestApi::UpdateDisplay for more details.
59   void UpdateDisplay(const std::string& display_specs);
60 
61   // Returns a root Window. Usually this is the active root Window, but that
62   // method can return NULL sometimes, and in those cases, we fall back on the
63   // primary root Window.
64   aura::Window* CurrentContext();
65 
66   // Versions of the functions in aura::test:: that go through our shell
67   // StackingController instead of taking a parent.
68   aura::Window* CreateTestWindowInShellWithId(int id);
69   aura::Window* CreateTestWindowInShellWithBounds(const gfx::Rect& bounds);
70   aura::Window* CreateTestWindowInShell(SkColor color,
71                                         int id,
72                                         const gfx::Rect& bounds);
73   aura::Window* CreateTestWindowInShellWithDelegate(
74       aura::WindowDelegate* delegate,
75       int id,
76       const gfx::Rect& bounds);
77   aura::Window* CreateTestWindowInShellWithDelegateAndType(
78       aura::WindowDelegate* delegate,
79       ui::wm::WindowType type,
80       int id,
81       const gfx::Rect& bounds);
82 
83   // Attach |window| to the current shell's root window.
84   void ParentWindowInPrimaryRootWindow(aura::Window* window);
85 
86   // Returns the EventGenerator that uses screen coordinates and works
87   // across multiple displays. It createse a new generator if it
88   // hasn't been created yet.
89   aura::test::EventGenerator& GetEventGenerator();
90 
91  protected:
92   enum UserSessionBlockReason {
93     FIRST_BLOCK_REASON,
94     BLOCKED_BY_LOCK_SCREEN = FIRST_BLOCK_REASON,
95     BLOCKED_BY_LOGIN_SCREEN,
96     BLOCKED_BY_USER_ADDING_SCREEN,
97     NUMBER_OF_BLOCK_REASONS
98   };
99 
100   // Proxy to AshTestHelper::SupportsMultipleDisplays().
101   static bool SupportsMultipleDisplays();
102 
103   // Proxy to AshTestHelper::SupportsHostWindowResize().
104   static bool SupportsHostWindowResize();
105 
set_start_session(bool start_session)106   void set_start_session(bool start_session) { start_session_ = start_session; }
107 
ash_test_helper()108   AshTestHelper* ash_test_helper() { return ash_test_helper_.get(); }
109 
110   void RunAllPendingInMessageLoop();
111 
112   TestScreenshotDelegate* GetScreenshotDelegate();
113   TestSystemTrayDelegate* GetSystemTrayDelegate();
114 
115   // Utility methods to emulate user logged in or not, session started or not
116   // and user able to lock screen or not cases.
117   void SetSessionStarted(bool session_started);
118   void SetUserLoggedIn(bool user_logged_in);
119   void SetCanLockScreen(bool can_lock_screen);
120   void SetShouldLockScreenBeforeSuspending(bool should_lock);
121   void SetUserAddingScreenRunning(bool user_adding_screen_running);
122 
123   // Methods to emulate blocking and unblocking user session with given
124   // |block_reason|.
125   void BlockUserSession(UserSessionBlockReason block_reason);
126   void UnblockUserSession();
127 
128  private:
129   bool setup_called_;
130   bool teardown_called_;
131   // |SetUp()| doesn't activate session if this is set to false.
132   bool start_session_;
133   scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
134   scoped_ptr<AshTestHelper> ash_test_helper_;
135   scoped_ptr<aura::test::EventGenerator> event_generator_;
136 #if defined(OS_WIN)
137   // Note that the order is important here as ipc_thread_ should be destroyed
138   // after metro_viewer_host_->channel_.
139   scoped_ptr<base::Thread> ipc_thread_;
140   scoped_ptr<TestMetroViewerProcessHost> metro_viewer_host_;
141   ui::ScopedOleInitializer ole_initializer_;
142 #endif
143 
144   DISALLOW_COPY_AND_ASSIGN(AshTestBase);
145 };
146 
147 class NoSessionAshTestBase : public AshTestBase {
148  public:
NoSessionAshTestBase()149   NoSessionAshTestBase() {
150     set_start_session(false);
151   }
~NoSessionAshTestBase()152   virtual ~NoSessionAshTestBase() {}
153 
154  private:
155   DISALLOW_COPY_AND_ASSIGN(NoSessionAshTestBase);
156 };
157 
158 }  // namespace test
159 }  // namespace ash
160 
161 #endif  // ASH_TEST_ASH_TEST_BASE_H_
162