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

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(const std::string& name,
58 const Extension* extension,
59 bool expect_has_action)
60 : name_(name),
61 extension_(extension),
62 expect_has_action_(expect_has_action),
63 listener_(
64 new ExtensionTestMessageListener(name, false /* won't reply */)) {
65 }
66
67 ActiveScriptTester::~ActiveScriptTester() {
68 }
69
70 testing::AssertionResult ActiveScriptTester::Verify(Browser* browser) {
71 if (!extension_)
72 return testing::AssertionFailure() << "Could not load extension: " << name_;
73
74 listener_->WaitUntilSatisfied();
75 content::WebContents* web_contents =
76 browser ? browser->tab_strip_model()->GetActiveWebContents() : NULL;
77
78 if (!web_contents)
79 return testing::AssertionFailure() << "Could not find web contents.";
80
81 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents);
82 if (!tab_helper)
83 return testing::AssertionFailure() << "Could not find tab helper.";
84
85 ActiveScriptController* controller =
86 tab_helper->location_bar_controller()->active_script_controller();
87 if (!controller)
88 return testing::AssertionFailure() << "Could not find controller.";
89
90 bool has_action = controller->GetActionForExtension(extension_) != NULL;
91 if (expect_has_action_ != has_action) {
92 return testing::AssertionFailure()
93 << "Improper action status: expected " << expect_has_action_
94 << ", found " << has_action;
95 }
96
97 return testing::AssertionSuccess();
98 }
99
100 IN_PROC_BROWSER_TEST_F(ActiveScriptControllerBrowserTest,
101 ActiveScriptsAreDisplayed) {
102 base::FilePath active_script_path =
103 test_data_dir_.AppendASCII("active_script");
104
105 const char* kExtensionNames[] = {
106 "content_scripts_all_hosts",
107 "inject_scripts_all_hosts",
108 "content_scripts_explicit_hosts"
109 };
110
111 // First, we load up three extensions:
112 // - An extension with a content script that runs on all hosts,
113 // - An extension that injects scripts into all hosts,
114 // - An extension with a content script that runs on explicit hosts.
115 ActiveScriptTester testers[] = {
116 ActiveScriptTester(
117 kExtensionNames[0],
118 LoadExtension(active_script_path.AppendASCII(kExtensionNames[0])),
119 true /* expect action */),
120 ActiveScriptTester(
121 kExtensionNames[1],
122 LoadExtension(active_script_path.AppendASCII(kExtensionNames[1])),
123 true /* expect action */),
124 ActiveScriptTester(
125 kExtensionNames[2],
126 LoadExtension(active_script_path.AppendASCII(kExtensionNames[2])),
127 false /* expect action */)
128 };
129
130 // Navigate to an URL (which matches the explicit host specified in the
131 // extension content_scripts_explicit_hosts). All three extensions should
132 // inject the script.
133 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
134 ui_test_utils::NavigateToURL(
135 browser(), embedded_test_server()->GetURL("/extensions/test_file.html"));
136
137 for (size_t i = 0u; i < arraysize(testers); ++i)
138 ASSERT_TRUE(testers[i].Verify(browser())) << kExtensionNames[i];
139 }
140
141 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/active_script_controller.cc ('k') | chrome/browser/extensions/activity_log/uma_policy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698