Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / service_process / service_process_control_mac_unittest.mm
blobfebb5f21c73b129cb925d2602912b90aa9fb6f73
1 // Copyright 2014 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.
5 #include <launch.h>
7 #include "base/mac/launchd.h"
8 #include "base/mac/mac_util.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/google_toolbox_for_mac/src/Foundation/GTMServiceManagement.h"
13 namespace {
15 // Returns the parameters needed to launch a really simple script from launchd.
16 NSDictionary* TestJobDictionary(NSString* label_ns) {
17   NSString* shell_script_ns = @"sleep 10; echo TestGTMSMJobSubmitRemove";
18   base::scoped_nsobject<NSMutableArray> job_arguments(
19       [[NSMutableArray alloc] init]);
20   [job_arguments addObject:@"/bin/sh"];
21   [job_arguments addObject:@"-c"];
22   [job_arguments addObject:shell_script_ns];
24   NSMutableDictionary* job_dictionary_ns = [NSMutableDictionary dictionary];
25   [job_dictionary_ns setObject:label_ns forKey:@LAUNCH_JOBKEY_LABEL];
26   [job_dictionary_ns setObject:[NSNumber numberWithBool:YES]
27                         forKey:@LAUNCH_JOBKEY_RUNATLOAD];
28   [job_dictionary_ns setObject:job_arguments
29                         forKey:@LAUNCH_JOBKEY_PROGRAMARGUMENTS];
30   return job_dictionary_ns;
33 }  // namespace
35 TEST(ServiceProcessControlMac, TestGTMSMJobSubmitRemove) {
36   NSString* label_ns = @"com.chromium.ServiceProcessStateFileManipulationTest";
37   std::string label(label_ns.UTF8String);
38   CFStringRef label_cf = base::mac::NSToCFCast(label_ns);
40   // If the job is loaded or running, remove it.
41   pid_t pid = base::mac::PIDForJob(label);
42   CFErrorRef error = NULL;
43   if (pid >= 0)
44     ASSERT_TRUE(GTMSMJobRemove(label_cf, &error));
46   // The job should not be loaded or running.
47   pid = base::mac::PIDForJob(label);
48   EXPECT_LT(pid, 0);
50   // Submit a new job.
51   NSDictionary* job_dictionary_ns = TestJobDictionary(label_ns);
52   CFDictionaryRef job_dictionary_cf = base::mac::NSToCFCast(job_dictionary_ns);
53   ASSERT_TRUE(GTMSMJobSubmit(job_dictionary_cf, &error));
55   // The new job should be running.
56   pid = base::mac::PIDForJob(label);
57   EXPECT_GT(pid, 0);
59   // Remove the job.
60   ASSERT_TRUE(GTMSMJobRemove(label_cf, &error));
61   pid = base::mac::PIDForJob(label);
62   EXPECT_LT(pid, 0);
64   // Attempting to remove the job again should fail.
65   EXPECT_FALSE(GTMSMJobRemove(label_cf, &error));