ash_test_base.h revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef ASH_TEST_ASH_TEST_BASE_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define ASH_TEST_ASH_TEST_BASE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/message_loop/message_loop.h"
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/threading/thread.h"
13a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "content/public/test/test_browser_thread_bundle.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "third_party/skia/include/core/SkColor.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/test/test_views_delegate.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/wm/public/window_types.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_WIN)
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/base/win/scoped_ole_initializer.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace aura {
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class RootWindow;
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Window;
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class WindowDelegate;
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace test {
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class EventGenerator;
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace test
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace aura
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ash {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal {
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class DisplayManager;
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace internal
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace test {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class AshTestHelper;
414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class TestScreenshotDelegate;
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class TestSystemTrayDelegate;
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_WIN)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class TestMetroViewerProcessHost;
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AshTestViewsDelegate : public views::TestViewsDelegate {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overriden from TestViewsDelegate.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual content::WebContents* CreateWebContents(
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::BrowserContext* browser_context,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::SiteInstance* site_instance) OVERRIDE;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AshTestBase : public testing::Test {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AshTestBase();
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~AshTestBase();
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // testing::Test:
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUp() OVERRIDE;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void TearDown() OVERRIDE;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the display configuration as given in |display_specs|.
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // See ash::test::DisplayManagerTestApi::UpdateDisplay for more details.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateDisplay(const std::string& display_specs);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Returns a root Window. Usually this is the active root Window, but that
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // method can return NULL sometimes, and in those cases, we fall back on the
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // primary root Window.
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  aura::Window* CurrentContext();
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Versions of the functions in aura::test:: that go through our shell
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // StackingController instead of taking a parent.
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* CreateTestWindowInShellWithId(int id);
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* CreateTestWindowInShellWithBounds(const gfx::Rect& bounds);
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* CreateTestWindowInShell(SkColor color,
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        int id,
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        const gfx::Rect& bounds);
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* CreateTestWindowInShellWithDelegate(
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      aura::WindowDelegate* delegate,
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int id,
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const gfx::Rect& bounds);
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* CreateTestWindowInShellWithDelegateAndType(
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      aura::WindowDelegate* delegate,
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ui::wm::WindowType type,
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int id,
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const gfx::Rect& bounds);
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Attach |window| to the current shell's root window.
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void ParentWindowInPrimaryRootWindow(aura::Window* window);
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the EventGenerator that uses screen coordinates and works
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // across multiple displays. It createse a new generator if it
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // hasn't been created yet.
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::test::EventGenerator& GetEventGenerator();
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
99bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  enum UserSessionBlockReason {
100bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    FIRST_BLOCK_REASON,
101bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    BLOCKED_BY_LOCK_SCREEN = FIRST_BLOCK_REASON,
102bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    BLOCKED_BY_LOGIN_SCREEN,
103bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    BLOCKED_BY_USER_ADDING_SCREEN,
104bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    NUMBER_OF_BLOCK_REASONS
105bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  };
106bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // True if the running environment supports multiple displays,
1087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // or false otherwise (e.g. win8 bot).
1097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static bool SupportsMultipleDisplays();
1107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // True if the running environment supports host window resize,
112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // or false otherwise (e.g. win8 bot).
113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  static bool SupportsHostWindowResize();
114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
115bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  void set_start_session(bool start_session) { start_session_ = start_session; }
116bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AshTestHelper* ash_test_helper() { return ash_test_helper_.get(); }
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RunAllPendingInMessageLoop();
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TestScreenshotDelegate* GetScreenshotDelegate();
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TestSystemTrayDelegate* GetSystemTrayDelegate();
1234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Utility methods to emulate user logged in or not, session started or not
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // and user able to lock screen or not cases.
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetSessionStarted(bool session_started);
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetUserLoggedIn(bool user_logged_in);
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetCanLockScreen(bool can_lock_screen);
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void SetShouldLockScreenBeforeSuspending(bool should_lock);
130bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  void SetUserAddingScreenRunning(bool user_adding_screen_running);
131bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
132bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // Methods to emulate blocking and unblocking user session with given
133bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // |block_reason|.
134bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  void BlockUserSession(UserSessionBlockReason block_reason);
135bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  void UnblockUserSession();
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool setup_called_;
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool teardown_called_;
140bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  // |SetUp()| doesn't activate session if this is set to false.
141bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  bool start_session_;
14223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<AshTestHelper> ash_test_helper_;
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<aura::test::EventGenerator> event_generator_;
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_WIN)
14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Note that the order is important here as ipc_thread_ should be destroyed
14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // after metro_viewer_host_->channel_.
14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<base::Thread> ipc_thread_;
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<TestMetroViewerProcessHost> metro_viewer_host_;
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ui::ScopedOleInitializer ole_initializer_;
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AshTestBase);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
156bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochclass NoSessionAshTestBase : public AshTestBase {
157bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch public:
158bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  NoSessionAshTestBase() {
159bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    set_start_session(false);
160bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  }
161bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  virtual ~NoSessionAshTestBase() {}
162bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
163bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch private:
164bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(NoSessionAshTestBase);
165bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch};
166bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace test
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ash
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // ASH_TEST_ASH_TEST_BASE_H_
171