/** * Copyright (c) 2015-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ #import "FBResponsePayload.h" #import "FBElementCache.h" #import "FBResponseFilePayload.h" #import "FBResponseJSONPayload.h" #import "FBSession.h" #import "XCUIElement+FBUtilities.h" #import "XCUIElement+FBWebDriverAttributes.h" inline static NSDictionary *FBDictionaryResponseWithElement(XCUIElement *element, NSString *elementUUID, BOOL compact); id FBResponseWithOK() { return FBResponseWithStatus(FBCommandStatusNoError, nil); } id FBResponseWithObject(id object) { return FBResponseWithStatus(FBCommandStatusNoError, object); } id FBResponseWithCachedElement(XCUIElement *element, FBElementCache *elementCache, BOOL compact) { NSString *elementUUID = [elementCache storeElement:element]; return FBResponseWithStatus(FBCommandStatusNoError, FBDictionaryResponseWithElement(element, elementUUID, compact)); } id FBResponseWithCachedElements(NSArray *elements, FBElementCache *elementCache, BOOL compact) { NSMutableArray *elementsResponse = [NSMutableArray array]; for (XCUIElement *element in elements) { NSString *elementUUID = [elementCache storeElement:element]; [elementsResponse addObject:FBDictionaryResponseWithElement(element, elementUUID, compact)]; } return FBResponseWithStatus(FBCommandStatusNoError, elementsResponse); } id FBResponseWithElementUUID(NSString *elementUUID) { return [[FBResponseJSONPayload alloc] initWithDictionary:@{ @"id" : elementUUID, @"sessionId" : [FBSession activeSession].identifier ?: NSNull.null, @"value" : @"", @"status" : @0, }]; } id FBResponseWithError(NSError *error) { return FBResponseWithStatus(FBCommandStatusUnhandled, error.description); } id FBResponseWithErrorFormat(NSString *format, ...) { va_list argList; va_start(argList, format); NSString *errorMessage = [[NSString alloc] initWithFormat:format arguments:argList]; id payload = FBResponseWithStatus(FBCommandStatusUnhandled, errorMessage); va_end(argList); return payload; } id FBResponseWithStatus(FBCommandStatus status, id object) { return [[FBResponseJSONPayload alloc] initWithDictionary:@{ @"value" : object ?: @{}, @"sessionId" : [FBSession activeSession].identifier ?: NSNull.null, @"status" : @(status), }]; } id FBResponseFileWithPath(NSString *path) { return [[FBResponseFilePayload alloc] initWithFilePath:path]; } inline static NSDictionary *FBDictionaryResponseWithElement(XCUIElement *element, NSString *elementUUID, BOOL compact) { NSMutableDictionary *dictionary = [NSMutableDictionary new]; dictionary[@"ELEMENT"] = elementUUID; if (!compact) { XCElementSnapshot *snapshot = element.fb_lastSnapshot; dictionary[@"type"] = snapshot.wdType; dictionary[@"label"] = snapshot.wdLabel ?: [NSNull null]; } return dictionary.copy; }