info_map_unittest.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "extensions/browser/info_map.h"
  5. #include "base/path_service.h"
  6. #include "content/public/test/browser_task_environment.h"
  7. #include "extensions/browser/unloaded_extension_reason.h"
  8. #include "extensions/common/extension.h"
  9. #include "extensions/common/extension_builder.h"
  10. #include "extensions/common/extension_paths.h"
  11. #include "extensions/common/manifest_constants.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace keys = extensions::manifest_keys;
  14. namespace extensions {
  15. class InfoMapTest : public testing::Test {
  16. public:
  17. InfoMapTest() = default;
  18. private:
  19. content::BrowserTaskEnvironment task_environment_;
  20. };
  21. // Returns a barebones test Extension object with the given name.
  22. static scoped_refptr<const Extension> CreateExtension(const std::string& name) {
  23. base::FilePath path;
  24. base::PathService::Get(DIR_TEST_DATA, &path);
  25. return ExtensionBuilder(name).SetPath(path.AppendASCII(name)).Build();
  26. }
  27. // Test that the InfoMap handles refcounting properly.
  28. TEST_F(InfoMapTest, RefCounting) {
  29. scoped_refptr<InfoMap> info_map(new InfoMap());
  30. // New extensions should have a single reference holding onto them.
  31. scoped_refptr<const Extension> extension1(CreateExtension("extension1"));
  32. scoped_refptr<const Extension> extension2(CreateExtension("extension2"));
  33. scoped_refptr<const Extension> extension3(CreateExtension("extension3"));
  34. EXPECT_TRUE(extension1->HasOneRef());
  35. EXPECT_TRUE(extension2->HasOneRef());
  36. EXPECT_TRUE(extension3->HasOneRef());
  37. // Add a ref to each extension and give it to the info map.
  38. info_map->AddExtension(extension1.get(), base::Time(), false, false);
  39. info_map->AddExtension(extension2.get(), base::Time(), false, false);
  40. info_map->AddExtension(extension3.get(), base::Time(), false, false);
  41. // Release extension1, and the info map should have the only ref.
  42. const Extension* weak_extension1 = extension1.get();
  43. extension1.reset();
  44. EXPECT_TRUE(weak_extension1->HasOneRef());
  45. // Remove extension2, and the extension2 object should have the only ref.
  46. info_map->RemoveExtension(extension2->id());
  47. EXPECT_TRUE(extension2->HasOneRef());
  48. // Delete the info map, and the extension3 object should have the only ref.
  49. info_map.reset();
  50. EXPECT_TRUE(extension3->HasOneRef());
  51. }
  52. // Tests that we can query a few extension properties from the InfoMap.
  53. TEST_F(InfoMapTest, Properties) {
  54. scoped_refptr<InfoMap> info_map(new InfoMap());
  55. scoped_refptr<const Extension> extension1(CreateExtension("extension1"));
  56. scoped_refptr<const Extension> extension2(CreateExtension("extension2"));
  57. info_map->AddExtension(extension1.get(), base::Time(), false, false);
  58. info_map->AddExtension(extension2.get(), base::Time(), false, false);
  59. EXPECT_EQ(2u, info_map->extensions().size());
  60. EXPECT_EQ(extension1.get(), info_map->extensions().GetByID(extension1->id()));
  61. EXPECT_EQ(extension2.get(), info_map->extensions().GetByID(extension2->id()));
  62. }
  63. } // namespace extensions