From 9e050cc73a17372d4a824f3de84be56559029d8a Mon Sep 17 00:00:00 2001 From: Edwin Wu Date: Tue, 16 Sep 2025 04:24:35 -0700 Subject: [PATCH] [NC Apple coverage] Add tests for timer.mm. PiperOrigin-RevId: 807639173 --- .../platform/implementation/apple/Tests/BUILD | 1 + .../apple/Tests/GNCTimerTest.mm | 116 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 internal/platform/implementation/apple/Tests/GNCTimerTest.mm diff --git a/internal/platform/implementation/apple/Tests/BUILD b/internal/platform/implementation/apple/Tests/BUILD index 98ebae0d..bb164140 100644 --- a/internal/platform/implementation/apple/Tests/BUILD +++ b/internal/platform/implementation/apple/Tests/BUILD @@ -32,6 +32,7 @@ objc_library( "GNCPreferencesManagerTest.mm", "GNCScheduledExecutorTest.mm", "GNCSingleThreadExecutorTest.mm", + "GNCTimerTest.mm", "GNCUtilsTest.m", "GNCWifiLanMediumTest.mm", "UtilsTest.mm", diff --git a/internal/platform/implementation/apple/Tests/GNCTimerTest.mm b/internal/platform/implementation/apple/Tests/GNCTimerTest.mm new file mode 100644 index 00000000..924ef521 --- /dev/null +++ b/internal/platform/implementation/apple/Tests/GNCTimerTest.mm @@ -0,0 +1,116 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "internal/platform/implementation/apple/timer.h" + +#import + +#include + +@interface GNCTimerTest : XCTestCase +@end + +@implementation GNCTimerTest + +- (void)testCreateTimer { + auto timer = std::make_unique(); + XCTAssertTrue(timer != nullptr); +} + +- (void)testStartAndFire { + XCTestExpectation* expectation = [self expectationWithDescription:@"Timer fired"]; + auto timer = std::make_unique(); + + bool fired = false; + XCTAssertTrue(timer->Create(10, 0, [&]() { + fired = true; + [expectation fulfill]; + })); + + [self waitForExpectationsWithTimeout:1.0 handler:nil]; + XCTAssertTrue(fired); +} + +- (void)testStop { + auto timer = std::make_unique(); + + bool fired = false; + XCTAssertTrue(timer->Create(1000, 0, [&]() { fired = true; })); + + XCTAssertTrue(timer->Stop()); + + // Wait for a second to ensure the timer doesn't fire. + [NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]]; + XCTAssertFalse(fired); +} + +- (void)testPeriodicTimer { + XCTestExpectation* expectation = [self expectationWithDescription:@"Timer fired twice"]; + auto timer = std::make_unique(); + + int fireCount = 0; + XCTAssertTrue(timer->Create(10, 10, [&]() { + fireCount++; + if (fireCount == 2) { + [expectation fulfill]; + } + })); + + [self waitForExpectationsWithTimeout:1.0 handler:nil]; + XCTAssertEqual(fireCount, 2); + XCTAssertTrue(timer->Stop()); +} + +- (void)testRestart { + XCTestExpectation* expectation = [self expectationWithDescription:@"Timer fired"]; + auto timer = std::make_unique(); + + bool firstFired = false; + XCTAssertTrue(timer->Create(1000, 0, [&]() { firstFired = true; })); + + XCTAssertTrue(timer->Stop()); + + bool secondFired = false; + XCTAssertTrue(timer->Create(10, 0, [&]() { + secondFired = true; + [expectation fulfill]; + })); + + [self waitForExpectationsWithTimeout:1.0 handler:nil]; + XCTAssertFalse(firstFired); + XCTAssertTrue(secondFired); +} + +- (void)testStopWhenNotRunning { + auto timer = std::make_unique(); + XCTAssertTrue(timer->Stop()); +} + +- (void)testFireNow { + XCTestExpectation* expectation = [self expectationWithDescription:@"Timer fired"]; + auto timer = std::make_unique(); + + bool fired = false; + XCTAssertTrue(timer->Create(1000, 0, [&]() { + fired = true; + [expectation fulfill]; + })); + + XCTAssertTrue(timer->FireNow()); + + [self waitForExpectationsWithTimeout:1.0 handler:nil]; + XCTAssertTrue(fired); +} + +@end