chromium/chrome/browser/optimization_guide/prediction/prediction_model_store_browsertest.cc

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/ranges/algorithm.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/task_environment.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/optimization_guide/browser_test_util.h"
#include "chrome/browser/optimization_guide/chrome_prediction_model_store.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/optimization_guide/core/model_store_metadata_entry.h"
#include "components/optimization_guide/core/model_util.h"
#include "components/optimization_guide/core/optimization_guide_constants.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_switches.h"
#include "components/optimization_guide/core/prediction_manager.h"
#include "components/optimization_guide/core/prediction_model_fetch_timer.h"
#include "components/optimization_guide/proto/models.pb.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_switches.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

namespace optimization_guide {

namespace {

constexpr int kSuccessfulModelVersion =;

// Test locales.
constexpr char kTestLocaleFoo[] =;
constexpr char kTestLocaleBar[] =;

// Timeout to allow the model file to be downloaded, unzipped and sent to the
// model file observers.
constexpr base::TimeDelta kModelFileDownloadTimeout =;

Profile* CreateProfile() {}

proto::ModelCacheKey CreateModelCacheKey(const std::string& locale) {}

}  // namespace

class PredictionModelStoreBrowserTestBase : public InProcessBrowserTest {};

class PredictionModelStoreBrowserTest
    : public PredictionModelStoreBrowserTestBase {};

// TODO(crbug.com/329617221): Test is flaky on Win, Linux, and Mac ASan bots.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || \
    (BUILDFLAG(IS_MAC) && defined(ADDRESS_SANITIZER))
#define MAYBE_TestRegularProfile
#else
#define MAYBE_TestRegularProfile
#endif
IN_PROC_BROWSER_TEST_F(PredictionModelStoreBrowserTest,
                       MAYBE_TestRegularProfile) {}

// TODO(crbug.com/41490438): Re-enable this test
#if BUILDFLAG(IS_CHROMEOS) && defined(ADDRESS_SANITIZER)
#define MAYBE_TestIncognitoProfile
#else
#define MAYBE_TestIncognitoProfile
#endif
IN_PROC_BROWSER_TEST_F(PredictionModelStoreBrowserTest,
                       MAYBE_TestIncognitoProfile) {}

// Tests that two similar profiles share the model, and the model is not
// redownloaded.
IN_PROC_BROWSER_TEST_F(PredictionModelStoreBrowserTest,
                       TestSimilarProfilesShareModel) {}

// Tests that two dissimilar profiles do not share the model, and the model will
// be redownloaded.
IN_PROC_BROWSER_TEST_F(PredictionModelStoreBrowserTest,
                       TestDissimilarProfilesNotShareModel) {}

// Tests that two similar profiles share the model, and the model is not
// redownloaded, based on server returned model cache key.
IN_PROC_BROWSER_TEST_F(PredictionModelStoreBrowserTest,
                       TestSimilarProfilesShareModelWithServerModelCacheKey) {}

// Tests that two dissimilar profiles do not share the model, and the model will
// be redownloaded, based on server returned model cache key.
IN_PROC_BROWSER_TEST_F(
    PredictionModelStoreBrowserTest,
    TestDissimilarProfilesNotShareModelWithServerModelCacheKey) {}

// Tests that when a second similar profile is loaded, model is downloaded when
// the model version has been updated. The old model should not be used.
IN_PROC_BROWSER_TEST_F(PredictionModelStoreBrowserTest,
                       TestSimilarProfilesOnModelVersionUpdate) {}

IN_PROC_BROWSER_TEST_F(PredictionModelStoreBrowserTest,
                       PRE_TestOldModelRemovedOnModelUpdate) {}

IN_PROC_BROWSER_TEST_F(PredictionModelStoreBrowserTest,
                       TestOldModelRemovedOnModelUpdate) {}

// Tests the case when local state is inconsistent with the model directory,
// i.e., when model file does not exist but the local state entry is populated,
// it will lead to redownloading of the model.
IN_PROC_BROWSER_TEST_F(PredictionModelStoreBrowserTest,
                       TestInconsistentLocalState) {}

}  // namespace optimization_guide