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
11281cff8cd679728fe395f7f0203c05e763c0c789pbos@webrtc.org#include "testing/gtest/include/gtest/gtest.h"
12281cff8cd679728fe395f7f0203c05e763c0c789pbos@webrtc.org#include "webrtc/video_engine/test/auto_test/primitives/fake_stdin.h"
13281cff8cd679728fe395f7f0203c05e763c0c789pbos@webrtc.org#include "webrtc/video_engine/test/auto_test/primitives/input_helpers.h"
14b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
15b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgnamespace webrtc {
16b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
17b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgclass InputHelpersTest: public testing::Test {
18b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
19b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(InputHelpersTest, AcceptsAnyInputExceptEmptyByDefault) {
21b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  FILE* fake_stdin = FakeStdin("\n\nWhatever\n");
228969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  std::string result = TypedInput("Title")
238969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org      .WithInputSource(fake_stdin).AskForInput();
24b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("Whatever", result);
25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  fclose(fake_stdin);
26b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
27b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
28b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(InputHelpersTest, ReturnsDefaultOnEmptyInputIfDefaultSet) {
29b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  FILE* fake_stdin = FakeStdin("\n\nWhatever\n");
308969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  std::string result = TypedInput("Title")
31b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      .WithInputSource(fake_stdin)
32b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      .WithDefault("MyDefault")
33b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      .AskForInput();
34b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("MyDefault", result);
35b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  fclose(fake_stdin);
36b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
37b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
38b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTEST_F(InputHelpersTest, ObeysInputValidator) {
39b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  class ValidatorWhichOnlyAcceptsFooBar : public InputValidator {
40b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org   public:
41b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    bool InputOk(const std::string& input) const {
42b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      return input == "FooBar";
43b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
44b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  };
45b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  FILE* fake_stdin = FakeStdin("\nFoo\nBar\nFoo Bar\nFooBar\n");
468969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  std::string result = TypedInput("Title")
47b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      .WithInputSource(fake_stdin)
48b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      .WithInputValidator(new ValidatorWhichOnlyAcceptsFooBar())
49b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      .AskForInput();
50b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  EXPECT_EQ("FooBar", result);
51b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  fclose(fake_stdin);
52b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
538969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org
548969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.orgTEST_F(InputHelpersTest, OverrideRegistryParsesOverridesCorrectly) {
558969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  // TODO(phoglund): Ignore spaces where appropriate
568969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  OverrideRegistry override_registry("My Title=Value,My Choice=1");
578969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_TRUE(override_registry.HasOverrideFor("My Title"));
588969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_EQ("Value", override_registry.GetOverrideFor("My Title"));
598969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_TRUE(override_registry.HasOverrideFor("My Choice"));
608969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_EQ("1", override_registry.GetOverrideFor("My Choice"));
618969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_FALSE(override_registry.HasOverrideFor("Not Overridden"));
628969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org}
638969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org
648969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.orgTEST_F(InputHelpersTest, ObeysOverridesBeforeAnythingElse) {
658969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  class CarelessValidator : public InputValidator {
668969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  public:
678969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org    bool InputOk(const std::string& input) const {
688969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org      return true;
698969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org    }
708969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  };
718969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  FILE* fake_stdin = FakeStdin("\nFoo\nBar\nFoo Bar\nFooBar\n");
728969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  OverrideRegistry override_registry("My Title=Value,My Choice=1");
738969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  EXPECT_EQ("Value", InputBuilder("My Title",
748969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org      new CarelessValidator(), override_registry)
758969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org          .WithDefault("Whatever")
768969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org          .WithInputSource(fake_stdin).AskForInput());
778969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org  fclose(fake_stdin);
788969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org}
798969e9378a0ee22cf5601f7eaac5dd1b9546977cphoglund@webrtc.org
80b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
81