browsertest_util_browsertest.cc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "extensions/browser/browsertest_util.h"
  5. #include <string>
  6. #include "base/memory/ref_counted.h"
  7. #include "base/strings/string_number_conversions.h"
  8. #include "extensions/common/constants.h"
  9. #include "extensions/common/extension.h"
  10. #include "extensions/shell/test/shell_apitest.h"
  11. #include "extensions/test/result_catcher.h"
  12. #include "testing/gtest/include/gtest/gtest-spi.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace extensions {
  15. namespace browsertest_util {
  16. namespace {
  17. class ExtensionBrowsertestUtilTest : public ShellApiTest {
  18. public:
  19. ExtensionBrowsertestUtilTest() = default;
  20. ExtensionBrowsertestUtilTest(const ExtensionBrowsertestUtilTest&) = delete;
  21. ExtensionBrowsertestUtilTest& operator=(const ExtensionBrowsertestUtilTest&) =
  22. delete;
  23. ~ExtensionBrowsertestUtilTest() override = default;
  24. void SetUpOnMainThread() override {
  25. ShellApiTest::SetUpOnMainThread();
  26. extension_ = LoadExtension("extension");
  27. ASSERT_TRUE(extension_.get());
  28. // Wait for the test result to ensure the extension has loaded.
  29. // TODO(michaelpg): Implement a real extension readiness observer.
  30. ResultCatcher catcher;
  31. ASSERT_TRUE(catcher.GetNextResult());
  32. }
  33. protected:
  34. const Extension* extension() const { return extension_.get(); }
  35. private:
  36. scoped_refptr<const Extension> extension_;
  37. };
  38. IN_PROC_BROWSER_TEST_F(ExtensionBrowsertestUtilTest,
  39. ExecuteScriptInBackgroundPage) {
  40. EXPECT_EQ(extension()->id(),
  41. ExecuteScriptInBackgroundPage(
  42. browser_context(), extension()->id(),
  43. "window.domAutomationController.send(chrome.runtime.id);"));
  44. // Check that executing a script doesn't block the browser process.
  45. EXPECT_EQ(std::string("/") + extensions::kGeneratedBackgroundPageFilename,
  46. ExecuteScriptInBackgroundPage(
  47. browser_context(), extension()->id(),
  48. "chrome.runtime.getBackgroundPage(function(result) {\n"
  49. " let url = new URL(result.location.href);\n"
  50. " window.domAutomationController.send(url.pathname);\n"
  51. "});"));
  52. // An argument that isn't a string should cause a failure, not a hang.
  53. EXPECT_NONFATAL_FAILURE(
  54. ExecuteScriptInBackgroundPage(browser_context(), extension()->id(),
  55. "window.domAutomationController.send(3);"),
  56. "send(3)");
  57. }
  58. IN_PROC_BROWSER_TEST_F(ExtensionBrowsertestUtilTest,
  59. ExecuteScriptInBackgroundPageNoWait) {
  60. // Run an arbitrary script to check that we don't wait for a response.
  61. ASSERT_TRUE(ExecuteScriptInBackgroundPageNoWait(
  62. browser_context(), extension()->id(), "let foo = 0;"));
  63. // Run a script asynchronously that passes the test.
  64. ResultCatcher catcher;
  65. ASSERT_TRUE(ExecuteScriptInBackgroundPageNoWait(
  66. browser_context(), extension()->id(), "chrome.test.notifyPass();"));
  67. ASSERT_TRUE(catcher.GetNextResult());
  68. // Specifying a non-existent extension should add a non-fatal failure.
  69. EXPECT_NONFATAL_FAILURE(
  70. EXPECT_FALSE(ExecuteScriptInBackgroundPageNoWait(
  71. browser_context(), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "")),
  72. "No enabled extension with id: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  73. }
  74. } // namespace
  75. } // namespace browsertest_util
  76. } // namespace extensions