diff options
author | mikhal@google.com <mikhal@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-05 22:53:13 +0000 |
---|---|---|
committer | mikhal@google.com <mikhal@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-05 22:53:13 +0000 |
commit | a0c69c8bab30a87e7e1c67089b2c55f3f6bef61b (patch) | |
tree | 1f474f89143e7bb154d3903274345e7e7889fa2f /media/cast | |
parent | 22b7b2cede575642c81042aab307a5afc6a88568 (diff) | |
download | chromium_src-a0c69c8bab30a87e7e1c67089b2c55f3f6bef61b.zip chromium_src-a0c69c8bab30a87e7e1c67089b2c55f3f6bef61b.tar.gz chromium_src-a0c69c8bab30a87e7e1c67089b2c55f3f6bef61b.tar.bz2 |
Adding test utility: input helper
A helper class to handle user inputs - will be used in the cast test apps - upcoming cl's.
Review URL: https://codereview.chromium.org/48773004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@233120 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/cast')
-rw-r--r-- | media/cast/test/utility/input_helper.cc | 64 | ||||
-rw-r--r-- | media/cast/test/utility/input_helper.h | 46 | ||||
-rw-r--r-- | media/cast/test/utility/utility.gyp | 19 |
3 files changed, 129 insertions, 0 deletions
diff --git a/media/cast/test/utility/input_helper.cc b/media/cast/test/utility/input_helper.cc new file mode 100644 index 0000000..3af5eb0 --- /dev/null +++ b/media/cast/test/utility/input_helper.cc @@ -0,0 +1,64 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "media/cast/test/utility/input_helper.h" + +#include <stdlib.h> +#include <cstdio> + +#include "base/logging.h" +#include "base/strings/string_number_conversions.h" + +namespace media { +namespace cast { +namespace test { + +InputBuilder::InputBuilder(const std::string& title, + const std::string& default_value, + int low_range, + int high_range) + : title_(title), + default_value_(default_value), + low_range_(low_range), + high_range_(high_range) {} + +InputBuilder::~InputBuilder() {} + +std::string InputBuilder::GetInput() const { + printf("\n%s\n", title_.c_str()); + if (!default_value_.empty()) + printf("Hit enter for default (%s):\n", default_value_.c_str()); + + printf("# "); + fflush(stdout); + char raw_input[128]; + if (!fgets(raw_input, 128, stdin)) { + NOTREACHED(); + return std::string(); + } + + std::string input = raw_input; + input = input.substr(0, input.size() - 1); // Strip last \n. + if (input.empty() && !default_value_.empty()) + return default_value_; + + if (!ValidateInput(input)) { + printf("Invalid input. Please try again.\n"); + return GetInput(); + } + return input; +} + +bool InputBuilder::ValidateInput(const std::string input) const { + // Check for a valid range. + if (low_range_ == INT_MIN && high_range_ == INT_MAX) return true; + int value; + if (!base::StringToInt(input, &value)) + return false; + return value >= low_range_ && value <= high_range_; +} + +} // namespace test +} // namespace cast +} // namespace media diff --git a/media/cast/test/utility/input_helper.h b/media/cast/test/utility/input_helper.h new file mode 100644 index 0000000..53add86 --- /dev/null +++ b/media/cast/test/utility/input_helper.h @@ -0,0 +1,46 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MEDIA_CAST_TEST_UTILITY_INPUT_HELPER_ +#define MEDIA_CAST_TEST_UTILITY_INPUT_HELPER_ + +#include <string> + +namespace media { +namespace cast { +namespace test { + +// This class handles general user input to the application. The user will be +// displayed with the title string and be given a default value. When forced +// a range, the input values should be within low_range to high_range. +// Setting low and high to INT_MIN/INT_MAX is equivalent to not setting a range. +class InputBuilder { + public: + InputBuilder(const std::string& title, + const std::string& default_value, + int low_range, + int high_range); + virtual ~InputBuilder(); + + // Ask the user for input, reads input from the input source and returns + // the answer. This method will keep asking the user until a correct answer + // is returned and is thereby guaranteed to return a response that is + // acceptable within the predefined range. + std::string GetInput() const; + + private: + bool ValidateInput(const std::string input) const; + + const std::string title_; + const std::string default_value_; + // Low and high range values for input validation. + const int low_range_; + const int high_range_; +}; + +} // namespace test +} // namespace cast +} // namespace media + +#endif // MEDIA_CAST_TEST_UTILITY_INPUT_HELPER_ diff --git a/media/cast/test/utility/utility.gyp b/media/cast/test/utility/utility.gyp new file mode 100644 index 0000000..fbe2e4d --- /dev/null +++ b/media/cast/test/utility/utility.gyp @@ -0,0 +1,19 @@ +# Copyright 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'targets': [ + { + 'target_name': 'cast_test_utility', + 'type': 'static_library', + 'include_dirs': [ + '<(DEPTH)/', + ], + 'sources': [ + 'input_helper.cc', + 'input_helper.', + ], # source + }, + ], +}
\ No newline at end of file |