| 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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 scoped_refptr<Extension> ext1 = CreateExtension("alpha", false); | 141 scoped_refptr<Extension> ext1 = CreateExtension("alpha", false); |
| 142 scoped_refptr<Extension> ext2 = CreateExtension("bravo", false); | 142 scoped_refptr<Extension> ext2 = CreateExtension("bravo", false); |
| 143 scoped_refptr<Extension> ext3 = CreateExtension("charlie", false); | 143 scoped_refptr<Extension> ext3 = CreateExtension("charlie", false); |
| 144 scoped_refptr<Extension> bgapp1 = CreateExtension("delta", true); | 144 scoped_refptr<Extension> bgapp1 = CreateExtension("delta", true); |
| 145 scoped_refptr<Extension> bgapp2 = CreateExtension("echo", true); | 145 scoped_refptr<Extension> bgapp2 = CreateExtension("echo", true); |
| 146 ASSERT_TRUE(service->extensions() != NULL); | 146 ASSERT_TRUE(service->extensions() != NULL); |
| 147 ASSERT_EQ(0U, service->extensions()->size()); | 147 ASSERT_EQ(0U, service->extensions()->size()); |
| 148 ASSERT_EQ(0U, model->size()); | 148 ASSERT_EQ(0U, model->size()); |
| 149 | 149 |
| 150 // Add alternating Extensions and Background Apps | 150 // Add alternating Extensions and Background Apps |
| 151 ASSERT_FALSE(IsBackgroundApp(*ext1)); | 151 ASSERT_FALSE(IsBackgroundApp(*ext1.get())); |
| 152 service->AddExtension(ext1); | 152 service->AddExtension(ext1.get()); |
| 153 ASSERT_EQ(1U, service->extensions()->size()); | 153 ASSERT_EQ(1U, service->extensions()->size()); |
| 154 ASSERT_EQ(0U, model->size()); | 154 ASSERT_EQ(0U, model->size()); |
| 155 ASSERT_TRUE(IsBackgroundApp(*bgapp1)); | 155 ASSERT_TRUE(IsBackgroundApp(*bgapp1.get())); |
| 156 service->AddExtension(bgapp1); | 156 service->AddExtension(bgapp1.get()); |
| 157 ASSERT_EQ(2U, service->extensions()->size()); | 157 ASSERT_EQ(2U, service->extensions()->size()); |
| 158 ASSERT_EQ(1U, model->size()); | 158 ASSERT_EQ(1U, model->size()); |
| 159 ASSERT_FALSE(IsBackgroundApp(*ext2)); | 159 ASSERT_FALSE(IsBackgroundApp(*ext2.get())); |
| 160 service->AddExtension(ext2); | 160 service->AddExtension(ext2.get()); |
| 161 ASSERT_EQ(3U, service->extensions()->size()); | 161 ASSERT_EQ(3U, service->extensions()->size()); |
| 162 ASSERT_EQ(1U, model->size()); | 162 ASSERT_EQ(1U, model->size()); |
| 163 ASSERT_TRUE(IsBackgroundApp(*bgapp2)); | 163 ASSERT_TRUE(IsBackgroundApp(*bgapp2.get())); |
| 164 service->AddExtension(bgapp2); | 164 service->AddExtension(bgapp2.get()); |
| 165 ASSERT_EQ(4U, service->extensions()->size()); | 165 ASSERT_EQ(4U, service->extensions()->size()); |
| 166 ASSERT_EQ(2U, model->size()); | 166 ASSERT_EQ(2U, model->size()); |
| 167 ASSERT_FALSE(IsBackgroundApp(*ext3)); | 167 ASSERT_FALSE(IsBackgroundApp(*ext3.get())); |
| 168 service->AddExtension(ext3); | 168 service->AddExtension(ext3.get()); |
| 169 ASSERT_EQ(5U, service->extensions()->size()); | 169 ASSERT_EQ(5U, service->extensions()->size()); |
| 170 ASSERT_EQ(2U, model->size()); | 170 ASSERT_EQ(2U, model->size()); |
| 171 | 171 |
| 172 // Remove in FIFO order. | 172 // Remove in FIFO order. |
| 173 ASSERT_FALSE(IsBackgroundApp(*ext1)); | 173 ASSERT_FALSE(IsBackgroundApp(*ext1.get())); |
| 174 service->UninstallExtension(ext1->id(), false, NULL); | 174 service->UninstallExtension(ext1->id(), false, NULL); |
| 175 ASSERT_EQ(4U, service->extensions()->size()); | 175 ASSERT_EQ(4U, service->extensions()->size()); |
| 176 ASSERT_EQ(2U, model->size()); | 176 ASSERT_EQ(2U, model->size()); |
| 177 ASSERT_TRUE(IsBackgroundApp(*bgapp1)); | 177 ASSERT_TRUE(IsBackgroundApp(*bgapp1.get())); |
| 178 service->UninstallExtension(bgapp1->id(), false, NULL); | 178 service->UninstallExtension(bgapp1->id(), false, NULL); |
| 179 ASSERT_EQ(3U, service->extensions()->size()); | 179 ASSERT_EQ(3U, service->extensions()->size()); |
| 180 ASSERT_EQ(1U, model->size()); | 180 ASSERT_EQ(1U, model->size()); |
| 181 ASSERT_FALSE(IsBackgroundApp(*ext2)); | 181 ASSERT_FALSE(IsBackgroundApp(*ext2.get())); |
| 182 service->UninstallExtension(ext2->id(), false, NULL); | 182 service->UninstallExtension(ext2->id(), false, NULL); |
| 183 ASSERT_EQ(2U, service->extensions()->size()); | 183 ASSERT_EQ(2U, service->extensions()->size()); |
| 184 ASSERT_EQ(1U, model->size()); | 184 ASSERT_EQ(1U, model->size()); |
| 185 ASSERT_TRUE(IsBackgroundApp(*bgapp2)); | 185 ASSERT_TRUE(IsBackgroundApp(*bgapp2.get())); |
| 186 service->UninstallExtension(bgapp2->id(), false, NULL); | 186 service->UninstallExtension(bgapp2->id(), false, NULL); |
| 187 ASSERT_EQ(1U, service->extensions()->size()); | 187 ASSERT_EQ(1U, service->extensions()->size()); |
| 188 ASSERT_EQ(0U, model->size()); | 188 ASSERT_EQ(0U, model->size()); |
| 189 ASSERT_FALSE(IsBackgroundApp(*ext3)); | 189 ASSERT_FALSE(IsBackgroundApp(*ext3.get())); |
| 190 service->UninstallExtension(ext3->id(), false, NULL); | 190 service->UninstallExtension(ext3->id(), false, NULL); |
| 191 ASSERT_EQ(0U, service->extensions()->size()); | 191 ASSERT_EQ(0U, service->extensions()->size()); |
| 192 ASSERT_EQ(0U, model->size()); | 192 ASSERT_EQ(0U, model->size()); |
| 193 } | 193 } |
| 194 | 194 |
| 195 // With minimal test logic, verifies behavior with dynamic permissions. | 195 // With minimal test logic, verifies behavior with dynamic permissions. |
| 196 TEST_F(BackgroundApplicationListModelTest, AddRemovePermissionsTest) { | 196 TEST_F(BackgroundApplicationListModelTest, AddRemovePermissionsTest) { |
| 197 InitializeAndLoadEmptyExtensionService(); | 197 InitializeAndLoadEmptyExtensionService(); |
| 198 ExtensionService* service = extensions::ExtensionSystem::Get(profile_.get())-> | 198 ExtensionService* service = extensions::ExtensionSystem::Get(profile_.get())-> |
| 199 extension_service(); | 199 extension_service(); |
| 200 ASSERT_TRUE(service); | 200 ASSERT_TRUE(service); |
| 201 ASSERT_TRUE(service->is_ready()); | 201 ASSERT_TRUE(service->is_ready()); |
| 202 ASSERT_TRUE(service->extensions()); | 202 ASSERT_TRUE(service->extensions()); |
| 203 ASSERT_TRUE(service->extensions()->is_empty()); | 203 ASSERT_TRUE(service->extensions()->is_empty()); |
| 204 scoped_ptr<BackgroundApplicationListModel> model( | 204 scoped_ptr<BackgroundApplicationListModel> model( |
| 205 new BackgroundApplicationListModel(profile_.get())); | 205 new BackgroundApplicationListModel(profile_.get())); |
| 206 ASSERT_EQ(0U, model->size()); | 206 ASSERT_EQ(0U, model->size()); |
| 207 | 207 |
| 208 scoped_refptr<Extension> ext = CreateExtension("extension", false); | 208 scoped_refptr<Extension> ext = CreateExtension("extension", false); |
| 209 ASSERT_FALSE(ext->HasAPIPermission(APIPermission::kBackground)); | 209 ASSERT_FALSE(ext->HasAPIPermission(APIPermission::kBackground)); |
| 210 scoped_refptr<Extension> bgapp = CreateExtension("application", true); | 210 scoped_refptr<Extension> bgapp = CreateExtension("application", true); |
| 211 ASSERT_TRUE(bgapp->HasAPIPermission(APIPermission::kBackground)); | 211 ASSERT_TRUE(bgapp->HasAPIPermission(APIPermission::kBackground)); |
| 212 ASSERT_TRUE(service->extensions() != NULL); | 212 ASSERT_TRUE(service->extensions() != NULL); |
| 213 ASSERT_EQ(0U, service->extensions()->size()); | 213 ASSERT_EQ(0U, service->extensions()->size()); |
| 214 ASSERT_EQ(0U, model->size()); | 214 ASSERT_EQ(0U, model->size()); |
| 215 | 215 |
| 216 // Add one (non-background) extension and one background application | 216 // Add one (non-background) extension and one background application |
| 217 ASSERT_FALSE(IsBackgroundApp(*ext)); | 217 ASSERT_FALSE(IsBackgroundApp(*ext.get())); |
| 218 service->AddExtension(ext); | 218 service->AddExtension(ext.get()); |
| 219 ASSERT_EQ(1U, service->extensions()->size()); | 219 ASSERT_EQ(1U, service->extensions()->size()); |
| 220 ASSERT_EQ(0U, model->size()); | 220 ASSERT_EQ(0U, model->size()); |
| 221 ASSERT_TRUE(IsBackgroundApp(*bgapp)); | 221 ASSERT_TRUE(IsBackgroundApp(*bgapp.get())); |
| 222 service->AddExtension(bgapp); | 222 service->AddExtension(bgapp.get()); |
| 223 ASSERT_EQ(2U, service->extensions()->size()); | 223 ASSERT_EQ(2U, service->extensions()->size()); |
| 224 ASSERT_EQ(1U, model->size()); | 224 ASSERT_EQ(1U, model->size()); |
| 225 | 225 |
| 226 // Change permissions back and forth | 226 // Change permissions back and forth |
| 227 AddBackgroundPermission(service, ext.get()); | 227 AddBackgroundPermission(service, ext.get()); |
| 228 ASSERT_TRUE(ext->HasAPIPermission(APIPermission::kBackground)); | 228 ASSERT_TRUE(ext->HasAPIPermission(APIPermission::kBackground)); |
| 229 ASSERT_EQ(2U, service->extensions()->size()); | 229 ASSERT_EQ(2U, service->extensions()->size()); |
| 230 ASSERT_EQ(2U, model->size()); | 230 ASSERT_EQ(2U, model->size()); |
| 231 RemoveBackgroundPermission(service, bgapp.get()); | 231 RemoveBackgroundPermission(service, bgapp.get()); |
| 232 ASSERT_FALSE(bgapp->HasAPIPermission(APIPermission::kBackground)); | 232 ASSERT_FALSE(bgapp->HasAPIPermission(APIPermission::kBackground)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 250 BackgroundApplicationListModel* model, | 250 BackgroundApplicationListModel* model, |
| 251 size_t* expected, | 251 size_t* expected, |
| 252 size_t* count) { | 252 size_t* count) { |
| 253 bool create_background = false; | 253 bool create_background = false; |
| 254 if (rand() % 2) { | 254 if (rand() % 2) { |
| 255 create_background = true; | 255 create_background = true; |
| 256 ++*expected; | 256 ++*expected; |
| 257 } | 257 } |
| 258 scoped_refptr<Extension> extension = | 258 scoped_refptr<Extension> extension = |
| 259 CreateExtension(GenerateUniqueExtensionName(), create_background); | 259 CreateExtension(GenerateUniqueExtensionName(), create_background); |
| 260 ASSERT_EQ(BackgroundApplicationListModel::IsBackgroundApp(*extension, | 260 ASSERT_EQ(BackgroundApplicationListModel::IsBackgroundApp(*extension.get(), |
| 261 service->profile()), | 261 service->profile()), |
| 262 create_background); | 262 create_background); |
| 263 extensions->insert(extension); | 263 extensions->insert(extension); |
| 264 ++*count; | 264 ++*count; |
| 265 ASSERT_EQ(*count, extensions->size()); | 265 ASSERT_EQ(*count, extensions->size()); |
| 266 service->AddExtension(extension); | 266 service->AddExtension(extension.get()); |
| 267 ASSERT_EQ(*count, service->extensions()->size()); | 267 ASSERT_EQ(*count, service->extensions()->size()); |
| 268 ASSERT_EQ(*expected, model->size()); | 268 ASSERT_EQ(*expected, model->size()); |
| 269 } | 269 } |
| 270 | 270 |
| 271 void RemoveExtension(ExtensionService* service, | 271 void RemoveExtension(ExtensionService* service, |
| 272 ExtensionCollection* extensions, | 272 ExtensionCollection* extensions, |
| 273 BackgroundApplicationListModel* model, | 273 BackgroundApplicationListModel* model, |
| 274 size_t* expected, | 274 size_t* expected, |
| 275 size_t* count) { // Maybe remove an extension. | 275 size_t* count) { // Maybe remove an extension. |
| 276 ExtensionCollection::iterator cursor = extensions->begin(); | 276 ExtensionCollection::iterator cursor = extensions->begin(); |
| 277 if (cursor == extensions->end()) { | 277 if (cursor == extensions->end()) { |
| 278 // Nothing to remove. Just verify accounting. | 278 // Nothing to remove. Just verify accounting. |
| 279 ASSERT_EQ(0U, *count); | 279 ASSERT_EQ(0U, *count); |
| 280 ASSERT_EQ(0U, *expected); | 280 ASSERT_EQ(0U, *expected); |
| 281 ASSERT_EQ(0U, service->extensions()->size()); | 281 ASSERT_EQ(0U, service->extensions()->size()); |
| 282 ASSERT_EQ(0U, model->size()); | 282 ASSERT_EQ(0U, model->size()); |
| 283 } else { | 283 } else { |
| 284 // Randomly select which extension to remove | 284 // Randomly select which extension to remove |
| 285 if (extensions->size() > 1) { | 285 if (extensions->size() > 1) { |
| 286 int offset = rand() % (extensions->size() - 1); | 286 int offset = rand() % (extensions->size() - 1); |
| 287 for (int index = 0; index < offset; ++index) | 287 for (int index = 0; index < offset; ++index) |
| 288 ++cursor; | 288 ++cursor; |
| 289 } | 289 } |
| 290 scoped_refptr<Extension> extension = cursor->get(); | 290 scoped_refptr<Extension> extension = cursor->get(); |
| 291 std::string id = extension->id(); | 291 std::string id = extension->id(); |
| 292 if (BackgroundApplicationListModel::IsBackgroundApp(*extension, | 292 if (BackgroundApplicationListModel::IsBackgroundApp(*extension.get(), |
| 293 service->profile())) { | 293 service->profile())) { |
| 294 --*expected; | 294 --*expected; |
| 295 } | 295 } |
| 296 extensions->erase(cursor); | 296 extensions->erase(cursor); |
| 297 --*count; | 297 --*count; |
| 298 ASSERT_EQ(*count, extensions->size()); | 298 ASSERT_EQ(*count, extensions->size()); |
| 299 service->UninstallExtension(extension->id(), false, NULL); | 299 service->UninstallExtension(extension->id(), false, NULL); |
| 300 ASSERT_EQ(*count, service->extensions()->size()); | 300 ASSERT_EQ(*count, service->extensions()->size()); |
| 301 ASSERT_EQ(*expected, model->size()); | 301 ASSERT_EQ(*expected, model->size()); |
| 302 } | 302 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 316 ASSERT_EQ(0U, model->size()); | 316 ASSERT_EQ(0U, model->size()); |
| 317 } else { | 317 } else { |
| 318 // Randomly select which extension to toggle. | 318 // Randomly select which extension to toggle. |
| 319 if (extensions->size() > 1) { | 319 if (extensions->size() > 1) { |
| 320 int offset = rand() % (extensions->size() - 1); | 320 int offset = rand() % (extensions->size() - 1); |
| 321 for (int index = 0; index < offset; ++index) | 321 for (int index = 0; index < offset; ++index) |
| 322 ++cursor; | 322 ++cursor; |
| 323 } | 323 } |
| 324 scoped_refptr<Extension> extension = cursor->get(); | 324 scoped_refptr<Extension> extension = cursor->get(); |
| 325 std::string id = extension->id(); | 325 std::string id = extension->id(); |
| 326 if (BackgroundApplicationListModel::IsBackgroundApp(*extension, | 326 if (BackgroundApplicationListModel::IsBackgroundApp(*extension.get(), |
| 327 service->profile())) { | 327 service->profile())) { |
| 328 --*expected; | 328 --*expected; |
| 329 ASSERT_EQ(*count, extensions->size()); | 329 ASSERT_EQ(*count, extensions->size()); |
| 330 RemoveBackgroundPermission(service, extension); | 330 RemoveBackgroundPermission(service, extension.get()); |
| 331 ASSERT_EQ(*count, service->extensions()->size()); | 331 ASSERT_EQ(*count, service->extensions()->size()); |
| 332 ASSERT_EQ(*expected, model->size()); | 332 ASSERT_EQ(*expected, model->size()); |
| 333 } else { | 333 } else { |
| 334 ++*expected; | 334 ++*expected; |
| 335 ASSERT_EQ(*count, extensions->size()); | 335 ASSERT_EQ(*count, extensions->size()); |
| 336 AddBackgroundPermission(service, extension); | 336 AddBackgroundPermission(service, extension.get()); |
| 337 ASSERT_EQ(*count, service->extensions()->size()); | 337 ASSERT_EQ(*count, service->extensions()->size()); |
| 338 ASSERT_EQ(*expected, model->size()); | 338 ASSERT_EQ(*expected, model->size()); |
| 339 } | 339 } |
| 340 } | 340 } |
| 341 } | 341 } |
| 342 } // namespace | 342 } // namespace |
| 343 | 343 |
| 344 // Verifies behavior with a pseudo-randomly generated set of actions: Adding and | 344 // Verifies behavior with a pseudo-randomly generated set of actions: Adding and |
| 345 // removing extensions, of which some are Background Apps and others are not. | 345 // removing extensions, of which some are Background Apps and others are not. |
| 346 TEST_F(BackgroundApplicationListModelTest, RandomTest) { | 346 TEST_F(BackgroundApplicationListModelTest, RandomTest) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 370 break; | 370 break; |
| 371 case 2: | 371 case 2: |
| 372 TogglePermission(service, &extensions, model.get(), &expected, &count); | 372 TogglePermission(service, &extensions, model.get(), &expected, &count); |
| 373 break; | 373 break; |
| 374 default: | 374 default: |
| 375 NOTREACHED(); | 375 NOTREACHED(); |
| 376 break; | 376 break; |
| 377 } | 377 } |
| 378 } | 378 } |
| 379 } | 379 } |
| OLD | NEW |