[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "extensions/browser/runtime_data.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 9 | #include "base/macros.h" |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "extensions/browser/extension_registry.h" |
| 12 | #include "extensions/common/extension.h" |
| 13 | #include "extensions/common/extension_builder.h" |
limasdf | f0dcf2f | 2014-09-18 20:17:37 | [diff] [blame] | 14 | #include "extensions/common/test_util.h" |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 15 | #include "extensions/common/value_builder.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace extensions { |
| 19 | namespace { |
| 20 | |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 21 | // Creates a very simple extension with a background page. |
| 22 | scoped_refptr<Extension> CreateExtensionWithBackgroundPage() { |
| 23 | return ExtensionBuilder() |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 24 | .SetManifest(DictionaryBuilder() |
| 25 | .Set("name", "test") |
| 26 | .Set("version", "0.1") |
| 27 | .Set("background", |
| 28 | DictionaryBuilder().Set("page", "bg.html").Build()) |
| 29 | .Build()) |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 30 | .SetID("id2") |
| 31 | .Build(); |
| 32 | } |
| 33 | |
| 34 | class RuntimeDataTest : public testing::Test { |
| 35 | public: |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 36 | RuntimeDataTest() : registry_(nullptr), runtime_data_(®istry_) {} |
dcheng | f9afb37 | 2014-10-27 21:43:14 | [diff] [blame] | 37 | ~RuntimeDataTest() override {} |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 38 | |
| 39 | protected: |
| 40 | ExtensionRegistry registry_; |
| 41 | RuntimeData runtime_data_; |
| 42 | |
| 43 | private: |
| 44 | DISALLOW_COPY_AND_ASSIGN(RuntimeDataTest); |
| 45 | }; |
| 46 | |
| 47 | TEST_F(RuntimeDataTest, IsBackgroundPageReady) { |
| 48 | // An extension without a background page is always considered ready. |
limasdf | f0dcf2f | 2014-09-18 20:17:37 | [diff] [blame] | 49 | scoped_refptr<Extension> no_background = test_util::CreateEmptyExtension(); |
dcheng | 7921e3f | 2014-08-25 22:20:01 | [diff] [blame] | 50 | EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(no_background.get())); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 51 | |
| 52 | // An extension with a background page is not ready until the flag is set. |
| 53 | scoped_refptr<Extension> with_background = |
| 54 | CreateExtensionWithBackgroundPage(); |
dcheng | 7921e3f | 2014-08-25 22:20:01 | [diff] [blame] | 55 | EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get())); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 56 | |
| 57 | // The flag can be toggled. |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 58 | runtime_data_.SetBackgroundPageReady(with_background->id(), true); |
dcheng | 7921e3f | 2014-08-25 22:20:01 | [diff] [blame] | 59 | EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(with_background.get())); |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 60 | runtime_data_.SetBackgroundPageReady(with_background->id(), false); |
dcheng | 7921e3f | 2014-08-25 22:20:01 | [diff] [blame] | 61 | EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get())); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | TEST_F(RuntimeDataTest, IsBeingUpgraded) { |
limasdf | f0dcf2f | 2014-09-18 20:17:37 | [diff] [blame] | 65 | scoped_refptr<Extension> extension = test_util::CreateEmptyExtension(); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 66 | |
| 67 | // An extension is not being upgraded until the flag is set. |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 68 | EXPECT_FALSE(runtime_data_.IsBeingUpgraded(extension->id())); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 69 | |
| 70 | // The flag can be toggled. |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 71 | runtime_data_.SetBeingUpgraded(extension->id(), true); |
| 72 | EXPECT_TRUE(runtime_data_.IsBeingUpgraded(extension->id())); |
| 73 | runtime_data_.SetBeingUpgraded(extension->id(), false); |
| 74 | EXPECT_FALSE(runtime_data_.IsBeingUpgraded(extension->id())); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 75 | } |
| 76 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 77 | // Unloading an extension erases any data that shouldn't explicitly be kept |
| 78 | // across loads. |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 79 | TEST_F(RuntimeDataTest, OnExtensionUnloaded) { |
| 80 | scoped_refptr<Extension> extension = CreateExtensionWithBackgroundPage(); |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 81 | runtime_data_.SetBackgroundPageReady(extension->id(), true); |
| 82 | ASSERT_TRUE(runtime_data_.HasExtensionForTesting(extension->id())); |
| 83 | runtime_data_.SetBeingUpgraded(extension->id(), true); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 84 | |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 85 | runtime_data_.OnExtensionUnloaded(nullptr, extension.get(), |
| 86 | UnloadedExtensionReason::DISABLE); |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 87 | EXPECT_TRUE(runtime_data_.HasExtensionForTesting(extension->id())); |
| 88 | EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(extension.get())); |
| 89 | EXPECT_TRUE(runtime_data_.IsBeingUpgraded(extension->id())); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | } // namespace |
| 93 | } // namespace extensions |