blob: 948b24cbd18b4f42b08bbbd2c63713beef0bebda [file] [log] [blame]
[email protected]45f5b7d2014-01-22 23:47:131// 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
avic9cec102015-12-23 00:39:269#include "base/macros.h"
[email protected]45f5b7d2014-01-22 23:47:1310#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"
limasdff0dcf2f2014-09-18 20:17:3714#include "extensions/common/test_util.h"
[email protected]45f5b7d2014-01-22 23:47:1315#include "extensions/common/value_builder.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace extensions {
19namespace {
20
[email protected]45f5b7d2014-01-22 23:47:1321// Creates a very simple extension with a background page.
22scoped_refptr<Extension> CreateExtensionWithBackgroundPage() {
23 return ExtensionBuilder()
dcheng794d2bd2016-02-27 03:51:3224 .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]45f5b7d2014-01-22 23:47:1330 .SetID("id2")
31 .Build();
32}
33
34class RuntimeDataTest : public testing::Test {
35 public:
limasdf0deef2042017-05-03 19:17:1736 RuntimeDataTest() : registry_(nullptr), runtime_data_(&registry_) {}
dchengf9afb372014-10-27 21:43:1437 ~RuntimeDataTest() override {}
[email protected]45f5b7d2014-01-22 23:47:1338
39 protected:
40 ExtensionRegistry registry_;
41 RuntimeData runtime_data_;
42
43 private:
44 DISALLOW_COPY_AND_ASSIGN(RuntimeDataTest);
45};
46
47TEST_F(RuntimeDataTest, IsBackgroundPageReady) {
48 // An extension without a background page is always considered ready.
limasdff0dcf2f2014-09-18 20:17:3749 scoped_refptr<Extension> no_background = test_util::CreateEmptyExtension();
dcheng7921e3f2014-08-25 22:20:0150 EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(no_background.get()));
[email protected]45f5b7d2014-01-22 23:47:1351
52 // An extension with a background page is not ready until the flag is set.
53 scoped_refptr<Extension> with_background =
54 CreateExtensionWithBackgroundPage();
dcheng7921e3f2014-08-25 22:20:0155 EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get()));
[email protected]45f5b7d2014-01-22 23:47:1356
57 // The flag can be toggled.
rdevlin.cronin2c16c602014-11-21 01:35:4958 runtime_data_.SetBackgroundPageReady(with_background->id(), true);
dcheng7921e3f2014-08-25 22:20:0159 EXPECT_TRUE(runtime_data_.IsBackgroundPageReady(with_background.get()));
rdevlin.cronin2c16c602014-11-21 01:35:4960 runtime_data_.SetBackgroundPageReady(with_background->id(), false);
dcheng7921e3f2014-08-25 22:20:0161 EXPECT_FALSE(runtime_data_.IsBackgroundPageReady(with_background.get()));
[email protected]45f5b7d2014-01-22 23:47:1362}
63
64TEST_F(RuntimeDataTest, IsBeingUpgraded) {
limasdff0dcf2f2014-09-18 20:17:3765 scoped_refptr<Extension> extension = test_util::CreateEmptyExtension();
[email protected]45f5b7d2014-01-22 23:47:1366
67 // An extension is not being upgraded until the flag is set.
rdevlin.cronin2c16c602014-11-21 01:35:4968 EXPECT_FALSE(runtime_data_.IsBeingUpgraded(extension->id()));
[email protected]45f5b7d2014-01-22 23:47:1369
70 // The flag can be toggled.
rdevlin.cronin2c16c602014-11-21 01:35:4971 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]45f5b7d2014-01-22 23:47:1375}
76
rdevlin.cronin2c16c602014-11-21 01:35:4977// Unloading an extension erases any data that shouldn't explicitly be kept
78// across loads.
[email protected]45f5b7d2014-01-22 23:47:1379TEST_F(RuntimeDataTest, OnExtensionUnloaded) {
80 scoped_refptr<Extension> extension = CreateExtensionWithBackgroundPage();
rdevlin.cronin2c16c602014-11-21 01:35:4981 runtime_data_.SetBackgroundPageReady(extension->id(), true);
82 ASSERT_TRUE(runtime_data_.HasExtensionForTesting(extension->id()));
83 runtime_data_.SetBeingUpgraded(extension->id(), true);
[email protected]45f5b7d2014-01-22 23:47:1384
limasdf0deef2042017-05-03 19:17:1785 runtime_data_.OnExtensionUnloaded(nullptr, extension.get(),
86 UnloadedExtensionReason::DISABLE);
rdevlin.cronin2c16c602014-11-21 01:35:4987 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]45f5b7d2014-01-22 23:47:1390}
91
92} // namespace
93} // namespace extensions