1 // Copyright (c) 2012 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 <gmock/gmock.h>
6 #include <gtest/gtest.h>
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/chromeos/imageburner/burn_manager.h"
12 namespace imageburner
{
15 using ::testing::AnyNumber
;
16 using ::testing::InSequence
;
18 const std::string kConfigFileWithNoHwidProperty
=
22 "url=http://image.bin.zip\n";
24 const std::string kConfigFileWithNoNameProperty
=
27 "url=http://some_image.bin.zip\n";
29 const std::string kConfigFileWithNoNewLineAtEnd
=
34 "url=http://image.bin.zip";
36 const std::string kSampleConfigFile
=
38 "hwid=block_no_name\n"
48 "url=http://image1.bin.zip\n"
57 "url=http://image2.bin.zip\n"
65 "url=http://image3.bin.zip\n"
68 "name=some_block_with_no_hwid\n"
71 "name=some_name_invalid_block\n" // Good line.
72 "version=version \n" // Trailing whitespace.
73 "hwid=hwid41=q\n" // Extra =.
75 "hwid= \n" // Blank property value.
77 "filesize=\n" // Empty property value.
80 "name=another_block_with_no_hwid\n"
83 TEST(BurnManagerTest
, ConfigFileTest
) {
84 scoped_ptr
<ConfigFile
> cf(new ConfigFile());
85 EXPECT_TRUE(cf
->empty());
87 cf
.reset(new ConfigFile(""));
88 EXPECT_TRUE(cf
->empty());
90 cf
.reset(new ConfigFile(kConfigFileWithNoNameProperty
));
91 EXPECT_TRUE(cf
->empty());
93 cf
.reset(new ConfigFile(kConfigFileWithNoHwidProperty
));
94 EXPECT_TRUE(cf
->empty());
96 cf
.reset(new ConfigFile(kConfigFileWithNoNewLineAtEnd
));
97 EXPECT_FALSE(cf
->empty());
98 EXPECT_EQ(1u, cf
->size());
99 EXPECT_EQ("http://image.bin.zip", cf
->GetProperty("url", "some_hwid"));
100 EXPECT_EQ("some_name", cf
->GetProperty("name", "some_hwid"));
102 cf
.reset(new ConfigFile(kSampleConfigFile
));
103 EXPECT_FALSE(cf
->empty());
105 EXPECT_EQ(4u, cf
->size());
107 EXPECT_EQ("", cf
->GetProperty("version", "block_no_name"));
109 EXPECT_EQ("some_name1", cf
->GetProperty("name", "hwid11"));
110 EXPECT_EQ("version1", cf
->GetProperty("version", "hwid12"));
111 EXPECT_EQ("", cf
->GetProperty("filesize", "hwid1_non_existent"));
112 EXPECT_EQ("http://image1.bin.zip", cf
->GetProperty("url", "hwid13"));
113 EXPECT_EQ("", cf
->GetProperty("hwid", "hwid11"));
114 EXPECT_EQ("", cf
->GetProperty("", "hwid12"));
115 EXPECT_EQ("", cf
->GetProperty("name", ""));
116 EXPECT_EQ("", cf
->GetProperty("some_name", "hwid11"));
117 EXPECT_EQ("url", cf
->GetProperty("file", "hwid11"));
119 EXPECT_EQ("http://image2.bin.zip", cf
->GetProperty("url", "hwid21"));
120 EXPECT_EQ("some_name2", cf
->GetProperty("name", "hwid23"));
122 EXPECT_EQ("http://image3.bin.zip", cf
->GetProperty("url", "hwid31"));
123 EXPECT_EQ("some_name3", cf
->GetProperty("name", "hwid31"));
125 EXPECT_EQ("some_name_invalid_block", cf
->GetProperty("name", "hwid42"));
126 // TODO(tbarzic): make this pass.
127 // EXPECT_EQ("version", cf->GetProperty("version", "hwid42"));
128 EXPECT_EQ("", cf
->GetProperty("filesize", "hwid42"));
129 EXPECT_EQ("", cf
->GetProperty("url", "hwid42"));
130 // TODO(tbarzic): make this pass.
131 // EXPECT_EQ("", cf->GetProperty(" ", "hwid42"));
132 EXPECT_EQ("", cf
->GetProperty("name", "hwid41"));
135 class MockStateMachineObserver
: public StateMachine::Observer
{
137 MOCK_METHOD1(OnBurnStateChanged
, void(StateMachine::State
));
138 MOCK_METHOD1(OnError
, void(int));
141 TEST(BurnManagerTest
, StateMachineNormalWorkflow
) {
142 scoped_ptr
<StateMachine
> state_machine(new StateMachine());
143 EXPECT_EQ(StateMachine::INITIAL
, state_machine
->state());
145 MockStateMachineObserver observer
;
146 state_machine
->AddObserver(&observer
);
147 EXPECT_CALL(observer
, OnBurnStateChanged(StateMachine::DOWNLOADING
))
149 .RetiresOnSaturation();
151 EXPECT_CALL(observer
, OnBurnStateChanged(StateMachine::BURNING
))
153 .RetiresOnSaturation();
155 EXPECT_CALL(observer
, OnBurnStateChanged(StateMachine::INITIAL
))
157 .RetiresOnSaturation();
159 EXPECT_FALSE(state_machine
->download_started());
160 EXPECT_FALSE(state_machine
->download_finished());
161 EXPECT_TRUE(state_machine
->new_burn_posible());
163 state_machine
->OnDownloadStarted();
165 EXPECT_EQ(StateMachine::DOWNLOADING
, state_machine
->state());
166 EXPECT_TRUE(state_machine
->download_started());
167 EXPECT_FALSE(state_machine
->download_finished());
168 EXPECT_FALSE(state_machine
->new_burn_posible());
170 state_machine
->OnDownloadFinished();
172 // TODO(tbarzic): make this pass.
173 // EXPECT_EQ(StateMachine::INITIAL, state_machine->state());
174 EXPECT_TRUE(state_machine
->download_started());
175 EXPECT_TRUE(state_machine
->download_finished());
176 EXPECT_FALSE(state_machine
->new_burn_posible());
178 state_machine
->OnBurnStarted();
180 EXPECT_EQ(StateMachine::BURNING
, state_machine
->state());
181 EXPECT_TRUE(state_machine
->download_started());
182 EXPECT_TRUE(state_machine
->download_finished());
183 EXPECT_FALSE(state_machine
->new_burn_posible());
185 state_machine
->OnSuccess();
187 EXPECT_EQ(StateMachine::INITIAL
, state_machine
->state());
188 EXPECT_TRUE(state_machine
->download_started());
189 EXPECT_TRUE(state_machine
->download_finished());
190 EXPECT_TRUE(state_machine
->new_burn_posible());
193 TEST(BurnManagerTest
, StateMachineError
) {
194 scoped_ptr
<StateMachine
> state_machine(new StateMachine());
196 MockStateMachineObserver observer
;
197 // We don't want state change to INITIAL due to error to be reported to
198 // observers. We use OnError for that.
199 EXPECT_CALL(observer
, OnBurnStateChanged(_
))
201 EXPECT_CALL(observer
, OnBurnStateChanged(StateMachine::INITIAL
))
204 InSequence error_calls
;
205 EXPECT_CALL(observer
, OnError(1234))
207 EXPECT_CALL(observer
, OnError(4321))
209 EXPECT_CALL(observer
, OnError(0))
212 state_machine
->AddObserver(&observer
);
214 state_machine
->OnDownloadStarted();
216 state_machine
->OnError(1234);
218 // If called before download finished, download flags should be reset.
219 EXPECT_FALSE(state_machine
->download_started());
220 EXPECT_EQ(state_machine
->state(), StateMachine::INITIAL
);
221 EXPECT_TRUE(state_machine
->new_burn_posible());
223 state_machine
->OnDownloadStarted();
224 state_machine
->OnDownloadFinished();
226 state_machine
->OnError(4321);
228 // If called after download finished, download flags should not be changed.
229 EXPECT_TRUE(state_machine
->download_started());
230 EXPECT_TRUE(state_machine
->download_finished());
231 EXPECT_EQ(state_machine
->state(), StateMachine::INITIAL
);
232 EXPECT_TRUE(state_machine
->new_burn_posible());
234 state_machine
->OnBurnStarted();
235 state_machine
->OnError(0);
237 EXPECT_EQ(state_machine
->state(), StateMachine::INITIAL
);
238 EXPECT_TRUE(state_machine
->new_burn_posible());
241 TEST(BurnManagerTest
, StateMachineObservers
) {
242 scoped_ptr
<StateMachine
> state_machine(new StateMachine());
244 MockStateMachineObserver observer1
, observer2
;
246 EXPECT_CALL(observer1
, OnBurnStateChanged(_
))
248 EXPECT_CALL(observer2
, OnBurnStateChanged(_
))
250 EXPECT_CALL(observer1
, OnError(_
))
252 EXPECT_CALL(observer2
, OnError(_
))
255 state_machine
->OnDownloadStarted();
256 state_machine
->OnError(1);
258 state_machine
->AddObserver(&observer1
);
259 state_machine
->AddObserver(&observer2
);
260 EXPECT_CALL(observer1
, OnBurnStateChanged(_
))
262 EXPECT_CALL(observer2
, OnBurnStateChanged(_
))
264 EXPECT_CALL(observer1
, OnError(_
))
266 EXPECT_CALL(observer2
, OnError(_
))
269 state_machine
->OnDownloadStarted();
270 state_machine
->OnError(1);
272 state_machine
->RemoveObserver(&observer1
);
273 EXPECT_CALL(observer1
, OnBurnStateChanged(_
))
275 EXPECT_CALL(observer2
, OnBurnStateChanged(_
))
277 EXPECT_CALL(observer1
, OnError(_
))
279 EXPECT_CALL(observer2
, OnError(_
))
281 state_machine
->OnDownloadStarted();
282 state_machine
->OnError(1);
285 } // namespace imageburner
286 } // namespace chromeos