1// Copyright 2013 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.
4
5#include "ui/events/latency_info.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace ui {
10
11TEST(LatencyInfoTest, AddTwoSeparateEvent) {
12  LatencyInfo info;
13  info.AddLatencyNumberWithTimestamp(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT,
14                                     0,
15                                     1,
16                                     base::TimeTicks::FromInternalValue(100),
17                                     1,
18                                     true);
19  info.AddLatencyNumberWithTimestamp(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
20                                     1,
21                                     5,
22                                     base::TimeTicks::FromInternalValue(1000),
23                                     2,
24                                     true);
25
26  EXPECT_EQ(info.latency_components.size(), 2u);
27  LatencyInfo::LatencyComponent component;
28  EXPECT_FALSE(
29      info.FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT, 0, &component));
30  EXPECT_FALSE(
31      info.FindLatency(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT, 1, &component));
32  EXPECT_TRUE(
33      info.FindLatency(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT, 0, &component));
34  EXPECT_EQ(component.sequence_number, 1);
35  EXPECT_EQ(component.event_count, 1u);
36  EXPECT_EQ(component.event_time.ToInternalValue(), 100);
37  EXPECT_TRUE(
38      info.FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 1, &component));
39  EXPECT_EQ(component.sequence_number, 5);
40  EXPECT_EQ(component.event_count, 2u);
41  EXPECT_EQ(component.event_time.ToInternalValue(), 1000);
42}
43
44TEST(LatencyInfoTest, AddTwoSameEvent) {
45  LatencyInfo info;
46  info.AddLatencyNumberWithTimestamp(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
47                                     0,
48                                     30,
49                                     base::TimeTicks::FromInternalValue(100),
50                                     2,
51                                     true);
52  info.AddLatencyNumberWithTimestamp(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
53                                     0,
54                                     13,
55                                     base::TimeTicks::FromInternalValue(200),
56                                     3,
57                                     true);
58
59  EXPECT_EQ(info.latency_components.size(), 1u);
60  LatencyInfo::LatencyComponent component;
61  EXPECT_FALSE(
62      info.FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT, 0, &component));
63  EXPECT_FALSE(
64      info.FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 1, &component));
65  EXPECT_TRUE(
66      info.FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0, &component));
67  EXPECT_EQ(component.sequence_number, 30);
68  EXPECT_EQ(component.event_count, 5u);
69  EXPECT_EQ(component.event_time.ToInternalValue(), (100 * 2 + 200 * 3) / 5);
70}
71
72TEST(LatencyInfoTest, MergeTwoSeparateEvent) {
73  LatencyInfo info1;
74  LatencyInfo info2;
75  info1.AddLatencyNumberWithTimestamp(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT,
76                                      0,
77                                      1,
78                                      base::TimeTicks::FromInternalValue(100),
79                                      1,
80                                      true);
81  info2.AddLatencyNumberWithTimestamp(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
82                                      1,
83                                      5,
84                                      base::TimeTicks::FromInternalValue(1000),
85                                      2,
86                                      true);
87  info1.MergeWith(info2);
88
89  EXPECT_EQ(info1.latency_components.size(), 2u);
90  LatencyInfo::LatencyComponent component;
91  EXPECT_FALSE(
92      info1.FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT, 0, &component));
93  EXPECT_FALSE(info1.FindLatency(
94      INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT, 1, &component));
95  EXPECT_TRUE(info1.FindLatency(
96      INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT, 0, &component));
97  EXPECT_EQ(component.sequence_number, 1);
98  EXPECT_EQ(component.event_count, 1u);
99  EXPECT_EQ(component.event_time.ToInternalValue(), 100);
100  EXPECT_TRUE(
101      info1.FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 1, &component));
102  EXPECT_EQ(component.sequence_number, 5);
103  EXPECT_EQ(component.event_count, 2u);
104  EXPECT_EQ(component.event_time.ToInternalValue(), 1000);
105}
106
107TEST(LatencyInfoTest, MergeTwoSameEvent) {
108  LatencyInfo info1;
109  LatencyInfo info2;
110  info1.AddLatencyNumberWithTimestamp(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
111                                      0,
112                                      30,
113                                      base::TimeTicks::FromInternalValue(100),
114                                      2,
115                                      true);
116  info2.AddLatencyNumberWithTimestamp(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
117                                      0,
118                                      13,
119                                      base::TimeTicks::FromInternalValue(200),
120                                      3,
121                                      true);
122  info1.MergeWith(info2);
123
124  EXPECT_EQ(info1.latency_components.size(), 1u);
125  LatencyInfo::LatencyComponent component;
126  EXPECT_FALSE(
127      info1.FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT, 0, &component));
128  EXPECT_FALSE(
129      info1.FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 1, &component));
130  EXPECT_TRUE(
131      info1.FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0, &component));
132  EXPECT_EQ(component.sequence_number, 30);
133  EXPECT_EQ(component.event_count, 5u);
134  EXPECT_EQ(component.event_time.ToInternalValue(), (100 * 2 + 200 * 3) / 5);
135}
136
137TEST(LatencyInfoTest, ClearEvents) {
138  LatencyInfo info;
139  info.AddLatencyNumberWithTimestamp(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
140                                     0,
141                                     30,
142                                     base::TimeTicks::FromInternalValue(100),
143                                     2,
144                                     true);
145
146  EXPECT_EQ(info.latency_components.size(), 1u);
147  info.Clear();
148  EXPECT_EQ(info.latency_components.size(), 0u);
149}
150
151}  // namespace ui
152