1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "tests/Test.h"
9 
10 #include <stdlib.h>
11 
12 #include "include/core/SkString.h"
13 #include "include/core/SkTime.h"
14 #include "tools/flags/CommandLineFlags.h"
15 
16 static DEFINE_string2(tmpDir, t, nullptr, "Temp directory to use.");
17 
bumpTestCount()18 void skiatest::Reporter::bumpTestCount() {}
19 
allowExtendedTest() const20 bool skiatest::Reporter::allowExtendedTest() const { return false; }
21 
verbose() const22 bool skiatest::Reporter::verbose() const { return false; }
23 
24 
reportFailedWithContext(const skiatest::Failure & f)25 void skiatest::Reporter::reportFailedWithContext(const skiatest::Failure& f) {
26     SkString fullMessage = f.message;
27     if (!fContextStack.empty()) {
28         fullMessage.append(" [");
29         for (int i = 0; i < fContextStack.count(); ++i) {
30             if (i > 0) {
31                 fullMessage.append(", ");
32             }
33             fullMessage.append(fContextStack[i]);
34         }
35         fullMessage.append("]");
36     }
37     this->reportFailed(skiatest::Failure(f.fileName, f.lineNo, f.condition, fullMessage));
38 }
39 
toString() const40 SkString skiatest::Failure::toString() const {
41     SkString result = SkStringPrintf("%s:%d\t", this->fileName, this->lineNo);
42     if (!this->message.isEmpty()) {
43         result.append(this->message);
44         if (strlen(this->condition) > 0) {
45             result.append(": ");
46         }
47     }
48     result.append(this->condition);
49     return result;
50 }
51 
GetTmpDir()52 SkString skiatest::GetTmpDir() {
53     if (!FLAGS_tmpDir.isEmpty()) {
54         return SkString(FLAGS_tmpDir[0]);
55     }
56 #ifdef SK_BUILD_FOR_ANDROID
57     const char* environmentVariable = "TMPDIR";
58     const char* defaultValue = "/data/local/tmp";
59 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX)
60     const char* environmentVariable = "TMPDIR";
61     const char* defaultValue = "/tmp";
62 #elif defined(SK_BUILD_FOR_WIN)
63     const char* environmentVariable = "TEMP";
64     const char* defaultValue = nullptr;
65 #else
66     const char* environmentVariable = nullptr;
67     const char* defaultValue = nullptr;
68 #endif
69     const char* tmpdir = environmentVariable ? getenv(environmentVariable) : nullptr;
70     return SkString(tmpdir ? tmpdir : defaultValue);
71 }
72 
Timer()73 skiatest::Timer::Timer() : fStartNanos(SkTime::GetNSecs()) {}
74 
elapsedNs() const75 double skiatest::Timer::elapsedNs() const {
76     return SkTime::GetNSecs() - fStartNanos;
77 }
78 
elapsedMs() const79 double skiatest::Timer::elapsedMs() const { return this->elapsedNs() * 1e-6; }
80 
elapsedMsInt() const81 SkMSec skiatest::Timer::elapsedMsInt() const {
82     const double elapsedMs = this->elapsedMs();
83     SkASSERT(SK_MSecMax >= elapsedMs);
84     return static_cast<SkMSec>(elapsedMs);
85 }
86