test_task.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2016 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. #ifndef COMPONENTS_OFFLINE_PAGES_TASK_TEST_TASK_H_
  5. #define COMPONENTS_OFFLINE_PAGES_TASK_TEST_TASK_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "components/offline_pages/task/task.h"
  8. namespace offline_pages {
  9. // Sample resource consumed by the task during execution. In this set of tests
  10. // used to provide the capability to continue the task.
  11. class ConsumedResource {
  12. public:
  13. ConsumedResource();
  14. ~ConsumedResource();
  15. void Step(base::OnceClosure step_callback);
  16. void CompleteStep();
  17. bool HasNextStep() const { return !next_step_.is_null(); }
  18. private:
  19. base::OnceClosure next_step_;
  20. };
  21. // Sample test task. This should not be used as a example of task implementation
  22. // with respect to callback safety. Otherwise it captures the idea of splitting
  23. // the work into multiple steps separated by potentially asynchronous calls
  24. // spanning multiple threads.
  25. //
  26. // For an implementation example of a task that covers problems better is
  27. // |offline_pages::ChangeRequestsStateTask|.
  28. class TestTask : public Task {
  29. public:
  30. enum class TaskState { NOT_STARTED, STEP_1, STEP_2, COMPLETED };
  31. explicit TestTask(ConsumedResource* resource);
  32. TestTask(ConsumedResource* resource, bool leave_early);
  33. ~TestTask() override;
  34. // Run is Step 1 in our case.
  35. void Run() override;
  36. void Step2();
  37. void LastStep();
  38. TaskState state() const { return state_; }
  39. private:
  40. raw_ptr<ConsumedResource> resource_;
  41. TaskState state_;
  42. bool leave_early_;
  43. };
  44. } // namespace offline_pages
  45. #endif // COMPONENTS_OFFLINE_PAGES_TASK_TEST_TASK_H_