Make unit tests use generic testcontext rather than glcontext
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1971613003

Review-Url: https://codereview.chromium.org/1971613003
diff --git a/tests/GrContextFactoryTest.cpp b/tests/GrContextFactoryTest.cpp
index e710ca4..3b12b83 100644
--- a/tests/GrContextFactoryTest.cpp
+++ b/tests/GrContextFactoryTest.cpp
@@ -78,9 +78,7 @@
         if (!info1.grContext()) {
             continue;
         }
-        if (GrContextFactory::ContextTypeBackend(ctxType) == kOpenGL_GrBackend) {
-            REPORTER_ASSERT(reporter, info1.glContext());
-        }
+        REPORTER_ASSERT(reporter, info1.testContext());
          // Ref for comparison. The API does not explicitly say that this stays alive.
         info1.grContext()->ref();
         testFactory.abandonContexts();
@@ -88,9 +86,8 @@
         // Test that we get different context after abandon.
         ContextInfo info2 = testFactory.getContextInfo(ctxType);
         REPORTER_ASSERT(reporter, info2.grContext());
-        if (GrContextFactory::ContextTypeBackend(ctxType) == kOpenGL_GrBackend) {
-            REPORTER_ASSERT(reporter, info2.glContext());
-        }
+        REPORTER_ASSERT(reporter, info2.testContext());
+
         REPORTER_ASSERT(reporter, info1.grContext() != info2.grContext());
         // The GL context should also change, but it also could get the same address.
 
diff --git a/tests/ImageTest.cpp b/tests/ImageTest.cpp
index 88f4d7f..0922364 100644
--- a/tests/ImageTest.cpp
+++ b/tests/ImageTest.cpp
@@ -404,14 +404,15 @@
     }
 }
 
-DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_newTextureImage, reporter, contextInfo) {
+DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_newTextureImage, reporter, contextInfo) {
     GrContext* context = contextInfo.grContext();
-    sk_gpu_test::GLTestContext* glContext = contextInfo.glContext();
+    sk_gpu_test::TestContext* testContext = contextInfo.testContext();
 
     GrContextFactory otherFactory;
-    ContextInfo otherContextInfo =
-        otherFactory.getContextInfo(GrContextFactory::kNativeGL_ContextType);
-    glContext->makeCurrent();
+    GrContextFactory::ContextType otherContextType =
+            GrContextFactory::NativeContextTypeForBackend(testContext->backend());
+    ContextInfo otherContextInfo = otherFactory.getContextInfo(otherContextType);
+    testContext->makeCurrent();
 
     std::function<sk_sp<SkImage>()> imageFactories[] = {
         create_image,
@@ -422,10 +423,10 @@
         // Create a texture image.
         [context] { return create_gpu_image(context); },
         // Create a texture image in a another GrContext.
-        [glContext, otherContextInfo] {
-            otherContextInfo.glContext()->makeCurrent();
+        [testContext, otherContextInfo] {
+            otherContextInfo.testContext()->makeCurrent();
             sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext());
-            glContext->makeCurrent();
+            testContext->makeCurrent();
             return otherContextImage;
         }
     };
@@ -824,16 +825,16 @@
     }
 }
 
-DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(DeferredTextureImage, reporter, ctxInfo) {
+DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredTextureImage, reporter, ctxInfo) {
     GrContext* context = ctxInfo.grContext();
-    sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
+    sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
     SkAutoTUnref<GrContextThreadSafeProxy> proxy(context->threadSafeProxy());
 
     GrContextFactory otherFactory;
     ContextInfo otherContextInfo =
         otherFactory.getContextInfo(GrContextFactory::kNativeGL_ContextType);
 
-    glContext->makeCurrent();
+    testContext->makeCurrent();
     REPORTER_ASSERT(reporter, proxy);
     struct {
         std::function<sk_sp<SkImage> ()> fImageFactory;
@@ -845,10 +846,10 @@
         { create_picture_image,  false },
         { [context] { return create_gpu_image(context); }, false },
         // Create a texture image in a another GrContext.
-        { [glContext, otherContextInfo] {
-            otherContextInfo.glContext()->makeCurrent();
+        { [testContext, otherContextInfo] {
+            otherContextInfo.testContext()->makeCurrent();
             sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext());
-            glContext->makeCurrent();
+            testContext->makeCurrent();
             return otherContextImage;
           }, false },
     };
@@ -890,7 +891,7 @@
                     sk_sp<SkImage> newImage2(SkImage::MakeFromDeferredTextureImageData(
                         otherContextInfo.grContext(), buffer, budgeted));
                     REPORTER_ASSERT(reporter, !newImage2);
-                    glContext->makeCurrent();
+                    testContext->makeCurrent();
                 }
             }
             sk_free(buffer);
diff --git a/tools/gpu/GrContextFactory.h b/tools/gpu/GrContextFactory.h
index dc896a7..ed8e645 100644
--- a/tools/gpu/GrContextFactory.h
+++ b/tools/gpu/GrContextFactory.h
@@ -28,6 +28,8 @@
 
     GrContext* grContext() const { return fGrContext; }
 
+    TestContext* testContext() const { return fTestContext; }
+
     GLTestContext* glContext() const {
         SkASSERT(kOpenGL_GrBackend == fBackend);
         return static_cast<GLTestContext*>(fTestContext);
@@ -93,6 +95,18 @@
         kRequireSRGBSupport_ContextOptions  = 0x2,
     };
 
+    static ContextType NativeContextTypeForBackend(GrBackend backend) {
+        switch (backend) {
+            case kOpenGL_GrBackend:
+                return kNativeGL_ContextType;
+            case kVulkan_GrBackend:
+                return kVulkan_ContextType;
+            default:
+                SkFAIL("Unknown backend");
+                return kNullGL_ContextType;
+        }
+    }
+
     static bool IsRenderingContext(ContextType type) {
         switch (type) {
             case kNullGL_ContextType: