1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/common/unittest_util.h"
16 :
17 : namespace testing {
18 :
19 : ApplicationTestBase* ApplicationTestBase::self_ = NULL;
20 :
21 : void ApplicationTestBase::InitStreams(const base::FilePath& in_path,
22 : const base::FilePath& out_path,
23 E : const base::FilePath& err_path) {
24 E : ASSERT_FALSE(in_path.empty());
25 E : ASSERT_FALSE(out_path.empty());
26 E : ASSERT_FALSE(err_path.empty());
27 :
28 E : in_.reset(file_util::OpenFile(in_path, "r"));
29 E : out_.reset(file_util::OpenFile(out_path, "w"));
30 E : err_.reset(file_util::OpenFile(err_path, "w"));
31 :
32 E : ASSERT_TRUE(in_.get() != NULL);
33 E : ASSERT_TRUE(out_.get() != NULL);
34 E : ASSERT_TRUE(err_.get() != NULL);
35 :
36 : // Intercept logging.
37 E : ASSERT_TRUE(self_ == NULL);
38 E : ASSERT_TRUE(log_handler_ == NULL);
39 E : self_ = this;
40 E : log_handler_ = logging::GetLogMessageHandler();
41 E : logging::SetLogMessageHandler(&HandleLogMessage);
42 E : }
43 :
44 E : void ApplicationTestBase::TearDownStreams() {
45 E : if (self_ != NULL) {
46 E : logging::SetLogMessageHandler(log_handler_);
47 E : log_handler_ = NULL;
48 E : self_ = NULL;
49 : }
50 :
51 E : ASSERT_NO_FATAL_FAILURE(TearDownStream(&in_));
52 E : ASSERT_NO_FATAL_FAILURE(TearDownStream(&out_));
53 E : ASSERT_NO_FATAL_FAILURE(TearDownStream(&err_));
54 E : }
55 :
56 E : void ApplicationTestBase::SetUp() {
57 E : Super::SetUp();
58 :
59 : // Save the log level so that we can restore it in TearDown.
60 E : log_level_ = logging::GetMinLogLevel();
61 E : }
62 :
63 E : void ApplicationTestBase::TearDown() {
64 E : logging::SetMinLogLevel(log_level_);
65 :
66 : // These need to be shut down before we can delete the temporary
67 : // directories.
68 E : EXPECT_NO_FATAL_FAILURE(TearDownStreams());
69 :
70 E : DirList::const_iterator iter;
71 E : for (iter = temp_dirs_.begin(); iter != temp_dirs_.end(); ++iter) {
72 E : EXPECT_TRUE(file_util::Delete(*iter, true));
73 E : }
74 :
75 E : Super::TearDown();
76 E : }
77 :
78 : bool ApplicationTestBase::HandleLogMessage(int severity, const char* file,
79 E : int line, size_t message_start, const std::string& str) {
80 E : DCHECK(self_ != NULL);
81 E : if (severity < logging::GetMinLogLevel())
82 i : return true;
83 E : fprintf(self_->err(), "%s", str.c_str());
84 E : return true;
85 E : }
86 :
87 E : void ApplicationTestBase::TearDownStream(file_util::ScopedFILE* stream) {
88 E : ASSERT_TRUE(stream != NULL);
89 E : if (stream->get() == NULL)
90 E : return;
91 E : ASSERT_EQ(0, ::fclose(stream->get()));
92 E : stream->reset();
93 E : }
94 :
95 : FILE* ApplicationTestBase::GetOrInitFile(file_util::ScopedFILE* f,
96 E : const char* mode) {
97 E : DCHECK(f != NULL);
98 E : DCHECK(mode != NULL);
99 E : if (f->get() == NULL)
100 E : f->reset(file_util::OpenFile(base::FilePath(L"NUL"), mode));
101 E : return f->get();
102 E : }
103 :
104 : } // namespace testing
|