1 /*
2 * Copyright (C) 2021 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 "perfetto/tracing/traced_proto.h"
18
19 #include "perfetto/tracing/track_event.h"
20 #include "protos/perfetto/trace/test_event.gen.h"
21 #include "protos/perfetto/trace/test_event.pb.h"
22 #include "protos/perfetto/trace/test_event.pbzero.h"
23 #include "protos/perfetto/trace/track_event/track_event.gen.h"
24 #include "protos/perfetto/trace/track_event/track_event.pb.h"
25 #include "test/gtest_and_gmock.h"
26
27 namespace perfetto {
28
29 class TracedProtoTest : public ::testing::Test {
30 public:
TracedProtoTest()31 TracedProtoTest() : context_(track_event_.get(), &incremental_state_) {}
32
context()33 EventContext& context() { return context_; }
34
35 private:
36 protozero::HeapBuffered<protos::pbzero::TrackEvent> track_event_;
37 internal::TrackEventIncrementalState incremental_state_;
38 EventContext context_;
39 };
40
41 using TestPayload = protos::pbzero::TestEvent::TestPayload;
42
TEST_F(TracedProtoTest,SingleInt)43 TEST_F(TracedProtoTest, SingleInt) {
44 protozero::HeapBuffered<TestPayload> event;
45 WriteIntoTracedProto(context().Wrap(event.get()), TestPayload::kSingleInt,
46 42);
47
48 protos::TestEvent::TestPayload result;
49 result.ParseFromString(event.SerializeAsString());
50 EXPECT_TRUE(result.has_single_int());
51 EXPECT_EQ(result.single_int(), 42);
52 }
53
TEST_F(TracedProtoTest,RepeatedInt)54 TEST_F(TracedProtoTest, RepeatedInt) {
55 protozero::HeapBuffered<TestPayload> event;
56 WriteIntoTracedProto(context().Wrap(event.get()), TestPayload::kRepeatedInts,
57 std::vector<int>{1, 2, 3});
58
59 protos::TestEvent::TestPayload result;
60 result.ParseFromString(event.SerializeAsString());
61 EXPECT_THAT(result.repeated_ints(), ::testing::ElementsAre(1, 2, 3));
62 }
63
TEST_F(TracedProtoTest,SingleString)64 TEST_F(TracedProtoTest, SingleString) {
65 protozero::HeapBuffered<TestPayload> event;
66 WriteIntoTracedProto(context().Wrap(event.get()), TestPayload::kSingleString,
67 "foo");
68
69 protos::TestEvent::TestPayload result;
70 result.ParseFromString(event.SerializeAsString());
71 EXPECT_TRUE(result.has_single_string());
72 EXPECT_EQ(result.single_string(), "foo");
73 }
74
TEST_F(TracedProtoTest,RepeatedString)75 TEST_F(TracedProtoTest, RepeatedString) {
76 protozero::HeapBuffered<TestPayload> event;
77 WriteIntoTracedProto(context().Wrap(event.get()), TestPayload::kStr,
78 std::vector<std::string>{"foo", "bar"});
79
80 protos::TestEvent::TestPayload result;
81 result.ParseFromString(event.SerializeAsString());
82 EXPECT_THAT(result.str(), ::testing::ElementsAre("foo", "bar"));
83 }
84
85 namespace {
86
87 struct Foo {
WriteIntoTraceperfetto::__anon9d29d3d00111::Foo88 void WriteIntoTrace(TracedProto<TestPayload> message) const {
89 message->set_single_int(42);
90 }
91 };
92
93 } // namespace
94
TEST_F(TracedProtoTest,SingleNestedMessage)95 TEST_F(TracedProtoTest, SingleNestedMessage) {
96 protozero::HeapBuffered<protos::pbzero::TestEvent> event;
97 WriteIntoTracedProto(context().Wrap(event.get()),
98 protos::pbzero::TestEvent::kPayload, Foo());
99
100 protos::TestEvent result;
101 result.ParseFromString(event.SerializeAsString());
102 EXPECT_EQ(result.payload().single_int(), 42);
103 }
104
TEST_F(TracedProtoTest,RepeatedNestedMessage)105 TEST_F(TracedProtoTest, RepeatedNestedMessage) {
106 protozero::HeapBuffered<TestPayload> event;
107 WriteIntoTracedProto(context().Wrap(event.get()), TestPayload::kNested,
108 std::vector<Foo>{Foo(), Foo()});
109
110 protos::TestEvent::TestPayload result;
111 result.ParseFromString(event.SerializeAsString());
112 EXPECT_EQ(result.nested_size(), 2);
113 EXPECT_EQ(result.nested(0).single_int(), 42);
114 EXPECT_EQ(result.nested(1).single_int(), 42);
115 }
116
117 } // namespace perfetto
118