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> | 5 #include <string> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 24 matching lines...) Expand all Loading... | |
35 // Run the attach function. If |expected_error| is not empty, then the | 35 // Run the attach function. If |expected_error| is not empty, then the |
36 // function should fail with the error. Otherwise, the function is expected | 36 // function should fail with the error. Otherwise, the function is expected |
37 // to succeed. | 37 // to succeed. |
38 testing::AssertionResult RunAttachFunction(const GURL& url, | 38 testing::AssertionResult RunAttachFunction(const GURL& url, |
39 const std::string& expected_error); | 39 const std::string& expected_error); |
40 | 40 |
41 const Extension* extension() const { return extension_.get(); } | 41 const Extension* extension() const { return extension_.get(); } |
42 base::CommandLine* command_line() const { return command_line_; } | 42 base::CommandLine* command_line() const { return command_line_; } |
43 | 43 |
44 private: | 44 private: |
45 testing::AssertionResult RunAttachFunctionOnTarget( | |
46 const std::string& debuggee_target, const std::string& expected_error); | |
47 | |
45 // The command-line for the test process, preserved in order to modify | 48 // The command-line for the test process, preserved in order to modify |
46 // mid-test. | 49 // mid-test. |
47 base::CommandLine* command_line_; | 50 base::CommandLine* command_line_; |
48 | 51 |
49 // A basic extension with the debugger permission. | 52 // A basic extension with the debugger permission. |
50 scoped_refptr<const Extension> extension_; | 53 scoped_refptr<const Extension> extension_; |
51 }; | 54 }; |
52 | 55 |
53 void DebuggerApiTest::SetUpCommandLine(base::CommandLine* command_line) { | 56 void DebuggerApiTest::SetUpCommandLine(base::CommandLine* command_line) { |
54 ExtensionApiTest::SetUpCommandLine(command_line); | 57 ExtensionApiTest::SetUpCommandLine(command_line); |
(...skipping 10 matching lines...) Expand all Loading... | |
65 .Set("manifest_version", 2) | 68 .Set("manifest_version", 2) |
66 .Set("permissions", | 69 .Set("permissions", |
67 ListBuilder().Append("debugger"))).Build(); | 70 ListBuilder().Append("debugger"))).Build(); |
68 } | 71 } |
69 | 72 |
70 testing::AssertionResult DebuggerApiTest::RunAttachFunction( | 73 testing::AssertionResult DebuggerApiTest::RunAttachFunction( |
71 const GURL& url, const std::string& expected_error) { | 74 const GURL& url, const std::string& expected_error) { |
72 ui_test_utils::NavigateToURL(browser(), url); | 75 ui_test_utils::NavigateToURL(browser(), url); |
73 content::WebContents* web_contents = | 76 content::WebContents* web_contents = |
74 browser()->tab_strip_model()->GetActiveWebContents(); | 77 browser()->tab_strip_model()->GetActiveWebContents(); |
78 | |
79 // Attach by tabId. | |
75 int tab_id = SessionTabHelper::IdForTab(web_contents); | 80 int tab_id = SessionTabHelper::IdForTab(web_contents); |
81 std::string debugee_by_tab = base::StringPrintf("{\"tabId\": %d}", tab_id); | |
82 testing::AssertionResult result = | |
83 RunAttachFunctionOnTarget(debugee_by_tab, expected_error); | |
84 if (!result) | |
85 return result; | |
86 | |
87 // Attach by targetId. | |
88 scoped_refptr<DebuggerGetTargetsFunction> get_targets_function = | |
89 new DebuggerGetTargetsFunction(); | |
90 scoped_ptr<base::Value> value( | |
91 extension_function_test_utils::RunFunctionAndReturnSingleResult( | |
92 get_targets_function.get(), "[]", browser())); | |
93 base::ListValue* targets = NULL; | |
Devlin
2015/02/10 17:14:42
nit: nullptr (also line 98)
robwu
2015/02/10 22:35:43
Done.
| |
94 EXPECT_TRUE(value->GetAsList(&targets)); | |
95 | |
96 std::string debugger_target_id; | |
97 for (size_t i = 0; i < targets->GetSize(); ++i) { | |
98 base::DictionaryValue* target_dict = NULL; | |
99 EXPECT_TRUE(targets->GetDictionary(i, &target_dict)); | |
100 int id; | |
Devlin
2015/02/10 17:14:42
nit: even though it shouldn't matter, please initi
robwu
2015/02/10 22:35:43
Done.
| |
101 if (target_dict->GetInteger("tabId", &id) && id == tab_id) { | |
102 EXPECT_TRUE(target_dict->GetString("id", &debugger_target_id)); | |
103 break; | |
104 } | |
105 } | |
106 EXPECT_TRUE(!debugger_target_id.empty()); | |
107 | |
108 std::string debugee_by_target_id = | |
109 base::StringPrintf("{\"targetId\": \"%s\"}", debugger_target_id.c_str()); | |
110 result = RunAttachFunctionOnTarget(debugee_by_target_id, expected_error); | |
Devlin
2015/02/10 17:14:42
nit: just return RunAttach... here.
robwu
2015/02/10 22:35:43
Done.
| |
111 | |
112 return result; | |
113 } | |
114 | |
115 testing::AssertionResult DebuggerApiTest::RunAttachFunctionOnTarget( | |
116 const std::string& debuggee_target, const std::string& expected_error) { | |
76 scoped_refptr<DebuggerAttachFunction> attach_function = | 117 scoped_refptr<DebuggerAttachFunction> attach_function = |
77 new DebuggerAttachFunction(); | 118 new DebuggerAttachFunction(); |
78 attach_function->set_extension(extension_.get()); | 119 attach_function->set_extension(extension_.get()); |
79 std::string args = base::StringPrintf("[{\"tabId\": %d}, \"1.1\"]", tab_id); | |
80 | 120 |
81 if (!expected_error.empty()) { | 121 std::string actual_error; |
82 std::string actual_error = | 122 if (!RunFunction(attach_function.get(), |
83 extension_function_test_utils::RunFunctionAndReturnError( | 123 base::StringPrintf("[%s, \"1.1\"]", debuggee_target.c_str()), |
84 attach_function.get(), args, browser()); | 124 browser(), |
85 if (actual_error != expected_error) { | 125 extension_function_test_utils::NONE)) { |
86 return testing::AssertionFailure() << "Did not get correct error: " | 126 actual_error = attach_function->GetError(); |
87 << "expected: " << expected_error << ", found: " << actual_error; | |
88 } | |
89 } else { | 127 } else { |
90 if (!RunFunction(attach_function.get(), | |
91 args, | |
92 browser(), | |
93 extension_function_test_utils::NONE)) { | |
94 return testing::AssertionFailure() << "Could not run function: " | |
95 << attach_function->GetError(); | |
96 } | |
97 | |
98 // Clean up and detach. | 128 // Clean up and detach. |
99 scoped_refptr<DebuggerDetachFunction> detach_function = | 129 scoped_refptr<DebuggerDetachFunction> detach_function = |
100 new DebuggerDetachFunction(); | 130 new DebuggerDetachFunction(); |
101 detach_function->set_extension(extension_.get()); | 131 detach_function->set_extension(extension_.get()); |
102 if (!RunFunction(detach_function.get(), | 132 if (!RunFunction(detach_function.get(), |
103 base::StringPrintf("[{\"tabId\": %d}]", tab_id), | 133 base::StringPrintf("[%s]", debuggee_target.c_str()), |
104 browser(), | 134 browser(), |
105 extension_function_test_utils::NONE)) { | 135 extension_function_test_utils::NONE)) { |
106 return testing::AssertionFailure() << "Could not detach: " | 136 return testing::AssertionFailure() << "Could not detach from " |
107 << detach_function->GetError(); | 137 << debuggee_target << " : " << detach_function->GetError(); |
108 } | 138 } |
109 } | 139 } |
140 | |
141 if (expected_error.empty() && !actual_error.empty()) { | |
142 return testing::AssertionFailure() << "Could not attach to " | |
143 << debuggee_target << " : " << actual_error; | |
144 } else if (actual_error != expected_error) { | |
145 return testing::AssertionFailure() << "Did not get correct error upon " | |
146 << "attach to " << debuggee_target << " : " | |
147 << "expected: " << expected_error << ", found: " << actual_error; | |
148 } | |
110 return testing::AssertionSuccess(); | 149 return testing::AssertionSuccess(); |
111 } | 150 } |
112 | 151 |
113 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Debugger) { | 152 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Debugger) { |
114 ASSERT_TRUE(RunExtensionTest("debugger")) << message_; | 153 ASSERT_TRUE(RunExtensionTest("debugger")) << message_; |
115 } | 154 } |
116 | 155 |
117 IN_PROC_BROWSER_TEST_F(DebuggerApiTest, | 156 IN_PROC_BROWSER_TEST_F(DebuggerApiTest, |
118 DebuggerNotAllowedOnOtherExtensionPages) { | 157 DebuggerNotAllowedOnOtherExtensionPages) { |
119 // Load another arbitrary extension with an associated resource (popup.html). | 158 // Load another arbitrary extension with an associated resource (popup.html). |
(...skipping 17 matching lines...) Expand all Loading... | |
137 extension()->id().c_str())), | 176 extension()->id().c_str())), |
138 std::string())); | 177 std::string())); |
139 | 178 |
140 // Append extensions on chrome urls switch. The extension should now be able | 179 // Append extensions on chrome urls switch. The extension should now be able |
141 // to debug any extension. | 180 // to debug any extension. |
142 command_line()->AppendSwitch(switches::kExtensionsOnChromeURLs); | 181 command_line()->AppendSwitch(switches::kExtensionsOnChromeURLs); |
143 EXPECT_TRUE(RunAttachFunction(other_ext_url, std::string())); | 182 EXPECT_TRUE(RunAttachFunction(other_ext_url, std::string())); |
144 } | 183 } |
145 | 184 |
146 } // namespace extensions | 185 } // namespace extensions |
OLD | NEW |