dm: require tmpDir, reasonable defaults

Change-Id: I9d84ce1ebbe417160a29ca2221b1df04901238e3
Reviewed-on: https://skia-review.googlesource.com/83541
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
diff --git a/tests/PDFDocumentTest.cpp b/tests/PDFDocumentTest.cpp
index 9a79ec8..82dafa3 100644
--- a/tests/PDFDocumentTest.cpp
+++ b/tests/PDFDocumentTest.cpp
@@ -44,12 +44,15 @@
     SkString tmpDir = skiatest::GetTmpDir();
 
     if (tmpDir.isEmpty()) {
-        return;  // TODO(edisonn): unfortunatelly this pattern is used in other
-                 // tests, but if GetTmpDir() starts returning and empty dir
-                 // allways, then all these tests will be disabled.
+        ERRORF(reporter, "missing tmpDir.");
+        return;
     }
 
     SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
+    if (!SkFILEWStream(path.c_str()).isValid()) {
+        ERRORF(reporter, "unable to write to: %s", path.c_str());
+        return;
+    }
 
     // Make sure doc's destructor is called to flush.
     {
@@ -72,12 +75,15 @@
 static void test_file(skiatest::Reporter* reporter) {
     SkString tmpDir = skiatest::GetTmpDir();
     if (tmpDir.isEmpty()) {
-        return;  // TODO(edisonn): unfortunatelly this pattern is used in other
-                 // tests, but if GetTmpDir() starts returning and empty dir
-                 // allways, then all these tests will be disabled.
+        ERRORF(reporter, "missing tmpDir.");
+        return;
     }
 
     SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
+    if (!SkFILEWStream(path.c_str()).isValid()) {
+        ERRORF(reporter, "unable to write to: %s", path.c_str());
+        return;
+    }
 
     sk_sp<SkDocument> doc(SkDocument::MakePDF(path.c_str()));
 
diff --git a/tests/StreamBufferTest.cpp b/tests/StreamBufferTest.cpp
index 35bebad..5435bf9 100644
--- a/tests/StreamBufferTest.cpp
+++ b/tests/StreamBufferTest.cpp
@@ -28,6 +28,9 @@
 // Test buffering from the beginning, by different amounts.
 static void test_buffer_from_beginning(skiatest::Reporter* r, std::unique_ptr<SkStream> stream,
                                        size_t length) {
+    if (!stream) {
+        return;
+    }
     SkStreamBuffer buffer(std::move(stream));
 
     // Buffer an arbitrary amount:
@@ -46,6 +49,9 @@
 // Test flushing the stream as we read.
 static void test_flushing(skiatest::Reporter* r, std::unique_ptr<SkStream> stream, size_t length,
                           bool getDataAtPosition) {
+    if (!stream) {
+        return;
+    }
     SkStreamBuffer buffer(std::move(stream));
     const size_t step = 5;
     for (size_t position = 0; position + step <= length; position += step) {
@@ -78,6 +84,10 @@
     if (!tmpDir.isEmpty()) {
         path = SkOSPath::Join(tmpDir.c_str(), subdir);
         SkFILEWStream writer(path.c_str());
+        if (!writer.isValid()) {
+            ERRORF(r, "unable to write to '%s'\n", path.c_str());
+            return;
+        }
         writer.write(gText, size);
     }
 
@@ -85,9 +95,11 @@
         std::function<std::unique_ptr<SkStream>()>  createStream;
         bool                                        skipIfNoTmpDir;
     } factories[] = {
-        { [&data]() { return skstd::make_unique<SkMemoryStream>(data); },       false },
-        { [&data]() { return skstd::make_unique<NotAssetMemStream>(data); },    false },
-        { [&path]() { return skstd::make_unique<SkFILEStream>(path.c_str()); }, true  },
+        { [&data]() { return skstd::make_unique<SkMemoryStream>(data); },       false  },
+        { [&data]() { return skstd::make_unique<NotAssetMemStream>(data); },    false  },
+        { [&path]() { return path.isEmpty()
+                             ? nullptr
+                             : skstd::make_unique<SkFILEStream>(path.c_str()); }, true },
     };
 
     for (auto f : factories) {
diff --git a/tests/StreamTest.cpp b/tests/StreamTest.cpp
index 24e74c6..01967b7 100644
--- a/tests/StreamTest.cpp
+++ b/tests/StreamTest.cpp
@@ -287,12 +287,25 @@
     test_fully_peekable_stream(reporter, &memStream, memStream.getLength());
 
     // Test an arbitrary file stream. file streams do not support peeking.
-    constexpr char filename[] = "images/baby_tux.webp";
-    SkString path = GetResourcePath(filename);
-    if (!sk_exists(path.c_str())) {
-        ERRORF(reporter, "file missing: %s\n", filename);
+    auto tmpdir = skiatest::GetTmpDir();
+    if (tmpdir.isEmpty()) {
+        ERRORF(reporter, "no tmp dir!");
         return;
     }
+    auto path = SkOSPath::Join(tmpdir.c_str(), "file");
+    {
+        SkFILEWStream wStream(path.c_str());
+        constexpr char filename[] = "images/baby_tux.webp";
+        auto data = GetResourceAsData(filename);
+        if (!data || data->size() == 0) {
+            ERRORF(reporter, "resource missing: %s\n", filename);
+            return;
+        }
+        if (!wStream.isValid() || !wStream.write(data->data(), data->size())) {
+            ERRORF(reporter, "error wrtiting to file %s", path.c_str());
+            return;
+        }
+    }
     SkFILEStream fileStream(path.c_str());
     REPORTER_ASSERT(reporter, fileStream.isValid());
     if (!fileStream.isValid()) {
diff --git a/tests/Test.cpp b/tests/Test.cpp
index 37515dd..96bbbad 100644
--- a/tests/Test.cpp
+++ b/tests/Test.cpp
@@ -7,6 +7,8 @@
 
 #include "Test.h"
 
+#include <stdlib.h>
+
 #include "SkCommandLineFlags.h"
 #include "SkString.h"
 #include "SkTime.h"
@@ -32,8 +34,24 @@
 }
 
 SkString skiatest::GetTmpDir() {
-    const char* tmpDir = FLAGS_tmpDir.isEmpty() ? nullptr : FLAGS_tmpDir[0];
-    return SkString(tmpDir);
+    if (!FLAGS_tmpDir.isEmpty()) {
+        return SkString(FLAGS_tmpDir[0]);
+    }
+#ifdef SK_BUILD_FOR_ANDROID
+    const char* environmentVariable = "TMPDIR";
+    const char* defaultValue = "/data/local/tmp";
+#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX)
+    const char* environmentVariable = "TMPDIR";
+    const char* defaultValue = "/tmp";
+#elif defined(SK_BUILD_FOR_WIN32)
+    const char* environmentVariable = "TEMP";
+    const char* defaultValue = nullptr;
+#else
+    const char* environmentVariable = nullptr;
+    const char* defaultValue = nullptr;
+#endif
+    const char* tmpdir = environmentVariable ? getenv(environmentVariable) : nullptr;
+    return SkString(tmpdir ? tmpdir : defaultValue);
 }
 
 skiatest::Timer::Timer() : fStartNanos(SkTime::GetNSecs()) {}