Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / browser / runtime_data_unittest.cc
blob23b8098fb55515822a0f8f3eaddc7d281f8b1ef5
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"
7 #include <string>
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 {
17 namespace {
19 // Creates a very simple extension.
20 scoped_refptr<Extension> CreateExtension() {
21 return ExtensionBuilder()
22 .SetManifest(
23 DictionaryBuilder().Set("name", "test").Set("version", "0.1"))
24 .SetID("id1")
25 .Build();
28 // Creates a very simple extension with a background page.
29 scoped_refptr<Extension> CreateExtensionWithBackgroundPage() {
30 return ExtensionBuilder()
31 .SetManifest(
32 DictionaryBuilder()
33 .Set("name", "test")
34 .Set("version", "0.1")
35 .Set("background", DictionaryBuilder().Set("page", "bg.html")))
36 .SetID("id2")
37 .Build();
40 class RuntimeDataTest : public testing::Test {
41 public:
42 RuntimeDataTest() : registry_(NULL), runtime_data_(&registry_) {}
43 virtual ~RuntimeDataTest() {}
45 protected:
46 ExtensionRegistry registry_;
47 RuntimeData runtime_data_;
49 private:
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.get()));
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.get()));
63 // The flag can be toggled.
64 runtime_data_.SetBackgroundPageReady(with_background.get(), true);
65 EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(with_background.get()));
66 runtime_data_.SetBackgroundPageReady(with_background.get(), false);
67 EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get()));
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.get()));
76 // The flag can be toggled.
77 runtime_data_.SetBeingUpgraded(extension.get(), true);
78 EXPECT_TRUE(runtime_data_.IsBeingUpgraded(extension.get()));
79 runtime_data_.SetBeingUpgraded(extension.get(), false);
80 EXPECT_FALSE(runtime_data_.IsBeingUpgraded(extension.get()));
83 TEST_F(RuntimeDataTest, HasUsedWebRequest) {
84 scoped_refptr<Extension> extension = CreateExtension();
86 // An extension has not used web request until the flag is set.
87 EXPECT_FALSE(runtime_data_.HasUsedWebRequest(extension.get()));
89 // The flag can be toggled.
90 runtime_data_.SetHasUsedWebRequest(extension.get(), true);
91 EXPECT_TRUE(runtime_data_.HasUsedWebRequest(extension.get()));
92 runtime_data_.SetHasUsedWebRequest(extension.get(), false);
93 EXPECT_FALSE(runtime_data_.HasUsedWebRequest(extension.get()));
96 // Unloading an extension stops tracking it.
97 TEST_F(RuntimeDataTest, OnExtensionUnloaded) {
98 scoped_refptr<Extension> extension = CreateExtensionWithBackgroundPage();
99 runtime_data_.SetBackgroundPageReady(extension.get(), true);
100 ASSERT_TRUE(runtime_data_.HasExtensionForTesting(extension.get()));
102 runtime_data_.OnExtensionUnloaded(
103 NULL, extension.get(), UnloadedExtensionInfo::REASON_DISABLE);
104 EXPECT_FALSE(runtime_data_.HasExtensionForTesting(extension.get()));
107 } // namespace
108 } // namespace extensions