OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | |
6 | |
5 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/memory/ref_counted.h" | |
9 #include "base/path_service.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "chrome/browser/extensions/api/debugger/debugger_api.h" | |
12 #include "chrome/browser/extensions/api/debugger/debugger_api_constants.h" | |
6 #include "chrome/browser/extensions/extension_apitest.h" | 13 #include "chrome/browser/extensions/extension_apitest.h" |
14 #include "chrome/browser/extensions/extension_function_test_utils.h" | |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
16 #include "chrome/common/chrome_paths.h" | |
7 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/test/base/ui_test_utils.h" | |
19 #include "extensions/browser/extension_function.h" | |
20 #include "extensions/common/extension.h" | |
21 #include "extensions/common/extension_builder.h" | |
22 #include "extensions/common/switches.h" | |
23 #include "extensions/common/value_builder.h" | |
24 | |
25 namespace extensions { | |
26 | |
27 class DebuggerApiTest : public ExtensionApiTest { | |
28 protected: | |
29 virtual ~DebuggerApiTest() {} | |
30 | |
31 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE; | |
32 virtual void SetUpOnMainThread() OVERRIDE; | |
33 | |
34 // Run the attach function. If |expected_error| is not empty, then the | |
35 // function should fail with the error. Otherwise, the function is expected | |
36 // to succeed. | |
37 testing::AssertionResult RunAttachFunction(const std::string& url, | |
38 const std::string& expected_error); | |
39 | |
40 const Extension* extension() const { return extension_; } | |
41 base::CommandLine* command_line() { return command_line_; } | |
meacer
2014/06/24 18:15:54
command_line() const {..}?
Devlin
2014/06/24 18:45:40
Sure. Didn't originally because it _does_ modify
| |
42 | |
43 private: | |
44 // The command-line for the test process, preserved in order to modify | |
45 // mid-test. | |
46 base::CommandLine* command_line_; | |
47 | |
48 // A basic extension with the debugger permission. | |
49 scoped_refptr<const Extension> extension_; | |
50 }; | |
51 | |
52 void DebuggerApiTest::SetUpCommandLine(base::CommandLine* command_line) { | |
53 ExtensionApiTest::SetUpCommandLine(command_line); | |
54 // We need to hold onto |command_line| in order to modify it during the test. | |
55 command_line_ = command_line; | |
56 } | |
57 | |
58 void DebuggerApiTest::SetUpOnMainThread() { | |
59 ExtensionApiTest::SetUpOnMainThread(); | |
60 extension_ = | |
61 ExtensionBuilder().SetManifest( | |
62 DictionaryBuilder().Set("name", "debugger") | |
63 .Set("version", "0.1") | |
64 .Set("manifest_version", 2) | |
65 .Set("permissions", | |
66 ListBuilder().Append("debugger"))).Build(); | |
67 } | |
68 | |
69 testing::AssertionResult DebuggerApiTest::RunAttachFunction( | |
70 const std::string& url, const std::string& expected_error) { | |
meacer
2014/06/24 18:15:54
nit: Could pass |url| as a GURL.
Devlin
2014/06/24 18:45:40
Yeah, just made the test code look more cluttered
| |
71 ui_test_utils::NavigateToURL(browser(), GURL(url)); | |
72 content::WebContents* web_contents = | |
73 browser()->tab_strip_model()->GetActiveWebContents(); | |
meacer
2014/06/24 18:15:54
Two more spaces
Devlin
2014/06/24 18:45:40
Done.
| |
74 int tab_id = SessionID::IdForTab(web_contents); | |
75 scoped_refptr<DebuggerAttachFunction> attach_function = | |
76 new DebuggerAttachFunction(); | |
77 attach_function->set_extension(extension_); | |
78 std::string args = base::StringPrintf("[{\"tabId\": %d}, \"1.1\"]", tab_id); | |
79 | |
80 if (!expected_error.empty()) { | |
81 std::string actual_error = | |
82 extension_function_test_utils::RunFunctionAndReturnError( | |
83 attach_function, args, browser()); | |
84 if (actual_error != expected_error) { | |
85 return testing::AssertionFailure() << "Did not get correct error: " | |
86 << "expected: " << expected_error << ", found: " << actual_error; | |
87 } | |
88 } else { | |
89 if (!RunFunction(attach_function, | |
90 args, | |
91 browser(), | |
92 extension_function_test_utils::NONE)) { | |
93 return testing::AssertionFailure() << "Could not run function: " | |
94 << attach_function->GetError(); | |
95 } | |
96 | |
97 // Clean up and detach. | |
98 scoped_refptr<DebuggerDetachFunction> detach_function = | |
99 new DebuggerDetachFunction(); | |
100 detach_function->set_extension(extension_); | |
101 if (!RunFunction(detach_function, | |
102 base::StringPrintf("[{\"tabId\": %d}]", tab_id), | |
103 browser(), | |
104 extension_function_test_utils::NONE)) { | |
105 return testing::AssertionFailure() << "Could not detach: " | |
106 << detach_function->GetError(); | |
107 } | |
108 } | |
109 | |
meacer
2014/06/24 18:15:54
nit: remove empty line
Devlin
2014/06/24 18:45:40
Done.
| |
110 return testing::AssertionSuccess(); | |
111 } | |
8 | 112 |
9 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Debugger) { | 113 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Debugger) { |
10 ASSERT_TRUE(RunExtensionTest("debugger")) << message_; | 114 ASSERT_TRUE(RunExtensionTest("debugger")) << message_; |
11 } | 115 } |
116 | |
117 IN_PROC_BROWSER_TEST_F(DebuggerApiTest, | |
118 DebuggerNotAllowedOnOtherExtensionPages) { | |
119 // Load another arbitrary extension with an associated resource (popup.html). | |
120 base::FilePath path; | |
121 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | |
122 path = path.AppendASCII("extensions").AppendASCII("good_unpacked"); | |
123 const Extension* another_extension = LoadExtension(path); | |
124 ASSERT_TRUE(another_extension); | |
125 | |
126 std::string other_ext_url = | |
127 base::StringPrintf("chrome-extension://%s/popup.html", | |
128 another_extension->id().c_str()); | |
129 | |
130 // This extension should not be able to access another extension. | |
131 EXPECT_TRUE(RunAttachFunction( | |
meacer
2014/06/24 18:15:54
nit: It's somewhat unintuitive to get EXPECT_TRUE
Devlin
2014/06/24 18:45:40
It makes the code messier. If we just test for tr
meacer
2014/06/24 18:57:02
Thanks, makes sense. Sounds good as it is.
| |
132 other_ext_url, debugger_api_constants::kAttachToOtherExtensionError)); | |
133 | |
134 // This extension *should* be able to debug itself. | |
135 EXPECT_TRUE(RunAttachFunction( | |
136 base::StringPrintf("chrome-extension://%s/foo.html", | |
137 extension()->id().c_str()), | |
138 std::string())); | |
139 | |
140 // Append extensions on chrome urls switch. The extension should now be able | |
141 // to debug any extension. | |
142 command_line()->AppendSwitch(switches::kExtensionsOnChromeURLs); | |
143 EXPECT_TRUE(RunAttachFunction(other_ext_url, std::string())); | |
144 } | |
145 | |
146 } // namespace extensions | |
OLD | NEW |