summaryrefslogtreecommitdiffstats
path: root/chrome/test/automation/javascript_message_utils.h
blob: e4b61f4d9961f222f192336398e99dd2dc185cf8 (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
// Copyright (c) 2011 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_TEST_AUTOMATION_JAVASCRIPT_MESSAGE_UTILS_H_
#define CHROME_TEST_AUTOMATION_JAVASCRIPT_MESSAGE_UTILS_H_
#pragma once

#include <string>
#include <vector>

#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
#include "base/stringprintf.h"
#include "base/values.h"
#include "chrome/test/automation/dom_element_proxy.h"
#include "chrome/test/automation/value_conversion_traits.h"

template <>
struct ValueConversionTraits<DOMElementProxy::By> {
  typedef DOMElementProxy::By type;
  static Value* CreateValueFrom(const type& t) {
    DictionaryValue* value = new DictionaryValue();
    std::string by_type;
    switch (t.type()) {
      case type::TYPE_XPATH:
        by_type = "xpath";
        break;
      case type::TYPE_SELECTORS:
        by_type = "selectors";
        break;
      case type::TYPE_TEXT:
        by_type = "text";
        break;
      default:
        NOTREACHED();
        break;
    }
    value->SetString("type", by_type);
    value->SetString("queryString", t.query());
    return value;
  }
};

template <typename T>
struct ValueConversionTraits<std::vector<T> > {
  static Value* CreateValueFrom(const std::vector<T>& t) {
    ListValue* value = new ListValue();
    for (size_t i = 0; i < t.size(); i++) {
      value->Append(ValueConversionTraits<T>::CreateValueFrom(t[i]));
    }
    return value;
  }
  static bool SetFromValue(const Value* value, std::vector<T>* t) {
    if (!value->IsType(Value::TYPE_LIST))
      return false;

    const ListValue* list_value = static_cast<const ListValue*>(value);
    ListValue::const_iterator iter;
    for (iter = list_value->begin(); iter != list_value->end(); ++iter) {
      if (!ValueConversionTraits<T>::CanConvert(*iter))
        return false;
    }
    for (iter = list_value->begin(); iter != list_value->end(); ++iter) {
      T inner_value;
      ValueConversionTraits<T>::SetFromValue(*iter, &inner_value);
      t->push_back(inner_value);
    }
    return true;
  }
};

namespace javascript_utils {

// Converts |arg| to a JSON string.
template <typename T>
std::string JSONStringify(const T& arg) {
  std::string javascript;
  scoped_ptr<Value> value(ValueConversionTraits<T>::CreateValueFrom(arg));
  base::JSONWriter::Write(value.get(), false, &javascript);
  return javascript;
}

// Converts |arg| to a JSON string and returns a string formatted as
// |format| specifies. |format| should only expect string arguments.
template <typename T>
std::string JavaScriptPrintf(const std::string& format, const T& arg) {
  return base::StringPrintf(format.c_str(), JSONStringify(arg).c_str());
}

// Similar to above, but with an additional argument.
template <typename T1, typename T2>
std::string JavaScriptPrintf(const std::string& format, const T1& arg1,
                             const T2& arg2) {
  return base::StringPrintf(format.c_str(),
                            JSONStringify(arg1).c_str(),
                            JSONStringify(arg2).c_str());
}

// Similar to above, but with an additional argument.
template <typename T1, typename T2, typename T3>
std::string JavaScriptPrintf(const std::string& format, const T1& arg1,
                             const T2& arg2, const T3& arg3) {
  return base::StringPrintf(format.c_str(),
                            JSONStringify(arg1).c_str(),
                            JSONStringify(arg2).c_str(),
                            JSONStringify(arg3).c_str());
}

}  // namespace javascript_utils

#endif  // CHROME_TEST_AUTOMATION_JAVASCRIPT_MESSAGE_UTILS_H_