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#include "webrtc/video_engine/test/auto_test/interface/vie_autotest_main.h"
12
13#include "gflags/gflags.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16#include "webrtc/test/field_trial.h"
17#include "webrtc/test/testsupport/fileutils.h"
18#include "webrtc/video_engine/test/auto_test/interface/vie_autotest.h"
19#include "webrtc/video_engine/test/auto_test/interface/vie_autotest_window_manager_interface.h"
20#include "webrtc/video_engine/test/auto_test/interface/vie_window_creator.h"
21
22DEFINE_bool(automated, false, "Run Video engine tests in noninteractive mode.");
23DEFINE_bool(auto_custom_call, false, "Run custom call directly.");
24DEFINE_string(force_fieldtrials, "",
25    "Field trials control experimental feature code which can be forced. "
26    "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
27    " will assign the group Enable to field trial WebRTC-FooFeature.");
28
29static const std::string kStandardTest = "ViEStandardIntegrationTest";
30static const std::string kExtendedTest = "ViEExtendedIntegrationTest";
31static const std::string kApiTest = "ViEApiIntegrationTest";
32
33ViEAutoTestMain::ViEAutoTestMain() {
34  index_to_test_method_map_[1] = "RunsBaseTestWithoutErrors";
35  index_to_test_method_map_[2] = "RunsCaptureTestWithoutErrors";
36  index_to_test_method_map_[3] = "RunsCodecTestWithoutErrors";
37  index_to_test_method_map_[4] = "[unused]";
38  index_to_test_method_map_[5] = "RunsImageProcessTestWithoutErrors";
39  index_to_test_method_map_[6] = "RunsNetworkTestWithoutErrors";
40  index_to_test_method_map_[7] = "RunsRenderTestWithoutErrors";
41  index_to_test_method_map_[8] = "RunsRtpRtcpTestWithoutErrors";
42}
43
44int ViEAutoTestMain::RunTests(int argc, char** argv) {
45  // Initialize logging.
46  ViETest::Init();
47  // Initialize WebRTC testing framework so paths to resources can be resolved.
48  webrtc::test::SetExecutablePath(argv[0]);
49  // Initialize the testing framework.
50  testing::InitGoogleTest(&argc, argv);
51  // AllowCommandLineParsing allows us to ignore flags passed on to us by
52  // Chromium build bots without having to explicitly disable them.
53  google::AllowCommandLineReparsing();
54  // Parse remaining flags:
55  google::ParseCommandLineFlags(&argc, &argv, true);
56  // Initialize field trial
57  webrtc::test::InitFieldTrialsFromString(FLAGS_force_fieldtrials);
58
59  int result;
60  if (FLAGS_automated) {
61    // Run in automated mode.
62#if defined(WEBRTC_LINUX)
63    // All window-related tests are disabled on Linux for now.
64    // See https://code.google.com/p/chromium/issues/detail?id=318760
65    return 0;
66#endif
67    result = RUN_ALL_TESTS();
68  } else if (FLAGS_auto_custom_call) {
69    // Run automated custom call.
70    result = RunSpecialTestCase(8);
71  } else {
72    // Run in interactive mode.
73    result = RunInteractiveMode();
74  }
75
76  ViETest::Terminate();
77  return result;
78}
79
80int ViEAutoTestMain::AskUserForTestCase() {
81  int choice;
82  std::string answer;
83
84  do {
85    ViETest::Log("\nSpecific tests:");
86    ViETest::Log("\t 0. Go back to previous menu.");
87
88    // Print all test method choices. Assumes that map sorts on its key.
89    int last_valid_choice = 0;
90    std::map<int, std::string>::const_iterator iterator;
91    for (iterator = index_to_test_method_map_.begin();
92        iterator != index_to_test_method_map_.end();
93        ++iterator) {
94      ViETest::Log("\t %d. %s", iterator->first, iterator->second.c_str());
95      last_valid_choice = iterator->first;
96    }
97
98    ViETest::Log("Choose specific test:");
99    choice = AskUserForNumber(0, last_valid_choice);
100  } while (choice == kInvalidChoice);
101
102  return choice;
103}
104
105int ViEAutoTestMain::AskUserForNumber(int min_allowed, int max_allowed) {
106  int result;
107  if (scanf("%d", &result) <= 0) {
108    ViETest::Log("\nPlease enter a number instead, then hit enter.");
109    getchar();
110    return kInvalidChoice;
111  }
112  getchar();  // Consume enter key.
113
114  if (result < min_allowed || result > max_allowed) {
115    ViETest::Log("%d-%d are valid choices. Please try again.", min_allowed,
116                 max_allowed);
117    return kInvalidChoice;
118  }
119
120  return result;
121}
122
123int ViEAutoTestMain::RunTestMatching(const std::string test_case,
124                                     const std::string test_method) {
125  testing::FLAGS_gtest_filter = test_case + "." + test_method;
126  return RUN_ALL_TESTS();
127}
128
129int ViEAutoTestMain::RunSpecificTestCaseIn(const std::string test_case_name)
130{
131  // If user says 0, it means don't run anything.
132  int specific_choice = AskUserForTestCase();
133  if (specific_choice != 0){
134    return RunTestMatching(test_case_name,
135                           index_to_test_method_map_[specific_choice]);
136  }
137  return 0;
138}
139
140int ViEAutoTestMain::RunSpecialTestCase(int choice) {
141  // 7-10 don't run in GTest and need to initialize by themselves.
142  assert(choice >= 7 && choice <= 10);
143
144  // Create the windows
145  ViEWindowCreator windowCreator;
146  ViEAutoTestWindowManagerInterface* windowManager =
147      windowCreator.CreateTwoWindows();
148
149  // Create the test cases
150  ViEAutoTest vieAutoTest(windowManager->GetWindow1(),
151                          windowManager->GetWindow2());
152
153  int errors = 0;
154  switch (choice) {
155    case 7: errors = vieAutoTest.ViELoopbackCall();  break;
156    case 8: errors = vieAutoTest.ViECustomCall();    break;
157    case 9: errors = vieAutoTest.ViESimulcastCall(); break;
158    case 10: errors = vieAutoTest.ViERecordCall();   break;
159  }
160
161  windowCreator.TerminateWindows();
162  return errors;
163}
164
165int ViEAutoTestMain::RunInteractiveMode() {
166  ViETest::Log(" ============================== ");
167  ViETest::Log("    WebRTC ViE 3.x Autotest     ");
168  ViETest::Log(" ============================== \n");
169
170  int choice = 0;
171  int errors = 0;
172  do {
173    ViETest::Log("Test types: ");
174    ViETest::Log("\t 0. Quit");
175    ViETest::Log("\t 1. All standard tests (delivery test)");
176    ViETest::Log("\t 2. All API tests");
177    ViETest::Log("\t 3. All extended test");
178    ViETest::Log("\t 4. Specific standard test");
179    ViETest::Log("\t 5. Specific API test");
180    ViETest::Log("\t 6. Specific extended test");
181    ViETest::Log("\t 7. Simple loopback call");
182    ViETest::Log("\t 8. Custom configure a call");
183    ViETest::Log("\t 9. Simulcast in loopback");
184    ViETest::Log("\t 10. Record");
185    ViETest::Log("Select type of test:");
186
187    choice = AskUserForNumber(0, 10);
188    if (choice == kInvalidChoice) {
189      continue;
190    }
191    switch (choice) {
192      case 0:                                                 break;
193      case 1:  errors = RunTestMatching(kStandardTest, "*");  break;
194      case 2:  errors = RunTestMatching(kApiTest,      "*");  break;
195      case 3:  errors = RunTestMatching(kExtendedTest, "*");  break;
196      case 4:  errors = RunSpecificTestCaseIn(kStandardTest); break;
197      case 5:  errors = RunSpecificTestCaseIn(kApiTest);      break;
198      case 6:  errors = RunSpecificTestCaseIn(kExtendedTest); break;
199      default: errors = RunSpecialTestCase(choice);           break;
200    }
201  } while (choice != 0);
202
203  if (errors) {
204    ViETest::Log("Test done with errors, see ViEAutotestLog.txt for test "
205        "result.\n");
206    return 1;
207  } else {
208    ViETest::Log("Test done without errors, see ViEAutotestLog.txt for "
209        "test result.\n");
210    return 0;
211  }
212}
213