| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <set> | 6 #include <set> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 3195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3206 target->HandleKeyboardMessage(WM_SYSCHAR, 0, 0, &handled); | 3206 target->HandleKeyboardMessage(WM_SYSCHAR, 0, 0, &handled); |
| 3207 target->HandleKeyboardMessage(WM_SYSDEADCHAR, 0, 0, &handled); | 3207 target->HandleKeyboardMessage(WM_SYSDEADCHAR, 0, 0, &handled); |
| 3208 widget.CloseNow(); | 3208 widget.CloseNow(); |
| 3209 } | 3209 } |
| 3210 | 3210 |
| 3211 // Provides functionality to subclass a window and keep track of messages | 3211 // Provides functionality to subclass a window and keep track of messages |
| 3212 // received. | 3212 // received. |
| 3213 class SubclassWindowHelper { | 3213 class SubclassWindowHelper { |
| 3214 public: | 3214 public: |
| 3215 explicit SubclassWindowHelper(HWND window) | 3215 explicit SubclassWindowHelper(HWND window) |
| 3216 : window_(window) { | 3216 : window_(window), |
| 3217 message_to_destroy_on_(0) { |
| 3217 EXPECT_EQ(instance_, nullptr); | 3218 EXPECT_EQ(instance_, nullptr); |
| 3218 instance_ = this; | 3219 instance_ = this; |
| 3219 EXPECT_TRUE(Subclass()); | 3220 EXPECT_TRUE(Subclass()); |
| 3220 } | 3221 } |
| 3221 | 3222 |
| 3222 ~SubclassWindowHelper() { | 3223 ~SubclassWindowHelper() { |
| 3223 Unsubclass(); | 3224 Unsubclass(); |
| 3224 instance_ = nullptr; | 3225 instance_ = nullptr; |
| 3225 } | 3226 } |
| 3226 | 3227 |
| 3227 // Returns true if the |message| passed in was received. | 3228 // Returns true if the |message| passed in was received. |
| 3228 bool received_message(unsigned int message) { | 3229 bool received_message(unsigned int message) { |
| 3229 return (messages_.find(message) != messages_.end()); | 3230 return (messages_.find(message) != messages_.end()); |
| 3230 } | 3231 } |
| 3231 | 3232 |
| 3232 void Clear() { | 3233 void Clear() { |
| 3233 messages_.clear(); | 3234 messages_.clear(); |
| 3234 } | 3235 } |
| 3235 | 3236 |
| 3237 void set_message_to_destroy_on(unsigned int message) { |
| 3238 message_to_destroy_on_ = message; |
| 3239 } |
| 3240 |
| 3236 private: | 3241 private: |
| 3237 bool Subclass() { | 3242 bool Subclass() { |
| 3238 old_proc_ = reinterpret_cast<WNDPROC>( | 3243 old_proc_ = reinterpret_cast<WNDPROC>( |
| 3239 ::SetWindowLongPtr(window_, | 3244 ::SetWindowLongPtr(window_, |
| 3240 GWLP_WNDPROC, | 3245 GWLP_WNDPROC, |
| 3241 reinterpret_cast<LONG_PTR>(WndProc))); | 3246 reinterpret_cast<LONG_PTR>(WndProc))); |
| 3242 return old_proc_ != nullptr; | 3247 return old_proc_ != nullptr; |
| 3243 } | 3248 } |
| 3244 | 3249 |
| 3245 void Unsubclass() { | 3250 void Unsubclass() { |
| 3246 ::SetWindowLongPtr(window_, | 3251 ::SetWindowLongPtr(window_, |
| 3247 GWLP_WNDPROC, | 3252 GWLP_WNDPROC, |
| 3248 reinterpret_cast<LONG_PTR>(old_proc_)); | 3253 reinterpret_cast<LONG_PTR>(old_proc_)); |
| 3249 } | 3254 } |
| 3250 | 3255 |
| 3251 static LRESULT CALLBACK WndProc(HWND window, | 3256 static LRESULT CALLBACK WndProc(HWND window, |
| 3252 unsigned int message, | 3257 unsigned int message, |
| 3253 WPARAM w_param, | 3258 WPARAM w_param, |
| 3254 LPARAM l_param) { | 3259 LPARAM l_param) { |
| 3255 EXPECT_NE(instance_, nullptr); | 3260 EXPECT_NE(instance_, nullptr); |
| 3256 EXPECT_EQ(window, instance_->window_); | 3261 EXPECT_EQ(window, instance_->window_); |
| 3257 | 3262 |
| 3258 // Keep track of messags received for this window. | 3263 // Keep track of messags received for this window. |
| 3259 instance_->messages_.insert(message); | 3264 instance_->messages_.insert(message); |
| 3260 | 3265 |
| 3261 return ::CallWindowProc(instance_->old_proc_, window, message, w_param, | 3266 LRESULT ret = ::CallWindowProc(instance_->old_proc_, window, message, |
| 3262 l_param); | 3267 w_param, l_param); |
| 3268 if (message == instance_->message_to_destroy_on_) { |
| 3269 instance_->Unsubclass(); |
| 3270 ::DestroyWindow(window); |
| 3271 } |
| 3272 return ret; |
| 3263 } | 3273 } |
| 3264 | 3274 |
| 3265 WNDPROC old_proc_; | 3275 WNDPROC old_proc_; |
| 3266 HWND window_; | 3276 HWND window_; |
| 3267 static SubclassWindowHelper* instance_; | 3277 static SubclassWindowHelper* instance_; |
| 3268 std::set<unsigned int> messages_; | 3278 std::set<unsigned int> messages_; |
| 3279 unsigned int message_to_destroy_on_; |
| 3269 | 3280 |
| 3270 DISALLOW_COPY_AND_ASSIGN(SubclassWindowHelper); | 3281 DISALLOW_COPY_AND_ASSIGN(SubclassWindowHelper); |
| 3271 }; | 3282 }; |
| 3272 | 3283 |
| 3273 SubclassWindowHelper* SubclassWindowHelper::instance_ = nullptr; | 3284 SubclassWindowHelper* SubclassWindowHelper::instance_ = nullptr; |
| 3274 | 3285 |
| 3275 // This test validates whether the WM_SYSCOMMAND message for SC_MOVE is | 3286 // This test validates whether the WM_SYSCOMMAND message for SC_MOVE is |
| 3276 // received when we post a WM_NCLBUTTONDOWN message for the caption in the | 3287 // received when we post a WM_NCLBUTTONDOWN message for the caption in the |
| 3277 // following scenarios:- | 3288 // following scenarios:- |
| 3278 // 1. Posting a WM_NCMOUSEMOVE message for a different location. | 3289 // 1. Posting a WM_NCMOUSEMOVE message for a different location. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3340 ::PostMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(100, 100)); | 3351 ::PostMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(100, 100)); |
| 3341 ::PostMessage(window, WM_MOUSEMOVE, HTCLIENT, MAKELPARAM(110, 110)); | 3352 ::PostMessage(window, WM_MOUSEMOVE, HTCLIENT, MAKELPARAM(110, 110)); |
| 3342 RunPendingMessages(); | 3353 RunPendingMessages(); |
| 3343 | 3354 |
| 3344 EXPECT_TRUE(subclass_helper.received_message(WM_NCLBUTTONDOWN)); | 3355 EXPECT_TRUE(subclass_helper.received_message(WM_NCLBUTTONDOWN)); |
| 3345 EXPECT_TRUE(subclass_helper.received_message(WM_MOUSEMOVE)); | 3356 EXPECT_TRUE(subclass_helper.received_message(WM_MOUSEMOVE)); |
| 3346 EXPECT_TRUE(subclass_helper.received_message(WM_SYSCOMMAND)); | 3357 EXPECT_TRUE(subclass_helper.received_message(WM_SYSCOMMAND)); |
| 3347 | 3358 |
| 3348 widget.CloseNow(); | 3359 widget.CloseNow(); |
| 3349 } | 3360 } |
| 3361 |
| 3362 // This test validates that destroying the window in the context of the |
| 3363 // WM_SYSCOMMAND message with SC_MOVE does not crash. |
| 3364 TEST_F(WidgetTest, DestroyInSysCommandNCLButtonDownOnCaption) { |
| 3365 Widget widget; |
| 3366 Widget::InitParams params = |
| 3367 CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 3368 params.native_widget = new PlatformDesktopNativeWidget(&widget); |
| 3369 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 3370 widget.Init(params); |
| 3371 widget.SetBounds(gfx::Rect(0, 0, 200, 200)); |
| 3372 widget.Show(); |
| 3373 ::SetCursorPos(500, 500); |
| 3374 |
| 3375 HWND window = widget.GetNativeWindow()->GetHost()->GetAcceleratedWidget(); |
| 3376 |
| 3377 SubclassWindowHelper subclass_helper(window); |
| 3378 |
| 3379 // Destroying the window in the context of the WM_SYSCOMMAND message |
| 3380 // should not crash. |
| 3381 subclass_helper.set_message_to_destroy_on(WM_SYSCOMMAND); |
| 3382 |
| 3383 ::PostMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(100, 100)); |
| 3384 ::PostMessage(window, WM_NCMOUSEMOVE, HTCAPTION, MAKELPARAM(110, 110)); |
| 3385 RunPendingMessages(); |
| 3386 |
| 3387 EXPECT_TRUE(subclass_helper.received_message(WM_NCLBUTTONDOWN)); |
| 3388 EXPECT_TRUE(subclass_helper.received_message(WM_SYSCOMMAND)); |
| 3389 |
| 3390 widget.CloseNow(); |
| 3391 } |
| 3392 |
| 3350 #endif | 3393 #endif |
| 3351 | 3394 |
| 3352 // Test that SetAlwaysOnTop and IsAlwaysOnTop are consistent. | 3395 // Test that SetAlwaysOnTop and IsAlwaysOnTop are consistent. |
| 3353 TEST_F(WidgetTest, AlwaysOnTop) { | 3396 TEST_F(WidgetTest, AlwaysOnTop) { |
| 3354 WidgetAutoclosePtr widget(CreateTopLevelNativeWidget()); | 3397 WidgetAutoclosePtr widget(CreateTopLevelNativeWidget()); |
| 3355 EXPECT_FALSE(widget->IsAlwaysOnTop()); | 3398 EXPECT_FALSE(widget->IsAlwaysOnTop()); |
| 3356 widget->SetAlwaysOnTop(true); | 3399 widget->SetAlwaysOnTop(true); |
| 3357 EXPECT_TRUE(widget->IsAlwaysOnTop()); | 3400 EXPECT_TRUE(widget->IsAlwaysOnTop()); |
| 3358 widget->SetAlwaysOnTop(false); | 3401 widget->SetAlwaysOnTop(false); |
| 3359 EXPECT_FALSE(widget->IsAlwaysOnTop()); | 3402 EXPECT_FALSE(widget->IsAlwaysOnTop()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3396 EXPECT_EQ(scale_factor, view->last_scale_factor()); | 3439 EXPECT_EQ(scale_factor, view->last_scale_factor()); |
| 3397 | 3440 |
| 3398 // Changes should be propagated. | 3441 // Changes should be propagated. |
| 3399 scale_factor *= 2.0f; | 3442 scale_factor *= 2.0f; |
| 3400 widget->GetLayer()->OnDeviceScaleFactorChanged(scale_factor); | 3443 widget->GetLayer()->OnDeviceScaleFactorChanged(scale_factor); |
| 3401 EXPECT_EQ(scale_factor, view->last_scale_factor()); | 3444 EXPECT_EQ(scale_factor, view->last_scale_factor()); |
| 3402 } | 3445 } |
| 3403 | 3446 |
| 3404 } // namespace test | 3447 } // namespace test |
| 3405 } // namespace views | 3448 } // namespace views |
| OLD | NEW |