Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(501)

Side by Side Diff: chrome/browser/extensions/active_script_controller_browsertest.cc

Issue 270153004: Introduce ActiveScriptController; track active extension scripts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/macros.h"
6 #include "chrome/browser/extensions/active_script_controller.h"
7 #include "chrome/browser/extensions/extension_action.h"
8 #include "chrome/browser/extensions/extension_browsertest.h"
9 #include "chrome/browser/extensions/extension_test_message_listener.h"
10 #include "chrome/browser/extensions/location_bar_controller.h"
11 #include "chrome/browser/extensions/tab_helper.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "extensions/common/feature_switch.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace extensions {
20
21 class ActiveScriptControllerBrowserTest : public ExtensionBrowserTest {
22 public:
23 ActiveScriptControllerBrowserTest()
24 : feature_override_(FeatureSwitch::scripts_require_action(),
25 FeatureSwitch::OVERRIDE_ENABLED) {}
26 private:
27 FeatureSwitch::ScopedOverride feature_override_;
28 };
29
30 class ActiveScriptTester {
31 public:
32 ActiveScriptTester(const std::string& name,
33 const Extension* extension,
34 bool expect_has_action);
35 ~ActiveScriptTester();
36
37 testing::AssertionResult Verify(Browser* browser);
38
39 private:
40 // The name of the extension, and also the message it sends.
41 std::string name_;
42
43 // The extension associated with this tester.
44 const Extension* extension_;
45
46 // Whether or not we expect the extension to have an action for script
47 // injection.
48 bool expect_has_action_;
49
50 // All of these extensions should inject a script (either through content
51 // scripts or through chrome.tabs.executeScript()) that sends a message with
52 // their names (for verification).
53 // Be prepared to wait for each extension to inject the script.
54 linked_ptr<ExtensionTestMessageListener> listener_;
55 };
56
57 ActiveScriptTester::ActiveScriptTester(
58 const std::string& name,
59 const Extension* extension,
60 bool expect_has_action)
61 : name_(name),
62 extension_(extension),
63 expect_has_action_(expect_has_action),
64 listener_(
65 new ExtensionTestMessageListener(name, false /* won't reply */)) {
66 }
67
68 ActiveScriptTester::~ActiveScriptTester() {
69 }
70
71 testing::AssertionResult ActiveScriptTester::Verify(Browser* browser) {
72 if (!extension_)
73 return testing::AssertionFailure() << "Could not load extension: " << name_;
not at google - send to devlin 2014/05/08 23:33:19 what happened to your RETURN_IF_FALSE macro?
Devlin 2014/05/09 00:15:32 I don't wanna just chuck it into a test file. I'l
74
75 listener_->WaitUntilSatisfied();
76 content::WebContents* web_contents =
77 browser ? browser->tab_strip_model()->GetActiveWebContents() : NULL;
78
79 if (!web_contents)
80 return testing::AssertionFailure() << "Could not find web contents.";
81
82 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents);
83 if (!tab_helper)
84 return testing::AssertionFailure() << "Could not find tab helper.";
85
86 ActiveScriptController* controller =
87 tab_helper->location_bar_controller()->active_script_controller();
88 if (!controller)
89 return testing::AssertionFailure() << "Could not find controller.";
90
91 bool has_action = controller->GetActionForExtension(extension_) != NULL;
92 if (expect_has_action_ != has_action) {
93 return testing::AssertionFailure()
94 << "Improper action status: expected " << expect_has_action_
95 << ", found " << has_action;
96 }
97
98 return testing::AssertionSuccess();
99 }
100
101
102 IN_PROC_BROWSER_TEST_F(ActiveScriptControllerBrowserTest,
103 ActiveScriptsAreDisplayed) {
104 base::FilePath active_script_path =
105 test_data_dir_.AppendASCII("active_script");
106
107 const char* kExtensionNames[] = {
108 "content_scripts_all_hosts",
109 "inject_scripts_all_hosts",
110 "content_scripts_explicit_hosts"
111 };
112
113 // First, we load up three extensions:
114 // - An extension with a content script that runs on all hosts,
115 // - An extension that injects scripts into all hosts,
116 // - An extension with a content script that runs on explicit hosts.
117 ActiveScriptTester testers[] = {
118 ActiveScriptTester(
119 kExtensionNames[0],
120 LoadExtension(active_script_path.AppendASCII(kExtensionNames[0])),
121 true /* expect action */),
122 ActiveScriptTester(
123 kExtensionNames[1],
124 LoadExtension(active_script_path.AppendASCII(kExtensionNames[1])),
125 true /* expect action */),
126 ActiveScriptTester(
127 kExtensionNames[2],
128 LoadExtension(active_script_path.AppendASCII(kExtensionNames[2])),
129 false /* expect action */)
130 };
131
132 // Navigate to an URL (which matches the explicit host specified in the
133 // extension content_scripts_explicit_hosts). All three extensions should
134 // inject the script.
135 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
136 ui_test_utils::NavigateToURL(
137 browser(),
138 embedded_test_server()->GetURL("/extensions/test_file.html"));
139
140 for (size_t i = 0u; i < arraysize(testers); ++i)
141 ASSERT_TRUE(testers[i].Verify(browser())) << kExtensionNames[i];
142 }
143
144 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698