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