assistant_screen_context_controller_impl_unittest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2018 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 "ash/assistant/assistant_screen_context_controller_impl.h"
  5. #include <memory>
  6. #include "ash/assistant/assistant_controller_impl.h"
  7. #include "ash/assistant/test/assistant_ash_test_base.h"
  8. #include "ash/public/cpp/shell_window_ids.h"
  9. #include "ash/shell.h"
  10. #include "ash/wm/desks/desks_util.h"
  11. #include "base/bind.h"
  12. #include "base/run_loop.h"
  13. #include "base/test/task_environment.h"
  14. #include "chromeos/ui/base/window_properties.h"
  15. #include "ui/aura/window.h"
  16. #include "ui/compositor/layer.h"
  17. #include "ui/compositor/layer_tree_owner.h"
  18. #include "ui/compositor/layer_type.h"
  19. namespace ash {
  20. namespace {
  21. ui::Layer* FindLayerWithClosure(
  22. ui::Layer* root,
  23. const base::RepeatingCallback<bool(ui::Layer*)>& callback) {
  24. if (callback.Run(root))
  25. return root;
  26. for (ui::Layer* child : root->children()) {
  27. ui::Layer* result = FindLayerWithClosure(child, callback);
  28. if (result)
  29. return result;
  30. }
  31. return nullptr;
  32. }
  33. } // namespace
  34. class AssistantScreenContextControllerTest : public AssistantAshTestBase {
  35. public:
  36. AssistantScreenContextControllerTest(
  37. const AssistantScreenContextControllerTest&) = delete;
  38. AssistantScreenContextControllerTest& operator=(
  39. const AssistantScreenContextControllerTest&) = delete;
  40. protected:
  41. AssistantScreenContextControllerTest()
  42. : AssistantAshTestBase(
  43. base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  44. ~AssistantScreenContextControllerTest() override = default;
  45. void SetUp() override {
  46. AssistantAshTestBase::SetUp();
  47. controller_ =
  48. Shell::Get()->assistant_controller()->screen_context_controller();
  49. DCHECK(controller_);
  50. }
  51. AssistantScreenContextControllerImpl* controller() { return controller_; }
  52. private:
  53. AssistantScreenContextControllerImpl* controller_ = nullptr;
  54. };
  55. // Verify that incognito windows are blocked in screenshot.
  56. TEST_F(AssistantScreenContextControllerTest, Screenshot) {
  57. std::unique_ptr<aura::Window> window1 = CreateToplevelTestWindow(
  58. gfx::Rect(0, 0, 200, 200), desks_util::GetActiveDeskContainerId());
  59. std::unique_ptr<aura::Window> window2 = CreateToplevelTestWindow(
  60. gfx::Rect(30, 30, 100, 100), desks_util::GetActiveDeskContainerId());
  61. ui::Layer* window1_layer = window1->layer();
  62. ui::Layer* window2_layer = window2->layer();
  63. window1->SetProperty(chromeos::kBlockedForAssistantSnapshotKey, true);
  64. std::unique_ptr<ui::LayerTreeOwner> layer_owner =
  65. controller()->CreateLayerForAssistantSnapshotForTest();
  66. // Test that windows marked as blocked for assistant snapshot is not included.
  67. EXPECT_FALSE(FindLayerWithClosure(
  68. layer_owner->root(), base::BindRepeating(
  69. [](ui::Layer* layer, ui::Layer* mirror) {
  70. return layer->ContainsMirrorForTest(mirror);
  71. },
  72. window1_layer)));
  73. EXPECT_TRUE(FindLayerWithClosure(
  74. layer_owner->root(), base::BindRepeating(
  75. [](ui::Layer* layer, ui::Layer* mirror) {
  76. return layer->ContainsMirrorForTest(mirror);
  77. },
  78. window2_layer)));
  79. // Test that a solid black layer is inserted.
  80. EXPECT_TRUE(FindLayerWithClosure(
  81. layer_owner->root(), base::BindRepeating([](ui::Layer* layer) {
  82. return layer->type() == ui::LayerType::LAYER_SOLID_COLOR;
  83. })));
  84. }
  85. } // namespace ash