Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // TODO(rickcam): Bug 73183: Add unit tests for image loading | 5 // TODO(rickcam): Bug 73183: Add unit tests for image loading |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "chrome/browser/background/background_application_list_model.h" | 10 #include "chrome/browser/background/background_application_list_model.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 std::string GenerateUniqueExtensionName() { | 77 std::string GenerateUniqueExtensionName() { |
| 78 static int uniqueness = 0; | 78 static int uniqueness = 0; |
| 79 std::ostringstream output; | 79 std::ostringstream output; |
| 80 output << "Unique Named Extension " << uniqueness; | 80 output << "Unique Named Extension " << uniqueness; |
| 81 ++uniqueness; | 81 ++uniqueness; |
| 82 return output.str(); | 82 return output.str(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void AddBackgroundPermission(ExtensionService* service, | 85 void AddBackgroundPermission(ExtensionService* service, |
| 86 Extension* extension) { | 86 Extension* extension) { |
| 87 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) return; | 87 if (BackgroundApplicationListModel::IsBackgroundApp(*extension, |
|
Mihai Parparita -not on Chrome
2012/05/03 00:54:11
Nit: You may want to make this into a IsBackground
Andrew T Wilson (Slow)
2012/05/03 07:42:56
Done.
| |
| 88 service->profile())) { | |
| 89 return; | |
| 90 } | |
| 88 | 91 |
| 89 static scoped_refptr<Extension> temporary = | 92 static scoped_refptr<Extension> temporary = |
| 90 CreateExtension(GenerateUniqueExtensionName(), true); | 93 CreateExtension(GenerateUniqueExtensionName(), true); |
| 91 scoped_refptr<const ExtensionPermissionSet> permissions = | 94 scoped_refptr<const ExtensionPermissionSet> permissions = |
| 92 temporary->GetActivePermissions(); | 95 temporary->GetActivePermissions(); |
| 93 extensions::PermissionsUpdater(service->profile()).AddPermissions( | 96 extensions::PermissionsUpdater(service->profile()).AddPermissions( |
| 94 extension, permissions.get()); | 97 extension, permissions.get()); |
| 95 } | 98 } |
| 96 | 99 |
| 97 void RemoveBackgroundPermission(ExtensionService* service, | 100 void RemoveBackgroundPermission(ExtensionService* service, |
| 98 Extension* extension) { | 101 Extension* extension) { |
| 99 if (!BackgroundApplicationListModel::IsBackgroundApp(*extension)) return; | 102 if (!BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 103 service->profile())) { | |
| 104 return; | |
| 105 } | |
| 100 extensions::PermissionsUpdater(service->profile()).RemovePermissions( | 106 extensions::PermissionsUpdater(service->profile()).RemovePermissions( |
| 101 extension, extension->GetActivePermissions()); | 107 extension, extension->GetActivePermissions()); |
| 102 } | 108 } |
| 103 } // namespace | 109 } // namespace |
| 104 | 110 |
| 105 // With minimal test logic, verifies behavior over an explicit set of | 111 // With minimal test logic, verifies behavior over an explicit set of |
| 106 // extensions, of which some are Background Apps and others are not. | 112 // extensions, of which some are Background Apps and others are not. |
| 107 TEST_F(BackgroundApplicationListModelTest, ExplicitTest) { | 113 TEST_F(BackgroundApplicationListModelTest, ExplicitTest) { |
| 108 InitializeAndLoadEmptyExtensionService(); | 114 InitializeAndLoadEmptyExtensionService(); |
| 109 ExtensionService* service = profile_->GetExtensionService(); | 115 ExtensionService* service = profile_->GetExtensionService(); |
| 110 ASSERT_TRUE(service); | 116 ASSERT_TRUE(service); |
| 111 ASSERT_TRUE(service->is_ready()); | 117 ASSERT_TRUE(service->is_ready()); |
| 112 ASSERT_TRUE(service->extensions()); | 118 ASSERT_TRUE(service->extensions()); |
| 113 ASSERT_TRUE(service->extensions()->is_empty()); | 119 ASSERT_TRUE(service->extensions()->is_empty()); |
| 114 scoped_ptr<BackgroundApplicationListModel> model( | 120 scoped_ptr<BackgroundApplicationListModel> model( |
| 115 new BackgroundApplicationListModel(profile_.get())); | 121 new BackgroundApplicationListModel(profile_.get())); |
| 116 ASSERT_EQ(0U, model->size()); | 122 ASSERT_EQ(0U, model->size()); |
| 117 | 123 |
| 118 scoped_refptr<Extension> ext1 = CreateExtension("alpha", false); | 124 scoped_refptr<Extension> ext1 = CreateExtension("alpha", false); |
| 119 scoped_refptr<Extension> ext2 = CreateExtension("bravo", false); | 125 scoped_refptr<Extension> ext2 = CreateExtension("bravo", false); |
| 120 scoped_refptr<Extension> ext3 = CreateExtension("charlie", false); | 126 scoped_refptr<Extension> ext3 = CreateExtension("charlie", false); |
| 121 scoped_refptr<Extension> bgapp1 = CreateExtension("delta", true); | 127 scoped_refptr<Extension> bgapp1 = CreateExtension("delta", true); |
| 122 scoped_refptr<Extension> bgapp2 = CreateExtension("echo", true); | 128 scoped_refptr<Extension> bgapp2 = CreateExtension("echo", true); |
| 123 ASSERT_TRUE(service->extensions() != NULL); | 129 ASSERT_TRUE(service->extensions() != NULL); |
| 124 ASSERT_EQ(0U, service->extensions()->size()); | 130 ASSERT_EQ(0U, service->extensions()->size()); |
| 125 ASSERT_EQ(0U, model->size()); | 131 ASSERT_EQ(0U, model->size()); |
| 126 | 132 |
| 127 // Add alternating Extensions and Background Apps | 133 // Add alternating Extensions and Background Apps |
| 128 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext1)); | 134 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext1, |
| 135 profile_.get())); | |
| 129 service->AddExtension(ext1); | 136 service->AddExtension(ext1); |
| 130 ASSERT_EQ(1U, service->extensions()->size()); | 137 ASSERT_EQ(1U, service->extensions()->size()); |
| 131 ASSERT_EQ(0U, model->size()); | 138 ASSERT_EQ(0U, model->size()); |
| 132 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp1)); | 139 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp1, |
| 140 profile_.get())); | |
| 133 service->AddExtension(bgapp1); | 141 service->AddExtension(bgapp1); |
| 134 ASSERT_EQ(2U, service->extensions()->size()); | 142 ASSERT_EQ(2U, service->extensions()->size()); |
| 135 ASSERT_EQ(1U, model->size()); | 143 ASSERT_EQ(1U, model->size()); |
| 136 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext2)); | 144 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext2, |
| 145 profile_.get())); | |
| 137 service->AddExtension(ext2); | 146 service->AddExtension(ext2); |
| 138 ASSERT_EQ(3U, service->extensions()->size()); | 147 ASSERT_EQ(3U, service->extensions()->size()); |
| 139 ASSERT_EQ(1U, model->size()); | 148 ASSERT_EQ(1U, model->size()); |
| 140 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp2)); | 149 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp2, |
| 150 profile_.get())); | |
| 141 service->AddExtension(bgapp2); | 151 service->AddExtension(bgapp2); |
| 142 ASSERT_EQ(4U, service->extensions()->size()); | 152 ASSERT_EQ(4U, service->extensions()->size()); |
| 143 ASSERT_EQ(2U, model->size()); | 153 ASSERT_EQ(2U, model->size()); |
| 144 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext3)); | 154 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext3, |
| 155 profile_.get())); | |
| 145 service->AddExtension(ext3); | 156 service->AddExtension(ext3); |
| 146 ASSERT_EQ(5U, service->extensions()->size()); | 157 ASSERT_EQ(5U, service->extensions()->size()); |
| 147 ASSERT_EQ(2U, model->size()); | 158 ASSERT_EQ(2U, model->size()); |
| 148 | 159 |
| 149 // Remove in FIFO order. | 160 // Remove in FIFO order. |
| 150 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext1)); | 161 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext1, |
| 162 profile_.get())); | |
| 151 service->UninstallExtension(ext1->id(), false, NULL); | 163 service->UninstallExtension(ext1->id(), false, NULL); |
| 152 ASSERT_EQ(4U, service->extensions()->size()); | 164 ASSERT_EQ(4U, service->extensions()->size()); |
| 153 ASSERT_EQ(2U, model->size()); | 165 ASSERT_EQ(2U, model->size()); |
| 154 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp1)); | 166 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp1, |
| 167 profile_.get())); | |
| 155 service->UninstallExtension(bgapp1->id(), false, NULL); | 168 service->UninstallExtension(bgapp1->id(), false, NULL); |
| 156 ASSERT_EQ(3U, service->extensions()->size()); | 169 ASSERT_EQ(3U, service->extensions()->size()); |
| 157 ASSERT_EQ(1U, model->size()); | 170 ASSERT_EQ(1U, model->size()); |
| 158 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext2)); | 171 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext2, |
| 172 profile_.get())); | |
| 159 service->UninstallExtension(ext2->id(), false, NULL); | 173 service->UninstallExtension(ext2->id(), false, NULL); |
| 160 ASSERT_EQ(2U, service->extensions()->size()); | 174 ASSERT_EQ(2U, service->extensions()->size()); |
| 161 ASSERT_EQ(1U, model->size()); | 175 ASSERT_EQ(1U, model->size()); |
| 162 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp2)); | 176 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp2, |
| 177 profile_.get())); | |
| 163 service->UninstallExtension(bgapp2->id(), false, NULL); | 178 service->UninstallExtension(bgapp2->id(), false, NULL); |
| 164 ASSERT_EQ(1U, service->extensions()->size()); | 179 ASSERT_EQ(1U, service->extensions()->size()); |
| 165 ASSERT_EQ(0U, model->size()); | 180 ASSERT_EQ(0U, model->size()); |
| 166 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext3)); | 181 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext3, |
| 182 profile_.get())); | |
| 167 service->UninstallExtension(ext3->id(), false, NULL); | 183 service->UninstallExtension(ext3->id(), false, NULL); |
| 168 ASSERT_EQ(0U, service->extensions()->size()); | 184 ASSERT_EQ(0U, service->extensions()->size()); |
| 169 ASSERT_EQ(0U, model->size()); | 185 ASSERT_EQ(0U, model->size()); |
| 170 } | 186 } |
| 171 | 187 |
| 172 // With minimal test logic, verifies behavior with dynamic permissions. | 188 // With minimal test logic, verifies behavior with dynamic permissions. |
| 173 TEST_F(BackgroundApplicationListModelTest, AddRemovePermissionsTest) { | 189 TEST_F(BackgroundApplicationListModelTest, AddRemovePermissionsTest) { |
| 174 InitializeAndLoadEmptyExtensionService(); | 190 InitializeAndLoadEmptyExtensionService(); |
| 175 ExtensionService* service = profile_->GetExtensionService(); | 191 ExtensionService* service = profile_->GetExtensionService(); |
| 176 ASSERT_TRUE(service); | 192 ASSERT_TRUE(service); |
| 177 ASSERT_TRUE(service->is_ready()); | 193 ASSERT_TRUE(service->is_ready()); |
| 178 ASSERT_TRUE(service->extensions()); | 194 ASSERT_TRUE(service->extensions()); |
| 179 ASSERT_TRUE(service->extensions()->is_empty()); | 195 ASSERT_TRUE(service->extensions()->is_empty()); |
| 180 scoped_ptr<BackgroundApplicationListModel> model( | 196 scoped_ptr<BackgroundApplicationListModel> model( |
| 181 new BackgroundApplicationListModel(profile_.get())); | 197 new BackgroundApplicationListModel(profile_.get())); |
| 182 ASSERT_EQ(0U, model->size()); | 198 ASSERT_EQ(0U, model->size()); |
| 183 | 199 |
| 184 scoped_refptr<Extension> ext = CreateExtension("extension", false); | 200 scoped_refptr<Extension> ext = CreateExtension("extension", false); |
| 185 scoped_refptr<Extension> bgapp = CreateExtension("application", true); | 201 scoped_refptr<Extension> bgapp = CreateExtension("application", true); |
| 186 ASSERT_TRUE(service->extensions() != NULL); | 202 ASSERT_TRUE(service->extensions() != NULL); |
| 187 ASSERT_EQ(0U, service->extensions()->size()); | 203 ASSERT_EQ(0U, service->extensions()->size()); |
| 188 ASSERT_EQ(0U, model->size()); | 204 ASSERT_EQ(0U, model->size()); |
| 189 | 205 |
| 190 // Add one (non-background) extension and one background application | 206 // Add one (non-background) extension and one background application |
| 191 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext)); | 207 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext, |
| 208 profile_.get())); | |
| 192 service->AddExtension(ext); | 209 service->AddExtension(ext); |
| 193 ASSERT_EQ(1U, service->extensions()->size()); | 210 ASSERT_EQ(1U, service->extensions()->size()); |
| 194 ASSERT_EQ(0U, model->size()); | 211 ASSERT_EQ(0U, model->size()); |
| 195 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp)); | 212 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp, |
| 213 profile_.get())); | |
| 196 service->AddExtension(bgapp); | 214 service->AddExtension(bgapp); |
| 197 ASSERT_EQ(2U, service->extensions()->size()); | 215 ASSERT_EQ(2U, service->extensions()->size()); |
| 198 ASSERT_EQ(1U, model->size()); | 216 ASSERT_EQ(1U, model->size()); |
| 199 | 217 |
| 200 // Change permissions back and forth | 218 // Change permissions back and forth |
| 201 AddBackgroundPermission(service, ext.get()); | 219 AddBackgroundPermission(service, ext.get()); |
| 202 ASSERT_EQ(2U, service->extensions()->size()); | 220 ASSERT_EQ(2U, service->extensions()->size()); |
| 203 ASSERT_EQ(2U, model->size()); | 221 ASSERT_EQ(2U, model->size()); |
| 204 RemoveBackgroundPermission(service, bgapp.get()); | 222 RemoveBackgroundPermission(service, bgapp.get()); |
| 205 ASSERT_EQ(2U, service->extensions()->size()); | 223 ASSERT_EQ(2U, service->extensions()->size()); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 220 BackgroundApplicationListModel* model, | 238 BackgroundApplicationListModel* model, |
| 221 size_t* expected, | 239 size_t* expected, |
| 222 size_t* count) { | 240 size_t* count) { |
| 223 bool create_background = false; | 241 bool create_background = false; |
| 224 if (rand() % 2) { | 242 if (rand() % 2) { |
| 225 create_background = true; | 243 create_background = true; |
| 226 ++*expected; | 244 ++*expected; |
| 227 } | 245 } |
| 228 scoped_refptr<Extension> extension = | 246 scoped_refptr<Extension> extension = |
| 229 CreateExtension(GenerateUniqueExtensionName(), create_background); | 247 CreateExtension(GenerateUniqueExtensionName(), create_background); |
| 230 ASSERT_EQ(BackgroundApplicationListModel::IsBackgroundApp(*extension), | 248 ASSERT_EQ(BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 249 service->profile()), | |
| 231 create_background); | 250 create_background); |
| 232 extensions->insert(extension); | 251 extensions->insert(extension); |
| 233 ++*count; | 252 ++*count; |
| 234 ASSERT_EQ(*count, extensions->size()); | 253 ASSERT_EQ(*count, extensions->size()); |
| 235 service->AddExtension(extension); | 254 service->AddExtension(extension); |
| 236 ASSERT_EQ(*count, service->extensions()->size()); | 255 ASSERT_EQ(*count, service->extensions()->size()); |
| 237 ASSERT_EQ(*expected, model->size()); | 256 ASSERT_EQ(*expected, model->size()); |
| 238 } | 257 } |
| 239 | 258 |
| 240 void RemoveExtension(ExtensionService* service, | 259 void RemoveExtension(ExtensionService* service, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 251 ASSERT_EQ(0U, model->size()); | 270 ASSERT_EQ(0U, model->size()); |
| 252 } else { | 271 } else { |
| 253 // Randomly select which extension to remove | 272 // Randomly select which extension to remove |
| 254 if (extensions->size() > 1) { | 273 if (extensions->size() > 1) { |
| 255 int offset = rand() % (extensions->size() - 1); | 274 int offset = rand() % (extensions->size() - 1); |
| 256 for (int index = 0; index < offset; ++index) | 275 for (int index = 0; index < offset; ++index) |
| 257 ++cursor; | 276 ++cursor; |
| 258 } | 277 } |
| 259 scoped_refptr<Extension> extension = cursor->get(); | 278 scoped_refptr<Extension> extension = cursor->get(); |
| 260 std::string id = extension->id(); | 279 std::string id = extension->id(); |
| 261 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) | 280 if (BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 281 service->profile())) { | |
| 262 --*expected; | 282 --*expected; |
| 283 } | |
| 263 extensions->erase(cursor); | 284 extensions->erase(cursor); |
| 264 --*count; | 285 --*count; |
| 265 ASSERT_EQ(*count, extensions->size()); | 286 ASSERT_EQ(*count, extensions->size()); |
| 266 service->UninstallExtension(extension->id(), false, NULL); | 287 service->UninstallExtension(extension->id(), false, NULL); |
| 267 ASSERT_EQ(*count, service->extensions()->size()); | 288 ASSERT_EQ(*count, service->extensions()->size()); |
| 268 ASSERT_EQ(*expected, model->size()); | 289 ASSERT_EQ(*expected, model->size()); |
| 269 } | 290 } |
| 270 } | 291 } |
| 271 | 292 |
| 272 void TogglePermission(ExtensionService* service, | 293 void TogglePermission(ExtensionService* service, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 283 ASSERT_EQ(0U, model->size()); | 304 ASSERT_EQ(0U, model->size()); |
| 284 } else { | 305 } else { |
| 285 // Randomly select which extension to toggle. | 306 // Randomly select which extension to toggle. |
| 286 if (extensions->size() > 1) { | 307 if (extensions->size() > 1) { |
| 287 int offset = rand() % (extensions->size() - 1); | 308 int offset = rand() % (extensions->size() - 1); |
| 288 for (int index = 0; index < offset; ++index) | 309 for (int index = 0; index < offset; ++index) |
| 289 ++cursor; | 310 ++cursor; |
| 290 } | 311 } |
| 291 scoped_refptr<Extension> extension = cursor->get(); | 312 scoped_refptr<Extension> extension = cursor->get(); |
| 292 std::string id = extension->id(); | 313 std::string id = extension->id(); |
| 293 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) { | 314 if (BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 315 service->profile())) { | |
| 294 --*expected; | 316 --*expected; |
| 295 ASSERT_EQ(*count, extensions->size()); | 317 ASSERT_EQ(*count, extensions->size()); |
| 296 RemoveBackgroundPermission(service, extension); | 318 RemoveBackgroundPermission(service, extension); |
| 297 ASSERT_EQ(*count, service->extensions()->size()); | 319 ASSERT_EQ(*count, service->extensions()->size()); |
| 298 ASSERT_EQ(*expected, model->size()); | 320 ASSERT_EQ(*expected, model->size()); |
| 299 } else { | 321 } else { |
| 300 ++*expected; | 322 ++*expected; |
| 301 ASSERT_EQ(*count, extensions->size()); | 323 ASSERT_EQ(*count, extensions->size()); |
| 302 AddBackgroundPermission(service, extension); | 324 AddBackgroundPermission(service, extension); |
| 303 ASSERT_EQ(*count, service->extensions()->size()); | 325 ASSERT_EQ(*count, service->extensions()->size()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 335 break; | 357 break; |
| 336 case 2: | 358 case 2: |
| 337 TogglePermission(service, &extensions, model.get(), &expected, &count); | 359 TogglePermission(service, &extensions, model.get(), &expected, &count); |
| 338 break; | 360 break; |
| 339 default: | 361 default: |
| 340 NOTREACHED(); | 362 NOTREACHED(); |
| 341 break; | 363 break; |
| 342 } | 364 } |
| 343 } | 365 } |
| 344 } | 366 } |
| OLD | NEW |