| 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 namespace internal { |
| 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( |
| 23 ActivityIconLoader::ActivityName("p0", "a0"))); | 22 ActivityIconLoader::ActivityName("p0", "a0"))); |
| 24 EXPECT_EQ(1U, activity_to_icons->count( | 23 EXPECT_EQ(1U, activity_to_icons->count( |
| 25 ActivityIconLoader::ActivityName("p1", "a1"))); | 24 ActivityIconLoader::ActivityName("p1", "a1"))); |
| 26 EXPECT_EQ(1U, activity_to_icons->count( | 25 EXPECT_EQ(1U, activity_to_icons->count( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 41 | 40 |
| 42 void OnIconsReady3( | 41 void OnIconsReady3( |
| 43 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> activity_to_icons) { | 42 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> activity_to_icons) { |
| 44 EXPECT_EQ(2U, activity_to_icons->size()); | 43 EXPECT_EQ(2U, activity_to_icons->size()); |
| 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 | |
| 52 | |
| 53 // Tests if InvalidateIcons properly cleans up the cache. | 50 // Tests if InvalidateIcons properly cleans up the cache. |
| 54 TEST(ActivityIconLoaderTest, TestInvalidateIcons) { | 51 TEST(ActivityIconLoaderTest, TestInvalidateIcons) { |
| 55 scoped_refptr<ActivityIconLoader> loader(new ActivityIconLoader); | 52 ActivityIconLoader loader; |
| 56 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a0")); | 53 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a0")); |
| 57 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a1")); | 54 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a1")); |
| 58 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a0")); | 55 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a0")); |
| 59 EXPECT_EQ(3U, loader->cached_icons_for_testing().size()); | 56 EXPECT_EQ(3U, loader.cached_icons_for_testing().size()); |
| 60 | 57 |
| 61 loader->InvalidateIcons("p2"); // delete none. | 58 loader.InvalidateIcons("p2"); // delete none. |
| 62 EXPECT_EQ(3U, loader->cached_icons_for_testing().size()); | 59 EXPECT_EQ(3U, loader.cached_icons_for_testing().size()); |
| 63 | 60 |
| 64 loader->InvalidateIcons("p0"); // delete 2 entries. | 61 loader.InvalidateIcons("p0"); // delete 2 entries. |
| 65 EXPECT_EQ(1U, loader->cached_icons_for_testing().size()); | 62 EXPECT_EQ(1U, loader.cached_icons_for_testing().size()); |
| 66 | 63 |
| 67 loader->InvalidateIcons("p2"); // delete none. | 64 loader.InvalidateIcons("p2"); // delete none. |
| 68 EXPECT_EQ(1U, loader->cached_icons_for_testing().size()); | 65 EXPECT_EQ(1U, loader.cached_icons_for_testing().size()); |
| 69 | 66 |
| 70 loader->InvalidateIcons("p1"); // delete 1 entry. | 67 loader.InvalidateIcons("p1"); // delete 1 entry. |
| 71 EXPECT_EQ(0U, loader->cached_icons_for_testing().size()); | 68 EXPECT_EQ(0U, loader.cached_icons_for_testing().size()); |
| 72 } | 69 } |
| 73 | 70 |
| 74 // Tests if GetActivityIcons immediately returns cached icons. | 71 // Tests if GetActivityIcons immediately returns cached icons. |
| 75 TEST(ActivityIconLoaderTest, TestGetActivityIcons) { | 72 TEST(ActivityIconLoaderTest, TestGetActivityIcons) { |
| 76 scoped_refptr<ActivityIconLoader> loader(new ActivityIconLoader); | 73 ActivityIconLoader loader; |
| 77 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a0")); | 74 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p0", "a0")); |
| 78 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a1")); | 75 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a1")); |
| 79 loader->AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a0")); | 76 loader.AddCacheEntryForTesting(ActivityIconLoader::ActivityName("p1", "a0")); |
| 80 | 77 |
| 81 // Check that GetActivityIcons() immediately calls OnIconsReady0() with all | 78 // Check that GetActivityIcons() immediately calls OnIconsReady0() with all |
| 82 // locally cached icons. | 79 // locally cached icons. |
| 83 std::vector<ActivityIconLoader::ActivityName> activities; | 80 std::vector<ActivityIconLoader::ActivityName> activities; |
| 84 activities.emplace_back("p0", "a0"); | 81 activities.emplace_back("p0", "a0"); |
| 85 activities.emplace_back("p1", "a1"); | 82 activities.emplace_back("p1", "a1"); |
| 86 activities.emplace_back("p1", "a0"); | 83 activities.emplace_back("p1", "a0"); |
| 87 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, | 84 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, |
| 88 loader->GetActivityIcons(activities, base::Bind(&OnIconsReady0))); | 85 loader.GetActivityIcons(activities, base::Bind(&OnIconsReady0))); |
| 89 | 86 |
| 90 // Test with different |activities|. | 87 // Test with different |activities|. |
| 91 activities.clear(); | 88 activities.clear(); |
| 92 activities.emplace_back("p1", "a1"); | 89 activities.emplace_back("p1", "a1"); |
| 93 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, | 90 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, |
| 94 loader->GetActivityIcons(activities, base::Bind(&OnIconsReady1))); | 91 loader.GetActivityIcons(activities, base::Bind(&OnIconsReady1))); |
| 95 activities.clear(); | 92 activities.clear(); |
| 96 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, | 93 EXPECT_EQ(ActivityIconLoader::GetResult::SUCCEEDED_SYNC, |
| 97 loader->GetActivityIcons(activities, base::Bind(&OnIconsReady2))); | 94 loader.GetActivityIcons(activities, base::Bind(&OnIconsReady2))); |
| 98 activities.emplace_back("p1", "a_unknown"); | 95 activities.emplace_back("p1", "a_unknown"); |
| 99 EXPECT_EQ(ActivityIconLoader::GetResult::FAILED_ARC_NOT_SUPPORTED, | 96 EXPECT_EQ(ActivityIconLoader::GetResult::FAILED_ARC_NOT_SUPPORTED, |
| 100 loader->GetActivityIcons(activities, base::Bind(&OnIconsReady2))); | 97 loader.GetActivityIcons(activities, base::Bind(&OnIconsReady2))); |
| 101 } | 98 } |
| 102 | 99 |
| 103 // Tests if OnIconsResized updates the cache. | 100 // Tests if OnIconsResized updates the cache. |
| 104 TEST(ActivityIconLoaderTest, TestOnIconsResized) { | 101 TEST(ActivityIconLoaderTest, TestOnIconsResized) { |
| 105 scoped_refptr<ActivityIconLoader> loader(new ActivityIconLoader); | |
| 106 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> activity_to_icons( | 102 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> activity_to_icons( |
| 107 new ActivityIconLoader::ActivityToIconsMap); | 103 new ActivityIconLoader::ActivityToIconsMap); |
| 108 activity_to_icons->insert(std::make_pair( | 104 activity_to_icons->insert(std::make_pair( |
| 109 ActivityIconLoader::ActivityName("p0", "a0"), | 105 ActivityIconLoader::ActivityName("p0", "a0"), |
| 110 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 106 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 111 activity_to_icons->insert(std::make_pair( | 107 activity_to_icons->insert(std::make_pair( |
| 112 ActivityIconLoader::ActivityName("p1", "a1"), | 108 ActivityIconLoader::ActivityName("p1", "a1"), |
| 113 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 109 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 114 activity_to_icons->insert(std::make_pair( | 110 activity_to_icons->insert(std::make_pair( |
| 115 ActivityIconLoader::ActivityName("p1", "a0"), | 111 ActivityIconLoader::ActivityName("p1", "a0"), |
| 116 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 112 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 117 // Duplicated entey which should be ignored. | 113 // Duplicated entey which should be ignored. |
| 118 activity_to_icons->insert(std::make_pair( | 114 activity_to_icons->insert(std::make_pair( |
| 119 ActivityIconLoader::ActivityName("p0", "a0"), | 115 ActivityIconLoader::ActivityName("p0", "a0"), |
| 120 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 116 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 121 | 117 |
| 118 ActivityIconLoader loader; |
| 119 |
| 122 // Call OnIconsResized() and check that the cache is properly updated. | 120 // Call OnIconsResized() and check that the cache is properly updated. |
| 123 loader->OnIconsResizedForTesting(base::Bind(&OnIconsReady0), | 121 loader.OnIconsResizedForTesting(base::Bind(&OnIconsReady0), |
| 124 std::move(activity_to_icons)); | 122 std::move(activity_to_icons)); |
| 125 EXPECT_EQ(3U, loader->cached_icons_for_testing().size()); | 123 EXPECT_EQ(3U, loader.cached_icons_for_testing().size()); |
| 126 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 124 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 127 ActivityIconLoader::ActivityName("p0", "a0"))); | 125 ActivityIconLoader::ActivityName("p0", "a0"))); |
| 128 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 126 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 129 ActivityIconLoader::ActivityName("p1", "a1"))); | 127 ActivityIconLoader::ActivityName("p1", "a1"))); |
| 130 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 128 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 131 ActivityIconLoader::ActivityName("p1", "a0"))); | 129 ActivityIconLoader::ActivityName("p1", "a0"))); |
| 132 | 130 |
| 133 // Call OnIconsResized() again to make sure that the second call does not | 131 // Call OnIconsResized() again to make sure that the second call does not |
| 134 // remove the cache the previous call added. | 132 // remove the cache the previous call added. |
| 135 activity_to_icons.reset(new ActivityIconLoader::ActivityToIconsMap); | 133 activity_to_icons.reset(new ActivityIconLoader::ActivityToIconsMap); |
| 136 // Duplicated entry. | 134 // Duplicated entry. |
| 137 activity_to_icons->insert(std::make_pair( | 135 activity_to_icons->insert(std::make_pair( |
| 138 ActivityIconLoader::ActivityName("p1", "a1"), | 136 ActivityIconLoader::ActivityName("p1", "a1"), |
| 139 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 137 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 140 // New entry. | 138 // New entry. |
| 141 activity_to_icons->insert(std::make_pair( | 139 activity_to_icons->insert(std::make_pair( |
| 142 ActivityIconLoader::ActivityName("p2", "a2"), | 140 ActivityIconLoader::ActivityName("p2", "a2"), |
| 143 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); | 141 ActivityIconLoader::Icons(gfx::Image(), gfx::Image(), nullptr))); |
| 144 loader->OnIconsResizedForTesting(base::Bind(&OnIconsReady3), | 142 loader.OnIconsResizedForTesting(base::Bind(&OnIconsReady3), |
| 145 std::move(activity_to_icons)); | 143 std::move(activity_to_icons)); |
| 146 EXPECT_EQ(4U, loader->cached_icons_for_testing().size()); | 144 EXPECT_EQ(4U, loader.cached_icons_for_testing().size()); |
| 147 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 145 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 148 ActivityIconLoader::ActivityName("p0", "a0"))); | 146 ActivityIconLoader::ActivityName("p0", "a0"))); |
| 149 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 147 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 150 ActivityIconLoader::ActivityName("p1", "a1"))); | 148 ActivityIconLoader::ActivityName("p1", "a1"))); |
| 151 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 149 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 152 ActivityIconLoader::ActivityName("p1", "a0"))); | 150 ActivityIconLoader::ActivityName("p1", "a0"))); |
| 153 EXPECT_EQ(1U, loader->cached_icons_for_testing().count( | 151 EXPECT_EQ(1U, loader.cached_icons_for_testing().count( |
| 154 ActivityIconLoader::ActivityName("p2", "a2"))); | 152 ActivityIconLoader::ActivityName("p2", "a2"))); |
| 155 } | 153 } |
| 156 | 154 |
| 155 } // namespace |
| 156 } // namespace internal |
| 157 } // namespace arc | 157 } // namespace arc |
| OLD | NEW |