summaryrefslogtreecommitdiffstats
path: root/extensions/common/manifest_handlers/file_handler_manifest_unittest.cc
blob: d55c14848be2632af82ea5a5b87d0659fc25f222 (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
// Copyright 2014 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/macros.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/file_handler_info.h"
#include "extensions/common/manifest_test.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

namespace errors = manifest_errors;

typedef ManifestTest FileHandlersManifestTest;

TEST_F(FileHandlersManifestTest, InvalidFileHandlers) {
  Testcase testcases[] = {
    Testcase("file_handlers_invalid_handlers.json",
             errors::kInvalidFileHandlers),
    Testcase("file_handlers_invalid_type.json",
             errors::kInvalidFileHandlerType),
    Testcase("file_handlers_invalid_extension.json",
             errors::kInvalidFileHandlerExtension),
    Testcase("file_handlers_invalid_no_type_or_extension.json",
             errors::kInvalidFileHandlerNoTypeOrExtension),
    Testcase("file_handlers_invalid_type_element.json",
             errors::kInvalidFileHandlerTypeElement),
    Testcase("file_handlers_invalid_extension_element.json",
             errors::kInvalidFileHandlerExtensionElement),
    Testcase("file_handlers_invalid_too_many.json",
             errors::kInvalidFileHandlersTooManyTypesAndExtensions),
  };
  RunTestcases(testcases, arraysize(testcases), EXPECT_TYPE_ERROR);
}

TEST_F(FileHandlersManifestTest, ValidFileHandlers) {
  scoped_refptr<const Extension> extension =
      LoadAndExpectSuccess("file_handlers_valid.json");

  ASSERT_TRUE(extension.get());
  const FileHandlersInfo* handlers =
      FileHandlers::GetFileHandlers(extension.get());
  ASSERT_TRUE(handlers != NULL);
  ASSERT_EQ(2U, handlers->size());

  FileHandlerInfo handler = handlers->at(0);
  EXPECT_EQ("image", handler.id);
  EXPECT_EQ(1U, handler.types.size());
  EXPECT_EQ(1U, handler.types.count("image/*"));
  EXPECT_EQ(2U, handler.extensions.size());
  EXPECT_EQ(1U, handler.extensions.count(".png"));
  EXPECT_EQ(1U, handler.extensions.count(".gif"));

  handler = handlers->at(1);
  EXPECT_EQ("text", handler.id);
  EXPECT_EQ(1U, handler.types.size());
  EXPECT_EQ(1U, handler.types.count("text/*"));
  EXPECT_EQ(0U, handler.extensions.size());
}

TEST_F(FileHandlersManifestTest, NotPlatformApp) {
  // This should load successfully but have the file handlers ignored.
  scoped_refptr<const Extension> extension =
      LoadAndExpectSuccess("file_handlers_invalid_not_app.json");

  ASSERT_TRUE(extension.get());
  const FileHandlersInfo* handlers =
      FileHandlers::GetFileHandlers(extension.get());
  ASSERT_TRUE(handlers == NULL);
}

}  // namespace extensions