summaryrefslogtreecommitdiffstats
path: root/dom/media/gtest/TestMediaUtils.cpp
blob: 33a32b7ea0949463a301585b720e611a6c80bb3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "MediaUtils.h"
#include "mozilla/AppShutdown.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/gtest/MozHelpers.h"

using namespace mozilla;
using namespace mozilla::gtest;
using namespace mozilla::media;

// Spawning the death test child process aborts on Android.
#if !defined(ANDROID)

// Kept here for reference as it can be handy during development.
#  define DISABLE_CRASH_REPORTING  \
    gtest::DisableCrashReporter(); \
    ZERO_GDB_SLEEP();

void DoCreateTicketBeforeAppShutdownOnMain() {
  auto reporter = ScopedTestResultReporter::Create(ExitMode::ExitOnDtor);

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownNetTeardown);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTeardown);

  Monitor mon("TestMonitor");
  bool pastAppShutdown = false;
  bool backgroundTaskFinished = false;

  UniquePtr ticket = ShutdownBlockingTicket::Create(
      u"Test"_ns, NS_LITERAL_STRING_FROM_CSTRING(__FILE__), __LINE__);

  MOZ_ALWAYS_SUCCEEDS(
      NS_DispatchBackgroundTask(NS_NewRunnableFunction(__func__, [&] {
        TimeStamp now = TimeStamp::Now();
        TimeStamp end = now + TimeDuration::FromSeconds(0.2);
        MonitorAutoLock lock(mon);
        while (!pastAppShutdown && (end - now) > TimeDuration()) {
          lock.Wait(end - now);
          now = TimeStamp::Now();
        }
        EXPECT_FALSE(pastAppShutdown);
        ticket = nullptr;
        while (!pastAppShutdown) {
          lock.Wait();
        }
        EXPECT_TRUE(pastAppShutdown);
        backgroundTaskFinished = true;
        lock.Notify();
      })));

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdown);

  {
    MonitorAutoLock lock(mon);
    pastAppShutdown = true;
    lock.Notify();
    while (!backgroundTaskFinished) {
      lock.Wait();
    }
  }

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownQM);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTelemetry);

  NS_ShutdownXPCOM(nullptr);
}

void DoCreateTicketAfterAppShutdownOnMain() {
  auto reporter = ScopedTestResultReporter::Create(ExitMode::ExitOnDtor);

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownNetTeardown);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTeardown);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdown);

  auto ticket = ShutdownBlockingTicket::Create(
      u"Test"_ns, NS_LITERAL_STRING_FROM_CSTRING(__FILE__), __LINE__);
  EXPECT_FALSE(ticket);

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownQM);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTelemetry);

  NS_ShutdownXPCOM(nullptr);
}

void DoCreateTicketBeforeAppShutdownOffMain() {
  auto reporter = ScopedTestResultReporter::Create(ExitMode::ExitOnDtor);

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownNetTeardown);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTeardown);

  Monitor mon("TestMonitor");
  bool pastAppShutdown = false;
  bool ticketCreated = false;
  bool backgroundTaskFinished = false;

  MOZ_ALWAYS_SUCCEEDS(
      NS_DispatchBackgroundTask(NS_NewRunnableFunction(__func__, [&] {
        MonitorAutoLock lock(mon);
        auto ticket = ShutdownBlockingTicket::Create(
            u"Test"_ns, NS_LITERAL_STRING_FROM_CSTRING(__FILE__), __LINE__);
        EXPECT_TRUE(ticket);
        ticketCreated = true;
        lock.Notify();

        TimeStamp now = TimeStamp::Now();
        TimeStamp end = now + TimeDuration::FromSeconds(0.2);
        while (!pastAppShutdown && (end - now) > TimeDuration()) {
          lock.Wait(end - now);
          now = TimeStamp::Now();
        }
        EXPECT_FALSE(pastAppShutdown);
        ticket = nullptr;
        while (!pastAppShutdown) {
          lock.Wait();
        }
        EXPECT_TRUE(pastAppShutdown);
        backgroundTaskFinished = true;
        lock.Notify();
      })));

  {
    MonitorAutoLock lock(mon);
    while (!ticketCreated) {
      lock.Wait();
    }
  }

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdown);

  MonitorAutoLock lock(mon);
  pastAppShutdown = true;
  lock.Notify();
  while (!backgroundTaskFinished) {
    lock.Wait();
  }

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownQM);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTelemetry);

  NS_ShutdownXPCOM(nullptr);
}

void DoCreateTicketAfterAppShutdownOffMain() {
  auto reporter = ScopedTestResultReporter::Create(ExitMode::ExitOnDtor);

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownNetTeardown);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTeardown);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdown);

  UniquePtr<ShutdownBlockingTicket> ticket;
  MOZ_ALWAYS_SUCCEEDS(NS_DispatchBackgroundTask(
      MakeAndAddRef<SyncRunnable>(NS_NewRunnableFunction(__func__, [&] {
        ticket = ShutdownBlockingTicket::Create(
            u"Test"_ns, NS_LITERAL_STRING_FROM_CSTRING(__FILE__), __LINE__);
      }))));

  EXPECT_FALSE(ticket);

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownQM);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTelemetry);

  NS_ShutdownXPCOM(nullptr);
}

void DoTwoTicketsWithSameNameBothBlockShutdown() {
  auto reporter = ScopedTestResultReporter::Create(ExitMode::ExitOnDtor);

  const auto name = u"Test"_ns;
  auto ticket1 = ShutdownBlockingTicket::Create(
      name, NS_LITERAL_STRING_FROM_CSTRING(__FILE__), __LINE__);
  EXPECT_TRUE(ticket1);
  auto ticket2 = ShutdownBlockingTicket::Create(
      name, NS_LITERAL_STRING_FROM_CSTRING(__FILE__), __LINE__);
  EXPECT_TRUE(ticket2);

  ticket1 = nullptr;

  // A copyable holder for the std::function in NS_NewTimerWithCallback.
  auto ticket2Holder =
      MakeRefPtr<Refcountable<UniquePtr<ShutdownBlockingTicket>>>(
          ticket2.release());

  const auto waitBeforeDestroyingTicket = TimeDuration::FromMilliseconds(100);
  TimeStamp before = TimeStamp::Now();
  auto timerResult = NS_NewTimerWithCallback(
      [t = std::move(ticket2Holder)](nsITimer* aTimer) {},
      waitBeforeDestroyingTicket, nsITimer::TYPE_ONE_SHOT, __func__);
  ASSERT_TRUE(timerResult.isOk());

  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownNetTeardown);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTeardown);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdown);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownQM);
  AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTelemetry);

  NS_ShutdownXPCOM(nullptr);
  TimeStamp after = TimeStamp::Now();
  EXPECT_GT((after - before).ToMilliseconds(),
            waitBeforeDestroyingTicket.ToMilliseconds());
}

TEST(ShutdownBlockingTicketDeathTest, CreateTicketBeforeAppShutdownOnMain)
{
  EXPECT_EXIT(DoCreateTicketBeforeAppShutdownOnMain(),
              testing::ExitedWithCode(0), "");
}

TEST(ShutdownBlockingTicketDeathTest, CreateTicketAfterAppShutdownOnMain)
{
  EXPECT_EXIT(DoCreateTicketAfterAppShutdownOnMain(),
              testing::ExitedWithCode(0), "");
}

TEST(ShutdownBlockingTicketDeathTest, CreateTicketBeforeAppShutdownOffMain)
{
  EXPECT_EXIT(DoCreateTicketBeforeAppShutdownOffMain(),
              testing::ExitedWithCode(0), "");
}

TEST(ShutdownBlockingTicketDeathTest, CreateTicketAfterAppShutdownOffMain)
{
  EXPECT_EXIT(DoCreateTicketAfterAppShutdownOffMain(),
              testing::ExitedWithCode(0), "");
}

TEST(ShutdownBlockingTicketDeathTest, TwoTicketsWithSameNameBothBlockShutdown)
{
  EXPECT_EXIT(DoTwoTicketsWithSameNameBothBlockShutdown(),
              testing::ExitedWithCode(0), "");
}

#  undef DISABLE_CRASH_REPORTING

#endif