1 // Copyright 2012 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 <cstdio>
6 #include <string>
7
8 #include <gtest/gtest.h>
9
10 #include "include/activity_replay.h"
11 #include "include/file_util.h"
12 #include "include/gestures.h"
13 #include "include/interpreter.h"
14 #include "include/logging_filter_interpreter.h"
15 #include "include/prop_registry.h"
16 #include "include/unittest_util.h"
17 using std::string;
18
19 namespace gestures {
20
21 class LoggingFilterInterpreterTest : public ::testing::Test {};
22
23 class LoggingFilterInterpreterResetLogTestInterpreter : public Interpreter {
24 public:
LoggingFilterInterpreterResetLogTestInterpreter()25 LoggingFilterInterpreterResetLogTestInterpreter()
26 : Interpreter(nullptr, nullptr, false) {}
27 protected:
SyncInterpretImpl(HardwareState & hwstate,stime_t * timeout)28 virtual void SyncInterpretImpl(HardwareState& hwstate,
29 stime_t* timeout) {}
SetHardwarePropertiesImpl(const HardwareProperties & hw_props)30 virtual void SetHardwarePropertiesImpl(const HardwareProperties& hw_props) {
31 }
HandleTimerImpl(stime_t now,stime_t * timeout)32 virtual void HandleTimerImpl(stime_t now, stime_t* timeout) {}
33 };
34
TEST(LoggingFilterInterpreterTest,LogResetHandlerTest)35 TEST(LoggingFilterInterpreterTest, LogResetHandlerTest) {
36 PropRegistry prop_reg;
37 LoggingFilterInterpreterResetLogTestInterpreter* base_interpreter =
38 new LoggingFilterInterpreterResetLogTestInterpreter();
39 LoggingFilterInterpreter interpreter(&prop_reg, base_interpreter, nullptr);
40
41 interpreter.event_logging_enable_.SetValue(Json::Value(true));
42 interpreter.BoolWasWritten(&interpreter.event_logging_enable_);
43
44 using EventDebug = ActivityLog::EventDebug;
45 EXPECT_EQ(interpreter.enable_event_debug_logging_, 0);
46 interpreter.event_debug_logging_enable_.SetValue(
47 Json::Value((1 << static_cast<int>(EventDebug::Gesture)) |
48 (1 << static_cast<int>(EventDebug::HardwareState))));
49 interpreter.IntWasWritten(&interpreter.event_debug_logging_enable_);
50 EXPECT_EQ(interpreter.enable_event_debug_logging_,
51 (1 << static_cast<int>(EventDebug::Gesture)) |
52 (1 << static_cast<int>(EventDebug::HardwareState)));
53
54 HardwareProperties hwprops = {
55 .right = 100, .bottom = 100,
56 .res_x = 10,
57 .res_y = 10,
58 .orientation_minimum = -1,
59 .orientation_maximum = 2,
60 .max_finger_cnt = 2, .max_touch_cnt = 5,
61 .supports_t5r2 = 1, .support_semi_mt = 0, .is_button_pad = 0,
62 .has_wheel = 0, .wheel_is_hi_res = 0,
63 .is_haptic_pad = 0,
64 };
65
66 TestInterpreterWrapper wrapper(&interpreter, &hwprops);
67 FingerState finger_state = {
68 // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
69 0, 0, 0, 0, 10, 0, 50, 50, 1, 0
70 };
71 HardwareState hardware_state = make_hwstate(200000, 0, 1, 1, &finger_state);
72 stime_t timeout = NO_DEADLINE;
73 wrapper.SyncInterpret(hardware_state, &timeout);
74 EXPECT_EQ(interpreter.log_->size(), 1);
75
76 wrapper.SyncInterpret(hardware_state, &timeout);
77 EXPECT_EQ(interpreter.log_->size(), 2);
78
79 // Assume the ResetLog property is set.
80 interpreter.logging_reset_.HandleGesturesPropWritten();
81 EXPECT_EQ(interpreter.log_->size(), 0);
82
83 wrapper.SyncInterpret(hardware_state, &timeout);
84 EXPECT_EQ(interpreter.log_->size(), 1);
85
86 std::string str = interpreter.EncodeActivityLog();
87 EXPECT_NE(0, str.size());
88
89 // std::tmpnam is considered unsafe because another process could create the
90 // temporary file after time std::tmpnam returns the name but before the code
91 // actually opens it. Because this is just test code, we don't need to be
92 // concerned about such security holes here.
93 #pragma GCC diagnostic push
94 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
95 const char* filename = std::tmpnam(nullptr);
96 #pragma GCC diagnostic pop
97 ASSERT_NE(nullptr, filename) << "Couldn't generate a temporary file name";
98 interpreter.log_location_.SetValue(Json::Value(filename));
99 interpreter.IntWasWritten(&interpreter.logging_notify_);
100
101 std::string read_str = "";
102 bool couldRead = ReadFileToString(filename, &read_str);
103
104 EXPECT_TRUE(couldRead);
105 EXPECT_NE(0, read_str.size());
106 }
107 } // namespace gestures
108