OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #import "base/mac/scoped_nsobject.h" | 5 #import "base/mac/scoped_nsobject.h" |
6 #include "chrome/browser/ui/cocoa/fullscreen_low_power_coordinator.h" | 6 #include "chrome/browser/ui/cocoa/fullscreen_low_power_coordinator.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "ui/accelerated_widget_mac/accelerated_widget_mac.h" | 8 #include "ui/accelerated_widget_mac/accelerated_widget_mac.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 void RecreateLayers() { | 55 void RecreateLayers() { |
56 content_layer_.reset([[CALayer alloc] init]); | 56 content_layer_.reset([[CALayer alloc] init]); |
57 low_power_layer_.reset([[CALayer alloc] init]); | 57 low_power_layer_.reset([[CALayer alloc] init]); |
58 } | 58 } |
59 void SendFrames(int count, bool low_power_valid) { | 59 void SendFrames(int count, bool low_power_valid) { |
60 for (int i = 0; i < count; ++i) | 60 for (int i = 0; i < count; ++i) |
61 widget_->GotCALayerFrame(content_layer_, low_power_valid, | 61 widget_->GotCALayerFrame(content_layer_, low_power_valid, |
62 low_power_layer_, gfx::Size(1, 1), 1); | 62 low_power_layer_, gfx::Size(1, 1), 1); |
63 } | 63 } |
| 64 bool DoesTransitionToLowPowerMode() { |
| 65 if (IsInLowPowerMode()) |
| 66 return false; |
| 67 for (int i = 0; i < 20; ++i) |
| 68 coordinator_->TickEnterOrExitForTesting(); |
| 69 return IsInLowPowerMode(); |
| 70 } |
64 bool IsInLowPowerMode() { | 71 bool IsInLowPowerMode() { |
65 // Return true if the fullscreen low power window is in front of the content | 72 // Return true if the fullscreen low power window is in front of the content |
66 // window. | 73 // window. |
67 for (NSWindow* window in [NSApp orderedWindows]) { | 74 for (NSWindow* window in [NSApp orderedWindows]) { |
68 if (window == content_window_.get()) | 75 if (window == content_window_.get()) |
69 return false; | 76 return false; |
70 if (window == coordinator_->GetFullscreenLowPowerWindow()) | 77 if (window == coordinator_->GetFullscreenLowPowerWindow()) |
71 return true; | 78 return true; |
72 } | 79 } |
73 return false; | 80 return false; |
(...skipping 30 matching lines...) Expand all Loading... |
104 coordinator_.reset(); | 111 coordinator_.reset(); |
105 } | 112 } |
106 | 113 |
107 TEST_F(FullscreenLowPowerTest, TransitionAndHysteresis) { | 114 TEST_F(FullscreenLowPowerTest, TransitionAndHysteresis) { |
108 // Ensure that we can't enter low power mode until we finish the fullscreen | 115 // Ensure that we can't enter low power mode until we finish the fullscreen |
109 // transition. | 116 // transition. |
110 EXPECT_FALSE(IsInLowPowerMode()); | 117 EXPECT_FALSE(IsInLowPowerMode()); |
111 SendFrames(kEnoughFrames, true); | 118 SendFrames(kEnoughFrames, true); |
112 EXPECT_FALSE(IsInLowPowerMode()); | 119 EXPECT_FALSE(IsInLowPowerMode()); |
113 coordinator_->SetInFullscreenTransition(false); | 120 coordinator_->SetInFullscreenTransition(false); |
114 EXPECT_TRUE(IsInLowPowerMode()); | 121 EXPECT_TRUE(DoesTransitionToLowPowerMode()); |
115 coordinator_->SetInFullscreenTransition(true); | 122 coordinator_->SetInFullscreenTransition(true); |
116 EXPECT_FALSE(IsInLowPowerMode()); | 123 EXPECT_FALSE(IsInLowPowerMode()); |
117 | 124 |
118 // Verify hysteresis. | 125 // Verify hysteresis. |
119 coordinator_->SetInFullscreenTransition(false); | 126 coordinator_->SetInFullscreenTransition(false); |
120 EXPECT_TRUE(IsInLowPowerMode()); | 127 EXPECT_TRUE(DoesTransitionToLowPowerMode()); |
121 SendFrames(1, false); | 128 SendFrames(1, false); |
122 EXPECT_FALSE(IsInLowPowerMode()); | 129 EXPECT_FALSE(IsInLowPowerMode()); |
123 SendFrames(kNotEnoughFrames, true); | 130 SendFrames(kNotEnoughFrames, true); |
124 EXPECT_FALSE(IsInLowPowerMode()); | 131 EXPECT_FALSE(DoesTransitionToLowPowerMode()); |
125 SendFrames(kEnoughFrames, true); | 132 SendFrames(kEnoughFrames, true); |
126 EXPECT_TRUE(IsInLowPowerMode()); | 133 EXPECT_TRUE(IsInLowPowerMode()); |
127 } | 134 } |
128 | 135 |
129 TEST_F(FullscreenLowPowerTest, Layout) { | 136 TEST_F(FullscreenLowPowerTest, Layout) { |
130 SendFrames(kEnoughFrames, true); | 137 SendFrames(kEnoughFrames, true); |
131 coordinator_->SetInFullscreenTransition(false); | 138 coordinator_->SetInFullscreenTransition(false); |
132 EXPECT_TRUE(IsInLowPowerMode()); | 139 EXPECT_TRUE(DoesTransitionToLowPowerMode()); |
133 NSRect screen_frame = [[content_window_ screen] frame]; | 140 NSRect screen_frame = [[content_window_ screen] frame]; |
134 | 141 |
135 // Test a valid layout first (and again after each invalid layout). | 142 // Test a valid layout first (and again after each invalid layout). |
136 auto set_valid_layout = [this, screen_frame]{ | 143 auto set_valid_layout = [this, screen_frame]{ |
137 coordinator_->SetLayoutParameters( | 144 coordinator_->SetLayoutParameters( |
138 NSMakeRect(0, screen_frame.size.height, 10, 10), | 145 NSMakeRect(0, screen_frame.size.height, 10, 10), |
139 NSMakeRect(0, screen_frame.size.height, 10, 10), | 146 NSMakeRect(0, screen_frame.size.height, 10, 10), |
140 screen_frame, | 147 screen_frame, |
141 NSMakeRect(0, 0, 20, 0)); | 148 NSMakeRect(0, 0, 20, 0)); |
142 }; | 149 }; |
143 set_valid_layout(); | 150 set_valid_layout(); |
144 EXPECT_TRUE(IsInLowPowerMode()); | 151 EXPECT_TRUE(IsInLowPowerMode()); |
145 | 152 |
146 // Invalid layout -- toolbar visible. | 153 // Invalid layout -- toolbar visible. |
147 coordinator_->SetLayoutParameters( | 154 coordinator_->SetLayoutParameters( |
148 NSMakeRect(0, screen_frame.size.height - 5, 10, 10), | 155 NSMakeRect(0, screen_frame.size.height - 5, 10, 10), |
149 NSMakeRect(0, screen_frame.size.height, 10, 10), | 156 NSMakeRect(0, screen_frame.size.height, 10, 10), |
150 screen_frame, | 157 screen_frame, |
151 NSMakeRect(0, 0, 20, 0)); | 158 NSMakeRect(0, 0, 20, 0)); |
152 EXPECT_FALSE(IsInLowPowerMode()); | 159 EXPECT_FALSE(IsInLowPowerMode()); |
153 set_valid_layout(); | 160 set_valid_layout(); |
154 EXPECT_TRUE(IsInLowPowerMode()); | 161 EXPECT_TRUE(DoesTransitionToLowPowerMode()); |
155 | 162 |
156 // Invalid layout -- infobar visible. | 163 // Invalid layout -- infobar visible. |
157 coordinator_->SetLayoutParameters( | 164 coordinator_->SetLayoutParameters( |
158 NSMakeRect(0, screen_frame.size.height, 10, 10), | 165 NSMakeRect(0, screen_frame.size.height, 10, 10), |
159 NSMakeRect(0, screen_frame.size.height - 5, 10, 10), | 166 NSMakeRect(0, screen_frame.size.height - 5, 10, 10), |
160 screen_frame, | 167 screen_frame, |
161 NSMakeRect(0, 0, 20, 0)); | 168 NSMakeRect(0, 0, 20, 0)); |
162 EXPECT_FALSE(IsInLowPowerMode()); | 169 EXPECT_FALSE(IsInLowPowerMode()); |
163 set_valid_layout(); | 170 set_valid_layout(); |
164 EXPECT_TRUE(IsInLowPowerMode()); | 171 EXPECT_TRUE(DoesTransitionToLowPowerMode()); |
165 | 172 |
166 // Invalid layout -- not fullscreen. | 173 // Invalid layout -- not fullscreen. |
167 coordinator_->SetLayoutParameters( | 174 coordinator_->SetLayoutParameters( |
168 NSMakeRect(0, screen_frame.size.height, 10, 10), | 175 NSMakeRect(0, screen_frame.size.height, 10, 10), |
169 NSMakeRect(0, screen_frame.size.height, 10, 10), | 176 NSMakeRect(0, screen_frame.size.height, 10, 10), |
170 NSMakeRect(screen_frame.origin.x, screen_frame.origin.y, | 177 NSMakeRect(screen_frame.origin.x, screen_frame.origin.y, |
171 screen_frame.size.width, screen_frame.size.height / 2), | 178 screen_frame.size.width, screen_frame.size.height / 2), |
172 NSMakeRect(0, 0, 20, 0)); | 179 NSMakeRect(0, 0, 20, 0)); |
173 EXPECT_FALSE(IsInLowPowerMode()); | 180 EXPECT_FALSE(IsInLowPowerMode()); |
174 set_valid_layout(); | 181 set_valid_layout(); |
175 EXPECT_TRUE(IsInLowPowerMode()); | 182 EXPECT_TRUE(DoesTransitionToLowPowerMode()); |
176 | 183 |
177 // Invalid layout -- download shelf visible. | 184 // Invalid layout -- download shelf visible. |
178 coordinator_->SetLayoutParameters( | 185 coordinator_->SetLayoutParameters( |
179 NSMakeRect(0, screen_frame.size.height, 10, 10), | 186 NSMakeRect(0, screen_frame.size.height, 10, 10), |
180 NSMakeRect(0, screen_frame.size.height - 5, 10, 10), | 187 NSMakeRect(0, screen_frame.size.height - 5, 10, 10), |
181 screen_frame, | 188 screen_frame, |
182 NSMakeRect(0, 0, 20, 20)); | 189 NSMakeRect(0, 0, 20, 20)); |
183 EXPECT_FALSE(IsInLowPowerMode()); | 190 EXPECT_FALSE(IsInLowPowerMode()); |
184 set_valid_layout(); | 191 set_valid_layout(); |
185 EXPECT_TRUE(IsInLowPowerMode()); | 192 EXPECT_TRUE(DoesTransitionToLowPowerMode()); |
186 } | 193 } |
187 | 194 |
188 TEST_F(FullscreenLowPowerTest, OrderFrontAndChildWindows) { | 195 TEST_F(FullscreenLowPowerTest, OrderFrontAndChildWindows) { |
189 SendFrames(kEnoughFrames, true); | 196 SendFrames(kEnoughFrames, true); |
190 coordinator_->SetInFullscreenTransition(false); | 197 coordinator_->SetInFullscreenTransition(false); |
191 EXPECT_TRUE(IsInLowPowerMode()); | 198 EXPECT_TRUE(DoesTransitionToLowPowerMode()); |
192 | 199 |
193 // Create a child window. | 200 // Create a child window. |
194 base::scoped_nsobject<NSWindow> child_window([[NSWindow alloc] | 201 base::scoped_nsobject<NSWindow> child_window([[NSWindow alloc] |
195 initWithContentRect:NSMakeRect(0, 0, 256, 256) | 202 initWithContentRect:NSMakeRect(0, 0, 256, 256) |
196 styleMask:NSTitledWindowMask | NSResizableWindowMask | 203 styleMask:NSTitledWindowMask | NSResizableWindowMask |
197 backing:NSBackingStoreBuffered | 204 backing:NSBackingStoreBuffered |
198 defer:NO]); | 205 defer:NO]); |
199 [child_window setReleasedWhenClosed:NO]; | 206 [child_window setReleasedWhenClosed:NO]; |
200 [child_window orderFront:nil]; | 207 [child_window orderFront:nil]; |
201 | 208 |
202 // The orderFront puts the window in front of the low power window, but | 209 // The orderFront puts the window in front of the low power window, but |
203 // the low power window is still in front of the content window, so we | 210 // the low power window is still in front of the content window, so we |
204 // still think that we're in low power mode. | 211 // still think that we're in low power mode. |
205 EXPECT_TRUE(IsInLowPowerMode()); | 212 EXPECT_TRUE(IsInLowPowerMode()); |
206 SendFrames(1, true); | 213 SendFrames(1, true); |
207 EXPECT_TRUE(IsInLowPowerMode()); | 214 EXPECT_TRUE(IsInLowPowerMode()); |
208 | 215 |
209 // Now make this be a child window, and ensure that we're no longer in low | 216 // Now make this be a child window, and ensure that we're no longer in low |
210 // power mode. | 217 // power mode. |
211 [content_window_ addChildWindow:child_window ordered:NSWindowAbove]; | 218 [content_window_ addChildWindow:child_window ordered:NSWindowAbove]; |
212 coordinator_->ChildWindowsChanged(); | 219 coordinator_->ChildWindowsChanged(); |
213 EXPECT_FALSE(IsInLowPowerMode()); | 220 EXPECT_FALSE(IsInLowPowerMode()); |
214 | 221 |
215 // And remove it, and ensure that we're back in low power mode. | 222 // And remove it, and ensure that we're back in low power mode. |
216 [content_window_ removeChildWindow:child_window]; | 223 [content_window_ removeChildWindow:child_window]; |
217 coordinator_->ChildWindowsChanged(); | 224 coordinator_->ChildWindowsChanged(); |
218 EXPECT_TRUE(IsInLowPowerMode()); | 225 EXPECT_TRUE(DoesTransitionToLowPowerMode()); |
219 | 226 |
220 // Now manually send the content window to the front, and make sure that | 227 // Now manually send the content window to the front, and make sure that |
221 // the next frame restores the low power window. | 228 // the next frame restores the low power window. |
222 [content_window_ orderFront:nil]; | 229 [content_window_ orderFront:nil]; |
223 EXPECT_FALSE(IsInLowPowerMode()); | 230 EXPECT_FALSE(IsInLowPowerMode()); |
224 SendFrames(1, true); | 231 SendFrames(1, true); |
225 EXPECT_TRUE(IsInLowPowerMode()); | 232 EXPECT_TRUE(IsInLowPowerMode()); |
226 } | 233 } |
227 | 234 |
228 } // namespace | 235 } // namespace |
OLD | NEW |