1 // Copyright 2011 The ChromiumOS Authors
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 <string>
6
7 #include <gtest/gtest.h>
8
9 #include "include/activity_log.h"
10 #include "include/macros.h"
11 #include "include/prop_registry.h"
12 #include "include/unittest_util.h"
13
14 using std::string;
15
16 namespace gestures {
17
18 class ActivityLogTest : public ::testing::Test {};
19
TEST(ActivityLogTest,SimpleTest)20 TEST(ActivityLogTest, SimpleTest) {
21 PropRegistry prop_reg;
22 BoolProperty true_prop(&prop_reg, "true prop", true);
23 BoolProperty false_prop(&prop_reg, "false prop", false);
24 DoubleProperty double_prop(&prop_reg, "double prop", 77.25);
25 IntProperty int_prop(&prop_reg, "int prop", -816);
26 StringProperty string_prop(&prop_reg, "string prop", "foobarstr");
27
28 ActivityLog log(&prop_reg);
29 EXPECT_TRUE(strstr(log.Encode().c_str(), "true"));
30 EXPECT_TRUE(strstr(log.Encode().c_str(), "false"));
31 EXPECT_TRUE(strstr(log.Encode().c_str(), "77.25"));
32 EXPECT_TRUE(strstr(log.Encode().c_str(), "-816"));
33 EXPECT_TRUE(strstr(log.Encode().c_str(), "foobarstr"));
34
35 HardwareProperties hwprops = {
36 6011, // left edge
37 6012, // top edge
38 6013, // right edge
39 6014, // bottom edge
40 6015, // x pixels/TP width
41 6016, // y pixels/TP height
42 6017, // x screen DPI
43 6018, // y screen DPI
44 6019, // orientation minimum
45 6020, // orientation maximum
46 6021, // max fingers
47 6022, // max touch
48 1, // t5r2
49 0, // semi-mt
50 1, // is button pad,
51 0, // has wheel
52 0, // vertical wheel is high resolution
53 0, // is haptic pad
54 };
55
56 log.SetHardwareProperties(hwprops);
57
58 const char* expected_strings[] = {
59 "6011", "6012", "6013", "6014", "6015", "6016",
60 "6017", "6018", "6019", "6020", "6021", "6022"
61 };
62 string hwprops_log = log.Encode();
63 for (size_t i = 0; i < arraysize(expected_strings); i++)
64 EXPECT_TRUE(strstr(hwprops_log.c_str(), expected_strings[i]));
65
66 EXPECT_EQ(0, log.size());
67 EXPECT_GT(log.MaxSize(), 10);
68
69 FingerState fs = { 0.0, 0.0, 0.0, 0.0, 9.0, 0.0, 3.0, 4.0, 22, 0 };
70 HardwareState hs = make_hwstate(1.0, 0, 1, 1, &fs);
71 log.LogHardwareState(hs);
72 EXPECT_EQ(1, log.size());
73 EXPECT_TRUE(strstr(log.Encode().c_str(), "22"));
74 ActivityLog::Entry* entry = log.GetEntry(0);
75 EXPECT_EQ(ActivityLog::kHardwareState, entry->type);
76
77 log.LogTimerCallback(234.5);
78 EXPECT_EQ(2, log.size());
79 EXPECT_TRUE(strstr(log.Encode().c_str(), "234.5"));
80 entry = log.GetEntry(1);
81 EXPECT_EQ(ActivityLog::kTimerCallback, entry->type);
82
83 log.LogCallbackRequest(90210);
84 EXPECT_EQ(3, log.size());
85 EXPECT_TRUE(strstr(log.Encode().c_str(), "90210"));
86 entry = log.GetEntry(2);
87 EXPECT_EQ(ActivityLog::kCallbackRequest, entry->type);
88
89 Gesture null;
90 Gesture move(kGestureMove, 1.0, 2.0, 773, 4.0);
91 Gesture scroll(kGestureScroll, 1.0, 2.0, 312, 4.0);
92 Gesture buttons(kGestureButtonsChange, 1.0, 847, 3, 4, false);
93 Gesture contact_initiated;
94 contact_initiated.type = kGestureTypeContactInitiated;
95 Gesture mousewheel(kGestureMouseWheel, 1.0, 2.0, 30.0, 40.0, 3, 4);
96 Gesture pinch(kGesturePinch, 1.0, 2.0, 3.0, 4.0);
97 Gesture fling(kGestureFling, 1.0, 2.0, 42.0, 24.0, 1);
98 Gesture swipe(kGestureSwipe, 1.0, 2.0, 128.0, 4.0);
99 Gesture swipelift(kGestureSwipeLift, 1.0, 2.0);
100 Gesture swipe4f(kGestureFourFingerSwipe, 1.0, 2.0, 256.0, 4.0);
101 Gesture swipe4flift(kGestureFourFingerSwipeLift, 1.0, 2.0);
102 Gesture metrics(kGestureMetrics, 1.0, 2.0,
103 kGestureMetricsTypeMouseMovement, 3.0, 4.0);
104
105 Gesture* gs[] = {
106 &null, &move, &scroll, &buttons, &contact_initiated,
107 &mousewheel, &pinch, &fling, &swipe, &swipelift,
108 &swipe4f, &swipe4flift, &metrics
109 };
110 const char* test_strs[] = {
111 "null", "773", "312", "847", "nitiated",
112 "30", "3", "42", "128", "null",
113 "256", "null", "1"
114 };
115
116 ASSERT_EQ(arraysize(gs), arraysize(test_strs));
117 for (size_t i = 0; i < arraysize(gs); ++i) {
118 log.LogGesture(*gs[i]);
119 EXPECT_TRUE(strstr(log.Encode().c_str(), test_strs[i])) << "i=" << i;
120 entry = log.GetEntry(log.size() - 1);
121 EXPECT_EQ(ActivityLog::kGesture, entry->type) << "i=" << i;
122 }
123
124 log.Clear();
125 EXPECT_EQ(0, log.size());
126 }
127
TEST(ActivityLogTest,WrapAroundTest)128 TEST(ActivityLogTest, WrapAroundTest) {
129 ActivityLog log(NULL);
130 // overfill the buffer
131 const size_t fill_size = (ActivityLog::kBufferSize * 3) / 2;
132 for (size_t i = 0; i < fill_size; i++)
133 log.LogCallbackRequest(static_cast<stime_t>(i));
134 const string::size_type prefix_length = 100;
135 string first_prefix = log.Encode().substr(0, prefix_length);
136 log.LogCallbackRequest(static_cast<stime_t>(fill_size));
137 string second_prefix = log.Encode().substr(0, prefix_length);
138 EXPECT_NE(first_prefix, second_prefix);
139 }
140
TEST(ActivityLogTest,VersionTest)141 TEST(ActivityLogTest, VersionTest) {
142 ActivityLog log(NULL);
143 string thelog = log.Encode();
144 EXPECT_TRUE(thelog.find(VCSID) != string::npos);
145 }
146
147 } // namespace gestures
148