OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/services/public/cpp/view_manager/view.h" | 5 #include "mojo/services/public/cpp/view_manager/view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" | 9 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" |
10 #include "mojo/services/public/cpp/view_manager/util.h" | 10 #include "mojo/services/public/cpp/view_manager/util.h" |
11 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 11 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| 12 #include "mojo/services/public/cpp/view_manager/view_property.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace mojo { | 15 namespace mojo { |
15 | 16 |
16 // View ------------------------------------------------------------------------ | 17 // View ------------------------------------------------------------------------ |
17 | 18 |
18 typedef testing::Test ViewTest; | 19 typedef testing::Test ViewTest; |
19 | 20 |
20 // Subclass with public ctor/dtor. | 21 // Subclass with public ctor/dtor. |
21 class TestView : public View { | 22 class TestView : public View { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 TestView v11; | 96 TestView v11; |
96 v1.AddChild(&v11); | 97 v1.AddChild(&v11); |
97 EXPECT_TRUE(v11.visible()); | 98 EXPECT_TRUE(v11.visible()); |
98 EXPECT_TRUE(v11.IsDrawn()); | 99 EXPECT_TRUE(v11.IsDrawn()); |
99 | 100 |
100 v1.RemoveChild(&v11); | 101 v1.RemoveChild(&v11); |
101 EXPECT_TRUE(v11.visible()); | 102 EXPECT_TRUE(v11.visible()); |
102 EXPECT_FALSE(v11.IsDrawn()); | 103 EXPECT_FALSE(v11.IsDrawn()); |
103 } | 104 } |
104 | 105 |
| 106 namespace { |
| 107 DEFINE_VIEW_PROPERTY_KEY(int, kIntKey, -2); |
| 108 DEFINE_VIEW_PROPERTY_KEY(const char*, kStringKey, "squeamish"); |
| 109 } |
| 110 |
| 111 TEST_F(ViewTest, Property) { |
| 112 TestView v; |
| 113 |
| 114 // Non-existent properties should return the default values. |
| 115 EXPECT_EQ(-2, v.GetLocalProperty(kIntKey)); |
| 116 EXPECT_EQ(std::string("squeamish"), v.GetLocalProperty(kStringKey)); |
| 117 |
| 118 // A set property value should be returned again (even if it's the default |
| 119 // value). |
| 120 v.SetLocalProperty(kIntKey, INT_MAX); |
| 121 EXPECT_EQ(INT_MAX, v.GetLocalProperty(kIntKey)); |
| 122 v.SetLocalProperty(kIntKey, -2); |
| 123 EXPECT_EQ(-2, v.GetLocalProperty(kIntKey)); |
| 124 v.SetLocalProperty(kIntKey, INT_MIN); |
| 125 EXPECT_EQ(INT_MIN, v.GetLocalProperty(kIntKey)); |
| 126 |
| 127 v.SetLocalProperty(kStringKey, static_cast<const char*>(NULL)); |
| 128 EXPECT_EQ(NULL, v.GetLocalProperty(kStringKey)); |
| 129 v.SetLocalProperty(kStringKey, "squeamish"); |
| 130 EXPECT_EQ(std::string("squeamish"), v.GetLocalProperty(kStringKey)); |
| 131 v.SetLocalProperty(kStringKey, "ossifrage"); |
| 132 EXPECT_EQ(std::string("ossifrage"), v.GetLocalProperty(kStringKey)); |
| 133 |
| 134 // ClearProperty should restore the default value. |
| 135 v.ClearLocalProperty(kIntKey); |
| 136 EXPECT_EQ(-2, v.GetLocalProperty(kIntKey)); |
| 137 v.ClearLocalProperty(kStringKey); |
| 138 EXPECT_EQ(std::string("squeamish"), v.GetLocalProperty(kStringKey)); |
| 139 } |
| 140 |
| 141 namespace { |
| 142 |
| 143 class TestProperty { |
| 144 public: |
| 145 TestProperty() {} |
| 146 virtual ~TestProperty() { last_deleted_ = this; } |
| 147 static TestProperty* last_deleted() { return last_deleted_; } |
| 148 |
| 149 private: |
| 150 static TestProperty* last_deleted_; |
| 151 DISALLOW_COPY_AND_ASSIGN(TestProperty); |
| 152 }; |
| 153 |
| 154 TestProperty* TestProperty::last_deleted_ = NULL; |
| 155 |
| 156 DEFINE_OWNED_VIEW_PROPERTY_KEY(TestProperty, kOwnedKey, NULL); |
| 157 |
| 158 } // namespace |
| 159 |
| 160 TEST_F(ViewTest, OwnedProperty) { |
| 161 TestProperty* p3 = NULL; |
| 162 { |
| 163 TestView v; |
| 164 EXPECT_EQ(NULL, v.GetLocalProperty(kOwnedKey)); |
| 165 TestProperty* p1 = new TestProperty(); |
| 166 v.SetLocalProperty(kOwnedKey, p1); |
| 167 EXPECT_EQ(p1, v.GetLocalProperty(kOwnedKey)); |
| 168 EXPECT_EQ(NULL, TestProperty::last_deleted()); |
| 169 |
| 170 TestProperty* p2 = new TestProperty(); |
| 171 v.SetLocalProperty(kOwnedKey, p2); |
| 172 EXPECT_EQ(p2, v.GetLocalProperty(kOwnedKey)); |
| 173 EXPECT_EQ(p1, TestProperty::last_deleted()); |
| 174 |
| 175 v.ClearLocalProperty(kOwnedKey); |
| 176 EXPECT_EQ(NULL, v.GetLocalProperty(kOwnedKey)); |
| 177 EXPECT_EQ(p2, TestProperty::last_deleted()); |
| 178 |
| 179 p3 = new TestProperty(); |
| 180 v.SetLocalProperty(kOwnedKey, p3); |
| 181 EXPECT_EQ(p3, v.GetLocalProperty(kOwnedKey)); |
| 182 EXPECT_EQ(p2, TestProperty::last_deleted()); |
| 183 } |
| 184 |
| 185 EXPECT_EQ(p3, TestProperty::last_deleted()); |
| 186 } |
| 187 |
105 // ViewObserver -------------------------------------------------------- | 188 // ViewObserver -------------------------------------------------------- |
106 | 189 |
107 typedef testing::Test ViewObserverTest; | 190 typedef testing::Test ViewObserverTest; |
108 | 191 |
109 bool TreeChangeParamsMatch(const ViewObserver::TreeChangeParams& lhs, | 192 bool TreeChangeParamsMatch(const ViewObserver::TreeChangeParams& lhs, |
110 const ViewObserver::TreeChangeParams& rhs) { | 193 const ViewObserver::TreeChangeParams& rhs) { |
111 return lhs.target == rhs.target && lhs.old_parent == rhs.old_parent && | 194 return lhs.target == rhs.target && lhs.old_parent == rhs.old_parent && |
112 lhs.new_parent == rhs.new_parent && lhs.receiver == rhs.receiver; | 195 lhs.new_parent == rhs.new_parent && lhs.receiver == rhs.receiver; |
113 } | 196 } |
114 | 197 |
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 { | 690 { |
608 // Set visible to existing value and verify no notifications. | 691 // Set visible to existing value and verify no notifications. |
609 VisibilityChangeObserver observer(&v1); | 692 VisibilityChangeObserver observer(&v1); |
610 v1.SetVisible(false); | 693 v1.SetVisible(false); |
611 EXPECT_TRUE(observer.GetAndClearChanges().empty()); | 694 EXPECT_TRUE(observer.GetAndClearChanges().empty()); |
612 } | 695 } |
613 } | 696 } |
614 | 697 |
615 namespace { | 698 namespace { |
616 | 699 |
617 class PropertyChangeObserver : public ViewObserver { | 700 class SharedPropertyChangeObserver : public ViewObserver { |
618 public: | 701 public: |
619 explicit PropertyChangeObserver(View* view) : view_(view) { | 702 explicit SharedPropertyChangeObserver(View* view) : view_(view) { |
620 view_->AddObserver(this); | 703 view_->AddObserver(this); |
621 } | 704 } |
622 virtual ~PropertyChangeObserver() { view_->RemoveObserver(this); } | 705 virtual ~SharedPropertyChangeObserver() { view_->RemoveObserver(this); } |
623 | 706 |
624 Changes GetAndClearChanges() { | 707 Changes GetAndClearChanges() { |
625 Changes changes; | 708 Changes changes; |
626 changes.swap(changes_); | 709 changes.swap(changes_); |
627 return changes; | 710 return changes; |
628 } | 711 } |
629 | 712 |
630 private: | 713 private: |
631 // Overridden from ViewObserver: | 714 // Overridden from ViewObserver: |
632 void OnViewPropertyChanged(View* view, | 715 void OnViewSharedPropertyChanged( |
633 const std::string& name, | 716 View* view, |
634 const std::vector<uint8_t>* old_data, | 717 const std::string& name, |
635 const std::vector<uint8_t>* new_data) override { | 718 const std::vector<uint8_t>* old_data, |
| 719 const std::vector<uint8_t>* new_data) override { |
636 changes_.push_back(base::StringPrintf( | 720 changes_.push_back(base::StringPrintf( |
637 "view=%s property changed key=%s old_value=%s new_value=%s", | 721 "view=%s shared property changed key=%s old_value=%s new_value=%s", |
638 ViewIdToString(view->id()).c_str(), | 722 ViewIdToString(view->id()).c_str(), name.c_str(), |
639 name.c_str(), | 723 VectorToString(old_data).c_str(), VectorToString(new_data).c_str())); |
640 VectorToString(old_data).c_str(), | |
641 VectorToString(new_data).c_str())); | |
642 } | 724 } |
643 | 725 |
644 std::string VectorToString(const std::vector<uint8_t>* data) { | 726 std::string VectorToString(const std::vector<uint8_t>* data) { |
645 if (!data) | 727 if (!data) |
646 return "NULL"; | 728 return "NULL"; |
647 std::string s; | 729 std::string s; |
648 for (char c : *data) | 730 for (char c : *data) |
649 s += c; | 731 s += c; |
650 return s; | 732 return s; |
651 } | 733 } |
652 | 734 |
653 View* view_; | 735 View* view_; |
654 Changes changes_; | 736 Changes changes_; |
655 | 737 |
656 DISALLOW_COPY_AND_ASSIGN(PropertyChangeObserver); | 738 DISALLOW_COPY_AND_ASSIGN(SharedPropertyChangeObserver); |
657 }; | 739 }; |
658 | 740 |
659 } // namespace | 741 } // namespace |
660 | 742 |
661 TEST_F(ViewObserverTest, SetProperty) { | 743 TEST_F(ViewObserverTest, SetLocalProperty) { |
662 TestView v1; | 744 TestView v1; |
663 std::vector<uint8_t> one(1, '1'); | 745 std::vector<uint8_t> one(1, '1'); |
664 | 746 |
665 { | 747 { |
666 // Change visibility from true to false and make sure we get notifications. | 748 // Change visibility from true to false and make sure we get notifications. |
667 PropertyChangeObserver observer(&v1); | 749 SharedPropertyChangeObserver observer(&v1); |
668 v1.SetProperty("one", &one); | 750 v1.SetSharedProperty("one", &one); |
669 Changes changes = observer.GetAndClearChanges(); | 751 Changes changes = observer.GetAndClearChanges(); |
670 ASSERT_EQ(1U, changes.size()); | 752 ASSERT_EQ(1U, changes.size()); |
671 EXPECT_EQ("view=0,1 property changed key=one old_value=NULL new_value=1", | 753 EXPECT_EQ( |
672 changes[0]); | 754 "view=0,1 shared property changed key=one old_value=NULL new_value=1", |
673 EXPECT_EQ(1U, v1.properties().size()); | 755 changes[0]); |
| 756 EXPECT_EQ(1U, v1.shared_properties().size()); |
674 } | 757 } |
675 { | 758 { |
676 // Set visible to existing value and verify no notifications. | 759 // Set visible to existing value and verify no notifications. |
677 PropertyChangeObserver observer(&v1); | 760 SharedPropertyChangeObserver observer(&v1); |
678 v1.SetProperty("one", &one); | 761 v1.SetSharedProperty("one", &one); |
679 EXPECT_TRUE(observer.GetAndClearChanges().empty()); | 762 EXPECT_TRUE(observer.GetAndClearChanges().empty()); |
680 EXPECT_EQ(1U, v1.properties().size()); | 763 EXPECT_EQ(1U, v1.shared_properties().size()); |
681 } | 764 } |
682 { | 765 { |
683 // Set the value to NULL to delete it. | 766 // Set the value to NULL to delete it. |
684 // Change visibility from true to false and make sure we get notifications. | 767 // Change visibility from true to false and make sure we get notifications. |
685 PropertyChangeObserver observer(&v1); | 768 SharedPropertyChangeObserver observer(&v1); |
686 v1.SetProperty("one", NULL); | 769 v1.SetSharedProperty("one", NULL); |
687 Changes changes = observer.GetAndClearChanges(); | 770 Changes changes = observer.GetAndClearChanges(); |
688 ASSERT_EQ(1U, changes.size()); | 771 ASSERT_EQ(1U, changes.size()); |
689 EXPECT_EQ("view=0,1 property changed key=one old_value=1 new_value=NULL", | 772 EXPECT_EQ( |
690 changes[0]); | 773 "view=0,1 shared property changed key=one old_value=1 new_value=NULL", |
691 EXPECT_EQ(0U, v1.properties().size()); | 774 changes[0]); |
| 775 EXPECT_EQ(0U, v1.shared_properties().size()); |
692 } | 776 } |
693 { | 777 { |
694 // Setting a null property to null shouldn't update us. | 778 // Setting a null property to null shouldn't update us. |
695 PropertyChangeObserver observer(&v1); | 779 SharedPropertyChangeObserver observer(&v1); |
696 v1.SetProperty("one", NULL); | 780 v1.SetSharedProperty("one", NULL); |
697 EXPECT_TRUE(observer.GetAndClearChanges().empty()); | 781 EXPECT_TRUE(observer.GetAndClearChanges().empty()); |
698 EXPECT_EQ(0U, v1.properties().size()); | 782 EXPECT_EQ(0U, v1.shared_properties().size()); |
699 } | 783 } |
700 } | 784 } |
701 | 785 |
| 786 namespace { |
| 787 |
| 788 typedef std::pair<const void*, intptr_t> PropertyChangeInfo; |
| 789 |
| 790 class LocalPropertyChangeObserver : public ViewObserver { |
| 791 public: |
| 792 explicit LocalPropertyChangeObserver(View* view) |
| 793 : view_(view), |
| 794 property_key_(nullptr), |
| 795 old_property_value_(-1) { |
| 796 view_->AddObserver(this); |
| 797 } |
| 798 virtual ~LocalPropertyChangeObserver() { view_->RemoveObserver(this); } |
| 799 |
| 800 PropertyChangeInfo PropertyChangeInfoAndClear() { |
| 801 PropertyChangeInfo result(property_key_, old_property_value_); |
| 802 property_key_ = NULL; |
| 803 old_property_value_ = -3; |
| 804 return result; |
| 805 } |
| 806 |
| 807 private: |
| 808 void OnViewLocalPropertyChanged(View* window, |
| 809 const void* key, |
| 810 intptr_t old) override { |
| 811 property_key_ = key; |
| 812 old_property_value_ = old; |
| 813 } |
| 814 |
| 815 View* view_; |
| 816 const void* property_key_; |
| 817 intptr_t old_property_value_; |
| 818 |
| 819 DISALLOW_COPY_AND_ASSIGN(LocalPropertyChangeObserver); |
| 820 }; |
| 821 |
| 822 } // namespace |
| 823 |
| 824 TEST_F(ViewObserverTest, LocalPropertyChanged) { |
| 825 TestView v1; |
| 826 LocalPropertyChangeObserver o(&v1); |
| 827 |
| 828 static const ViewProperty<int> prop = {-2}; |
| 829 |
| 830 v1.SetLocalProperty(&prop, 1); |
| 831 EXPECT_EQ(PropertyChangeInfo(&prop, -2), o.PropertyChangeInfoAndClear()); |
| 832 v1.SetLocalProperty(&prop, -2); |
| 833 EXPECT_EQ(PropertyChangeInfo(&prop, 1), o.PropertyChangeInfoAndClear()); |
| 834 v1.SetLocalProperty(&prop, 3); |
| 835 EXPECT_EQ(PropertyChangeInfo(&prop, -2), o.PropertyChangeInfoAndClear()); |
| 836 v1.ClearLocalProperty(&prop); |
| 837 EXPECT_EQ(PropertyChangeInfo(&prop, 3), o.PropertyChangeInfoAndClear()); |
| 838 |
| 839 // Sanity check to see if |PropertyChangeInfoAndClear| really clears. |
| 840 EXPECT_EQ(PropertyChangeInfo( |
| 841 reinterpret_cast<const void*>(NULL), -3), o.PropertyChangeInfoAndClear()); |
| 842 } |
| 843 |
702 } // namespace mojo | 844 } // namespace mojo |
OLD | NEW |