OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/base/class_property.h" |
| 6 |
| 7 #include <limits.h> |
| 8 |
| 9 #include <string> |
| 10 #include <utility> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/compiler_specific.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" |
| 16 #include "build/build_config.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 DECLARE_UI_CLASS_PROPERTY_TYPE(const char*) |
| 20 DECLARE_UI_CLASS_PROPERTY_TYPE(int) |
| 21 |
| 22 namespace { |
| 23 |
| 24 class TestProperty { |
| 25 public: |
| 26 TestProperty() {} |
| 27 ~TestProperty() { |
| 28 last_deleted_ = this; |
| 29 } |
| 30 static void* last_deleted() { return last_deleted_; } |
| 31 |
| 32 private: |
| 33 static void* last_deleted_; |
| 34 DISALLOW_COPY_AND_ASSIGN(TestProperty); |
| 35 }; |
| 36 |
| 37 void* TestProperty::last_deleted_ = nullptr; |
| 38 |
| 39 DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(TestProperty, kOwnedKey, NULL); |
| 40 |
| 41 } // namespace |
| 42 |
| 43 DECLARE_UI_CLASS_PROPERTY_TYPE(TestProperty*); |
| 44 |
| 45 namespace ui { |
| 46 namespace test { |
| 47 |
| 48 namespace { |
| 49 |
| 50 const int kDefaultIntValue = -2; |
| 51 const char* kDefaultStringValue = "squeamish"; |
| 52 const char* kTestStringValue = "ossifrage"; |
| 53 |
| 54 DEFINE_UI_CLASS_PROPERTY_KEY(int, kIntKey, kDefaultIntValue); |
| 55 DEFINE_UI_CLASS_PROPERTY_KEY(const char*, kStringKey, kDefaultStringValue); |
| 56 } |
| 57 |
| 58 TEST(PropertyTest, Property) { |
| 59 PropertyHandler h; |
| 60 |
| 61 // Non-existent properties should return the default values. |
| 62 EXPECT_EQ(kDefaultIntValue, h.GetProperty(kIntKey)); |
| 63 EXPECT_EQ(std::string(kDefaultStringValue), h.GetProperty(kStringKey)); |
| 64 |
| 65 // A set property value should be returned again (even if it's the default |
| 66 // value). |
| 67 h.SetProperty(kIntKey, INT_MAX); |
| 68 EXPECT_EQ(INT_MAX, h.GetProperty(kIntKey)); |
| 69 h.SetProperty(kIntKey, kDefaultIntValue); |
| 70 EXPECT_EQ(kDefaultIntValue, h.GetProperty(kIntKey)); |
| 71 h.SetProperty(kIntKey, INT_MIN); |
| 72 EXPECT_EQ(INT_MIN, h.GetProperty(kIntKey)); |
| 73 |
| 74 h.SetProperty<const char*>(kStringKey, nullptr); |
| 75 EXPECT_EQ(NULL, h.GetProperty(kStringKey)); |
| 76 h.SetProperty(kStringKey, kDefaultStringValue); |
| 77 EXPECT_EQ(std::string(kDefaultStringValue), h.GetProperty(kStringKey)); |
| 78 h.SetProperty(kStringKey, kTestStringValue); |
| 79 EXPECT_EQ(std::string(kTestStringValue), h.GetProperty(kStringKey)); |
| 80 |
| 81 // ClearProperty should restore the default value. |
| 82 h.ClearProperty(kIntKey); |
| 83 EXPECT_EQ(kDefaultIntValue, h.GetProperty(kIntKey)); |
| 84 h.ClearProperty(kStringKey); |
| 85 EXPECT_EQ(std::string(kDefaultStringValue), h.GetProperty(kStringKey)); |
| 86 } |
| 87 |
| 88 TEST(PropertyTest, OwnedProperty) { |
| 89 std::unique_ptr<PropertyHandler> h = base::MakeUnique<PropertyHandler>(); |
| 90 |
| 91 EXPECT_EQ(NULL, h->GetProperty(kOwnedKey)); |
| 92 void* last_deleted = TestProperty::last_deleted(); |
| 93 TestProperty* p1 = new TestProperty(); |
| 94 h->SetProperty(kOwnedKey, p1); |
| 95 EXPECT_EQ(p1, h->GetProperty(kOwnedKey)); |
| 96 EXPECT_EQ(last_deleted, TestProperty::last_deleted()); |
| 97 |
| 98 TestProperty* p2 = new TestProperty(); |
| 99 h->SetProperty(kOwnedKey, p2); |
| 100 EXPECT_EQ(p2, h->GetProperty(kOwnedKey)); |
| 101 EXPECT_EQ(p1, TestProperty::last_deleted()); |
| 102 |
| 103 h->ClearProperty(kOwnedKey); |
| 104 EXPECT_EQ(NULL, h->GetProperty(kOwnedKey)); |
| 105 EXPECT_EQ(p2, TestProperty::last_deleted()); |
| 106 |
| 107 TestProperty* p3 = new TestProperty(); |
| 108 h->SetProperty(kOwnedKey, p3); |
| 109 EXPECT_EQ(p3, h->GetProperty(kOwnedKey)); |
| 110 EXPECT_EQ(p2, TestProperty::last_deleted()); |
| 111 h.reset(); |
| 112 EXPECT_EQ(p3, TestProperty::last_deleted()); |
| 113 } |
| 114 |
| 115 } // namespace test |
| 116 } // namespace ui |
OLD | NEW |