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/ftrace_reader/ftrace_procfs.h"
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21
22 using testing::AnyNumber;
23 using testing::IsEmpty;
24 using testing::Return;
25 using testing::UnorderedElementsAre;
26
27 namespace perfetto {
28 namespace {
29
30 class MockFtraceProcfs : public FtraceProcfs {
31 public:
MockFtraceProcfs()32 MockFtraceProcfs() : FtraceProcfs("/root/") {}
33
34 MOCK_METHOD2(WriteToFile,
35 bool(const std::string& path, const std::string& str));
36 MOCK_METHOD1(ReadOneCharFromFile, char(const std::string& path));
37 MOCK_METHOD1(ClearFile, bool(const std::string& path));
38 MOCK_CONST_METHOD1(ReadFileIntoString, std::string(const std::string& path));
39 MOCK_CONST_METHOD0(NumberOfCpus, size_t());
40 };
41
TEST(FtraceProcfsTest,ParseAvailableClocks)42 TEST(FtraceProcfsTest, ParseAvailableClocks) {
43 MockFtraceProcfs ftrace;
44
45 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
46 .WillOnce(Return("[local] global boot"));
47 EXPECT_THAT(ftrace.AvailableClocks(),
48 UnorderedElementsAre("local", "global", "boot"));
49
50 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
51 .WillOnce(Return("[local] global boot"));
52 EXPECT_THAT(ftrace.GetClock(), "local");
53
54 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
55 .WillOnce(Return("local [global] boot"));
56 EXPECT_THAT(ftrace.GetClock(), "global");
57
58 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
59 .WillOnce(Return(""));
60 EXPECT_THAT(ftrace.AvailableClocks(), IsEmpty());
61 }
62
63 } // namespace
64 } // namespace perfetto
65