• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_VIDEO_ENGINE_TEST_AUTO_TEST_PRIMITIVES_
12 #define WEBRTC_VIDEO_ENGINE_TEST_AUTO_TEST_PRIMITIVES_
13 
14 #include <stdlib.h>
15 
16 #include <map>
17 #include <string>
18 #include <vector>
19 
20 #include "gflags/gflags.h"
21 
22 namespace webrtc {
23 
24 class InputValidator;
25 class OverrideRegistry;
26 
27 // This class handles general user input to the application.
28 class InputBuilder {
29  public:
30   // The input builder takes ownership of the validator (but not the
31   // override registry).
32   InputBuilder(const std::string& title,
33                const InputValidator* input_validator,
34                const OverrideRegistry& override_registry);
35   ~InputBuilder();
36 
37   // Ask the user for input, reads input from the input source and returns
38   // the answer. This method will keep asking the user until a correct answer
39   // is returned and is thereby guaranteed to return a response that is
40   // acceptable to the input validator.
41   //
42   // In some cases we will not actually ask the user for input, for instance
43   // if the --choose-defaults or --override flags are specified. See the
44   // definition of those flags in the .cc file for more information.
45   std::string AskForInput() const;
46 
47   // Replaces the input source where we ask for input. Default is stdin.
48   InputBuilder& WithInputSource(FILE* input_source);
49   // Sets the input validator. The input builder takes ownership. If a default
50   // value has been set, it must be acceptable to this validator.
51   InputBuilder& WithInputValidator(const InputValidator* input_validator);
52   // Sets a default value if the user doesn't want to give input. This value
53   // must be acceptable to the input validator.
54   InputBuilder& WithDefault(const std::string& default_value);
55   // Prints additional info after the title.
56   InputBuilder& WithAdditionalInfo(const std::string& title);
57 
58  private:
59   const std::string& GetOverride() const;
60   std::string ActuallyAskUser() const;
61 
62   FILE* input_source_;
63   const InputValidator* input_validator_;
64   const OverrideRegistry& override_registry_;
65   std::string default_value_;
66   std::string title_;
67   std::string additional_info_;
68 };
69 
70 // Keeps track of overrides for any input points. Overrides are passed in the
71 // format Title 1=Value 1,Title 2=Value 2. Spaces are not trimmed anywhere.
72 class OverrideRegistry {
73  public:
74   OverrideRegistry(const std::string& overrides);
75   bool HasOverrideFor(const std::string& title) const;
76   const std::string& GetOverrideFor(const std::string& title) const;
77  private:
78   typedef std::map<std::string, std::string> OverrideMap;
79   OverrideMap overrides_;
80 };
81 
82 class InputValidator {
83  public:
~InputValidator()84   virtual ~InputValidator() {}
85 
86   virtual bool InputOk(const std::string& value) const = 0;
87 };
88 
89 // Ensures input is an integer between low and high (inclusive).
90 class IntegerWithinRangeValidator : public InputValidator {
91  public:
IntegerWithinRangeValidator(int low,int high)92   IntegerWithinRangeValidator(int low, int high)
93       : low_(low), high_(high) {}
94 
InputOk(const std::string & input)95   bool InputOk(const std::string& input) const {
96     int value = atoi(input.c_str());
97     // Note: atoi returns 0 on failure.
98     if (value == 0 && input.length() > 0 && input[0] != '0')
99       return false;  // Probably bad input.
100     return value >= low_ && value <= high_;
101   }
102 
103  private:
104   int low_;
105   int high_;
106 };
107 
108 std::vector<std::string> Split(const std::string& to_split,
109                                const std::string& delimiter);
110 
111 // Convenience method for creating an input builder.
112 InputBuilder TypedInput(const std::string& title);
113 
114 }  // namespace webrtc
115 
116 #endif  // WEBRTC_VIDEO_ENGINE_TEST_AUTO_TEST_PRIMITIVES_
117