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/value_builder.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace extensions
{
19 // Creates a very simple extension.
20 scoped_refptr
<Extension
> CreateExtension() {
21 return ExtensionBuilder()
23 DictionaryBuilder().Set("name", "test").Set("version", "0.1"))
28 // Creates a very simple extension with a background page.
29 scoped_refptr
<Extension
> CreateExtensionWithBackgroundPage() {
30 return ExtensionBuilder()
34 .Set("version", "0.1")
35 .Set("background", DictionaryBuilder().Set("page", "bg.html")))
40 class RuntimeDataTest
: public testing::Test
{
42 RuntimeDataTest() : runtime_data_(®istry_
) {}
43 virtual ~RuntimeDataTest() {}
46 ExtensionRegistry registry_
;
47 RuntimeData runtime_data_
;
50 DISALLOW_COPY_AND_ASSIGN(RuntimeDataTest
);
53 TEST_F(RuntimeDataTest
, IsBackgroundPageReady
) {
54 // An extension without a background page is always considered ready.
55 scoped_refptr
<Extension
> no_background
= CreateExtension();
56 EXPECT_TRUE(runtime_data_
.IsBackgroundPageReady(no_background
));
58 // An extension with a background page is not ready until the flag is set.
59 scoped_refptr
<Extension
> with_background
=
60 CreateExtensionWithBackgroundPage();
61 EXPECT_FALSE(runtime_data_
.IsBackgroundPageReady(with_background
));
63 // The flag can be toggled.
64 runtime_data_
.SetBackgroundPageReady(with_background
, true);
65 EXPECT_TRUE(runtime_data_
.IsBackgroundPageReady(with_background
));
66 runtime_data_
.SetBackgroundPageReady(with_background
, false);
67 EXPECT_FALSE(runtime_data_
.IsBackgroundPageReady(with_background
));
70 TEST_F(RuntimeDataTest
, IsBeingUpgraded
) {
71 scoped_refptr
<Extension
> extension
= CreateExtension();
73 // An extension is not being upgraded until the flag is set.
74 EXPECT_FALSE(runtime_data_
.IsBeingUpgraded(extension
));
76 // The flag can be toggled.
77 runtime_data_
.SetBeingUpgraded(extension
, true);
78 EXPECT_TRUE(runtime_data_
.IsBeingUpgraded(extension
));
79 runtime_data_
.SetBeingUpgraded(extension
, false);
80 EXPECT_FALSE(runtime_data_
.IsBeingUpgraded(extension
));
83 // TODO(mpcomplete): Remove. http://crbug.com/100411
84 TEST_F(RuntimeDataTest
, HasUsedWebRequest
) {
85 scoped_refptr
<Extension
> extension
= CreateExtension();
87 // An extension has not used web request until the flag is set.
88 EXPECT_FALSE(runtime_data_
.HasUsedWebRequest(extension
));
90 // The flag can be toggled.
91 runtime_data_
.SetHasUsedWebRequest(extension
, true);
92 EXPECT_TRUE(runtime_data_
.HasUsedWebRequest(extension
));
93 runtime_data_
.SetHasUsedWebRequest(extension
, false);
94 EXPECT_FALSE(runtime_data_
.HasUsedWebRequest(extension
));
97 // Unloading an extension stops tracking it.
98 TEST_F(RuntimeDataTest
, OnExtensionUnloaded
) {
99 scoped_refptr
<Extension
> extension
= CreateExtensionWithBackgroundPage();
100 runtime_data_
.SetBackgroundPageReady(extension
, true);
101 ASSERT_TRUE(runtime_data_
.HasExtensionForTesting(extension
));
103 runtime_data_
.OnExtensionUnloaded(extension
);
104 EXPECT_FALSE(runtime_data_
.HasExtensionForTesting(extension
));
108 } // namespace extensions