Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: ios/chrome/app/deferred_initialization_runner_unittest.mm

Issue 2217083002: Add sequential dispatching for InitializationRunner (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix tests Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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
5 #import "ios/chrome/app/deferred_initialization_runner.h"
6
7 #import "base/test/ios/wait_util.h"
8 #include "base/time/time.h"
9 #include "testing/platform_test.h"
10
11 TEST(DeferredInitializationRunnerTest, TestSharedInstance) {
12 EXPECT_TRUE([DeferredInitializationRunner sharedInstance]);
13 // Cancelling a non-existing block does nothing.
14 [[DeferredInitializationRunner sharedInstance]
15 cancelBlockNamed:@"Invalid Name"];
16 }
17
18 // Tests that all blocks added on the queue are executed after a delay.
19 TEST(DeferredInitializationRunnerTest, TestRunBlockSequentially) {
20 // Setup.
21 __block bool firstFlag = NO;
22 __block bool secondFlag = NO;
23 DeferredInitializationRunner* runner =
24 [DeferredInitializationRunner sharedInstance];
25 ProceduralBlock firstBlock = ^() {
26 EXPECT_FALSE(firstFlag);
27 firstFlag = YES;
28 };
29 ProceduralBlock secondBlock = ^() {
30 EXPECT_FALSE(secondFlag);
31 secondFlag = YES;
32 };
33 ConditionBlock secondBlockRun = ^bool {
34 return secondFlag;
35 };
36 runner.delayBetweenBlocks = 0.01;
37 runner.delayBeforeFirstBlock = 0.01;
38
39 [runner enqueueBlockNamed:@"first block" block:firstBlock];
40 [runner enqueueBlockNamed:@"second block" block:secondBlock];
41
42 ASSERT_FALSE(firstFlag);
43 ASSERT_FALSE(secondFlag);
44 EXPECT_EQ(2U, [runner numberOfBlocksRemaining]);
45
46 // Action.
47 base::test::ios::WaitUntilCondition(secondBlockRun);
48
49 // Test.
50 EXPECT_TRUE(firstFlag);
51 EXPECT_TRUE(secondFlag);
52 EXPECT_EQ(0U, [runner numberOfBlocksRemaining]);
53 }
54
55 // Tests that runBlockIfNecessary do not execute the block if it has already
jif 2016/09/26 09:12:47 s/do/does/ ?
gambard 2016/09/26 13:42:55 Done.
56 // been executed and runs synchronously the one not executed.
57 TEST(DeferredInitializationRunnerTest, TestRunBlock) {
58 // Setup.
59 __block bool quickFlag = NO;
60 __block bool slowFlag = NO;
61 DeferredInitializationRunner* runner =
62 [DeferredInitializationRunner sharedInstance];
63 ProceduralBlock quickBlock = ^() {
64 EXPECT_FALSE(quickFlag);
65 quickFlag = YES;
66 // Make sure we have time to go back to this test before running the second
67 // task.
68 runner.delayBetweenBlocks = 1;
69 };
70 ConditionBlock quickBlockRun = ^bool {
71 return quickFlag;
72 };
73 ProceduralBlock slowBlock = ^() {
74 EXPECT_FALSE(slowFlag);
75 slowFlag = YES;
76 };
77 runner.delayBeforeFirstBlock = 0.01;
78
79 // Action.
80 [runner enqueueBlockNamed:@"quick block" block:quickBlock];
81 [runner enqueueBlockNamed:@"slow block" block:slowBlock];
82
83 // Test.
84 base::test::ios::WaitUntilCondition(quickBlockRun);
85 EXPECT_TRUE(quickFlag);
86 EXPECT_FALSE(slowFlag);
87 EXPECT_EQ(1U, [runner numberOfBlocksRemaining]);
88 [runner runBlockIfNecessary:@"quick block"];
89 [runner runBlockIfNecessary:@"slow block"];
90 EXPECT_TRUE(quickFlag);
91 EXPECT_TRUE(slowFlag);
92 EXPECT_EQ(0U, [runner numberOfBlocksRemaining]);
93 }
94
95 // Tests that a block is not executed when cancelled and it is removed from the
96 // remaining blocks list.
97 TEST(DeferredInitializationRunnerTest, TestCancelBlock) {
98 // Setup.
99 __block BOOL blockFinished = NO;
100 DeferredInitializationRunner* runner =
101 [DeferredInitializationRunner sharedInstance];
102 runner.delayBeforeFirstBlock = 0.01;
103 runner.delayBetweenBlocks = 0.01;
104
105 [runner enqueueBlockNamed:@"cancel me"
106 block:^() {
107 blockFinished = YES;
108 }];
109 ASSERT_EQ(1U, [runner numberOfBlocksRemaining]);
110
111 // Action.
112 [runner cancelBlockNamed:@"cancel me"];
113
114 // Test.
115 EXPECT_FALSE(blockFinished);
116 EXPECT_EQ(0U, [runner numberOfBlocksRemaining]);
117 }
118
119 // Tests that a cancelled block will do nothing when run by name.
120 TEST(DeferredInitializationRunnerTest, TestCancelledBlockDoNothing) {
121 // Setup.
122 __block BOOL blockFinished = NO;
123 DeferredInitializationRunner* runner =
124 [DeferredInitializationRunner sharedInstance];
125 runner.delayBeforeFirstBlock = 0.01;
126 runner.delayBetweenBlocks = 0.01;
127
128 [runner enqueueBlockNamed:@"cancel me"
129 block:^() {
130 blockFinished = YES;
131 }];
132
133 // Action.
134 [runner cancelBlockNamed:@"cancel me"];
135 [runner runBlockIfNecessary:@"cancel me"];
136
137 // Test: expect false, the block should never be executed because it was
138 // cancelled before it started running.
139 EXPECT_FALSE(blockFinished);
140 }
141
142 // Tests that adding a block with the same name as an existing block will
143 // override the existing one.
144 TEST(DeferredInitializationRunnerTest, TestSecondBlockInvalidatesFirst) {
145 // Setup.
146 __block int blockRunCount = 0;
147 ProceduralBlock runBlock = ^() {
148 ++blockRunCount;
149 };
150 DeferredInitializationRunner* runner =
151 [DeferredInitializationRunner sharedInstance];
152 runner.delayBeforeFirstBlock = 0.01;
153 runner.delayBetweenBlocks = 0.01;
154
155 // Action.
156 [runner enqueueBlockNamed:@"multiple" block:runBlock];
157 [runner enqueueBlockNamed:@"multiple" block:runBlock];
158
159 // Test: |runBlock| was executed only once.
160 EXPECT_EQ(1U, [runner numberOfBlocksRemaining]);
161 [runner runBlockIfNecessary:@"multiple"];
162 EXPECT_EQ(0U, [runner numberOfBlocksRemaining]);
163 EXPECT_EQ(1, blockRunCount);
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698