summaryrefslogtreecommitdiff
path: root/chromium/ash/wm/screen_dimmer_unittest.cc
blob: a87c71bf08844b0aaca43dd05a0944be7c5ba2b7 (plain)
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
// Copyright (c) 2012 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 "ash/wm/screen_dimmer.h"

#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "ui/aura/root_window.h"
#include "ui/compositor/layer.h"

namespace ash {
namespace test {

class ScreenDimmerTest : public AshTestBase {
 public:
  ScreenDimmerTest() : dimmer_(NULL) {}
  virtual ~ScreenDimmerTest() {}

  virtual void SetUp() OVERRIDE {
    AshTestBase::SetUp();
    dimmer_ = Shell::GetPrimaryRootWindowController()->screen_dimmer();
    test_api_.reset(new internal::ScreenDimmer::TestApi(dimmer_));
  }

 protected:
  internal::ScreenDimmer* dimmer_;  // not owned

  scoped_ptr<internal::ScreenDimmer::TestApi> test_api_;

 private:
  DISALLOW_COPY_AND_ASSIGN(ScreenDimmerTest);
};

TEST_F(ScreenDimmerTest, DimAndUndim) {
  // Don't create a layer until we need to.
  EXPECT_TRUE(test_api_->layer() == NULL);
  dimmer_->SetDimming(false);
  EXPECT_TRUE(test_api_->layer() == NULL);

  // When we enable dimming, the layer should be created and stacked at the top
  // of the root's children.
  dimmer_->SetDimming(true);
  ASSERT_TRUE(test_api_->layer() != NULL);
  ui::Layer* root_layer = Shell::GetPrimaryRootWindow()->layer();
  ASSERT_TRUE(!root_layer->children().empty());
  EXPECT_EQ(test_api_->layer(), root_layer->children().back());
  EXPECT_TRUE(test_api_->layer()->visible());
  EXPECT_GT(test_api_->layer()->GetTargetOpacity(), 0.0f);

  // When we disable dimming, the layer should be animated back to full
  // transparency.
  dimmer_->SetDimming(false);
  ASSERT_TRUE(test_api_->layer() != NULL);
  EXPECT_TRUE(test_api_->layer()->visible());
  EXPECT_FLOAT_EQ(0.0f, test_api_->layer()->GetTargetOpacity());
}

TEST_F(ScreenDimmerTest, ResizeLayer) {
  // The dimming layer should be initially sized to cover the root window.
  dimmer_->SetDimming(true);
  ui::Layer* dimming_layer = test_api_->layer();
  ASSERT_TRUE(dimming_layer != NULL);
  ui::Layer* root_layer = Shell::GetPrimaryRootWindow()->layer();
  EXPECT_EQ(gfx::Rect(root_layer->bounds().size()).ToString(),
            dimming_layer->bounds().ToString());

  // When we resize the root window, the dimming layer should be resized to
  // match.
  gfx::Size kNewSize(400, 300);
  Shell::GetPrimaryRootWindow()->GetDispatcher()->SetHostSize(kNewSize);
  EXPECT_EQ(kNewSize.ToString(), dimming_layer->bounds().size().ToString());
}

}  // namespace test
}  // namespace ash