summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_unittest.cc
blob: 7aa51a04b79c84f3e43396c59ac34f0935c1d687 (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
// Copyright (c) 2006-2008 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 "base/string_util.h"
#include "chrome/browser/extensions/extension.h"
#include "testing/gtest/include/gtest/gtest.h"

class ExtensionTest : public testing::Test {
};

TEST(ExtensionTest, InitFromValueInvalid) {
  Extension extension;
  std::string error;

  // Test invalid format version
  DictionaryValue input_value;
  input_value.SetInteger(Extension::kFormatVersionKey, 2);
  EXPECT_FALSE(extension.InitFromValue(input_value, &error));
  EXPECT_EQ(Extension::kInvalidFormatVersionError, error);
  input_value.SetInteger(Extension::kFormatVersionKey, 1);

  // Test missing and invalid ids
  EXPECT_FALSE(extension.InitFromValue(input_value, &error));
  EXPECT_EQ(Extension::kInvalidIdError, error);
  input_value.SetInteger(Extension::kIdKey, 42);
  EXPECT_FALSE(extension.InitFromValue(input_value, &error));
  EXPECT_EQ(Extension::kInvalidIdError, error);
  input_value.SetString(Extension::kIdKey, L"com.google.myextension");

  // Test missing and invalid names
  EXPECT_FALSE(extension.InitFromValue(input_value, &error));
  EXPECT_EQ(Extension::kInvalidNameError, error);
  input_value.SetInteger(Extension::kNameKey, 42);
  EXPECT_FALSE(extension.InitFromValue(input_value, &error));
  EXPECT_EQ(Extension::kInvalidNameError, error);
  input_value.SetString(Extension::kNameKey, L"my extension");

  // Test invalid description
  input_value.SetInteger(Extension::kDescriptionKey, 42);
  EXPECT_FALSE(extension.InitFromValue(input_value, &error));
  EXPECT_EQ(Extension::kInvalidDescriptionError, error);
  input_value.Remove(Extension::kDescriptionKey, NULL);

  // Test invalid content scripts list
  input_value.SetInteger(Extension::kContentScriptsKey, 42);
  EXPECT_FALSE(extension.InitFromValue(input_value, &error));
  EXPECT_EQ(Extension::kInvalidContentScriptsListError, error);

  // Test invalid content script item
  ListValue* content_scripts = new ListValue;
  input_value.Set(Extension::kContentScriptsKey, content_scripts);
  content_scripts->Set(0, Value::CreateIntegerValue(42));
  EXPECT_FALSE(extension.InitFromValue(input_value, &error));
  EXPECT_EQ(0u, error.find(Extension::kInvalidContentScriptError));
}

TEST(ExtensionTest, InitFromValueValid) {
  Extension extension;
  std::string error;
  DictionaryValue input_value;
  DictionaryValue output_value;

  // Test minimal extension
  input_value.SetInteger(Extension::kFormatVersionKey, 1);
  input_value.SetString(Extension::kIdKey, L"com.google.myextension");
  input_value.SetString(Extension::kNameKey, L"my extension");

  EXPECT_TRUE(extension.InitFromValue(input_value, &error));
  extension.CopyToValue(&output_value);
  EXPECT_TRUE(input_value.Equals(&output_value));

  // Test with a description
  input_value.SetString(Extension::kDescriptionKey,
                        L"my extension does things");
  EXPECT_TRUE(extension.InitFromValue(input_value, &error));
  extension.CopyToValue(&output_value);
  EXPECT_TRUE(input_value.Equals(&output_value));

  // Test content_scripts
  ListValue* content_scripts = new ListValue();
  input_value.Set(Extension::kContentScriptsKey, content_scripts);
  content_scripts->Set(0, Value::CreateStringValue(L"foo/bar.js"));
  content_scripts->Set(1, Value::CreateStringValue(L"hot/dog.js"));
  EXPECT_TRUE(extension.InitFromValue(input_value, &error));
  extension.CopyToValue(&output_value);
  EXPECT_TRUE(input_value.Equals(&output_value));
}