1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
113f45c2e0ac4cb280f941efa3a3476895795e3dd6pbos@webrtc.org#include <stdio.h>
12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
13281cff8cd679728fe395f7f0203c05e763c0c789pbos@webrtc.org#include "testing/gtest/include/gtest/gtest.h"
14281cff8cd679728fe395f7f0203c05e763c0c789pbos@webrtc.org#include "webrtc/video_engine/test/auto_test/primitives/choice_helpers.h"
15281cff8cd679728fe395f7f0203c05e763c0c789pbos@webrtc.org#include "webrtc/video_engine/test/auto_test/primitives/fake_stdin.h"
16b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
17b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgnamespace webrtc {
18b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
19b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgclass ChoiceHelpersTest : public testing::Test {
20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
21b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
22b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(ChoiceHelpersTest, SplitReturnsEmptyChoicesForEmptyInput) {
23b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_TRUE(SplitChoices("").empty());
24b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
26b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(ChoiceHelpersTest, SplitHandlesSingleChoice) {
27b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  Choices choices = SplitChoices("Single Choice");
28b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ(1u, choices.size());
29b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("Single Choice", choices[0]);
30b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
31b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
32b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(ChoiceHelpersTest, SplitHandlesSingleChoiceWithEndingNewline) {
33b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  Choices choices = SplitChoices("Single Choice\n");
34b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ(1u, choices.size());
35b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("Single Choice", choices[0]);
36b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
37b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
38b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(ChoiceHelpersTest, SplitHandlesMultipleChoices) {
39b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  Choices choices = SplitChoices(
40b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "Choice 1\n"
41b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "Choice 2\n"
42b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "Choice 3");
43b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ(3u, choices.size());
44b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("Choice 1", choices[0]);
45b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("Choice 2", choices[1]);
46b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("Choice 3", choices[2]);
47b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
48b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
49b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(ChoiceHelpersTest, SplitHandlesMultipleChoicesWithEndingNewline) {
50b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  Choices choices = SplitChoices(
51b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "Choice 1\n"
52b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "Choice 2\n"
53b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "Choice 3\n");
54b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ(3u, choices.size());
55b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("Choice 1", choices[0]);
56b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("Choice 2", choices[1]);
57b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("Choice 3", choices[2]);
58b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
59b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
60b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(ChoiceHelpersTest, CanSelectUsingChoiceBuilder) {
61b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  FILE* fake_stdin = FakeStdin("1\n2\n");
628969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_EQ(1, FromChoices("Title",
638969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org                           "Choice 1\n"
64b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                           "Choice 2").WithInputSource(fake_stdin).Choose());
658969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_EQ(2, FromChoices("","Choice 1\n"
66b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                           "Choice 2").WithInputSource(fake_stdin).Choose());
67b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  fclose(fake_stdin);
68b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
69b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
70b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(ChoiceHelpersTest, RetriesIfGivenInvalidChoice) {
71b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  FILE* fake_stdin = FakeStdin("3\n0\n99\n23409234809\na\nwhatever\n1\n");
728969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_EQ(1, FromChoices("Title",
738969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org                           "Choice 1\n"
74b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                           "Choice 2").WithInputSource(fake_stdin).Choose());
75b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  fclose(fake_stdin);
76b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
77b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
78b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(ChoiceHelpersTest, RetriesOnEnterIfNoDefaultSet) {
79b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  FILE* fake_stdin = FakeStdin("\n2\n");
808969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_EQ(2, FromChoices("Title",
818969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org                           "Choice 1\n"
82b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                           "Choice 2").WithInputSource(fake_stdin).Choose());
83b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  fclose(fake_stdin);
84b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
85b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
86b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(ChoiceHelpersTest, PicksDefaultOnEnterIfDefaultSet) {
87b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  FILE* fake_stdin = FakeStdin("\n");
888969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_EQ(2, FromChoices("Title",
898969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org                           "Choice 1\n"
90b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                           "Choice 2").WithInputSource(fake_stdin)
918969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org                               .WithDefault("Choice 2").Choose());
92b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  fclose(fake_stdin);
93b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
94b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
95b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}  // namespace webrtc
96