Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 | |
| 5 // TODO(rickcam): Bug 73183: Add unit tests for image loading | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cstdlib> | |
| 9 #include <set> | |
|
Andrew T Wilson (Slow)
2011/02/17 05:56:47
Remove duplicate #include <set> line.
The wrong rickcam account
2011/02/17 18:24:01
Done. A couple of others there were leftovers, so
| |
| 10 #include <set> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "chrome/browser/background_application_list_model.h" | |
| 14 | |
| 15 #include "base/command_line.h" | |
| 16 #include "base/file_path.h" | |
| 17 #include "base/file_util.h" | |
| 18 #include "base/message_loop.h" | |
| 19 #include "base/scoped_ptr.h" | |
| 20 #include "base/stl_util-inl.h" | |
| 21 #include "chrome/browser/browser_thread.h" | |
| 22 #include "chrome/browser/extensions/extension_service.h" | |
| 23 #include "chrome/common/extensions/extension.h" | |
| 24 #include "chrome/common/notification_registrar.h" | |
| 25 #include "chrome/common/notification_service.h" | |
| 26 #include "chrome/common/notification_type.h" | |
| 27 #include "chrome/test/testing_profile.h" | |
| 28 #include "testing/gtest/include/gtest/gtest.h" | |
| 29 | |
| 30 // This value is used to seed the PRNG at the beginning of a sequence of | |
| 31 // operations to produce a repeatable sequence. | |
| 32 #define RANDOM_SEED (0x33F7A7A7) | |
| 33 | |
| 34 // For ExtensionService interface when it requires a path that is not used. | |
| 35 FilePath bogus_file_path() { | |
| 36 return FilePath(FILE_PATH_LITERAL("//foobar_nonexistent")); | |
| 37 } | |
| 38 | |
| 39 class BackgroundApplicationListModelTest : public testing::Test { | |
| 40 public: | |
| 41 BackgroundApplicationListModelTest(); | |
| 42 ~BackgroundApplicationListModelTest(); | |
| 43 | |
| 44 virtual void InitializeEmptyExtensionService(); | |
| 45 | |
| 46 protected: | |
| 47 scoped_ptr<Profile> profile_; | |
| 48 scoped_refptr<ExtensionService> service_; | |
| 49 MessageLoop loop_; | |
| 50 BrowserThread ui_thread_; | |
| 51 }; | |
| 52 | |
| 53 // The message loop may be used in tests which require it to be an IO loop. | |
| 54 BackgroundApplicationListModelTest::BackgroundApplicationListModelTest() | |
| 55 : loop_(MessageLoop::TYPE_IO), | |
| 56 ui_thread_(BrowserThread::UI, &loop_) { | |
| 57 } | |
| 58 | |
| 59 BackgroundApplicationListModelTest::~BackgroundApplicationListModelTest() { | |
| 60 // Drop reference to ExtensionService and TestingProfile, so that they can be | |
| 61 // destroyed while BrowserThreads and MessageLoop are still around. They | |
| 62 // are used in the destruction process. | |
| 63 service_ = NULL; | |
| 64 profile_.reset(NULL); | |
| 65 MessageLoop::current()->RunAllPending(); | |
| 66 } | |
| 67 | |
| 68 // This is modeled on a similar routine in ExtensionServiceTestBase. | |
| 69 void BackgroundApplicationListModelTest::InitializeEmptyExtensionService() { | |
| 70 TestingProfile* profile = new TestingProfile(); | |
| 71 profile_.reset(profile); | |
| 72 service_ = profile->CreateExtensionService( | |
| 73 CommandLine::ForCurrentProcess(), | |
| 74 bogus_file_path()); | |
| 75 service_->set_extensions_enabled(true); | |
| 76 service_->set_show_extensions_prompts(false); | |
| 77 service_->OnLoadedInstalledExtensions(); /* Sends EXTENSIONS_READY */ | |
| 78 } | |
| 79 | |
| 80 // Returns a barebones test Extension object with the specified |name|. The | |
| 81 // returned extension will include background permission iff | |
| 82 // |background_permission| is true. | |
| 83 static scoped_refptr<Extension> CreateExtension(const std::string& name, | |
| 84 bool background_permission) { | |
| 85 DictionaryValue manifest; | |
| 86 manifest.SetString(extension_manifest_keys::kVersion, "1.0.0.0"); | |
| 87 manifest.SetString(extension_manifest_keys::kName, name); | |
| 88 if (background_permission) { | |
| 89 ListValue* permissions = new ListValue(); | |
| 90 manifest.Set(extension_manifest_keys::kPermissions, permissions); | |
| 91 permissions->Append(Value::CreateStringValue("background")); | |
| 92 } | |
| 93 std::string error; | |
| 94 scoped_refptr<Extension> extension = Extension::Create( | |
| 95 bogus_file_path().AppendASCII(name), Extension::INVALID, manifest, false, | |
| 96 &error); | |
| 97 // Cannot ASSERT_* here because that attempts an illegitimate return. | |
| 98 // Cannot EXPECT_NE here because that assumes non-pointers unlike EXPECT_EQ | |
| 99 EXPECT_TRUE(extension.get() != NULL) << error; | |
| 100 return extension; | |
| 101 } | |
| 102 | |
| 103 // With minimal test logic, verifies behavior over an explicit set of | |
| 104 // extensions, of which some are Background Apps and others are not. | |
| 105 TEST_F(BackgroundApplicationListModelTest, LoadExplicitExtensions) { | |
| 106 InitializeEmptyExtensionService(); | |
| 107 ExtensionService* service = profile_->GetExtensionService(); | |
| 108 ASSERT_TRUE(service); | |
| 109 ASSERT_TRUE(service->is_ready()); | |
| 110 ASSERT_TRUE(service->extensions()); | |
| 111 ASSERT_TRUE(service->extensions()->empty()); | |
| 112 scoped_ptr<BackgroundApplicationListModel> model( | |
| 113 new BackgroundApplicationListModel(profile_.get())); | |
| 114 ASSERT_EQ(0U, model->size()); | |
| 115 | |
| 116 scoped_refptr<Extension> ext1 = CreateExtension("alpha", false); | |
| 117 scoped_refptr<Extension> ext2 = CreateExtension("bravo", false); | |
| 118 scoped_refptr<Extension> ext3 = CreateExtension("charlie", false); | |
| 119 scoped_refptr<Extension> bgapp1 = CreateExtension("delta", true); | |
| 120 scoped_refptr<Extension> bgapp2 = CreateExtension("echo", true); | |
| 121 ASSERT_TRUE(service->extensions() != NULL); | |
| 122 ASSERT_EQ(0U, service->extensions()->size()); | |
| 123 ASSERT_EQ(0U, model->size()); | |
| 124 // Add alternating Extensions and Background Apps | |
| 125 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext1)); | |
| 126 service->AddExtension(ext1); | |
| 127 ASSERT_EQ(1U, service->extensions()->size()); | |
| 128 ASSERT_EQ(0U, model->size()); | |
| 129 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp1)); | |
| 130 service->AddExtension(bgapp1); | |
| 131 ASSERT_EQ(2U, service->extensions()->size()); | |
| 132 ASSERT_EQ(1U, model->size()); | |
| 133 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext2)); | |
| 134 service->AddExtension(ext2); | |
| 135 ASSERT_EQ(3U, service->extensions()->size()); | |
| 136 ASSERT_EQ(1U, model->size()); | |
| 137 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp2)); | |
| 138 service->AddExtension(bgapp2); | |
| 139 ASSERT_EQ(4U, service->extensions()->size()); | |
| 140 ASSERT_EQ(2U, model->size()); | |
| 141 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext3)); | |
| 142 service->AddExtension(ext3); | |
| 143 ASSERT_EQ(5U, service->extensions()->size()); | |
| 144 ASSERT_EQ(2U, model->size()); | |
| 145 // Remove in FIFO order. | |
| 146 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext1)); | |
| 147 service->UninstallExtension(ext1->id(), false); | |
| 148 ASSERT_EQ(4U, service->extensions()->size()); | |
| 149 ASSERT_EQ(2U, model->size()); | |
| 150 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp1)); | |
| 151 service->UninstallExtension(bgapp1->id(), false); | |
| 152 ASSERT_EQ(3U, service->extensions()->size()); | |
| 153 ASSERT_EQ(1U, model->size()); | |
| 154 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext2)); | |
| 155 service->UninstallExtension(ext2->id(), false); | |
| 156 ASSERT_EQ(2U, service->extensions()->size()); | |
| 157 ASSERT_EQ(1U, model->size()); | |
| 158 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp2)); | |
| 159 service->UninstallExtension(bgapp2->id(), false); | |
| 160 ASSERT_EQ(1U, service->extensions()->size()); | |
| 161 ASSERT_EQ(0U, model->size()); | |
| 162 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext3)); | |
| 163 service->UninstallExtension(ext3->id(), false); | |
| 164 ASSERT_EQ(0U, service->extensions()->size()); | |
| 165 ASSERT_EQ(0U, model->size()); | |
| 166 } | |
| 167 | |
| 168 typedef std::set<scoped_refptr<Extension> > ExtensionSet; | |
| 169 | |
| 170 namespace { | |
| 171 std::string | |
| 172 GenerateUniqueExtensionName() { | |
|
Andrew T Wilson (Slow)
2011/02/17 05:56:47
Looks like the return value is still on a separate
The wrong rickcam account
2011/02/17 18:24:01
Done. For real this time.
| |
| 173 static unsigned int uniqueness = 0; | |
| 174 std::ostringstream output; | |
| 175 output << "Unique Named Extension " << uniqueness; | |
| 176 ++uniqueness; | |
| 177 return output.str(); | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 // Verifies behavior with a pseudo-randomly generated set of actions: Adding and | |
| 182 // removing extensions, of which some are Background Apps and others are not. | |
| 183 TEST_F(BackgroundApplicationListModelTest, LoadRandomExtension) { | |
| 184 InitializeEmptyExtensionService(); | |
| 185 ExtensionService* service = profile_->GetExtensionService(); | |
| 186 ASSERT_TRUE(service); | |
| 187 ASSERT_TRUE(service->is_ready()); | |
| 188 ASSERT_TRUE(service->extensions()); | |
| 189 ASSERT_TRUE(service->extensions()->empty()); | |
| 190 scoped_ptr<BackgroundApplicationListModel> model( | |
| 191 new BackgroundApplicationListModel(profile_.get())); | |
| 192 ASSERT_EQ(0U, model->size()); | |
| 193 | |
| 194 static const unsigned int kIterations = 500; | |
|
The wrong rickcam account
2011/02/17 18:24:01
Making this int as well.
| |
| 195 ExtensionSet extensions; | |
| 196 unsigned int count = 0; | |
|
Andrew T Wilson (Slow)
2011/02/17 05:56:47
This should either be int or size_t, whichever is
The wrong rickcam account
2011/02/17 18:24:01
Done.
| |
| 197 unsigned int expected = 0; | |
| 198 srandom(RANDOM_SEED); | |
| 199 for (unsigned int index = 0; index < kIterations; ++index) { | |
|
Andrew T Wilson (Slow)
2011/02/17 05:56:47
Use int here, not "unsigned int":
http://google-s
The wrong rickcam account
2011/02/17 18:24:01
Done.
| |
| 200 if (random() % 2) { // Add an extension | |
| 201 std::string name = GenerateUniqueExtensionName(); | |
| 202 bool create_background = false; | |
| 203 if (random() % 2) { | |
| 204 create_background = true; | |
| 205 ++expected; | |
| 206 } | |
| 207 scoped_refptr<Extension> extension = | |
| 208 CreateExtension(name, create_background); | |
| 209 ASSERT_EQ(BackgroundApplicationListModel::IsBackgroundApp(*extension), | |
| 210 create_background); | |
| 211 extensions.insert(extension); | |
| 212 ++count; | |
| 213 ASSERT_EQ(count, extensions.size()); | |
| 214 service->AddExtension(extension); | |
| 215 ASSERT_EQ(count, service->extensions()->size()); | |
| 216 ASSERT_EQ(expected, model->size()); | |
| 217 } else { // Maybe remove an extension. | |
|
The wrong rickcam account
2011/02/17 18:24:01
I decided to make that minor logic tweak that I me
| |
| 218 ExtensionSet::iterator cursor = extensions.begin(); | |
| 219 if (cursor == extensions.end()) { | |
| 220 // Nothing to remove. Just verify accounting. | |
| 221 ASSERT_EQ(0U, count); | |
| 222 ASSERT_EQ(0U, expected); | |
| 223 ASSERT_EQ(0U, service->extensions()->size()); | |
| 224 ASSERT_EQ(0U, model->size()); | |
| 225 } else { | |
| 226 // Randomly select which extension to remove | |
| 227 if (extensions.size() > 1) { | |
| 228 unsigned int offset = random() % (extensions.size() - 1); | |
| 229 for (unsigned int index = 0; index < offset; ++index) | |
|
The wrong rickcam account
2011/02/17 18:24:01
I made this int as well.
| |
| 230 ++cursor; | |
| 231 } | |
| 232 scoped_refptr<Extension> extension = cursor->get(); | |
| 233 std::string id = extension->id(); | |
| 234 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) | |
| 235 --expected; | |
| 236 extensions.erase(cursor); | |
| 237 --count; | |
| 238 ASSERT_EQ(count, extensions.size()); | |
| 239 service->UninstallExtension(extension->id(), false); | |
| 240 ASSERT_EQ(count, service->extensions()->size()); | |
| 241 ASSERT_EQ(expected, model->size()); | |
| 242 } | |
| 243 } | |
| 244 } | |
| 245 } | |
| OLD | NEW |