File: resource_manager_impl_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (161 lines) | stat: -rw-r--r-- 5,223 bytes parent folder | download
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