summaryrefslogtreecommitdiffstats
path: root/chrome/browser/intents/web_intents_util_unittest.cc
blob: 931204b094a86f926a74782adb2a1924a962a176 (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
89
90
91
92
93
94
95
// Copyright (c) 2012 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 "chrome/browser/intents/web_intents_util.h"

#include <string>

#include "base/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace web_intents {
namespace {

bool IsRecognized(const std::string& value) {
  return web_intents::IsRecognizedAction(ASCIIToUTF16(value));
}

ActionId ToAction(const std::string& value) {
  return web_intents::ToActionId(ASCIIToUTF16(value));
}

bool TypesMatch(const std::string& a, const std::string& b) {
  return MimeTypesMatch(ASCIIToUTF16(a), ASCIIToUTF16(b));
}

}  // namespace

TEST(WebIntentsUtilTest, IsRecognizedAction) {
  EXPECT_TRUE(IsRecognized(kActionEdit));
  EXPECT_FALSE(IsRecognized("http://webintents.org/eDit"));  // case matters
  EXPECT_TRUE(IsRecognized(kActionPick));
  EXPECT_TRUE(IsRecognized(kActionSave));
  EXPECT_TRUE(IsRecognized(kActionShare));
  EXPECT_TRUE(IsRecognized(kActionSubscribe));
  EXPECT_TRUE(IsRecognized(kActionView));
}

TEST(WebIntentsUtilTest, IsRecognizedActionFailure) {
  EXPECT_FALSE(IsRecognized(std::string(kActionPick) + "lezooka"));
  EXPECT_FALSE(IsRecognized("Chrome LAX"));
  EXPECT_FALSE(IsRecognized("_zoom "));
  EXPECT_FALSE(IsRecognized("  "));
  EXPECT_FALSE(IsRecognized(""));
}

TEST(WebIntentsUtilTest, ToActionId) {
  EXPECT_EQ(ACTION_ID_EDIT, ToAction(kActionEdit));
  EXPECT_EQ(ACTION_ID_PICK, ToAction(kActionPick));
  EXPECT_EQ(ACTION_ID_SAVE, ToAction(kActionSave));
  EXPECT_EQ(ACTION_ID_SHARE, ToAction(kActionShare));
  EXPECT_EQ(ACTION_ID_SUBSCRIBE, ToAction(kActionSubscribe));
  EXPECT_EQ(ACTION_ID_VIEW, ToAction(kActionView));
}

TEST(WebIntentsUtilTest, MimeTypesMatchLiteral) {
  EXPECT_TRUE(TypesMatch("image/png", "image/png"));

  EXPECT_FALSE(TypesMatch(" image/png", "image/png"));
  EXPECT_FALSE(TypesMatch("image/png", " image/png"));
  EXPECT_FALSE(TypesMatch("image/png ", "image/png"));
  EXPECT_FALSE(TypesMatch("image/png", "image/png "));
  EXPECT_FALSE(TypesMatch("image/jpg", "image/png"));
  EXPECT_FALSE(TypesMatch("image/png", "image/jpg"));
}

TEST(WebIntentsUtilTest, MimeTypesMatchWildCards) {
  EXPECT_TRUE(TypesMatch("*", "*"));
  EXPECT_TRUE(TypesMatch("*", "*/*"));
  EXPECT_TRUE(TypesMatch("*/*", "*"));
  EXPECT_TRUE(TypesMatch("*/*", "*/*"));
  EXPECT_TRUE(TypesMatch("*", "image/png"));
  EXPECT_TRUE(TypesMatch("image/png", "*"));
  EXPECT_TRUE(TypesMatch("*/*", "image/png"));
  EXPECT_TRUE(TypesMatch("image/png", "*/*"));

  EXPECT_FALSE(TypesMatch(" */*", "image/png"));
  EXPECT_FALSE(TypesMatch("*/* ", "image/png"));
  EXPECT_FALSE(TypesMatch("**", "image/png"));
}

TEST(WebIntentsUtilTest, MimeTypesMatchParameters) {
  EXPECT_TRUE(TypesMatch("*", "video/*;single=true"));
  EXPECT_TRUE(TypesMatch("*/*", "video/mpg;single=true"));
  EXPECT_TRUE(TypesMatch("video/*", "video/mpg;single=true"));
  EXPECT_TRUE(TypesMatch("video/mpg", "video/mpg;single=true"));
  EXPECT_TRUE(TypesMatch("video/mpg;single=true", "video/mpg;single=true"));
  EXPECT_TRUE(TypesMatch("video/mpg;a=b;single=true",
                         "video/mpg;single=true;a=b"));
  EXPECT_FALSE(TypesMatch("video/mpg;a=b;single=true",
                          "video/mpg;single=false;a=b"));
  EXPECT_FALSE(TypesMatch("video/mpg;single=true", "video/mpg;single=false"));
}

}  // namepsace web_intents