• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_METHOD(bool,
31               SetAndroidProperty,
32               (const std::string&, const std::string&),
33               (override));
34 };
35 
TEST(SystemPropertyTest,All)36 TEST(SystemPropertyTest, All) {
37   MockSystemProperties prop;
38   InSequence s;
39   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "all"))
40       .WillOnce(Return(true));
41   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
42       .WillOnce(Return(true));
43   auto handle = prop.SetAll();
44 }
45 
TEST(SystemPropertyTest,RefcountAll)46 TEST(SystemPropertyTest, RefcountAll) {
47   MockSystemProperties prop;
48   InSequence s;
49   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "all"))
50       .WillOnce(Return(true));
51   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
52       .WillOnce(Return(true));
53   {
54     auto handle = prop.SetAll();
55     { auto handle2 = prop.SetAll(); }
56   }
57 }
58 
TEST(SystemPropertyTest,CleanupAll)59 TEST(SystemPropertyTest, CleanupAll) {
60   MockSystemProperties prop;
61   InSequence s;
62   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "all"))
63       .WillOnce(Return(true));
64   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
65       .WillOnce(Return(true));
66   { auto handle = prop.SetAll(); }
67 }
68 
TEST(SystemPropertyTest,Specific)69 TEST(SystemPropertyTest, Specific) {
70   MockSystemProperties prop;
71   InSequence s;
72   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
73       .WillOnce(Return(true));
74   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "1"))
75       .WillOnce(Return(true));
76   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", ""))
77       .WillOnce(Return(true));
78   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
79       .WillOnce(Return(true));
80   auto handle2 = prop.SetProperty("system_server");
81 }
82 
TEST(SystemPropertyTest,RefcountSpecific)83 TEST(SystemPropertyTest, RefcountSpecific) {
84   MockSystemProperties prop;
85   InSequence s;
86   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
87       .WillOnce(Return(true));
88   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "1"))
89       .WillOnce(Return(true));
90   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", ""))
91       .WillOnce(Return(true));
92   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
93       .WillOnce(Return(true));
94   {
95     auto handle = prop.SetProperty("system_server");
96     { auto handle2 = prop.SetProperty("system_server"); }
97   }
98 }
99 
TEST(SystemPropertyTest,CleanupSpecific)100 TEST(SystemPropertyTest, CleanupSpecific) {
101   MockSystemProperties prop;
102   InSequence s;
103   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
104       .WillOnce(Return(true));
105   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "1"))
106       .WillOnce(Return(true));
107   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", ""))
108       .WillOnce(Return(true));
109   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
110       .WillOnce(Return(true));
111   { auto handle2 = prop.SetProperty("system_server"); }
112 }
113 
TEST(SystemPropertyTest,AllAndSpecific)114 TEST(SystemPropertyTest, AllAndSpecific) {
115   MockSystemProperties prop;
116   InSequence s;
117   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "all"))
118       .WillOnce(Return(true));
119   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
120       .WillOnce(Return(true));
121   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "1"))
122       .WillOnce(Return(true));
123   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", ""))
124       .WillOnce(Return(true));
125   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", ""))
126       .WillOnce(Return(true));
127   auto handle = prop.SetAll();
128   auto handle2 = prop.SetProperty("system_server");
129   { SystemProperties::Handle destroy = std::move(handle); }
130 }
131 
TEST(SystemPropertyTest,AllFailed)132 TEST(SystemPropertyTest, AllFailed) {
133   MockSystemProperties prop;
134   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "all"))
135       .WillOnce(Return(false));
136   auto handle = prop.SetAll();
137   EXPECT_FALSE(handle);
138 }
139 
TEST(SystemPropertyTest,SpecificFailed)140 TEST(SystemPropertyTest, SpecificFailed) {
141   MockSystemProperties prop;
142   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
143       .WillOnce(Return(false));
144   auto handle = prop.SetProperty("system_server");
145   EXPECT_FALSE(handle);
146 }
147 
TEST(SystemPropertyTest,SpecificFailedMainProperty)148 TEST(SystemPropertyTest, SpecificFailedMainProperty) {
149   MockSystemProperties prop;
150   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable.system_server", "1"))
151       .WillOnce(Return(true));
152   EXPECT_CALL(prop, SetAndroidProperty("heapprofd.enable", "1"))
153       .WillOnce(Return(false));
154   auto handle = prop.SetProperty("system_server");
155   EXPECT_FALSE(handle);
156 }
157 
158 }  // namespace
159 }  // namespace profiling
160 }  // namespace perfetto
161