| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/arc/intent_helper/activity_icon_loader.h" | 5 #include "components/arc/intent_helper/activity_icon_loader.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 13 |
| 15 namespace arc { | 14 namespace arc { |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 void OnIconsReady0( | 18 void OnIconsReady0( |
| 20 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> activity_to_icons) { | 19 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> activity_to_icons) { |
| 21 EXPECT_EQ(3U, activity_to_icons->size()); | 20 EXPECT_EQ(3U, activity_to_icons->size()); |
| 22 EXPECT_EQ(1U, activity_to_icons->count( | 21 EXPECT_EQ(1U, activity_to_icons->count( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 45 EXPECT_EQ(1U, activity_to_icons->count( | 44 EXPECT_EQ(1U, activity_to_icons->count( |
| 46 ActivityIconLoader::ActivityName("p1", "a1"))); | 45 ActivityIconLoader::ActivityName("p1", "a1"))); |
| 47 EXPECT_EQ(1U, activity_to_icons->count( | 46 EXPECT_EQ(1U, activity_to_icons->count( |
| 48 ActivityIconLoader::ActivityName("p2", "a2"))); | 47 ActivityIconLoader::ActivityName("p2", "a2"))); |
| 49 } | 48 } |
| 50 | 49 |
| 51 } // namespace | 50 } // namespace |
| 52 | 51 |
| 53 // Tests if InvalidateIcons properly cleans up the cache. | 52 // Tests if InvalidateIcons properly cleans up the cache. |
| 54 TEST(ActivityIconLoaderTest, TestInvalidateIcons) { | 53 TEST(ActivityIconLoaderTest, TestInvalidateIcons) { |
| 55 scoped_refptr<ActivityIconLoader> loader(new ActivityIconLoader); | 54 ActivityIconLoader loader; |
| 56 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a0")); | 55 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a0")); |
| 57 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a1")); | 56 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a1")); |
| 58 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a0")); | 57 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a0")); |
| 59 EXPECT_EQ(3U, loader->cached_icons_for_testing().size()); | 58 EXPECT_EQ(3U, loader.cached_icons_for_testing().size()); |
| 60 | 59 |
| 61 loader->InvalidateIcons("p2"); // delete none. | 60 loader.InvalidateIcons("p2"); // delete none. |
| 62 EXPECT_EQ(3U, loader->cached_icons_for_testing().size()); | 61 EXPECT_EQ(3U, loader.cached_icons_for_testing().size()); |
| 63 | 62 |
| 64 loader->InvalidateIcons("p0"); // delete 2 entries. | 63 loader.InvalidateIcons("p0"); // delete 2 entries. |
| 65 EXPECT_EQ(1U, loader->cached_icons_for_testing().size()); | 64 EXPECT_EQ(1U, loader.cached_icons_for_testing().size()); |
| 66 | 65 |
| 67 loader->InvalidateIcons("p2"); // delete none. | 66 loader.InvalidateIcons("p2"); // delete none. |
| 68 EXPECT_EQ(1U, loader->cached_icons_for_testing().size()); | 67 EXPECT_EQ(1U, loader.cached_icons_for_testing().size()); |
| 69 | 68 |
| 70 loader->InvalidateIcons("p1"); // delete 1 entry. | 69 loader.InvalidateIcons("p1"); // delete 1 entry. |
| 71 EXPECT_EQ(0U, loader->cached_icons_for_testing().size()); | 70 EXPECT_EQ(0U, loader.cached_icons_for_testing().size()); |
| 72 } | 71 } |
| 73 | 72 |
| 74 // Tests if GetActivityIcons immediately returns cached icons. | 73 // Tests if GetActivityIcons immediately returns cached icons. |
| 75 TEST(ActivityIconLoaderTest, TestGetActivityIcons) { | 74 TEST(ActivityIconLoaderTest, TestGetActivityIcons) { |
| 76 scoped_refptr<ActivityIconLoader> loader(new ActivityIconLoader); | 75 ActivityIconLoader loader; |
| 77 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a0")); | 76 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a0")); |
| 78 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a1")); | 77 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a1")); |
| 79 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a0")); | 78 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a0")); |
| 80 | 79 |
| 81 // Check that GetActivityIcons() immediately calls OnIconsReady0() with all | 80 // Check that GetActivityIcons() immediately calls OnIconsReady0() with all |
| 82 // locally cached icons. | 81 // locally cached icons. |
| 83 std::vector<ActivityIconLoader::ActivityName> activities; | 82 std::vector<ActivityIconLoader::ActivityName> activities; |
| 84 activities.emplace_back("p0", "a0"); | 83 activities.emplace_back("p0", "a0"); |
| 85 activities.emplace_back("p1", "a1"); | 84 activities.emplace_back("p1", "a1"); |
| 86 activities.emplace_back("p1", "a0"); | 85 activities.emplace_back("p1", "a0"); |
| 87 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, | 86 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, |
| 88 loader->GetActivityIcons(activities, base::Bind(&OnIconsReady0))); | 87 loader.GetActivityIcons(activities, base::Bind(&OnIconsReady0))); |
| 89 | 88 |
| 90 // Test with different |activities|. | 89 // Test with different |activities|. |
| 91 activities.clear(); | 90 activities.clear(); |
| 92 activities.emplace_back("p1", "a1"); | 91 activities.emplace_back("p1", "a1"); |
| 93 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, | 92 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, |
| 94 loader->GetActivityIcons(activities, base::Bind(&OnIconsReady1))); | 93 loader.GetActivityIcons(activities, base::Bind(&OnIconsReady1))); |
| 95 activities.clear(); | 94 activities.clear(); |
| 96 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, | 95 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, |
| 97 loader->GetActivityIcons(activities, base::Bind(&OnIconsReady2))); | 96 loader.GetActivityIcons(activities, base::Bind(&OnIconsReady2))); |
| 98 activities.emplace_back("p1", "a_unknown"); | 97 activities.emplace_back("p1", "a_unknown"); |
| 99 EXPECT_EQ(ActivityIconLoader::GetResult::FAILED_ARC_NOT_SUPPORTED, | 98 EXPECT_EQ(ActivityIconLoader::GetResult::FAILED_ARC_NOT_SUPPORTED, |
| 100 loader->GetActivityIcons(activities, base::Bind(&OnIconsReady2))); | 99 loader.GetActivityIcons(activities, base::Bind(&OnIconsReady2))); |
| 101 } | 100 } |
| 102 | 101 |
| 103 // Tests if OnIconsResized updates the cache. | 102 // Tests if OnIconsResized updates the cache. |
| 104 TEST(ActivityIconLoaderTest, TestOnIconsResized) { | 103 TEST(ActivityIconLoaderTest, TestOnIconsResized) { |
| 105 scoped_refptr<ActivityIconLoader> loader(new ActivityIconLoader); | |
| 106 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> activity_to_icons( | 104 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> activity_to_icons( |
| 107 new ActivityIconLoader::ActivityToIconsMap); | 105 new ActivityIconLoader::ActivityToIconsMap); |
| 108 activity_to_icons->insert(std::make_pair( | 106 activity_to_icons->insert(std::make_pair( |
| 109 ActivityIconLoader::ActivityName("p0", "a0"), | 107 ActivityIconLoader::ActivityName("p0", "a0"), |
| 110 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 108 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 111 activity_to_icons->insert(std::make_pair( | 109 activity_to_icons->insert(std::make_pair( |
| 112 ActivityIconLoader::ActivityName("p1", "a1"), | 110 ActivityIconLoader::ActivityName("p1", "a1"), |
| 113 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 111 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 114 activity_to_icons->insert(std::make_pair( | 112 activity_to_icons->insert(std::make_pair( |
| 115 ActivityIconLoader::ActivityName("p1", "a0"), | 113 ActivityIconLoader::ActivityName("p1", "a0"), |
| 116 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 114 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 117 // Duplicated entey which should be ignored. | 115 // Duplicated entey which should be ignored. |
| 118 activity_to_icons->insert(std::make_pair( | 116 activity_to_icons->insert(std::make_pair( |
| 119 ActivityIconLoader::ActivityName("p0", "a0"), | 117 ActivityIconLoader::ActivityName("p0", "a0"), |
| 120 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 118 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 121 | 119 |
| 120 ActivityIconLoader loader; |
| 121 |
| 122 // Call OnIconsResized() and check that the cache is properly updated. | 122 // Call OnIconsResized() and check that the cache is properly updated. |
| 123 loader->OnIconsResizedForTesting(base::Bind(&OnIconsReady0), | 123 loader.OnIconsResizedForTesting(base::Bind(&OnIconsReady0), |
| 124 std::move(activity_to_icons)); | 124 std::move(activity_to_icons)); |
| 125 EXPECT_EQ(3U, loader->cached_icons_for_testing().size()); | 125 EXPECT_EQ(3U, loader.cached_icons_for_testing().size()); |
| 126 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 126 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 127 ActivityIconLoader::ActivityName("p0", "a0"))); | 127 ActivityIconLoader::ActivityName("p0", "a0"))); |
| 128 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 128 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 129 ActivityIconLoader::ActivityName("p1", "a1"))); | 129 ActivityIconLoader::ActivityName("p1", "a1"))); |
| 130 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 130 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 131 ActivityIconLoader::ActivityName("p1", "a0"))); | 131 ActivityIconLoader::ActivityName("p1", "a0"))); |
| 132 | 132 |
| 133 // Call OnIconsResized() again to make sure that the second call does not | 133 // Call OnIconsResized() again to make sure that the second call does not |
| 134 // remove the cache the previous call added. | 134 // remove the cache the previous call added. |
| 135 activity_to_icons.reset(new ActivityIconLoader::ActivityToIconsMap); | 135 activity_to_icons.reset(new ActivityIconLoader::ActivityToIconsMap); |
| 136 // Duplicated entry. | 136 // Duplicated entry. |
| 137 activity_to_icons->insert(std::make_pair( | 137 activity_to_icons->insert(std::make_pair( |
| 138 ActivityIconLoader::ActivityName("p1", "a1"), | 138 ActivityIconLoader::ActivityName("p1", "a1"), |
| 139 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 139 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 140 // New entry. | 140 // New entry. |
| 141 activity_to_icons->insert(std::make_pair( | 141 activity_to_icons->insert(std::make_pair( |
| 142 ActivityIconLoader::ActivityName("p2", "a2"), | 142 ActivityIconLoader::ActivityName("p2", "a2"), |
| 143 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 143 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 144 loader->OnIconsResizedForTesting(base::Bind(&OnIconsReady3), | 144 loader.OnIconsResizedForTesting(base::Bind(&OnIconsReady3), |
| 145 std::move(activity_to_icons)); | 145 std::move(activity_to_icons)); |
| 146 EXPECT_EQ(4U, loader->cached_icons_for_testing().size()); | 146 EXPECT_EQ(4U, loader.cached_icons_for_testing().size()); |
| 147 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 147 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 148 ActivityIconLoader::ActivityName("p0", "a0"))); | 148 ActivityIconLoader::ActivityName("p0", "a0"))); |
| 149 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 149 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 150 ActivityIconLoader::ActivityName("p1", "a1"))); | 150 ActivityIconLoader::ActivityName("p1", "a1"))); |
| 151 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 151 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 152 ActivityIconLoader::ActivityName("p1", "a0"))); | 152 ActivityIconLoader::ActivityName("p1", "a0"))); |
| 153 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 153 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 154 ActivityIconLoader::ActivityName("p2", "a2"))); | 154 ActivityIconLoader::ActivityName("p2", "a2"))); |
| 155 } | 155 } |
| 156 | 156 |
| 157 } // namespace arc | 157 } // namespace arc |
| OLD | NEW |