mock_permission_prompt_factory.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_PERMISSIONS_TEST_MOCK_PERMISSION_PROMPT_FACTORY_H_
  5. #define COMPONENTS_PERMISSIONS_TEST_MOCK_PERMISSION_PROMPT_FACTORY_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "components/permissions/permission_prompt.h"
  10. #include "components/permissions/permission_request.h"
  11. #include "components/permissions/permission_request_manager.h"
  12. #include "url/gurl.h"
  13. namespace content {
  14. class WebContents;
  15. }
  16. namespace permissions {
  17. class MockPermissionPrompt;
  18. enum class RequestType;
  19. // Provides a skeleton class for both unit and browser testing when trying to
  20. // test the bubble manager logic. Should not be used for anything that requires
  21. // actual UI.
  22. // See example usage in
  23. // chrome/browser/permissions/permission_request_manager_unittest.cc
  24. class MockPermissionPromptFactory {
  25. public:
  26. explicit MockPermissionPromptFactory(PermissionRequestManager* manager);
  27. MockPermissionPromptFactory(const MockPermissionPromptFactory&) = delete;
  28. MockPermissionPromptFactory& operator=(const MockPermissionPromptFactory&) =
  29. delete;
  30. ~MockPermissionPromptFactory();
  31. // Create method called by the PBM to show a bubble.
  32. std::unique_ptr<PermissionPrompt> Create(
  33. content::WebContents* web_contents,
  34. PermissionPrompt::Delegate* delegate);
  35. void ResetCounts();
  36. void DocumentOnLoadCompletedInPrimaryMainFrame();
  37. void set_response_type(PermissionRequestManager::AutoResponseType type) {
  38. response_type_ = type;
  39. }
  40. PermissionRequestManager::AutoResponseType response_type() {
  41. return response_type_;
  42. }
  43. // If the current view is visible.
  44. bool is_visible();
  45. // Number of times |Show| was called on any bubble.
  46. int show_count() { return show_count_; }
  47. // Number of requests seen by the last |Show|.
  48. int request_count() { return requests_count_; }
  49. // Number of requests seen.
  50. int TotalRequestCount();
  51. // Whether the specified permission was shown in a prompt.
  52. bool RequestTypeSeen(RequestType type);
  53. // Whether a prompt with the given origin was shown.
  54. bool RequestOriginSeen(const GURL& origin);
  55. void WaitForPermissionBubble();
  56. private:
  57. friend class MockPermissionPrompt;
  58. // This shouldn't be called. Is here to fail tests that try to create a bubble
  59. // after the factory has been destroyed.
  60. static std::unique_ptr<PermissionPrompt> DoNotCreate(
  61. content::WebContents* web_contents,
  62. PermissionPrompt::Delegate* delegate);
  63. void HideView(MockPermissionPrompt* view);
  64. int show_count_;
  65. int requests_count_;
  66. std::vector<RequestType> request_types_seen_;
  67. std::vector<GURL> request_origins_seen_;
  68. std::vector<MockPermissionPrompt*> prompts_;
  69. PermissionRequestManager::AutoResponseType response_type_;
  70. base::RepeatingClosure show_bubble_quit_closure_;
  71. // The bubble manager that will be associated with this factory.
  72. raw_ptr<PermissionRequestManager> manager_;
  73. };
  74. } // namespace permissions
  75. #endif // COMPONENTS_PERMISSIONS_TEST_MOCK_PERMISSION_PROMPT_FACTORY_H_