summaryrefslogtreecommitdiffstats
path: root/chrome/test/ui/javascript_test_util.cc
blob: b71d67ff5bc1f74b5b6e6b670d0ef3e779ad0156 (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
// 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.

#include "chrome/test/ui/javascript_test_util.h"

#include "base/json/json_value_serializer.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"

bool JsonDictionaryToMap(const std::string& json,
                         std::map<std::string, std::string>* results) {
  DCHECK(results != NULL);
  JSONStringValueSerializer deserializer(json);
  scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL));

  // Note that we don't use ASSERT_TRUE here (and in some other places) as it
  // doesn't work inside a function with a return type other than void.
  EXPECT_TRUE(root.get());
  if (!root.get())
    return false;

  EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
  if (!root->IsType(Value::TYPE_DICTIONARY))
    return false;

  DictionaryValue* dict = static_cast<DictionaryValue*>(root.get());

  for (DictionaryValue::key_iterator it = dict->begin_keys();
       it != dict->end_keys(); ++it) {
    Value* value = NULL;
    bool succeeded = dict->GetWithoutPathExpansion(*it, &value);

    EXPECT_TRUE(succeeded);
    if (!succeeded)
      continue;

    const std::string& key(*it);
    std::string result;

    switch (value->GetType()) {
      case Value::TYPE_STRING:
        succeeded = value->GetAsString(&result);
        break;
      case Value::TYPE_DOUBLE: {
        double double_result;
        succeeded = value->GetAsDouble(&double_result);
        if (succeeded)
          result = base::DoubleToString(double_result);
        break;
      }
      default:
        NOTREACHED() << "Value type not supported!";
        return false;
    }

    EXPECT_TRUE(succeeded);
    if (succeeded)
      results->insert(std::make_pair(key, result));
  }

  return true;
}