OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/runtime_data.h" | 5 #include "extensions/browser/runtime_data.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "extensions/browser/extension_registry.h" | 10 #include "extensions/browser/extension_registry.h" |
11 #include "extensions/common/extension.h" | 11 #include "extensions/common/extension.h" |
12 #include "extensions/common/extension_builder.h" | 12 #include "extensions/common/extension_builder.h" |
| 13 #include "extensions/common/test_util.h" |
13 #include "extensions/common/value_builder.h" | 14 #include "extensions/common/value_builder.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace extensions { | 17 namespace extensions { |
17 namespace { | 18 namespace { |
18 | 19 |
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(); | |
26 } | |
27 | |
28 // Creates a very simple extension with a background page. | 20 // Creates a very simple extension with a background page. |
29 scoped_refptr<Extension> CreateExtensionWithBackgroundPage() { | 21 scoped_refptr<Extension> CreateExtensionWithBackgroundPage() { |
30 return ExtensionBuilder() | 22 return ExtensionBuilder() |
31 .SetManifest( | 23 .SetManifest( |
32 DictionaryBuilder() | 24 DictionaryBuilder() |
33 .Set("name", "test") | 25 .Set("name", "test") |
34 .Set("version", "0.1") | 26 .Set("version", "0.1") |
35 .Set("background", DictionaryBuilder().Set("page", "bg.html"))) | 27 .Set("background", DictionaryBuilder().Set("page", "bg.html"))) |
36 .SetID("id2") | 28 .SetID("id2") |
37 .Build(); | 29 .Build(); |
38 } | 30 } |
39 | 31 |
40 class RuntimeDataTest : public testing::Test { | 32 class RuntimeDataTest : public testing::Test { |
41 public: | 33 public: |
42 RuntimeDataTest() : registry_(NULL), runtime_data_(®istry_) {} | 34 RuntimeDataTest() : registry_(NULL), runtime_data_(®istry_) {} |
43 virtual ~RuntimeDataTest() {} | 35 virtual ~RuntimeDataTest() {} |
44 | 36 |
45 protected: | 37 protected: |
46 ExtensionRegistry registry_; | 38 ExtensionRegistry registry_; |
47 RuntimeData runtime_data_; | 39 RuntimeData runtime_data_; |
48 | 40 |
49 private: | 41 private: |
50 DISALLOW_COPY_AND_ASSIGN(RuntimeDataTest); | 42 DISALLOW_COPY_AND_ASSIGN(RuntimeDataTest); |
51 }; | 43 }; |
52 | 44 |
53 TEST_F(RuntimeDataTest, IsBackgroundPageReady) { | 45 TEST_F(RuntimeDataTest, IsBackgroundPageReady) { |
54 // An extension without a background page is always considered ready. | 46 // An extension without a background page is always considered ready. |
55 scoped_refptr<Extension> no_background = CreateExtension(); | 47 scoped_refptr<Extension> no_background = test_util::CreateEmptyExtension(); |
56 EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(no_background.get())); | 48 EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(no_background.get())); |
57 | 49 |
58 // An extension with a background page is not ready until the flag is set. | 50 // An extension with a background page is not ready until the flag is set. |
59 scoped_refptr<Extension> with_background = | 51 scoped_refptr<Extension> with_background = |
60 CreateExtensionWithBackgroundPage(); | 52 CreateExtensionWithBackgroundPage(); |
61 EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get())); | 53 EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get())); |
62 | 54 |
63 // The flag can be toggled. | 55 // The flag can be toggled. |
64 runtime_data_.SetBackgroundPageReady(with_background.get(), true); | 56 runtime_data_.SetBackgroundPageReady(with_background.get(), true); |
65 EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(with_background.get())); | 57 EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(with_background.get())); |
66 runtime_data_.SetBackgroundPageReady(with_background.get(), false); | 58 runtime_data_.SetBackgroundPageReady(with_background.get(), false); |
67 EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get())); | 59 EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get())); |
68 } | 60 } |
69 | 61 |
70 TEST_F(RuntimeDataTest, IsBeingUpgraded) { | 62 TEST_F(RuntimeDataTest, IsBeingUpgraded) { |
71 scoped_refptr<Extension> extension = CreateExtension(); | 63 scoped_refptr<Extension> extension = test_util::CreateEmptyExtension(); |
72 | 64 |
73 // An extension is not being upgraded until the flag is set. | 65 // An extension is not being upgraded until the flag is set. |
74 EXPECT_FALSE(runtime_data_.IsBeingUpgraded(extension.get())); | 66 EXPECT_FALSE(runtime_data_.IsBeingUpgraded(extension.get())); |
75 | 67 |
76 // The flag can be toggled. | 68 // The flag can be toggled. |
77 runtime_data_.SetBeingUpgraded(extension.get(), true); | 69 runtime_data_.SetBeingUpgraded(extension.get(), true); |
78 EXPECT_TRUE(runtime_data_.IsBeingUpgraded(extension.get())); | 70 EXPECT_TRUE(runtime_data_.IsBeingUpgraded(extension.get())); |
79 runtime_data_.SetBeingUpgraded(extension.get(), false); | 71 runtime_data_.SetBeingUpgraded(extension.get(), false); |
80 EXPECT_FALSE(runtime_data_.IsBeingUpgraded(extension.get())); | 72 EXPECT_FALSE(runtime_data_.IsBeingUpgraded(extension.get())); |
81 } | 73 } |
82 | 74 |
83 TEST_F(RuntimeDataTest, HasUsedWebRequest) { | 75 TEST_F(RuntimeDataTest, HasUsedWebRequest) { |
84 scoped_refptr<Extension> extension = CreateExtension(); | 76 scoped_refptr<Extension> extension = test_util::CreateEmptyExtension(); |
85 | 77 |
86 // An extension has not used web request until the flag is set. | 78 // An extension has not used web request until the flag is set. |
87 EXPECT_FALSE(runtime_data_.HasUsedWebRequest(extension.get())); | 79 EXPECT_FALSE(runtime_data_.HasUsedWebRequest(extension.get())); |
88 | 80 |
89 // The flag can be toggled. | 81 // The flag can be toggled. |
90 runtime_data_.SetHasUsedWebRequest(extension.get(), true); | 82 runtime_data_.SetHasUsedWebRequest(extension.get(), true); |
91 EXPECT_TRUE(runtime_data_.HasUsedWebRequest(extension.get())); | 83 EXPECT_TRUE(runtime_data_.HasUsedWebRequest(extension.get())); |
92 runtime_data_.SetHasUsedWebRequest(extension.get(), false); | 84 runtime_data_.SetHasUsedWebRequest(extension.get(), false); |
93 EXPECT_FALSE(runtime_data_.HasUsedWebRequest(extension.get())); | 85 EXPECT_FALSE(runtime_data_.HasUsedWebRequest(extension.get())); |
94 } | 86 } |
95 | 87 |
96 // Unloading an extension stops tracking it. | 88 // Unloading an extension stops tracking it. |
97 TEST_F(RuntimeDataTest, OnExtensionUnloaded) { | 89 TEST_F(RuntimeDataTest, OnExtensionUnloaded) { |
98 scoped_refptr<Extension> extension = CreateExtensionWithBackgroundPage(); | 90 scoped_refptr<Extension> extension = CreateExtensionWithBackgroundPage(); |
99 runtime_data_.SetBackgroundPageReady(extension.get(), true); | 91 runtime_data_.SetBackgroundPageReady(extension.get(), true); |
100 ASSERT_TRUE(runtime_data_.HasExtensionForTesting(extension.get())); | 92 ASSERT_TRUE(runtime_data_.HasExtensionForTesting(extension.get())); |
101 | 93 |
102 runtime_data_.OnExtensionUnloaded( | 94 runtime_data_.OnExtensionUnloaded( |
103 NULL, extension.get(), UnloadedExtensionInfo::REASON_DISABLE); | 95 NULL, extension.get(), UnloadedExtensionInfo::REASON_DISABLE); |
104 EXPECT_FALSE(runtime_data_.HasExtensionForTesting(extension.get())); | 96 EXPECT_FALSE(runtime_data_.HasExtensionForTesting(extension.get())); |
105 } | 97 } |
106 | 98 |
107 } // namespace | 99 } // namespace |
108 } // namespace extensions | 100 } // namespace extensions |
OLD | NEW |