extension_state_tester.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2021 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 EXTENSIONS_TEST_EXTENSION_STATE_TESTER_H_
  5. #define EXTENSIONS_TEST_EXTENSION_STATE_TESTER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "extensions/browser/disable_reason.h"
  8. #include "extensions/common/extension_id.h"
  9. namespace content {
  10. class BrowserContext;
  11. }
  12. namespace extensions {
  13. class ExtensionPrefs;
  14. class ExtensionRegistry;
  15. // A utility class to help check expected extension states (enabled, disabled,
  16. // etc). Generally prefer this class to direct checks on the ExtensionRegistry.
  17. //
  18. // This class will do more robust checking than just checking the
  19. // associated ExtensionRegistry set like so:
  20. // ExtensionRegistry* registry = GetRegistry();
  21. // EXPECT_TRUE(registry->disabled_extensions().Contains(id));
  22. // Unlike the above code, ExtensionStateTester will validate other assumptions,
  23. // including that the extension is *only* in a single registry set, that it is
  24. // disabled with the proper reason(s), etc.
  25. //
  26. // Each method will emit detailed failures to the test via gtest's ADD_FAILURE()
  27. // for failed assumptions. There may be multiple failed assumptions for a given
  28. // method call, which can help in diagnosing the current state versus the
  29. // expected state. For instance, a call to ExpectEnabled() with a disabled
  30. // extension will log that:
  31. // - The extension is not in the enabled set
  32. // - The extension is unexpectedly in the disabled set
  33. // - The extension had unexpected disable reasons
  34. // Since these failures are added from the body of these functions, they do not
  35. // include the line number of the calling function. To pinpoint which call
  36. // failed when multiple calls are in a single test (not uncommon), callers
  37. // should EXPECT_TRUE() on the return value of each function call. This way,
  38. // there will also be a failure listed for the immediate callsite, highlighting
  39. // which call to the method failed.
  40. //
  41. // Example usage:
  42. // ExtensionStateTester state_tester(browser_context());
  43. // EXPECT_TRUE(state_tester.ExpectEnabled(id));
  44. // DisableExtension(id, disable_reason::DISABLE_USER_ACTION);
  45. // EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
  46. // id, disable_reason::DISABLE_USER_ACTION));
  47. // ...
  48. //
  49. // You should never use EXPECT_FALSE() on one of these methods; instead, call
  50. // the appropriate method for the expectation. For instance, use
  51. // EXPECT_TRUE(state_tester.ExpectBlocklisted(id));
  52. // rather than
  53. // EXPECT_FALSE(state_tester.ExpectEnabled(id));
  54. // Though ExpectEnabled() *will* return false if the extension is not enabled,
  55. // it will also add (potentially multiple) failures to the active test.
  56. class ExtensionStateTester {
  57. public:
  58. explicit ExtensionStateTester(content::BrowserContext* browser_context);
  59. ExtensionStateTester(const ExtensionStateTester&) = delete;
  60. ExtensionStateTester& operator=(const ExtensionStateTester&) = delete;
  61. ~ExtensionStateTester();
  62. // Expects the extension to be enabled.
  63. [[nodiscard]] bool ExpectEnabled(const ExtensionId& extension_id);
  64. // Expects the extension to be disabled with (only) the specified `reason`.
  65. [[nodiscard]] bool ExpectDisabledWithSingleReason(
  66. const ExtensionId& extension_id,
  67. disable_reason::DisableReason reason);
  68. // Expects the extension to be disabled with exactly the specified
  69. // `disable_reasons`.
  70. [[nodiscard]] bool ExpectDisabledWithReasons(const ExtensionId& extension_id,
  71. int disable_reasons);
  72. // Expects the extension to be blocklisted.
  73. [[nodiscard]] bool ExpectBlocklisted(const ExtensionId& extension_id);
  74. // Expects the extension to be terminated.
  75. [[nodiscard]] bool ExpectTerminated(const ExtensionId& extension_id);
  76. private:
  77. // Helper method to iterate over the registry sets, expecting the extension
  78. // to only be within the one indicated by `set_name`.
  79. [[nodiscard]] bool ExpectOnlyInSet(const ExtensionId& extension_id,
  80. const char* set_name);
  81. const raw_ptr<ExtensionRegistry> registry_;
  82. const raw_ptr<ExtensionPrefs> prefs_;
  83. };
  84. } // namespace extensions
  85. #endif // EXTENSIONS_TEST_EXTENSION_STATE_TESTER_H_