summaryrefslogtreecommitdiffstats
path: root/chrome/browser/background_application_list_model_unittest.cc
blob: f38c8ee63f664640be443b3f6850a2f951b79651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// TODO(rickcam): Bug 73183: Add unit tests for image loading

#include <cstdlib>
#include <set>

#include "chrome/browser/background_application_list_model.h"

#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/scoped_ptr.h"
#include "base/stl_util-inl.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/test/testing_profile.h"
#include "content/browser/browser_thread.h"
#include "content/common/notification_registrar.h"
#include "content/common/notification_service.h"
#include "content/common/notification_type.h"
#include "testing/gtest/include/gtest/gtest.h"

// This value is used to seed the PRNG at the beginning of a sequence of
// operations to produce a repeatable sequence.
#define RANDOM_SEED (0x33F7A7A7)

// For ExtensionService interface when it requires a path that is not used.
FilePath bogus_file_path() {
  return FilePath(FILE_PATH_LITERAL("//foobar_nonexistent"));
}

class BackgroundApplicationListModelTest : public testing::Test {
 public:
  BackgroundApplicationListModelTest();
  ~BackgroundApplicationListModelTest();

  virtual void InitializeEmptyExtensionService();

 protected:
  scoped_ptr<Profile> profile_;
  scoped_refptr<ExtensionService> service_;
  MessageLoop loop_;
  BrowserThread ui_thread_;
};

// The message loop may be used in tests which require it to be an IO loop.
BackgroundApplicationListModelTest::BackgroundApplicationListModelTest()
    : loop_(MessageLoop::TYPE_IO),
      ui_thread_(BrowserThread::UI, &loop_) {
}

BackgroundApplicationListModelTest::~BackgroundApplicationListModelTest() {
  // Drop reference to ExtensionService and TestingProfile, so that they can be
  // destroyed while BrowserThreads and MessageLoop are still around.  They
  // are used in the destruction process.
  service_ = NULL;
  profile_.reset(NULL);
  MessageLoop::current()->RunAllPending();
}

// This is modeled on a similar routine in ExtensionServiceTestBase.
void BackgroundApplicationListModelTest::InitializeEmptyExtensionService() {
  TestingProfile* profile = new TestingProfile();
  profile_.reset(profile);
  service_ = profile->CreateExtensionService(
      CommandLine::ForCurrentProcess(),
      bogus_file_path());
  service_->set_extensions_enabled(true);
  service_->set_show_extensions_prompts(false);
  service_->OnLoadedInstalledExtensions(); /* Sends EXTENSIONS_READY */
}

// Returns a barebones test Extension object with the specified |name|.  The
// returned extension will include background permission iff
// |background_permission| is true.
static scoped_refptr<Extension> CreateExtension(const std::string& name,
                                                bool background_permission) {
  DictionaryValue manifest;
  manifest.SetString(extension_manifest_keys::kVersion, "1.0.0.0");
  manifest.SetString(extension_manifest_keys::kName, name);
  if (background_permission) {
    ListValue* permissions = new ListValue();
    manifest.Set(extension_manifest_keys::kPermissions, permissions);
    permissions->Append(Value::CreateStringValue("background"));
  }
  std::string error;
  scoped_refptr<Extension> extension = Extension::Create(
      bogus_file_path().AppendASCII(name), Extension::INVALID, manifest, false,
      true, &error);
  // Cannot ASSERT_* here because that attempts an illegitimate return.
  // Cannot EXPECT_NE here because that assumes non-pointers unlike EXPECT_EQ
  EXPECT_TRUE(extension.get() != NULL) << error;
  return extension;
}

// With minimal test logic, verifies behavior over an explicit set of
// extensions, of which some are Background Apps and others are not.
TEST_F(BackgroundApplicationListModelTest, LoadExplicitExtensions) {
  InitializeEmptyExtensionService();
  ExtensionService* service = profile_->GetExtensionService();
  ASSERT_TRUE(service);
  ASSERT_TRUE(service->is_ready());
  ASSERT_TRUE(service->extensions());
  ASSERT_TRUE(service->extensions()->empty());
  scoped_ptr<BackgroundApplicationListModel> model(
      new BackgroundApplicationListModel(profile_.get()));
  ASSERT_EQ(0U, model->size());

  scoped_refptr<Extension> ext1 = CreateExtension("alpha", false);
  scoped_refptr<Extension> ext2 = CreateExtension("bravo", false);
  scoped_refptr<Extension> ext3 = CreateExtension("charlie", false);
  scoped_refptr<Extension> bgapp1 = CreateExtension("delta", true);
  scoped_refptr<Extension> bgapp2 = CreateExtension("echo", true);
  ASSERT_TRUE(service->extensions() != NULL);
  ASSERT_EQ(0U, service->extensions()->size());
  ASSERT_EQ(0U, model->size());
  // Add alternating Extensions and Background Apps
  ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext1));
  service->AddExtension(ext1);
  ASSERT_EQ(1U, service->extensions()->size());
  ASSERT_EQ(0U, model->size());
  ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp1));
  service->AddExtension(bgapp1);
  ASSERT_EQ(2U, service->extensions()->size());
  ASSERT_EQ(1U, model->size());
  ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext2));
  service->AddExtension(ext2);
  ASSERT_EQ(3U, service->extensions()->size());
  ASSERT_EQ(1U, model->size());
  ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp2));
  service->AddExtension(bgapp2);
  ASSERT_EQ(4U, service->extensions()->size());
  ASSERT_EQ(2U, model->size());
  ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext3));
  service->AddExtension(ext3);
  ASSERT_EQ(5U, service->extensions()->size());
  ASSERT_EQ(2U, model->size());
  // Remove in FIFO order.
  ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext1));
  service->UninstallExtension(ext1->id(), false);
  ASSERT_EQ(4U, service->extensions()->size());
  ASSERT_EQ(2U, model->size());
  ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp1));
  service->UninstallExtension(bgapp1->id(), false);
  ASSERT_EQ(3U, service->extensions()->size());
  ASSERT_EQ(1U, model->size());
  ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext2));
  service->UninstallExtension(ext2->id(), false);
  ASSERT_EQ(2U, service->extensions()->size());
  ASSERT_EQ(1U, model->size());
  ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp2));
  service->UninstallExtension(bgapp2->id(), false);
  ASSERT_EQ(1U, service->extensions()->size());
  ASSERT_EQ(0U, model->size());
  ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext3));
  service->UninstallExtension(ext3->id(), false);
  ASSERT_EQ(0U, service->extensions()->size());
  ASSERT_EQ(0U, model->size());
}

typedef std::set<scoped_refptr<Extension> > ExtensionSet;

namespace {
std::string GenerateUniqueExtensionName() {
  static int uniqueness = 0;
  std::ostringstream output;
  output << "Unique Named Extension " << uniqueness;
  ++uniqueness;
  return output.str();
}
}

// Verifies behavior with a pseudo-randomly generated set of actions: Adding and
// removing extensions, of which some are Background Apps and others are not.
TEST_F(BackgroundApplicationListModelTest, LoadRandomExtension) {
  InitializeEmptyExtensionService();
  ExtensionService* service = profile_->GetExtensionService();
  ASSERT_TRUE(service);
  ASSERT_TRUE(service->is_ready());
  ASSERT_TRUE(service->extensions());
  ASSERT_TRUE(service->extensions()->empty());
  scoped_ptr<BackgroundApplicationListModel> model(
      new BackgroundApplicationListModel(profile_.get()));
  ASSERT_EQ(0U, model->size());

  static const int kIterations = 500;
  ExtensionSet extensions;
  size_t count = 0;
  size_t expected = 0;
  srand(RANDOM_SEED);
  for (int index = 0; index < kIterations; ++index) {
    if (rand() % 2) {  // Add an extension
      std::string name = GenerateUniqueExtensionName();
      bool create_background = false;
      if (rand() % 2) {
        create_background = true;
        ++expected;
      }
      scoped_refptr<Extension> extension =
          CreateExtension(name, create_background);
      ASSERT_EQ(BackgroundApplicationListModel::IsBackgroundApp(*extension),
                create_background);
      extensions.insert(extension);
      ++count;
      ASSERT_EQ(count, extensions.size());
      service->AddExtension(extension);
      ASSERT_EQ(count, service->extensions()->size());
      ASSERT_EQ(expected, model->size());
    } else {  // Maybe remove an extension.
      ExtensionSet::iterator cursor = extensions.begin();
      if (cursor == extensions.end()) {
        // Nothing to remove.  Just verify accounting.
        ASSERT_EQ(0U, count);
        ASSERT_EQ(0U, expected);
        ASSERT_EQ(0U, service->extensions()->size());
        ASSERT_EQ(0U, model->size());
      } else {
        // Randomly select which extension to remove
        if (extensions.size() > 1) {
          int offset = rand() % (extensions.size() - 1);
          for (int index = 0; index < offset; ++index)
            ++cursor;
        }
        scoped_refptr<Extension> extension = cursor->get();
        std::string id = extension->id();
        if (BackgroundApplicationListModel::IsBackgroundApp(*extension))
          --expected;
        extensions.erase(cursor);
        --count;
        ASSERT_EQ(count, extensions.size());
        service->UninstallExtension(extension->id(), false);
        ASSERT_EQ(count, service->extensions()->size());
        ASSERT_EQ(expected, model->size());
      }
    }
  }
}