Пример #1
0
    public IEnumerator Timeout()
    {
        // large picture.
        var imagePath    = "https://upload.wikimedia.org/wikipedia/commons/2/2e/Zhao_Chang_-_Picture_of_the_New_Year_-_Google_Art_Project.jpg";
        var loadedFailed = false;

        Autoya.Persist_URLCaching_Load <Sprite>(
            AutoyaURLCachingTestsFileDomain,
            imagePath,
            bytes =>
        {
            // return sprite from bytes.
            var tex = new Texture2D(1, 1);
            tex.LoadImage(bytes);
            var newSprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f));
            return(newSprite);
        },
            cached =>
        {
            Fail();
        },
            (code, reason) =>
        {
            loadedFailed = true;
        },
            null,
            1
            );

        yield return(WaitUntil(
                         () => loadedFailed,
                         () => { throw new TimeoutException("timeout."); },
                         1000
                         ));
    }
Пример #2
0
    public IEnumerator LoadExample()
    {
        var loaded    = false;
        var imagePath = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/2016-06-14_Orange_and_white_tabby_cat_born_in_2016_茶トラ白ねこ_DSCF6526☆彡.jpg/400px-2016-06-14_Orange_and_white_tabby_cat_born_in_2016_茶トラ白ねこ_DSCF6526☆彡.jpg?a=b";

        Autoya.Persist_URLCaching_Load <Sprite>(
            AutoyaURLCachingTestsFileDomain,
            imagePath,
            bytes =>
        {
            // return sprite from bytes.
            var tex = new Texture2D(1, 1);
            tex.LoadImage(bytes);
            var newSprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f));
            return(newSprite);
        },
            cached =>
        {
            loaded = true;
        },
            (code, reason) =>
        {
            Fail();
        }
            );

        yield return(WaitUntil(
                         () => loaded,
                         () => { throw new TimeoutException("timeout."); }
                         ));
    }
Пример #3
0
    public IEnumerator NotExistURLOnce()
    {
        var loadFailed = false;
        var imagePath  = "https://upload.wikimedia.org/notExists.jpg?a=b";

        Autoya.Persist_URLCaching_Load(
            AutoyaURLCachingTestsFileDomain,
            imagePath,
            bytes =>
        {
            // return sprite from bytes.
            var tex = new Texture2D(1, 1);
            tex.LoadImage(bytes);
            var newSprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f));
            return(newSprite);
        },
            cached =>
        {
            Fail();
        },
            (code, reason) =>
        {
            loadFailed = true;
            True(code == 404, "code:" + code + " reason:" + reason);
        }
            );


        yield return(WaitUntil(
                         () => loadFailed,
                         () => { throw new TimeoutException("timeout."); }
                         ));
    }
    public IEnumerator ExecuteExpiration()
    {
        var done = false;
        var day  = -1;// マイナスを入れると絶対にexpire対象になる

        // store one image.
        var loaded    = false;
        var imagePath = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/2016-06-14_Orange_and_white_tabby_cat_born_in_2016_茶トラ白ねこ_DSCF6526☆彡.jpg/400px-2016-06-14_Orange_and_white_tabby_cat_born_in_2016_茶トラ白ねこ_DSCF6526☆彡.jpg?a=b";

        Autoya.Persist_URLCaching_Load(
            AutoyaURLCachingTestsFileDomain,
            imagePath,
            bytes =>
        {
            // return sprite from bytes.
            var tex = new Texture2D(1, 1);
            tex.LoadImage(bytes);
            var newSprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f));
            return(newSprite);
        },
            cached =>
        {
            loaded = true;
        },
            (code, reason) =>
        {
            loaded = true;
            Fail();
        }
            );

        yield return(WaitUntil(
                         () => loaded,
                         () => { throw new TimeoutException("timeout for making cache."); }
                         ));

        var pathOfCachedImage = Autoya.Persist_URLCaching_PathOf(AutoyaURLCachingTestsFileDomain, imagePath);

        Assert.True(File.Exists(pathOfCachedImage), "not exist.");

        // delete expired file.
        Autoya.Persist_URLCaching_ExecuteExpiration(
            AutoyaURLCachingTestsFileDomain,
            day,
            () =>
        {
            done = true;
        }
            );

        yield return(WaitUntil(
                         () => done,
                         () => { throw new TimeoutException("timeout."); },
                         50
                         ));

        // check deletion.
        Assert.False(File.Exists(pathOfCachedImage), "should not exist.");
    }
Пример #5
0
    void Start()
    {
        // sprite file cache domain. can be path.
        var spriteDomain = "persistence/sprite_cache";

        // purge all files in domain.
        Autoya.Persist_URLCaching_PurgeByDomain(spriteDomain);

        // download image from url, then get cached & sprite assets asynchronously.
        var queryString = "?q=0";

        Autoya.Persist_URLCaching_Load <Sprite>(
            spriteDomain,
            "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Jewelkatz_Romeo_Of_Stalker-Bars.jpg/500px-Jewelkatz_Romeo_Of_Stalker-Bars.jpg" + queryString,
            bytes =>
        {
            // return sprite from bytes.
            var tex = new Texture2D(1, 1);
            tex.LoadImage(bytes);
            var newSprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f));
            return(newSprite);
        },
            cached =>
        {
            // attach cached sprite to current screen image.
            image.sprite = cached;

            // set aspect.
            image.preserveAspect = true;
        },
            (code, reason) =>
        {
            Debug.LogError("code:" + code + " reason:" + reason);
        }
            );
    }
Пример #6
0
    public IEnumerator PurgeByDomainThenGetNew()
    {
        Sprite sprite = null;


        var loaded    = false;
        var imagePath = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/2016-06-14_Orange_and_white_tabby_cat_born_in_2016_茶トラ白ねこ_DSCF6526☆彡.jpg/400px-2016-06-14_Orange_and_white_tabby_cat_born_in_2016_茶トラ白ねこ_DSCF6526☆彡.jpg?a=b";

        Autoya.Persist_URLCaching_Load(
            AutoyaURLCachingTestsFileDomain,
            imagePath,
            bytes =>
        {
            // return sprite from bytes.
            var tex = new Texture2D(1, 1);
            tex.LoadImage(bytes);
            var newSprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f));
            return(newSprite);
        },
            cached =>
        {
            sprite = cached;
            loaded = true;
        },
            (code, reason) =>
        {
            Fail("reason:" + reason);
        }
            );

        yield return(WaitUntil(
                         () => loaded,
                         () => { throw new TimeoutException("timeout."); },
                         10
                         ));

        Autoya.Persist_URLCaching_PurgeByDomain(AutoyaURLCachingTestsFileDomain);

        var loadedAgain = false;

        // call again.
        Autoya.Persist_URLCaching_Load(
            AutoyaURLCachingTestsFileDomain,
            imagePath,
            bytes =>
        {
            // return sprite from bytes.
            var tex = new Texture2D(1, 1);
            tex.LoadImage(bytes);
            var newSprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f));
            return(newSprite);
        },
            cached =>
        {
            True(sprite != cached, "nothing changed.");
            loadedAgain = true;
        },
            (code, reason) =>
        {
            Fail("reason:" + reason);
        }
            );

        yield return(WaitUntil(
                         () => loadedAgain,
                         () => { throw new TimeoutException("timeout."); },
                         10
                         ));
    }
    public IEnumerator ExecuteExpirationForManyFiles()
    {
        var done          = false;
        var day_minus_one = -1;// マイナスを入れると絶対にexpire対象になる

        // store many images.
        var loaded         = 0;
        var imagePathsList = new List <string>();

        for (var i = 0; i < 40; i++)
        {
            var path = "https://dummyimage.com/" + (i + 1) + ".png/09f/fff";
            imagePathsList.Add(path);
        }
        ;
        var imagePaths = imagePathsList.ToArray();

        var pathOfCachedImages = new List <string>();

        foreach (var imagePath in imagePaths)
        {
            Autoya.Persist_URLCaching_Load <Texture2D>(
                AutoyaURLCachingTestsFileDomain,
                imagePath,
                bytes =>
            {
                return(null);
            },
                cached =>
            {
                loaded++;
                var pathOf = Autoya.Persist_URLCaching_PathOf(AutoyaURLCachingTestsFileDomain, imagePath);
                pathOfCachedImages.Add(pathOf);
            },
                (code, reason) =>
            {
                Debug.LogError("code:" + code + " reason:" + reason + " imagePath:" + imagePath);
                loaded++;
                Fail();
            },
                null,
                100
                );
        }

        yield return(WaitUntil(
                         () => loaded == imagePaths.Length,
                         () => { throw new TimeoutException("timeout for making cache."); },
                         30
                         ));

        // delete expired file.
        Autoya.Persist_URLCaching_ExecuteExpiration(
            AutoyaURLCachingTestsFileDomain,
            day_minus_one,
            () =>
        {
            done = true;
        }
            );

        yield return(WaitUntil(
                         () => done,
                         () => { throw new TimeoutException("timeout."); },
                         50
                         ));

        // check deletion.
        foreach (var path in pathOfCachedImages)
        {
            Assert.False(File.Exists(path), "should not exist.");
        }
    }