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