summaryrefslogtreecommitdiffstats
path: root/chromium/extensions/browser/browsertest_util_browsertest.cc
blob: 5e536bc94c4056eba15dac759e52127b8cdaf91c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/browsertest_util.h"

#include <string>

#include "base/memory/ref_counted.h"
#include "base/strings/string_number_conversions.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/shell/test/shell_apitest.h"
#include "extensions/test/result_catcher.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {
namespace browsertest_util {

namespace {

class ExtensionBrowsertestUtilTest : public ShellApiTest {
 public:
  ExtensionBrowsertestUtilTest() = default;

  ExtensionBrowsertestUtilTest(const ExtensionBrowsertestUtilTest&) = delete;
  ExtensionBrowsertestUtilTest& operator=(const ExtensionBrowsertestUtilTest&) =
      delete;

  ~ExtensionBrowsertestUtilTest() override = default;

  void SetUpOnMainThread() override {
    ShellApiTest::SetUpOnMainThread();

    extension_ = LoadExtension("extension");
    ASSERT_TRUE(extension_.get());

    // Wait for the test result to ensure the extension has loaded.
    // TODO(michaelpg): Implement a real extension readiness observer.
    ResultCatcher catcher;
    ASSERT_TRUE(catcher.GetNextResult());
  }

 protected:
  const Extension* extension() const { return extension_.get(); }

 private:
  scoped_refptr<const Extension> extension_;
};

IN_PROC_BROWSER_TEST_F(ExtensionBrowsertestUtilTest,
                       ExecuteScriptInBackgroundPage) {
  EXPECT_EQ(extension()->id(),
            ExecuteScriptInBackgroundPage(
                browser_context(), extension()->id(),
                "chrome.test.sendScriptResult(chrome.runtime.id);"));

  // Tests a successful test injection, including running nested tasks in the
  // browser process (via an asynchronous extension API).
  EXPECT_EQ("success",
            ExecuteScriptInBackgroundPage(
                browser_context(), extension()->id(),
                R"(chrome.runtime.setUninstallURL('http://example.com',
                                                  function() {
                     chrome.test.sendScriptResult('success');
                   });)"));

  // Return a non-string argument.
  EXPECT_EQ(3,
            ExecuteScriptInBackgroundPage(browser_context(), extension()->id(),
                                          "chrome.test.sendScriptResult(3);")
                .GetInt());
}

IN_PROC_BROWSER_TEST_F(ExtensionBrowsertestUtilTest,
                       ExecuteScriptInBackgroundPageDeprecated) {
  EXPECT_EQ(extension()->id(),
            ExecuteScriptInBackgroundPageDeprecated(
                browser_context(), extension()->id(),
                "window.domAutomationController.send(chrome.runtime.id);"));

  // Tests a successful test injection, including running nested tasks in the
  // browser process (via an asynchronous extension API).
  EXPECT_EQ(std::string("/") + extensions::kGeneratedBackgroundPageFilename,
            ExecuteScriptInBackgroundPageDeprecated(
                browser_context(), extension()->id(),
                R"(chrome.runtime.getBackgroundPage(function(result) {
                     let url = new URL(result.location.href);
                     window.domAutomationController.send(url.pathname);
                   });)"));

  // An argument that isn't a string should cause a failure, not a hang.
  EXPECT_NONFATAL_FAILURE(ExecuteScriptInBackgroundPageDeprecated(
                              browser_context(), extension()->id(),
                              "window.domAutomationController.send(3);"),
                          "send(3)");
}

IN_PROC_BROWSER_TEST_F(ExtensionBrowsertestUtilTest,
                       ExecuteScriptInBackgroundPageNoWait) {
  // Run an arbitrary script to check that we don't wait for a response.
  ASSERT_TRUE(ExecuteScriptInBackgroundPageNoWait(
      browser_context(), extension()->id(), "let foo = 0;"));

  // Run a script asynchronously that passes the test.
  ResultCatcher catcher;
  ASSERT_TRUE(ExecuteScriptInBackgroundPageNoWait(
      browser_context(), extension()->id(), "chrome.test.notifyPass();"));
  ASSERT_TRUE(catcher.GetNextResult());

  // Specifying a non-existent extension should add a non-fatal failure.
  EXPECT_NONFATAL_FAILURE(
      EXPECT_FALSE(ExecuteScriptInBackgroundPageNoWait(
          browser_context(), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "")),
      "No enabled extension with id: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}

}  // namespace

}  // namespace browsertest_util
}  // namespace extensions