layer_dimmer_unittest.cc 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2017 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. #include "components/javascript_dialogs/views/layer_dimmer.h"
  5. #include <memory>
  6. #include "build/build_config.h"
  7. #include "testing/gmock/include/gmock/gmock.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "ui/aura/client/window_types.h"
  10. #include "ui/aura/window.h"
  11. #include "ui/compositor/layer.h"
  12. #include "ui/compositor/layer_type.h"
  13. #include "ui/gfx/geometry/rect.h"
  14. namespace javascript_dialogs {
  15. class LayerDimmerTest : public testing::Test {
  16. protected:
  17. void SetUp() override {
  18. parentWindow_ = std::make_unique<aura::Window>(
  19. nullptr, aura::client::WINDOW_TYPE_NORMAL);
  20. contentWindow_ = std::make_unique<aura::Window>(
  21. nullptr, aura::client::WINDOW_TYPE_NORMAL);
  22. dialogWindow_ = std::make_unique<aura::Window>(
  23. nullptr, aura::client::WINDOW_TYPE_POPUP);
  24. // Set up the windows' layers
  25. parentWindow_->Init(ui::LAYER_NOT_DRAWN);
  26. parentWindow_->SetBounds(gfx::Rect(1000, 500));
  27. contentWindow_->Init(ui::LAYER_TEXTURED);
  28. contentWindow_->SetBounds(gfx::Rect(1000, 500));
  29. parentWindow_->AddChild(contentWindow_.get());
  30. dialogWindow_->Init(ui::LAYER_TEXTURED);
  31. dialogWindow_->SetBounds(gfx::Rect(400, 100));
  32. parentWindow_->AddChild(dialogWindow_.get());
  33. layerDimmer_ =
  34. std::make_unique<LayerDimmer>(parentWindow_.get(), dialogWindow_.get());
  35. }
  36. std::unique_ptr<aura::Window> parentWindow_;
  37. std::unique_ptr<aura::Window> contentWindow_;
  38. std::unique_ptr<aura::Window> dialogWindow_;
  39. std::unique_ptr<LayerDimmer> layerDimmer_;
  40. };
  41. TEST_F(LayerDimmerTest, TestBoundsChange) {
  42. gfx::Rect parentBounds = parentWindow_->layer()->bounds();
  43. gfx::Rect dimmerBounds = layerDimmer_->GetLayerForTest()->bounds();
  44. EXPECT_TRUE(dimmerBounds.ApproximatelyEqual(parentBounds, 0));
  45. // Simulate a resize
  46. const gfx::Rect newBounds(950, 470);
  47. parentWindow_->SetBounds(newBounds);
  48. parentBounds = parentWindow_->layer()->bounds();
  49. dimmerBounds = layerDimmer_->GetLayerForTest()->bounds();
  50. EXPECT_TRUE(dimmerBounds.ApproximatelyEqual(newBounds, 0));
  51. }
  52. TEST_F(LayerDimmerTest, TestShowHide) {
  53. EXPECT_FLOAT_EQ(layerDimmer_->GetLayerForTest()->GetTargetOpacity(), 0.f);
  54. layerDimmer_->Show();
  55. EXPECT_FLOAT_EQ(layerDimmer_->GetLayerForTest()->GetTargetOpacity(), 1.f);
  56. layerDimmer_->Hide();
  57. EXPECT_FLOAT_EQ(layerDimmer_->GetLayerForTest()->GetTargetOpacity(), 0.f);
  58. }
  59. TEST_F(LayerDimmerTest, TestLayerOrder) {
  60. // Layer order should be correct after creating the LayerDimmer.
  61. // (The last child is on top)
  62. auto childLayers = parentWindow_->layer()->children();
  63. EXPECT_THAT(childLayers, testing::ElementsAre(contentWindow_->layer(),
  64. layerDimmer_->GetLayerForTest(),
  65. dialogWindow_->layer()));
  66. // Simulate stacking change which could re-order the layers. This can happen
  67. // when the user clicks on the dialog window.
  68. parentWindow_->layer()->StackAtBottom(layerDimmer_->GetLayerForTest());
  69. layerDimmer_->OnWindowStackingChanged(dialogWindow_.get());
  70. // Verify order is still the same
  71. childLayers = parentWindow_->layer()->children();
  72. EXPECT_THAT(childLayers, testing::ElementsAre(contentWindow_->layer(),
  73. layerDimmer_->GetLayerForTest(),
  74. dialogWindow_->layer()));
  75. }
  76. } // namespace javascript_dialogs