summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/greasemonkey_slave_unittest.cc
blob: bedbfbf390ce286cdc8a1b9da86862b6ad1f4c75 (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
// Copyright (c) 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/logging.h"
#include "chrome/renderer/greasemonkey_slave.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"

TEST(GreasemonkeySlaveTest, EscapeGlob) {
  EXPECT_EQ("", GreasemonkeyScript::EscapeGlob(""));
  EXPECT_EQ("*", GreasemonkeyScript::EscapeGlob("*"));
  EXPECT_EQ("www.google.com", GreasemonkeyScript::EscapeGlob("www.google.com"));
  EXPECT_EQ("*google.com*", GreasemonkeyScript::EscapeGlob("*google.com*"));
  EXPECT_EQ("foo\\\\bar\\?hot=dog",
            GreasemonkeyScript::EscapeGlob("foo\\bar?hot=dog"));
}

TEST(GreasemonkeySlaveTest, Parse1) {
  const std::string text(
    "// This is my awesome script\n"
    "// It does stuff.\n"
    "// ==UserScript==   trailing garbage\n"
    "// @name foobar script\n"
    "// @namespace http://www.google.com/\n"
    "// @include *mail.google.com*\n"
    "// \n"
    "// @othergarbage\n"
    "// @include *mail.yahoo.com*\r\n"
    "// @include  \t *mail.msn.com*\n" // extra spaces after "@include" OK
    "//@include not-recognized\n" // must have one space after "//"
    "// ==/UserScript==  trailing garbage\n"
    "\n"
    "\n"
    "alert('hoo!');\n");

  GreasemonkeyScript script("foo");
  script.Parse(text);
  EXPECT_EQ(3, script.include_patterns_.size());
  EXPECT_EQ(text, script.GetBody());
  EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com")));
  EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com/foo")));
  EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.yahoo.com/bar")));
  EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.msn.com/baz")));
  EXPECT_FALSE(script.MatchesUrl(GURL("http://www.hotmail.com")));
}

TEST(GreasemonkeySlaveTest, Parse2) {
  const std::string text("default to @include *");

  GreasemonkeyScript script("foo");
  script.Parse(text);
  EXPECT_EQ(1, script.include_patterns_.size());
  EXPECT_EQ(text, script.GetBody());
  EXPECT_TRUE(script.MatchesUrl(GURL("foo")));
  EXPECT_TRUE(script.MatchesUrl(GURL("bar")));
}

TEST(GreasemonkeySlaveTest, Parse3) {
  const std::string text(
    "// ==UserScript==\n"
    "// @include *foo*\n"
    "// ==/UserScript=="); // no trailing newline

  GreasemonkeyScript script("foo");
  script.Parse(text);
  EXPECT_EQ(1, script.include_patterns_.size());
  EXPECT_EQ(text, script.GetBody());
  EXPECT_TRUE(script.MatchesUrl(GURL("http://foo.com/bar")));
  EXPECT_FALSE(script.MatchesUrl(GURL("http://baz.org")));
}