Skip to content

Commit

Permalink
Merge pull request attentive-mobile#95 from vespinola/feature/APP-2804
Browse files Browse the repository at this point in the history
[APP-2804] Mobile App Domain Switching - iOS
  • Loading branch information
Vladimir Espinola Lezcano authored Jun 20, 2024
2 parents 0f912fc + 82a56d0 commit e12028d
Show file tree
Hide file tree
Showing 20 changed files with 562 additions and 106 deletions.
157 changes: 100 additions & 57 deletions Example/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion Example/CreativeUITest/CreativeUITest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class CreativeUITest: XCTestCase, BaseXCTestCase {

HomePage
.tapOnPushMeToCreative()
.addDelay(seconds: 3)
.addDelay(seconds: 4)

CreativePage.tapOnCloseCreative()

Expand Down Expand Up @@ -96,6 +96,29 @@ final class CreativeUITest: XCTestCase, BaseXCTestCase {

HomePage.verifyPushMeToCreativeIsHittable()
}

func testLoadCreative_switchDomain_shouldDisplayNewCreative() {
launch(with: .production)

HomePage
.verifyDomainTitle(text: "mobileapps")
.tapOnSwitchDomain()

SwitchDomainPage
.verifyIfTitleExists()
.fillDomainInput(text: "games")
.tapOnOk()
.addDelay(seconds: 1)

HomePage
.verifyDomainTitle(text: "games")
.tapOnPushMeToCreative()
.addDelay(seconds: 4)

CreativePage
.verifyIfElementExists(label: "AttentiveGames", elementType: .image)
.tapOnCloseCreative()
}
}

extension CreativeUITest {
Expand Down
23 changes: 23 additions & 0 deletions Example/CreativeUITest/Pages/HomePage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct HomePage: Page {
XCTFail("Push me to Creative is not visible")
return self
}
sleep(1)
XCTAssertTrue(creativeButton.isHittable)
return self
}
Expand All @@ -37,6 +38,24 @@ struct HomePage: Page {
productTabItem.tapOnElement()
return self
}

@discardableResult
static func tapOnSwitchDomain() -> Self.Type {
switchDomainButton.tapOnElement()
return self
}

@discardableResult
static func verifyDomainTitle(text: String) -> Self.Type {
let domainLabel = app.staticTexts["Domain: \(text)"]

guard domainLabel.elementExists() else {
XCTFail("Domain label does not exists")
return self
}

return self
}
}

fileprivate extension HomePage {
Expand All @@ -51,4 +70,8 @@ fileprivate extension HomePage {
static var productTabItem: XCUIElement {
app.tabBars.buttons["Product"]
}

static var switchDomainButton: XCUIElement {
app.buttons["Switch Domain"]
}
}
12 changes: 12 additions & 0 deletions Example/CreativeUITest/Pages/Page.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ extension Page {
window.tap()
return self
}

@discardableResult
static func verifyIfElementExists(label: String, elementType: XCUIElement.ElementType) -> Self.Type {
let element = app.descendants(matching: elementType)[label]

guard element.elementExists() else {
XCTFail("Element with label \(label) of type \(elementType) does not exists")
return self
}

return self
}
}
58 changes: 58 additions & 0 deletions Example/CreativeUITest/Pages/SwitchDomainPage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// SwitchDomainPage.swift
// CreativeUITest
//
// Created by Vladimir - Work on 2024-06-14.
//

import XCTest

struct SwitchDomainPage: Page {
private init() { }

@discardableResult
static func tapOnOk() -> Self.Type {
okButton.tapOnElement()
return self
}

@discardableResult
static func tapOnCancel() -> Self.Type {
cancelButton.tapOnElement()
return self
}

@discardableResult
static func fillDomainInput(text: String) -> Self.Type {
domainTextField.tapOnElement()
domainTextField.fillTextField(text)
return self
}

@discardableResult
static func verifyIfTitleExists() -> Self.Type {
guard title.elementExists() else {
XCTFail("Title does not exists")
return self
}
return self
}
}

fileprivate extension SwitchDomainPage {
static var title: XCUIElement {
app.staticTexts["Switch Domain"]
}

static var domainTextField: XCUIElement {
app.textFields["Enter the new Domain here"]
}

static var okButton: XCUIElement {
app.buttons["OK"]
}

static var cancelButton: XCUIElement {
app.buttons["Cancel"]
}
}
4 changes: 4 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
69CCAF5C28DCDE5A007620BD /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 69CCAF5B28DCDE5A007620BD /* Assets.xcassets */; };
69CCAF5F28DCDE5A007620BD /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 69CCAF5D28DCDE5A007620BD /* LaunchScreen.storyboard */; };
69CCAF6228DCDE5A007620BD /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 69CCAF6128DCDE5A007620BD /* main.m */; };
FB080C742C1C8AE500834BAA /* SwitchDomainPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB080C732C1C8AE500834BAA /* SwitchDomainPage.swift */; };
FB2F35B32C18D4CE0016CAE8 /* ATTNSDKFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FB2F35B22C18D4CE0016CAE8 /* ATTNSDKFramework.framework */; };
FB2F35B42C18D4CE0016CAE8 /* ATTNSDKFramework.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = FB2F35B22C18D4CE0016CAE8 /* ATTNSDKFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
FB90EF122C10AE66004DFC4A /* CreativeUITest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB90EF112C10AE66004DFC4A /* CreativeUITest.swift */; };
Expand Down Expand Up @@ -82,6 +83,7 @@
69CCAF6028DCDE5A007620BD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
69CCAF6128DCDE5A007620BD /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
69D3C149299EF2D10027934F /* CreativeUITest.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CreativeUITest.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
FB080C732C1C8AE500834BAA /* SwitchDomainPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwitchDomainPage.swift; sourceTree = "<group>"; };
FB2F35B22C18D4CE0016CAE8 /* ATTNSDKFramework.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = ATTNSDKFramework.framework; sourceTree = BUILT_PRODUCTS_DIR; };
FB6723742C134C13008D5516 /* Example - Pod-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Example - Pod-Bridging-Header.h"; sourceTree = "<group>"; };
FB90EF102C10AE66004DFC4A /* CreativeUITest-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "CreativeUITest-Bridging-Header.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -211,6 +213,7 @@
FB90EF1F2C10CF13004DFC4A /* SMSPage.swift */,
FB90EF212C10D412004DFC4A /* PricacyPolicyPage.swift */,
FB90EF232C10D83B004DFC4A /* ProductPage.swift */,
FB080C732C1C8AE500834BAA /* SwitchDomainPage.swift */,
);
path = Pages;
sourceTree = "<group>";
Expand Down Expand Up @@ -392,6 +395,7 @@
FB90EF202C10CF13004DFC4A /* SMSPage.swift in Sources */,
FB90EF152C10B871004DFC4A /* HomePage.swift in Sources */,
FB90EF122C10AE66004DFC4A /* CreativeUITest.swift in Sources */,
FB080C742C1C8AE500834BAA /* SwitchDomainPage.swift in Sources */,
FB90EF1C2C10B959004DFC4A /* XCUIElement+Extension.swift in Sources */,
FB90EF172C10B887004DFC4A /* BaseXCTestCase.swift in Sources */,
FB90EF222C10D412004DFC4A /* PricacyPolicyPage.swift in Sources */,
Expand Down
73 changes: 54 additions & 19 deletions Example/Example/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,6 @@ @implementation ViewController {

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor systemGray3Color];

CGFloat contentWidth = self.scrollView.bounds.size.width;
CGFloat contentHeight = self.scrollView.bounds.size.height * 3;
self.scrollView.contentSize = CGSizeMake(contentWidth, contentHeight);

CGFloat subviewHeight = (CGFloat)120;
CGFloat currentViewOffset = (CGFloat)600;

while (currentViewOffset < contentHeight) {
CGRect frame = CGRectInset(CGRectMake(0, currentViewOffset, contentWidth, subviewHeight), 5, 5);
CGFloat hue = currentViewOffset / contentHeight;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [UIColor colorWithHue:hue saturation:1 brightness:1 alpha:1];
[self.scrollView addSubview:subview];

currentViewOffset += subviewHeight;
}

// Replace with your Attentive domain to test with your Attentive account
_domain = @"YOUR_ATTENTIVE_DOMAIN";
Expand All @@ -66,7 +48,7 @@ - (void)viewDidLoad {
[sdk identify:_userIdentifiers];

// Attentive Example app specific setup code
[_domainLabel setText:_domain];
[_domainLabel setText: [NSString stringWithFormat:@"Domain: %@", _domain]];
}

- (IBAction)creativeButtonPress:(id)sender {
Expand Down Expand Up @@ -113,6 +95,45 @@ - (IBAction)clearUserButtonPressed:(id)sender {
[sdk clearUser];
}

- (IBAction)swichDomainTapped:(id)sender {
[self showAlertForSwitchingDomains];
}

- (void)showAlertForSwitchingDomains {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Switch Domain"
message:@"Here you will test switching domains"
preferredStyle:UIAlertControllerStyleAlert];

[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"Enter the new Domain here";
}];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
UITextField *textField = alertController.textFields.firstObject;
[self handleDomainInput:textField.text];
}];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:nil];
[alertController addAction:okAction];
[alertController addAction:cancelAction];

[self presentViewController:alertController animated:YES completion:nil];
}

- (void)handleDomainInput:(NSString *)text {
[sdk updateDomain:text];

_domain = text;

[_domainLabel setText: [NSString stringWithFormat:@"Domain: %@", _domain]];

[self showToast:[NSString stringWithFormat:@"New domain is %@", text]];
}

// Method for setting up UI Tests. Only used for testing purposes
- (void)setupForUITests {
// Override the hard-coded domain & mode with the values from the environment variables
Expand All @@ -135,4 +156,18 @@ - (void)setupForUITests {
}
}

- (void)showToast:(NSString*)message {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];

[self presentViewController:alert animated:YES completion:nil];

int duration = 3; // duration in seconds

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}

@end
16 changes: 8 additions & 8 deletions ExampleSwift/ExampleSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
588EB7242995BC3A00C0163A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 588EB7182995BBB700C0163A /* LaunchScreen.storyboard */; };
588EB7252995BC3A00C0163A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 587DC5C3299554B00082F8D0 /* Assets.xcassets */; };
588EB7262995BC3A00C0163A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 587DC5C82995551A0082F8D0 /* Main.storyboard */; };
FB080C7A2C1CB28500834BAA /* ATTNSDKFramework in Frameworks */ = {isa = PBXBuildFile; productRef = FB080C792C1CB28500834BAA /* ATTNSDKFramework */; };
FB2F35B62C18D6950016CAE8 /* ATTNSDKFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FB2F35B52C18D6950016CAE8 /* ATTNSDKFramework.framework */; };
FB2F35B72C18D6950016CAE8 /* ATTNSDKFramework.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = FB2F35B52C18D6950016CAE8 /* ATTNSDKFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
FB6723732C134958008D5516 /* ATTNSDKFramework in Frameworks */ = {isa = PBXBuildFile; productRef = FB6723722C134958008D5516 /* ATTNSDKFramework */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -68,7 +68,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
FB6723732C134958008D5516 /* ATTNSDKFramework in Frameworks */,
FB080C7A2C1CB28500834BAA /* ATTNSDKFramework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -169,7 +169,7 @@
);
name = "ExampleSwift - SPM";
packageProductDependencies = (
FB6723722C134958008D5516 /* ATTNSDKFramework */,
FB080C792C1CB28500834BAA /* ATTNSDKFramework */,
);
productName = ExampleSwift;
productReference = 588EB72C2995BC3A00C0163A /* ExampleSwift - SPM.app */;
Expand Down Expand Up @@ -200,7 +200,7 @@
);
mainGroup = 58389AA12987647900A31A35;
packageReferences = (
FB6723712C134958008D5516 /* XCRemoteSwiftPackageReference "attentive-ios-sdk" */,
FB080C782C1CB28500834BAA /* XCRemoteSwiftPackageReference "attentive-ios-sdk" */,
);
productRefGroup = 58389AAB2987647900A31A35 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -600,9 +600,9 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
FB6723712C134958008D5516 /* XCRemoteSwiftPackageReference "attentive-ios-sdk" */ = {
FB080C782C1CB28500834BAA /* XCRemoteSwiftPackageReference "attentive-ios-sdk" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/vespinola/attentive-ios-sdk.git";
repositoryURL = "https://github.com/attentive-mobile/attentive-ios-sdk";
requirement = {
branch = main;
kind = branch;
Expand All @@ -611,9 +611,9 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
FB6723722C134958008D5516 /* ATTNSDKFramework */ = {
FB080C792C1CB28500834BAA /* ATTNSDKFramework */ = {
isa = XCSwiftPackageProductDependency;
package = FB6723712C134958008D5516 /* XCRemoteSwiftPackageReference "attentive-ios-sdk" */;
package = FB080C782C1CB28500834BAA /* XCRemoteSwiftPackageReference "attentive-ios-sdk" */;
productName = ATTNSDKFramework;
};
/* End XCSwiftPackageProductDependency section */
Expand Down
12 changes: 6 additions & 6 deletions ExampleSwift/ExampleSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class AppDelegate : UIResponder, UIApplicationDelegate {

public static func createUserIdentifiers() -> [String: Any] {
[
IDENTIFIER_TYPE_PHONE: "+15671230987",
IDENTIFIER_TYPE_EMAIL: "[email protected]",
IDENTIFIER_TYPE_CLIENT_USER_ID: "APP_USER_ID",
IDENTIFIER_TYPE_SHOPIFY_ID: "207119551",
IDENTIFIER_TYPE_KLAVIYO_ID: "555555",
IDENTIFIER_TYPE_CUSTOM_IDENTIFIERS: ["customId": "customIdValue"]
ATTNIdentifierType.phone: "+15671230987",
ATTNIdentifierType.email: "[email protected]",
ATTNIdentifierType.clientUserId: "APP_USER_ID",
ATTNIdentifierType.shopifyId: "207119551",
ATTNIdentifierType.klaviyoId: "555555",
ATTNIdentifierType.customIdentifiers: ["customId": "customIdValue"]
]
}
}
Loading

0 comments on commit e12028d

Please sign in to comment.