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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_APITEST_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_APITEST_H_
#include <deque>
#include <string>
#include "base/compiler_specific.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "content/public/browser/notification_registrar.h"
namespace base {
class FilePath;
}
namespace extensions {
class Extension;
}
// The general flow of these API tests should work like this:
// (1) Setup initial browser state (e.g. create some bookmarks for the
// bookmark test)
// (2) Call ASSERT_TRUE(RunExtensionTest(name));
// (3) In your extension code, run your test and call chrome.test.pass or
// chrome.test.fail
// (4) Verify expected browser state.
// TODO(erikkay): There should also be a way to drive events in these tests.
class ExtensionApiTest : public ExtensionBrowserTest {
public:
// Flags used to configure how the tests are run.
// TODO(aa): Many of these are dupes of ExtensionBrowserTest::Flags. Combine
// somehow?
enum Flags {
kFlagNone = 0,
// Allow the extension to run in incognito mode.
kFlagEnableIncognito = 1 << 0,
// Launch the test page in an incognito window.
kFlagUseIncognito = 1 << 1,
// Allow file access for the extension.
kFlagEnableFileAccess = 1 << 2,
// Loads the extension with location COMPONENT.
kFlagLoadAsComponent = 1 << 3,
// Launch the extension as a platform app.
kFlagLaunchPlatformApp = 1 << 4,
// Don't fail when the loaded manifest has warnings.
kFlagIgnoreManifestWarnings = 1 << 5,
// Allow manifest versions older that Extension::kModernManifestVersion.
// Used to test old manifest features.
kFlagAllowOldManifestVersions = 1 << 6,
};
ExtensionApiTest();
~ExtensionApiTest() override;
protected:
// InProcessBrowserTest:
void SetUpInProcessBrowserTestFixture() override;
void TearDownInProcessBrowserTestFixture() override;
// Load |extension_name| and wait for pass / fail notification.
// |extension_name| is a directory in "test/data/extensions/api_test".
bool RunExtensionTest(const std::string& extension_name);
// Same as RunExtensionTest, but enables the extension for incognito mode.
bool RunExtensionTestIncognito(const std::string& extension_name);
// Same as RunExtensionTest, but ignores any warnings in the manifest.
bool RunExtensionTestIgnoreManifestWarnings(
const std::string& extension_name);
// Same as RunExtensionTest, allow old manifest ersions.
bool RunExtensionTestAllowOldManifestVersion(
const std::string& extension_name);
// Same as RunExtensionTest, but loads extension as component.
bool RunComponentExtensionTest(const std::string& extension_name);
// Same as RunExtensionTest, but disables file access.
bool RunExtensionTestNoFileAccess(const std::string& extension_name);
// Same as RunExtensionTestIncognito, but disables file access.
bool RunExtensionTestIncognitoNoFileAccess(const std::string& extension_name);
// If not empty, Load |extension_name|, load |page_url| and wait for pass /
// fail notification from the extension API on the page. Note that if
// |page_url| is not a valid url, it will be treated as a resource within
// the extension. |extension_name| is a directory in
// "test/data/extensions/api_test".
bool RunExtensionSubtest(const std::string& extension_name,
const std::string& page_url);
// Same as RunExtensionSubtest, except run with the specific |flags|
// (as defined in the Flags enum).
bool RunExtensionSubtest(const std::string& extension_name,
const std::string& page_url,
int flags);
// Load |page_url| and wait for pass / fail notification from the extension
// API on the page.
bool RunPageTest(const std::string& page_url);
bool RunPageTest(const std::string& page_url, int flags);
// Similar to RunExtensionTest, except used for running tests in platform app
// shell windows.
bool RunPlatformAppTest(const std::string& extension_name);
// Similar to RunPlatformAppTest, except sets an additional string argument
// |customArg| to the test config object.
bool RunPlatformAppTestWithArg(
const std::string& extension_name, const char* custom_arg);
// Similar to RunPlatformAppTest, with custom |flags| (as defined in the Flags
// enum). The kFlagLaunchPlatformApp flag is automatically added.
bool RunPlatformAppTestWithFlags(const std::string& extension_name,
int flags);
// Start the test server, and store details of its state. Those details
// will be available to javascript tests using chrome.test.getConfig().
bool StartEmbeddedTestServer();
// Start the test WebSocket server, and store details of its state. Those
// details will be available to javascript tests using
// chrome.test.getConfig().
bool StartWebSocketServer(const base::FilePath& root_directory);
// Start the test FTP server, and store details of its state. Those
// details will be available to JavaScript tests using
// chrome.test.getConfig().
bool StartFTPServer(const base::FilePath& root_directory);
// Start the spawned test server, and store details of its state. Those
// details will be available to javascript tests using
// chrome.test.getConfig().
bool StartSpawnedTestServer();
// Test that exactly one extension loaded. If so, return a pointer to
// the extension. If not, return NULL and set message_.
const extensions::Extension* GetSingleLoadedExtension();
// All extensions tested by ExtensionApiTest are in the "api_test" dir.
void SetUpCommandLine(base::CommandLine* command_line) override;
// If it failed, what was the error message?
std::string message_;
private:
bool RunExtensionTestImpl(const std::string& extension_name,
const std::string& test_page,
const char* custom_arg,
int flags);
// Hold details of the test, set in C++, which can be accessed by
// javascript using chrome.test.getConfig().
scoped_ptr<base::DictionaryValue> test_config_;
// Hold the test WebSocket server.
scoped_ptr<net::SpawnedTestServer> websocket_server_;
// Hold the test FTP server.
scoped_ptr<net::SpawnedTestServer> ftp_server_;
};
#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_APITEST_H_
|