1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/debug/trace_event_argument.h"
8 #include "base/time/time.h"
9 #include "base/values.h"
10 #include "cc/debug/frame_timing_tracker.h"
11 #include "testing/gtest/include/gtest/gtest.h"
16 static std::string
ToString(
17 scoped_ptr
<FrameTimingTracker::CompositeTimingSet
> timingset
) {
18 scoped_refptr
<base::debug::TracedValue
> value
=
19 new base::debug::TracedValue();
20 value
->BeginArray("values");
21 for (const auto& it
: *timingset
) {
22 value
->BeginDictionary();
23 value
->SetInteger("rect_id", it
.first
);
24 value
->BeginArray("events");
25 for (const auto& event
: it
.second
) {
26 value
->BeginDictionary();
27 value
->SetInteger("frame_id", event
.frame_id
);
28 value
->SetInteger("timestamp", event
.timestamp
.ToInternalValue());
29 value
->EndDictionary();
32 value
->EndDictionary();
35 return value
->ToString();
38 TEST(FrameTimingTrackerTest
, DefaultTrackerIsEmpty
) {
39 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
40 EXPECT_EQ("{\"values\":[]}", ToString(tracker
->GroupCountsByRectId()));
43 TEST(FrameTimingTrackerTest
, NoFrameIdsIsEmpty
) {
44 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
45 std::vector
<std::pair
<int, int64_t>> ids
;
46 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(100), ids
);
47 EXPECT_EQ("{\"values\":[]}", ToString(tracker
->GroupCountsByRectId()));
50 TEST(FrameTimingTrackerTest
, OneFrameId
) {
51 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
52 std::vector
<std::pair
<int, int64_t>> ids
;
53 ids
.push_back(std::make_pair(1, 2));
54 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(100), ids
);
56 "{\"values\":[{\"events\":["
57 "{\"frame_id\":1,\"timestamp\":100}],\"rect_id\":2}]}",
58 ToString(tracker
->GroupCountsByRectId()));
61 TEST(FrameTimingTrackerTest
, UnsortedTimestampsIds
) {
62 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
63 std::vector
<std::pair
<int, int64_t>> ids
;
64 ids
.push_back(std::make_pair(1, 2));
65 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(200), ids
);
66 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(400), ids
);
67 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(100), ids
);
69 "{\"values\":[{\"events\":["
70 "{\"frame_id\":1,\"timestamp\":100},"
71 "{\"frame_id\":1,\"timestamp\":200},"
72 "{\"frame_id\":1,\"timestamp\":400}],\"rect_id\":2}]}",
73 ToString(tracker
->GroupCountsByRectId()));
76 TEST(FrameTimingTrackerTest
, MultipleFrameIds
) {
77 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
79 std::vector
<std::pair
<int, int64_t>> ids200
;
80 ids200
.push_back(std::make_pair(1, 2));
81 ids200
.push_back(std::make_pair(1, 3));
82 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(200), ids200
);
84 std::vector
<std::pair
<int, int64_t>> ids400
;
85 ids400
.push_back(std::make_pair(2, 2));
86 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(400), ids400
);
88 std::vector
<std::pair
<int, int64_t>> ids100
;
89 ids100
.push_back(std::make_pair(3, 2));
90 ids100
.push_back(std::make_pair(2, 3));
91 ids100
.push_back(std::make_pair(3, 4));
92 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(100), ids100
);
94 std::string result
= ToString(tracker
->GroupCountsByRectId());
97 "{\"values\":[{\"events\":["
98 "{\"frame_id\":3,\"timestamp\":100},"
99 "{\"frame_id\":1,\"timestamp\":200},"
100 "{\"frame_id\":2,\"timestamp\":400}],\"rect_id\":2},"
102 "{\"frame_id\":2,\"timestamp\":100},"
103 "{\"frame_id\":1,\"timestamp\":200}],\"rect_id\":3},"
105 "{\"frame_id\":3,\"timestamp\":100}],\"rect_id\":4}"
108 EXPECT_NE(std::string::npos
,
110 "{\"frame_id\":3,\"timestamp\":100},"
111 "{\"frame_id\":1,\"timestamp\":200},"
112 "{\"frame_id\":2,\"timestamp\":400}],\"rect_id\":2}"));
113 EXPECT_NE(std::string::npos
,
116 "{\"frame_id\":2,\"timestamp\":100},"
117 "{\"frame_id\":1,\"timestamp\":200}],\"rect_id\":3}"));
118 EXPECT_NE(std::string::npos
,
121 "{\"frame_id\":3,\"timestamp\":100}],\"rect_id\":4}"));