Chromium Code Reviews| Index: chrome/common/web_apps_unittest.cc |
| diff --git a/chrome/common/web_apps_unittest.cc b/chrome/common/web_apps_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c4398fd9723be3b73bf6e68e7a4e1a9f3d1dee5c |
| --- /dev/null |
| +++ b/chrome/common/web_apps_unittest.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright (c) 2010 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/path_service.h" |
| +#include "base/scoped_ptr.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 "chrome/common/json_value_serializer.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +DictionaryValue* LoadDefinitionFile(const std::string& name) { |
| + FilePath path; |
| + PathService::Get(chrome::DIR_TEST_DATA, &path); |
|
Paweł Hajdan Jr.
2010/11/15 08:05:31
Check return value of this.
Aaron Boodman
2010/11/16 03:44:45
Done.
|
| + path = path.AppendASCII("web_app_info").AppendASCII(name.c_str()); |
| + if (!file_util::PathExists(path)) { |
| + ADD_FAILURE() << L"Path does not exist: " << path.ToWStringHack(); |
|
Erik does not do reviews
2010/11/15 19:45:36
I think you can just use value() instead of ToWStr
Aaron Boodman
2010/11/16 03:44:45
Done.
|
| + 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 (!ParseWebAppFromDefinitionFile(*defintion, 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 (ParseWebAppFromDefinitionFile(*definition, &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->privileges.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->privileges.size()); |
| + EXPECT_EQ("geolocation", web_app->privileges[0]); |
| + EXPECT_EQ("notifications", web_app->privileges[1]); |
| + EXPECT_EQ("panel", web_app->launch_container); |
| +} |