File: shadow_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 (150 lines) | stat: -rw-r--r-- 5,076 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
// 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 "ui/wm/core/shadow.h"

#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_tree_owner.h"
#include "ui/resources/grit/ui_resources.h"

namespace wm {

namespace {

const int kSmallBitmapSize = 129;
const int kLargeBitmapSize = 269;

// Mock for the ResourceBundle::Delegate class.
class MockResourceBundleDelegate : public ui::ResourceBundle::Delegate {
 public:
  MockResourceBundleDelegate() : last_resource_id_(0) {
    SkBitmap bitmap_small, bitmap_large;
    bitmap_small.allocPixels(
        SkImageInfo::MakeN32Premul(kSmallBitmapSize, kSmallBitmapSize));
    bitmap_large.allocPixels(
        SkImageInfo::MakeN32Premul(kLargeBitmapSize, kLargeBitmapSize));
    image_small_ = gfx::Image::CreateFrom1xBitmap(bitmap_small);
    image_large_ = gfx::Image::CreateFrom1xBitmap(bitmap_large);
  }
  ~MockResourceBundleDelegate() override {}

  // ResourceBundle::Delegate:
  base::FilePath GetPathForResourcePack(const base::FilePath& pack_path,
                                        ui::ScaleFactor scale_factor) override {
    return base::FilePath();
  }
  base::FilePath GetPathForLocalePack(const base::FilePath& pack_path,
                                      const std::string& locale) override {
    return base::FilePath();
  }
  gfx::Image GetImageNamed(int resource_id) override {
    last_resource_id_ = resource_id;
    switch (resource_id) {
      case IDR_WINDOW_BUBBLE_SHADOW_SMALL:
        return image_small_;
      case IDR_AURA_SHADOW_ACTIVE:
      case IDR_AURA_SHADOW_INACTIVE:
        return image_large_;
      default:
        NOTREACHED();
        return gfx::Image();
    }
  }
  gfx::Image GetNativeImageNamed(int resource_id,
                                 ui::ResourceBundle::ImageRTL rtl) override {
    return gfx::Image();
  }
  base::RefCountedStaticMemory* LoadDataResourceBytes(
      int resource_id,
      ui::ScaleFactor scale_factor) override {
    return NULL;
  }
  bool GetRawDataResource(int resource_id,
                          ui::ScaleFactor scale_factor,
                          base::StringPiece* value) override {
    return false;
  }
  bool GetLocalizedString(int message_id, base::string16* value) override {
    return false;
  }
  scoped_ptr<gfx::Font> GetFont(ui::ResourceBundle::FontStyle style) override {
    return nullptr;
  }

  int last_resource_id() const { return last_resource_id_; }

 private:
  gfx::Image image_small_;
  gfx::Image image_large_;
  int last_resource_id_;

  DISALLOW_COPY_AND_ASSIGN(MockResourceBundleDelegate);
};

}  // namespace

class ShadowTest: public aura::test::AuraTestBase {
 public:
  ShadowTest() {}
  ~ShadowTest() override {}

  MockResourceBundleDelegate* delegate() { return delegate_.get(); }

  // aura::testAuraBase:
  void SetUp() override {
    aura::test::AuraTestBase::SetUp();
    delegate_.reset(new MockResourceBundleDelegate());
    if (ResourceBundle::HasSharedInstance())
      ui::ResourceBundle::CleanupSharedInstance();
    ui::ResourceBundle::InitSharedInstanceWithLocale(
        "en-US", delegate(), ui::ResourceBundle::LOAD_COMMON_RESOURCES);
  }
  void TearDown() override {
    ui::ResourceBundle::CleanupSharedInstance();
    base::FilePath ui_test_pak_path;
    ASSERT_TRUE(PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path));
    ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path);
    aura::test::AuraTestBase::TearDown();
  }
 private:
  scoped_ptr<MockResourceBundleDelegate> delegate_;
  DISALLOW_COPY_AND_ASSIGN(ShadowTest);
};

// Test if the proper image is set for the specified style.
TEST_F(ShadowTest, UpdateImagesForStyle) {
  Shadow shadow;

  shadow.Init(Shadow::STYLE_SMALL);
  EXPECT_EQ(delegate()->last_resource_id(), IDR_WINDOW_BUBBLE_SHADOW_SMALL);
  shadow.SetStyle(Shadow::STYLE_ACTIVE);
  EXPECT_EQ(delegate()->last_resource_id(), IDR_AURA_SHADOW_ACTIVE);
  shadow.SetStyle(Shadow::STYLE_INACTIVE);
  EXPECT_EQ(delegate()->last_resource_id(), IDR_AURA_SHADOW_INACTIVE);
}

// Test if the proper content bounds is calculated based on the current style.
TEST_F(ShadowTest, SetContentBounds) {
  Shadow shadow;

  // Verify that layer bounds are inset from content bounds.
  shadow.Init(Shadow::STYLE_ACTIVE);
  gfx::Rect content_bounds(100, 100, 300, 300);
  shadow.SetContentBounds(content_bounds);
  EXPECT_EQ(shadow.content_bounds(), content_bounds);
  EXPECT_EQ(shadow.layer()->bounds(), gfx::Rect(36, 36, 428, 428));

  shadow.SetStyle(Shadow::STYLE_SMALL);
  EXPECT_EQ(shadow.content_bounds(), content_bounds);
  EXPECT_EQ(shadow.layer()->bounds(), gfx::Rect(96, 96, 308, 308));
}
}  // namespace wm