1 // Copyright 2014 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 "chrome/browser/ui/webui/help/version_updater_chromeos.h"
8 #include "base/compiler_specific.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
12 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
13 #include "chrome/browser/chromeos/settings/cros_settings.h"
14 #include "chrome/browser/chromeos/settings/device_settings_service.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/dbus/fake_update_engine_client.h"
17 #include "chromeos/dbus/shill_service_client.h"
18 #include "chromeos/network/network_handler.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h"
24 using ::testing::AtLeast
;
25 using ::testing::Return
;
31 void CheckNotification(VersionUpdater::Status
/* status */,
33 const base::string16
& /* message */) {
38 class VersionUpdaterCrosTest
: public ::testing::Test
{
40 VersionUpdaterCrosTest()
41 : version_updater_(VersionUpdater::Create(nullptr)),
42 fake_update_engine_client_(NULL
),
43 mock_user_manager_(new MockUserManager()),
44 user_manager_enabler_(mock_user_manager_
) {}
46 ~VersionUpdaterCrosTest() override
{}
48 void SetUp() override
{
49 fake_update_engine_client_
= new FakeUpdateEngineClient();
50 scoped_ptr
<DBusThreadManagerSetter
> dbus_setter
=
51 DBusThreadManager::GetSetterForTesting();
52 dbus_setter
->SetUpdateEngineClient(
53 scoped_ptr
<UpdateEngineClient
>(fake_update_engine_client_
).Pass());
55 EXPECT_CALL(*mock_user_manager_
, IsCurrentUserOwner())
56 .WillRepeatedly(Return(false));
57 EXPECT_CALL(*mock_user_manager_
, Shutdown()).Times(AtLeast(0));
59 DeviceSettingsService::Initialize();
60 CrosSettings::Initialize();
62 NetworkHandler::Initialize();
63 ShillServiceClient::TestInterface
* service_test
=
64 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
65 service_test
->AddService("/service/eth",
68 shill::kTypeEthernet
, shill::kStateOnline
,
70 base::MessageLoop::current()->RunUntilIdle();
73 void TearDown() override
{
74 NetworkHandler::Shutdown();
76 CrosSettings::Shutdown();
77 DeviceSettingsService::Shutdown();
80 content::TestBrowserThreadBundle thread_bundle_
;
81 scoped_ptr
<VersionUpdater
> version_updater_
;
82 FakeUpdateEngineClient
* fake_update_engine_client_
; // Not owned.
84 MockUserManager
* mock_user_manager_
; // Not owned.
85 ScopedUserManagerEnabler user_manager_enabler_
;
87 DISALLOW_COPY_AND_ASSIGN(VersionUpdaterCrosTest
);
90 // The test checks following behaviour:
91 // 1. The device is currently on the dev channel and an user decides to switch
92 // to the beta channel.
93 // 2. In the middle of channel switch the user decides to switch to the stable
95 // 3. Update engine reports an error because downloading channel (beta) is not
97 // to the target channel (stable).
98 // 4. When update engine becomes idle downloading of the stable channel is
100 TEST_F(VersionUpdaterCrosTest
, TwoOverlappingSetChannelRequests
) {
101 version_updater_
->SetChannel("beta-channel", true);
104 UpdateEngineClient::Status status
;
105 status
.status
= UpdateEngineClient::UPDATE_STATUS_IDLE
;
106 fake_update_engine_client_
->set_default_status(status
);
107 fake_update_engine_client_
->NotifyObserversThatStatusChanged(status
);
110 EXPECT_EQ(0, fake_update_engine_client_
->request_update_check_call_count());
112 // IDLE -> DOWNLOADING transition after update check.
113 version_updater_
->CheckForUpdate(base::Bind(&CheckNotification
));
114 EXPECT_EQ(1, fake_update_engine_client_
->request_update_check_call_count());
117 UpdateEngineClient::Status status
;
118 status
.status
= UpdateEngineClient::UPDATE_STATUS_DOWNLOADING
;
119 status
.download_progress
= 0.1;
120 fake_update_engine_client_
->set_default_status(status
);
121 fake_update_engine_client_
->NotifyObserversThatStatusChanged(status
);
124 version_updater_
->SetChannel("stable-channel", true);
126 // DOWNLOADING -> REPORTING_ERROR_EVENT transition since target channel is not
127 // equal to downloading channel now.
129 UpdateEngineClient::Status status
;
130 status
.status
= UpdateEngineClient::UPDATE_STATUS_REPORTING_ERROR_EVENT
;
131 fake_update_engine_client_
->set_default_status(status
);
132 fake_update_engine_client_
->NotifyObserversThatStatusChanged(status
);
135 version_updater_
->CheckForUpdate(base::Bind(&CheckNotification
));
136 EXPECT_EQ(1, fake_update_engine_client_
->request_update_check_call_count());
138 // REPORTING_ERROR_EVENT -> IDLE transition, update check should be
139 // automatically scheduled.
141 UpdateEngineClient::Status status
;
142 status
.status
= UpdateEngineClient::UPDATE_STATUS_IDLE
;
143 fake_update_engine_client_
->set_default_status(status
);
144 fake_update_engine_client_
->NotifyObserversThatStatusChanged(status
);
147 EXPECT_EQ(2, fake_update_engine_client_
->request_update_check_call_count());
150 } // namespace chromeos