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 TestProperty* last_deleted() { return last_deleted_; } | |
sky
2017/01/24 18:45:38
Returning a pointer to an object that was deleted
kylix_rd
2017/01/24 21:20:00
These tests were lifted, nearly verbatim from wind
| |
31 | |
32 private: | |
33 static TestProperty* last_deleted_; | |
34 DISALLOW_COPY_AND_ASSIGN(TestProperty); | |
35 }; | |
36 | |
37 TestProperty* 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 class PropertyTest : public testing::Test { | |
sky
2017/01/24 18:45:38
To write gtests you don't have to subclass Test. O
kylix_rd
2017/01/24 21:20:00
Done.
| |
49 public: | |
50 PropertyTest() : testing::Test() {} | |
51 ~PropertyTest() override {} | |
52 private: | |
53 DISALLOW_COPY_AND_ASSIGN(PropertyTest); | |
54 }; | |
55 | |
56 namespace { | |
57 DEFINE_UI_CLASS_PROPERTY_KEY(int, kIntKey, -2); | |
58 DEFINE_UI_CLASS_PROPERTY_KEY(const char*, kStringKey, "squeamish"); | |
59 } | |
60 | |
61 TEST_F(PropertyTest, Property) { | |
62 std::unique_ptr<PropertyHandler> h = base::MakeUnique<PropertyHandler>(); | |
sky
2017/01/24 18:45:38
As you never explicitly delete 'h'. It seems like
kylix_rd
2017/01/24 21:20:00
Done.
| |
63 | |
64 // Non-existent properties should return the default values. | |
65 EXPECT_EQ(-2, h->GetProperty(kIntKey)); | |
sky
2017/01/24 18:45:38
These assertions would be less mysterious if you u
kylix_rd
2017/01/24 21:20:00
Done.
| |
66 EXPECT_EQ(std::string("squeamish"), h->GetProperty(kStringKey)); | |
67 | |
68 // A set property value should be returned again (even if it's the default | |
69 // value). | |
70 h->SetProperty(kIntKey, INT_MAX); | |
71 EXPECT_EQ(INT_MAX, h->GetProperty(kIntKey)); | |
72 h->SetProperty(kIntKey, -2); | |
73 EXPECT_EQ(-2, h->GetProperty(kIntKey)); | |
74 h->SetProperty(kIntKey, INT_MIN); | |
75 EXPECT_EQ(INT_MIN, h->GetProperty(kIntKey)); | |
76 | |
77 h->SetProperty(kStringKey, static_cast<const char*>(NULL)); | |
sky
2017/01/24 18:45:38
NULL -> nullptr. Maybe that means you don't need t
kylix_rd
2017/01/24 21:20:00
NULL->nullptr - Done.
Either the static_cast is n
| |
78 EXPECT_EQ(NULL, h->GetProperty(kStringKey)); | |
79 h->SetProperty(kStringKey, "squeamish"); | |
80 EXPECT_EQ(std::string("squeamish"), h->GetProperty(kStringKey)); | |
81 h->SetProperty(kStringKey, "ossifrage"); | |
82 EXPECT_EQ(std::string("ossifrage"), h->GetProperty(kStringKey)); | |
83 | |
84 // ClearProperty should restore the default value. | |
85 h->ClearProperty(kIntKey); | |
86 EXPECT_EQ(-2, h->GetProperty(kIntKey)); | |
87 h->ClearProperty(kStringKey); | |
88 EXPECT_EQ(std::string("squeamish"), h->GetProperty(kStringKey)); | |
89 } | |
90 | |
91 TEST_F(PropertyTest, OwnedProperty) { | |
92 std::unique_ptr<PropertyHandler> h = base::MakeUnique<PropertyHandler>(); | |
93 | |
94 EXPECT_EQ(NULL, h->GetProperty(kOwnedKey)); | |
95 TestProperty* last_deleted = TestProperty::last_deleted(); | |
96 TestProperty* p1 = new TestProperty(); | |
97 h->SetProperty(kOwnedKey, p1); | |
98 EXPECT_EQ(p1, h->GetProperty(kOwnedKey)); | |
99 EXPECT_EQ(last_deleted, TestProperty::last_deleted()); | |
100 | |
101 TestProperty* p2 = new TestProperty(); | |
102 h->SetProperty(kOwnedKey, p2); | |
103 EXPECT_EQ(p2, h->GetProperty(kOwnedKey)); | |
104 EXPECT_EQ(p1, TestProperty::last_deleted()); | |
105 | |
106 h->ClearProperty(kOwnedKey); | |
107 EXPECT_EQ(NULL, h->GetProperty(kOwnedKey)); | |
108 EXPECT_EQ(p2, TestProperty::last_deleted()); | |
109 | |
110 TestProperty* p3 = new TestProperty(); | |
111 h->SetProperty(kOwnedKey, p3); | |
112 EXPECT_EQ(p3, h->GetProperty(kOwnedKey)); | |
113 EXPECT_EQ(p2, TestProperty::last_deleted()); | |
114 h.reset(); | |
115 EXPECT_EQ(p3, TestProperty::last_deleted()); | |
116 } | |
117 | |
118 } // namespace test | |
119 } // namespace ui | |
OLD | NEW |