OLD | NEW |
---|---|
(Empty) | |
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 "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 "chrome/browser/extensions/state_store.h" | |
11 #include "content/public/test/test_utils.h" | |
12 #include "extensions/browser/extension_registry.h" | |
13 #include "extensions/browser/extension_system.h" | |
14 #include "extensions/common/extension.h" | |
15 #include "third_party/skia/include/core/SkColor.h" | |
16 | |
17 namespace extensions { | |
18 | |
19 namespace { | |
20 | |
21 // A key into the StateStore; we don't use any results, but need to know when | |
22 // it's initialized. | |
23 const char kBrowserActionStorageKey[] = "browser_action"; | |
24 // The name of the extension we add. | |
25 const char kExtensionName[] = "Default Persistence Test Extension"; | |
26 | |
27 void QuitMessageLoop(content::MessageLoopRunner* runner, | |
28 scoped_ptr<base::Value> value) { | |
29 runner->Quit(); | |
30 } | |
31 | |
32 // We need to wait for the state store to initialize and respond to requests | |
33 // so we can see if the preferences persist. Do this by posting our own request | |
34 // to the state store, which should be handled after all others. | |
35 void WaitForStateStore(Profile* profile, const std::string& extension_id) { | |
36 scoped_refptr<content::MessageLoopRunner> runner = | |
37 new content::MessageLoopRunner; | |
38 ExtensionSystem::Get(profile)->state_store()->GetExtensionValue( | |
39 extension_id, | |
40 kBrowserActionStorageKey, | |
41 base::Bind(&QuitMessageLoop, runner)); | |
42 runner->Run(); | |
43 } | |
44 | |
45 } // namespace | |
46 | |
47 // Setup for the test by loading an extension, which should set the browser | |
48 // action background to blue. | |
49 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, | |
50 PRE_BrowserActionDefaultPersistence) { | |
51 const Extension* extension = | |
52 LoadExtension(test_data_dir_.AppendASCII("api_test") | |
53 .AppendASCII("browser_action") | |
54 .AppendASCII("default_persistence")); | |
55 ASSERT_TRUE(extension); | |
56 ASSERT_EQ(kExtensionName, extension->name()); | |
57 WaitForStateStore(profile(), extension->id()); | |
58 | |
59 ExtensionAction* extension_action = | |
60 ExtensionActionManager::Get(profile())->GetBrowserAction(*extension); | |
61 ASSERT_TRUE(extension_action); | |
62 EXPECT_EQ(SK_ColorBLUE, extension_action->GetBadgeBackgroundColor(0)); | |
63 } | |
64 | |
65 // When Chrome restarts, the Extension will immediately update the browser | |
66 // action, but will not modify the badge background color. Thus, the background | |
67 // should remain blue (persisting the default set in onInstalled()). | |
68 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, BrowserActionDefaultPersistence) { | |
69 // Find the extension (it's a shame we don't have an ID for this, but it | |
70 // was generated in the last test). | |
71 const Extension* extension = NULL; | |
72 const ExtensionSet& extension_set = | |
73 ExtensionRegistry::Get(profile())->enabled_extensions(); | |
74 for (ExtensionSet::const_iterator iter = extension_set.begin(); | |
75 iter != extension_set.end(); | |
76 ++iter) { | |
77 if ((*iter)->name() == kExtensionName) { | |
78 extension = *iter; | |
79 break; | |
80 } | |
81 } | |
82 ASSERT_TRUE(extension) << "Could not find extension in registry."; | |
83 | |
84 // This is *not* a noisy or usless log. If the state store has already been | |
Jeffrey Yasskin
2014/03/05 20:23:33
sp: usless
It'll probably be more effective to sa
Devlin
2014/03/05 23:04:31
Done.
| |
85 // initialized, this test is guaranteed to pass, and we should re-evaluate | |
86 // behavior if this becomes frequent. Since there is no good way of ensuring | |
87 // the StateStore has not initialized 100% of the time, we cannot ASSERT that | |
88 // it is initialized. | |
89 if (ExtensionSystem::Get(profile())->state_store()->IsInitialized()) | |
90 LOG(WARNING) << "State store already initialized; test guaranteed to pass."; | |
91 | |
92 // Wait for the StateStore to load, and fetch the defaults. | |
93 WaitForStateStore(profile(), extension->id()); | |
94 | |
95 // Ensure the BrowserAction's badge background is still blue. | |
96 ExtensionAction* extension_action = | |
97 ExtensionActionManager::Get(profile())->GetBrowserAction(*extension); | |
98 ASSERT_TRUE(extension_action); | |
99 EXPECT_EQ(SK_ColorBLUE, extension_action->GetBadgeBackgroundColor(0)); | |
100 } | |
101 | |
102 } // namespace extensions | |
OLD | NEW |