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 "content/browser/android/system_ui_resource_manager_impl.h"
7 #include "content/public/browser/android/ui_resource_client_android.h"
8 #include "content/public/browser/android/ui_resource_provider.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "third_party/skia/include/core/SkCanvas.h"
15 class TestSystemUIResourceManagerImpl
16 : public content::SystemUIResourceManagerImpl
{
18 TestSystemUIResourceManagerImpl(content::UIResourceProvider
* provider
)
19 : SystemUIResourceManagerImpl(provider
) {}
21 virtual ~TestSystemUIResourceManagerImpl() {}
23 virtual void BuildResource(
24 ui::SystemUIResourceManager::ResourceType type
) OVERRIDE
{}
26 void SetResourceAsLoaded(ui::SystemUIResourceManager::ResourceType type
) {
27 SkBitmap small_bitmap
;
28 SkCanvas
canvas(small_bitmap
);
29 small_bitmap
.allocPixels(
30 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType
, kOpaque_SkAlphaType
));
31 canvas
.drawColor(SK_ColorWHITE
);
32 small_bitmap
.setImmutable();
33 OnFinishedLoadBitmap(type
, &small_bitmap
);
39 const ui::SystemUIResourceManager::ResourceType TEST_RESOURCE_TYPE
=
40 ui::SystemUIResourceManager::OVERSCROLL_GLOW
;
42 class MockUIResourceProvider
: public content::UIResourceProvider
{
44 MockUIResourceProvider()
45 : next_ui_resource_id_(1),
46 has_layer_tree_host_(true),
47 system_ui_resource_manager_(this) {}
49 virtual ~MockUIResourceProvider() {}
51 virtual cc::UIResourceId
CreateUIResource(
52 content::UIResourceClientAndroid
* client
) OVERRIDE
{
53 if (!has_layer_tree_host_
)
55 cc::UIResourceId id
= next_ui_resource_id_
++;
56 client
->GetBitmap(id
, false);
57 ui_resource_client_map_
[id
] = client
;
61 virtual void DeleteUIResource(cc::UIResourceId id
) OVERRIDE
{
62 CHECK(has_layer_tree_host_
);
63 ui_resource_client_map_
.erase(id
);
66 virtual bool SupportsETC1NonPowerOfTwo() const OVERRIDE
{ return true; }
68 void LayerTreeHostCleared() {
69 has_layer_tree_host_
= false;
70 UIResourceClientMap client_map
= ui_resource_client_map_
;
71 ui_resource_client_map_
.clear();
72 for (UIResourceClientMap::iterator iter
= client_map
.begin();
73 iter
!= client_map
.end();
75 iter
->second
->UIResourceIsInvalid();
79 void LayerTreeHostReturned() { has_layer_tree_host_
= true; }
81 TestSystemUIResourceManagerImpl
& GetSystemUIResourceManager() {
82 return system_ui_resource_manager_
;
85 cc::UIResourceId
next_ui_resource_id() const { return next_ui_resource_id_
; }
88 typedef base::hash_map
<cc::UIResourceId
, content::UIResourceClientAndroid
*>
91 cc::UIResourceId next_ui_resource_id_
;
92 UIResourceClientMap ui_resource_client_map_
;
93 bool has_layer_tree_host_
;
95 // The UIResourceProvider owns the SystemUIResourceManager.
96 TestSystemUIResourceManagerImpl system_ui_resource_manager_
;
101 class SystemUIResourceManagerImplTest
: public testing::Test
{
103 void PreloadResource(ui::SystemUIResourceManager::ResourceType type
) {
104 ui_resource_provider_
.GetSystemUIResourceManager().PreloadResource(type
);
107 cc::UIResourceId
GetUIResourceId(
108 ui::SystemUIResourceManager::ResourceType type
) {
109 return ui_resource_provider_
.GetSystemUIResourceManager().GetUIResourceId(
113 void LayerTreeHostCleared() { ui_resource_provider_
.LayerTreeHostCleared(); }
115 void LayerTreeHostReturned() {
116 ui_resource_provider_
.LayerTreeHostReturned();
119 void SetResourceAsLoaded(ui::SystemUIResourceManager::ResourceType type
) {
120 ui_resource_provider_
.GetSystemUIResourceManager().SetResourceAsLoaded(
124 cc::UIResourceId
GetNextUIResourceId() const {
125 return ui_resource_provider_
.next_ui_resource_id();
129 MockUIResourceProvider ui_resource_provider_
;
132 TEST_F(SystemUIResourceManagerImplTest
, GetResourceAfterBitmapLoaded
) {
133 SetResourceAsLoaded(TEST_RESOURCE_TYPE
);
134 EXPECT_NE(0, GetUIResourceId(TEST_RESOURCE_TYPE
));
137 TEST_F(SystemUIResourceManagerImplTest
, GetResourceBeforeLoadBitmap
) {
138 EXPECT_EQ(0, GetUIResourceId(TEST_RESOURCE_TYPE
));
139 SetResourceAsLoaded(TEST_RESOURCE_TYPE
);
140 EXPECT_NE(0, GetUIResourceId(TEST_RESOURCE_TYPE
));
143 TEST_F(SystemUIResourceManagerImplTest
, PreloadEnsureResource
) {
144 // Preloading the resource should trigger bitmap loading, but the actual
145 // resource id will not be generated until it is explicitly requested.
146 cc::UIResourceId first_resource_id
= GetNextUIResourceId();
147 PreloadResource(TEST_RESOURCE_TYPE
);
148 SetResourceAsLoaded(TEST_RESOURCE_TYPE
);
149 EXPECT_EQ(first_resource_id
, GetNextUIResourceId());
150 EXPECT_NE(0, GetUIResourceId(TEST_RESOURCE_TYPE
));
151 EXPECT_NE(first_resource_id
, GetNextUIResourceId());
154 TEST_F(SystemUIResourceManagerImplTest
, ResetLayerTreeHost
) {
155 EXPECT_EQ(0, GetUIResourceId(TEST_RESOURCE_TYPE
));
156 LayerTreeHostCleared();
157 EXPECT_EQ(0, GetUIResourceId(TEST_RESOURCE_TYPE
));
158 LayerTreeHostReturned();
159 EXPECT_EQ(0, GetUIResourceId(TEST_RESOURCE_TYPE
));
161 SetResourceAsLoaded(TEST_RESOURCE_TYPE
);
162 EXPECT_NE(0, GetUIResourceId(TEST_RESOURCE_TYPE
));
163 LayerTreeHostCleared();
164 EXPECT_EQ(0, GetUIResourceId(TEST_RESOURCE_TYPE
));
165 LayerTreeHostReturned();
166 EXPECT_NE(0, GetUIResourceId(TEST_RESOURCE_TYPE
));
169 } // namespace content