1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/resources/ui_resource_bitmap.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "third_party/skia/include/core/SkBitmap.h"
8 #include "third_party/skia/include/core/SkCanvas.h"
9 #include "ui/android/resources/resource_manager_impl.h"
10 #include "ui/android/resources/system_ui_resource_type.h"
11 #include "ui/android/resources/ui_resource_client_android.h"
12 #include "ui/android/resources/ui_resource_provider.h"
13 #include "ui/gfx/android/java_bitmap.h"
17 class TestResourceManagerImpl
: public ResourceManagerImpl
{
19 explicit TestResourceManagerImpl(UIResourceProvider
* provider
)
20 : ResourceManagerImpl(provider
) {}
22 ~TestResourceManagerImpl() override
{}
24 void SetResourceAsLoaded(AndroidResourceType res_type
, int res_id
) {
25 SkBitmap small_bitmap
;
26 SkCanvas
canvas(small_bitmap
);
27 small_bitmap
.allocPixels(
28 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType
, kOpaque_SkAlphaType
));
29 canvas
.drawColor(SK_ColorWHITE
);
30 small_bitmap
.setImmutable();
32 OnResourceReady(NULL
, NULL
, res_type
, res_id
,
33 gfx::ConvertToJavaBitmap(&small_bitmap
).obj(), 0, 0, 0, 0,
38 void PreloadResourceFromJava(AndroidResourceType res_type
,
39 int res_id
) override
{}
41 void RequestResourceFromJava(AndroidResourceType res_type
,
42 int res_id
) override
{
43 SetResourceAsLoaded(res_type
, res_id
);
49 const ui::SystemUIResourceType kTestResourceType
= ui::OVERSCROLL_GLOW
;
51 class MockUIResourceProvider
: public ui::UIResourceProvider
{
53 MockUIResourceProvider()
54 : next_ui_resource_id_(1),
55 has_layer_tree_host_(true),
56 resource_manager_(this) {}
58 virtual ~MockUIResourceProvider() {}
60 cc::UIResourceId
CreateUIResource(
61 ui::UIResourceClientAndroid
* client
) override
{
62 if (!has_layer_tree_host_
)
64 cc::UIResourceId id
= next_ui_resource_id_
++;
65 client
->GetBitmap(id
, false);
66 ui_resource_client_map_
[id
] = client
;
70 void DeleteUIResource(cc::UIResourceId id
) override
{
71 CHECK(has_layer_tree_host_
);
72 ui_resource_client_map_
.erase(id
);
75 bool SupportsETC1NonPowerOfTwo() const override
{ return true; }
77 void LayerTreeHostCleared() {
78 has_layer_tree_host_
= false;
79 UIResourceClientMap client_map
= ui_resource_client_map_
;
80 ui_resource_client_map_
.clear();
81 for (UIResourceClientMap::iterator iter
= client_map
.begin();
82 iter
!= client_map
.end(); iter
++) {
83 iter
->second
->UIResourceIsInvalid();
87 void LayerTreeHostReturned() { has_layer_tree_host_
= true; }
89 TestResourceManagerImpl
& GetResourceManager() { return resource_manager_
; }
91 cc::UIResourceId
next_ui_resource_id() const { return next_ui_resource_id_
; }
94 typedef base::hash_map
<cc::UIResourceId
, ui::UIResourceClientAndroid
*>
97 cc::UIResourceId next_ui_resource_id_
;
98 UIResourceClientMap ui_resource_client_map_
;
99 bool has_layer_tree_host_
;
101 // The UIResourceProvider owns the ResourceManager.
102 TestResourceManagerImpl resource_manager_
;
107 class ResourceManagerTest
: public testing::Test
{
109 void PreloadResource(ui::SystemUIResourceType type
) {
110 ui_resource_provider_
.GetResourceManager().PreloadResource(
111 ui::ANDROID_RESOURCE_TYPE_SYSTEM
, type
);
114 cc::UIResourceId
GetUIResourceId(ui::SystemUIResourceType type
) {
115 return ui_resource_provider_
.GetResourceManager().GetUIResourceId(
116 ui::ANDROID_RESOURCE_TYPE_SYSTEM
, type
);
119 void LayerTreeHostCleared() { ui_resource_provider_
.LayerTreeHostCleared(); }
121 void LayerTreeHostReturned() {
122 ui_resource_provider_
.LayerTreeHostReturned();
125 void SetResourceAsLoaded(ui::SystemUIResourceType type
) {
126 ui_resource_provider_
.GetResourceManager().SetResourceAsLoaded(
127 ui::ANDROID_RESOURCE_TYPE_SYSTEM
, type
);
130 cc::UIResourceId
GetNextUIResourceId() const {
131 return ui_resource_provider_
.next_ui_resource_id();
135 MockUIResourceProvider ui_resource_provider_
;
138 TEST_F(ResourceManagerTest
, GetResource
) {
139 EXPECT_NE(0, GetUIResourceId(kTestResourceType
));
142 TEST_F(ResourceManagerTest
, PreloadEnsureResource
) {
143 // Preloading the resource should trigger bitmap loading, but the actual
144 // resource id will not be generated until it is explicitly requested.
145 cc::UIResourceId first_resource_id
= GetNextUIResourceId();
146 PreloadResource(kTestResourceType
);
147 SetResourceAsLoaded(kTestResourceType
);
148 EXPECT_EQ(first_resource_id
, GetNextUIResourceId());
149 EXPECT_NE(0, GetUIResourceId(kTestResourceType
));
150 EXPECT_NE(first_resource_id
, GetNextUIResourceId());
153 TEST_F(ResourceManagerTest
, ResetLayerTreeHost
) {
154 EXPECT_NE(0, GetUIResourceId(kTestResourceType
));
155 LayerTreeHostCleared();
156 EXPECT_EQ(0, GetUIResourceId(kTestResourceType
));
157 LayerTreeHostReturned();
158 EXPECT_NE(0, GetUIResourceId(kTestResourceType
));