1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_APITEST_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_APITEST_H_
11 #include "base/compiler_specific.h"
12 #include "base/values.h"
13 #include "chrome/browser/extensions/extension_browsertest.h"
14 #include "content/public/browser/notification_registrar.h"
20 namespace extensions
{
24 // The general flow of these API tests should work like this:
25 // (1) Setup initial browser state (e.g. create some bookmarks for the
27 // (2) Call ASSERT_TRUE(RunExtensionTest(name));
28 // (3) In your extension code, run your test and call chrome.test.pass or
30 // (4) Verify expected browser state.
31 // TODO(erikkay): There should also be a way to drive events in these tests.
32 class ExtensionApiTest
: public ExtensionBrowserTest
{
34 // Flags used to configure how the tests are run.
35 // TODO(aa): Many of these are dupes of ExtensionBrowserTest::Flags. Combine
40 // Allow the extension to run in incognito mode.
41 kFlagEnableIncognito
= 1 << 0,
43 // Launch the test page in an incognito window.
44 kFlagUseIncognito
= 1 << 1,
46 // Allow file access for the extension.
47 kFlagEnableFileAccess
= 1 << 2,
49 // Loads the extension with location COMPONENT.
50 kFlagLoadAsComponent
= 1 << 3,
52 // Launch the extension as a platform app.
53 kFlagLaunchPlatformApp
= 1 << 4,
55 // Don't fail when the loaded manifest has warnings.
56 kFlagIgnoreManifestWarnings
= 1 << 5,
58 // Allow manifest versions older that Extension::kModernManifestVersion.
59 // Used to test old manifest features.
60 kFlagAllowOldManifestVersions
= 1 << 6,
64 ~ExtensionApiTest() override
;
67 // InProcessBrowserTest:
68 void SetUpInProcessBrowserTestFixture() override
;
69 void TearDownInProcessBrowserTestFixture() override
;
71 // Load |extension_name| and wait for pass / fail notification.
72 // |extension_name| is a directory in "test/data/extensions/api_test".
73 bool RunExtensionTest(const std::string
& extension_name
);
75 // Same as RunExtensionTest, but enables the extension for incognito mode.
76 bool RunExtensionTestIncognito(const std::string
& extension_name
);
78 // Same as RunExtensionTest, but ignores any warnings in the manifest.
79 bool RunExtensionTestIgnoreManifestWarnings(
80 const std::string
& extension_name
);
82 // Same as RunExtensionTest, allow old manifest ersions.
83 bool RunExtensionTestAllowOldManifestVersion(
84 const std::string
& extension_name
);
86 // Same as RunExtensionTest, but loads extension as component.
87 bool RunComponentExtensionTest(const std::string
& extension_name
);
89 // Same as RunExtensionTest, but disables file access.
90 bool RunExtensionTestNoFileAccess(const std::string
& extension_name
);
92 // Same as RunExtensionTestIncognito, but disables file access.
93 bool RunExtensionTestIncognitoNoFileAccess(const std::string
& extension_name
);
95 // Returns true if the Subtest and Page tests are being skipped. This is
96 // will only be true for windows debug builds, see http://crbug.com/177163.
98 // Usually you won't need to check this, but if a test continues after
99 // RunExtensionSubtest, you may. For example, if a test calls
100 // RunExtensionSubtest then asserts that the Extension was installed, it will
101 // fail on win debug builds.
102 bool ExtensionSubtestsAreSkipped();
104 // If not empty, Load |extension_name|, load |page_url| and wait for pass /
105 // fail notification from the extension API on the page. Note that if
106 // |page_url| is not a valid url, it will be treated as a resource within
107 // the extension. |extension_name| is a directory in
108 // "test/data/extensions/api_test".
109 bool RunExtensionSubtest(const std::string
& extension_name
,
110 const std::string
& page_url
);
112 // Same as RunExtensionSubtest, except run with the specific |flags|
113 // (as defined in the Flags enum).
114 bool RunExtensionSubtest(const std::string
& extension_name
,
115 const std::string
& page_url
,
118 // Load |page_url| and wait for pass / fail notification from the extension
120 bool RunPageTest(const std::string
& page_url
);
121 bool RunPageTest(const std::string
& page_url
, int flags
);
123 // Similar to RunExtensionTest, except used for running tests in platform app
125 bool RunPlatformAppTest(const std::string
& extension_name
);
127 // Similar to RunPlatformAppTest, except sets an additional string argument
128 // |customArg| to the test config object.
129 bool RunPlatformAppTestWithArg(
130 const std::string
& extension_name
, const char* custom_arg
);
132 // Similar to RunPlatformAppTest, with custom |flags| (as defined in the Flags
133 // enum). The kFlagLaunchPlatformApp flag is automatically added.
134 bool RunPlatformAppTestWithFlags(const std::string
& extension_name
,
137 // Start the test server, and store details of its state. Those details
138 // will be available to javascript tests using chrome.test.getConfig().
139 bool StartEmbeddedTestServer();
141 // Start the test WebSocket server, and store details of its state. Those
142 // details will be available to javascript tests using
143 // chrome.test.getConfig().
144 bool StartWebSocketServer(const base::FilePath
& root_directory
);
146 // Start the test FTP server, and store details of its state. Those
147 // details will be available to JavaScript tests using
148 // chrome.test.getConfig().
149 bool StartFTPServer(const base::FilePath
& root_directory
);
151 // Start the spawned test server, and store details of its state. Those
152 // details will be available to javascript tests using
153 // chrome.test.getConfig().
154 bool StartSpawnedTestServer();
156 // Test that exactly one extension loaded. If so, return a pointer to
157 // the extension. If not, return NULL and set message_.
158 const extensions::Extension
* GetSingleLoadedExtension();
160 // All extensions tested by ExtensionApiTest are in the "api_test" dir.
161 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
163 // If it failed, what was the error message?
164 std::string message_
;
167 bool RunExtensionTestImpl(const std::string
& extension_name
,
168 const std::string
& test_page
,
169 const char* custom_arg
,
172 // Hold details of the test, set in C++, which can be accessed by
173 // javascript using chrome.test.getConfig().
174 scoped_ptr
<base::DictionaryValue
> test_config_
;
176 // Hold the test WebSocket server.
177 scoped_ptr
<net::SpawnedTestServer
> websocket_server_
;
179 // Hold the test FTP server.
180 scoped_ptr
<net::SpawnedTestServer
> ftp_server_
;
183 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_APITEST_H_