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