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 <map> | 5 #include <map> |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "grit/ui_strings.h" | 11 #include "grit/ui_strings.h" |
12 #include "ui/aura/window.h" | |
13 #include "ui/aura/window_event_dispatcher.h" | |
14 #include "ui/base/accelerators/accelerator.h" | 12 #include "ui/base/accelerators/accelerator.h" |
15 #include "ui/base/clipboard/clipboard.h" | 13 #include "ui/base/clipboard/clipboard.h" |
16 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
17 #include "ui/compositor/compositor.h" | 15 #include "ui/compositor/compositor.h" |
18 #include "ui/compositor/layer.h" | 16 #include "ui/compositor/layer.h" |
19 #include "ui/compositor/layer_animator.h" | 17 #include "ui/compositor/layer_animator.h" |
20 #include "ui/compositor/layer_tree_owner.h" | |
21 #include "ui/compositor/test/draw_waiter_for_test.h" | 18 #include "ui/compositor/test/draw_waiter_for_test.h" |
22 #include "ui/compositor/test/test_layers.h" | |
23 #include "ui/events/event.h" | 19 #include "ui/events/event.h" |
24 #include "ui/events/gestures/gesture_recognizer.h" | 20 #include "ui/events/gestures/gesture_recognizer.h" |
25 #include "ui/events/keycodes/keyboard_codes.h" | 21 #include "ui/events/keycodes/keyboard_codes.h" |
26 #include "ui/gfx/canvas.h" | 22 #include "ui/gfx/canvas.h" |
27 #include "ui/gfx/path.h" | 23 #include "ui/gfx/path.h" |
28 #include "ui/gfx/transform.h" | 24 #include "ui/gfx/transform.h" |
29 #include "ui/views/background.h" | 25 #include "ui/views/background.h" |
30 #include "ui/views/controls/native/native_view_host.h" | 26 #include "ui/views/controls/native/native_view_host.h" |
31 #include "ui/views/controls/scroll_view.h" | 27 #include "ui/views/controls/scroll_view.h" |
32 #include "ui/views/controls/textfield/textfield.h" | 28 #include "ui/views/controls/textfield/textfield.h" |
33 #include "ui/views/focus/view_storage.h" | 29 #include "ui/views/focus/view_storage.h" |
34 #include "ui/views/test/views_test_base.h" | 30 #include "ui/views/test/views_test_base.h" |
35 #include "ui/views/view.h" | 31 #include "ui/views/view.h" |
36 #include "ui/views/view_constants_aura.h" | |
37 #include "ui/views/views_delegate.h" | 32 #include "ui/views/views_delegate.h" |
38 #include "ui/views/widget/native_widget.h" | 33 #include "ui/views/widget/native_widget.h" |
39 #include "ui/views/widget/root_view.h" | 34 #include "ui/views/widget/root_view.h" |
40 #include "ui/views/window/dialog_client_view.h" | 35 #include "ui/views/window/dialog_client_view.h" |
41 #include "ui/views/window/dialog_delegate.h" | 36 #include "ui/views/window/dialog_delegate.h" |
42 #include "ui/wm/core/window_util.h" | |
43 | 37 |
44 using base::ASCIIToUTF16; | 38 using base::ASCIIToUTF16; |
45 | 39 |
46 namespace { | 40 namespace { |
47 | 41 |
48 // Returns true if |ancestor| is an ancestor of |layer|. | 42 // Returns true if |ancestor| is an ancestor of |layer|. |
49 bool LayerIsAncestor(const ui::Layer* ancestor, const ui::Layer* layer) { | 43 bool LayerIsAncestor(const ui::Layer* ancestor, const ui::Layer* layer) { |
50 while (layer && layer != ancestor) | 44 while (layer && layer != ancestor) |
51 layer = layer->parent(); | 45 layer = layer->parent(); |
52 return layer == ancestor; | 46 return layer == ancestor; |
(...skipping 3857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3910 // whether the View is focusable right now. | 3904 // whether the View is focusable right now. |
3911 TestView view; | 3905 TestView view; |
3912 view.SetFocusable(true); | 3906 view.SetFocusable(true); |
3913 EXPECT_TRUE(view.focusable()); | 3907 EXPECT_TRUE(view.focusable()); |
3914 view.SetEnabled(false); | 3908 view.SetEnabled(false); |
3915 EXPECT_TRUE(view.focusable()); | 3909 EXPECT_TRUE(view.focusable()); |
3916 view.SetFocusable(false); | 3910 view.SetFocusable(false); |
3917 EXPECT_FALSE(view.focusable()); | 3911 EXPECT_FALSE(view.focusable()); |
3918 } | 3912 } |
3919 | 3913 |
3920 // Creates a widget of TYPE_CONTROL. | |
3921 // The caller takes ownership of the returned widget. | |
3922 Widget* CreateControlWidget(aura::Window* parent, const gfx::Rect& bounds) { | |
3923 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); | |
3924 params.parent = parent; | |
3925 params.bounds = bounds; | |
3926 Widget* widget = new Widget(); | |
3927 widget->Init(params); | |
3928 return widget; | |
3929 } | |
3930 | |
3931 // Returns a view with a layer with the passed in |bounds| and |layer_name|. | |
3932 // The caller takes ownership of the returned view. | |
3933 View* CreateViewWithLayer(const gfx::Rect& bounds, | |
3934 const char* layer_name) { | |
3935 View* view = new View(); | |
3936 view->SetBoundsRect(bounds); | |
3937 view->SetPaintToLayer(true); | |
3938 view->layer()->set_name(layer_name); | |
3939 return view; | |
3940 } | |
3941 | |
3942 // Test that RecreateWindowLayers() recreates the layers for all child windows | |
3943 // and all child views and that the z-order of the recreated layers matches that | |
3944 // of the original layers. | |
3945 // Test hierarchy: | |
3946 // w1 | |
3947 // +-- v1 | |
3948 // +-- v2 (no layer) | |
3949 // +-- v3 (no layer) | |
3950 // +-- v4 | |
3951 // +-- w2 | |
3952 // +-- v5 | |
3953 // +-- v6 | |
3954 // +-- v7 | |
3955 // +-- v8 | |
3956 // +-- v9 | |
3957 TEST_F(ViewTest, RecreateLayers) { | |
3958 Widget* w1 = CreateControlWidget(GetContext(), gfx::Rect(0, 0, 100, 100)); | |
3959 w1->GetNativeView()->layer()->set_name("w1"); | |
3960 | |
3961 View* v2 = new View(); | |
3962 v2->SetBounds(0, 1, 100, 101); | |
3963 View* v3 = new View(); | |
3964 v3->SetBounds(0, 2, 100, 102); | |
3965 View* w2_host_view = new View(); | |
3966 | |
3967 View* v1 = CreateViewWithLayer(gfx::Rect(0, 3, 100, 103), "v1"); | |
3968 ui::Layer* v1_layer = v1->layer(); | |
3969 w1->GetRootView()->AddChildView(v1); | |
3970 w1->GetRootView()->AddChildView(v2); | |
3971 v2->AddChildView(v3); | |
3972 View* v4 = CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v4"); | |
3973 ui::Layer* v4_layer = v4->layer(); | |
3974 v2->AddChildView(v4); | |
3975 | |
3976 w1->GetRootView()->AddChildView(w2_host_view); | |
3977 View* v7 = CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v7"); | |
3978 ui::Layer* v7_layer = v7->layer(); | |
3979 w1->GetRootView()->AddChildView(v7); | |
3980 | |
3981 View* v8 = CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v8"); | |
3982 ui::Layer* v8_layer = v8->layer(); | |
3983 v7->AddChildView(v8); | |
3984 | |
3985 View* v9 = CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v9"); | |
3986 ui::Layer* v9_layer = v9->layer(); | |
3987 v7->AddChildView(v9); | |
3988 | |
3989 Widget* w2 = CreateControlWidget(w1->GetNativeView(), | |
3990 gfx::Rect(0, 5, 100, 105)); | |
3991 w2->GetNativeView()->layer()->set_name("w2"); | |
3992 w2->GetNativeView()->SetProperty(kHostViewKey, w2_host_view); | |
3993 | |
3994 View* v5 = CreateViewWithLayer(gfx::Rect(0, 6, 100, 106), "v5"); | |
3995 w2->GetRootView()->AddChildView(v5); | |
3996 View* v6 = CreateViewWithLayer(gfx::Rect(0, 7, 100, 107), "v6"); | |
3997 ui::Layer* v6_layer = v6->layer(); | |
3998 v5->AddChildView(v6); | |
3999 | |
4000 // Test the initial order of the layers. | |
4001 ui::Layer* w1_layer = w1->GetNativeView()->layer(); | |
4002 ASSERT_EQ("w1", w1_layer->name()); | |
4003 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_layer)); | |
4004 ui::Layer* w2_layer = w1_layer->children()[2]; | |
4005 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_layer)); | |
4006 ui::Layer* v5_layer = w2_layer->children()[0]; | |
4007 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_layer)); | |
4008 | |
4009 { | |
4010 scoped_ptr<ui::LayerTreeOwner> cloned_owner( | |
4011 wm::RecreateLayers(w1->GetNativeView())); | |
4012 EXPECT_EQ(w1_layer, cloned_owner->root()); | |
4013 EXPECT_NE(w1_layer, w1->GetNativeView()->layer()); | |
4014 | |
4015 // The old layers should still exist and have the same hierarchy. | |
4016 ASSERT_EQ("w1", w1_layer->name()); | |
4017 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_layer)); | |
4018 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_layer)); | |
4019 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_layer)); | |
4020 EXPECT_EQ("v8 v9", ui::test::ChildLayerNamesAsString(*v7_layer)); | |
4021 | |
4022 ASSERT_EQ(4u, w1_layer->children().size()); | |
4023 EXPECT_EQ(v1_layer, w1_layer->children()[0]); | |
4024 EXPECT_EQ(v4_layer, w1_layer->children()[1]); | |
4025 EXPECT_EQ(w2_layer, w1_layer->children()[2]); | |
4026 EXPECT_EQ(v7_layer, w1_layer->children()[3]); | |
4027 | |
4028 ASSERT_EQ(1u, w2_layer->children().size()); | |
4029 EXPECT_EQ(v5_layer, w2_layer->children()[0]); | |
4030 | |
4031 ASSERT_EQ(1u, v5_layer->children().size()); | |
4032 EXPECT_EQ(v6_layer, v5_layer->children()[0]); | |
4033 | |
4034 ASSERT_EQ(0u, v6_layer->children().size()); | |
4035 | |
4036 EXPECT_EQ(2u, v7_layer->children().size()); | |
4037 EXPECT_EQ(v8_layer, v7_layer->children()[0]); | |
4038 EXPECT_EQ(v9_layer, v7_layer->children()[1]); | |
4039 | |
4040 // The cloned layers should have the same hierarchy as old. | |
4041 ui::Layer* w1_new_layer = w1->GetNativeView()->layer(); | |
4042 EXPECT_EQ("w1", w1_new_layer->name()); | |
4043 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_new_layer)); | |
4044 ui::Layer* w2_new_layer = w1_new_layer->children()[2]; | |
4045 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_new_layer)); | |
4046 ui::Layer* v5_new_layer = w2_new_layer->children()[0]; | |
4047 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_new_layer)); | |
4048 ui::Layer* v7_new_layer = w1_new_layer->children()[3]; | |
4049 ASSERT_EQ("v8 v9", ui::test::ChildLayerNamesAsString(*v7_new_layer)); | |
4050 } | |
4051 // The views and the widgets are destroyed when AuraTestHelper::TearDown() | |
4052 // destroys root_window(). | |
4053 } | |
4054 | |
4055 // Verifies when a view is deleted it is removed from ViewStorage. | 3914 // Verifies when a view is deleted it is removed from ViewStorage. |
4056 TEST_F(ViewTest, UpdateViewStorageOnDelete) { | 3915 TEST_F(ViewTest, UpdateViewStorageOnDelete) { |
4057 ViewStorage* view_storage = ViewStorage::GetInstance(); | 3916 ViewStorage* view_storage = ViewStorage::GetInstance(); |
4058 const int storage_id = view_storage->CreateStorageID(); | 3917 const int storage_id = view_storage->CreateStorageID(); |
4059 { | 3918 { |
4060 View view; | 3919 View view; |
4061 view_storage->StoreView(storage_id, &view); | 3920 view_storage->StoreView(storage_id, &view); |
4062 } | 3921 } |
4063 EXPECT_TRUE(view_storage->RetrieveView(storage_id) == NULL); | 3922 EXPECT_TRUE(view_storage->RetrieveView(storage_id) == NULL); |
4064 } | 3923 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4096 // notification. | 3955 // notification. |
4097 TestView* test_view_child_2 = new TestView(); | 3956 TestView* test_view_child_2 = new TestView(); |
4098 test_view->AddChildView(test_view_child_2); | 3957 test_view->AddChildView(test_view_child_2); |
4099 EXPECT_TRUE(test_view_child_2->native_theme_); | 3958 EXPECT_TRUE(test_view_child_2->native_theme_); |
4100 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_); | 3959 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_); |
4101 | 3960 |
4102 widget->CloseNow(); | 3961 widget->CloseNow(); |
4103 } | 3962 } |
4104 | 3963 |
4105 } // namespace views | 3964 } // namespace views |
OLD | NEW |