RecordTestUtils.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef RecordTestUtils_DEFINED
  8. #define RecordTestUtils_DEFINED
  9. #include "src/core/SkRecord.h"
  10. #include "src/core/SkRecords.h"
  11. #include "tests/Test.h"
  12. // If the command we're reading is a U, set ptr to it, otherwise set it to nullptr.
  13. template <typename U>
  14. struct ReadAs {
  15. ReadAs() : ptr(nullptr), type(SkRecords::Type(~0)) {}
  16. const U* ptr;
  17. SkRecords::Type type;
  18. void operator()(const U& r) { ptr = &r; type = U::kType; }
  19. template <typename T>
  20. void operator()(const T&) { type = U::kType; }
  21. };
  22. // Assert that the ith command in record is of type T, and return it.
  23. template <typename T>
  24. static const T* assert_type(skiatest::Reporter* r, const SkRecord& record, int index) {
  25. ReadAs<T> reader;
  26. record.visit(index, reader);
  27. REPORTER_ASSERT(r, T::kType == reader.type);
  28. REPORTER_ASSERT(r, SkToBool(reader.ptr));
  29. return reader.ptr;
  30. }
  31. template <typename DrawT> struct MatchType {
  32. template <typename T> int operator()(const T&) { return 0; }
  33. int operator()(const DrawT&) { return 1; }
  34. };
  35. template <typename DrawT> int count_instances_of_type(const SkRecord& record) {
  36. MatchType<DrawT> matcher;
  37. int counter = 0;
  38. for (int i = 0; i < record.count(); i++) {
  39. counter += record.visit(i, matcher);
  40. }
  41. return counter;
  42. }
  43. template <typename DrawT> int find_first_instances_of_type(const SkRecord& record) {
  44. MatchType<DrawT> matcher;
  45. for (int i = 0; i < record.count(); i++) {
  46. if (record.visit(i, matcher)) {
  47. return i;
  48. }
  49. }
  50. return -1;
  51. }
  52. #endif//RecordTestUtils_DEFINED