fake_gcm_profile_service.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2013 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_GCM_DRIVER_FAKE_GCM_PROFILE_SERVICE_H_
  5. #define COMPONENTS_GCM_DRIVER_FAKE_GCM_PROFILE_SERVICE_H_
  6. #include <list>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "components/gcm_driver/common/gcm_message.h"
  11. #include "components/gcm_driver/gcm_client.h"
  12. #include "components/gcm_driver/gcm_profile_service.h"
  13. namespace content {
  14. class BrowserContext;
  15. } // namespace content
  16. namespace gcm {
  17. // Acts as a bridge between GCM API and GCM Client layer for testing purposes.
  18. class FakeGCMProfileService : public GCMProfileService {
  19. public:
  20. // Helper function to be used with KeyedServiceFactory::SetTestingFactory().
  21. static std::unique_ptr<KeyedService> Build(content::BrowserContext* context);
  22. FakeGCMProfileService();
  23. FakeGCMProfileService(const FakeGCMProfileService&) = delete;
  24. FakeGCMProfileService& operator=(const FakeGCMProfileService&) = delete;
  25. ~FakeGCMProfileService() override;
  26. void AddExpectedUnregisterResponse(GCMClient::Result result);
  27. void DispatchMessage(const std::string& app_id,
  28. const IncomingMessage& message);
  29. const OutgoingMessage& last_sent_message() const {
  30. return last_sent_message_;
  31. }
  32. const std::string& last_receiver_id() const { return last_receiver_id_; }
  33. const std::string& last_registered_app_id() const {
  34. return last_registered_app_id_;
  35. }
  36. const std::vector<std::string>& last_registered_sender_ids() const {
  37. return last_registered_sender_ids_;
  38. }
  39. // Set whether the service will collect parameters of the calls for further
  40. // verification in tests.
  41. void set_collect(bool collect) { collect_ = collect; }
  42. // Crude offline simulation: requests fail and never run their callbacks (in
  43. // reality, callbacks run within GetGCMBackoffPolicy().maximum_backoff_ms).
  44. void set_offline(bool is_offline) { is_offline_ = is_offline; }
  45. private:
  46. class CustomFakeGCMDriver;
  47. friend class CustomFakeGCMDriver;
  48. bool collect_ = false;
  49. bool is_offline_ = false;
  50. std::string last_registered_app_id_;
  51. std::vector<std::string> last_registered_sender_ids_;
  52. std::list<GCMClient::Result> unregister_responses_;
  53. OutgoingMessage last_sent_message_;
  54. std::string last_receiver_id_;
  55. };
  56. } // namespace gcm
  57. #endif // COMPONENTS_GCM_DRIVER_FAKE_GCM_PROFILE_SERVICE_H_