1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest_environment_test.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,192 @@ 1.4 +// Copyright 2007, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// Author: wan@google.com (Zhanyong Wan) 1.34 +// 1.35 +// Tests using global test environments. 1.36 + 1.37 +#include <stdlib.h> 1.38 +#include <stdio.h> 1.39 +#include "gtest/gtest.h" 1.40 + 1.41 +#define GTEST_IMPLEMENTATION_ 1 // Required for the next #include. 1.42 +#include "src/gtest-internal-inl.h" 1.43 +#undef GTEST_IMPLEMENTATION_ 1.44 + 1.45 +namespace testing { 1.46 +GTEST_DECLARE_string_(filter); 1.47 +} 1.48 + 1.49 +namespace { 1.50 + 1.51 +enum FailureType { 1.52 + NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE 1.53 +}; 1.54 + 1.55 +// For testing using global test environments. 1.56 +class MyEnvironment : public testing::Environment { 1.57 + public: 1.58 + MyEnvironment() { Reset(); } 1.59 + 1.60 + // Depending on the value of failure_in_set_up_, SetUp() will 1.61 + // generate a non-fatal failure, generate a fatal failure, or 1.62 + // succeed. 1.63 + virtual void SetUp() { 1.64 + set_up_was_run_ = true; 1.65 + 1.66 + switch (failure_in_set_up_) { 1.67 + case NON_FATAL_FAILURE: 1.68 + ADD_FAILURE() << "Expected non-fatal failure in global set-up."; 1.69 + break; 1.70 + case FATAL_FAILURE: 1.71 + FAIL() << "Expected fatal failure in global set-up."; 1.72 + break; 1.73 + default: 1.74 + break; 1.75 + } 1.76 + } 1.77 + 1.78 + // Generates a non-fatal failure. 1.79 + virtual void TearDown() { 1.80 + tear_down_was_run_ = true; 1.81 + ADD_FAILURE() << "Expected non-fatal failure in global tear-down."; 1.82 + } 1.83 + 1.84 + // Resets the state of the environment s.t. it can be reused. 1.85 + void Reset() { 1.86 + failure_in_set_up_ = NO_FAILURE; 1.87 + set_up_was_run_ = false; 1.88 + tear_down_was_run_ = false; 1.89 + } 1.90 + 1.91 + // We call this function to set the type of failure SetUp() should 1.92 + // generate. 1.93 + void set_failure_in_set_up(FailureType type) { 1.94 + failure_in_set_up_ = type; 1.95 + } 1.96 + 1.97 + // Was SetUp() run? 1.98 + bool set_up_was_run() const { return set_up_was_run_; } 1.99 + 1.100 + // Was TearDown() run? 1.101 + bool tear_down_was_run() const { return tear_down_was_run_; } 1.102 + 1.103 + private: 1.104 + FailureType failure_in_set_up_; 1.105 + bool set_up_was_run_; 1.106 + bool tear_down_was_run_; 1.107 +}; 1.108 + 1.109 +// Was the TEST run? 1.110 +bool test_was_run; 1.111 + 1.112 +// The sole purpose of this TEST is to enable us to check whether it 1.113 +// was run. 1.114 +TEST(FooTest, Bar) { 1.115 + test_was_run = true; 1.116 +} 1.117 + 1.118 +// Prints the message and aborts the program if condition is false. 1.119 +void Check(bool condition, const char* msg) { 1.120 + if (!condition) { 1.121 + printf("FAILED: %s\n", msg); 1.122 + testing::internal::posix::Abort(); 1.123 + } 1.124 +} 1.125 + 1.126 +// Runs the tests. Return true iff successful. 1.127 +// 1.128 +// The 'failure' parameter specifies the type of failure that should 1.129 +// be generated by the global set-up. 1.130 +int RunAllTests(MyEnvironment* env, FailureType failure) { 1.131 + env->Reset(); 1.132 + env->set_failure_in_set_up(failure); 1.133 + test_was_run = false; 1.134 + testing::internal::GetUnitTestImpl()->ClearAdHocTestResult(); 1.135 + return RUN_ALL_TESTS(); 1.136 +} 1.137 + 1.138 +} // namespace 1.139 + 1.140 +int main(int argc, char **argv) { 1.141 + testing::InitGoogleTest(&argc, argv); 1.142 + 1.143 + // Registers a global test environment, and verifies that the 1.144 + // registration function returns its argument. 1.145 + MyEnvironment* const env = new MyEnvironment; 1.146 + Check(testing::AddGlobalTestEnvironment(env) == env, 1.147 + "AddGlobalTestEnvironment() should return its argument."); 1.148 + 1.149 + // Verifies that RUN_ALL_TESTS() runs the tests when the global 1.150 + // set-up is successful. 1.151 + Check(RunAllTests(env, NO_FAILURE) != 0, 1.152 + "RUN_ALL_TESTS() should return non-zero, as the global tear-down " 1.153 + "should generate a failure."); 1.154 + Check(test_was_run, 1.155 + "The tests should run, as the global set-up should generate no " 1.156 + "failure"); 1.157 + Check(env->tear_down_was_run(), 1.158 + "The global tear-down should run, as the global set-up was run."); 1.159 + 1.160 + // Verifies that RUN_ALL_TESTS() runs the tests when the global 1.161 + // set-up generates no fatal failure. 1.162 + Check(RunAllTests(env, NON_FATAL_FAILURE) != 0, 1.163 + "RUN_ALL_TESTS() should return non-zero, as both the global set-up " 1.164 + "and the global tear-down should generate a non-fatal failure."); 1.165 + Check(test_was_run, 1.166 + "The tests should run, as the global set-up should generate no " 1.167 + "fatal failure."); 1.168 + Check(env->tear_down_was_run(), 1.169 + "The global tear-down should run, as the global set-up was run."); 1.170 + 1.171 + // Verifies that RUN_ALL_TESTS() runs no test when the global set-up 1.172 + // generates a fatal failure. 1.173 + Check(RunAllTests(env, FATAL_FAILURE) != 0, 1.174 + "RUN_ALL_TESTS() should return non-zero, as the global set-up " 1.175 + "should generate a fatal failure."); 1.176 + Check(!test_was_run, 1.177 + "The tests should not run, as the global set-up should generate " 1.178 + "a fatal failure."); 1.179 + Check(env->tear_down_was_run(), 1.180 + "The global tear-down should run, as the global set-up was run."); 1.181 + 1.182 + // Verifies that RUN_ALL_TESTS() doesn't do global set-up or 1.183 + // tear-down when there is no test to run. 1.184 + testing::GTEST_FLAG(filter) = "-*"; 1.185 + Check(RunAllTests(env, NO_FAILURE) == 0, 1.186 + "RUN_ALL_TESTS() should return zero, as there is no test to run."); 1.187 + Check(!env->set_up_was_run(), 1.188 + "The global set-up should not run, as there is no test to run."); 1.189 + Check(!env->tear_down_was_run(), 1.190 + "The global tear-down should not run, " 1.191 + "as the global set-up was not run."); 1.192 + 1.193 + printf("PASS\n"); 1.194 + return 0; 1.195 +}