summaryrefslogtreecommitdiffstats
path: root/chrome/installer/setup/install_worker_unittest.cc
blob: 8ff63c36d62310d0c4d2b84ef1a85119ff00e40a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/installer/setup/install_worker.h"

#include <vector>

#include "base/win/registry.h"
#include "base/version.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/installer/setup/setup_util.h"
#include "chrome/installer/util/create_reg_key_work_item.h"
#include "chrome/installer/util/delete_reg_key_work_item.h"
#include "chrome/installer/util/delete_tree_work_item.h"
#include "chrome/installer/util/helper.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/installation_state.h"
#include "chrome/installer/util/installer_state.h"
#include "chrome/installer/util/set_reg_value_work_item.h"
#include "chrome/installer/util/util_constants.h"
#include "chrome/installer/util/work_item_list.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gmock/include/gmock/gmock.h"

using base::win::RegKey;
using installer::InstallationState;
using installer::InstallerState;
using installer::Product;
using installer::ProductState;

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::AtLeast;
using ::testing::AtMost;
using ::testing::Bool;
using ::testing::Combine;
using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::StrCaseEq;
using ::testing::StrEq;
using ::testing::StrictMock;
using ::testing::Values;

// Mock classes to help with testing
//------------------------------------------------------------------------------

class MockWorkItemList : public WorkItemList {
 public:
  MockWorkItemList() {}

  MOCK_METHOD5(AddCopyTreeWorkItem,
               WorkItem*(const std::wstring&,
                         const std::wstring&,
                         const std::wstring&,
                         CopyOverWriteOption,
                         const std::wstring&));
  MOCK_METHOD1(AddCreateDirWorkItem, WorkItem* (const base::FilePath&));
  MOCK_METHOD3(AddCreateRegKeyWorkItem,
               WorkItem*(HKEY, const std::wstring&, REGSAM));
  MOCK_METHOD3(AddDeleteRegKeyWorkItem,
               WorkItem*(HKEY, const std::wstring&, REGSAM));
  MOCK_METHOD4(
      AddDeleteRegValueWorkItem,
      WorkItem*(HKEY, const std::wstring&, REGSAM, const std::wstring&));
  MOCK_METHOD3(AddDeleteTreeWorkItem,
               WorkItem*(const base::FilePath&,
                         const base::FilePath&,
                         const std::vector<base::FilePath>&));
  MOCK_METHOD2(AddDeleteTreeWorkItem,
               WorkItem*(const base::FilePath&, const base::FilePath&));
  MOCK_METHOD4(AddMoveTreeWorkItem,
               WorkItem*(const std::wstring&,
                         const std::wstring&,
                         const std::wstring&,
                         MoveTreeOption));
  // Workaround for gmock problems with disambiguating between string pointers
  // and DWORD.
  virtual WorkItem* AddSetRegValueWorkItem(HKEY a1,
                                           const std::wstring& a2,
                                           REGSAM a3,
                                           const std::wstring& a4,
                                           const std::wstring& a5,
                                           bool a6) {
    return AddSetRegStringValueWorkItem(a1, a2, a3, a4, a5, a6);
  }

  virtual WorkItem* AddSetRegValueWorkItem(HKEY a1,
                                           const std::wstring& a2,
                                           REGSAM a3,
                                           const std::wstring& a4,
                                           DWORD a5,
                                           bool a6) {
    return AddSetRegDwordValueWorkItem(a1, a2, a3, a4, a5, a6);
  }

  MOCK_METHOD6(AddSetRegStringValueWorkItem,
               WorkItem*(HKEY,
                         const std::wstring&,
                         REGSAM,
                         const std::wstring&,
                         const std::wstring&,
                         bool));
  MOCK_METHOD6(AddSetRegDwordValueWorkItem,
               WorkItem*(HKEY,
                         const std::wstring&,
                         REGSAM,
                         const std::wstring&,
                         DWORD,
                         bool));
  MOCK_METHOD3(AddSelfRegWorkItem, WorkItem* (const std::wstring&,
                                              bool,
                                              bool));
};

class MockProductState : public ProductState {
 public:
  // Takes ownership of |version|.
  void set_version(Version* version) { version_.reset(version); }
  void set_multi_install(bool multi) { multi_install_ = multi; }
  void set_brand(const std::wstring& brand) { brand_ = brand; }
  void set_eula_accepted(DWORD eula_accepted) {
    has_eula_accepted_ = true;
    eula_accepted_ = eula_accepted;
  }
  void clear_eula_accepted() { has_eula_accepted_ = false; }
  void set_usagestats(DWORD usagestats) {
    has_usagestats_ = true;
    usagestats_ = usagestats;
  }
  void clear_usagestats() { has_usagestats_ = false; }
  void set_oem_install(const std::wstring& oem_install) {
    has_oem_install_ = true;
    oem_install_ = oem_install;
  }
  void clear_oem_install() { has_oem_install_ = false; }
  void SetUninstallProgram(const base::FilePath& setup_exe) {
    uninstall_command_ = base::CommandLine(setup_exe);
  }
  void AddUninstallSwitch(const std::string& option) {
    uninstall_command_.AppendSwitch(option);
  }
};

// Okay, so this isn't really a mock as such, but it does add setter methods
// to make it easier to build custom InstallationStates.
class MockInstallationState : public InstallationState {
 public:
  // Included for testing.
  void SetProductState(bool system_install,
                       BrowserDistribution::Type type,
                       const ProductState& product_state) {
    ProductState& target = (system_install ? system_products_ :
        user_products_)[IndexFromDistType(type)];
    target.CopyFrom(product_state);
  }
};

class MockInstallerState : public InstallerState {
 public:
  void set_level(Level level) {
    InstallerState::set_level(level);
  }

  void set_operation(Operation operation) { operation_ = operation; }

  void set_state_key(const std::wstring& state_key) {
    state_key_ = state_key;
  }

  void set_state_type(BrowserDistribution::Type state_type) {
    state_type_ = state_type;
  }

  void set_package_type(PackageType type) {
    InstallerState::set_package_type(type);
  }
};

// The test fixture
//------------------------------------------------------------------------------

class InstallWorkerTest : public testing::Test {
 public:
  void SetUp() override {
    current_version_.reset(new Version("1.0.0.0"));
    new_version_.reset(new Version("42.0.0.0"));

    // Don't bother ensuring that these paths exist. Since we're just
    // building the work item lists and not running them, they shouldn't
    // actually be touched.
    archive_path_ =
        base::FilePath(L"C:\\UnlikelyPath\\Temp\\chrome_123\\chrome.7z");
    // TODO(robertshield): Take this from the BrowserDistribution once that
    // no longer depends on MasterPreferences.
    installation_path_ =
        base::FilePath(L"C:\\Program Files\\Google\\Chrome\\");
    src_path_ = base::FilePath(
        L"C:\\UnlikelyPath\\Temp\\chrome_123\\source\\Chrome-bin");
    setup_path_ = base::FilePath(
        L"C:\\UnlikelyPath\\Temp\\CR_123.tmp\\setup.exe");
    temp_dir_ = base::FilePath(L"C:\\UnlikelyPath\\Temp\\chrome_123");
  }

  void TearDown() override {}

  void MaybeAddBinariesToInstallationState(
      bool system_level,
      MockInstallationState* installation_state) {
    if (installation_state->GetProductState(
            system_level, BrowserDistribution::CHROME_BINARIES) == NULL) {
      MockProductState product_state;
      product_state.set_version(new Version(*current_version_));
      product_state.set_brand(L"TEST");
      product_state.set_multi_install(true);
      BrowserDistribution* dist =
          BrowserDistribution::GetSpecificDistribution(
              BrowserDistribution::CHROME_BINARIES);
      base::FilePath install_path =
          installer::GetChromeInstallPath(system_level, dist);
      product_state.SetUninstallProgram(
          install_path.AppendASCII(current_version_->GetString())
          .Append(installer::kInstallerDir)
          .Append(installer::kSetupExe));
      product_state.AddUninstallSwitch(installer::switches::kUninstall);
      product_state.AddUninstallSwitch(installer::switches::kMultiInstall);
      if (system_level)
        product_state.AddUninstallSwitch(installer::switches::kSystemLevel);
      installation_state->SetProductState(system_level,
                                          BrowserDistribution::CHROME_BINARIES,
                                          product_state);
    }
  }

  void AddChromeToInstallationState(
      bool system_level,
      bool multi_install,
      MockInstallationState* installation_state) {
    if (multi_install)
      MaybeAddBinariesToInstallationState(system_level, installation_state);
    MockProductState product_state;
    product_state.set_version(new Version(*current_version_));
    product_state.set_multi_install(multi_install);
    product_state.set_brand(L"TEST");
    product_state.set_eula_accepted(1);
    BrowserDistribution* dist =
        BrowserDistribution::GetSpecificDistribution(
            BrowserDistribution::CHROME_BROWSER);
    base::FilePath install_path =
        installer::GetChromeInstallPath(system_level, dist);
    product_state.SetUninstallProgram(
      install_path.AppendASCII(current_version_->GetString())
          .Append(installer::kInstallerDir)
          .Append(installer::kSetupExe));
    product_state.AddUninstallSwitch(installer::switches::kUninstall);
    if (system_level)
      product_state.AddUninstallSwitch(installer::switches::kSystemLevel);
    if (multi_install) {
      product_state.AddUninstallSwitch(installer::switches::kMultiInstall);
      product_state.AddUninstallSwitch(installer::switches::kChrome);
    }

    installation_state->SetProductState(system_level,
                                        BrowserDistribution::CHROME_BROWSER,
                                        product_state);
  }

  void AddChromeFrameToInstallationState(
      bool system_level,
      bool multi_install,
      MockInstallationState* installation_state) {
    if (multi_install)
      MaybeAddBinariesToInstallationState(system_level, installation_state);
    MockProductState product_state;
    product_state.set_version(new Version(*current_version_));
    product_state.set_multi_install(multi_install);
    BrowserDistribution* dist =
        BrowserDistribution::GetSpecificDistribution(
            multi_install ? BrowserDistribution::CHROME_BINARIES :
                BrowserDistribution::CHROME_FRAME);
    base::FilePath install_path =
        installer::GetChromeInstallPath(system_level, dist);
    product_state.SetUninstallProgram(
      install_path.AppendASCII(current_version_->GetString())
          .Append(installer::kInstallerDir)
          .Append(installer::kSetupExe));
    product_state.AddUninstallSwitch(installer::switches::kUninstall);
    product_state.AddUninstallSwitch(installer::switches::kChromeFrame);
    if (system_level)
      product_state.AddUninstallSwitch(installer::switches::kSystemLevel);
    if (multi_install)
      product_state.AddUninstallSwitch(installer::switches::kMultiInstall);

    installation_state->SetProductState(system_level,
                                        BrowserDistribution::CHROME_FRAME,
                                        product_state);
  }

  MockInstallationState* BuildChromeInstallationState(bool system_level,
                                                      bool multi_install) {
    scoped_ptr<MockInstallationState> installation_state(
        new MockInstallationState());
    AddChromeToInstallationState(system_level, multi_install,
                                 installation_state.get());
    return installation_state.release();
  }

  static MockInstallerState* BuildBasicInstallerState(
      bool system_install,
      bool multi_install,
      const InstallationState& machine_state,
      InstallerState::Operation operation) {
    scoped_ptr<MockInstallerState> installer_state(new MockInstallerState());

    InstallerState::Level level = system_install ?
        InstallerState::SYSTEM_LEVEL : InstallerState::USER_LEVEL;
    installer_state->set_level(level);
    installer_state->set_operation(operation);
    // Hope this next one isn't checked for now.
    installer_state->set_state_key(L"PROBABLY_INVALID_REG_PATH");
    installer_state->set_state_type(BrowserDistribution::CHROME_BROWSER);
    installer_state->set_package_type(multi_install ?
                                          InstallerState::MULTI_PACKAGE :
                                          InstallerState::SINGLE_PACKAGE);
    return installer_state.release();
  }

  static void AddChromeBinariesToInstallerState(
      const InstallationState& machine_state,
      MockInstallerState* installer_state) {
    if (!installer_state->is_multi_install()) {
      NOTREACHED();
      return;
    }
    if (installer_state->FindProduct(BrowserDistribution::CHROME_BINARIES))
      return;

    // Fresh install or upgrade?
    const ProductState* chrome_binaries =
        machine_state.GetProductState(installer_state->system_install(),
                                      BrowserDistribution::CHROME_BINARIES);
    if (chrome_binaries != NULL) {
      installer_state->AddProductFromState(BrowserDistribution::CHROME_BINARIES,
                                           *chrome_binaries);
    } else {
      BrowserDistribution* dist =
          BrowserDistribution::GetSpecificDistribution(
              BrowserDistribution::CHROME_BINARIES);
      scoped_ptr<Product> product(new Product(dist));
      product->SetOption(installer::kOptionMultiInstall, true);
      installer_state->AddProduct(&product);
    }
  }

  static void AddChromeToInstallerState(
      const InstallationState& machine_state,
      MockInstallerState* installer_state) {
    // Fresh install or upgrade?
    const ProductState* chrome =
        machine_state.GetProductState(installer_state->system_install(),
                                      BrowserDistribution::CHROME_BROWSER);
    if (chrome != NULL &&
        chrome->is_multi_install() == installer_state->is_multi_install()) {
      installer_state->AddProductFromState(BrowserDistribution::CHROME_BROWSER,
                                           *chrome);
    } else {
      BrowserDistribution* dist =
          BrowserDistribution::GetSpecificDistribution(
              BrowserDistribution::CHROME_BROWSER);
      scoped_ptr<Product> product(new Product(dist));
      if (installer_state->is_multi_install())
        product->SetOption(installer::kOptionMultiInstall, true);
      installer_state->AddProduct(&product);
    }
  }

  static void AddChromeFrameToInstallerState(
      const InstallationState& machine_state,
      MockInstallerState* installer_state) {
    // Fresh install or upgrade?
    const ProductState* cf =
        machine_state.GetProductState(installer_state->system_install(),
                                      BrowserDistribution::CHROME_FRAME);
    if (cf != NULL) {
      installer_state->AddProductFromState(BrowserDistribution::CHROME_FRAME,
                                           *cf);
    } else {
      BrowserDistribution* dist =
          BrowserDistribution::GetSpecificDistribution(
              BrowserDistribution::CHROME_FRAME);
      scoped_ptr<Product> product(new Product(dist));
      if (installer_state->is_multi_install())
        product->SetOption(installer::kOptionMultiInstall, true);
      installer_state->AddProduct(&product);
    }
  }

  static MockInstallerState* BuildChromeInstallerState(
      bool system_install,
      bool multi_install,
      const InstallationState& machine_state,
      InstallerState::Operation operation) {
    scoped_ptr<MockInstallerState> installer_state(
        BuildBasicInstallerState(system_install, multi_install, machine_state,
                                 operation));
    if (multi_install) {
      // We don't want to include Chrome Binaries for uninstall if the machine
      // has other products. For simplicity, we check Chrome Frame only.
      bool machine_has_other_products =
          machine_state.GetProductState(system_install,
              BrowserDistribution::CHROME_FRAME) != NULL;
      if (operation != InstallerState::UNINSTALL || !machine_has_other_products)
        AddChromeBinariesToInstallerState(machine_state, installer_state.get());
    }
    AddChromeToInstallerState(machine_state, installer_state.get());
    return installer_state.release();
  }

  static MockInstallerState* BuildChromeFrameInstallerState(
      bool system_install,
      bool multi_install,
      const InstallationState& machine_state,
      InstallerState::Operation operation) {
    // This method only works for installation/upgrade.
    DCHECK(operation != InstallerState::UNINSTALL);
    scoped_ptr<MockInstallerState> installer_state(
        BuildBasicInstallerState(system_install, multi_install, machine_state,
                                 operation));
    if (multi_install)
      AddChromeBinariesToInstallerState(machine_state, installer_state.get());
    AddChromeFrameToInstallerState(machine_state, installer_state.get());
    return installer_state.release();
  }

 protected:
  scoped_ptr<Version> current_version_;
  scoped_ptr<Version> new_version_;
  base::FilePath archive_path_;
  base::FilePath installation_path_;
  base::FilePath setup_path_;
  base::FilePath src_path_;
  base::FilePath temp_dir_;
};

// Tests
//------------------------------------------------------------------------------

TEST_F(InstallWorkerTest, TestInstallChromeSingleSystem) {
  const bool system_level = true;
  const bool multi_install = false;
  NiceMock<MockWorkItemList> work_item_list;

  const HKEY kRegRoot = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
  static const wchar_t kRegKeyPath[] = L"Software\\Chromium\\test";
  scoped_ptr<CreateRegKeyWorkItem> create_reg_key_work_item(
      WorkItem::CreateCreateRegKeyWorkItem(
          kRegRoot, kRegKeyPath, WorkItem::kWow64Default));
  scoped_ptr<SetRegValueWorkItem> set_reg_value_work_item(
      WorkItem::CreateSetRegValueWorkItem(
          kRegRoot, kRegKeyPath, WorkItem::kWow64Default, L"", L"", false));
  scoped_ptr<DeleteTreeWorkItem> delete_tree_work_item(
      WorkItem::CreateDeleteTreeWorkItem(base::FilePath(), base::FilePath(),
                                         std::vector<base::FilePath>()));
  scoped_ptr<DeleteRegKeyWorkItem> delete_reg_key_work_item(
      WorkItem::CreateDeleteRegKeyWorkItem(kRegRoot, kRegKeyPath,
                                           WorkItem::kWow64Default));

  scoped_ptr<InstallationState> installation_state(
      BuildChromeInstallationState(system_level, multi_install));

  scoped_ptr<InstallerState> installer_state(
      BuildChromeInstallerState(system_level, multi_install,
                                *installation_state,
                                InstallerState::SINGLE_INSTALL_OR_UPDATE));

  // Set up some expectations.
  // TODO(robertshield): Set up some real expectations.
  EXPECT_CALL(work_item_list, AddCopyTreeWorkItem(_, _, _, _, _))
      .Times(AtLeast(1));
  EXPECT_CALL(work_item_list, AddCreateRegKeyWorkItem(_, _, _))
      .WillRepeatedly(Return(create_reg_key_work_item.get()));
  EXPECT_CALL(work_item_list, AddSetRegStringValueWorkItem(_, _, _, _, _, _))
      .WillRepeatedly(Return(set_reg_value_work_item.get()));
  EXPECT_CALL(work_item_list, AddDeleteTreeWorkItem(_, _))
      .WillRepeatedly(Return(delete_tree_work_item.get()));
  EXPECT_CALL(work_item_list, AddDeleteRegKeyWorkItem(_, _, _))
      .WillRepeatedly(Return(delete_reg_key_work_item.get()));

  AddInstallWorkItems(*installation_state.get(),
                      *installer_state.get(),
                      setup_path_,
                      archive_path_,
                      src_path_,
                      temp_dir_,
                      current_version_.get(),
                      *new_version_.get(),
                      &work_item_list);
}

namespace {

const wchar_t old_elevation_key[] =
    L"SOFTWARE\\Microsoft\\Internet Explorer\\Low Rights\\ElevationPolicy\\"
    L"{6C288DD7-76FB-4721-B628-56FAC252E199}";

}  // namespace

// A test class for worker functions that manipulate the old IE low rights
// policies.
// Parameters:
// bool : system_level_
// bool : multi_install_
class OldIELowRightsTests : public InstallWorkerTest,
  public ::testing::WithParamInterface<std::tr1::tuple<bool, bool> > {
 protected:
  void SetUp() override {
    InstallWorkerTest::SetUp();

    const ParamType& param = GetParam();
    system_level_ = std::tr1::get<0>(param);
    multi_install_ = std::tr1::get<1>(param);
    root_key_ = system_level_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;

    installation_state_.reset(new MockInstallationState());
    AddChromeFrameToInstallationState(system_level_, multi_install_,
                                      installation_state_.get());
    installer_state_.reset(BuildBasicInstallerState(
        system_level_, multi_install_, *installation_state_,
        multi_install_ ? InstallerState::MULTI_UPDATE :
            InstallerState::SINGLE_INSTALL_OR_UPDATE));
    if (multi_install_)
      AddChromeBinariesToInstallerState(*installation_state_,
                                        installer_state_.get());
    AddChromeFrameToInstallerState(*installation_state_,
                                   installer_state_.get());
  }

  scoped_ptr<MockInstallationState> installation_state_;
  scoped_ptr<MockInstallerState> installer_state_;
  bool system_level_;
  bool multi_install_;
  HKEY root_key_;
};

TEST_P(OldIELowRightsTests, AddDeleteOldIELowRightsPolicyWorkItems) {
  StrictMock<MockWorkItemList> work_item_list;

  EXPECT_CALL(work_item_list,
              AddDeleteRegKeyWorkItem(root_key_, StrEq(old_elevation_key), _))
      .Times(1);

  AddDeleteOldIELowRightsPolicyWorkItems(*installer_state_.get(),
                                         &work_item_list);
}

INSTANTIATE_TEST_CASE_P(Variations, OldIELowRightsTests,
                        Combine(Bool(), Bool()));

TEST_F(InstallWorkerTest, GoogleUpdateWorkItemsTest) {
  const bool system_level = true;
  const bool multi_install = true;
  MockWorkItemList work_item_list;

  // Per-machine single-install Chrome is installed.
  scoped_ptr<MockInstallationState> installation_state(
      BuildChromeInstallationState(system_level, false));

  MockProductState cf_state;
  cf_state.set_version(new Version(*current_version_));
  cf_state.set_multi_install(false);

  // Per-machine single-install Chrome Frame is installed.
  installation_state->SetProductState(system_level,
      BrowserDistribution::CHROME_FRAME, cf_state);

  // Prepare per-machine multi-install Chrome for installation.
  scoped_ptr<MockInstallerState> installer_state(
      BuildChromeInstallerState(system_level, multi_install,
                                *installation_state,
                                InstallerState::MULTI_INSTALL));

  // Expect the multi Client State key to be created for the binaries.
#if defined(GOOGLE_CHROME_BUILD)
  BrowserDistribution* multi_dist =
      BrowserDistribution::GetSpecificDistribution(
          BrowserDistribution::CHROME_BINARIES);
  std::wstring multi_app_guid(multi_dist->GetAppGuid());
  std::wstring multi_client_state_suffix(L"ClientState\\" + multi_app_guid);
  std::wstring multi_medium_suffix(L"ClientStateMedium\\" + multi_app_guid);

  // Expect ClientStateMedium to be created for system-level installs.
  EXPECT_CALL(work_item_list,
              AddCreateRegKeyWorkItem(_, HasSubstr(multi_medium_suffix), _))
      .Times(system_level ? 1 : 0);
#else
  std::wstring multi_client_state_suffix(L"Chromium Binaries");
#endif
  EXPECT_CALL(work_item_list, AddCreateRegKeyWorkItem(
                                  _, HasSubstr(multi_client_state_suffix), _))
      .Times(AnyNumber());

  // Expect to see a set value for the "TEST" brand code in the multi Client
  // State key.
  EXPECT_CALL(work_item_list, AddSetRegStringValueWorkItem(
                                  _, HasSubstr(multi_client_state_suffix), _,
                                  StrEq(google_update::kRegBrandField),
                                  StrEq(L"TEST"), _)).Times(1);

  // There may also be some calls to set 'ap' values.
  EXPECT_CALL(work_item_list, AddSetRegStringValueWorkItem(
                                  _, _, _, StrEq(google_update::kRegApField), _,
                                  _)).Times(AnyNumber());

  // Expect "oeminstall" to be cleared.
  EXPECT_CALL(work_item_list, AddDeleteRegValueWorkItem(
                                  _, HasSubstr(multi_client_state_suffix), _,
                                  StrEq(google_update::kRegOemInstallField)))
      .Times(1);

  // Expect "eulaaccepted" to set.
  EXPECT_CALL(work_item_list, AddSetRegDwordValueWorkItem(
                                  _, HasSubstr(multi_client_state_suffix), _,
                                  StrEq(google_update::kRegEULAAceptedField),
                                  Eq(static_cast<DWORD>(1)), _)).Times(1);

  AddGoogleUpdateWorkItems(*installation_state.get(),
                           *installer_state.get(),
                           &work_item_list);
}

// Test that usagestats values are migrated properly.
TEST_F(InstallWorkerTest, AddUsageStatsWorkItems) {
  const bool system_level = true;
  const bool multi_install = true;
  MockWorkItemList work_item_list;

  scoped_ptr<MockInstallationState> installation_state(
      BuildChromeInstallationState(system_level, multi_install));

  MockProductState chrome_state;
  chrome_state.set_version(new Version(*current_version_));
  chrome_state.set_multi_install(false);
  chrome_state.set_usagestats(1);

  installation_state->SetProductState(system_level,
      BrowserDistribution::CHROME_BROWSER, chrome_state);

  scoped_ptr<MockInstallerState> installer_state(
      BuildChromeInstallerState(system_level, multi_install,
                                *installation_state,
                                InstallerState::MULTI_INSTALL));

  // Expect the multi Client State key to be created.
  BrowserDistribution* multi_dist =
      BrowserDistribution::GetSpecificDistribution(
          BrowserDistribution::CHROME_BINARIES);
  std::wstring multi_app_guid(multi_dist->GetAppGuid());
  EXPECT_CALL(work_item_list, AddCreateRegKeyWorkItem(
                                  _, HasSubstr(multi_app_guid), _)).Times(1);

  // Expect to see a set value for the usagestats in the multi Client State key.
  EXPECT_CALL(work_item_list, AddSetRegDwordValueWorkItem(
                                  _, HasSubstr(multi_app_guid), _,
                                  StrEq(google_update::kRegUsageStatsField),
                                  Eq(static_cast<DWORD>(1)), Eq(true)))
      .Times(1);

  // Expect to see some values cleaned up from Chrome's keys.
  BrowserDistribution* chrome_dist =
      BrowserDistribution::GetSpecificDistribution(
          BrowserDistribution::CHROME_BROWSER);
  if (system_level) {
#if defined(GOOGLE_CHROME_BUILD)
    EXPECT_CALL(work_item_list,
                AddDeleteRegValueWorkItem(
                    _, StrEq(chrome_dist->GetStateMediumKey()), _,
                    StrEq(google_update::kRegUsageStatsField))).Times(1);
#endif
    EXPECT_CALL(work_item_list,
                AddDeleteRegValueWorkItem(
                    Eq(HKEY_CURRENT_USER), StrEq(chrome_dist->GetStateKey()), _,
                    StrEq(google_update::kRegUsageStatsField))).Times(1);
  }
#if defined(GOOGLE_CHROME_BUILD)
  const int kDeleteTimes = 1;
#else
  // Expect two deletes to the same key name since ClientState and
  // ClientStateMedium are identical for Chromium.
  const int kDeleteTimes = 2;
#endif
  EXPECT_CALL(
      work_item_list,
      AddDeleteRegValueWorkItem(
          Eq(installer_state->root_key()), StrEq(chrome_dist->GetStateKey()), _,
          StrEq(google_update::kRegUsageStatsField))).Times(kDeleteTimes);

  AddUsageStatsWorkItems(*installation_state.get(),
                         *installer_state.get(),
                         &work_item_list);
}

// The Quick Enable tests only make sense for the Google Chrome build as it
// interacts with registry values that are specific to Google Update.
#if defined(GOOGLE_CHROME_BUILD)

// Test scenarios under which the quick-enable-cf command should not exist after
// the run.  We're permissive in that we allow the DeleteRegKeyWorkItem even if
// it isn't strictly needed.
class QuickEnableAbsentTest : public InstallWorkerTest {
 public:
  virtual void SetUp() {
    InstallWorkerTest::SetUp();
    root_key_ = system_level_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
    delete_reg_key_item_.reset(WorkItem::CreateDeleteRegKeyWorkItem(
        root_key_, kRegKeyPath, WorkItem::kWow64Default));
    machine_state_.reset(new MockInstallationState());
    EXPECT_CALL(work_item_list_, AddDeleteRegKeyWorkItem(
                                     Eq(root_key_), StrCaseEq(kRegKeyPath), _))
        .Times(AtMost(1))
        .WillRepeatedly(Return(delete_reg_key_item_.get()));
  }
  virtual void TearDown() {
    machine_state_.reset();
    delete_reg_key_item_.reset();
    root_key_ = NULL;
    InstallWorkerTest::TearDown();
  }
 protected:
  static const bool system_level_ = false;
  static const wchar_t kRegKeyPath[];
  HKEY root_key_;
  scoped_ptr<DeleteRegKeyWorkItem> delete_reg_key_item_;
  scoped_ptr<MockInstallationState> machine_state_;
  StrictMock<MockWorkItemList> work_item_list_;
};

const wchar_t QuickEnableAbsentTest::kRegKeyPath[] =
    L"Software\\Google\\Update\\Clients\\"
    L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}\\Commands\\quick-enable-cf";

TEST_F(QuickEnableAbsentTest, CleanInstallSingleChrome) {
  // Install single Chrome on a clean system.
  scoped_ptr<MockInstallerState> installer_state(
      BuildBasicInstallerState(system_level_, true, *machine_state_,
                                InstallerState::MULTI_UPDATE));
  AddQuickEnableChromeFrameWorkItems(*installer_state, &work_item_list_);
}

TEST_F(InstallWorkerTest, WillProductBePresentAfterSetup) {
  BrowserDistribution::Type prod_type_list[] = {
    BrowserDistribution::CHROME_BROWSER,
    BrowserDistribution::CHROME_FRAME,
    // Excluding BrowserDistribution::CHROME_BINARIES, since it is installed
    // along with other products.
  };
  enum {  // Index into prod_type_list[].
    TYPE_BROWSER = 0,
    TYPE_CF,
    NUM_TYPE  // This must appear last.
  };
  DCHECK(arraysize(prod_type_list) == NUM_TYPE);
  InstallerState::Operation op_list[] = {
    InstallerState::UNINSTALL,
    InstallerState::SINGLE_INSTALL_OR_UPDATE
  };

  const bool system_level = false;
  const bool multi_install = true;

  // Loop over machine states: {No product, Chrome, CF, Chrome + CF}.
  for (int i_mach = 0; i_mach < (1 << NUM_TYPE); ++i_mach) {
    // i_mach is the machine state before operation, as bit mask.
    scoped_ptr<MockInstallationState> machine_state(
        new MockInstallationState());
    if ((i_mach & (1 << TYPE_BROWSER)) != 0) {  // Add Chrome.
      AddChromeToInstallationState(system_level, multi_install,
                                   machine_state.get());
    }
    if ((i_mach & (1 << TYPE_CF)) != 0) {  // Add Chrome Frame.
      AddChromeFrameToInstallationState(system_level, multi_install,
                                        machine_state.get());
    }

    // Loop over operations: {uninstall, install/update}.
    for (int i_op = 0; i_op < arraysize(op_list); ++i_op) {

      // Loop over product types to operate on: {TYPE_BROWSER, TYPE_CF}.
      for (int i_type_op = 0; i_type_op < NUM_TYPE; ++i_type_op) {
        scoped_ptr<InstallerState> installer_state;
        if (i_type_op == TYPE_BROWSER) {
          installer_state.reset(BuildChromeInstallerState(
              system_level, multi_install, *machine_state, op_list[i_op]));
        } else if (i_type_op == TYPE_CF) {
          // Skip the CF uninstall case due to limitations in
          // BuildChromeFrameInstallerState().
          if (op_list[i_op] == InstallerState::UNINSTALL)
            continue;

          installer_state.reset(BuildChromeFrameInstallerState(
              system_level, multi_install, *machine_state, op_list[i_op]));
        } else {
          NOTREACHED();
        }

        // Calculate the machine state after operation, as bit mask.
        // If uninstall, remove product with bitwise AND; else add with OR.
        int mach_after = (op_list[i_op] == InstallerState::UNINSTALL) ?
            i_mach & ~(1 << i_type_op) : i_mach | (1 << i_type_op);

        // Verify predicted presence of Chrome Binaries.
        bool bin_res = installer::WillProductBePresentAfterSetup(
            *installer_state,
            *machine_state,
            BrowserDistribution::CHROME_BINARIES);
        // Binaries are expected to be present iff any product is installed.
        bool bin_expect = mach_after != 0;
        EXPECT_EQ(bin_expect, bin_res);

        // Loop over product types to check: {TYPE_BROWSER, TYPE_CF}.
        for (int i_type_check = 0; i_type_check < NUM_TYPE; ++i_type_check) {
          // Verify predicted presence of product.
          bool prod_res = installer::WillProductBePresentAfterSetup(
              *installer_state,
              *machine_state,
              prod_type_list[i_type_check]);
          bool prod_expect = (mach_after & (1 << i_type_check)) != 0;
          EXPECT_EQ(prod_expect, prod_res);
        }
      }
    }
  }
}

#endif  // defined(GOOGLE_CHROME_BUILD)