Bug 460546 - nsBrowserGlue should use a smart getter for the pref service. r=gavin
[wine-gecko.git] / extensions / metrics / test / TestUICommandCollector.cpp
blob3b3d018bcd346ac2803e1a8d2054329e4e4d4b23
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is the Metrics extension.
18 * The Initial Developer of the Original Code is Google Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Brian Ryner <bryner@gmail.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 // Unit test for nsUICommandCollector
41 #include <stdio.h>
43 #include "TestCommon.h"
44 #include "nsXPCOM.h"
45 #include "nsCOMPtr.h"
46 #include "nsAutoPtr.h"
47 #include "nsIDOMDocument.h"
48 #include "nsIDOMDocumentEvent.h"
49 #include "nsIDOMElement.h"
50 #include "nsIDOMEvent.h"
51 #include "nsIDOMEventTarget.h"
52 #include "nsIDOMText.h"
53 #include "nsIDOMXULCommandEvent.h"
54 #include "nsIPrivateDOMEvent.h"
55 #include "nsComponentManagerUtils.h"
56 #include "nsUICommandCollector.h"
57 #include "nsMetricsService.h"
59 // This is a little unconventional, but it lets us register the metrics
60 // components statically so that they can initialize properly in the test
61 // environment, while still allowing us access to non-interface methods.
62 #include "nsMetricsModule.cpp"
64 static int gTotalTests = 0;
65 static int gPassedTests = 0;
67 class UICommandCollectorTest
69 public:
70 UICommandCollectorTest() {}
71 ~UICommandCollectorTest() {}
73 void SetUp();
74 void TestGetEventTargets();
75 void TestGetEventKeyId();
77 private:
78 nsRefPtr<nsUICommandCollector> mCollector;
79 nsCOMPtr<nsIDOMDocument> mDocument;
80 nsCOMPtr<nsIPrivateDOMEvent> mDOMEvent;
81 nsString kXULNS;
84 void UICommandCollectorTest::SetUp()
86 ++gTotalTests;
87 mCollector = new nsUICommandCollector();
88 mDocument = do_CreateInstance("@mozilla.org/xml/xml-document;1");
89 kXULNS.Assign(NS_LITERAL_STRING("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"));
92 void UICommandCollectorTest::TestGetEventTargets()
94 nsCOMPtr<nsIDOMDocumentEvent> docEvent = do_QueryInterface(mDocument);
95 nsCOMPtr<nsIDOMEvent> event;
96 docEvent->CreateEvent(NS_LITERAL_STRING("xulcommandevents"),
97 getter_AddRefs(event));
99 // The private event interface lets us directly set the target
100 // and original target for easier testing.
101 nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
102 ASSERT_TRUE(privateEvent);
104 nsCOMPtr<nsIDOMElement> buttonElement;
105 mDocument->CreateElementNS(kXULNS, NS_LITERAL_STRING("button"),
106 getter_AddRefs(buttonElement));
107 ASSERT_TRUE(buttonElement);
108 buttonElement->SetAttribute(NS_LITERAL_STRING("id"),
109 NS_LITERAL_STRING("btn1"));
111 // First test the simple case where we have only explicit content.
112 // Note that originalTarget == target unless set otherwise.
113 privateEvent->SetTarget(
114 nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(buttonElement)));
116 nsString targetId, targetAnonId;
117 nsresult rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
118 ASSERT_TRUE(NS_SUCCEEDED(rv));
119 ASSERT_TRUE(targetId.Equals(NS_LITERAL_STRING("btn1")));
120 ASSERT_TRUE(targetAnonId.IsEmpty());
122 // If there was an anonid, it should be ignored
123 buttonElement->SetAttribute(NS_LITERAL_STRING("anonid"),
124 NS_LITERAL_STRING("abc"));
125 rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
126 ASSERT_TRUE(NS_SUCCEEDED(rv));
127 ASSERT_TRUE(targetId.Equals(NS_LITERAL_STRING("btn1")));
128 ASSERT_TRUE(targetAnonId.IsEmpty());
130 // If the target has no id, GetEventTargets should fail
131 buttonElement->RemoveAttribute(NS_LITERAL_STRING("id"));
132 rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
133 ASSERT_TRUE(NS_FAILED(rv));
135 // The same should be true with no anonid
136 buttonElement->RemoveAttribute(NS_LITERAL_STRING("anonid"));
137 rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
138 ASSERT_TRUE(NS_FAILED(rv));
140 // Empty attributes should work the same as no attributes
141 buttonElement->SetAttribute(NS_LITERAL_STRING("id"), nsString());
142 rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
143 ASSERT_TRUE(NS_FAILED(rv));
145 // Now test some cases where the originalTarget is different
146 nsCOMPtr<nsIDOMElement> anonChild;
147 mDocument->CreateElementNS(kXULNS, NS_LITERAL_STRING("hbox"),
148 getter_AddRefs(anonChild));
149 ASSERT_TRUE(anonChild);
151 privateEvent->SetOriginalTarget(
152 nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(anonChild)));
154 // This is the typical id/anonid case for anonymous content
155 buttonElement->SetAttribute(NS_LITERAL_STRING("id"),
156 NS_LITERAL_STRING("btn1"));
157 anonChild->SetAttribute(NS_LITERAL_STRING("anonid"),
158 NS_LITERAL_STRING("box1"));
159 targetId.SetLength(0);
160 targetAnonId.SetLength(0);
161 rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
162 ASSERT_TRUE(NS_SUCCEEDED(rv));
163 ASSERT_TRUE(targetId.Equals(NS_LITERAL_STRING("btn1")));
164 ASSERT_TRUE(targetAnonId.Equals(NS_LITERAL_STRING("box1")));
166 // If there is no id or anonid, it should fail
167 anonChild->RemoveAttribute(NS_LITERAL_STRING("anonid"));
168 rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
169 ASSERT_TRUE(NS_FAILED(rv));
171 // Test the failure case where the target/originalTarget is not an element
172 privateEvent->SetOriginalTarget(nsnull); // now mirrors target again
173 nsCOMPtr<nsIDOMText> textNode;
174 mDocument->CreateTextNode(NS_LITERAL_STRING("blah"),
175 getter_AddRefs(textNode));
176 ASSERT_TRUE(textNode);
177 privateEvent->SetTarget(
178 nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(textNode)));
179 rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
180 ASSERT_TRUE(NS_FAILED(rv));
182 ++gPassedTests;
185 void UICommandCollectorTest::TestGetEventKeyId()
187 nsCOMPtr<nsIDOMDocumentEvent> docEvent = do_QueryInterface(mDocument);
188 nsCOMPtr<nsIDOMEvent> event;
189 docEvent->CreateEvent(NS_LITERAL_STRING("xulcommandevents"),
190 getter_AddRefs(event));
192 // The private event interface lets us directly set the target
193 // and original target for easier testing.
194 nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
195 ASSERT_TRUE(privateEvent);
197 nsCOMPtr<nsIDOMElement> elem;
198 mDocument->CreateElementNS(kXULNS, NS_LITERAL_STRING("hbox"),
199 getter_AddRefs(elem));
200 ASSERT_TRUE(elem);
202 privateEvent->SetTarget(
203 nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(elem)));
205 // In its initial state, the command event will have no source event.
206 // GetEventKeyId should leave keyId empty.
207 nsString keyId;
208 mCollector->GetEventKeyId(event, keyId);
209 ASSERT_TRUE(keyId.IsEmpty());
211 // Now set up a source event
212 nsCOMPtr<nsIDOMEvent> sourceEvent;
213 docEvent->CreateEvent(NS_LITERAL_STRING("Events"),
214 getter_AddRefs(sourceEvent));
215 nsCOMPtr<nsIPrivateDOMEvent> privateSource = do_QueryInterface(sourceEvent);
216 ASSERT_TRUE(privateSource);
218 nsCOMPtr<nsIDOMXULCommandEvent> xcEvent = do_QueryInterface(event);
219 ASSERT_TRUE(xcEvent);
220 nsresult rv = xcEvent->InitCommandEvent(NS_LITERAL_STRING("command"),
221 PR_TRUE, PR_TRUE, nsnull, 0,
222 PR_FALSE, PR_FALSE, PR_FALSE,
223 PR_FALSE, sourceEvent);
224 ASSERT_TRUE(NS_SUCCEEDED(rv));
226 // The source event will initially point to a non-key element
227 privateSource->SetTarget(
228 nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(elem)));
230 mCollector->GetEventKeyId(event, keyId);
231 ASSERT_TRUE(keyId.IsEmpty());
233 // Create a key element and point the source event there
234 nsCOMPtr<nsIDOMElement> keyElem;
235 mDocument->CreateElementNS(kXULNS, NS_LITERAL_STRING("key"),
236 getter_AddRefs(keyElem));
237 ASSERT_TRUE(keyElem);
238 keyElem->SetAttribute(NS_LITERAL_STRING("id"), NS_LITERAL_STRING("keyFoo"));
239 privateSource->SetTarget(
240 nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(keyElem)));
242 mCollector->GetEventKeyId(event, keyId);
243 ASSERT_TRUE(keyId.Equals(NS_LITERAL_STRING("keyFoo")));
245 // Make sure we don't crash if the source event target is a non-element
246 nsCOMPtr<nsIDOMText> textNode;
247 mDocument->CreateTextNode(NS_LITERAL_STRING("blah"),
248 getter_AddRefs(textNode));
249 ASSERT_TRUE(textNode);
250 privateSource->SetTarget(
251 nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(textNode)));
252 keyId.SetLength(0);
253 mCollector->GetEventKeyId(event, keyId);
254 ASSERT_TRUE(keyId.IsEmpty());
256 ++gPassedTests;
259 int main(int argc, char *argv[])
261 nsStaticModuleInfo staticComps = { "metrics", &NSGetModule };
262 NS_InitXPCOM3(nsnull, nsnull, nsnull, &staticComps, 1);
263 // Pre-initialize the metrics service since it's assumed to exist
264 nsMetricsService::get();
266 // Use a separate instantiation of the test objects for each test
268 UICommandCollectorTest test;
269 test.SetUp();
270 test.TestGetEventTargets();
273 UICommandCollectorTest test;
274 test.SetUp();
275 test.TestGetEventKeyId();
278 NS_ShutdownXPCOM(nsnull);
280 printf("%d/%d tests passed\n", gPassedTests, gTotalTests);
281 return 0;