This source file includes following definitions.
- SetUp
- TearDown
- CreateResourceCollection
- DestroyResourceCollection
- CreateResourceArray
- UnusedResourcesAreAvailable
- ReturnAndResetResourcesAvailable
- TEST_F
- ReturnResourcesOnThread
- TEST_F
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "cc/layers/delegated_frame_resource_collection.h"
#include "cc/resources/returned_resource.h"
#include "cc/resources/transferable_resource.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cc {
namespace {
class DelegatedFrameResourceCollectionTest
: public testing::Test,
public DelegatedFrameResourceCollectionClient {
protected:
DelegatedFrameResourceCollectionTest() : resources_available_(false) {}
virtual void SetUp() OVERRIDE { CreateResourceCollection(); }
virtual void TearDown() OVERRIDE { DestroyResourceCollection(); }
void CreateResourceCollection() {
DCHECK(!resource_collection_);
resource_collection_ = new DelegatedFrameResourceCollection;
resource_collection_->SetClient(this);
}
void DestroyResourceCollection() {
if (resource_collection_) {
resource_collection_->SetClient(NULL);
resource_collection_ = NULL;
}
}
TransferableResourceArray CreateResourceArray() {
TransferableResourceArray resources;
TransferableResource resource;
resource.id = 444;
resources.push_back(resource);
return resources;
}
virtual void UnusedResourcesAreAvailable() OVERRIDE {
resources_available_ = true;
resource_collection_->TakeUnusedResourcesForChildCompositor(
&returned_resources_);
if (!resources_available_closure_.is_null())
resources_available_closure_.Run();
}
bool ReturnAndResetResourcesAvailable() {
bool r = resources_available_;
resources_available_ = false;
return r;
}
scoped_refptr<DelegatedFrameResourceCollection> resource_collection_;
bool resources_available_;
ReturnedResourceArray returned_resources_;
base::Closure resources_available_closure_;
};
TEST_F(DelegatedFrameResourceCollectionTest, NoRef) {
EXPECT_TRUE(resource_collection_->HasOneRef());
ReturnCallback return_callback =
resource_collection_->GetReturnResourcesCallbackForImplThread();
EXPECT_TRUE(resource_collection_->HasOneRef());
}
void ReturnResourcesOnThread(ReturnCallback callback,
const ReturnedResourceArray& resources,
base::WaitableEvent* event) {
callback.Run(resources);
if (event)
event->Wait();
}
TEST_F(DelegatedFrameResourceCollectionTest, Thread) {
base::Thread thread("test thread");
thread.Start();
TransferableResourceArray resources = CreateResourceArray();
resource_collection_->ReceivedResources(resources);
resource_collection_->RefResources(resources);
ReturnedResourceArray returned_resources;
TransferableResource::ReturnResources(resources, &returned_resources);
base::WaitableEvent event(false, false);
{
base::RunLoop run_loop;
resources_available_closure_ = run_loop.QuitClosure();
thread.message_loop()->PostTask(
FROM_HERE,
base::Bind(
&ReturnResourcesOnThread,
resource_collection_->GetReturnResourcesCallbackForImplThread(),
returned_resources,
&event));
run_loop.Run();
}
EXPECT_TRUE(ReturnAndResetResourcesAvailable());
EXPECT_EQ(1u, returned_resources_.size());
EXPECT_EQ(444u, returned_resources_[0].id);
EXPECT_EQ(1, returned_resources_[0].count);
returned_resources_.clear();
DestroyResourceCollection();
event.Signal();
CreateResourceCollection();
resource_collection_->ReceivedResources(resources);
resource_collection_->RefResources(resources);
ReturnCallback return_callback =
resource_collection_->GetReturnResourcesCallbackForImplThread();
resource_collection_->LoseAllResources();
DestroyResourceCollection();
EXPECT_TRUE(ReturnAndResetResourcesAvailable());
EXPECT_EQ(1u, returned_resources_.size());
EXPECT_EQ(444u, returned_resources_[0].id);
EXPECT_EQ(1, returned_resources_[0].count);
EXPECT_TRUE(returned_resources_[0].lost);
returned_resources_.clear();
base::WaitableEvent* null_event = NULL;
thread.message_loop()->PostTask(FROM_HERE,
base::Bind(&ReturnResourcesOnThread,
return_callback,
returned_resources,
null_event));
thread.Stop();
}
}
}