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