browsertest_util.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2018 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_BROWSER_BROWSERTEST_UTIL_H_
  5. #define EXTENSIONS_BROWSER_BROWSERTEST_UTIL_H_
  6. #include <string>
  7. #include "content/public/test/browser_test_utils.h"
  8. namespace content {
  9. class BrowserContext;
  10. } // namespace content
  11. namespace extensions::browsertest_util {
  12. // Determine if a user activation notification should be triggered before
  13. // executing a script
  14. enum class ScriptUserActivation {
  15. kActivate,
  16. kDontActivate,
  17. };
  18. // Waits until |script| calls "window.domAutomationController.send(result)",
  19. // where |result| is a string, and returns |result|. Fails the test and returns
  20. // an empty string if |extension_id| isn't installed in |context| or doesn't
  21. // have a background page, or if executing the script fails. The argument
  22. // |script_user_activation| determines if the script should be executed after a
  23. // user activation.
  24. std::string ExecuteScriptInBackgroundPage(
  25. content::BrowserContext* context,
  26. const std::string& extension_id,
  27. const std::string& script,
  28. ScriptUserActivation script_user_activation =
  29. ScriptUserActivation::kActivate);
  30. // Same as ExecuteScriptInBackgroundPage, but doesn't wait for the script
  31. // to return a result. Fails the test and returns false if |extension_id|
  32. // isn't installed in |context| or doesn't have a background page, or if
  33. // executing the script fails.
  34. bool ExecuteScriptInBackgroundPageNoWait(content::BrowserContext* context,
  35. const std::string& extension_id,
  36. const std::string& script);
  37. // Synchronously stops the service worker registered by the extension with the
  38. // given `extension_id` at global scope. The extension must be installed and
  39. // enabled.
  40. void StopServiceWorkerForExtensionGlobalScope(content::BrowserContext* context,
  41. const std::string& extension_id);
  42. } // namespace extensions::browsertest_util
  43. #endif // EXTENSIONS_BROWSER_BROWSERTEST_UTIL_H_