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/callback.h" |
14 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
15 #include "base/task.h" | |
16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "views/widget/widget_delegate.h" | 18 #include "views/widget/widget_delegate.h" |
19 | 19 |
20 class Task; | |
21 | |
22 namespace gfx { | 20 namespace gfx { |
23 class Size; | 21 class Size; |
24 } | 22 } |
25 | 23 |
26 // Base class for Views based tests that dispatch events. | 24 // Base class for Views based tests that dispatch events. |
27 // | 25 // |
28 // As views based event test involves waiting for events to be processed, | 26 // 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 | 27 // 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 | 28 // 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 | 29 // 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 | 72 |
75 // Destroys the window. | 73 // Destroys the window. |
76 virtual void TearDown(); | 74 virtual void TearDown(); |
77 | 75 |
78 // Overridden from views::WidgetDelegate: | 76 // Overridden from views::WidgetDelegate: |
79 virtual bool CanResize() const OVERRIDE; | 77 virtual bool CanResize() const OVERRIDE; |
80 virtual views::View* GetContentsView() OVERRIDE; | 78 virtual views::View* GetContentsView() OVERRIDE; |
81 virtual const views::Widget* GetWidget() const OVERRIDE; | 79 virtual const views::Widget* GetWidget() const OVERRIDE; |
82 virtual views::Widget* GetWidget() OVERRIDE; | 80 virtual views::Widget* GetWidget() OVERRIDE; |
83 | 81 |
84 // Overriden to do nothing so that this class can be used in runnable tasks. | 82 // Overridden to do nothing so that this class can be used in runnable tasks. |
85 void AddRef() {} | 83 void AddRef() {} |
86 void Release() {} | 84 void Release() {} |
87 static bool ImplementsThreadSafeReferenceCounting() { return false; } | 85 static bool ImplementsThreadSafeReferenceCounting() { return false; } |
88 | 86 |
89 protected: | 87 protected: |
90 virtual ~ViewEventTestBase(); | 88 virtual ~ViewEventTestBase(); |
91 | 89 |
92 // Returns the view that is added to the window. | 90 // Returns the view that is added to the window. |
93 virtual views::View* CreateContentsView() = 0; | 91 virtual views::View* CreateContentsView() = 0; |
94 | 92 |
95 // Called once the message loop is running. | 93 // Called once the message loop is running. |
96 virtual void DoTestOnMessageLoop() = 0; | 94 virtual void DoTestOnMessageLoop() = 0; |
97 | 95 |
98 // Invoke from test main. Shows the window, starts the message loop and | 96 // Invoke from test main. Shows the window, starts the message loop and |
99 // schedules a task that invokes DoTestOnMessageLoop. | 97 // schedules a task that invokes DoTestOnMessageLoop. |
100 void StartMessageLoopAndRunTest(); | 98 void StartMessageLoopAndRunTest(); |
101 | 99 |
102 // Returns an empty Size. Subclasses that want a preferred size other than | 100 // Returns an empty Size. Subclasses that want a preferred size other than |
103 // that of the View returned by CreateContentsView should override this | 101 // that of the View returned by CreateContentsView should override this |
104 // appropriately. | 102 // appropriately. |
105 virtual gfx::Size GetPreferredSize(); | 103 virtual gfx::Size GetPreferredSize(); |
106 | 104 |
107 // Creates a task that calls the specified method back. The specified | 105 // 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 | 106 // method is called in such a way that if there are any test failures |
109 // Done is invoked. | 107 // Done is invoked. |
110 template <class T, class Method> | 108 template <class T, class Method> |
111 Task* CreateEventTask(T* target, Method method) { | 109 base::Closure CreateEventTask(T* target, Method method) { |
112 return NewRunnableMethod(this, &ViewEventTestBase::RunTestMethod, | 110 return base::Bind(&ViewEventTestBase::RunTestMethod, this, |
113 NewRunnableMethod(target, method)); | 111 base::Bind(method, target)); |
114 } | 112 } |
115 | 113 |
116 // Spawns a new thread posts a MouseMove in the background. | 114 // Spawns a new thread posts a MouseMove in the background. |
117 void ScheduleMouseMoveInBackground(int x, int y); | 115 void ScheduleMouseMoveInBackground(int x, int y); |
118 | 116 |
119 views::Widget* window_; | 117 views::Widget* window_; |
120 | 118 |
121 private: | 119 private: |
122 // Stops the thread started by ScheduleMouseMoveInBackground. | 120 // Stops the thread started by ScheduleMouseMoveInBackground. |
123 void StopBackgroundThread(); | 121 void StopBackgroundThread(); |
124 | 122 |
125 // Callback from CreateEventTask. Stops the background thread, runs the | 123 // Callback from CreateEventTask. Stops the background thread, runs the |
126 // supplied task and if there are failures invokes Done. | 124 // supplied task and if there are failures invokes Done. |
127 void RunTestMethod(Task* task); | 125 void RunTestMethod(const base::Closure& task); |
128 | 126 |
129 // The content of the Window. | 127 // The content of the Window. |
130 views::View* content_view_; | 128 views::View* content_view_; |
131 | 129 |
132 // Thread for posting background MouseMoves. | 130 // Thread for posting background MouseMoves. |
133 scoped_ptr<base::Thread> dnd_thread_; | 131 scoped_ptr<base::Thread> dnd_thread_; |
134 | 132 |
135 MessageLoopForUI message_loop_; | 133 MessageLoopForUI message_loop_; |
136 | 134 |
137 // Method factory used for time-outs. | 135 // Method factory used for time-outs. |
138 ScopedRunnableMethodFactory<ViewEventTestBase> method_factory_; | 136 ScopedRunnableMethodFactory<ViewEventTestBase> method_factory_; |
139 | 137 |
140 DISALLOW_COPY_AND_ASSIGN(ViewEventTestBase); | 138 DISALLOW_COPY_AND_ASSIGN(ViewEventTestBase); |
141 }; | 139 }; |
142 | 140 |
143 // Convenience macro for defining a ViewEventTestBase. See class description | 141 // Convenience macro for defining a ViewEventTestBase. See class description |
144 // of ViewEventTestBase for details. | 142 // of ViewEventTestBase for details. |
145 #define VIEW_TEST(test_class, name) \ | 143 #define VIEW_TEST(test_class, name) \ |
146 TEST_F(test_class, name) {\ | 144 TEST_F(test_class, name) {\ |
147 StartMessageLoopAndRunTest();\ | 145 StartMessageLoopAndRunTest();\ |
148 } | 146 } |
149 | 147 |
150 #endif // defined(HAS_OUT_OF_PROC_TEST_RUNNER) | 148 #endif // defined(HAS_OUT_OF_PROC_TEST_RUNNER) |
151 | 149 |
152 #endif // CHROME_TEST_BASE_VIEW_EVENT_TEST_BASE_H_ | 150 #endif // CHROME_TEST_BASE_VIEW_EVENT_TEST_BASE_H_ |
OLD | NEW |