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.
4
5#include "extensions/browser/file_highlighter.h"
6
7#include <string>
8
9#include "base/logging.h"
10#include "base/strings/string_util.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace extensions {
14
15namespace {
16
17const char kManifest[] =
18"{\n"
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"
27"    \"tabs\"\n"
28"  ],\n"
29"  \"content_scripts\": [\n"
30"    {\n"
31"      \"matches\": [\"*://aaronboodman.com/*\", \"*://rdcronin.com/*\"],\n"
32"      \"js\": [\"myscript.js\"]\n"
33"    }\n"
34"  ],\n"
35"  \"test_key\": {\n"
36"    \"escaped_quoted\\\"\",\n"
37"    \"/*foo*/\"\n"
38"  },\n"
39"  \"manifest_version\": 2,\n"
40"  \"international_key\": \"還是不要\"\n"
41"}";
42
43}  // namespace
44
45TEST(ManifestHighlighterUnitTest, ManifestHighlighterUnitTest) {
46  // Get a full key.
47  const char kPermissionsFeature[] =
48      "\"permissions\": [\n"
49      "    /* This is a tricky comment because it has brackets }]*/\n"
50      "    \"tabs\"\n"
51      "  ]";
52  ManifestHighlighter permissions(kManifest, "permissions", std::string());
53  EXPECT_EQ(kPermissionsFeature, permissions.GetFeature());
54
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());
59
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());
64
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());
71
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());
75
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());
81
82  const char kEscapedQuotedFeature[] = "\"escaped_quoted\\\"\"";
83  ManifestHighlighter escaped_quoted(
84      kManifest, "test_key", "escaped_quoted\\\"");
85  EXPECT_EQ(kEscapedQuotedFeature, escaped_quoted.GetFeature());
86
87  const char kFeatureWithComment[] = "\"/*foo*/\"";
88  ManifestHighlighter feature_with_comment(kManifest, "test_key", "/*foo*/");
89  EXPECT_EQ(kFeatureWithComment, feature_with_comment.GetFeature());
90
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());
96}
97
98TEST(SouceHighlighterUnitTest, SourceHighlighterUnitTest) {
99  const char kBasicSourceFile[] = "line one\nline two\nline three";
100
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());
107
108  const char kNoNewlineSourceFile[] = "thisisonelonglinewithnobreaksinit";
109
110  SourceHighlighter full_line(kNoNewlineSourceFile, 1u);
111  EXPECT_EQ(kNoNewlineSourceFile, full_line.GetFeature());
112
113  SourceHighlighter line_zero(kNoNewlineSourceFile, 0u);
114  EXPECT_EQ(std::string(), line_zero.GetFeature());
115
116  SourceHighlighter out_of_bounds(kNoNewlineSourceFile, 2u);
117  EXPECT_EQ(std::string(), out_of_bounds.GetFeature());
118}
119
120}  // namespace extensions
121