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_CHOICE_HELPERS_H_ 12 #define WEBRTC_VIDEO_ENGINE_TEST_AUTO_TEST_PRIMITIVES_CHOICE_HELPERS_H_ 13 14 #include <string> 15 #include <vector> 16 17 #include "webrtc/video_engine/test/auto_test/primitives/input_helpers.h" 18 19 namespace webrtc { 20 21 typedef std::vector<std::string> Choices; 22 23 /** 24 * Used to ask the user to make a choice. This class will allow you to 25 * configure how to ask the question, and then ask it. For instance, 26 * 27 * int choice = FromChoices("Choice 1\n" 28 * "Choice 2\n").WithDefault("Choice 1").Choose(); 29 * 30 * will print a menu presenting the two choices and ask for input. The user, 31 * can input 1, 2 or just hit enter since we specified a default in this case. 32 * The Choose call will block until the user gives valid input one way or the 33 * other. The choice variable is guaranteed to contain either 1 or 2 after 34 * this particular call. 35 * 36 * The class uses stdout and stdin by default, but stdin can be replaced using 37 * WithInputSource for unit tests. 38 */ 39 class ChoiceBuilder { 40 public: 41 explicit ChoiceBuilder(const std::string& title, const Choices& choices); 42 43 // Specifies the choice as the default. The choice must be one of the choices 44 // passed in the constructor. If this method is not called, the user has to 45 // choose an option explicitly. 46 ChoiceBuilder& WithDefault(const std::string& default_choice); 47 48 // Replaces the input source where we ask for input. Default is stdin. 49 ChoiceBuilder& WithInputSource(FILE* input_source); 50 51 // Prints the choice list and requests input from the input source. Returns 52 // the choice number (choices start at 1). 53 int Choose(); 54 private: 55 std::string MakeHumanReadableOptions(); 56 57 Choices choices_; 58 InputBuilder input_helper_; 59 }; 60 61 // Convenience function that creates a choice builder given a string where 62 // choices are separated by \n. 63 ChoiceBuilder FromChoices(const std::string& title, 64 const std::string& raw_choices); 65 66 // Creates choices from a string where choices are separated by \n. 67 Choices SplitChoices(const std::string& raw_choices); 68 69 } // namespace webrtc 70 71 #endif // CHOICE_HELPERS_H_ 72