Posts

Post not yet marked as solved
2 Replies
2.1k Views
In Unit Test, I have a simple table view with a bunch of basic cells (cells with just a label). I would like to access the cell using `cellForRow(at:)`, so I can test thing like selecting and deselecting the row programmatically, but this `cellForRow` queiry always returns `nil`.There is some discussion online that I should be using the data source's `tableView(_, cellForRowAt:)` instead. This is _not_ my intention. I only want to test the cell's visibility, test selecting and deselecting them. To test for visibility, `cellForRow(at:)` is the right function to use. Furthermore, the data source's `tableView(_, cellForRowAt:)` has no safeguard for out-of-range index access, while table view's `cellForRow(at:)` will gracefully return `nil` in this case, which I also wanted to test my table view controller on.However, while I can always get a valid cell from `tableViewController.tableView(_:cellForRowAt:)`, I couldn't understand why I always get `nil` from `tableView.cellForRow(at:)`. I have verfied both the `tableView` and the `tableViewController` are not `nil` in my unit test, and I have also triggered view loading with:_ = tableViewController.viewin `setUp()`. I also verified with `tableView.indexPathsForVisibleRows`, and the result _does_ include the index path I used for `cellForRow(at:)`.When I queried my cell through LLDB and breakpoints, sometimes my cell would show up properly. Is it possible that I am missing things like asynchronous waiting since loading up cells visually may be done in a different thread? Am I supposed to add expectation waiting of some sort to wait until the cells are fully loaded up before I can access them with `cellForRow(at:)`, even through `tableView.indexPathsForVisibleRows` already returns the expected index paths.? I tried to set this up but I'm not sure how to override my table view controller's `init()`.Here's my code in the Unit Test class.import XCTest @testable import TableViewTest class TableViewTestTests: XCTestCase { private var appDelegate: AppDelegate! private var tableVC: TableViewController! override func setUp() { super.setUp() appDelegate = UIApplication.shared.delegate as! AppDelegate let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) tableVC = storyboard.instantiateViewController(withIdentifier: "TableViewController") as! TableViewController // Trigger view load and viewDidLoad() _ = tableVC.view } override func tearDown() { super.tearDown() } func testGetFirstRow() { let tableView = tableVC.tableView! let indexPath0 = IndexPath(item: 0, section: 0) let cell0 = tableView.cellForRow(at: indexPath0) let visibleRows = tableView.indexPathsForVisibleRows XCTAssert(visibleRows != nil) // PASSED XCTAssert(tableView.indexPathsForVisibleRows!.contains(indexPath0)) // PASSED XCTAssert(cell0 != nil) // FAILED } func testGetFirstRowDataSource() { let tableView = tableVC.tableView! let indexPath0 = IndexPath(item: 0, section: 0) // This won't check for cell visibility. let cell0 = tableVC.tableView(tableView, cellForRowAt: indexPath0) let visibleRows = tableView.indexPathsForVisibleRows XCTAssert(visibleRows != nil) // PASSED XCTAssert(tableView.indexPathsForVisibleRows!.contains(indexPath0)) // PASSED }
Posted
by huapapojt.
Last updated
.
Post not yet marked as solved
0 Replies
715 Views
This is a repost from my own SO thread: https://stackoverflow.com/questions/47181619/size-classd-auto-layout-constraint-activated-for-wrong-size-classWhen I set up an Auto Layout constraint customized by Size Classes, such that the constraint is installed in some Size Class(es), and not in others, I get this Unsatisfiable Constraints error message during runtime but the result is as expected on Interface Builder with no errors / warnings. [1]: https://i.stack.imgur.com/CLrTs.png [2]: https://i.stack.imgur.com/f9cQa.pngWhat I am testingFor this simplified example, to demonstrate how Unsatisfiable Constraints unexpectedly happen, I want the star image to have fixed height, but custom width based on Size Class. As you can see in the image, I want it fairly big for Regular Width.Because the width constraint is not installed for Compact Width, it gets the image size based on the intrinsic content of the star image. (Credit: This star is from [Use Your Loaf](https://useyourloaf.com/blog/adapting-stack-views-with-size-classes/))The problemAs I rotate the device from landscape to portrait in the simulator running iPhone 8 Plus (iOS 11.1 with Xcode 9.1), I get the following error:Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. .... Will attempt to recover by breaking constraint <NSLayoutConstraint:0x6040002839d0 UIImageView:0x7fe177d0b940.width == 480 (active)>So apparently, this width constraint is still present even when my view is changing to Compact Width, where I specified to have the constraint NOT installed. Why is this? Is using `installed` the proper way to customize Auto Layout constraints based on different Size Class? I don't see any other way to achieve it in Storyboard though.I tried creating the width constraint programmatically and setting `.isActive` from inside `traitCollectionDidChange` instead, and I still got the same error.I didn't get such error when testing the sample code from [Mysteries of Auto Layout WWDC talk](https://developer.apple.com/videos/play/wwdc2015/218/), which is deactivating / activating constraints through `traitCollectionDidChange` just like I did.UpdateI added my own code on [GitHub](https://github.com/pt2277/SingleStarAutoLayoutTest/tree/master).
Posted
by huapapojt.
Last updated
.
Post not yet marked as solved
0 Replies
1.3k Views
(This is a repost from my SO post here).When deploying an app to my iOS device, triggering log collection via _sysdiagnose_, then exploring log messages in `system_logs.logarchive` obtained from iTunes sync, my app has never shown Info / Debug log messages type.Please note that this question is different from existing but similar-sound questions like- [How to set the log level on iOS 10?](https://stackoverflow.com/questions/41608912/how-to-set-the-log-level-on-ios-10)- [Xcode 8 - os_log_debug and os_log_info logs are not displayed on new Mac console (unified logging)](https://stackoverflow.com/questions/38303711/xcode-8-os-log-debug-and-os-log-info-logs-are-not-displayed-on-new-mac-console).I am using Paper Company (Swift) [sample code from Apple](https://developer.apple.com/library/content/samplecode/Logging/Introduction/Intro.html#//apple_ref/doc/uid/TP40017510-Intro-DontLinkElementID_2) to illustrate my problem. I start to deploy the app to my iOS device running iOS 11.0.3 as-is. The scheme is using Debug Mode. In my case:- I always have “Include Info Messages” and “Include Debug Messages” on in Console.- When I live stream my log messages while my app is running tethered to Xcode, I continue to see all log messages including Info and Debug types on Console.- When I trigger log collection via _sysdiagnose_ and read through `logarchive` file with Console, however, I never see my app’s Info and Debug messages. Other messages types, Default, Error, and Fault, continue to show up like when I live stream. - However, I can still see Info and Debug messages that belong to other system processes, but not my app’s process.![Live Stream. Info / Debug are Shown][1]![Live Stream. Activity Tracing Working][2]![Logarchive. No Info / Debug for my App][3]![Logarchive. No Activity Tracing][4]![Logarchive. Info / Debug Shown for other Processes][5]Because of such puzzling behavior, I started to think that it might have to do with system’s configuration. In [Logging documentation](https://developer.apple.com/documentation/os/logging):> Info-level messages are initially stored in memory buffers. Without a configuration change, they are not moved to the data store and are purged as memory buffers fill. They are, however, captured in the data store when faults and, optionally, errors occur. When info-level messages are added to the data store, they remain there until a storage quota is exceeded, at which point, the oldest messages are purged. Use this level to capture information that may be helpful, but isn’t essential, for troubleshooting errors.After reading that, I have also tried to change the Error type to Fault type, just for the sake of verifying that Info type messages will show up.os_log("B-b-b-b-b-b-b-bomb !", log: ViewController.ui_log, type: .fault) // previously .errorHowever, I still don’t see any Info log messages in `logarchive` file.My questions:- Am I missing something that I need to set to make the logging behavior for _sysdiagnose_ match the Console output I see when live streaming? Please note that Paper Company (Swift) already uses the `ASSETCATALOG_COMPRESSION` Build Settings, as has been pointed out as important to make `os_log` work as expected. See [Read logs using the new swift os_log api](https://stackoverflow.com/questions/40272910/read-logs-using-the-new-swift-os-log-api)- Is this an issue with Configuration Profile? In order to see Info / Debug messages from `logarchive`, do I actually need to add a custom profile like shown in Apple’s Logging doc and [this mobleconfig example](https://gist.github.com/zwaldowski/f9c82dfe1595506a36bd71f55cbb7538)? It seems suspicious, however, given that even with Fault type, the Info messages still don’t get logged, and that I can still see Info / Debug messages from other processes. [1]: https://i.stack.imgur.com/rwn2r.jpg [2]: https://i.stack.imgur.com/UOMPs.jpg [3]: https://i.stack.imgur.com/UBeid.jpg [4]: https://i.stack.imgur.com/d2ODe.png [5]: https://i.stack.imgur.com/YKJJT.png
Posted
by huapapojt.
Last updated
.