test_utils.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include "ui/compositor/test/test_utils.h"
  5. #include "base/cancelable_callback.h"
  6. #include "base/run_loop.h"
  7. #include "base/test/bind.h"
  8. #include "base/timer/timer.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "ui/compositor/compositor.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. #include "ui/gfx/geometry/rounded_corners_f.h"
  13. #include "ui/gfx/geometry/transform.h"
  14. namespace ui {
  15. // TODO(avallee): Use macros in ui/gfx/geometry/test/geometry_util.h.
  16. void CheckApproximatelyEqual(const gfx::Transform& lhs,
  17. const gfx::Transform& rhs) {
  18. unsigned int errors = 0;
  19. for (int i = 0; i < 4; ++i) {
  20. for (int j = 0; j < 4; ++j) {
  21. EXPECT_FLOAT_EQ(lhs.matrix().rc(i, j), rhs.matrix().rc(i, j))
  22. << "(i, j) = (" << i << ", " << j << "), error count: " << ++errors;
  23. }
  24. }
  25. if (errors) {
  26. ADD_FAILURE() << "Expected matrix:\n"
  27. << lhs.ToString() << "\n"
  28. << "Actual matrix:\n"
  29. << rhs.ToString();
  30. }
  31. }
  32. void CheckApproximatelyEqual(const gfx::Rect& lhs, const gfx::Rect& rhs) {
  33. EXPECT_FLOAT_EQ(lhs.x(), rhs.x());
  34. EXPECT_FLOAT_EQ(lhs.y(), rhs.y());
  35. EXPECT_FLOAT_EQ(lhs.width(), rhs.width());
  36. EXPECT_FLOAT_EQ(lhs.height(), rhs.height());
  37. }
  38. void CheckApproximatelyEqual(const gfx::RoundedCornersF& lhs,
  39. const gfx::RoundedCornersF& rhs) {
  40. EXPECT_FLOAT_EQ(lhs.upper_left(), rhs.upper_left());
  41. EXPECT_FLOAT_EQ(lhs.upper_right(), rhs.upper_right());
  42. EXPECT_FLOAT_EQ(lhs.lower_left(), rhs.lower_left());
  43. EXPECT_FLOAT_EQ(lhs.lower_right(), rhs.lower_right());
  44. }
  45. bool WaitForNextFrameToBePresented(ui::Compositor* compositor,
  46. absl::optional<base::TimeDelta> timeout) {
  47. bool frames_presented = false;
  48. base::RunLoop runloop;
  49. base::CancelableOnceCallback<void(const gfx::PresentationFeedback&)>
  50. cancelable_callback(base::BindLambdaForTesting(
  51. [&](const gfx::PresentationFeedback& feedback) {
  52. frames_presented = true;
  53. runloop.Quit();
  54. }));
  55. compositor->RequestPresentationTimeForNextFrame(
  56. cancelable_callback.callback());
  57. absl::optional<base::OneShotTimer> timer;
  58. if (timeout.has_value()) {
  59. timer.emplace();
  60. timer->Start(FROM_HERE, timeout.value(), runloop.QuitClosure());
  61. }
  62. runloop.Run();
  63. return frames_presented;
  64. }
  65. } // namespace ui