OLD | NEW |
| (Empty) |
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. | |
4 | |
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" | |
12 | |
13 namespace content { | |
14 | |
15 class TestSystemUIResourceManagerImpl | |
16 : public content::SystemUIResourceManagerImpl { | |
17 public: | |
18 TestSystemUIResourceManagerImpl(content::UIResourceProvider* provider) | |
19 : SystemUIResourceManagerImpl(provider) {} | |
20 | |
21 virtual ~TestSystemUIResourceManagerImpl() {} | |
22 | |
23 virtual void BuildResource(ui::SystemUIResourceType type) override {} | |
24 | |
25 void SetResourceAsLoaded(ui::SystemUIResourceType type) { | |
26 SkBitmap small_bitmap; | |
27 SkCanvas canvas(small_bitmap); | |
28 small_bitmap.allocPixels( | |
29 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType)); | |
30 canvas.drawColor(SK_ColorWHITE); | |
31 small_bitmap.setImmutable(); | |
32 OnFinishedLoadBitmap(type, &small_bitmap); | |
33 } | |
34 }; | |
35 | |
36 namespace { | |
37 | |
38 const ui::SystemUIResourceType kTestResourceType = ui::OVERSCROLL_GLOW; | |
39 | |
40 class MockUIResourceProvider : public content::UIResourceProvider { | |
41 public: | |
42 MockUIResourceProvider() | |
43 : next_ui_resource_id_(1), | |
44 has_layer_tree_host_(true), | |
45 system_ui_resource_manager_(this) {} | |
46 | |
47 virtual ~MockUIResourceProvider() {} | |
48 | |
49 virtual cc::UIResourceId CreateUIResource( | |
50 content::UIResourceClientAndroid* client) override { | |
51 if (!has_layer_tree_host_) | |
52 return 0; | |
53 cc::UIResourceId id = next_ui_resource_id_++; | |
54 client->GetBitmap(id, false); | |
55 ui_resource_client_map_[id] = client; | |
56 return id; | |
57 } | |
58 | |
59 virtual void DeleteUIResource(cc::UIResourceId id) override { | |
60 CHECK(has_layer_tree_host_); | |
61 ui_resource_client_map_.erase(id); | |
62 } | |
63 | |
64 virtual bool SupportsETC1NonPowerOfTwo() const override { return true; } | |
65 | |
66 void LayerTreeHostCleared() { | |
67 has_layer_tree_host_ = false; | |
68 UIResourceClientMap client_map = ui_resource_client_map_; | |
69 ui_resource_client_map_.clear(); | |
70 for (UIResourceClientMap::iterator iter = client_map.begin(); | |
71 iter != client_map.end(); | |
72 iter++) { | |
73 iter->second->UIResourceIsInvalid(); | |
74 } | |
75 } | |
76 | |
77 void LayerTreeHostReturned() { has_layer_tree_host_ = true; } | |
78 | |
79 TestSystemUIResourceManagerImpl& GetSystemUIResourceManager() { | |
80 return system_ui_resource_manager_; | |
81 } | |
82 | |
83 cc::UIResourceId next_ui_resource_id() const { return next_ui_resource_id_; } | |
84 | |
85 private: | |
86 typedef base::hash_map<cc::UIResourceId, content::UIResourceClientAndroid*> | |
87 UIResourceClientMap; | |
88 | |
89 cc::UIResourceId next_ui_resource_id_; | |
90 UIResourceClientMap ui_resource_client_map_; | |
91 bool has_layer_tree_host_; | |
92 | |
93 // The UIResourceProvider owns the SystemUIResourceManager. | |
94 TestSystemUIResourceManagerImpl system_ui_resource_manager_; | |
95 }; | |
96 | |
97 } // namespace | |
98 | |
99 class SystemUIResourceManagerImplTest : public testing::Test { | |
100 public: | |
101 void PreloadResource(ui::SystemUIResourceType type) { | |
102 ui_resource_provider_.GetSystemUIResourceManager().PreloadResource(type); | |
103 } | |
104 | |
105 cc::UIResourceId GetUIResourceId(ui::SystemUIResourceType type) { | |
106 return ui_resource_provider_.GetSystemUIResourceManager().GetUIResourceId( | |
107 type); | |
108 } | |
109 | |
110 void LayerTreeHostCleared() { ui_resource_provider_.LayerTreeHostCleared(); } | |
111 | |
112 void LayerTreeHostReturned() { | |
113 ui_resource_provider_.LayerTreeHostReturned(); | |
114 } | |
115 | |
116 void SetResourceAsLoaded(ui::SystemUIResourceType type) { | |
117 ui_resource_provider_.GetSystemUIResourceManager().SetResourceAsLoaded( | |
118 type); | |
119 } | |
120 | |
121 cc::UIResourceId GetNextUIResourceId() const { | |
122 return ui_resource_provider_.next_ui_resource_id(); | |
123 } | |
124 | |
125 private: | |
126 MockUIResourceProvider ui_resource_provider_; | |
127 }; | |
128 | |
129 TEST_F(SystemUIResourceManagerImplTest, GetResourceAfterBitmapLoaded) { | |
130 SetResourceAsLoaded(kTestResourceType); | |
131 EXPECT_NE(0, GetUIResourceId(kTestResourceType)); | |
132 } | |
133 | |
134 TEST_F(SystemUIResourceManagerImplTest, GetResourceBeforeLoadBitmap) { | |
135 EXPECT_EQ(0, GetUIResourceId(kTestResourceType)); | |
136 SetResourceAsLoaded(kTestResourceType); | |
137 EXPECT_NE(0, GetUIResourceId(kTestResourceType)); | |
138 } | |
139 | |
140 TEST_F(SystemUIResourceManagerImplTest, PreloadEnsureResource) { | |
141 // Preloading the resource should trigger bitmap loading, but the actual | |
142 // resource id will not be generated until it is explicitly requested. | |
143 cc::UIResourceId first_resource_id = GetNextUIResourceId(); | |
144 PreloadResource(kTestResourceType); | |
145 SetResourceAsLoaded(kTestResourceType); | |
146 EXPECT_EQ(first_resource_id, GetNextUIResourceId()); | |
147 EXPECT_NE(0, GetUIResourceId(kTestResourceType)); | |
148 EXPECT_NE(first_resource_id, GetNextUIResourceId()); | |
149 } | |
150 | |
151 TEST_F(SystemUIResourceManagerImplTest, ResetLayerTreeHost) { | |
152 EXPECT_EQ(0, GetUIResourceId(kTestResourceType)); | |
153 LayerTreeHostCleared(); | |
154 EXPECT_EQ(0, GetUIResourceId(kTestResourceType)); | |
155 LayerTreeHostReturned(); | |
156 EXPECT_EQ(0, GetUIResourceId(kTestResourceType)); | |
157 | |
158 SetResourceAsLoaded(kTestResourceType); | |
159 EXPECT_NE(0, GetUIResourceId(kTestResourceType)); | |
160 LayerTreeHostCleared(); | |
161 EXPECT_EQ(0, GetUIResourceId(kTestResourceType)); | |
162 LayerTreeHostReturned(); | |
163 EXPECT_NE(0, GetUIResourceId(kTestResourceType)); | |
164 } | |
165 | |
166 } // namespace content | |
OLD | NEW |