summaryrefslogtreecommitdiffstats
path: root/chrome/common/web_apps_unittest.cc
blob: 9f8605f8b769c50a9e0712062bdc09095360fc6c (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2011 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/common/web_apps.h"

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/json_schema_validator.h"
#include "content/common/json_value_serializer.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

DictionaryValue* LoadDefinitionFile(const std::string& name) {
  FilePath path;
  if (!PathService::Get(chrome::DIR_TEST_DATA, &path)) {
    ADD_FAILURE() << "Could not get test data dir.";
    return NULL;
  }

  path = path.AppendASCII("web_app_info").AppendASCII(name.c_str());
  if (!file_util::PathExists(path)) {
    ADD_FAILURE() << "Path does not exist: " << path.value();
    return NULL;
  }

  std::string error;
  JSONFileValueSerializer serializer(path);
  DictionaryValue* result = static_cast<DictionaryValue*>(
      serializer.Deserialize(NULL, &error));
  if (!result) {
    ADD_FAILURE() << "Error parsing " << name << ": " << error;
    return NULL;
  }

  return result;
}

WebApplicationInfo* ParseFromDefinitionAndExpectSuccess(
    const std::string& name) {
  scoped_ptr<DictionaryValue> defintion(LoadDefinitionFile(name));
  if (!defintion.get())
    return NULL;

  scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo());
  web_app->manifest_url = GURL("http://example.com/");

  string16 error;
  if (!web_apps::ParseWebAppFromDefinitionFile(defintion.get(), web_app.get(),
                                               &error)) {
    ADD_FAILURE() << "Error parsing " << name << ": " << UTF16ToUTF8(error);
    return NULL;
  }

  return web_app.release();
}

void ParseFromDefinitionAndExpectFailure(const std::string& name,
                                         const string16& expected_error) {
  scoped_ptr<DictionaryValue> definition(LoadDefinitionFile(name));
  if (!definition.get())
    return;

  WebApplicationInfo web_app;
  web_app.manifest_url = GURL("http://example.com/");

  string16 error;
  if (web_apps::ParseWebAppFromDefinitionFile(definition.get(), &web_app,
                                              &error)) {
    ADD_FAILURE() << "Expected error parsing " << name
                  << " but parse succeeded.";
    return;
  }

  EXPECT_EQ(UTF16ToUTF8(expected_error), UTF16ToUTF8(error)) << name;
}

}

TEST(WebAppInfo, ParseFromDefinitionFileErrors) {
  // Test one definition file with a JSON schema error, just to make sure we're
  // correctly propagating those. We don't extensively test all the properties
  // covered by the schema, since we assume JSON schema is working correctly.
  ParseFromDefinitionAndExpectFailure(
      "missing_name.json",
      UTF8ToUTF16(std::string("name: ") +
                      JSONSchemaValidator::kObjectPropertyIsRequired));

  ParseFromDefinitionAndExpectFailure(
      "invalid_launch_url.json",
      UTF8ToUTF16(WebApplicationInfo::kInvalidLaunchURL));

  ParseFromDefinitionAndExpectFailure(
      "invalid_urls.json",
      UTF8ToUTF16(
          JSONSchemaValidator::FormatErrorMessage(
              WebApplicationInfo::kInvalidURL, "2")));
}

TEST(WebAppInfo, Minimal) {
  scoped_ptr<WebApplicationInfo> web_app(
      ParseFromDefinitionAndExpectSuccess("minimal.json"));

  EXPECT_EQ(UTF8ToUTF16("hello"), web_app->title);
  EXPECT_EQ(UTF8ToUTF16(""), web_app->description);
  EXPECT_EQ(GURL("http://example.com/launch_url"), web_app->app_url);
  EXPECT_EQ(0u, web_app->icons.size());
  EXPECT_EQ(0u, web_app->urls.size());
  EXPECT_EQ(0u, web_app->permissions.size());
  EXPECT_EQ("", web_app->launch_container);
}

TEST(WebAppInfo, Full) {
  scoped_ptr<WebApplicationInfo> web_app(
      ParseFromDefinitionAndExpectSuccess("full.json"));

  EXPECT_EQ(UTF8ToUTF16("hello"), web_app->title);
  EXPECT_EQ(UTF8ToUTF16("This app is super awesome"), web_app->description);
  EXPECT_EQ(GURL("http://example.com/launch_url"), web_app->app_url);
  ASSERT_EQ(1u, web_app->icons.size());
  EXPECT_EQ("http://example.com/16.png", web_app->icons[0].url.spec());
  EXPECT_EQ(16, web_app->icons[0].width);
  EXPECT_EQ(16, web_app->icons[0].height);
  ASSERT_EQ(2u, web_app->urls.size());
  EXPECT_EQ("http://example.com/foobar", web_app->urls[0].spec());
  EXPECT_EQ("http://example.com/baz", web_app->urls[1].spec());
  ASSERT_EQ(2u, web_app->permissions.size());
  EXPECT_EQ("geolocation", web_app->permissions[0]);
  EXPECT_EQ("notifications", web_app->permissions[1]);
  EXPECT_EQ("panel", web_app->launch_container);
}

// Tests ParseIconSizes with various input.
TEST(WebAppInfo, ParseIconSizes) {
  struct TestData {
    const char* input;
    const bool expected_result;
    const bool is_any;
    const size_t expected_size_count;
    const int width1;
    const int height1;
    const int width2;
    const int height2;
  } data[] = {
    // Bogus input cases.
    { "10",         false, false, 0, 0, 0, 0, 0 },
    { "10 10",      false, false, 0, 0, 0, 0, 0 },
    { "010",        false, false, 0, 0, 0, 0, 0 },
    { " 010 ",      false, false, 0, 0, 0, 0, 0 },
    { " 10x ",      false, false, 0, 0, 0, 0, 0 },
    { " x10 ",      false, false, 0, 0, 0, 0, 0 },
    { "any 10x10",  false, false, 0, 0, 0, 0, 0 },
    { "",           false, false, 0, 0, 0, 0, 0 },
    { "10ax11",     false, false, 0, 0, 0, 0, 0 },

    // Any.
    { "any",        true, true, 0, 0, 0, 0, 0 },
    { " any",       true, true, 0, 0, 0, 0, 0 },
    { " any ",      true, true, 0, 0, 0, 0, 0 },

    // Sizes.
    { "10x11",      true, false, 1, 10, 11, 0, 0 },
    { " 10x11 ",    true, false, 1, 10, 11, 0, 0 },
    { " 10x11 1x2", true, false, 2, 10, 11, 1, 2 },
  };
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
    bool is_any;
    std::vector<gfx::Size> sizes;
    bool result = web_apps::ParseIconSizes(ASCIIToUTF16(data[i].input), &sizes,
                                           &is_any);
    ASSERT_EQ(result, data[i].expected_result);
    if (result) {
      ASSERT_EQ(data[i].is_any, is_any);
      ASSERT_EQ(data[i].expected_size_count, sizes.size());
      if (!sizes.empty()) {
        ASSERT_EQ(data[i].width1, sizes[0].width());
        ASSERT_EQ(data[i].height1, sizes[0].height());
      }
      if (sizes.size() > 1) {
        ASSERT_EQ(data[i].width2, sizes[1].width());
        ASSERT_EQ(data[i].height2, sizes[1].height());
      }
    }
  }
}