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 "base/files/file_path.h"
6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/extensions/extension_action.h"
8 #include "chrome/browser/extensions/extension_action_manager.h"
9 #include "chrome/browser/extensions/extension_browsertest.h"
10 #include "content/public/test/test_utils.h"
11 #include "extensions/browser/extension_registry.h"
12 #include "extensions/browser/extension_system.h"
13 #include "extensions/browser/state_store.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/test/extension_test_message_listener.h"
16 #include "third_party/skia/include/core/SkColor.h"
18 namespace extensions
{
22 // A key into the StateStore; we don't use any results, but need to know when
24 const char kBrowserActionStorageKey
[] = "browser_action";
25 // The name of the extension we add.
26 const char kExtensionName
[] = "Default Persistence Test Extension";
28 void QuitMessageLoop(content::MessageLoopRunner
* runner
,
29 scoped_ptr
<base::Value
> value
) {
33 // We need to wait for the state store to initialize and respond to requests
34 // so we can see if the preferences persist. Do this by posting our own request
35 // to the state store, which should be handled after all others.
36 void WaitForStateStore(Profile
* profile
, const std::string
& extension_id
) {
37 scoped_refptr
<content::MessageLoopRunner
> runner
=
38 new content::MessageLoopRunner
;
39 ExtensionSystem::Get(profile
)->state_store()->GetExtensionValue(
41 kBrowserActionStorageKey
,
42 base::Bind(&QuitMessageLoop
, runner
));
48 // Setup for the test by loading an extension, which should set the browser
49 // action background to blue.
50 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
,
51 PRE_BrowserActionDefaultPersistence
) {
52 ExtensionTestMessageListener
listener("Background Color Set",
53 false /* won't send custom reply */);
55 const Extension
* extension
=
56 LoadExtension(test_data_dir_
.AppendASCII("api_test")
57 .AppendASCII("browser_action")
58 .AppendASCII("default_persistence"));
59 ASSERT_TRUE(extension
);
60 ASSERT_EQ(kExtensionName
, extension
->name());
61 WaitForStateStore(profile(), extension
->id());
63 // Make sure we've given the extension enough time to set the background color
64 // in chrome.runtime.onInstalled.
65 ASSERT_TRUE(listener
.WaitUntilSatisfied());
67 ExtensionAction
* extension_action
=
68 ExtensionActionManager::Get(profile())->GetBrowserAction(*extension
);
69 ASSERT_TRUE(extension_action
);
70 EXPECT_EQ(SK_ColorBLUE
, extension_action
->GetBadgeBackgroundColor(0));
73 // When Chrome restarts, the Extension will immediately update the browser
74 // action, but will not modify the badge background color. Thus, the background
75 // should remain blue (persisting the default set in onInstalled()).
76 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
, BrowserActionDefaultPersistence
) {
77 // Find the extension (it's a shame we don't have an ID for this, but it
78 // was generated in the last test).
79 const Extension
* extension
= NULL
;
80 const ExtensionSet
& extension_set
=
81 ExtensionRegistry::Get(profile())->enabled_extensions();
82 for (ExtensionSet::const_iterator iter
= extension_set
.begin();
83 iter
!= extension_set
.end();
85 if ((*iter
)->name() == kExtensionName
) {
86 extension
= iter
->get();
90 ASSERT_TRUE(extension
) << "Could not find extension in registry.";
92 ExtensionAction
* extension_action
=
93 ExtensionActionManager::Get(profile())->GetBrowserAction(*extension
);
94 ASSERT_TRUE(extension_action
);
96 // If the extension hasn't already set the badge text, then we should wait for
98 if (extension_action
->GetBadgeText(0) != "Hello") {
99 ExtensionTestMessageListener
listener("Badge Text Set",
100 false /* won't send custom reply */);
101 ASSERT_TRUE(listener
.WaitUntilSatisfied());
104 // If this log becomes frequent, this test is losing its effectiveness, and
105 // we need to find a more invasive way of ensuring the test's StateStore
106 // initializes after extensions get their onStartup event.
107 if (ExtensionSystem::Get(profile())->state_store()->IsInitialized())
108 LOG(WARNING
) << "State store already initialized; test guaranteed to pass.";
110 // Wait for the StateStore to load, and fetch the defaults.
111 WaitForStateStore(profile(), extension
->id());
113 // Ensure the BrowserAction's badge background is still blue.
114 EXPECT_EQ(SK_ColorBLUE
, extension_action
->GetBadgeBackgroundColor(0));
117 } // namespace extensions