示例#1
0
 /// <summary> Flag will be flipped to true if it was false, otherwise will return false </summary>
 public static bool FlipToTrue(ref int flag)
 {
     AssertV2.IsTrue(flag == 0 || flag == 1, "int was not 0 or 1 but " + flag);
     return(Interlocked.CompareExchange(ref flag, 1, 0) == 0);
 }
示例#2
0
        private IEnumerator AssertNoVisualRegressionCoroutine(string id, StackTrace stacktrace)
        {
            id = EnvironmentV2.SanatizeToFileName(id);
            if (id.IsNullOrEmpty())
            {
                throw new ArgumentNullException("Invalid ID passed");
            }

            var idFolder = GetFolderFor(id);
            var oldImg   = idFolder.GetChild(id + ".regression.jpg");
            var newImg   = idFolder.GetChild(id + ".jpg");
            var backup   = idFolder.GetChild(id + ".jpg.backup");

            Config config = LoadConfigFor(id);

            yield return(new WaitForEndOfFrame());

            Texture2D screenShot = ScreenCapture.CaptureScreenshotAsTexture(config.screenshotUpscaleFactor);

            // Texture2D screenShot = Camera.allCameras.CaptureScreenshot(); // Does not capture UI

            if (newImg.Exists)
            {
                newImg.CopyToV2(backup, replaceExisting: false);
            }
            screenShot.SaveToJpgFile(newImg, config.screenshotQuality);
            screenShot.Destroy();

            var diffImg = CalculateDiffImage(oldImg, newImg, config.maxAllowedDiff, config.errorMetric);

            if (diffImg != null)
            {
                var e = $"Visual diff to previous '{id}' detected! To approve an allowed visual change, delete '{oldImg.Name}'";
                if (!config.customErrorMessage.IsNullOrEmpty())
                {
                    e = config.customErrorMessage + "/n" + e;
                }
                var exeption = new Error(e, stacktrace);
                if (config.throwAssertException)
                {
                    throw exeption;
                }
                else if (config.logAsError)
                {
                    Log.e(exeption);
                }
                else if (config.logAsWarning)
                {
                    Log.w(e);
                }
                if (config.openExternallyOnAssertFail)
                {
                    diffImg.Parent.OpenInExternalApp();
                    diffImg.OpenInExternalApp();
                }
            }
            else     // No difference between oldImg and newImg
            // To prevent git from detecting invalid file changes:
            {
                if (backup.Exists)                               // If there is a backup of newImg..
                {
                    newImg.DeleteV2();                           // Remove the newly generated version ..
                    backup.Rename(newImg.Name, out FileEntry _); // and replace it with the backup
                }
            }
            backup.DeleteV2(); // If a backup file was created during the process delete it
            AssertV2.IsTrue(newImg.Exists, "newImg did not exist after AssertNoVisualChange done");
        }