1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "ash/wm/screen_dimmer.h" 6 7 #include "ash/root_window_controller.h" 8 #include "ash/shell.h" 9 #include "ash/test/ash_test_base.h" 10 #include "base/basictypes.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "ui/aura/window_event_dispatcher.h" 13 #include "ui/compositor/layer.h" 14 15 namespace ash { 16 namespace test { 17 18 class ScreenDimmerTest : public AshTestBase { 19 public: 20 ScreenDimmerTest() : dimmer_(NULL) {} 21 virtual ~ScreenDimmerTest() {} 22 23 virtual void SetUp() OVERRIDE { 24 AshTestBase::SetUp(); 25 dimmer_ = Shell::GetPrimaryRootWindowController()->screen_dimmer(); 26 test_api_.reset(new ScreenDimmer::TestApi(dimmer_)); 27 } 28 29 protected: 30 ScreenDimmer* dimmer_; // not owned 31 32 scoped_ptr<ScreenDimmer::TestApi> test_api_; 33 34 private: 35 DISALLOW_COPY_AND_ASSIGN(ScreenDimmerTest); 36 }; 37 38 TEST_F(ScreenDimmerTest, DimAndUndim) { 39 // Don't create a layer until we need to. 40 EXPECT_TRUE(test_api_->layer() == NULL); 41 dimmer_->SetDimming(false); 42 EXPECT_TRUE(test_api_->layer() == NULL); 43 44 // When we enable dimming, the layer should be created and stacked at the top 45 // of the root's children. 46 dimmer_->SetDimming(true); 47 ASSERT_TRUE(test_api_->layer() != NULL); 48 ui::Layer* root_layer = Shell::GetPrimaryRootWindow()->layer(); 49 ASSERT_TRUE(!root_layer->children().empty()); 50 EXPECT_EQ(test_api_->layer(), root_layer->children().back()); 51 EXPECT_TRUE(test_api_->layer()->visible()); 52 EXPECT_GT(test_api_->layer()->GetTargetOpacity(), 0.0f); 53 54 // When we disable dimming, the layer should be animated back to full 55 // transparency. 56 dimmer_->SetDimming(false); 57 ASSERT_TRUE(test_api_->layer() != NULL); 58 EXPECT_TRUE(test_api_->layer()->visible()); 59 EXPECT_FLOAT_EQ(0.0f, test_api_->layer()->GetTargetOpacity()); 60 } 61 62 TEST_F(ScreenDimmerTest, ResizeLayer) { 63 // The dimming layer should be initially sized to cover the root window. 64 dimmer_->SetDimming(true); 65 ui::Layer* dimming_layer = test_api_->layer(); 66 ASSERT_TRUE(dimming_layer != NULL); 67 ui::Layer* root_layer = Shell::GetPrimaryRootWindow()->layer(); 68 EXPECT_EQ(gfx::Rect(root_layer->bounds().size()).ToString(), 69 dimming_layer->bounds().ToString()); 70 71 // When we resize the root window, the dimming layer should be resized to 72 // match. 73 gfx::Rect kNewBounds(400, 300); 74 Shell::GetPrimaryRootWindow()->GetHost()->SetBounds(kNewBounds); 75 EXPECT_EQ(kNewBounds.ToString(), dimming_layer->bounds().ToString()); 76 } 77 78 } // namespace test 79 } // namespace ash 80