Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5288)

Unified Diff: chrome/test/webdriver/commands/chrome_commands.cc

Issue 8806030: Add commands to Chrome WebDriver for installing and manipulating extensions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/test/webdriver/commands/chrome_commands.cc
diff --git a/chrome/test/webdriver/commands/chrome_commands.cc b/chrome/test/webdriver/commands/chrome_commands.cc
index 0714518de54095669f3d033b3807ad26772e6b45..2033b74988d5a41834beb52f0775be3febda6f3b 100644
--- a/chrome/test/webdriver/commands/chrome_commands.cc
+++ b/chrome/test/webdriver/commands/chrome_commands.cc
@@ -8,16 +8,22 @@
#include <vector>
#include "base/file_path.h"
+#include "base/stringprintf.h"
#include "chrome/test/automation/value_conversion_util.h"
#include "chrome/test/webdriver/commands/response.h"
#include "chrome/test/webdriver/webdriver_error.h"
#include "chrome/test/webdriver/webdriver_session.h"
+#include "chrome/test/webdriver/webdriver_util.h"
+
+using base::DictionaryValue;
+using base::ListValue;
+using base::Value;
namespace webdriver {
ExtensionsCommand::ExtensionsCommand(
const std::vector<std::string>& path_segments,
- const base::DictionaryValue* const parameters)
+ const DictionaryValue* const parameters)
: WebDriverCommand(path_segments, parameters) {}
ExtensionsCommand::~ExtensionsCommand() {}
@@ -31,22 +37,46 @@ bool ExtensionsCommand::DoesPost() {
}
void ExtensionsCommand::ExecuteGet(Response* const response) {
- std::vector<std::string> extension_ids;
- Error* error = session_->GetInstalledExtensions(&extension_ids);
+ ListValue extensions_list;
+ Error* error = session_->GetExtensionsInfo(&extensions_list);
if (error) {
response->SetError(error);
return;
}
- base::ListValue* extensions = new base::ListValue();
- for (size_t i = 0; i < extension_ids.size(); ++i)
- extensions->Append(CreateValueFrom(extension_ids[i]));
- response->SetValue(extensions);
+
+ ListValue id_list;
+ for (size_t i = 0; i < extensions_list.GetSize(); ++i) {
+ DictionaryValue* extension_dict;
+ if (!extensions_list.GetDictionary(i, &extension_dict)) {
+ response->SetError(
+ new Error(kUnknownError, "Invalid extension dictionary"));
+ return;
+ }
+ bool is_component;
+ if (!extension_dict->GetBoolean("is_component", &is_component)) {
+ response->SetError(
+ new Error(kUnknownError, "Missing or invalid 'is_component'"));
+ return;
+ }
+ if (is_component)
+ continue;
+
+ std::string extension_id;
+ if (!extension_dict->GetString("id", &extension_id)) {
+ response->SetError(new Error(kUnknownError, "Missing or invalid 'id'"));
+ return;
+ }
+
+ id_list.Append(Value::CreateStringValue(extension_id));
+ }
+
+ response->SetValue(id_list.DeepCopy());
}
void ExtensionsCommand::ExecutePost(Response* const response) {
FilePath::StringType path_string;
if (!GetStringParameter("path", &path_string)) {
- response->SetError(new Error(kUnknownError, "'path' missing or invalid"));
+ response->SetError(new Error(kBadRequest, "'path' missing or invalid"));
return;
}
@@ -57,7 +87,174 @@ void ExtensionsCommand::ExecutePost(Response* const response) {
response->SetError(error);
return;
}
- response->SetValue(CreateValueFrom(extension_id));
+ response->SetValue(Value::CreateStringValue(extension_id));
+}
+
+ExtensionCommand::ExtensionCommand(
+ const std::vector<std::string>& path_segments,
+ const DictionaryValue* const parameters)
+ : WebDriverCommand(path_segments, parameters) {}
+
+ExtensionCommand::~ExtensionCommand() {}
+
+bool ExtensionCommand::Init(Response* const response) {
+ if (!WebDriverCommand::Init(response))
+ return false;
+
+ // Path: "/session/$id/chrome/extension/$id".
Huyen 2011/12/08 02:10:32 is the second path variable the session id? maybe
kkania 2011/12/08 17:31:21 Done.
+ extension_id_ = GetPathVariable(5);
+ if (extension_id_.empty()) {
+ response->SetError(new Error(kBadRequest, "Invalid extension ID"));
+ return false;
+ }
+ return true;
+}
+
+bool ExtensionCommand::DoesGet() {
+ return true;
+}
+
+bool ExtensionCommand::DoesPost() {
+ return true;
+}
+
+bool ExtensionCommand::DoesDelete() {
+ return true;
+}
+
+void ExtensionCommand::ExecuteGet(Response* const response) {
+ ListValue extensions_list;
+ Error* error = session_->GetExtensionsInfo(&extensions_list);
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+
+ bool found = false;
+ DictionaryValue extension;
+ for (size_t i = 0; i < extensions_list.GetSize(); ++i) {
+ DictionaryValue* extension_dict;
+ if (!extensions_list.GetDictionary(i, &extension_dict)) {
+ response->SetError(
+ new Error(kUnknownError, "Invalid extension dictionary"));
+ return;
+ }
+ std::string id;
+ if (!extension_dict->GetString("id", &id)) {
+ response->SetError(
+ new Error(kUnknownError, "Missing extension ID"));
+ return;
+ }
+ if (id == extension_id_) {
+ found = true;
+ extension.Swap(extension_dict);
+ break;
+ }
+ }
+
+ if (!found) {
+ response->SetError(
+ new Error(kUnknownError, "Extension is not installed"));
+ return;
+ }
+
+ bool is_enabled;
+ if (!extension.GetBoolean("is_enabled", &is_enabled)) {
+ response->SetError(
+ new Error(kUnknownError, "Missing or invalid 'is_enabled'"));
+ return;
+ }
+ bool has_page_action;
+ if (!extension.GetBoolean("has_page_action", &has_page_action)) {
+ response->SetError(
+ new Error(kUnknownError, "Missing or invalid 'is_enabled'"));
+ return;
+ }
+
+ bool is_visible = false;
+ if (is_enabled && has_page_action) {
+ // Only check page action visibility if we are enabled with a page action.
+ // Otherwise Chrome will throw an error saying the extension does not have
+ // a page action.
+ error = session_->IsPageActionVisible(
+ session_->current_target().view_id, extension_id_, &is_visible);
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+ }
+
+ extension.SetBoolean("is_page_action_visible", is_visible);
+ response->SetValue(extension.DeepCopy());
+}
+
+void ExtensionCommand::ExecutePost(Response* const response) {
+ Error* error = NULL;
+ if (HasParameter("enable")) {
+ bool enable;
+ if (!GetBooleanParameter("enable", &enable)) {
+ response->SetError(new Error(kBadRequest, "'enable' must be a bool"));
+ return;
+ }
+ error = session_->SetExtensionState(extension_id_, enable);
+
Huyen 2011/12/08 02:10:32 delete newline
kkania 2011/12/08 17:31:21 Done.
+ } else if (HasParameter("click_button")) {
+ std::string button;
+ if (!GetStringParameter("click_button", &button)) {
+ response->SetError(
+ new Error(kBadRequest, "'click_button' must be a string"));
+ return;
+ }
+ error = session_->ClickExtensionButton(extension_id_,
+ button == "browser_action");
+
Huyen 2011/12/08 02:10:32 delete newline
kkania 2011/12/08 17:31:21 Done.
+ } else {
+ error = new Error(kBadRequest, "Missing action parameter");
+ }
+
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+}
+
+void ExtensionCommand::ExecuteDelete(Response* const response) {
+ Error* error = session_->UninstallExtension(extension_id_);
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+}
+
+ViewsCommand::ViewsCommand(
+ const std::vector<std::string>& path_segments,
+ const DictionaryValue* const parameters)
+ : WebDriverCommand(path_segments, parameters) {}
+
+ViewsCommand::~ViewsCommand() {}
+
+bool ViewsCommand::DoesGet() {
+ return true;
+}
+
+void ViewsCommand::ExecuteGet(Response* const response) {
+ std::vector<WebViewInfo> views;
+ Error* error = session_->GetViews(&views);
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+ ListValue* views_list = new ListValue();
+ for (size_t i = 0; i < views.size(); ++i) {
+ DictionaryValue* dict = new DictionaryValue();
+ AutomationId id = views[i].view_id.GetId();
+ dict->SetString("handle", JsonStringify(id.ToValue()));
+ dict->SetInteger("type", id.type());
+ if (!views[i].extension_id.empty())
+ dict->SetString("extension_id", views[i].extension_id);
+ views_list->Append(dict);
+ }
+ response->SetValue(views_list);
}
} // namespace webdriver

Powered by Google App Engine
This is Rietveld 408576698