Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Attributes
UICommandCollectorTest Class Reference
Collaboration diagram for UICommandCollectorTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 UICommandCollectorTest ()
 ~UICommandCollectorTest ()
void SetUp ()
void TestGetEventTargets ()
void TestGetEventKeyId ()

Private Attributes

nsRefPtr< nsUICommandCollectormCollector
nsCOMPtr< nsIDOMDocumentmDocument
nsCOMPtr< nsIPrivateDOMEventmDOMEvent
nsString kXULNS

Detailed Description

Definition at line 67 of file TestUICommandCollector.cpp.


Constructor & Destructor Documentation

Definition at line 70 of file TestUICommandCollector.cpp.

{}

Definition at line 71 of file TestUICommandCollector.cpp.

{}

Member Function Documentation

Definition at line 84 of file TestUICommandCollector.cpp.

{
  ++gTotalTests;
  mCollector = new nsUICommandCollector();
  mDocument = do_CreateInstance("@mozilla.org/xml/xml-document;1");
  kXULNS.Assign(NS_LITERAL_STRING("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"));
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 185 of file TestUICommandCollector.cpp.

{
  nsCOMPtr<nsIDOMDocumentEvent> docEvent = do_QueryInterface(mDocument);
  nsCOMPtr<nsIDOMEvent> event;
  docEvent->CreateEvent(NS_LITERAL_STRING("xulcommandevents"),
                        getter_AddRefs(event));

  // The private event interface lets us directly set the target
  // and original target for easier testing.
  nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
  ASSERT_TRUE(privateEvent);

  nsCOMPtr<nsIDOMElement> elem;
  mDocument->CreateElementNS(kXULNS, NS_LITERAL_STRING("hbox"),
                             getter_AddRefs(elem));
  ASSERT_TRUE(elem);

  privateEvent->SetTarget(
      nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(elem)));

  // In its initial state, the command event will have no source event.
  // GetEventKeyId should leave keyId empty.
  nsString keyId;
  mCollector->GetEventKeyId(event, keyId);
  ASSERT_TRUE(keyId.IsEmpty());

  // Now set up a source event
  nsCOMPtr<nsIDOMEvent> sourceEvent;
  docEvent->CreateEvent(NS_LITERAL_STRING("Events"),
                        getter_AddRefs(sourceEvent));
  nsCOMPtr<nsIPrivateDOMEvent> privateSource = do_QueryInterface(sourceEvent);
  ASSERT_TRUE(privateSource);

  nsCOMPtr<nsIDOMXULCommandEvent> xcEvent = do_QueryInterface(event);
  ASSERT_TRUE(xcEvent);
  nsresult rv = xcEvent->InitCommandEvent(NS_LITERAL_STRING("command"),
                                          PR_TRUE, PR_TRUE, nsnull, 0,
                                          PR_FALSE, PR_FALSE, PR_FALSE,
                                          PR_FALSE, sourceEvent);
  ASSERT_TRUE(NS_SUCCEEDED(rv));

  // The source event will initially point to a non-key element
  privateSource->SetTarget(
      nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(elem)));

  mCollector->GetEventKeyId(event, keyId);
  ASSERT_TRUE(keyId.IsEmpty());

  // Create a key element and point the source event there
  nsCOMPtr<nsIDOMElement> keyElem;
  mDocument->CreateElementNS(kXULNS, NS_LITERAL_STRING("key"),
                             getter_AddRefs(keyElem));
  ASSERT_TRUE(keyElem);
  keyElem->SetAttribute(NS_LITERAL_STRING("id"), NS_LITERAL_STRING("keyFoo"));
  privateSource->SetTarget(
      nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(keyElem)));

  mCollector->GetEventKeyId(event, keyId);
  ASSERT_TRUE(keyId.Equals(NS_LITERAL_STRING("keyFoo")));

  // Make sure we don't crash if the source event target is a non-element
  nsCOMPtr<nsIDOMText> textNode;
  mDocument->CreateTextNode(NS_LITERAL_STRING("blah"),
                            getter_AddRefs(textNode));
  ASSERT_TRUE(textNode);
  privateSource->SetTarget(
      nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(textNode)));
  keyId.SetLength(0);
  mCollector->GetEventKeyId(event, keyId);
  ASSERT_TRUE(keyId.IsEmpty());

  ++gPassedTests;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 92 of file TestUICommandCollector.cpp.

{
  nsCOMPtr<nsIDOMDocumentEvent> docEvent = do_QueryInterface(mDocument);
  nsCOMPtr<nsIDOMEvent> event;
  docEvent->CreateEvent(NS_LITERAL_STRING("xulcommandevents"),
                        getter_AddRefs(event));

  // The private event interface lets us directly set the target
  // and original target for easier testing.
  nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
  ASSERT_TRUE(privateEvent);

  nsCOMPtr<nsIDOMElement> buttonElement;
  mDocument->CreateElementNS(kXULNS, NS_LITERAL_STRING("button"),
                            getter_AddRefs(buttonElement));
  ASSERT_TRUE(buttonElement);
  buttonElement->SetAttribute(NS_LITERAL_STRING("id"),
                              NS_LITERAL_STRING("btn1"));

  // First test the simple case where we have only explicit content.
  // Note that originalTarget == target unless set otherwise.
  privateEvent->SetTarget(
      nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(buttonElement)));

  nsString targetId, targetAnonId;
  nsresult rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
  ASSERT_TRUE(NS_SUCCEEDED(rv));
  ASSERT_TRUE(targetId.Equals(NS_LITERAL_STRING("btn1")));
  ASSERT_TRUE(targetAnonId.IsEmpty());

  // If there was an anonid, it should be ignored
  buttonElement->SetAttribute(NS_LITERAL_STRING("anonid"),
                              NS_LITERAL_STRING("abc"));
  rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
  ASSERT_TRUE(NS_SUCCEEDED(rv));
  ASSERT_TRUE(targetId.Equals(NS_LITERAL_STRING("btn1")));
  ASSERT_TRUE(targetAnonId.IsEmpty());

  // If the target has no id, GetEventTargets should fail
  buttonElement->RemoveAttribute(NS_LITERAL_STRING("id"));
  rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
  ASSERT_TRUE(NS_FAILED(rv));

  // The same should be true with no anonid
  buttonElement->RemoveAttribute(NS_LITERAL_STRING("anonid"));
  rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
  ASSERT_TRUE(NS_FAILED(rv));

  // Empty attributes should work the same as no attributes
  buttonElement->SetAttribute(NS_LITERAL_STRING("id"), nsString());
  rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
  ASSERT_TRUE(NS_FAILED(rv));

  // Now test some cases where the originalTarget is different
  nsCOMPtr<nsIDOMElement> anonChild;
  mDocument->CreateElementNS(kXULNS, NS_LITERAL_STRING("hbox"),
                            getter_AddRefs(anonChild));
  ASSERT_TRUE(anonChild);

  privateEvent->SetOriginalTarget(
      nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(anonChild)));

  // This is the typical id/anonid case for anonymous content
  buttonElement->SetAttribute(NS_LITERAL_STRING("id"),
                              NS_LITERAL_STRING("btn1"));
  anonChild->SetAttribute(NS_LITERAL_STRING("anonid"),
                          NS_LITERAL_STRING("box1"));
  targetId.SetLength(0);
  targetAnonId.SetLength(0);
  rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
  ASSERT_TRUE(NS_SUCCEEDED(rv));
  ASSERT_TRUE(targetId.Equals(NS_LITERAL_STRING("btn1")));
  ASSERT_TRUE(targetAnonId.Equals(NS_LITERAL_STRING("box1")));

  // If there is no id or anonid, it should fail
  anonChild->RemoveAttribute(NS_LITERAL_STRING("anonid"));
  rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
  ASSERT_TRUE(NS_FAILED(rv));

  // Test the failure case where the target/originalTarget is not an element
  privateEvent->SetOriginalTarget(nsnull);  // now mirrors target again
  nsCOMPtr<nsIDOMText> textNode;
  mDocument->CreateTextNode(NS_LITERAL_STRING("blah"),
                           getter_AddRefs(textNode));
  ASSERT_TRUE(textNode);
  privateEvent->SetTarget(
      nsCOMPtr<nsIDOMEventTarget>(do_QueryInterface(textNode)));
  rv = mCollector->GetEventTargets(event, targetId, targetAnonId);
  ASSERT_TRUE(NS_FAILED(rv));

  ++gPassedTests;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 81 of file TestUICommandCollector.cpp.

Definition at line 78 of file TestUICommandCollector.cpp.

Definition at line 79 of file TestUICommandCollector.cpp.

Definition at line 80 of file TestUICommandCollector.cpp.


The documentation for this class was generated from the following file: