1 // Copyright 2013 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/file_highlighter.h"
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace extensions
{
17 const char kManifest
[] =
19 " \"name\": \"Content Scripts\",\n"
20 " \"version\": \"2.0\",\n"
21 " // this is a comment with the word permissions.\n"
22 " /* This is a multine\n"
23 " comment with the word permissions\n"
24 " that shouldn't be highlighted */\n"
25 " \"permissions\": [\n"
26 " /* This is a tricky comment because it has brackets }]*/\n"
29 " \"content_scripts\": [\n"
31 " \"matches\": [\"*://aaronboodman.com/*\", \"*://rdcronin.com/*\"],\n"
32 " \"js\": [\"myscript.js\"]\n"
36 " \"escaped_quoted\\\"\",\n"
39 " \"manifest_version\": 2,\n"
40 " \"international_key\": \"還是不要\"\n"
45 TEST(ManifestHighlighterUnitTest
, ManifestHighlighterUnitTest
) {
47 const char kPermissionsFeature
[] =
48 "\"permissions\": [\n"
49 " /* This is a tricky comment because it has brackets }]*/\n"
52 ManifestHighlighter
permissions(kManifest
, "permissions", std::string());
53 EXPECT_EQ(kPermissionsFeature
, permissions
.GetFeature());
55 // Get a specific portion of a key.
56 const char kTabsFeature
[] = "\"tabs\"";
57 ManifestHighlighter
tabs(kManifest
, "permissions", "tabs");
58 EXPECT_EQ(kTabsFeature
, tabs
.GetFeature());
60 // Get a single-character, non-quoted entity of a key.
61 const char kManifestVersionFeature
[] = "2";
62 ManifestHighlighter
version(kManifest
, "manifest_version", "2");
63 EXPECT_EQ(kManifestVersionFeature
, version
.GetFeature());
65 // Get a compound portion of a key, including quoted '//' (which shouldn't be
66 // mistaken for comments).
67 const char kMatchesFeature
[] =
68 "\"matches\": [\"*://aaronboodman.com/*\", \"*://rdcronin.com/*\"]";
69 ManifestHighlighter
matches(kManifest
, "content_scripts", "matches");
70 EXPECT_EQ(kMatchesFeature
, matches
.GetFeature());
72 // If a feature isn't present, we should get an empty string.
73 ManifestHighlighter
not_present(kManifest
, "a_fake_feature", std::string());
74 EXPECT_EQ(std::string(), not_present
.GetFeature());
76 // If we request a specific portion of a key which is not found, we should
77 // get an empty string.
78 ManifestHighlighter
specific_portion_not_present(
79 kManifest
, "permissions", "a_fake_feature");
80 EXPECT_EQ(std::string(), specific_portion_not_present
.GetFeature());
82 const char kEscapedQuotedFeature
[] = "\"escaped_quoted\\\"\"";
83 ManifestHighlighter
escaped_quoted(
84 kManifest
, "test_key", "escaped_quoted\\\"");
85 EXPECT_EQ(kEscapedQuotedFeature
, escaped_quoted
.GetFeature());
87 const char kFeatureWithComment
[] = "\"/*foo*/\"";
88 ManifestHighlighter
feature_with_comment(kManifest
, "test_key", "/*foo*/");
89 EXPECT_EQ(kFeatureWithComment
, feature_with_comment
.GetFeature());
91 // Check with non-ascii characters.
92 const char kInternationalFeature
[] = "\"international_key\": \"還是不要\"";
93 ManifestHighlighter
international_feature(
94 kManifest
, "international_key", std::string());
95 EXPECT_EQ(kInternationalFeature
, international_feature
.GetFeature());
98 TEST(SouceHighlighterUnitTest
, SourceHighlighterUnitTest
) {
99 const char kBasicSourceFile
[] = "line one\nline two\nline three";
101 SourceHighlighter
basic1(kBasicSourceFile
, 1u);
102 EXPECT_EQ("line one", basic1
.GetFeature());
103 SourceHighlighter
basic2(kBasicSourceFile
, 2u);
104 EXPECT_EQ("line two", basic2
.GetFeature());
105 SourceHighlighter
basic3(kBasicSourceFile
, 3u);
106 EXPECT_EQ("line three", basic3
.GetFeature());
108 const char kNoNewlineSourceFile
[] = "thisisonelonglinewithnobreaksinit";
110 SourceHighlighter
full_line(kNoNewlineSourceFile
, 1u);
111 EXPECT_EQ(kNoNewlineSourceFile
, full_line
.GetFeature());
113 SourceHighlighter
line_zero(kNoNewlineSourceFile
, 0u);
114 EXPECT_EQ(std::string(), line_zero
.GetFeature());
116 SourceHighlighter
out_of_bounds(kNoNewlineSourceFile
, 2u);
117 EXPECT_EQ(std::string(), out_of_bounds
.GetFeature());
120 } // namespace extensions