summaryrefslogtreecommitdiffstats
path: root/extensions/browser/runtime_data_unittest.cc
blob: 790dd257bc018fc07971fc602be5e7edb5b6afaa (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
// Copyright 2014 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.

#include "extensions/browser/runtime_data.h"

#include <string>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/test_util.h"
#include "extensions/common/value_builder.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {
namespace {

// Creates a very simple extension with a background page.
scoped_refptr<Extension> CreateExtensionWithBackgroundPage() {
  return ExtensionBuilder()
      .SetManifest(DictionaryBuilder()
                       .Set("name", "test")
                       .Set("version", "0.1")
                       .Set("background",
                            DictionaryBuilder().Set("page", "bg.html").Build())
                       .Build())
      .SetID("id2")
      .Build();
}

class RuntimeDataTest : public testing::Test {
 public:
  RuntimeDataTest() : registry_(NULL), runtime_data_(&registry_) {}
  ~RuntimeDataTest() override {}

 protected:
  ExtensionRegistry registry_;
  RuntimeData runtime_data_;

 private:
  DISALLOW_COPY_AND_ASSIGN(RuntimeDataTest);
};

TEST_F(RuntimeDataTest, IsBackgroundPageReady) {
  // An extension without a background page is always considered ready.
  scoped_refptr<Extension> no_background = test_util::CreateEmptyExtension();
  EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(no_background.get()));

  // An extension with a background page is not ready until the flag is set.
  scoped_refptr<Extension> with_background =
      CreateExtensionWithBackgroundPage();
  EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get()));

  // The flag can be toggled.
  runtime_data_.SetBackgroundPageReady(with_background->id(), true);
  EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(with_background.get()));
  runtime_data_.SetBackgroundPageReady(with_background->id(), false);
  EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get()));
}

TEST_F(RuntimeDataTest, IsBeingUpgraded) {
  scoped_refptr<Extension> extension = test_util::CreateEmptyExtension();

  // An extension is not being upgraded until the flag is set.
  EXPECT_FALSE(runtime_data_.IsBeingUpgraded(extension->id()));

  // The flag can be toggled.
  runtime_data_.SetBeingUpgraded(extension->id(), true);
  EXPECT_TRUE(runtime_data_.IsBeingUpgraded(extension->id()));
  runtime_data_.SetBeingUpgraded(extension->id(), false);
  EXPECT_FALSE(runtime_data_.IsBeingUpgraded(extension->id()));
}

TEST_F(RuntimeDataTest, HasUsedWebRequest) {
  scoped_refptr<Extension> extension = test_util::CreateEmptyExtension();

  // An extension has not used web request until the flag is set.
  EXPECT_FALSE(runtime_data_.HasUsedWebRequest(extension->id()));

  // The flag can be toggled.
  runtime_data_.SetHasUsedWebRequest(extension->id(), true);
  EXPECT_TRUE(runtime_data_.HasUsedWebRequest(extension->id()));
  runtime_data_.SetHasUsedWebRequest(extension->id(), false);
  EXPECT_FALSE(runtime_data_.HasUsedWebRequest(extension->id()));
}

// Unloading an extension erases any data that shouldn't explicitly be kept
// across loads.
TEST_F(RuntimeDataTest, OnExtensionUnloaded) {
  scoped_refptr<Extension> extension = CreateExtensionWithBackgroundPage();
  runtime_data_.SetBackgroundPageReady(extension->id(), true);
  ASSERT_TRUE(runtime_data_.HasExtensionForTesting(extension->id()));
  runtime_data_.SetBeingUpgraded(extension->id(), true);

  runtime_data_.OnExtensionUnloaded(
      NULL, extension.get(), UnloadedExtensionInfo::REASON_DISABLE);
  EXPECT_TRUE(runtime_data_.HasExtensionForTesting(extension->id()));
  EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(extension.get()));
  EXPECT_TRUE(runtime_data_.IsBeingUpgraded(extension->id()));
}

}  // namespace
}  // namespace extensions