Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: views/view_unittest.cc

Issue 8202026: views: Some more layer change, and add/update some tests for layers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include <map> 5 #include <map>
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gmock/include/gmock/gmock.h"
(...skipping 27 matching lines...) Expand all
38 38
39 #if defined(OS_WIN) 39 #if defined(OS_WIN)
40 #include "views/test/test_views_delegate.h" 40 #include "views/test/test_views_delegate.h"
41 #endif 41 #endif
42 #if defined(USE_AURA) 42 #if defined(USE_AURA)
43 #include "ui/aura/desktop.h" 43 #include "ui/aura/desktop.h"
44 #endif 44 #endif
45 45
46 using ::testing::_; 46 using ::testing::_;
47 47
48 namespace {
49
50 // Returns true if |ancestor| is an ancestor of |layer|.
51 bool LayerIsAncestor(const ui::Layer* ancestor, const ui::Layer* layer) {
52 while (layer && layer != ancestor)
53 layer = layer->parent();
54 return layer == ancestor;
55 }
56
57 }
58
48 namespace views { 59 namespace views {
49 60
50 typedef ViewsTestBase ViewTest; 61 typedef ViewsTestBase ViewTest;
51 62
52 // A derived class for testing purpose. 63 // A derived class for testing purpose.
53 class TestView : public View { 64 class TestView : public View {
54 public: 65 public:
55 TestView() : View(), in_touch_sequence_(false) {} 66 TestView() : View(), in_touch_sequence_(false) {}
56 virtual ~TestView() {} 67 virtual ~TestView() {}
57 68
(...skipping 2501 matching lines...) Expand 10 before | Expand all | Expand 10 after
2559 v1->AddChildView(v2); 2570 v1->AddChildView(v2);
2560 v2->SetPaintToLayer(true); 2571 v2->SetPaintToLayer(true);
2561 ASSERT_TRUE(v2->layer() != NULL); 2572 ASSERT_TRUE(v2->layer() != NULL);
2562 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2->layer()->bounds()); 2573 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2->layer()->bounds());
2563 2574
2564 v1->SetPosition(gfx::Point(25, 36)); 2575 v1->SetPosition(gfx::Point(25, 36));
2565 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2->layer()->bounds()); 2576 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2->layer()->bounds());
2566 2577
2567 v2->SetPosition(gfx::Point(11, 12)); 2578 v2->SetPosition(gfx::Point(11, 12));
2568 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2->layer()->bounds()); 2579 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2->layer()->bounds());
2580
2581 // Bounds of the layer should change even if the view is not invisible.
2582 v1->SetVisible(false);
2583 v1->SetPosition(gfx::Point(20, 30));
2584 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2->layer()->bounds());
2585
2586 v2->SetVisible(false);
2587 v2->SetBounds(10, 11, 20, 30);
2588 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2->layer()->bounds());
2569 } 2589 }
2570 2590
2571 // Makes sure a transform persists after toggling the visibility. 2591 // Makes sure a transform persists after toggling the visibility.
2572 TEST_F(ViewLayerTest, ToggleVisibilityWithTransform) { 2592 TEST_F(ViewLayerTest, ToggleVisibilityWithTransform) {
2573 View* view = new View; 2593 View* view = new View;
2574 ui::Transform transform; 2594 ui::Transform transform;
2575 transform.SetScale(2.0f, 2.0f); 2595 transform.SetScale(2.0f, 2.0f);
2576 view->SetTransform(transform); 2596 view->SetTransform(transform);
2577 widget()->SetContentsView(view); 2597 widget()->SetContentsView(view);
2578 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0)); 2598 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
(...skipping 18 matching lines...) Expand all
2597 2617
2598 View* parent = view->parent(); 2618 View* parent = view->parent();
2599 parent->RemoveChildView(view); 2619 parent->RemoveChildView(view);
2600 parent->AddChildView(view); 2620 parent->AddChildView(view);
2601 2621
2602 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0)); 2622 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
2603 ASSERT_TRUE(view->layer() != NULL); 2623 ASSERT_TRUE(view->layer() != NULL);
2604 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0)); 2624 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
2605 } 2625 }
2606 2626
2607 // Makes sure that layer persists after toggling the visibility. 2627 // Makes sure that layer visibility is correct after toggling View visibility.
2608 TEST_F(ViewLayerTest, ToggleVisibilityWithLayer) { 2628 TEST_F(ViewLayerTest, ToggleVisibilityWithLayer) {
2609 View* content_view = new View; 2629 View* content_view = new View;
2610 widget()->SetContentsView(content_view); 2630 widget()->SetContentsView(content_view);
2611 2631
2632 // The view isn't attached to a widget or a parent view yet. But it should
2633 // still have a layer, but the layer should not be attached to the root
2634 // layer.
2612 View* v1 = new View; 2635 View* v1 = new View;
2636 v1->SetPaintToLayer(true);
2637 EXPECT_TRUE(v1->layer());
2638 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2639 v1->layer()));
2640
2641 // Once the view is attached to a widget, its layer should be attached to the
2642 // root layer and visible.
2613 content_view->AddChildView(v1); 2643 content_view->AddChildView(v1);
2614 v1->SetPaintToLayer(true); 2644 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2645 v1->layer()));
2646 EXPECT_TRUE(v1->layer()->IsDrawn());
2647
2648 v1->SetVisible(false);
2649 EXPECT_FALSE(v1->layer()->IsDrawn());
2615 2650
2616 v1->SetVisible(true); 2651 v1->SetVisible(true);
2617 EXPECT_TRUE(v1->layer()); 2652 EXPECT_TRUE(v1->layer()->IsDrawn());
2618 2653
2619 v1->SetVisible(false); 2654 widget()->Hide();
2620 EXPECT_TRUE(v1->layer()); 2655 EXPECT_FALSE(v1->layer()->IsDrawn());
2621 2656
2622 v1->SetVisible(true); 2657 widget()->Show();
2623 EXPECT_TRUE(v1->layer()); 2658 EXPECT_TRUE(v1->layer()->IsDrawn());
2624 } 2659 }
2625 2660
2626 // Test that a hole in a layer is correctly created regardless of whether 2661 // Test that a hole in a layer is correctly created regardless of whether
2627 // the opacity attribute is set before or after the layer is created. 2662 // the opacity attribute is set before or after the layer is created.
2628 TEST_F(ViewLayerTest, ToggleOpacityWithLayer) { 2663 TEST_F(ViewLayerTest, ToggleOpacityWithLayer) {
2629 View* content_view = new View; 2664 View* content_view = new View;
2630 widget()->SetContentsView(content_view); 2665 widget()->SetContentsView(content_view);
2631 2666
2632 View* parent_view = new View; 2667 View* parent_view = new View;
2633 content_view->AddChildView(parent_view); 2668 content_view->AddChildView(parent_view);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
2710 gfx::Rect(50, 50, 100, 100), parent_view->layer()->hole_rect()); 2745 gfx::Rect(50, 50, 100, 100), parent_view->layer()->hole_rect());
2711 2746
2712 child_view->SetVisible(false); 2747 child_view->SetVisible(false);
2713 EXPECT_TRUE(parent_view->layer()->hole_rect().IsEmpty()); 2748 EXPECT_TRUE(parent_view->layer()->hole_rect().IsEmpty());
2714 2749
2715 child_view->SetVisible(true); 2750 child_view->SetVisible(true);
2716 EXPECT_EQ( 2751 EXPECT_EQ(
2717 gfx::Rect(50, 50, 100, 100), parent_view->layer()->hole_rect()); 2752 gfx::Rect(50, 50, 100, 100), parent_view->layer()->hole_rect());
2718 } 2753 }
2719 2754
2755 // Tests that the layers in the subtree are orphaned after a View is removed
2756 // from the parent.
2757 TEST_F(ViewLayerTest, OrphanLayerAfterViewRemove) {
2758 View* content_view = new View;
2759 widget()->SetContentsView(content_view);
2760
2761 View* v1 = new View;
2762 content_view->AddChildView(v1);
2763
2764 View* v2 = new View;
2765 v1->AddChildView(v2);
2766 v2->SetPaintToLayer(true);
2767 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2768 v2->layer()));
2769 EXPECT_TRUE(v2->layer()->IsDrawn());
2770
2771 content_view->RemoveChildView(v1);
2772 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2773 v2->layer()));
2774
2775 // Reparent |v2|.
2776 content_view->AddChildView(v2);
2777 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2778 v2->layer()));
2779 EXPECT_TRUE(v2->layer()->IsDrawn());
2780 }
2781
2720 // TODO(sky): reenable once focus issues are straightened out so that this 2782 // TODO(sky): reenable once focus issues are straightened out so that this
2721 // doesn't crash. 2783 // doesn't crash.
2722 TEST_F(ViewLayerTest, DISABLED_NativeWidgetView) { 2784 TEST_F(ViewLayerTest, DISABLED_NativeWidgetView) {
2723 View* content_view = new View; 2785 View* content_view = new View;
2724 widget()->SetContentsView(content_view); 2786 widget()->SetContentsView(content_view);
2725 View* view = new View; 2787 View* view = new View;
2726 content_view->AddChildView(view); 2788 content_view->AddChildView(view);
2727 view->SetBounds(10, 20, 300, 400); 2789 view->SetBounds(10, 20, 300, 400);
2728 2790
2729 views_delegate().set_default_parent_view(view); 2791 views_delegate().set_default_parent_view(view);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
2800 // Make content_view have a dirty rect, paint the layers and make sure 2862 // Make content_view have a dirty rect, paint the layers and make sure
2801 // PaintTrackingView is painted. 2863 // PaintTrackingView is painted.
2802 content_view->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10)); 2864 content_view->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
2803 GetRootLayer()->DrawTree(); 2865 GetRootLayer()->DrawTree();
2804 EXPECT_TRUE(content_view->painted()); 2866 EXPECT_TRUE(content_view->painted());
2805 } 2867 }
2806 2868
2807 #endif // VIEWS_COMPOSITOR 2869 #endif // VIEWS_COMPOSITOR
2808 2870
2809 } // namespace views 2871 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698