FBXPathTests.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import <XCTest/XCTest.h>
  10. #import "FBMacros.h"
  11. #import "FBXPath.h"
  12. #import "FBXPath-Private.h"
  13. #import "XCUIElementDouble.h"
  14. #import "XCElementSnapshotDouble.h"
  15. #import "FBXCElementSnapshotWrapper+Helpers.h"
  16. @interface FBXPathTests : XCTestCase
  17. @end
  18. @implementation FBXPathTests
  19. - (NSString *)xmlStringWithElement:(id<FBElement>)element
  20. xpathQuery:(nullable NSString *)query
  21. excludingAttributes:(nullable NSArray<NSString *> *)excludedAttributes
  22. {
  23. xmlDocPtr doc;
  24. xmlTextWriterPtr writer = xmlNewTextWriterDoc(&doc, 0);
  25. NSMutableDictionary *elementStore = [NSMutableDictionary dictionary];
  26. int buffersize;
  27. xmlChar *xmlbuff = NULL;
  28. int rc = xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
  29. if (rc >= 0) {
  30. rc = [FBXPath xmlRepresentationWithRootElement:element
  31. writer:writer
  32. elementStore:elementStore
  33. query:query
  34. excludingAttributes:excludedAttributes];
  35. if (rc >= 0) {
  36. rc = xmlTextWriterEndDocument(writer);
  37. }
  38. }
  39. if (rc >= 0) {
  40. xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
  41. }
  42. xmlFreeTextWriter(writer);
  43. xmlFreeDoc(doc);
  44. XCTAssertTrue(rc >= 0);
  45. XCTAssertEqual(1, [elementStore count]);
  46. NSString *result = [NSString stringWithCString:(const char *)xmlbuff encoding:NSUTF8StringEncoding];
  47. xmlFree(xmlbuff);
  48. return result;
  49. }
  50. - (void)testDefaultXPathPresentation
  51. {
  52. XCElementSnapshotDouble *snapshot = [XCElementSnapshotDouble new];
  53. id<FBElement> element = (id<FBElement>)[FBXCElementSnapshotWrapper ensureWrapped:(id)snapshot];
  54. NSString *resultXml = [self xmlStringWithElement:element
  55. xpathQuery:nil
  56. excludingAttributes:nil];
  57. NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" value=\"%@\" name=\"%@\" label=\"%@\" enabled=\"%@\" visible=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" index=\"%lu\" private_indexPath=\"top\"/>\n",
  58. element.wdType, element.wdType, element.wdValue, element.wdName, element.wdLabel, FBBoolToString(element.wdEnabled), FBBoolToString(element.wdVisible), FBBoolToString(element.wdAccessible), element.wdRect[@"x"], element.wdRect[@"y"], element.wdRect[@"width"], element.wdRect[@"height"], element.wdIndex];
  59. XCTAssertTrue([resultXml isEqualToString: expectedXml]);
  60. }
  61. - (void)testtXPathPresentationWithSomeAttributesExcluded
  62. {
  63. XCElementSnapshotDouble *snapshot = [XCElementSnapshotDouble new];
  64. id<FBElement> element = (id<FBElement>)[FBXCElementSnapshotWrapper ensureWrapped:(id)snapshot];
  65. NSString *resultXml = [self xmlStringWithElement:element
  66. xpathQuery:nil
  67. excludingAttributes:@[@"type", @"visible", @"value", @"index"]];
  68. NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ name=\"%@\" label=\"%@\" enabled=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" private_indexPath=\"top\"/>\n",
  69. element.wdType, element.wdName, element.wdLabel, FBBoolToString(element.wdEnabled), FBBoolToString(element.wdAccessible), element.wdRect[@"x"], element.wdRect[@"y"], element.wdRect[@"width"], element.wdRect[@"height"]];
  70. XCTAssertEqualObjects(resultXml, expectedXml);
  71. }
  72. - (void)testXPathPresentationBasedOnQueryMatchingAllAttributes
  73. {
  74. XCElementSnapshotDouble *snapshot = [XCElementSnapshotDouble new];
  75. snapshot.value = @"йоло<>&\"";
  76. snapshot.label = @"a\nb";
  77. id<FBElement> element = (id<FBElement>)[FBXCElementSnapshotWrapper ensureWrapped:(id)snapshot];
  78. NSString *resultXml = [self xmlStringWithElement:element
  79. xpathQuery:[NSString stringWithFormat:@"//%@[@*]", element.wdType]
  80. excludingAttributes:@[@"visible"]];
  81. NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" value=\"%@\" name=\"%@\" label=\"%@\" enabled=\"%@\" visible=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" index=\"%lu\" hittable=\"%@\" private_indexPath=\"top\"/>\n",
  82. element.wdType, element.wdType, @"йоло&lt;&gt;&amp;&quot;", element.wdName, @"a&#10;b", FBBoolToString(element.wdEnabled), FBBoolToString(element.wdVisible), FBBoolToString(element.wdAccessible), element.wdRect[@"x"], element.wdRect[@"y"], element.wdRect[@"width"], element.wdRect[@"height"], element.wdIndex, FBBoolToString(element.wdHittable)];
  83. XCTAssertEqualObjects(expectedXml, resultXml);
  84. }
  85. - (void)testXPathPresentationBasedOnQueryMatchingSomeAttributes
  86. {
  87. XCElementSnapshotDouble *snapshot = [XCElementSnapshotDouble new];
  88. id<FBElement> element = (id<FBElement>)[FBXCElementSnapshotWrapper ensureWrapped:(id)snapshot];
  89. NSString *resultXml = [self xmlStringWithElement:element
  90. xpathQuery:[NSString stringWithFormat:@"//%@[@%@ and contains(@%@, 'blabla')]", element.wdType, @"value", @"name"]
  91. excludingAttributes:nil];
  92. NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ value=\"%@\" name=\"%@\" private_indexPath=\"top\"/>\n",
  93. element.wdType, element.wdValue, element.wdName];
  94. XCTAssertTrue([resultXml isEqualToString: expectedXml]);
  95. }
  96. - (void)testSnapshotXPathResultsMatching
  97. {
  98. xmlDocPtr doc;
  99. xmlTextWriterPtr writer = xmlNewTextWriterDoc(&doc, 0);
  100. NSMutableDictionary *elementStore = [NSMutableDictionary dictionary];
  101. XCElementSnapshotDouble *snapshot = [XCElementSnapshotDouble new];
  102. id<FBElement> root = (id<FBElement>)[FBXCElementSnapshotWrapper ensureWrapped:(id)snapshot];
  103. NSString *query = [NSString stringWithFormat:@"//%@", root.wdType];
  104. int rc = xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
  105. if (rc >= 0) {
  106. rc = [FBXPath xmlRepresentationWithRootElement:root
  107. writer:writer
  108. elementStore:elementStore
  109. query:query
  110. excludingAttributes:nil];
  111. if (rc >= 0) {
  112. rc = xmlTextWriterEndDocument(writer);
  113. }
  114. }
  115. if (rc < 0) {
  116. xmlFreeTextWriter(writer);
  117. xmlFreeDoc(doc);
  118. XCTFail(@"Unable to create the source XML document");
  119. }
  120. xmlXPathObjectPtr queryResult = [FBXPath evaluate:query document:doc];
  121. if (NULL == queryResult) {
  122. xmlFreeTextWriter(writer);
  123. xmlFreeDoc(doc);
  124. XCTAssertNotEqual(NULL, queryResult);
  125. }
  126. NSArray *matchingSnapshots = [FBXPath collectMatchingSnapshots:queryResult->nodesetval
  127. elementStore:elementStore];
  128. xmlXPathFreeObject(queryResult);
  129. xmlFreeTextWriter(writer);
  130. xmlFreeDoc(doc);
  131. XCTAssertNotNil(matchingSnapshots);
  132. XCTAssertEqual(1, [matchingSnapshots count]);
  133. }
  134. @end