autotest_ambient_api.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2020 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/public/cpp/autotest_ambient_api.h"
  5. #include "ash/ambient/ambient_controller.h"
  6. #include "ash/ambient/model/ambient_photo_config.h"
  7. #include "ash/ambient/ui/ambient_view_delegate.h"
  8. #include "ash/public/cpp/ambient/ambient_backend_controller.h"
  9. #include "ash/shell.h"
  10. #include "base/bind.h"
  11. #include "base/callback.h"
  12. #include "base/time/time.h"
  13. #include "base/timer/timer.h"
  14. namespace ash {
  15. namespace {
  16. class PhotoTransitionAnimationObserver : public AmbientViewDelegateObserver {
  17. public:
  18. PhotoTransitionAnimationObserver(int num_completions,
  19. base::TimeDelta timeout,
  20. base::OnceClosure on_complete,
  21. base::OnceClosure on_timeout)
  22. : num_completions_(num_completions),
  23. on_complete_(std::move(on_complete)),
  24. on_timeout_(std::move(on_timeout)) {
  25. DCHECK_GT(num_completions, 0);
  26. DCHECK_GT(timeout, base::TimeDelta());
  27. DCHECK(on_complete_);
  28. DCHECK(on_timeout_);
  29. // |base::Unretained| is safe here because this timer will be abandoned in
  30. // the destructor.
  31. timer_.Start(FROM_HERE, timeout,
  32. base::BindOnce(&PhotoTransitionAnimationObserver::OnTimeout,
  33. base::Unretained(this)));
  34. scoped_observation_.Observe(
  35. Shell::Get()->ambient_controller()->ambient_view_delegate());
  36. }
  37. PhotoTransitionAnimationObserver(const PhotoTransitionAnimationObserver&) =
  38. delete;
  39. PhotoTransitionAnimationObserver& operator=(
  40. const PhotoTransitionAnimationObserver&) = delete;
  41. ~PhotoTransitionAnimationObserver() override = default;
  42. // AmbientViewDelegateObserver:
  43. void OnMarkerHit(AmbientPhotoConfig::Marker marker) override {
  44. if (marker != AmbientPhotoConfig::Marker::kUiCycleEnded)
  45. return;
  46. --num_completions_;
  47. if (num_completions_ == 0) {
  48. Cleanup();
  49. std::move(on_complete_).Run();
  50. delete this;
  51. }
  52. }
  53. private:
  54. void OnTimeout() {
  55. Cleanup();
  56. std::move(on_timeout_).Run();
  57. delete this;
  58. }
  59. void Cleanup() {
  60. timer_.AbandonAndStop();
  61. scoped_observation_.Reset();
  62. }
  63. int num_completions_;
  64. base::OnceClosure on_complete_;
  65. base::OnceClosure on_timeout_;
  66. base::OneShotTimer timer_;
  67. base::ScopedObservation<AmbientViewDelegate, AmbientViewDelegateObserver>
  68. scoped_observation_{this};
  69. };
  70. } // namespace
  71. AutotestAmbientApi::AutotestAmbientApi() = default;
  72. AutotestAmbientApi::~AutotestAmbientApi() = default;
  73. void AutotestAmbientApi::WaitForPhotoTransitionAnimationCompleted(
  74. int num_completions,
  75. base::TimeDelta timeout,
  76. base::OnceClosure on_complete,
  77. base::OnceClosure on_timeout) {
  78. new PhotoTransitionAnimationObserver(
  79. num_completions, timeout, std::move(on_complete), std::move(on_timeout));
  80. }
  81. } // namespace ash