xref: /aosp_15_r20/external/libchrome-gestures/src/activity_replay_unittest.cc (revision aed3e5085e770be5b69ce25295ecf6ddf906af95)
1*aed3e508SAndroid Build Coastguard Worker // Copyright 2012 The ChromiumOS Authors
2*aed3e508SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*aed3e508SAndroid Build Coastguard Worker // found in the LICENSE file.
4*aed3e508SAndroid Build Coastguard Worker 
5*aed3e508SAndroid Build Coastguard Worker #include <set>
6*aed3e508SAndroid Build Coastguard Worker #include <string>
7*aed3e508SAndroid Build Coastguard Worker #include <vector>
8*aed3e508SAndroid Build Coastguard Worker 
9*aed3e508SAndroid Build Coastguard Worker #include <gtest/gtest.h>
10*aed3e508SAndroid Build Coastguard Worker 
11*aed3e508SAndroid Build Coastguard Worker #include "include/activity_replay.h"
12*aed3e508SAndroid Build Coastguard Worker #include "include/command_line.h"
13*aed3e508SAndroid Build Coastguard Worker #include "include/file_util.h"
14*aed3e508SAndroid Build Coastguard Worker #include "include/finger_metrics.h"
15*aed3e508SAndroid Build Coastguard Worker #include "include/gestures.h"
16*aed3e508SAndroid Build Coastguard Worker #include "include/logging_filter_interpreter.h"
17*aed3e508SAndroid Build Coastguard Worker #include "include/string_util.h"
18*aed3e508SAndroid Build Coastguard Worker 
19*aed3e508SAndroid Build Coastguard Worker using std::string;
20*aed3e508SAndroid Build Coastguard Worker 
21*aed3e508SAndroid Build Coastguard Worker namespace gestures {
22*aed3e508SAndroid Build Coastguard Worker 
23*aed3e508SAndroid Build Coastguard Worker namespace {
24*aed3e508SAndroid Build Coastguard Worker 
25*aed3e508SAndroid Build Coastguard Worker template <typename STR>
SplitStringT(const STR & str,const typename STR::value_type s,bool trim_whitespace,std::vector<STR> * r)26*aed3e508SAndroid Build Coastguard Worker void SplitStringT(const STR& str,
27*aed3e508SAndroid Build Coastguard Worker                   const typename STR::value_type s,
28*aed3e508SAndroid Build Coastguard Worker                   bool trim_whitespace,
29*aed3e508SAndroid Build Coastguard Worker                   std::vector<STR>* r) {
30*aed3e508SAndroid Build Coastguard Worker   r->clear();
31*aed3e508SAndroid Build Coastguard Worker   size_t last = 0;
32*aed3e508SAndroid Build Coastguard Worker   size_t c = str.size();
33*aed3e508SAndroid Build Coastguard Worker   for (size_t i = 0; i <= c; ++i) {
34*aed3e508SAndroid Build Coastguard Worker     if (i == c || str[i] == s) {
35*aed3e508SAndroid Build Coastguard Worker       STR tmp(str, last, i - last);
36*aed3e508SAndroid Build Coastguard Worker       if (trim_whitespace)
37*aed3e508SAndroid Build Coastguard Worker         tmp = TrimWhitespaceASCII(tmp);
38*aed3e508SAndroid Build Coastguard Worker       // Avoid converting an empty or all-whitespace source string into a vector
39*aed3e508SAndroid Build Coastguard Worker       // of one empty string.
40*aed3e508SAndroid Build Coastguard Worker       if (i != c || !r->empty() || !tmp.empty())
41*aed3e508SAndroid Build Coastguard Worker         r->push_back(tmp);
42*aed3e508SAndroid Build Coastguard Worker       last = i + 1;
43*aed3e508SAndroid Build Coastguard Worker     }
44*aed3e508SAndroid Build Coastguard Worker   }
45*aed3e508SAndroid Build Coastguard Worker }
46*aed3e508SAndroid Build Coastguard Worker 
47*aed3e508SAndroid Build Coastguard Worker // |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which
48*aed3e508SAndroid Build Coastguard Worker // the trailing byte of a multi-byte character can be in the ASCII range.
49*aed3e508SAndroid Build Coastguard Worker // UTF-8, and other single/multi-byte ASCII-compatible encodings are OK.
50*aed3e508SAndroid Build Coastguard Worker // Note: |c| must be in the ASCII range.
SplitString(const std::string & str,char c,std::vector<std::string> * r)51*aed3e508SAndroid Build Coastguard Worker void SplitString(const std::string& str,
52*aed3e508SAndroid Build Coastguard Worker                  char c,
53*aed3e508SAndroid Build Coastguard Worker                  std::vector<std::string>* r) {
54*aed3e508SAndroid Build Coastguard Worker   SplitStringT(str, c, true, r);
55*aed3e508SAndroid Build Coastguard Worker }
56*aed3e508SAndroid Build Coastguard Worker 
57*aed3e508SAndroid Build Coastguard Worker }  // namespace
58*aed3e508SAndroid Build Coastguard Worker 
59*aed3e508SAndroid Build Coastguard Worker class ActivityReplayTest : public ::testing::Test {};
60*aed3e508SAndroid Build Coastguard Worker 
61*aed3e508SAndroid Build Coastguard Worker // This test reads a log file and replays it. This test should be enabled for a
62*aed3e508SAndroid Build Coastguard Worker // hands-on debugging session.
63*aed3e508SAndroid Build Coastguard Worker 
TEST(ActivityReplayTest,DISABLED_SimpleTest)64*aed3e508SAndroid Build Coastguard Worker TEST(ActivityReplayTest, DISABLED_SimpleTest) {
65*aed3e508SAndroid Build Coastguard Worker   CommandLine* cl = CommandLine::ForCurrentProcess();
66*aed3e508SAndroid Build Coastguard Worker   GestureInterpreter* c_interpreter = NewGestureInterpreter();
67*aed3e508SAndroid Build Coastguard Worker   c_interpreter->Initialize();
68*aed3e508SAndroid Build Coastguard Worker 
69*aed3e508SAndroid Build Coastguard Worker   Interpreter* interpreter = c_interpreter->interpreter();
70*aed3e508SAndroid Build Coastguard Worker   PropRegistry* prop_reg = c_interpreter->prop_reg();
71*aed3e508SAndroid Build Coastguard Worker   {
72*aed3e508SAndroid Build Coastguard Worker     MetricsProperties mprops(prop_reg);
73*aed3e508SAndroid Build Coastguard Worker 
74*aed3e508SAndroid Build Coastguard Worker     string log_contents;
75*aed3e508SAndroid Build Coastguard Worker     ASSERT_TRUE(ReadFileToString(cl->GetSwitchValueASCII("in").c_str(),
76*aed3e508SAndroid Build Coastguard Worker                                  &log_contents));
77*aed3e508SAndroid Build Coastguard Worker 
78*aed3e508SAndroid Build Coastguard Worker     ActivityReplay replay(prop_reg);
79*aed3e508SAndroid Build Coastguard Worker     std::vector<string> honor_props;
80*aed3e508SAndroid Build Coastguard Worker     if (cl->GetSwitchValueASCII("only_honor")[0])
81*aed3e508SAndroid Build Coastguard Worker       SplitString(cl->GetSwitchValueASCII("only_honor"),
82*aed3e508SAndroid Build Coastguard Worker                         ',', &honor_props);
83*aed3e508SAndroid Build Coastguard Worker     std::set<string> honor_props_set(honor_props.begin(), honor_props.end());
84*aed3e508SAndroid Build Coastguard Worker     replay.Parse(log_contents, honor_props_set);
85*aed3e508SAndroid Build Coastguard Worker     replay.Replay(interpreter, &mprops);
86*aed3e508SAndroid Build Coastguard Worker 
87*aed3e508SAndroid Build Coastguard Worker     // Dump the new log
88*aed3e508SAndroid Build Coastguard Worker     const string kOutSwitchName = "outfile";
89*aed3e508SAndroid Build Coastguard Worker     if (cl->HasSwitch(kOutSwitchName))
90*aed3e508SAndroid Build Coastguard Worker       static_cast<LoggingFilterInterpreter*>(interpreter)->Dump(
91*aed3e508SAndroid Build Coastguard Worker           cl->GetSwitchValueASCII(kOutSwitchName).c_str());
92*aed3e508SAndroid Build Coastguard Worker   }
93*aed3e508SAndroid Build Coastguard Worker 
94*aed3e508SAndroid Build Coastguard Worker   DeleteGestureInterpreter(c_interpreter);
95*aed3e508SAndroid Build Coastguard Worker }
96*aed3e508SAndroid Build Coastguard Worker 
97*aed3e508SAndroid Build Coastguard Worker }  // namespace gestures
98