OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_TEST_BASE_VIEW_EVENT_TEST_BASE_H_ | 5 #ifndef CHROME_TEST_BASE_VIEW_EVENT_TEST_BASE_H_ |
6 #define CHROME_TEST_BASE_VIEW_EVENT_TEST_BASE_H_ | 6 #define CHROME_TEST_BASE_VIEW_EVENT_TEST_BASE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 // We only want to use ViewEventTestBase in test targets which properly | 9 // We only want to use ViewEventTestBase in test targets which properly |
10 // isolate each test case by running each test in a separate process. | 10 // isolate each test case by running each test in a separate process. |
11 // This way if a test hangs the test launcher can reliably terminate it. | 11 // This way if a test hangs the test launcher can reliably terminate it. |
12 #if defined(HAS_OUT_OF_PROC_TEST_RUNNER) | 12 #if defined(HAS_OUT_OF_PROC_TEST_RUNNER) |
13 | 13 |
| 14 #include "base/bind.h" |
| 15 #include "base/callback.h" |
14 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
15 #include "base/task.h" | |
16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "views/widget/widget_delegate.h" | 19 #include "views/widget/widget_delegate.h" |
19 | 20 |
20 class Task; | |
21 | |
22 namespace gfx { | 21 namespace gfx { |
23 class Size; | 22 class Size; |
24 } | 23 } |
25 | 24 |
26 // Base class for Views based tests that dispatch events. | 25 // Base class for Views based tests that dispatch events. |
27 // | 26 // |
28 // As views based event test involves waiting for events to be processed, | 27 // As views based event test involves waiting for events to be processed, |
29 // writing a views based test is slightly different than that of writing | 28 // writing a views based test is slightly different than that of writing |
30 // other unit tests. In particular when the test fails or is done you need | 29 // other unit tests. In particular when the test fails or is done you need |
31 // to stop the message loop. This can be done by way of invoking the Done | 30 // to stop the message loop. This can be done by way of invoking the Done |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 73 |
75 // Destroys the window. | 74 // Destroys the window. |
76 virtual void TearDown(); | 75 virtual void TearDown(); |
77 | 76 |
78 // Overridden from views::WidgetDelegate: | 77 // Overridden from views::WidgetDelegate: |
79 virtual bool CanResize() const OVERRIDE; | 78 virtual bool CanResize() const OVERRIDE; |
80 virtual views::View* GetContentsView() OVERRIDE; | 79 virtual views::View* GetContentsView() OVERRIDE; |
81 virtual const views::Widget* GetWidget() const OVERRIDE; | 80 virtual const views::Widget* GetWidget() const OVERRIDE; |
82 virtual views::Widget* GetWidget() OVERRIDE; | 81 virtual views::Widget* GetWidget() OVERRIDE; |
83 | 82 |
84 // Overriden to do nothing so that this class can be used in runnable tasks. | 83 // Overridden to do nothing so that this class can be used in runnable tasks. |
85 void AddRef() {} | 84 void AddRef() {} |
86 void Release() {} | 85 void Release() {} |
87 static bool ImplementsThreadSafeReferenceCounting() { return false; } | 86 static bool ImplementsThreadSafeReferenceCounting() { return false; } |
88 | 87 |
89 protected: | 88 protected: |
90 virtual ~ViewEventTestBase(); | 89 virtual ~ViewEventTestBase(); |
91 | 90 |
92 // Returns the view that is added to the window. | 91 // Returns the view that is added to the window. |
93 virtual views::View* CreateContentsView() = 0; | 92 virtual views::View* CreateContentsView() = 0; |
94 | 93 |
95 // Called once the message loop is running. | 94 // Called once the message loop is running. |
96 virtual void DoTestOnMessageLoop() = 0; | 95 virtual void DoTestOnMessageLoop() = 0; |
97 | 96 |
98 // Invoke from test main. Shows the window, starts the message loop and | 97 // Invoke from test main. Shows the window, starts the message loop and |
99 // schedules a task that invokes DoTestOnMessageLoop. | 98 // schedules a task that invokes DoTestOnMessageLoop. |
100 void StartMessageLoopAndRunTest(); | 99 void StartMessageLoopAndRunTest(); |
101 | 100 |
102 // Returns an empty Size. Subclasses that want a preferred size other than | 101 // Returns an empty Size. Subclasses that want a preferred size other than |
103 // that of the View returned by CreateContentsView should override this | 102 // that of the View returned by CreateContentsView should override this |
104 // appropriately. | 103 // appropriately. |
105 virtual gfx::Size GetPreferredSize(); | 104 virtual gfx::Size GetPreferredSize(); |
106 | 105 |
107 // Creates a task that calls the specified method back. The specified | 106 // Creates a task that calls the specified method back. The specified |
108 // method is called in such a way that if there are any test failures | 107 // method is called in such a way that if there are any test failures |
109 // Done is invoked. | 108 // Done is invoked. |
110 template <class T, class Method> | 109 template <class T, class Method> |
111 Task* CreateEventTask(T* target, Method method) { | 110 base::Closure CreateEventTask(T* target, Method method) { |
112 return NewRunnableMethod(this, &ViewEventTestBase::RunTestMethod, | 111 return base::Bind(&ViewEventTestBase::RunTestMethod, this, |
113 NewRunnableMethod(target, method)); | 112 base::Bind(method, target)); |
114 } | 113 } |
115 | 114 |
116 // Spawns a new thread posts a MouseMove in the background. | 115 // Spawns a new thread posts a MouseMove in the background. |
117 void ScheduleMouseMoveInBackground(int x, int y); | 116 void ScheduleMouseMoveInBackground(int x, int y); |
118 | 117 |
119 views::Widget* window_; | 118 views::Widget* window_; |
120 | 119 |
121 private: | 120 private: |
122 // Stops the thread started by ScheduleMouseMoveInBackground. | 121 // Stops the thread started by ScheduleMouseMoveInBackground. |
123 void StopBackgroundThread(); | 122 void StopBackgroundThread(); |
124 | 123 |
125 // Callback from CreateEventTask. Stops the background thread, runs the | 124 // Callback from CreateEventTask. Stops the background thread, runs the |
126 // supplied task and if there are failures invokes Done. | 125 // supplied task and if there are failures invokes Done. |
127 void RunTestMethod(Task* task); | 126 void RunTestMethod(const base::Closure& task); |
128 | 127 |
129 // The content of the Window. | 128 // The content of the Window. |
130 views::View* content_view_; | 129 views::View* content_view_; |
131 | 130 |
132 // Thread for posting background MouseMoves. | 131 // Thread for posting background MouseMoves. |
133 scoped_ptr<base::Thread> dnd_thread_; | 132 scoped_ptr<base::Thread> dnd_thread_; |
134 | 133 |
135 MessageLoopForUI message_loop_; | 134 MessageLoopForUI message_loop_; |
136 | 135 |
137 // Method factory used for time-outs. | 136 // Method factory used for time-outs. |
138 ScopedRunnableMethodFactory<ViewEventTestBase> method_factory_; | 137 ScopedRunnableMethodFactory<ViewEventTestBase> method_factory_; |
139 | 138 |
140 DISALLOW_COPY_AND_ASSIGN(ViewEventTestBase); | 139 DISALLOW_COPY_AND_ASSIGN(ViewEventTestBase); |
141 }; | 140 }; |
142 | 141 |
143 // Convenience macro for defining a ViewEventTestBase. See class description | 142 // Convenience macro for defining a ViewEventTestBase. See class description |
144 // of ViewEventTestBase for details. | 143 // of ViewEventTestBase for details. |
145 #define VIEW_TEST(test_class, name) \ | 144 #define VIEW_TEST(test_class, name) \ |
146 TEST_F(test_class, name) {\ | 145 TEST_F(test_class, name) {\ |
147 StartMessageLoopAndRunTest();\ | 146 StartMessageLoopAndRunTest();\ |
148 } | 147 } |
149 | 148 |
150 #endif // defined(HAS_OUT_OF_PROC_TEST_RUNNER) | 149 #endif // defined(HAS_OUT_OF_PROC_TEST_RUNNER) |
151 | 150 |
152 #endif // CHROME_TEST_BASE_VIEW_EVENT_TEST_BASE_H_ | 151 #endif // CHROME_TEST_BASE_VIEW_EVENT_TEST_BASE_H_ |
OLD | NEW |