Back to index

unity  6.0.0
test_texture_cache.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as published
00006  * by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * version 3 along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>
00016  *
00017  * Authored by: Tim Penhey <tim.penhey@canonical.com>
00018  */
00019 
00020 #include <gmock/gmock.h>
00021 
00022 #include "TextureCache.h"
00023 
00024 using namespace testing;
00025 using namespace unity;
00026 
00027 namespace
00028 {
00029 typedef nux::BaseTexture* (*TextureCallBack)(std::string const&, int, int);
00030 
00031 TEST(TestTextureCache, TestInitiallyEmpty)
00032 {
00033   TextureCache& cache = TextureCache::GetDefault();
00034 
00035   EXPECT_THAT(cache.Size(), Eq(0));
00036 }
00037 
00038 struct TextureCallbackValues
00039 {
00040   std::string id;
00041   int width;
00042   int height;
00043 
00044   TextureCallbackValues() : width(0), height(0) {}
00045   nux::BaseTexture* callback(std::string const& i, int w, int h) {
00046     id = i;
00047     width = w;
00048     height = h;
00049     return nux::GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableTexture();
00050   }
00051 };
00052 
00053 TEST(TestTextureCache, DISABLED_TestCallsCreateTextureCallback)
00054 {
00055   // Another lambda issue.  If the lambda takes a reference to any other
00056   // variables, it seems incapable of assigning the function to the
00057   // TextureCallback type.
00058   TextureCallbackValues values;
00059 
00060   TextureCache& cache = TextureCache::GetDefault();
00061   TextureCache::CreateTextureCallback callback(sigc::mem_fun(values, &TextureCallbackValues::callback));
00062 
00063   nux::ObjectPtr<nux::BaseTexture> texture = cache.FindTexture("foo", 5, 7, callback);
00064 
00065   EXPECT_THAT(cache.Size(), Eq(1));
00066   EXPECT_THAT(values.id, Eq("foo"));
00067   EXPECT_THAT(values.width, Eq(5));
00068   EXPECT_THAT(values.height, Eq(7));
00069   EXPECT_THAT(texture->GetReferenceCount(), Eq(1));
00070 }
00071 
00072 struct TextureCallbackCounter
00073 {
00074   int count;
00075 
00076   TextureCallbackCounter() : count(0) {}
00077   nux::BaseTexture* callback(std::string const& i, int w, int h) {
00078     ++count;
00079     return nux::GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableTexture();
00080   }
00081 };
00082 
00083 TEST(TestTextureCache, DISABLED_TestCallbackOnlyCalledOnce)
00084 {
00085   TextureCallbackCounter counter;
00086   TextureCache::CreateTextureCallback callback(sigc::mem_fun(counter, &TextureCallbackCounter::callback));
00087 
00088   TextureCache& cache = TextureCache::GetDefault();
00089 
00090   nux::ObjectPtr<nux::BaseTexture> t1 = cache.FindTexture("foo", 5, 7, callback);
00091   nux::ObjectPtr<nux::BaseTexture> t2 = cache.FindTexture("foo", 5, 7, callback);
00092 
00093   EXPECT_THAT(cache.Size(), Eq(1));
00094   EXPECT_THAT(counter.count, Eq(1));
00095   EXPECT_TRUE(t1 == t2);
00096 }
00097 
00098 TEST(TestTextureCache, DISABLED_TestCacheRemovesDeletedObject)
00099 {
00100   // Note for others, if just using the lambda function, the return value is
00101   // lost in the type deduction that sigc uses.  So we have the typedef
00102   // (TextureCallback) at the top which includes the return value.  The lambda
00103   // constructor is fine to assign into this as it knows, and the explicit
00104   // return type is good for sigc.
00105   TextureCallBack callback =
00106     [](std::string const& i, int w, int h) -> nux::BaseTexture*
00107     {
00108       return nux::GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableTexture();
00109     };
00110 
00111   TextureCache& cache = TextureCache::GetDefault();
00112 
00113   nux::ObjectPtr<nux::BaseTexture> t1 = cache.FindTexture("foo", 5, 7, callback);
00114 
00115   // Now delete the only reference to the texture.
00116   t1 = nux::ObjectPtr<nux::BaseTexture>();
00117 
00118   EXPECT_THAT(cache.Size(), Eq(0));
00119 }
00120 
00121 
00122 }