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

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: Add comments Created 4 years, 4 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
« no previous file with comments | « ios/chrome/app/deferred_initialization_runner.mm ('k') | ios/chrome/ios_chrome_tests.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
38 [runner enqueueBlockNamed:@"first block" block:firstBlock];
39 [runner enqueueBlockNamed:@"second block" block:secondBlock];
40
41 ASSERT_FALSE(firstFlag);
42 ASSERT_FALSE(secondFlag);
43 EXPECT_EQ(2U, [runner numberOfBlocksRemaining]);
44
45 // Action.
46 base::test::ios::WaitUntilCondition(secondBlockRun);
47
48 // Test.
49 EXPECT_TRUE(firstFlag);
50 EXPECT_TRUE(secondFlag);
51 EXPECT_EQ(0U, [runner numberOfBlocksRemaining]);
52 }
53
54 // Tests that runBlockIfNecessary do not execute the block if it has already
55 // been executed and runs synchronously the one not executed.
56 TEST(DeferredInitializationRunnerTest, TestRunBlock) {
57 // Setup.
58 __block BOOL quickFlag = NO;
59 __block BOOL slowFlag = NO;
60 DeferredInitializationRunner* runner =
61 [DeferredInitializationRunner sharedInstance];
62 ProceduralBlock quickBlock = ^() {
63 EXPECT_FALSE(quickFlag);
64 quickFlag = YES;
65 // Make sure we have time to go back to this test before running the second
66 // task.
67 runner.delayBetweenBlocks = 1;
68 };
69 ConditionBlock quickBlockRun = ^BOOL {
70 return quickFlag;
71 };
72 ProceduralBlock slowBlock = ^() {
73 EXPECT_FALSE(slowFlag);
74 slowFlag = YES;
75 };
76 runner.delayBetweenBlocks = 0.01;
77
78 // Action.
79 [runner enqueueBlockNamed:@"quick block" block:quickBlock];
80 [runner enqueueBlockNamed:@"slow block" block:slowBlock];
81
82 // Test.
83 base::test::ios::WaitUntilCondition(quickBlockRun);
84 EXPECT_TRUE(quickFlag);
85 EXPECT_FALSE(slowFlag);
86 EXPECT_EQ(1U, [runner numberOfBlocksRemaining]);
87 [runner runBlockIfNecessary:@"quick block"];
88 [runner runBlockIfNecessary:@"slow block"];
89 EXPECT_TRUE(quickFlag);
90 EXPECT_TRUE(slowFlag);
91 EXPECT_EQ(0U, [runner numberOfBlocksRemaining]);
92 }
93
94 // Tests that a block is not executed when canceld and it is removed from the
pkl (ping after 24h if needed) 2016/08/09 13:22:57 s/canceld/cancelled/
gambard 2016/08/19 12:16:24 Done.
95 // remaining block list.
pkl (ping after 24h if needed) 2016/08/09 13:22:56 s/block/blocks/
gambard 2016/08/19 12:16:24 Done.
96 TEST(DeferredInitializationRunnerTest, TestCancelBlock) {
97 // Setup.
98 __block BOOL blockFinished = NO;
99 DeferredInitializationRunner* runner =
100 [DeferredInitializationRunner sharedInstance];
101 runner.delayBetweenBlocks = 0.01;
102
103 [runner enqueueBlockNamed:@"cancel me"
104 block:^() {
105 blockFinished = YES;
106 }];
107 ASSERT_EQ(1U, [runner numberOfBlocksRemaining]);
108
109 // Action.
110 [runner cancelBlockNamed:@"cancel me"];
111
112 // Test.
113 EXPECT_FALSE(blockFinished);
114 EXPECT_EQ(0U, [runner numberOfBlocksRemaining]);
115 }
116
117 // Tests that a canceled block will do nothing when run by name.
pkl (ping after 24h if needed) 2016/08/09 13:22:56 s/canceled/cancelled/
gambard 2016/08/19 12:16:24 Done.
118 TEST(DeferredInitializationRunnerTest, TestCancelledBlockDoNothing) {
119 // Setup.
120 __block BOOL blockFinished = NO;
121 DeferredInitializationRunner* runner =
122 [DeferredInitializationRunner sharedInstance];
123 runner.delayBetweenBlocks = 0.01;
124
125 [runner enqueueBlockNamed:@"cancel me"
126 block:^() {
127 blockFinished = YES;
128 }];
129
130 // Action.
131 [runner cancelBlockNamed:@"cancel me"];
132 [runner runBlockIfNecessary:@"cancel me"];
133
134 // Test: expect false, the block should never be executed because it was
pkl (ping after 24h if needed) 2016/08/09 13:22:57 The "expect false, " part seems unnecessary.
gambard 2016/08/19 12:16:24 It tests that the block has not been run after bei
135 // cancelled before it started running.
136 EXPECT_FALSE(blockFinished);
137 }
138
139 // Tests that adding a block with the same name as an existing block will
140 // override the existing one.
141 TEST(DeferredInitializationRunnerTest, TestSecondBlockInvalidatesFirst) {
142 // Setup.
143 __block int blockRunCount = 0;
144 ProceduralBlock runBlock = ^() {
145 ++blockRunCount;
146 };
147 DeferredInitializationRunner* runner =
148 [DeferredInitializationRunner sharedInstance];
149 runner.delayBetweenBlocks = 0.01;
150
151 // Action.
152 [runner enqueueBlockNamed:@"multiple" block:runBlock];
153 [runner enqueueBlockNamed:@"multiple" block:runBlock];
154
155 // Test: |runBlock| was executed only once.
156 EXPECT_EQ(1U, [runner numberOfBlocksRemaining]);
157 [runner runBlockIfNecessary:@"multiple"];
158 EXPECT_EQ(0U, [runner numberOfBlocksRemaining]);
159 EXPECT_EQ(1, blockRunCount);
160 }
OLDNEW
« no previous file with comments | « ios/chrome/app/deferred_initialization_runner.mm ('k') | ios/chrome/ios_chrome_tests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698