Chromium Code Reviews| 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 "base/thread_task_runner_handle.h" | |
| 5 #include "cc/resources/ui_resource_bitmap.h" | 6 #include "cc/resources/ui_resource_bitmap.h" |
| 7 #include "cc/trees/layer_tree_host.h" | |
| 8 #include "testing/gmock/include/gmock/gmock.h" | |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
| 8 #include "third_party/skia/include/core/SkCanvas.h" | 11 #include "third_party/skia/include/core/SkCanvas.h" |
| 9 #include "ui/android/resources/resource_manager_impl.h" | 12 #include "ui/android/resources/resource_manager_impl.h" |
| 10 #include "ui/android/resources/system_ui_resource_type.h" | 13 #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" | 14 #include "ui/gfx/android/java_bitmap.h" |
| 14 | 15 |
| 15 namespace ui { | 16 namespace ui { |
| 16 | 17 |
| 17 class TestResourceManagerImpl : public ResourceManagerImpl { | 18 class TestResourceManagerImpl : public ResourceManagerImpl { |
| 18 public: | 19 public: |
| 19 explicit TestResourceManagerImpl(UIResourceProvider* provider) | 20 TestResourceManagerImpl() {} |
| 20 : ResourceManagerImpl(provider) {} | |
| 21 | |
| 22 ~TestResourceManagerImpl() override {} | 21 ~TestResourceManagerImpl() override {} |
| 23 | 22 |
| 24 void SetResourceAsLoaded(AndroidResourceType res_type, int res_id) { | 23 void SetResourceAsLoaded(AndroidResourceType res_type, int res_id) { |
| 25 SkBitmap small_bitmap; | 24 SkBitmap small_bitmap; |
| 26 SkCanvas canvas(small_bitmap); | 25 SkCanvas canvas(small_bitmap); |
| 27 small_bitmap.allocPixels( | 26 small_bitmap.allocPixels( |
| 28 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType)); | 27 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType)); |
| 29 canvas.drawColor(SK_ColorWHITE); | 28 canvas.drawColor(SK_ColorWHITE); |
| 30 small_bitmap.setImmutable(); | 29 small_bitmap.setImmutable(); |
| 31 | 30 |
| 32 OnResourceReady(NULL, NULL, res_type, res_id, | 31 OnResourceReady(NULL, NULL, res_type, res_id, |
| 33 gfx::ConvertToJavaBitmap(&small_bitmap).obj(), 0, 0, 0, 0, | 32 gfx::ConvertToJavaBitmap(&small_bitmap).obj(), 0, 0, 0, 0, |
| 34 0, 0, 0, 0); | 33 0, 0, 0, 0); |
| 35 } | 34 } |
| 36 | 35 |
| 37 protected: | 36 protected: |
| 38 void PreloadResourceFromJava(AndroidResourceType res_type, | 37 void PreloadResourceFromJava(AndroidResourceType res_type, |
| 39 int res_id) override {} | 38 int res_id) override {} |
| 40 | 39 |
| 41 void RequestResourceFromJava(AndroidResourceType res_type, | 40 void RequestResourceFromJava(AndroidResourceType res_type, |
| 42 int res_id) override { | 41 int res_id) override { |
| 43 SetResourceAsLoaded(res_type, res_id); | 42 SetResourceAsLoaded(res_type, res_id); |
| 44 } | 43 } |
| 45 }; | 44 }; |
| 46 | 45 |
| 47 namespace { | 46 namespace { |
| 48 | 47 |
| 49 const ui::SystemUIResourceType kTestResourceType = ui::OVERSCROLL_GLOW; | 48 const ui::SystemUIResourceType kTestResourceType = ui::OVERSCROLL_GLOW; |
| 50 | 49 |
| 51 class MockUIResourceProvider : public ui::UIResourceProvider { | 50 cc::LayerTreeHost::InitParams g_init_params; |
| 51 | |
| 52 class MockLayerTreeHost : public cc::LayerTreeHost { | |
| 52 public: | 53 public: |
| 53 MockUIResourceProvider() | 54 MockLayerTreeHost() : cc::LayerTreeHost(&g_init_params) {} |
| 54 : next_ui_resource_id_(1), | |
| 55 has_layer_tree_host_(true), | |
| 56 resource_manager_(this) {} | |
| 57 | 55 |
| 58 virtual ~MockUIResourceProvider() {} | 56 MOCK_METHOD1(CreateUIResource, cc::UIResourceId(cc::UIResourceClient*)); |
|
jdduke (slow)
2015/09/30 17:19:44
Hopefully this is much cleaner =/
| |
| 59 | 57 MOCK_METHOD1(DeleteUIResource, void(cc::UIResourceId)); |
| 60 cc::UIResourceId CreateUIResource( | |
| 61 ui::UIResourceClientAndroid* client) override { | |
| 62 if (!has_layer_tree_host_) | |
| 63 return 0; | |
| 64 cc::UIResourceId id = next_ui_resource_id_++; | |
| 65 client->GetBitmap(id, false); | |
| 66 ui_resource_client_map_[id] = client; | |
| 67 return id; | |
| 68 } | |
| 69 | |
| 70 void DeleteUIResource(cc::UIResourceId id) override { | |
| 71 CHECK(has_layer_tree_host_); | |
| 72 ui_resource_client_map_.erase(id); | |
| 73 } | |
| 74 | |
| 75 bool SupportsETC1NonPowerOfTwo() const override { return true; } | |
| 76 | |
| 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(); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 void LayerTreeHostReturned() { has_layer_tree_host_ = true; } | |
| 88 | |
| 89 TestResourceManagerImpl& GetResourceManager() { return resource_manager_; } | |
| 90 | |
| 91 cc::UIResourceId next_ui_resource_id() const { return next_ui_resource_id_; } | |
| 92 | |
| 93 private: | |
| 94 typedef base::hash_map<cc::UIResourceId, ui::UIResourceClientAndroid*> | |
| 95 UIResourceClientMap; | |
| 96 | |
| 97 cc::UIResourceId next_ui_resource_id_; | |
| 98 UIResourceClientMap ui_resource_client_map_; | |
| 99 bool has_layer_tree_host_; | |
| 100 | |
| 101 // The UIResourceProvider owns the ResourceManager. | |
| 102 TestResourceManagerImpl resource_manager_; | |
| 103 }; | 58 }; |
| 104 | 59 |
| 105 } // namespace | 60 } // namespace |
| 106 | 61 |
| 107 class ResourceManagerTest : public testing::Test { | 62 class ResourceManagerTest : public testing::Test { |
| 108 public: | 63 public: |
| 64 ResourceManagerTest() { | |
| 65 resource_manager_.Init(&host_); | |
| 66 } | |
| 67 | |
| 109 void PreloadResource(ui::SystemUIResourceType type) { | 68 void PreloadResource(ui::SystemUIResourceType type) { |
| 110 ui_resource_provider_.GetResourceManager().PreloadResource( | 69 resource_manager_.PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM, type); |
| 111 ui::ANDROID_RESOURCE_TYPE_SYSTEM, type); | |
| 112 } | 70 } |
| 113 | 71 |
| 114 cc::UIResourceId GetUIResourceId(ui::SystemUIResourceType type) { | 72 cc::UIResourceId GetUIResourceId(ui::SystemUIResourceType type) { |
| 115 return ui_resource_provider_.GetResourceManager().GetUIResourceId( | 73 return resource_manager_.GetUIResourceId(ui::ANDROID_RESOURCE_TYPE_SYSTEM, |
| 116 ui::ANDROID_RESOURCE_TYPE_SYSTEM, type); | 74 type); |
| 117 } | |
| 118 | |
| 119 void LayerTreeHostCleared() { ui_resource_provider_.LayerTreeHostCleared(); } | |
| 120 | |
| 121 void LayerTreeHostReturned() { | |
| 122 ui_resource_provider_.LayerTreeHostReturned(); | |
| 123 } | 75 } |
| 124 | 76 |
| 125 void SetResourceAsLoaded(ui::SystemUIResourceType type) { | 77 void SetResourceAsLoaded(ui::SystemUIResourceType type) { |
| 126 ui_resource_provider_.GetResourceManager().SetResourceAsLoaded( | 78 resource_manager_.SetResourceAsLoaded(ui::ANDROID_RESOURCE_TYPE_SYSTEM, |
| 127 ui::ANDROID_RESOURCE_TYPE_SYSTEM, type); | 79 type); |
| 128 } | 80 } |
| 129 | 81 |
| 130 cc::UIResourceId GetNextUIResourceId() const { | 82 protected: |
| 131 return ui_resource_provider_.next_ui_resource_id(); | 83 TestResourceManagerImpl resource_manager_; |
| 132 } | 84 MockLayerTreeHost host_; |
| 133 | |
| 134 private: | |
| 135 MockUIResourceProvider ui_resource_provider_; | |
| 136 }; | 85 }; |
| 137 | 86 |
| 138 TEST_F(ResourceManagerTest, GetResource) { | 87 TEST_F(ResourceManagerTest, GetResource) { |
| 88 // TODO: |host_| expectations | |
| 139 EXPECT_NE(0, GetUIResourceId(kTestResourceType)); | 89 EXPECT_NE(0, GetUIResourceId(kTestResourceType)); |
| 140 } | 90 } |
| 141 | 91 |
| 142 TEST_F(ResourceManagerTest, PreloadEnsureResource) { | 92 TEST_F(ResourceManagerTest, PreloadEnsureResource) { |
| 143 // Preloading the resource should trigger bitmap loading, but the actual | 93 // TODO: |host_| expectations |
| 144 // resource id will not be generated until it is explicitly requested. | |
| 145 cc::UIResourceId first_resource_id = GetNextUIResourceId(); | |
| 146 PreloadResource(kTestResourceType); | 94 PreloadResource(kTestResourceType); |
| 147 SetResourceAsLoaded(kTestResourceType); | 95 SetResourceAsLoaded(kTestResourceType); |
| 148 EXPECT_EQ(first_resource_id, GetNextUIResourceId()); | |
| 149 EXPECT_NE(0, GetUIResourceId(kTestResourceType)); | |
| 150 EXPECT_NE(first_resource_id, GetNextUIResourceId()); | |
| 151 } | |
| 152 | |
| 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)); | 96 EXPECT_NE(0, GetUIResourceId(kTestResourceType)); |
| 159 } | 97 } |
| 160 | 98 |
| 161 } // namespace ui | 99 } // namespace ui |
| OLD | NEW |