1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/browser/api_test_utils.h"
6 #include "extensions/common/api/declarative/declarative_manifest_data.h"
7 #include "extensions/common/manifest_test.h"
11 using api_test_utils::ParseDictionary
;
12 using DeclarativeManifestTest
= ManifestTest
;
14 TEST_F(DeclarativeManifestTest
, Valid
) {
15 scoped_refptr
<Extension
> extension
= LoadAndExpectSuccess("event_rules.json");
16 DeclarativeManifestData
* manifest_data
=
17 DeclarativeManifestData::Get(extension
.get());
18 ASSERT_TRUE(manifest_data
);
19 std::vector
<linked_ptr
<DeclarativeManifestData::Rule
>>& rules
=
20 manifest_data
->RulesForEvent("foo");
21 EXPECT_EQ(1u, rules
.size());
22 scoped_ptr
<base::DictionaryValue
> expected_rule
= ParseDictionary(
25 " \"instanceType\": \"action_type\""
27 " \"conditions\" : [{"
28 " \"instanceType\" : \"condition_type\""
31 EXPECT_TRUE(expected_rule
->Equals(rules
[0]->ToValue().get()));
34 TEST_F(DeclarativeManifestTest
, ConditionMissingType
) {
36 scoped_ptr
<base::DictionaryValue
> manifest_data
= ParseDictionary(
38 " \"name\": \"Test\","
39 " \"version\": \"1\","
42 " \"event\": \"declarativeContent.onPageChanged\","
44 " \"type\": \"declarativeContent.ShowPageAction\""
46 " \"conditions\" : [{"
47 " \"css\": [\"video\"]"
52 ManifestData
manifest(manifest_data
.get(), "test");
53 LoadAndExpectError(manifest
, "'type' is required and must be a string");
56 TEST_F(DeclarativeManifestTest
, ConditionNotDictionary
) {
58 scoped_ptr
<base::DictionaryValue
> manifest_data
= ParseDictionary(
60 " \"name\": \"Test\","
61 " \"version\": \"1\","
64 " \"event\": \"declarativeContent.onPageChanged\","
66 " \"type\": \"declarativeContent.ShowPageAction\""
68 " \"conditions\" : [true]"
72 ManifestData
manifest(manifest_data
.get(), "test");
73 LoadAndExpectError(manifest
, "expected dictionary, got boolean");
76 TEST_F(DeclarativeManifestTest
, ActionMissingType
) {
78 scoped_ptr
<base::DictionaryValue
> manifest_data
= ParseDictionary(
80 " \"name\": \"Test\","
81 " \"version\": \"1\","
84 " \"event\": \"declarativeContent.onPageChanged\","
86 " \"conditions\" : [{"
87 " \"css\": [\"video\"],"
88 " \"type\" : \"declarativeContent.PageStateMatcher\""
93 ManifestData
manifest(manifest_data
.get(), "test");
94 LoadAndExpectError(manifest
, "'type' is required and must be a string");
97 TEST_F(DeclarativeManifestTest
, ActionNotDictionary
) {
99 scoped_ptr
<base::DictionaryValue
> manifest_data
= ParseDictionary(
101 " \"name\": \"Test\","
102 " \"version\": \"1\","
103 " \"event_rules\": ["
105 " \"event\": \"declarativeContent.onPageChanged\","
106 " \"actions\": [[]],"
107 " \"conditions\" : [{"
108 " \"css\": [\"video\"],"
109 " \"type\" : \"declarativeContent.PageStateMatcher\""
114 ManifestData
manifest(manifest_data
.get(), "test");
115 LoadAndExpectError(manifest
, "expected dictionary, got list");
118 TEST_F(DeclarativeManifestTest
, EventRulesNotList
) {
120 scoped_ptr
<base::DictionaryValue
> manifest_data
= ParseDictionary(
122 " \"name\": \"Test\","
123 " \"version\": \"1\","
124 " \"event_rules\": {}"
126 ManifestData
manifest(manifest_data
.get(), "test");
127 LoadAndExpectError(manifest
, "'event_rules' expected list, got dictionary");
130 TEST_F(DeclarativeManifestTest
, EventRuleNotDictionary
) {
132 scoped_ptr
<base::DictionaryValue
> manifest_data
= ParseDictionary(
134 " \"name\": \"Test\","
135 " \"version\": \"1\","
136 " \"event_rules\": [0,1,2]"
138 ManifestData
manifest(manifest_data
.get(), "test");
139 LoadAndExpectError(manifest
, "expected dictionary, got integer");
142 TEST_F(DeclarativeManifestTest
, EventMissingFromRule
) {
144 scoped_ptr
<base::DictionaryValue
> manifest_data
= ParseDictionary(
146 " \"name\": \"Test\","
147 " \"version\": \"1\","
148 " \"event_rules\": ["
151 " \"type\": \"declarativeContent.ShowPageAction\""
153 " \"conditions\" : [{"
154 " \"css\": [\"video\"],"
155 " \"type\" : \"declarativeContent.PageStateMatcher\""
160 ManifestData
manifest(manifest_data
.get(), "test");
161 LoadAndExpectError(manifest
, "'event' is required");
164 TEST_F(DeclarativeManifestTest
, RuleFailedToPopulate
) {
166 scoped_ptr
<base::DictionaryValue
> manifest_data
= ParseDictionary(
168 " \"name\": \"Test\","
169 " \"version\": \"1\","
170 " \"event_rules\": ["
172 " \"event\": \"declarativeContent.onPageChanged\""
176 ManifestData
manifest(manifest_data
.get(), "test");
177 LoadAndExpectError(manifest
, "rule failed to populate");
180 } // namespace extensions