test_listener_ios.mm 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/test/test_listener_ios.h"
  5. #import <Foundation/Foundation.h>
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. // The iOS watchdog timer will kill an app that doesn't spin the main event
  8. // loop often enough. This uses a Gtest TestEventListener to spin the current
  9. // loop after each test finishes. However, if any individual test takes too
  10. // long, it is still possible that the app will get killed.
  11. namespace {
  12. class IOSRunLoopListener : public testing::EmptyTestEventListener {
  13. public:
  14. virtual void OnTestEnd(const testing::TestInfo& test_info);
  15. };
  16. void IOSRunLoopListener::OnTestEnd(const testing::TestInfo& test_info) {
  17. @autoreleasepool {
  18. // At the end of the test, spin the default loop for a moment.
  19. NSDate* stop_date = [NSDate dateWithTimeIntervalSinceNow:0.001];
  20. [[NSRunLoop currentRunLoop] runUntilDate:stop_date];
  21. }
  22. }
  23. } // namespace
  24. namespace base {
  25. namespace test_listener_ios {
  26. void RegisterTestEndListener() {
  27. testing::TestEventListeners& listeners =
  28. testing::UnitTest::GetInstance()->listeners();
  29. listeners.Append(new IOSRunLoopListener);
  30. }
  31. } // namespace test_listener_ios
  32. } // namespace base