ash_test_base.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 {
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class DisplayManager;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace test {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class AshTestHelper;
394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class TestScreenshotDelegate;
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class TestSystemTrayDelegate;
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_WIN)
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class TestMetroViewerProcessHost;
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AshTestViewsDelegate : public views::TestViewsDelegate {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overriden from TestViewsDelegate.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual content::WebContents* CreateWebContents(
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::BrowserContext* browser_context,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::SiteInstance* site_instance) OVERRIDE;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AshTestBase : public testing::Test {
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AshTestBase();
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~AshTestBase();
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // testing::Test:
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUp() OVERRIDE;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void TearDown() OVERRIDE;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the display configuration as given in |display_specs|.
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // See ash::test::DisplayManagerTestApi::UpdateDisplay for more details.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateDisplay(const std::string& display_specs);
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Returns a root Window. Usually this is the active root Window, but that
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // method can return NULL sometimes, and in those cases, we fall back on the
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // primary root Window.
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  aura::Window* CurrentContext();
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Versions of the functions in aura::test:: that go through our shell
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // StackingController instead of taking a parent.
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* CreateTestWindowInShellWithId(int id);
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* CreateTestWindowInShellWithBounds(const gfx::Rect& bounds);
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* CreateTestWindowInShell(SkColor color,
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        int id,
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        const gfx::Rect& bounds);
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* CreateTestWindowInShellWithDelegate(
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      aura::WindowDelegate* delegate,
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int id,
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const gfx::Rect& bounds);
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* CreateTestWindowInShellWithDelegateAndType(
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      aura::WindowDelegate* delegate,
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ui::wm::WindowType type,
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int id,
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const gfx::Rect& bounds);
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Attach |window| to the current shell's root window.
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void ParentWindowInPrimaryRootWindow(aura::Window* window);
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the EventGenerator that uses screen coordinates and works
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // across multiple displays. It createse a new generator if it
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // hasn't been created yet.
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::test::EventGenerator& GetEventGenerator();
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
97bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  enum UserSessionBlockReason {
98bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    FIRST_BLOCK_REASON,
99bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    BLOCKED_BY_LOCK_SCREEN = FIRST_BLOCK_REASON,
100bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    BLOCKED_BY_LOGIN_SCREEN,
101bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    BLOCKED_BY_USER_ADDING_SCREEN,
102bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    NUMBER_OF_BLOCK_REASONS
103bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  };
104bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
105c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Proxy to AshTestHelper::SupportsMultipleDisplays().
1067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static bool SupportsMultipleDisplays();
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
108c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Proxy to AshTestHelper::SupportsHostWindowResize().
109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  static bool SupportsHostWindowResize();
110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
111bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  void set_start_session(bool start_session) { start_session_ = start_session; }
112bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AshTestHelper* ash_test_helper() { return ash_test_helper_.get(); }
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RunAllPendingInMessageLoop();
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TestScreenshotDelegate* GetScreenshotDelegate();
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TestSystemTrayDelegate* GetSystemTrayDelegate();
1194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Utility methods to emulate user logged in or not, session started or not
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // and user able to lock screen or not cases.
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetSessionStarted(bool session_started);
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetUserLoggedIn(bool user_logged_in);
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetCanLockScreen(bool can_lock_screen);
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void SetShouldLockScreenBeforeSuspending(bool should_lock);
126bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  void SetUserAddingScreenRunning(bool user_adding_screen_running);
127bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
128bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // Methods to emulate blocking and unblocking user session with given
129bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // |block_reason|.
130bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  void BlockUserSession(UserSessionBlockReason block_reason);
131bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  void UnblockUserSession();
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool setup_called_;
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool teardown_called_;
136bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  // |SetUp()| doesn't activate session if this is set to false.
137bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  bool start_session_;
13823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<AshTestHelper> ash_test_helper_;
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<aura::test::EventGenerator> event_generator_;
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_WIN)
14290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Note that the order is important here as ipc_thread_ should be destroyed
14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // after metro_viewer_host_->channel_.
14490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<base::Thread> ipc_thread_;
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<TestMetroViewerProcessHost> metro_viewer_host_;
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ui::ScopedOleInitializer ole_initializer_;
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AshTestBase);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
152bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochclass NoSessionAshTestBase : public AshTestBase {
153bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch public:
154bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  NoSessionAshTestBase() {
155bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    set_start_session(false);
156bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  }
157bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  virtual ~NoSessionAshTestBase() {}
158bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
159bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch private:
160bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(NoSessionAshTestBase);
161bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch};
162bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace test
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ash
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // ASH_TEST_ASH_TEST_BASE_H_
167