1// Copyright (c) 2011 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#include "base/memory/ref_counted.h"
6#include "base/utf_string_conversions.h"
7#include "chrome/browser/chromeos/cros/mock_update_library.h"
8#include "chrome/browser/chromeos/update_observer.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/browser_list.h"
11#include "chrome/common/url_constants.h"
12#include "chrome/test/automation/dom_element_proxy.h"
13#include "chrome/test/in_process_browser_test.h"
14#include "chrome/test/ui_test_utils.h"
15
16using ::testing::AnyNumber;
17using ::testing::InvokeWithoutArgs;
18using ::testing::Return;
19using ::testing::ReturnRef;
20using ::testing::_;
21using ::testing::Invoke;
22using ::testing::Exactly;
23
24namespace {
25
26typedef ObserverList<chromeos::UpdateLibrary::Observer> Observers;
27
28void CallObservers(chromeos::MockUpdateLibrary* lib,
29                   Observers* observers,
30                   const chromeos::UpdateLibrary::Status& x) {
31  EXPECT_CALL(*lib, status())
32    .Times(AnyNumber())
33    .WillRepeatedly((ReturnRef(x)))
34    .RetiresOnSaturation();
35  FOR_EACH_OBSERVER(chromeos::UpdateLibrary::Observer, *observers,
36                    UpdateStatusChanged(lib));
37}
38
39void FireSuccessSequence(chromeos::MockUpdateLibrary* lib,
40                         Observers* observer) {
41  chromeos::UpdateLibrary::Status status;
42
43  status.status = chromeos::UPDATE_STATUS_IDLE;
44  CallObservers(lib, observer, status);
45
46  status.status = chromeos::UPDATE_STATUS_CHECKING_FOR_UPDATE;
47  CallObservers(lib, observer, status);
48
49  status.status = chromeos::UPDATE_STATUS_UPDATE_AVAILABLE;
50  CallObservers(lib, observer, status);
51
52  status.status = chromeos::UPDATE_STATUS_DOWNLOADING;
53  status.download_progress = 10;
54  CallObservers(lib, observer, status);
55
56  status.status = chromeos::UPDATE_STATUS_DOWNLOADING;
57  status.download_progress = 50;
58  CallObservers(lib, observer, status);
59
60  status.status = chromeos::UPDATE_STATUS_DOWNLOADING;
61  status.download_progress = 90;
62  CallObservers(lib, observer, status);
63
64  status.status = chromeos::UPDATE_STATUS_VERIFYING;
65  CallObservers(lib, observer, status);
66
67  status.status = chromeos::UPDATE_STATUS_FINALIZING;
68  CallObservers(lib, observer, status);
69
70  status.status = chromeos::UPDATE_STATUS_UPDATED_NEED_REBOOT;
71  CallObservers(lib, observer, status);
72}
73
74void FireFailureSequence(chromeos::MockUpdateLibrary* lib,
75                         Observers* observer) {
76  chromeos::UpdateLibrary::Status status;
77
78  status.status = chromeos::UPDATE_STATUS_IDLE;
79  CallObservers(lib, observer, status);
80
81  status.status = chromeos::UPDATE_STATUS_CHECKING_FOR_UPDATE;
82  CallObservers(lib, observer, status);
83
84  status.status = chromeos::UPDATE_STATUS_UPDATE_AVAILABLE;
85  CallObservers(lib, observer, status);
86
87  status.status = chromeos::UPDATE_STATUS_DOWNLOADING;
88  status.download_progress = 10;
89  CallObservers(lib, observer, status);
90
91  status.status = chromeos::UPDATE_STATUS_ERROR;
92  status.download_progress = 10;
93  CallObservers(lib, observer, status);
94}
95
96class UpdateBrowserTest : public InProcessBrowserTest {
97 public:
98  UpdateBrowserTest() {}
99};
100
101IN_PROC_BROWSER_TEST_F(UpdateBrowserTest, Notifications) {
102  scoped_ptr<chromeos::MockUpdateLibrary> lib(
103      new chromeos::MockUpdateLibrary());
104
105  Observers observers;
106
107  EXPECT_CALL(*lib, AddObserver(_))
108      .WillRepeatedly(Invoke(&observers,
109                             &Observers::AddObserver));
110
111  chromeos::UpdateObserver* observe =
112      new chromeos::UpdateObserver(browser()->profile());
113  lib->AddObserver(observe);
114
115  FireSuccessSequence(lib.get(), &observers);
116  FireFailureSequence(lib.get(), &observers);
117}
118
119}  // namespace
120