• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     .screen_x_dpi = 0,
59     .screen_y_dpi = 0,
60     .orientation_minimum = -1,
61     .orientation_maximum = 2,
62     .max_finger_cnt = 2, .max_touch_cnt = 5,
63     .supports_t5r2 = 1, .support_semi_mt = 0, .is_button_pad = 0,
64     .has_wheel = 0, .wheel_is_hi_res = 0,
65     .is_haptic_pad = 0,
66   };
67 
68   TestInterpreterWrapper wrapper(&interpreter, &hwprops);
69   FingerState finger_state = {
70     // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
71     0, 0, 0, 0, 10, 0, 50, 50, 1, 0
72   };
73   HardwareState hardware_state = make_hwstate(200000, 0, 1, 1, &finger_state);
74   stime_t timeout = NO_DEADLINE;
75   wrapper.SyncInterpret(hardware_state, &timeout);
76   EXPECT_EQ(interpreter.log_->size(), 1);
77 
78   wrapper.SyncInterpret(hardware_state, &timeout);
79   EXPECT_EQ(interpreter.log_->size(), 2);
80 
81   // Assume the ResetLog property is set.
82   interpreter.logging_reset_.HandleGesturesPropWritten();
83   EXPECT_EQ(interpreter.log_->size(), 0);
84 
85   wrapper.SyncInterpret(hardware_state, &timeout);
86   EXPECT_EQ(interpreter.log_->size(), 1);
87 
88   std::string str = interpreter.EncodeActivityLog();
89   EXPECT_NE(0, str.size());
90 
91   // std::tmpnam is considered unsafe because another process could create the
92   // temporary file after time std::tmpnam returns the name but before the code
93   // actually opens it. Because this is just test code, we don't need to be
94   // concerned about such security holes here.
95 #pragma GCC diagnostic push
96 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
97   const char* filename = std::tmpnam(nullptr);
98 #pragma GCC diagnostic pop
99   ASSERT_NE(nullptr, filename) << "Couldn't generate a temporary file name";
100   interpreter.log_location_.SetValue(Json::Value(filename));
101   interpreter.IntWasWritten(&interpreter.logging_notify_);
102 
103   std::string read_str = "";
104   bool couldRead = ReadFileToString(filename, &read_str);
105 
106   EXPECT_TRUE(couldRead);
107   EXPECT_NE(0, read_str.size());
108 }
109 }  // namespace gestures
110