1 // Copyright 2014 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.
5 #include "extensions/browser/runtime_data.h"
9 #include "base/memory/ref_counted.h"
10 #include "extensions/browser/extension_registry.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/extension_builder.h"
13 #include "extensions/common/test_util.h"
14 #include "extensions/common/value_builder.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace extensions
{
20 // Creates a very simple extension with a background page.
21 scoped_refptr
<Extension
> CreateExtensionWithBackgroundPage() {
22 return ExtensionBuilder()
26 .Set("version", "0.1")
27 .Set("background", DictionaryBuilder().Set("page", "bg.html")))
32 class RuntimeDataTest
: public testing::Test
{
34 RuntimeDataTest() : registry_(NULL
), runtime_data_(®istry_
) {}
35 ~RuntimeDataTest() override
{}
38 ExtensionRegistry registry_
;
39 RuntimeData runtime_data_
;
42 DISALLOW_COPY_AND_ASSIGN(RuntimeDataTest
);
45 TEST_F(RuntimeDataTest
, IsBackgroundPageReady
) {
46 // An extension without a background page is always considered ready.
47 scoped_refptr
<Extension
> no_background
= test_util::CreateEmptyExtension();
48 EXPECT_TRUE(runtime_data_
.IsBackgroundPageReady(no_background
.get()));
50 // An extension with a background page is not ready until the flag is set.
51 scoped_refptr
<Extension
> with_background
=
52 CreateExtensionWithBackgroundPage();
53 EXPECT_FALSE(runtime_data_
.IsBackgroundPageReady(with_background
.get()));
55 // The flag can be toggled.
56 runtime_data_
.SetBackgroundPageReady(with_background
->id(), true);
57 EXPECT_TRUE(runtime_data_
.IsBackgroundPageReady(with_background
.get()));
58 runtime_data_
.SetBackgroundPageReady(with_background
->id(), false);
59 EXPECT_FALSE(runtime_data_
.IsBackgroundPageReady(with_background
.get()));
62 TEST_F(RuntimeDataTest
, IsBeingUpgraded
) {
63 scoped_refptr
<Extension
> extension
= test_util::CreateEmptyExtension();
65 // An extension is not being upgraded until the flag is set.
66 EXPECT_FALSE(runtime_data_
.IsBeingUpgraded(extension
->id()));
68 // The flag can be toggled.
69 runtime_data_
.SetBeingUpgraded(extension
->id(), true);
70 EXPECT_TRUE(runtime_data_
.IsBeingUpgraded(extension
->id()));
71 runtime_data_
.SetBeingUpgraded(extension
->id(), false);
72 EXPECT_FALSE(runtime_data_
.IsBeingUpgraded(extension
->id()));
75 TEST_F(RuntimeDataTest
, HasUsedWebRequest
) {
76 scoped_refptr
<Extension
> extension
= test_util::CreateEmptyExtension();
78 // An extension has not used web request until the flag is set.
79 EXPECT_FALSE(runtime_data_
.HasUsedWebRequest(extension
->id()));
81 // The flag can be toggled.
82 runtime_data_
.SetHasUsedWebRequest(extension
->id(), true);
83 EXPECT_TRUE(runtime_data_
.HasUsedWebRequest(extension
->id()));
84 runtime_data_
.SetHasUsedWebRequest(extension
->id(), false);
85 EXPECT_FALSE(runtime_data_
.HasUsedWebRequest(extension
->id()));
88 // Unloading an extension erases any data that shouldn't explicitly be kept
90 TEST_F(RuntimeDataTest
, OnExtensionUnloaded
) {
91 scoped_refptr
<Extension
> extension
= CreateExtensionWithBackgroundPage();
92 runtime_data_
.SetBackgroundPageReady(extension
->id(), true);
93 ASSERT_TRUE(runtime_data_
.HasExtensionForTesting(extension
->id()));
94 runtime_data_
.SetBeingUpgraded(extension
->id(), true);
96 runtime_data_
.OnExtensionUnloaded(
97 NULL
, extension
.get(), UnloadedExtensionInfo::REASON_DISABLE
);
98 EXPECT_TRUE(runtime_data_
.HasExtensionForTesting(extension
->id()));
99 EXPECT_FALSE(runtime_data_
.IsBackgroundPageReady(extension
.get()));
100 EXPECT_TRUE(runtime_data_
.IsBeingUpgraded(extension
->id()));
104 } // namespace extensions