1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/profiling/memory/system_property.h"
18
19 #include "test/gtest_and_gmock.h"
20
21 namespace perfetto {
22 namespace profiling {
23 namespace {
24
25 using ::testing::InSequence;
26 using ::testing::Return;
27
28 class MockSystemProperties : public SystemProperties {
29 public:
30 MOCK_METHOD2(SetAndroidProperty,
31 bool(const std::string&, const std::string&));
32 };
33
TEST(SystemPropertyTest,All)34 TEST(SystemPropertyTest, All) {
35 MockSystemProperties prop;
36 InSequence s;
37 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "all"))
38 .WillOnce(Return(true));
39 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
40 .WillOnce(Return(true));
41 auto handle = prop.SetAll();
42 }
43
TEST(SystemPropertyTest,RefcountAll)44 TEST(SystemPropertyTest, RefcountAll) {
45 MockSystemProperties prop;
46 InSequence s;
47 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "all"))
48 .WillOnce(Return(true));
49 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
50 .WillOnce(Return(true));
51 {
52 auto handle = prop.SetAll();
53 { auto handle2 = prop.SetAll(); }
54 }
55 }
56
TEST(SystemPropertyTest,CleanupAll)57 TEST(SystemPropertyTest, CleanupAll) {
58 MockSystemProperties prop;
59 InSequence s;
60 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "all"))
61 .WillOnce(Return(true));
62 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
63 .WillOnce(Return(true));
64 { auto handle = prop.SetAll(); }
65 }
66
TEST(SystemPropertyTest,Specific)67 TEST(SystemPropertyTest, Specific) {
68 MockSystemProperties prop;
69 InSequence s;
70 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
71 .WillOnce(Return(true));
72 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "1"))
73 .WillOnce(Return(true));
74 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", ""))
75 .WillOnce(Return(true));
76 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
77 .WillOnce(Return(true));
78 auto handle2 = prop.SetProperty("system_server");
79 }
80
TEST(SystemPropertyTest,RefcountSpecific)81 TEST(SystemPropertyTest, RefcountSpecific) {
82 MockSystemProperties prop;
83 InSequence s;
84 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
85 .WillOnce(Return(true));
86 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "1"))
87 .WillOnce(Return(true));
88 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", ""))
89 .WillOnce(Return(true));
90 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
91 .WillOnce(Return(true));
92 {
93 auto handle = prop.SetProperty("system_server");
94 { auto handle2 = prop.SetProperty("system_server"); }
95 }
96 }
97
TEST(SystemPropertyTest,CleanupSpecific)98 TEST(SystemPropertyTest, CleanupSpecific) {
99 MockSystemProperties prop;
100 InSequence s;
101 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
102 .WillOnce(Return(true));
103 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "1"))
104 .WillOnce(Return(true));
105 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", ""))
106 .WillOnce(Return(true));
107 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
108 .WillOnce(Return(true));
109 { auto handle2 = prop.SetProperty("system_server"); }
110 }
111
TEST(SystemPropertyTest,AllAndSpecific)112 TEST(SystemPropertyTest, AllAndSpecific) {
113 MockSystemProperties prop;
114 InSequence s;
115 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "all"))
116 .WillOnce(Return(true));
117 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
118 .WillOnce(Return(true));
119 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "1"))
120 .WillOnce(Return(true));
121 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", ""))
122 .WillOnce(Return(true));
123 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
124 .WillOnce(Return(true));
125 auto handle = prop.SetAll();
126 auto handle2 = prop.SetProperty("system_server");
127 { SystemProperties::Handle destroy = std::move(handle); }
128 }
129
TEST(SystemPropertyTest,AllFailed)130 TEST(SystemPropertyTest, AllFailed) {
131 MockSystemProperties prop;
132 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "all"))
133 .WillOnce(Return(false));
134 auto handle = prop.SetAll();
135 EXPECT_FALSE(handle);
136 }
137
TEST(SystemPropertyTest,SpecificFailed)138 TEST(SystemPropertyTest, SpecificFailed) {
139 MockSystemProperties prop;
140 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
141 .WillOnce(Return(false));
142 auto handle = prop.SetProperty("system_server");
143 EXPECT_FALSE(handle);
144 }
145
TEST(SystemPropertyTest,SpecificFailedMainProperty)146 TEST(SystemPropertyTest, SpecificFailedMainProperty) {
147 MockSystemProperties prop;
148 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
149 .WillOnce(Return(true));
150 EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "1"))
151 .WillOnce(Return(false));
152 auto handle = prop.SetProperty("system_server");
153 EXPECT_FALSE(handle);
154 }
155
156 } // namespace
157 } // namespace profiling
158 } // namespace perfetto
159