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 19 matching lines...) Expand all Loading... | |
30 ~DebuggerApiTest() override {} | 30 ~DebuggerApiTest() override {} |
31 | 31 |
32 void SetUpCommandLine(base::CommandLine* command_line) override; | 32 void SetUpCommandLine(base::CommandLine* command_line) override; |
33 void SetUpOnMainThread() override; | 33 void SetUpOnMainThread() override; |
34 | 34 |
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 testing::AssertionResult RunAttachFunctionImpl( | |
Devlin
2015/02/09 23:11:22
Two things:
- Despite what you see around the code
robwu
2015/02/10 12:56:31
Done.
| |
41 const std::string& debuggee_target, const std::string& expected_error); | |
40 | 42 |
41 const Extension* extension() const { return extension_.get(); } | 43 const Extension* extension() const { return extension_.get(); } |
42 base::CommandLine* command_line() const { return command_line_; } | 44 base::CommandLine* command_line() const { return command_line_; } |
43 | 45 |
44 private: | 46 private: |
45 // The command-line for the test process, preserved in order to modify | 47 // The command-line for the test process, preserved in order to modify |
46 // mid-test. | 48 // mid-test. |
47 base::CommandLine* command_line_; | 49 base::CommandLine* command_line_; |
48 | 50 |
49 // A basic extension with the debugger permission. | 51 // A basic extension with the debugger permission. |
(...skipping 15 matching lines...) Expand all Loading... | |
65 .Set("manifest_version", 2) | 67 .Set("manifest_version", 2) |
66 .Set("permissions", | 68 .Set("permissions", |
67 ListBuilder().Append("debugger"))).Build(); | 69 ListBuilder().Append("debugger"))).Build(); |
68 } | 70 } |
69 | 71 |
70 testing::AssertionResult DebuggerApiTest::RunAttachFunction( | 72 testing::AssertionResult DebuggerApiTest::RunAttachFunction( |
71 const GURL& url, const std::string& expected_error) { | 73 const GURL& url, const std::string& expected_error) { |
72 ui_test_utils::NavigateToURL(browser(), url); | 74 ui_test_utils::NavigateToURL(browser(), url); |
73 content::WebContents* web_contents = | 75 content::WebContents* web_contents = |
74 browser()->tab_strip_model()->GetActiveWebContents(); | 76 browser()->tab_strip_model()->GetActiveWebContents(); |
77 | |
78 // Attach by tabId | |
Devlin
2015/02/09 23:11:22
nit: comments should usually end in a period (also
robwu
2015/02/10 12:56:31
Done.
| |
75 int tab_id = SessionTabHelper::IdForTab(web_contents); | 79 int tab_id = SessionTabHelper::IdForTab(web_contents); |
80 std::string debugee_by_tab = base::StringPrintf("{\"tabId\": %d}", tab_id); | |
81 testing::AssertionResult result = | |
82 RunAttachFunctionImpl(debugee_by_tab, expected_error); | |
83 if (!result) | |
84 return result; | |
85 | |
86 // Attach by targetId | |
87 scoped_refptr<DebuggerGetTargetsFunction> get_targets_function = | |
88 new DebuggerGetTargetsFunction(); | |
89 scoped_ptr<base::Value> value( | |
90 extension_function_test_utils::RunFunctionAndReturnSingleResult( | |
91 get_targets_function.get(), "[]", browser())); | |
92 base::ListValue* targets = NULL; | |
93 EXPECT_TRUE(value->GetAsList(&targets)); | |
94 | |
95 std::string debugger_target_id; | |
96 for (size_t i = 0; i < targets->GetSize(); ++i) { | |
97 base::DictionaryValue* target_dict = NULL; | |
98 EXPECT_TRUE(targets->GetDictionary(i, &target_dict)); | |
99 int id; | |
100 if (target_dict->GetInteger("tabId", &id) && id == tab_id) { | |
101 EXPECT_TRUE(target_dict->GetString("id", &debugger_target_id)); | |
102 break; | |
103 } | |
104 } | |
105 EXPECT_TRUE(!debugger_target_id.empty()); | |
106 | |
107 std::string debugee_by_target_id = | |
108 base::StringPrintf("{\"targetId\": \"%s\"}", debugger_target_id.c_str()); | |
109 result = RunAttachFunctionImpl(debugee_by_target_id, expected_error); | |
110 | |
111 return result; | |
112 } | |
113 | |
114 testing::AssertionResult DebuggerApiTest::RunAttachFunctionImpl( | |
115 const std::string& debuggee_target, const std::string& expected_error) { | |
76 scoped_refptr<DebuggerAttachFunction> attach_function = | 116 scoped_refptr<DebuggerAttachFunction> attach_function = |
77 new DebuggerAttachFunction(); | 117 new DebuggerAttachFunction(); |
78 attach_function->set_extension(extension_.get()); | 118 attach_function->set_extension(extension_.get()); |
79 std::string args = base::StringPrintf("[{\"tabId\": %d}, \"1.1\"]", tab_id); | |
80 | 119 |
81 if (!expected_error.empty()) { | 120 std::string actual_error; |
82 std::string actual_error = | 121 if (!RunFunction(attach_function.get(), |
83 extension_function_test_utils::RunFunctionAndReturnError( | 122 base::StringPrintf("[%s, \"1.1\"]", debuggee_target.c_str()), |
84 attach_function.get(), args, browser()); | 123 browser(), |
85 if (actual_error != expected_error) { | 124 extension_function_test_utils::NONE)) { |
86 return testing::AssertionFailure() << "Did not get correct error: " | 125 actual_error = attach_function->GetError(); |
87 << "expected: " << expected_error << ", found: " << actual_error; | |
88 } | |
89 } else { | 126 } 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. | 127 // Clean up and detach. |
99 scoped_refptr<DebuggerDetachFunction> detach_function = | 128 scoped_refptr<DebuggerDetachFunction> detach_function = |
100 new DebuggerDetachFunction(); | 129 new DebuggerDetachFunction(); |
101 detach_function->set_extension(extension_.get()); | 130 detach_function->set_extension(extension_.get()); |
102 if (!RunFunction(detach_function.get(), | 131 if (!RunFunction(detach_function.get(), |
103 base::StringPrintf("[{\"tabId\": %d}]", tab_id), | 132 base::StringPrintf("[%s]", debuggee_target.c_str()), |
104 browser(), | 133 browser(), |
105 extension_function_test_utils::NONE)) { | 134 extension_function_test_utils::NONE)) { |
106 return testing::AssertionFailure() << "Could not detach: " | 135 return testing::AssertionFailure() << "Could not detach from " |
107 << detach_function->GetError(); | 136 << debuggee_target << " : " << detach_function->GetError(); |
137 } | |
138 } | |
139 | |
140 if (expected_error.empty()) { | |
Devlin
2015/02/09 23:11:22
We can simplify these ifs to be
if (expected_error
robwu
2015/02/10 12:56:31
Done.
| |
141 if (!actual_error.empty()) { | |
142 return testing::AssertionFailure() << "Could not attach to " | |
143 << debuggee_target << " : " << actual_error; | |
144 } | |
145 } else { | |
146 if (actual_error != expected_error) { | |
147 return testing::AssertionFailure() << "Did not get correct error upon " | |
148 << "attach to " << debuggee_target << " : " | |
149 << "expected: " << expected_error << ", found: " << actual_error; | |
108 } | 150 } |
109 } | 151 } |
110 return testing::AssertionSuccess(); | 152 return testing::AssertionSuccess(); |
111 } | 153 } |
112 | 154 |
113 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Debugger) { | 155 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Debugger) { |
114 ASSERT_TRUE(RunExtensionTest("debugger")) << message_; | 156 ASSERT_TRUE(RunExtensionTest("debugger")) << message_; |
115 } | 157 } |
116 | 158 |
117 IN_PROC_BROWSER_TEST_F(DebuggerApiTest, | 159 IN_PROC_BROWSER_TEST_F(DebuggerApiTest, |
(...skipping 19 matching lines...) Expand all Loading... | |
137 extension()->id().c_str())), | 179 extension()->id().c_str())), |
138 std::string())); | 180 std::string())); |
139 | 181 |
140 // Append extensions on chrome urls switch. The extension should now be able | 182 // Append extensions on chrome urls switch. The extension should now be able |
141 // to debug any extension. | 183 // to debug any extension. |
142 command_line()->AppendSwitch(switches::kExtensionsOnChromeURLs); | 184 command_line()->AppendSwitch(switches::kExtensionsOnChromeURLs); |
143 EXPECT_TRUE(RunAttachFunction(other_ext_url, std::string())); | 185 EXPECT_TRUE(RunAttachFunction(other_ext_url, std::string())); |
144 } | 186 } |
145 | 187 |
146 } // namespace extensions | 188 } // namespace extensions |
OLD | NEW |