1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "nsWidgetsCID.h"
8 #include "nsIIdleService.h"
9 
10 #ifndef TEST_NAME
11 #  error "Must #define TEST_NAME before including places_test_harness_tail.h"
12 #endif
13 
14 int gTestsIndex = 0;
15 
16 #define TEST_INFO_STR "TEST-INFO | "
17 
18 class RunNextTest : public mozilla::Runnable {
19  public:
RunNextTest()20   RunNextTest() : mozilla::Runnable("RunNextTest") {}
Run()21   NS_IMETHOD Run() override {
22     NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
23     if (gTestsIndex < int(mozilla::ArrayLength(gTests))) {
24       do_test_pending();
25       Test& test = gTests[gTestsIndex++];
26       (void)fprintf(stderr, TEST_INFO_STR "Running %s.\n", test.name);
27       test.func();
28     }
29 
30     do_test_finished();
31     return NS_OK;
32   }
33 };
34 
35 static const bool kDebugRunNextTest = false;
36 
run_next_test()37 void run_next_test() {
38   if (kDebugRunNextTest) {
39     printf_stderr("run_next_test()\n");
40     nsTraceRefcnt::WalkTheStack(stderr);
41   }
42   nsCOMPtr<nsIRunnable> event = new RunNextTest();
43   do_check_success(NS_DispatchToCurrentThread(event));
44 }
45 
46 int gPendingTests = 0;
47 
do_test_pending()48 void do_test_pending() {
49   NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
50   if (kDebugRunNextTest) {
51     printf_stderr("do_test_pending()\n");
52     nsTraceRefcnt::WalkTheStack(stderr);
53   }
54   gPendingTests++;
55 }
56 
do_test_finished()57 void do_test_finished() {
58   NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
59   NS_ASSERTION(gPendingTests > 0, "Invalid pending test count!");
60   gPendingTests--;
61 }
62 
disable_idle_service()63 void disable_idle_service() {
64   (void)fprintf(stderr, TEST_INFO_STR "Disabling Idle Service.\n");
65 
66   nsCOMPtr<nsIIdleService> idle =
67       do_GetService("@mozilla.org/widget/idleservice;1");
68   idle->SetDisabled(true);
69 }
70 
TEST(IHistory,Test)71 TEST(IHistory, Test)
72 {
73   RefPtr<WaitForConnectionClosed> spinClose = new WaitForConnectionClosed();
74 
75   // Tinderboxes are constantly on idle.  Since idle tasks can interact with
76   // tests, causing random failures, disable the idle service.
77   disable_idle_service();
78 
79   do_test_pending();
80   run_next_test();
81 
82   // Spin the event loop until we've run out of tests to run.
83   mozilla::SpinEventLoopUntil([&]() { return !gPendingTests; });
84 
85   // And let any other events finish before we quit.
86   (void)NS_ProcessPendingEvents(nullptr);
87 }
88