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 11 matching lines...) Expand all Loading... |
22 #include "ui/gfx/geometry/point.h" | 22 #include "ui/gfx/geometry/point.h" |
23 #include "ui/gfx/native_widget_types.h" | 23 #include "ui/gfx/native_widget_types.h" |
24 #include "ui/views/bubble/bubble_delegate.h" | 24 #include "ui/views/bubble/bubble_delegate.h" |
25 #include "ui/views/controls/textfield/textfield.h" | 25 #include "ui/views/controls/textfield/textfield.h" |
26 #include "ui/views/test/test_views.h" | 26 #include "ui/views/test/test_views.h" |
27 #include "ui/views/test/test_widget_observer.h" | 27 #include "ui/views/test/test_widget_observer.h" |
28 #include "ui/views/test/widget_test.h" | 28 #include "ui/views/test/widget_test.h" |
29 #include "ui/views/widget/native_widget_delegate.h" | 29 #include "ui/views/widget/native_widget_delegate.h" |
30 #include "ui/views/widget/root_view.h" | 30 #include "ui/views/widget/root_view.h" |
31 #include "ui/views/widget/widget_deletion_observer.h" | 31 #include "ui/views/widget/widget_deletion_observer.h" |
| 32 #include "ui/views/widget/widget_removals_observer.h" |
32 #include "ui/views/window/dialog_delegate.h" | 33 #include "ui/views/window/dialog_delegate.h" |
33 #include "ui/views/window/native_frame_view.h" | 34 #include "ui/views/window/native_frame_view.h" |
34 | 35 |
35 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
36 #include "ui/aura/window.h" | 37 #include "ui/aura/window.h" |
37 #include "ui/aura/window_tree_host.h" | 38 #include "ui/aura/window_tree_host.h" |
38 #include "ui/base/view_prop.h" | 39 #include "ui/base/view_prop.h" |
39 #include "ui/base/win/window_event_target.h" | 40 #include "ui/base/win/window_event_target.h" |
40 #include "ui/views/win/hwnd_util.h" | 41 #include "ui/views/win/hwnd_util.h" |
41 #endif | 42 #endif |
(...skipping 3395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3437 // about the initial scale factor. Fake it. | 3438 // about the initial scale factor. Fake it. |
3438 view->OnDeviceScaleFactorChanged(scale_factor); | 3439 view->OnDeviceScaleFactorChanged(scale_factor); |
3439 EXPECT_EQ(scale_factor, view->last_scale_factor()); | 3440 EXPECT_EQ(scale_factor, view->last_scale_factor()); |
3440 | 3441 |
3441 // Changes should be propagated. | 3442 // Changes should be propagated. |
3442 scale_factor *= 2.0f; | 3443 scale_factor *= 2.0f; |
3443 widget->GetLayer()->OnDeviceScaleFactorChanged(scale_factor); | 3444 widget->GetLayer()->OnDeviceScaleFactorChanged(scale_factor); |
3444 EXPECT_EQ(scale_factor, view->last_scale_factor()); | 3445 EXPECT_EQ(scale_factor, view->last_scale_factor()); |
3445 } | 3446 } |
3446 | 3447 |
| 3448 namespace { |
| 3449 |
| 3450 class TestWidgetRemovalsObserver : public WidgetRemovalsObserver { |
| 3451 public: |
| 3452 TestWidgetRemovalsObserver() {} |
| 3453 ~TestWidgetRemovalsObserver() override {} |
| 3454 |
| 3455 void OnWillRemoveView(Widget* widget, View* view) override { |
| 3456 removed_views_.insert(view); |
| 3457 } |
| 3458 |
| 3459 bool DidRemoveView(View* view) { |
| 3460 return removed_views_.find(view) != removed_views_.end(); |
| 3461 } |
| 3462 |
| 3463 private: |
| 3464 std::set<View*> removed_views_; |
| 3465 }; |
| 3466 |
| 3467 } |
| 3468 |
| 3469 // Test that WidgetRemovalsObserver::OnWillRemoveView is called when deleting |
| 3470 // a view. |
| 3471 TEST_F(WidgetTest, WidgetRemovalsObserverCalled) { |
| 3472 WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget()); |
| 3473 TestWidgetRemovalsObserver removals_observer; |
| 3474 widget->AddRemovalsObserver(&removals_observer); |
| 3475 |
| 3476 View* parent = new View(); |
| 3477 widget->client_view()->AddChildView(parent); |
| 3478 |
| 3479 View* child = new View(); |
| 3480 parent->AddChildView(child); |
| 3481 |
| 3482 widget->client_view()->RemoveChildView(parent); |
| 3483 EXPECT_TRUE(removals_observer.DidRemoveView(parent)); |
| 3484 EXPECT_FALSE(removals_observer.DidRemoveView(child)); |
| 3485 |
| 3486 widget->RemoveRemovalsObserver(&removals_observer); |
| 3487 } |
| 3488 |
| 3489 // Test that WidgetRemovalsObserver::OnWillRemoveView is called when moving |
| 3490 // a view from one widget to another, but not when moving a view within |
| 3491 // the same widget. |
| 3492 TEST_F(WidgetTest, WidgetRemovalsObserverCalledWhenMovingBetweenWidgets) { |
| 3493 WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget()); |
| 3494 TestWidgetRemovalsObserver removals_observer; |
| 3495 widget->AddRemovalsObserver(&removals_observer); |
| 3496 |
| 3497 View* parent = new View(); |
| 3498 widget->client_view()->AddChildView(parent); |
| 3499 |
| 3500 View* child = new View(); |
| 3501 widget->client_view()->AddChildView(child); |
| 3502 |
| 3503 // Reparenting the child shouldn't call the removals observer. |
| 3504 parent->AddChildView(child); |
| 3505 EXPECT_FALSE(removals_observer.DidRemoveView(child)); |
| 3506 |
| 3507 // Moving the child to a different widget should call the removals observer. |
| 3508 WidgetAutoclosePtr widget2(CreateTopLevelPlatformWidget()); |
| 3509 widget2->client_view()->AddChildView(child); |
| 3510 EXPECT_TRUE(removals_observer.DidRemoveView(child)); |
| 3511 |
| 3512 widget->RemoveRemovalsObserver(&removals_observer); |
| 3513 } |
| 3514 |
3447 } // namespace test | 3515 } // namespace test |
3448 } // namespace views | 3516 } // namespace views |
OLD | NEW |