1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_action.h"
#include "extensions/browser/extension_action_manager.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/state_store.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "extensions/test/extension_test_message_listener.h"
#include "third_party/skia/include/core/SkColor.h"
namespace extensions {
namespace {
// A key into the StateStore; we don't use any results, but need to know when
// it's initialized.
const char kBrowserActionStorageKey[] = "browser_action";
// The name of the extension we add.
const char kExtensionName[] = "Default Persistence Test Extension";
void QuitMessageLoop(content::MessageLoopRunner* runner,
std::optional<base::Value> value) {
runner->Quit();
}
// We need to wait for the state store to initialize and respond to requests
// so we can see if the preferences persist. Do this by posting our own request
// to the state store, which should be handled after all others.
void WaitForStateStore(Profile* profile, const ExtensionId& extension_id) {
scoped_refptr<content::MessageLoopRunner> runner =
new content::MessageLoopRunner;
ExtensionSystem::Get(profile)->state_store()->GetExtensionValue(
extension_id, kBrowserActionStorageKey,
base::BindOnce(&QuitMessageLoop, base::RetainedRef(runner)));
runner->Run();
}
} // namespace
// Setup for the test by loading an extension, which should set the browser
// action background to blue.
IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest,
PRE_BrowserActionDefaultPersistence) {
ExtensionTestMessageListener listener("Background Color Set");
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("default_persistence"));
ASSERT_TRUE(extension);
ASSERT_EQ(kExtensionName, extension->name());
WaitForStateStore(profile(), extension->id());
// Make sure we've given the extension enough time to set the background color
// in chrome.runtime.onInstalled.
ASSERT_TRUE(listener.WaitUntilSatisfied());
ExtensionAction* extension_action =
ExtensionActionManager::Get(profile())->GetExtensionAction(*extension);
ASSERT_TRUE(extension_action);
EXPECT_EQ(SK_ColorBLUE, extension_action->GetBadgeBackgroundColor(0));
}
// When Chrome restarts, the Extension will immediately update the browser
// action, but will not modify the badge background color. Thus, the background
// should remain blue (persisting the default set in onInstalled()).
IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, BrowserActionDefaultPersistence) {
// Find the extension (it's a shame we don't have an ID for this, but it
// was generated in the last test).
const Extension* extension = nullptr;
const ExtensionSet& extension_set =
ExtensionRegistry::Get(profile())->enabled_extensions();
for (ExtensionSet::const_iterator iter = extension_set.begin();
iter != extension_set.end();
++iter) {
if ((*iter)->name() == kExtensionName) {
extension = iter->get();
break;
}
}
ASSERT_TRUE(extension) << "Could not find extension in registry.";
ExtensionAction* extension_action =
ExtensionActionManager::Get(profile())->GetExtensionAction(*extension);
ASSERT_TRUE(extension_action);
// If the extension hasn't already set the badge text, then we should wait for
// it to do so.
if (extension_action->GetExplicitlySetBadgeText(0) != "Hello") {
ExtensionTestMessageListener listener("Badge Text Set");
ASSERT_TRUE(listener.WaitUntilSatisfied());
}
// If this log becomes frequent, this test is losing its effectiveness, and
// we need to find a more invasive way of ensuring the test's StateStore
// initializes after extensions get their onStartup event.
if (ExtensionSystem::Get(profile())->state_store()->IsInitialized()) {
LOG(WARNING) << "State store already initialized; test guaranteed to pass.";
}
// Wait for the StateStore to load, and fetch the defaults.
WaitForStateStore(profile(), extension->id());
// Ensure the BrowserAction's badge background is still blue.
EXPECT_EQ(SK_ColorBLUE, extension_action->GetBadgeBackgroundColor(0));
}
} // namespace extensions
|